-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic pool implementation and process separation per worker id #10
Open
wavewave
wants to merge
14
commits into
main
Choose a base branch
from
wavewave/worker-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a6890cb
pass requestWorkerId
wavewave 274c94f
Pool status now has Maybe Id.
wavewave 25769ef
refactor out Pool module
wavewave a937dc6
getAssignableWorker
wavewave 8283168
requestWorkerId to Maybe Id
wavewave f42014a
take --worker-close flag
wavewave 62f44ff
remove assigned id from workers
wavewave b07879a
work factored out from serve
wavewave b082e62
can run initWorker dynamically
wavewave a96eced
wrap up spawnWorker
wavewave 80cec7f
dynamically increase the workers
wavewave 6741dd9
fix dead lock. assiging job should be reinvoked after process spawning.
wavewave c6b892b
hold ProcessHandle in HandleSet
wavewave dcc97aa
terminating processes after finishing linking is implemented!
wavewave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} | ||
|
||
module Pool | ||
( HandleSet (..), | ||
Pool (..), | ||
dumpStatus, | ||
assignJob, | ||
finishJob, | ||
removeWorker, | ||
) where | ||
|
||
import Control.Concurrent.STM (STM, TVar, atomically, readTVar, retry, writeTVar) | ||
import qualified Data.Foldable as F | ||
import Data.IntMap (IntMap) | ||
import qualified Data.IntMap as IM | ||
import qualified Data.List as List | ||
import Message (Id) | ||
import System.IO (Handle, hFlush, hPrint, hPutStrLn, stdout) | ||
import System.Process (ProcessHandle, terminateProcess) | ||
|
||
data HandleSet = HandleSet | ||
{ handleProcess :: ProcessHandle, | ||
handleArgIn :: Handle, | ||
handleMsgOut :: Handle | ||
} | ||
|
||
data Pool = Pool | ||
{ poolLimit :: Int, | ||
poolNext :: Int, | ||
poolStatus :: IntMap (Bool, Maybe Id), | ||
poolHandles :: [(Int, HandleSet)] | ||
} | ||
|
||
dumpStatus :: TVar Pool -> IO () | ||
dumpStatus ref = do | ||
pool <- atomically (readTVar ref) | ||
hPutStrLn stdout $ "poolLimit = " ++ show (poolLimit pool) | ||
mapM_ (hPrint stdout) $ IM.toAscList (poolStatus pool) | ||
hFlush stdout | ||
|
||
getAssignableWorker :: IntMap (Bool, Maybe Id) -> Maybe Id -> Maybe (Int, (Bool, Maybe Id)) | ||
getAssignableWorker workers mid' = List.find (isAssignable . snd) . IM.toAscList $ workers | ||
where | ||
isAssignable (b, mid) | ||
| not b = case (mid, mid') of | ||
(Nothing, _) -> True | ||
(Just _, Nothing) -> True | ||
(Just id'', Just id') -> id' == id'' | ||
| otherwise = False | ||
|
||
assignJob :: | ||
TVar Pool -> | ||
Maybe Id -> | ||
-- | Right assigned, Left new id that will be used for new spawned worker process. | ||
STM (Either Int (Int, HandleSet)) | ||
assignJob ref mid' = do | ||
pool <- readTVar ref | ||
let workers = poolStatus pool | ||
let m = getAssignableWorker workers mid' | ||
case m of | ||
Nothing -> do | ||
let nRunningJobs = length $ filter (\(b, _) -> b) $ F.toList workers | ||
if (nRunningJobs >= poolLimit pool) | ||
then retry | ||
else pure (Left nRunningJobs) | ||
Just (i, _) -> do | ||
let upd (_, Nothing) = Just (True, mid') | ||
upd (_, Just id'') = Just (True, Just id'') | ||
!workers' = IM.update upd i workers | ||
Just hset = List.lookup i (poolHandles pool) | ||
writeTVar ref (pool {poolStatus = workers'}) | ||
pure $ Right (i, hset) | ||
|
||
finishJob :: TVar Pool -> Int -> STM () | ||
finishJob ref i = do | ||
pool <- readTVar ref | ||
let workers = poolStatus pool | ||
!workers' = IM.update (\(_, m) -> Just (False, m)) i workers | ||
writeTVar ref (pool {poolStatus = workers'}) | ||
|
||
removeWorker :: TVar Pool -> Id -> IO () | ||
removeWorker ref id' = do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe |
||
dismissedHandles <- | ||
atomically $ do | ||
pool <- readTVar ref | ||
let workers = poolStatus pool | ||
(dismissed, remained) = IM.partition (\(_, m) -> m == Just id') workers | ||
let ks = IM.keys dismissed | ||
(dismissedHandles, remainedHandles) = List.partition ((`elem` ks) . fst) $ poolHandles pool | ||
writeTVar ref (pool {poolStatus = remained, poolHandles = remainedHandles}) | ||
pure (fmap (handleProcess . snd) dismissedHandles) | ||
|
||
mapM_ terminateProcess dismissedHandles | ||
-- atomically $ removeWorker ref id' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW,
Foldable.find @IntMap
searches in ascending key order 🙂