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