2{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE BangPatterns #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7{-# LANGUAGE RecordWildCards #-}
11module Control.Distributed.Process.Node
21-- TODO: Calls to 'sendBinary' and co (by the NC) may stall the node controller.
23#if ! MIN_VERSION_base(4,6,0)
24import Prelude hiding (catch)
27import System.IO (fixIO, hPutStrLn, stderr)
28import System.Mem.Weak (Weak, deRefWeak)
29import qualified Data.ByteString.Lazy as BSL (fromChunks)
30import Data.Binary (decode)
32import qualified Data.Map as Map
44import qualified Data.Set as Set
51import Data.Foldable (forM_)
52import Data.Maybe (isJust, fromJust, isNothing, catMaybes)
53import Data.Typeable (Typeable)
54import Control.Category ((>>>))
55import Control.Applicative (Applicative, (<$>))
56import Control.Monad (void, when, join)
57import Control.Monad.IO.Class (MonadIO, liftIO)
58import Control.Monad.State.Strict (MonadState, StateT, evalStateT, gets)
59import qualified Control.Monad.State.Strict as StateT (get, put)
60import Control.Monad.Reader (MonadReader, ReaderT, runReaderT, ask)
61import Control.Exception
63 , AsyncException(ThreadKilled)
67 , uninterruptibleMask_
69import qualified Control.Exception as Exception
75import Control.Concurrent (forkIO, forkIOWithUnmask, killThread, myThreadId)
76import Control.Distributed.Process.Internal.StrictMVar
86import Control.Concurrent.Chan (newChan, writeChan, readChan)
87import qualified Control.Concurrent.MVar as MVar (newEmptyMVar, takeMVar)
88import Control.Concurrent.STM
91import Control.Distributed.Process.Internal.CQueue
98import qualified Network.Transport as NT
112 , Reliability(ReliableOrdered)
114import Data.Accessor (Accessor, accessor, (^.), (^=), (^:))
115import System.Random (randomIO)
116import Control.Distributed.Static (RemoteTable, Closure)
117import qualified Control.Distributed.Static as Static
121import Control.Distributed.Process.Internal.Types
128 , ValidLocalNodeState(..)
129 , withValidLocalState
130 , modifyValidLocalState
132 , LocalProcessState(..)
144 , NodeClosedException(..)
145 , ProcessMonitorNotification(..)
146 , NodeMonitorNotification(..)
147 , PortMonitorNotification(..)
148 , ProcessExitException(..)
149 , ProcessLinkException(..)
150 , NodeLinkException(..)
151 , PortLinkException(..)
153 , DidUnlinkProcess(..)
163 , ProcessInfoNone(..)
171 , createUnencodedMessage
172 , unsafeCreateUnencodedMessage
174 , firstNonReservedProcessId
175 , ImplicitReconnect(WithImplicitReconnect,NoImplicitReconnect)
177import Control.Distributed.Process.Management.Internal.Agent
180import qualified Control.Distributed.Process.Management.Internal.Table as Table
182 , startTableCoordinator
184import qualified Control.Distributed.Process.Management.Internal.Trace.Remote as Trace
187import Control.Distributed.Process.Management.Internal.Trace.Tracer
190import Control.Distributed.Process.Management.Internal.Trace.Types
196import Control.Distributed.Process.Management.Internal.Types
199import Control.Distributed.Process.Serializable (Serializable)
200import Control.Distributed.Process.Internal.Messaging
203 , closeImplicitReconnections
206import Control.Distributed.Process.Internal.Primitives
214import Control.Distributed.Process.Internal.Types (SendPort, Tracer(..))
215import qualified Control.Distributed.Process.Internal.Closure.BuiltIn as BuiltIn (remoteTable)
216import Control.Distributed.Process.Internal.WeakTQueue (TQueue, writeTQueue)
217import qualified Control.Distributed.Process.Internal.StrictContainerAccessors as DAC
223--------------------------------------------------------------------------------
225--------------------------------------------------------------------------------
227initRemoteTable :: RemoteTable
228initRemoteTable = Trace.remoteTable $ BuiltIn.remoteTable Static.initRemoteTable
230-- | Initialize a new local node.
231newLocalNode :: NT.Transport -> RemoteTable -> IO LocalNode
232newLocalNode transport rtable = do
233 mEndPoint <- NT.newEndPoint transport
235 Left ex -> throwIO ex
237 localNode <- createBareLocalNode endPoint rtable
238 startServiceProcesses localNode
241-- | Create a new local node (without any service processes running)
242createBareLocalNode :: NT.EndPoint -> RemoteTable -> IO LocalNode
243createBareLocalNode endPoint rtable = do
245 state <- newMVar $ LocalNodeValid $ ValidLocalNodeState
246 { _localProcesses = Map.empty
247 , _localPidCounter = firstNonReservedProcessId
248 , _localPidUnique = unq
249 , _localConnections = Map.empty
252 let node = LocalNode { localNodeId = NodeId $ NT.address endPoint
253 , localEndPoint = endPoint
255 , localCtrlChan = ctrlChan
256 , localEventBus = MxEventBusInitialising
257 , remoteTable = rtable
259 tracedNode <- startMxAgent node
261 -- Once the NC terminates, the endpoint isn't much use,
262 void $ forkIO $ Exception.finally (runNodeController tracedNode)
263 (NT.closeEndPoint (localEndPoint node))
265 -- whilst a closed/failing endpoint will terminate the NC
266 void $ forkIO $ Exception.finally (handleIncomingMessages tracedNode)
272 writeChan (localCtrlChan node) NCMsg
273 { ctrlMsgSender = NodeIdentifier (localNodeId node)
274 , ctrlMsgSignal = SigShutdown
277startMxAgent :: LocalNode -> IO LocalNode
278startMxAgent node = do
279 -- see note [tracer/forkProcess races]
280 let fork = forkProcess node
281 mv <- MVar.newEmptyMVar
282 pid <- fork $ mxAgentController fork mv
283 (tracer', wqRef, mxNew') <- MVar.takeMVar mv
284 return node { localEventBus = (MxEventBus pid tracer' wqRef mxNew') }
286startDefaultTracer :: LocalNode -> IO ()
287startDefaultTracer node' = do
288 let t = localEventBus node'
290 MxEventBus _ (Tracer pid _) _ _ -> do
291 runProcess node' $ register "trace.controller" pid
292 pid' <- forkProcess node' defaultTracer
293 enableTrace (localEventBus node') pid'
294 runProcess node' $ register "tracer.initial" pid'
297-- TODO: we need a better mechanism for defining and registering services
299-- | Start and register the service processes on a node
300startServiceProcesses :: LocalNode -> IO ()
301startServiceProcesses node = do
302 -- tracing /spawns/ relies on the tracer being enabled, but we start
303 -- the default tracer first, even though it might @nsend@ to the logger
304 -- before /that/ process has started - this is a totally harmless race
305 -- however, so we deliberably ignore it
306 startDefaultTracer node
307 tableCoordinatorPid <- fork $ Table.startTableCoordinator fork
308 runProcess node $ register Table.mxTableCoordinator tableCoordinatorPid
309 logger <- forkProcess node loop
311 register "logger" logger
312 -- The trace.logger is used for tracing to the console to avoid feedback
313 -- loops during tracing if the user reregisters the "logger" with a custom
314 -- process which uses 'send' or other primitives which are traced.
315 register "trace.logger" logger
317 fork = forkProcess node
321 [ match $ \((time, pid, string) ::(String, ProcessId, String)) -> do
322 liftIO . hPutStrLn stderr $ time ++ " " ++ show pid ++ ": " ++ string
324 , match $ \((time, string) :: (String, String)) -> do
325 -- this is a 'trace' message from the local node tracer
326 liftIO . hPutStrLn stderr $ time ++ " [trace] " ++ string
328 , match $ \(ch :: SendPort ()) -> -- a shutdown request
332-- | Force-close a local node, killing all processes on that node.
333closeLocalNode :: LocalNode -> IO ()
334closeLocalNode node = do
335 -- Kill processes after refilling the mvar. Otherwise, there is potential for
336 -- deadlock as a dying process tries to get the mvar while masking exceptions
338 join $ modifyMVar (localState node) $ \st -> case st of
339 LocalNodeValid vst -> do
340 return ( LocalNodeClosed
341 , forM_ (vst ^. localProcesses) $ \lproc ->
342 -- Semantics of 'throwTo' guarantee that target thread will get
343 -- delivered an exception. Therefore, target thread will be
344 -- killed eventually and that's as good as we can do. No need
345 -- to wait for thread to actually finish dying.
346 killThread (processThread lproc)
348 LocalNodeClosed -> return (LocalNodeClosed, return ())
349 -- This call will have the effect of shutting down the NC as well (see
350 -- 'createBareLocalNode').
351 NT.closeEndPoint (localEndPoint node)
353-- | Run a process on a local node and wait for it to finish
354runProcess :: LocalNode -> Process () -> IO ()
355runProcess node proc = do
357 -- TODO; When forkProcess inherits the masking state, protect the forked
358 -- thread against async exceptions that could occur before 'try' is evaluated.
359 void $ forkProcess node $ try proc >>= liftIO . putMVar done
360 takeMVar done >>= either (throwIO :: SomeException -> IO a) return
362-- | Spawn a new process on a local node
363forkProcess :: LocalNode -> Process () -> IO ProcessId
364forkProcess node proc =
365 modifyMVarMasked (localState node) startProcess
367 startProcess :: LocalNodeState -> IO (LocalNodeState, ProcessId)
368 startProcess (LocalNodeValid vst) = do
369 let lpid = LocalProcessId { lpidCounter = vst ^. localPidCounter
370 , lpidUnique = vst ^. localPidUnique
372 let pid = ProcessId { processNodeId = localNodeId node
373 , processLocalId = lpid
375 pst <- newMVar LocalProcessState { _monitorCounter = 0
377 , _channelCounter = 0
378 , _typedChannels = Map.empty
381 weakQueue <- mkWeakCQueue queue (return ())
382 (_, lproc) <- fixIO $ \ ~(tid, _) -> do
383 let lproc = LocalProcess { processQueue = queue
384 , processWeakQ = weakQueue
387 , processThread = tid
390 tid' <- uninterruptibleMask_ $ forkIOWithUnmask $ \unmask -> do
391 reason <- Exception.catches
392 (unmask $ runLocalProcess lproc proc >> return DiedNormal)
393 [ (Exception.Handler (\ex@(ProcessExitException from msg) -> do
394 mMsg <- unwrapMessage msg :: IO (Maybe String)
396 Nothing -> return $ DiedException $ show ex
397 Just m -> return $ DiedException ("exit-from=" ++ (show from) ++ ",reason=" ++ m)))
399 (return . DiedException . (show :: SomeException -> String)))]
401 -- [Unified: Table 4, rules termination and exiting]
402 mconns <- modifyValidLocalState node (cleanupProcess pid)
403 -- XXX: Revisit after agreeing on the bigger picture for the semantics
404 -- of transport operations.
405 -- https://github.com/haskell-distributed/distributed-process/issues/204
406 forM_ mconns $ forkIO . mapM_ NT.close
408 writeChan (localCtrlChan node) NCMsg
409 { ctrlMsgSender = ProcessIdentifier pid
410 , ctrlMsgSignal = Died (ProcessIdentifier pid) reason
414 -- see note [tracer/forkProcess races]
415 trace node (MxSpawned pid)
417 if lpidCounter lpid == maxBound
419 -- TODO: this doesn't look right at all - how do we know
420 -- that newUnique represents a process id that is available!?
421 newUnique <- randomIO
422 return ( LocalNodeValid
423 $ (localProcessWithId lpid ^= Just lproc)
424 . (localPidCounter ^= firstNonReservedProcessId)
425 . (localPidUnique ^= newUnique)
430 return ( LocalNodeValid
431 $ (localProcessWithId lpid ^= Just lproc)
432 . (localPidCounter ^: (+ 1))
436 startProcess LocalNodeClosed =
437 throwIO $ NodeClosedException $ localNodeId node
439 cleanupProcess :: ProcessId
440 -> ValidLocalNodeState
441 -> IO (ValidLocalNodeState, [NT.Connection])
442 cleanupProcess pid vst = do
443 let pid' = ProcessIdentifier pid
444 let (affected, unaffected) = Map.partitionWithKey (\(fr, _to) !_v -> impliesDeathOf pid' fr) (vst ^. localConnections)
445 return ( (localProcessWithId (processLocalId pid) ^= Nothing)
446 . (localConnections ^= unaffected)
448 , map fst $ Map.elems affected
451-- note [tracer/forkProcess races]
453-- Our startTracing function uses forkProcess to start the trace controller
454-- process, and of course forkProcess attempts to call traceEvent once the
455-- process has started. This is harmless, as the localEventBus is not updated
456-- until /after/ the initial forkProcess completes, so the first call to
457-- traceEvent behaves as if tracing were disabled (i.e., it is ignored).
460--------------------------------------------------------------------------------
461-- Handle incoming messages --
462--------------------------------------------------------------------------------
464type IncomingConnection = (NT.EndPointAddress, IncomingTarget)
468 | ToProc ProcessId (Weak (CQueue Message))
469 | ToChan TypedChannel
472data ConnectionState = ConnectionState {
473 _incoming :: !(Map NT.ConnectionId IncomingConnection)
474 , _incomingFrom :: !(Map NT.EndPointAddress (Set NT.ConnectionId))
477initConnectionState :: ConnectionState
478initConnectionState = ConnectionState {
479 _incoming = Map.empty
480 , _incomingFrom = Map.empty
483incoming :: Accessor ConnectionState (Map NT.ConnectionId IncomingConnection)
484incoming = accessor _incoming (\conns st -> st { _incoming = conns })
486incomingAt :: NT.ConnectionId -> Accessor ConnectionState (Maybe IncomingConnection)
487incomingAt cid = incoming >>> DAC.mapMaybe cid
489incomingFrom :: NT.EndPointAddress -> Accessor ConnectionState (Set NT.ConnectionId)
490incomingFrom addr = aux >>> DAC.mapDefault Set.empty addr
492 aux = accessor _incomingFrom (\fr st -> st { _incomingFrom = fr })
494handleIncomingMessages :: LocalNode -> IO ()
495handleIncomingMessages node = go initConnectionState
496 `Exception.catch` \(NodeClosedException _) -> return ()
498 go :: ConnectionState -> IO ()
500 event <- NT.receive endpoint
502 NT.ConnectionOpened cid rel theirAddr ->
503 if rel == NT.ReliableOrdered
505 trace node (MxConnected cid theirAddr)
507 (incomingAt cid ^= Just (theirAddr, Uninit))
508 . (incomingFrom theirAddr ^: Set.insert cid)
511 else invalidRequest cid st
512 NT.Received cid payload ->
513 case st ^. incomingAt cid of
514 Just (_, ToProc pid weakQueue) -> do
515 mQueue <- deRefWeak weakQueue
516 forM_ mQueue $ \queue -> do
517 -- TODO: if we find that the queue is Nothing, should we remove
518 -- it from the NC state? (and same for channels, below)
519 let msg = payloadToMessage payload
520 enqueue queue msg -- 'enqueue' is strict
521 trace node (MxReceived pid msg)
523 Just (_, ToChan (TypedChannel chan')) -> do
524 mChan <- deRefWeak chan'
525 -- If mChan is Nothing, the process has given up the read end of
526 -- the channel and we simply ignore the incoming message
527 forM_ mChan $ \chan -> atomically $
528 -- We make sure the message is fully decoded when it is enqueued
529 writeTQueue chan $! decode (BSL.fromChunks payload)
531 Just (_, ToNode) -> do
532 let ctrlMsg = decode . BSL.fromChunks $ payload
533 writeChan ctrlChan $! ctrlMsg
535 Just (src, Uninit) ->
536 case decode (BSL.fromChunks payload) of
537 ProcessIdentifier pid -> do
538 let lpid = processLocalId pid
539 mProc <- withValidLocalState node $ return . (^. localProcessWithId lpid)
542 go (incomingAt cid ^= Just (src, ToProc pid (processWeakQ proc)) $ st)
544 invalidRequest cid st
545 SendPortIdentifier chId -> do
546 let lcid = sendPortLocalId chId
547 lpid = processLocalId (sendPortProcessId chId)
548 mProc <- withValidLocalState node $ return . (^. localProcessWithId lpid)
551 mChannel <- withMVar (processState proc) $ return . (^. typedChannelWithId lcid)
554 go (incomingAt cid ^= Just (src, ToChan channel) $ st)
556 invalidRequest cid st
558 invalidRequest cid st
559 NodeIdentifier nid ->
560 if nid == localNodeId node
561 then go (incomingAt cid ^= Just (src, ToNode) $ st)
562 else invalidRequest cid st
564 invalidRequest cid st
565 NT.ConnectionClosed cid ->
566 case st ^. incomingAt cid of
568 invalidRequest cid st
570 trace node (MxDisconnected cid src)
571 go ( (incomingAt cid ^= Nothing)
572 . (incomingFrom src ^: Set.delete cid)
575 NT.ErrorEvent (NT.TransportError (NT.EventConnectionLost theirAddr) _) -> do
576 -- [Unified table 9, rule node_disconnect]
577 let nid = NodeIdentifier $ NodeId theirAddr
578 writeChan ctrlChan NCMsg
579 { ctrlMsgSender = nid
580 , ctrlMsgSignal = Died nid DiedDisconnect
582 let notLost k = not (k `Set.member` (st ^. incomingFrom theirAddr))
583 closeImplicitReconnections node nid
584 go ( (incomingFrom theirAddr ^= Set.empty)
585 . (incoming ^: Map.filterWithKey (const . notLost))
588 NT.ErrorEvent (NT.TransportError NT.EventEndPointFailed str) ->
589 fail $ "Cloud Haskell fatal error: end point failed: " ++ str
590 NT.ErrorEvent (NT.TransportError NT.EventTransportFailed str) ->
591 fail $ "Cloud Haskell fatal error: transport failed: " ++ str
594 NT.ReceivedMulticast _ _ ->
595 -- If we received a multicast message, something went horribly wrong
596 -- and we just give up
597 fail "Cloud Haskell fatal error: received unexpected multicast"
599 invalidRequest :: NT.ConnectionId -> ConnectionState -> IO ()
600 invalidRequest cid st = do
601 -- TODO: We should treat this as a fatal error on the part of the remote
602 -- node. That is, we should report the remote node as having died, and we
603 -- should close incoming connections (this requires a Transport layer
605 traceEventFmtIO node "" [(TraceStr " [network] invalid request: "),
607 go ( incomingAt cid ^= Nothing
611 endpoint = localEndPoint node
612 ctrlChan = localCtrlChan node
614--------------------------------------------------------------------------------
615-- Top-level access to the node controller --
616--------------------------------------------------------------------------------
618runNodeController :: LocalNode -> IO ()
619runNodeController node =
620 runReaderT (evalStateT (unNC nodeController) initNCState) node
621 `Exception.catch` \(NodeClosedException _) -> return ()
623--------------------------------------------------------------------------------
624-- Internal data types --
625--------------------------------------------------------------------------------
627data NCState = NCState
628 { -- Mapping from remote processes to linked local processes
629 _links :: !(Map Identifier (Set ProcessId))
630 -- Mapping from remote processes to monitoring local processes
631 , _monitors :: !(Map Identifier (Set (ProcessId, MonitorRef)))
632 -- Process registry: names and where they live, mapped to the PIDs
633 , _registeredHere :: !(Map String ProcessId)
634 , _registeredOnNodes :: !(Map ProcessId [(NodeId,Int)])
637newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
638 deriving ( Applicative
643 , MonadReader LocalNode
646initNCState :: NCState
647initNCState = NCState { _links = Map.empty
648 , _monitors = Map.empty
649 , _registeredHere = Map.empty
650 , _registeredOnNodes = Map.empty
653-- | Thrown in response to the user invoking 'kill' (see Primitives.hs). This
654-- type is deliberately not exported so it cannot be caught explicitly.
655data ProcessKillException =
656 ProcessKillException !ProcessId !String
659instance Exception ProcessKillException
660instance Show ProcessKillException where
661 show (ProcessKillException pid reason) =
662 "killed-by=" ++ show pid ++ ",reason=" ++ reason
664ncSendToProcess :: ProcessId -> Message -> NC ()
665ncSendToProcess pid msg = do
667 if processNodeId pid == localNodeId node
668 then ncEffectLocalSend node pid msg
669 else liftIO $ sendBinary node
670 (NodeIdentifier $ localNodeId node)
671 (NodeIdentifier $ processNodeId pid)
672 WithImplicitReconnect
673 NCMsg { ctrlMsgSender = NodeIdentifier $ localNodeId node
674 , ctrlMsgSignal = UnreliableSend (processLocalId pid) msg
677ncSendToNode :: NodeId -> NCMsg -> NC ()
678ncSendToNode to msg = do
680 liftIO $ if to == localNodeId node
681 then writeChan (localCtrlChan node) $! msg
683 (NodeIdentifier $ localNodeId node)
685 WithImplicitReconnect
688--------------------------------------------------------------------------------
689-- Tracing/Debugging --
690--------------------------------------------------------------------------------
692-- [Issue #104 / DP-13]
694traceNotifyDied :: LocalNode -> Identifier -> DiedReason -> NC ()
695traceNotifyDied node ident reason =
696 -- TODO: sendPortDied notifications
697 liftIO $ withLocalTracer node $ \t ->
699 (NodeIdentifier nid) -> traceEvent t (MxNodeDied nid reason)
700 (ProcessIdentifier pid) -> traceEvent t (MxProcessDied pid reason)
703traceEventFmtIO :: LocalNode
707traceEventFmtIO node fmt args =
708 withLocalTracer node $ \t -> traceLogFmt t fmt args
710trace :: LocalNode -> MxEvent -> IO ()
711trace node ev = withLocalTracer node $ \t -> traceEvent t ev
713withLocalTracer :: LocalNode -> (MxEventBus -> IO ()) -> IO ()
714withLocalTracer node act = act (localEventBus node)
716--------------------------------------------------------------------------------
717-- Core functionality --
718--------------------------------------------------------------------------------
721nodeController :: NC ()
725 msg <- liftIO $ readChan (localCtrlChan node)
727 -- [Unified: Table 7, rule nc_forward]
728 case destNid (ctrlMsgSignal msg) of
729 Just nid' | nid' /= localNodeId node ->
730 ncSendToNode nid' msg
735 NCMsg (ProcessIdentifier from) (Link them) ->
736 ncEffectMonitor from them Nothing
737 NCMsg (ProcessIdentifier from) (Monitor ref) ->
738 ncEffectMonitor from (monitorRefIdent ref) (Just ref)
739 NCMsg (ProcessIdentifier from) (Unlink them) ->
740 ncEffectUnlink from them
741 NCMsg (ProcessIdentifier from) (Unmonitor ref) ->
742 ncEffectUnmonitor from ref
743 NCMsg _from (Died ident reason) ->
744 ncEffectDied ident reason
745 NCMsg (ProcessIdentifier from) (Spawn proc ref) ->
746 ncEffectSpawn from proc ref
747 NCMsg (ProcessIdentifier from) (Register label atnode pid force) ->
748 ncEffectRegister from label atnode pid force
749 NCMsg (ProcessIdentifier from) (WhereIs label) ->
750 ncEffectWhereIs from label
751 NCMsg _ (NamedSend label msg') ->
752 ncEffectNamedSend label msg'
753 NCMsg _ (UnreliableSend lpid msg') ->
754 ncEffectLocalSend node (ProcessId (localNodeId node) lpid) msg'
755 NCMsg _ (LocalSend to msg') ->
756 ncEffectLocalSend node to msg'
757 NCMsg _ (LocalPortSend to msg') ->
758 ncEffectLocalPortSend to msg'
759 NCMsg (ProcessIdentifier from) (Kill to reason) ->
760 ncEffectKill from to reason
761 NCMsg (ProcessIdentifier from) (Exit to reason) ->
762 ncEffectExit from to reason
763 NCMsg (ProcessIdentifier from) (GetInfo pid) ->
764 ncEffectGetInfo from pid
765 NCMsg _ SigShutdown ->
767 NT.closeEndPoint (localEndPoint node)
768 `Exception.finally` throwIO ThreadKilled
769 -- ThreadKilled seems to make more sense than fail/error here
770 NCMsg (ProcessIdentifier from) (GetNodeStats nid) ->
771 ncEffectGetNodeStats from nid
773 error $ "nodeController: unexpected message " ++ show unexpected
775-- [Unified: Table 10]
776ncEffectMonitor :: ProcessId -- ^ Who's watching?
777 -> Identifier -- ^ Who's being watched?
778 -> Maybe MonitorRef -- ^ 'Nothing' to link
780ncEffectMonitor from them mRef = do
783 if not (isLocal node them)
785 else isValidLocalIdentifier them
786 case (shouldLink, isLocal node (ProcessIdentifier from)) of
787 (True, _) -> -- [Unified: first rule]
789 Just ref -> modify' $ monitorsFor them ^: Set.insert (from, ref)
790 Nothing -> modify' $ linksFor them ^: Set.insert from
791 (False, True) -> -- [Unified: second rule]
792 notifyDied from them DiedUnknownId mRef
793 (False, False) -> -- [Unified: third rule]
794 -- TODO: this is the right sender according to the Unified semantics,
795 -- but perhaps having 'them' as the sender would make more sense
796 -- (see also: notifyDied)
797 ncSendToNode (processNodeId from) $ NCMsg
798 { ctrlMsgSender = NodeIdentifier (localNodeId node)
799 , ctrlMsgSignal = Died them DiedUnknownId
802-- [Unified: Table 11]
803ncEffectUnlink :: ProcessId -> Identifier -> NC ()
804ncEffectUnlink from them = do
806 when (isLocal node (ProcessIdentifier from)) $
808 ProcessIdentifier pid ->
809 postAsMessage from $ DidUnlinkProcess pid
810 NodeIdentifier nid ->
811 postAsMessage from $ DidUnlinkNode nid
812 SendPortIdentifier cid ->
813 postAsMessage from $ DidUnlinkPort cid
814 modify' $ linksFor them ^: Set.delete from
816-- [Unified: Table 11]
817ncEffectUnmonitor :: ProcessId -> MonitorRef -> NC ()
818ncEffectUnmonitor from ref = do
820 when (isLocal node (ProcessIdentifier from)) $
821 postAsMessage from $ DidUnmonitor ref
822 modify' $ monitorsFor (monitorRefIdent ref) ^: Set.delete (from, ref)
824-- [Unified: Table 12]
825ncEffectDied :: Identifier -> DiedReason -> NC ()
826ncEffectDied ident reason = do
828 traceNotifyDied node ident reason
829 (affectedLinks, unaffectedLinks) <- gets (splitNotif ident . (^. links))
830 (affectedMons, unaffectedMons) <- gets (splitNotif ident . (^. monitors))
832-- _registry :: !(Map (String,NodeId) ProcessId)
834 let localOnly = case ident of NodeIdentifier _ -> True ; _ -> False
836 forM_ (Map.toList affectedLinks) $ \(them, uss) ->
838 when (localOnly <= isLocal node (ProcessIdentifier us)) $
839 notifyDied us them reason Nothing
841 forM_ (Map.toList affectedMons) $ \(them, refs) ->
842 forM_ refs $ \(us, ref) ->
843 when (localOnly <= isLocal node (ProcessIdentifier us)) $
844 notifyDied us them reason (Just ref)
846 modify' $ (links ^= unaffectedLinks) . (monitors ^= unaffectedMons)
848 modify' $ registeredHere ^: Map.filter (\pid -> not $ ident `impliesDeathOf` ProcessIdentifier pid)
850 remaining <- fmap Map.toList (gets (^. registeredOnNodes)) >>=
851 mapM (\(pid,nidlist) ->
852 case ident `impliesDeathOf` ProcessIdentifier pid of
854 do forM_ nidlist $ \(nid,_) ->
855 when (not $ isLocal node (NodeIdentifier nid))
856 (forwardNameDeath node nid)
858 False -> return $ Just (pid,nidlist) )
859 modify' $ registeredOnNodes ^= (Map.fromList (catMaybes remaining))
861 forwardNameDeath node nid = ncSendToNode nid
862 NCMsg { ctrlMsgSender = NodeIdentifier (localNodeId node)
863 , ctrlMsgSignal = Died ident reason
866-- [Unified: Table 13]
867ncEffectSpawn :: ProcessId -> Closure (Process ()) -> SpawnRef -> NC ()
868ncEffectSpawn pid cProc ref = do
869 mProc <- unClosure cProc
870 -- If the closure does not exist, we spawn a process that throws an exception
871 -- This allows the remote node to find out what's happening
873 let proc = case mProc of
874 Left err -> fail $ "Error: Could not resolve closure: " ++ err
877 pid' <- liftIO $ forkProcess node proc
878 ncSendToProcess pid $ unsafeCreateUnencodedMessage $ DidSpawn ref pid'
880-- Unified semantics does not explicitly describe how to implement 'register',
881-- but mentions it's "very similar to nsend" (Table 14)
882-- We send a response indicated if the operation is invalid
883ncEffectRegister :: ProcessId -> String -> NodeId -> Maybe ProcessId -> Bool -> NC ()
884ncEffectRegister from label atnode mPid reregistration = do
886 currentVal <- gets (^. registeredHereFor label)
889 Nothing -> -- unregister request
890 return $ isJust currentVal
891 Just thepid -> -- register request
892 do isvalidlocal <- isValidLocalIdentifier (ProcessIdentifier thepid)
893 return $ (isNothing currentVal /= reregistration) &&
894 (not (isLocal node (ProcessIdentifier thepid) ) || isvalidlocal )
895 if isLocal node (NodeIdentifier atnode)
897 do modify' $ registeredHereFor label ^= mPid
898 updateRemote node currentVal mPid
900 (Just p) -> liftIO $ trace node (MxRegistered p label)
901 Nothing -> liftIO $ trace node (MxUnRegistered (fromJust currentVal) label)
902 newVal <- gets (^. registeredHereFor label)
903 ncSendToProcess from $ unsafeCreateUnencodedMessage $
904 RegisterReply label isOk newVal
906 case reregistration of
908 False -> flip incList
911 Just pid -> modify' $ registeredOnNodesFor pid ^: (maybeify $ operation atnode)
912 where updateRemote node (Just oldval) (Just newval) | processNodeId oldval /= processNodeId newval =
913 do forward node (processNodeId oldval) (Register label atnode (Just oldval) True)
914 forward node (processNodeId newval) (Register label atnode (Just newval) False)
915 updateRemote node Nothing (Just newval) =
916 forward node (processNodeId newval) (Register label atnode (Just newval) False)
917 updateRemote node (Just oldval) Nothing =
918 forward node (processNodeId oldval) (Register label atnode (Just oldval) True)
919 updateRemote _ _ _ = return ()
920 maybeify f Nothing = unmaybeify $ f []
921 maybeify f (Just x) = unmaybeify $ f x
923 unmaybeify [] = Nothing
924 unmaybeify x = Just x
925 incList [] tag = [(tag,1)]
926 incList ((atag,acount):xs) tag | tag==atag = (atag,acount+1) : xs
927 incList (x:xs) tag = x : incList xs tag
929 decList ((atag,1):xs) tag | atag == tag = xs
930 decList ((atag,n):xs) tag | atag == tag = (atag,n-1):xs
931 decList (x:xs) tag = x:decList xs tag
932 forward node to reg =
933 when (not $ isLocal node (NodeIdentifier to)) $
934 ncSendToNode to $ NCMsg { ctrlMsgSender = ProcessIdentifier from
935 , ctrlMsgSignal = reg
939-- Unified semantics does not explicitly describe 'whereis'
940ncEffectWhereIs :: ProcessId -> String -> NC ()
941ncEffectWhereIs from label = do
942 mPid <- gets (^. registeredHereFor label)
943 ncSendToProcess from $ unsafeCreateUnencodedMessage $ WhereIsReply label mPid
945-- [Unified: Table 14]
946ncEffectNamedSend :: String -> Message -> NC ()
947ncEffectNamedSend label msg = do
948 mPid <- gets (^. registeredHereFor label)
949 -- If mPid is Nothing, we just ignore the named send (as per Table 14)
950 forM_ mPid (`ncSendToProcess` msg)
953ncEffectLocalSend :: LocalNode -> ProcessId -> Message -> NC ()
954ncEffectLocalSend node to msg =
955 liftIO $ withLocalProc node to $ \p -> do
956 enqueue (processQueue p) msg
957 trace node (MxReceived to msg)
960ncEffectLocalPortSend :: SendPortId -> Message -> NC ()
961ncEffectLocalPortSend from msg = do
963 let pid = sendPortProcessId from
964 cid = sendPortLocalId from
965 liftIO $ withLocalProc node pid $ \proc -> do
966 mChan <- withMVar (processState proc) $ return . (^. typedChannelWithId cid)
968 -- in the unlikely event we know nothing about this channel id,
969 -- there's little to be done - perhaps some logging/tracing though...
971 Just (TypedChannel chan') -> do
972 -- If ch is Nothing, the process has given up the read end of
973 -- the channel and we simply ignore the incoming message - this
974 ch <- deRefWeak chan'
975 forM_ ch $ \chan -> deliverChan msg chan
976 where deliverChan :: forall a . Message -> TQueue a -> IO ()
977 deliverChan (UnencodedMessage _ raw) chan' =
978 atomically $ writeTQueue chan' ((unsafeCoerce raw) :: a)
979 deliverChan (EncodedMessage _ _) _ =
980 -- this will not happen unless someone screws with Primitives.hs
981 error "invalid local channel delivery"
984ncEffectKill :: ProcessId -> ProcessId -> String -> NC ()
985ncEffectKill from to reason = do
987 when (isLocal node (ProcessIdentifier to)) $
988 throwException to $ ProcessKillException from reason
991ncEffectExit :: ProcessId -> ProcessId -> Message -> NC ()
992ncEffectExit from to reason = do
994 when (isLocal node (ProcessIdentifier to)) $
995 throwException to $ ProcessExitException from reason
998ncEffectGetInfo :: ProcessId -> ProcessId -> NC ()
999ncEffectGetInfo from pid =
1000 let lpid = processLocalId pid
1001 them = (ProcessIdentifier pid)
1004 mProc <- liftIO $ withValidLocalState node
1005 $ return . (^. localProcessWithId lpid)
1007 Nothing -> dispatch (isLocal node (ProcessIdentifier from))
1008 from (ProcessInfoNone DiedUnknownId)
1010 itsLinks <- gets (^. linksFor them)
1011 itsMons <- gets (^. monitorsFor them)
1012 registered <- gets (^. registeredHere)
1013 size <- liftIO $ queueSize $ processQueue $ proc
1015 let reg = registeredNames registered
1016 dispatch (isLocal node (ProcessIdentifier from))
1019 infoNode = (processNodeId pid)
1020 , infoRegisteredNames = reg
1021 , infoMessageQueueLength = size
1022 , infoMonitors = Set.toList itsMons
1023 , infoLinks = Set.toList itsLinks
1025 where dispatch :: (Serializable a, Show a)
1030 dispatch True dest pInfo = postAsMessage dest $ pInfo
1031 dispatch False dest pInfo =
1032 ncSendToProcess dest $ unsafeCreateUnencodedMessage pInfo
1034 registeredNames = Map.foldlWithKey (\ks k v -> if v == pid
1038ncEffectGetNodeStats :: ProcessId -> NodeId -> NC ()
1039ncEffectGetNodeStats from _nid = do
1041 ncState <- StateT.get
1042 nodeState <- liftIO $ withValidLocalState node return
1045 nodeStatsNode = localNodeId node
1046 , nodeStatsRegisteredNames = Map.size $ ncState ^. registeredHere
1047 , nodeStatsMonitors = Map.size $ ncState ^. monitors
1048 , nodeStatsLinks = Map.size $ ncState ^. links
1049 , nodeStatsProcesses = Map.size (nodeState ^. localProcesses)
1051 postAsMessage from stats
1053--------------------------------------------------------------------------------
1055--------------------------------------------------------------------------------
1057notifyDied :: ProcessId -- ^ Who to notify?
1058 -> Identifier -- ^ Who died?
1059 -> DiedReason -- ^ How did they die?
1060 -> Maybe MonitorRef -- ^ 'Nothing' for linking
1062notifyDied dest src reason mRef = do
1064 case (isLocal node (ProcessIdentifier dest), mRef, src) of
1065 (True, Just ref, ProcessIdentifier pid) ->
1066 postAsMessage dest $ ProcessMonitorNotification ref pid reason
1067 (True, Just ref, NodeIdentifier nid) ->
1068 postAsMessage dest $ NodeMonitorNotification ref nid reason
1069 (True, Just ref, SendPortIdentifier cid) ->
1070 postAsMessage dest $ PortMonitorNotification ref cid reason
1071 (True, Nothing, ProcessIdentifier pid) ->
1072 throwException dest $ ProcessLinkException pid reason
1073 (True, Nothing, NodeIdentifier pid) ->
1074 throwException dest $ NodeLinkException pid reason
1075 (True, Nothing, SendPortIdentifier pid) ->
1076 throwException dest $ PortLinkException pid reason
1078 -- The change in sender comes from [Unified: Table 10]
1079 ncSendToNode (processNodeId dest) $ NCMsg
1080 { ctrlMsgSender = NodeIdentifier (localNodeId node)
1081 , ctrlMsgSignal = Died src reason
1084-- | [Unified: Table 8]
1085destNid :: ProcessSignal -> Maybe NodeId
1086destNid (Link ident) = Just $ nodeOf ident
1087destNid (Unlink ident) = Just $ nodeOf ident
1088destNid (Monitor ref) = Just $ nodeOf (monitorRefIdent ref)
1089destNid (Unmonitor ref) = Just $ nodeOf (monitorRefIdent ref)
1090destNid (Spawn _ _) = Nothing
1091destNid (Register _ _ _ _) = Nothing
1092destNid (WhereIs _) = Nothing
1093destNid (NamedSend _ _) = Nothing
1094destNid (UnreliableSend _ _) = Nothing
1095-- We don't need to forward 'Died' signals; if monitoring/linking is setup,
1096-- then when a local process dies the monitoring/linking machinery will take
1097-- care of notifying remote nodes
1098destNid (Died _ _) = Nothing
1099destNid (Kill pid _) = Just $ processNodeId pid
1100destNid (Exit pid _) = Just $ processNodeId pid
1101destNid (GetInfo pid) = Just $ processNodeId pid
1102destNid (GetNodeStats nid) = Just nid
1103destNid (LocalSend pid _) = Just $ processNodeId pid
1104destNid (LocalPortSend cid _) = Just $ processNodeId (sendPortProcessId cid)
1105destNid (SigShutdown) = Nothing
1107-- | Check if a process is local to our own node
1108isLocal :: LocalNode -> Identifier -> Bool
1109isLocal nid ident = nodeOf ident == localNodeId nid
1111-- | Lookup a local closure
1112unClosure :: Typeable a => Closure a -> NC (Either String a)
1113unClosure closure = do
1114 rtable <- remoteTable <$> ask
1115 return (Static.unclosure rtable closure)
1117-- | Check if an identifier refers to a valid local object
1118isValidLocalIdentifier :: Identifier -> NC Bool
1119isValidLocalIdentifier ident = do
1121 liftIO . withValidLocalState node $ \nSt ->
1123 NodeIdentifier nid ->
1124 return $ nid == localNodeId node
1125 ProcessIdentifier pid -> do
1126 let mProc = nSt ^. localProcessWithId (processLocalId pid)
1127 return $ isJust mProc
1128 SendPortIdentifier cid -> do
1129 let pid = sendPortProcessId cid
1130 mProc = nSt ^. localProcessWithId (processLocalId pid)
1132 Nothing -> return False
1133 Just proc -> withMVar (processState proc) $ \pSt -> do
1134 let mCh = pSt ^. typedChannelWithId (sendPortLocalId cid)
1137--------------------------------------------------------------------------------
1138-- Messages to local processes --
1139--------------------------------------------------------------------------------
1141postAsMessage :: Serializable a => ProcessId -> a -> NC ()
1142postAsMessage pid = postMessage pid . createUnencodedMessage
1144postMessage :: ProcessId -> Message -> NC ()
1145postMessage pid msg = do
1147 liftIO $ withLocalProc node pid $ \p -> enqueue (processQueue p) msg
1149throwException :: Exception e => ProcessId -> e -> NC ()
1150throwException pid e = do
1152 -- throwTo blocks until the exception is received by the target thread.
1153 -- We fork a helper thread to avoid blocking the node controller.
1154 liftIO $ withLocalProc node pid $ \p -> void $ forkIO $ throwTo (processThread p) e
1156withLocalProc :: LocalNode -> ProcessId -> (LocalProcess -> IO ()) -> IO ()
1157withLocalProc node pid p =
1158 -- By [Unified: table 6, rule missing_process] messages to dead processes
1159 -- can silently be dropped
1160 let lpid = processLocalId pid in do
1161 withValidLocalState node $ \vst ->
1162 forM_ (vst ^. localProcessWithId lpid) p
1164--------------------------------------------------------------------------------
1166--------------------------------------------------------------------------------
1168links :: Accessor NCState (Map Identifier (Set ProcessId))
1169links = accessor _links (\ls st -> st { _links = ls })
1171monitors :: Accessor NCState (Map Identifier (Set (ProcessId, MonitorRef)))
1172monitors = accessor _monitors (\ms st -> st { _monitors = ms })
1174registeredHere :: Accessor NCState (Map String ProcessId)
1175registeredHere = accessor _registeredHere (\ry st -> st { _registeredHere = ry })
1177registeredOnNodes :: Accessor NCState (Map ProcessId [(NodeId, Int)])
1178registeredOnNodes = accessor _registeredOnNodes (\ry st -> st { _registeredOnNodes = ry })
1180linksFor :: Identifier -> Accessor NCState (Set ProcessId)
1181linksFor ident = links >>> DAC.mapDefault Set.empty ident
1183monitorsFor :: Identifier -> Accessor NCState (Set (ProcessId, MonitorRef))
1184monitorsFor ident = monitors >>> DAC.mapDefault Set.empty ident
1186registeredHereFor :: String -> Accessor NCState (Maybe ProcessId)
1187registeredHereFor ident = registeredHere >>> DAC.mapMaybe ident
1189registeredOnNodesFor :: ProcessId -> Accessor NCState (Maybe [(NodeId,Int)])
1190registeredOnNodesFor ident = registeredOnNodes >>> DAC.mapMaybe ident
1192-- | @splitNotif ident@ splits a notifications map into those
1193-- notifications that should trigger when 'ident' fails and those links that
1196-- There is a hierarchy between identifiers: failure of a node implies failure
1197-- of all processes on that node, and failure of a process implies failure of
1198-- all typed channels to that process. In other words, if 'ident' refers to a
1199-- node, then the /should trigger/ set will include
1201-- * the notifications for the node specifically
1202-- * the notifications for processes on that node, and
1203-- * the notifications for typed channels to processes on that node.
1205-- Similarly, if 'ident' refers to a process, the /should trigger/ set will
1208-- * the notifications for that process specifically and
1209-- * the notifications for typed channels to that process.
1211-- See https://github.com/haskell/containers/issues/14 for the bang on _v.
1212splitNotif :: Identifier
1214 -> (Map Identifier a, Map Identifier a)
1215splitNotif ident = Map.partitionWithKey (\k !_v -> ident `impliesDeathOf` k)
1217--------------------------------------------------------------------------------
1219--------------------------------------------------------------------------------
1221-- | Modify and evaluate the state
1222modify' :: MonadState s m => (s -> s) -> m ()
1223modify' f = StateT.get >>= \s -> StateT.put $! f s
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list