git.iko.soy/distributed-process
on master help sign in
Remove monitors from remote nodes. (#295)

When a process dies, we need to remove it from the monitor lists of any nodes where there is a process being monitored by the dead process.

commit89c0030259d534baccc0f118be17039fcc0f4916 authorFacundo Domínguez <facundominguez@gmail.com> dateFri 26 Aug 2016, 16:06 (10 years ago) parent4fceb7a
2 files changed+25−9
src/Control/Distributed/Process/Internal/BiMultiMap.hs+8−5 src/Control/Distributed/Process/Node.hs+17−4
src/Control/Distributed/Process/Internal/BiMultiMap.hs+8−5
@@ -5,7 +5,8 @@ module Control.Distributed.Process.Internal.BiMultiMap
55 , singleton
66 , size
77 , insert
8− , lookup
8+ , lookupBy1st
9+ , lookupBy2nd
910 , delete
1011 , deleteAllBy1st
1112 , deleteAllBy2nd
@@ -62,10 +63,12 @@ insert a b v (BiMultiMap m r) =
6263 r)
6364
6465-- | Looks up all the triplets whose first component is the given value.
65−--
66−-- See 'flip' in order to look up by the second component.
67−lookup :: Ord a => a -> BiMultiMap a b v -> Set (b, v)
68−lookup a (BiMultiMap m _) = maybe Set.empty id $ Map.lookup a m
66+lookupBy1st :: Ord a => a -> BiMultiMap a b v -> Set (b, v)
67+lookupBy1st a (BiMultiMap m _) = maybe Set.empty id $ Map.lookup a m
68+
69+-- | Looks up all the triplets whose second component is the given value.
70+lookupBy2nd :: Ord b => b -> BiMultiMap a b v -> Set (a, v)
71+lookupBy2nd b = lookupBy1st b . flip
6972
7073-- | Deletes a triplet. It yields the original multimap if the triplet is
7174-- not present.
src/Control/Distributed/Process/Node.hs+17−4
@@ -37,6 +37,7 @@ import qualified Data.Map as Map
3737 , filterWithKey
3838 , foldlWithKey
3939 )
40+import qualified Data.Set as Set
4041import Data.Time.Format (formatTime)
4142#if MIN_VERSION_time(1,5,0)
4243import Data.Time.Format (defaultTimeLocale)
@@ -879,6 +880,18 @@ ncEffectDied ident reason = do
879880 when (localOnly <= isLocal node (ProcessIdentifier us)) $
880881 notifyDied us them reason (Just ref)
881882
883+ -- Notify remote nodes that the process died so it can be removed from monitor
884+ -- lists.
885+ mapM_ (forwardDeath node) $
886+ [ nid | ProcessIdentifier pid <- [ident]
887+ , i <- Set.toList $ Set.union
888+ (Set.map fst $ BiMultiMap.lookupBy2nd pid unaffectedLinks)
889+ (Set.map fst $ BiMultiMap.lookupBy2nd pid unaffectedMons)
890+ , let nid = nodeOf i
891+ , nid /= localNodeId node
892+ ]
893+
894+ -- Delete monitors in the local node.
882895 let deleteDeads :: (Ord a, Ord v)
883896 => BiMultiMap a ProcessId v -> BiMultiMap a ProcessId v
884897 deleteDeads = case ident of
@@ -899,12 +912,12 @@ ncEffectDied ident reason = do
899912 True ->
900913 do forM_ nidlist $ \(nid,_) ->
901914 when (not $ isLocal node (NodeIdentifier nid))
902− (forwardNameDeath node nid)
915+ (forwardDeath node nid)
903916 return Nothing
904917 False -> return $ Just (pid,nidlist) )
905918 modify' $ registeredOnNodes ^= (Map.fromList (catMaybes remaining))
906919 where
907− forwardNameDeath node nid = ncSendToNode nid
920+ forwardDeath node nid = ncSendToNode nid
908921 NCMsg { ctrlMsgSender = NodeIdentifier (localNodeId node)
909922 , ctrlMsgSignal = Died ident reason
910923 }
@@ -1059,9 +1072,9 @@ ncEffectGetInfo from pid =
10591072 Nothing -> dispatch (isLocal node (ProcessIdentifier from))
10601073 from (ProcessInfoNone DiedUnknownId)
10611074 Just proc -> do
1062− itsLinks <- Set.map fst . BiMultiMap.lookup them <$>
1075+ itsLinks <- Set.map fst . BiMultiMap.lookupBy1st them <$>
10631076 gets (^. links)
1064− itsMons <- BiMultiMap.lookup them <$> gets (^. monitors)
1077+ itsMons <- BiMultiMap.lookupBy1st them <$> gets (^. monitors)
10651078 registered <- gets (^. registeredHere)
10661079 size <- liftIO $ queueSize $ processQueue $ proc
10671080