git.iko.soy/distributed-process
on e648b03 help sign in
479 lines 14 kB history raw
1{-# LANGUAGE CPP #-}
2{- | [Cloud Haskell]
4This is an implementation of Cloud Haskell, as described in
5/Towards Haskell in the Cloud/ by Jeff Epstein, Andrew Black, and Simon
6Peyton Jones (see
7<http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/>),
8although some of the details are different. The precise message passing
9semantics are based on /A unified semantics for future Erlang/ by Hans
10Svensson, Lars-Åke Fredlund and Clara Benac Earle.
12For a detailed description of the package and other reference materials,
13please see the distributed-process wiki page on github:
14<https://github.com/haskell-distributed/distributed-process/wiki>.
16-}
17module Control.Distributed.Process
18 ( -- * Basic types
19 ProcessId
20 , NodeId(..)
21 , Process
22 , SendPortId
23 , processNodeId
24 , sendPortProcessId
25 , liftIO -- Reexported for convenience
26 -- * Basic messaging
27 , send
28 , usend
29 , expect
30 , expectTimeout
31 -- * Channels
32 , ReceivePort
33 , SendPort
34 , sendPortId
35 , newChan
36 , sendChan
37 , receiveChan
38 , receiveChanTimeout
39 , mergePortsBiased
40 , mergePortsRR
41 -- * Unsafe messaging variants
42 , unsafeSend
43 , unsafeUSend
44 , unsafeSendChan
45 , unsafeNSend
46 , unsafeNSendRemote
47 , unsafeWrapMessage
48 -- * Advanced messaging
49 , Match
50 , receiveWait
51 , receiveTimeout
52 , match
53 , matchIf
54 , matchUnknown
55 , matchAny
56 , matchAnyIf
57 , matchChan
58 , matchSTM
59 , Message
60 , matchMessage
61 , matchMessageIf
62 , isEncoded
63 , wrapMessage
64 , unwrapMessage
65 , handleMessage
66 , handleMessageIf
67 , handleMessage_
68 , handleMessageIf_
69 , forward
70 , uforward
71 , delegate
72 , relay
73 , proxy
74 -- * Process management
75 , spawn
76 , call
77 , terminate
78 , die
79 , kill
80 , exit
81 , catchExit
82 , catchesExit
83 , ProcessTerminationException(..)
84 , ProcessRegistrationException(..)
85 , SpawnRef
86 , getSelfPid
87 , getSelfNode
88 , ProcessInfo(..)
89 , getProcessInfo
90 , NodeStats(..)
91 , getNodeStats
92 , getLocalNodeStats
93 -- * Monitoring and linking
94 , link
95 , linkNode
96 , linkPort
97 , unlink
98 , unlinkNode
99 , unlinkPort
100 , monitor
101 , monitorNode
102 , monitorPort
103 , unmonitor
104 , withMonitor
105 , MonitorRef -- opaque
106 , ProcessLinkException(..)
107 , NodeLinkException(..)
108 , PortLinkException(..)
109 , ProcessMonitorNotification(..)
110 , NodeMonitorNotification(..)
111 , PortMonitorNotification(..)
112 , DiedReason(..)
113 -- * Closures
114 , Closure
115 , closure
116 , Static
117 , unStatic
118 , unClosure
119 , RemoteTable
120 -- * Logging
121 , say
122 -- * Registry
123 , register
124 , reregister
125 , unregister
126 , whereis
127 , nsend
128 , registerRemoteAsync
129 , reregisterRemoteAsync
130 , unregisterRemoteAsync
131 , whereisRemoteAsync
132 , nsendRemote
133 , WhereIsReply(..)
134 , RegisterReply(..)
135 -- * Exception handling
136 , catch
137 , Handler(..)
138 , catches
139 , try
140 , mask
141 , mask_
142 , onException
143 , bracket
144 , bracket_
145 , finally
146 -- * Auxiliary API
147 , spawnAsync
148 , spawnSupervised
149 , spawnLink
150 , spawnMonitor
151 , spawnChannel
152 , DidSpawn(..)
153 -- * Local versions of 'spawn'
154 , spawnLocal
155 , spawnChannelLocal
156 , callLocal
157 -- * Reconnecting
158 , reconnect
159 , reconnectPort
160 ) where
162#if ! MIN_VERSION_base(4,6,0)
163import Prelude hiding (catch)
164#endif
166import Control.Monad.IO.Class (liftIO)
167import Control.Applicative ((<$>))
168import Control.Monad.Reader (ask)
169import Control.Concurrent (forkIO)
170import Control.Concurrent.MVar
171 ( MVar
172 , newEmptyMVar
173 , takeMVar
174 , putMVar
175 )
176import Control.Distributed.Static
177 ( Closure
178 , closure
179 , Static
180 , RemoteTable
181 )
182import Control.Distributed.Process.Internal.Types
183 ( NodeId(..)
184 , ProcessId(..)
185 , Process(..)
186 , MonitorRef(..)
187 , ProcessMonitorNotification(..)
188 , NodeMonitorNotification(..)
189 , PortMonitorNotification(..)
190 , ProcessLinkException(..)
191 , NodeLinkException(..)
192 , PortLinkException(..)
193 , ProcessRegistrationException(..)
194 , DiedReason(..)
195 , SpawnRef(..)
196 , DidSpawn(..)
197 , SendPort(..)
198 , ReceivePort(..)
199 , SendPortId(..)
200 , WhereIsReply(..)
201 , RegisterReply(..)
202 , LocalProcess(processNode)
203 , runLocalProcess
204 , Message
205 )
206import Control.Distributed.Process.Serializable (Serializable)
207import Control.Distributed.Process.Internal.Primitives
208 ( -- Basic messaging
209 send
210 , usend
211 , expect
212 -- Channels
213 , newChan
214 , sendChan
215 , receiveChan
216 , mergePortsBiased
217 , mergePortsRR
218 , unsafeSend
219 , unsafeUSend
220 , unsafeSendChan
221 , unsafeNSend
222 , unsafeNSendRemote
223 -- Advanced messaging
224 , Match
225 , receiveWait
226 , receiveTimeout
227 , match
228 , matchIf
229 , matchUnknown
230 , matchAny
231 , matchAnyIf
232 , matchChan
233 , matchSTM
234 , matchMessage
235 , matchMessageIf
236 , isEncoded
237 , wrapMessage
238 , unsafeWrapMessage
239 , unwrapMessage
240 , handleMessage
241 , handleMessageIf
242 , handleMessage_
243 , handleMessageIf_
244 , forward
245 , uforward
246 , delegate
247 , relay
248 , proxy
249 -- Process management
250 , terminate
251 , ProcessTerminationException(..)
252 , die
253 , exit
254 , catchExit
255 , catchesExit
256 , kill
257 , getSelfPid
258 , getSelfNode
259 , ProcessInfo(..)
260 , getProcessInfo
261 , NodeStats(..)
262 , getNodeStats
263 , getLocalNodeStats
264 -- Monitoring and linking
265 , link
266 , linkNode
267 , linkPort
268 , unlink
269 , unlinkNode
270 , unlinkPort
271 , monitor
272 , monitorNode
273 , monitorPort
274 , unmonitor
275 , withMonitor
276 -- Logging
277 , say
278 -- Registry
279 , register
280 , reregister
281 , unregister
282 , whereis
283 , nsend
284 , registerRemoteAsync
285 , reregisterRemoteAsync
286 , unregisterRemoteAsync
287 , whereisRemoteAsync
288 , nsendRemote
289 -- Closures
290 , unStatic
291 , unClosure
292 -- Exception handling
293 , catch
294 , Handler(..)
295 , catches
296 , try
297 , mask
298 , mask_
299 , onException
300 , bracket
301 , bracket_
302 , finally
303 -- Auxiliary API
304 , expectTimeout
305 , receiveChanTimeout
306 , spawnAsync
307 -- Reconnecting
308 , reconnect
309 , reconnectPort
310 )
311import Control.Distributed.Process.Node (forkProcess)
312import Control.Distributed.Process.Internal.Types
313 ( processThread
314 , localState
315 , LocalNodeState(..)
316 , withValidLocalState
317 )
318import Control.Distributed.Process.Internal.Spawn
319 ( -- Spawning Processes/Channels
320 spawn
321 , spawnLink
322 , spawnMonitor
323 , spawnChannel
324 , spawnSupervised
325 , call
326 )
327import Control.Distributed.Process.Internal.StrictMVar (readMVar)
328import Control.Exception (SomeException, throw, throwTo, throwIO)
329import Control.Monad (forM_)
332-- INTERNAL NOTES
333--
334-- 1. 'send' never fails. If you want to know that the remote process received
335-- your message, you will need to send an explicit acknowledgement. If you
336-- want to know when the remote process failed, you will need to monitor
337-- that remote process.
338--
339-- 2. 'send' may block (when the system TCP buffers are full, while we are
340-- trying to establish a connection to the remote endpoint, etc.) but its
341-- return does not imply that the remote process received the message (much
342-- less processed it). When 'send' targets a local process, it is dispatched
343-- via the node controller however, in which cases it will /not/ block. This
344-- isn't part of the semantics, so it should be ok. The ordering is maintained
345-- because the ctrlChannel still has FIFO semantics with regards interactions
346-- between two disparate forkIO threads.
347--
348-- 3. Message delivery is reliable and ordered. That means that if process A
349-- sends messages m1, m2, m3 to process B, B will either arrive all three
350-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
351-- 'missing' (m1, m3) or reordered (m1, m3, m2)
352--
353-- In order to guarantee (3), we stipulate that
354--
355-- 3a. We do not garbage collect connections because Network.Transport provides
356-- ordering guarantees only *per connection*.
357--
358-- 3b. Once a connection breaks, we have no way of knowing which messages
359-- arrived and which did not; hence, once a connection fails, we assume the
360-- remote process to be forever unreachable. Otherwise we might sent m1 and
361-- m2, get notified of the broken connection, reconnect, send m3, but only
362-- m1 and m3 arrive.
363--
364-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
365-- we consider it forever unreachable. Hence, new processes should get new
366-- IDs or they too would be considered unreachable.
367--
368-- Main reference for Cloud Haskell is
369--
370-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
371-- Peyton-Jones.
372-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
373--
374-- The precise semantics for message passing is based on
375--
376-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
377-- and Clara Benac Earle (not freely available online, unfortunately)
378--
379-- Some pointers to related documentation about Erlang, for comparison and
380-- inspiration:
381--
382-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
383-- Hans Svensson and Lars-Ake Fredlund
384-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
385-- [4] The Erlang manual, sections "Message Sending" and "Send"
386-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
387-- http://www.erlang.org/doc/reference_manual/expressions.html#send
388-- [5] Questions "Is the order of message reception guaranteed?" and
389-- "If I send a message, is it guaranteed to reach the receiver?" of
390-- the Erlang FAQ
391-- http://www.erlang.org/faq/academic.html
392-- [6] "Delivery of Messages", post on erlang-questions
393-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
395--------------------------------------------------------------------------------
396-- Local versions of spawn --
397--------------------------------------------------------------------------------
399-- | Spawn a process on the local node
400spawnLocal :: Process () -> Process ProcessId
401spawnLocal proc = do
402 node <- processNode <$> ask
403 liftIO $ forkProcess node proc
405-- | Create a new typed channel, spawn a process on the local node, passing it
406-- the receive port, and return the send port
407spawnChannelLocal :: Serializable a
408 => (ReceivePort a -> Process ())
409 -> Process (SendPort a)
410spawnChannelLocal proc = do
411 node <- processNode <$> ask
412 liftIO $ do
413 mvar <- newEmptyMVar
414 _ <- forkProcess node $ do
415 -- It is important that we allocate the new channel in the new process,
416 -- because otherwise it will be associated with the wrong process ID
417 (sport, rport) <- newChan
418 liftIO $ putMVar mvar sport
419 proc rport
420 takeMVar mvar
422-- | Local version of 'call'. Running a process in this way isolates it from
423-- messages sent to the caller process, and also allows silently dropping late
424-- or duplicate messages sent to the isolated process after it exits.
425-- Silently dropping messages may not always be the best approach.
426callLocal :: Process a -> Process a
427callLocal proc = mask $ \release -> do
428 mv <- liftIO newEmptyMVar :: Process (MVar (Either SomeException a))
429 (spInit, rpInit) <- newChan -- TODO: Remove when spawnLocal inherits the
430 -- masking state.
431 child <- spawnLocal $ mask_ $
432 try (sendChan spInit () >> release proc) >>= liftIO . putMVar mv
433 rs <- liftIO (takeMVar mv)
434 `onException`
435 -- Exceptions need to be prevented from interrupting the clean up or
436 -- the original exception which caused entering the handler could be
437 -- forgotten. For instance, this could have a problematic effect
438 -- when the original exception was meant to kill the thread and the
439 -- second exception doesn't (like the exception thrown by
440 -- 'System.Timeout.timeout').
441 --
442 -- Using 'uninterruptibleMask_' would be problematic as there is no
443 -- way to be sure the node isn't closing when the cleanup executes.
444 -- Successfully running the cleanup is necessary to ensure
445 -- termination. If exceptions are masked uninterruptibly,
446 -- 'receiveChan' might block indefinitely and 'kill' might have no
447 -- effect.
448 --
449 do (exs0, _) <- collectExceptions [] $
450 whenNotClosed $ receiveChan rpInit
451 (exs1, _) <- collectExceptions exs0 $
452 whenNotClosed $ kill child "callLocal was interrupted"
453 (exs, mr) <- collectExceptions exs1 $
454 whenNotClosed $ liftIO (takeMVar mv)
455 -- Forward exceptions asynchronously.
456 lproc <- ask
457 liftIO $ forkIO $ forM_ (reverse exs) $
458 throwTo (processThread lproc)
459 here <- getSelfNode
460 liftIO $
461 maybe (throwIO $ userError $ "Node closed " ++ show here)
462 return mr
463 either throw return rs
464 where
465 -- Retries a given action until it succeeds.
466 collectExceptions :: [SomeException]
467 -> Process a
468 -> Process ([SomeException], a)
469 collectExceptions exs p =
470 try p >>= either (\e -> collectExceptions (e : exs) p) (return . (,) exs)
472 -- Runs the given closure if the node is not closed.
473 whenNotClosed :: Process a -> Process (Maybe a)
474 whenNotClosed p = do
475 lproc <- ask
476 st <- liftIO $ readMVar (localState $ processNode lproc)
477 case st of
478 LocalNodeValid _ -> fmap Just p
479 LocalNodeClosed -> return Nothing