git.iko.soy/distributed-process
on d126187 help sign in
396 lines 10 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 , unsafeSendChan
44 , unsafeNSend
45 , unsafeWrapMessage
46 -- * Advanced messaging
47 , Match
48 , receiveWait
49 , receiveTimeout
50 , match
51 , matchIf
52 , matchUnknown
53 , matchAny
54 , matchAnyIf
55 , matchChan
56 , matchSTM
57 , Message
58 , matchMessage
59 , matchMessageIf
60 , isEncoded
61 , wrapMessage
62 , unwrapMessage
63 , handleMessage
64 , handleMessageIf
65 , handleMessage_
66 , handleMessageIf_
67 , forward
68 , delegate
69 , relay
70 , proxy
71 -- * Process management
72 , spawn
73 , call
74 , terminate
75 , die
76 , kill
77 , exit
78 , catchExit
79 , catchesExit
80 , ProcessTerminationException(..)
81 , ProcessRegistrationException(..)
82 , SpawnRef
83 , getSelfPid
84 , getSelfNode
85 , ProcessInfo(..)
86 , getProcessInfo
87 , NodeStats(..)
88 , getNodeStats
89 , getLocalNodeStats
90 -- * Monitoring and linking
91 , link
92 , linkNode
93 , linkPort
94 , unlink
95 , unlinkNode
96 , unlinkPort
97 , monitor
98 , monitorNode
99 , monitorPort
100 , unmonitor
101 , withMonitor
102 , MonitorRef -- opaque
103 , ProcessLinkException(..)
104 , NodeLinkException(..)
105 , PortLinkException(..)
106 , ProcessMonitorNotification(..)
107 , NodeMonitorNotification(..)
108 , PortMonitorNotification(..)
109 , DiedReason(..)
110 -- * Closures
111 , Closure
112 , closure
113 , Static
114 , unStatic
115 , unClosure
116 , RemoteTable
117 -- * Logging
118 , say
119 -- * Registry
120 , register
121 , reregister
122 , unregister
123 , whereis
124 , nsend
125 , registerRemoteAsync
126 , reregisterRemoteAsync
127 , unregisterRemoteAsync
128 , whereisRemoteAsync
129 , nsendRemote
130 , WhereIsReply(..)
131 , RegisterReply(..)
132 -- * Exception handling
133 , catch
134 , Handler(..)
135 , catches
136 , try
137 , mask
138 , mask_
139 , onException
140 , bracket
141 , bracket_
142 , finally
143 -- * Auxiliary API
144 , spawnAsync
145 , spawnSupervised
146 , spawnLink
147 , spawnMonitor
148 , spawnChannel
149 , DidSpawn(..)
150 -- * Local versions of 'spawn'
151 , spawnLocal
152 , spawnChannelLocal
153 -- * Reconnecting
154 , reconnect
155 , reconnectPort
156 ) where
158#if ! MIN_VERSION_base(4,6,0)
159import Prelude hiding (catch)
160#endif
162import Control.Monad.IO.Class (liftIO)
163import Control.Applicative ((<$>))
164import Control.Monad.Reader (ask)
165import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
166import Control.Distributed.Static
167 ( Closure
168 , closure
169 , Static
170 , RemoteTable
171 )
172import Control.Distributed.Process.Internal.Types
173 ( NodeId(..)
174 , ProcessId(..)
175 , Process(..)
176 , MonitorRef(..)
177 , ProcessMonitorNotification(..)
178 , NodeMonitorNotification(..)
179 , PortMonitorNotification(..)
180 , ProcessLinkException(..)
181 , NodeLinkException(..)
182 , PortLinkException(..)
183 , ProcessRegistrationException(..)
184 , DiedReason(..)
185 , SpawnRef(..)
186 , DidSpawn(..)
187 , SendPort(..)
188 , ReceivePort(..)
189 , SendPortId(..)
190 , WhereIsReply(..)
191 , RegisterReply(..)
192 , LocalProcess(processNode)
193 , Message
194 )
195import Control.Distributed.Process.Serializable (Serializable)
196import Control.Distributed.Process.Internal.Primitives
197 ( -- Basic messaging
198 send
199 , usend
200 , expect
201 -- Channels
202 , newChan
203 , sendChan
204 , receiveChan
205 , mergePortsBiased
206 , mergePortsRR
207 , unsafeSend
208 , unsafeSendChan
209 , unsafeNSend
210 -- Advanced messaging
211 , Match
212 , receiveWait
213 , receiveTimeout
214 , match
215 , matchIf
216 , matchUnknown
217 , matchAny
218 , matchAnyIf
219 , matchChan
220 , matchSTM
221 , matchMessage
222 , matchMessageIf
223 , isEncoded
224 , wrapMessage
225 , unsafeWrapMessage
226 , unwrapMessage
227 , handleMessage
228 , handleMessageIf
229 , handleMessage_
230 , handleMessageIf_
231 , forward
232 , delegate
233 , relay
234 , proxy
235 -- Process management
236 , terminate
237 , ProcessTerminationException(..)
238 , die
239 , exit
240 , catchExit
241 , catchesExit
242 , kill
243 , getSelfPid
244 , getSelfNode
245 , ProcessInfo(..)
246 , getProcessInfo
247 , NodeStats(..)
248 , getNodeStats
249 , getLocalNodeStats
250 -- Monitoring and linking
251 , link
252 , linkNode
253 , linkPort
254 , unlink
255 , unlinkNode
256 , unlinkPort
257 , monitor
258 , monitorNode
259 , monitorPort
260 , unmonitor
261 , withMonitor
262 -- Logging
263 , say
264 -- Registry
265 , register
266 , reregister
267 , unregister
268 , whereis
269 , nsend
270 , registerRemoteAsync
271 , reregisterRemoteAsync
272 , unregisterRemoteAsync
273 , whereisRemoteAsync
274 , nsendRemote
275 -- Closures
276 , unStatic
277 , unClosure
278 -- Exception handling
279 , catch
280 , Handler(..)
281 , catches
282 , try
283 , mask
284 , mask_
285 , onException
286 , bracket
287 , bracket_
288 , finally
289 -- Auxiliary API
290 , expectTimeout
291 , receiveChanTimeout
292 , spawnAsync
293 -- Reconnecting
294 , reconnect
295 , reconnectPort
296 )
297import Control.Distributed.Process.Node (forkProcess)
298import Control.Distributed.Process.Internal.Spawn
299 ( -- Spawning Processes/Channels
300 spawn
301 , spawnLink
302 , spawnMonitor
303 , spawnChannel
304 , spawnSupervised
305 , call
306 )
308-- INTERNAL NOTES
309--
310-- 1. 'send' never fails. If you want to know that the remote process received
311-- your message, you will need to send an explicit acknowledgement. If you
312-- want to know when the remote process failed, you will need to monitor
313-- that remote process.
314--
315-- 2. 'send' may block (when the system TCP buffers are full, while we are
316-- trying to establish a connection to the remote endpoint, etc.) but its
317-- return does not imply that the remote process received the message (much
318-- less processed it). When 'send' targets a local process, it is dispatched
319-- via the node controller however, in which cases it will /not/ block. This
320-- isn't part of the semantics, so it should be ok. The ordering is maintained
321-- because the ctrlChannel still has FIFO semantics with regards interactions
322-- between two disparate forkIO threads.
323--
324-- 3. Message delivery is reliable and ordered. That means that if process A
325-- sends messages m1, m2, m3 to process B, B will either arrive all three
326-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
327-- 'missing' (m1, m3) or reordered (m1, m3, m2)
328--
329-- In order to guarantee (3), we stipulate that
330--
331-- 3a. We do not garbage collect connections because Network.Transport provides
332-- ordering guarantees only *per connection*.
333--
334-- 3b. Once a connection breaks, we have no way of knowing which messages
335-- arrived and which did not; hence, once a connection fails, we assume the
336-- remote process to be forever unreachable. Otherwise we might sent m1 and
337-- m2, get notified of the broken connection, reconnect, send m3, but only
338-- m1 and m3 arrive.
339--
340-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
341-- we consider it forever unreachable. Hence, new processes should get new
342-- IDs or they too would be considered unreachable.
343--
344-- Main reference for Cloud Haskell is
345--
346-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
347-- Peyton-Jones.
348-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
349--
350-- The precise semantics for message passing is based on
351--
352-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
353-- and Clara Benac Earle (not freely available online, unfortunately)
354--
355-- Some pointers to related documentation about Erlang, for comparison and
356-- inspiration:
357--
358-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
359-- Hans Svensson and Lars-Ake Fredlund
360-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
361-- [4] The Erlang manual, sections "Message Sending" and "Send"
362-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
363-- http://www.erlang.org/doc/reference_manual/expressions.html#send
364-- [5] Questions "Is the order of message reception guaranteed?" and
365-- "If I send a message, is it guaranteed to reach the receiver?" of
366-- the Erlang FAQ
367-- http://www.erlang.org/faq/academic.html
368-- [6] "Delivery of Messages", post on erlang-questions
369-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
371--------------------------------------------------------------------------------
372-- Local versions of spawn --
373--------------------------------------------------------------------------------
375-- | Spawn a process on the local node
376spawnLocal :: Process () -> Process ProcessId
377spawnLocal proc = do
378 node <- processNode <$> ask
379 liftIO $ forkProcess node proc
381-- | Create a new typed channel, spawn a process on the local node, passing it
382-- the receive port, and return the send port
383spawnChannelLocal :: Serializable a
384 => (ReceivePort a -> Process ())
385 -> Process (SendPort a)
386spawnChannelLocal proc = do
387 node <- processNode <$> ask
388 liftIO $ do
389 mvar <- newEmptyMVar
390 _ <- forkProcess node $ do
391 -- It is important that we allocate the new channel in the new process,
392 -- because otherwise it will be associated with the wrong process ID
393 (sport, rport) <- newChan
394 liftIO $ putMVar mvar sport
395 proc rport
396 takeMVar mvar