… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::MP; |
7 | use AnyEvent::MP; |
8 | |
8 | |
9 | $NODE # contains this node's noderef |
9 | $NODE # contains this node's node ID |
10 | NODE # returns this node's noderef |
10 | NODE # returns this node's node ID |
11 | NODE $port # returns the noderef of the port |
|
|
12 | |
11 | |
13 | $SELF # receiving/own port id in rcv callbacks |
12 | $SELF # receiving/own port id in rcv callbacks |
14 | |
13 | |
15 | # initialise the node so it can send/receive messages |
14 | # initialise the node so it can send/receive messages |
16 | initialise_node; |
15 | configure; |
17 | |
16 | |
18 | # ports are message endpoints |
17 | # ports are message destinations |
19 | |
18 | |
20 | # sending messages |
19 | # sending messages |
21 | snd $port, type => data...; |
20 | snd $port, type => data...; |
22 | snd $port, @msg; |
21 | snd $port, @msg; |
23 | snd @msg_with_first_element_being_a_port; |
22 | snd @msg_with_first_element_being_a_port; |
24 | |
23 | |
25 | # creating/using ports, the simple way |
24 | # creating/using ports, the simple way |
26 | my $simple_port = port { my @msg = @_; 0 }; |
25 | my $simple_port = port { my @msg = @_ }; |
27 | |
26 | |
28 | # creating/using ports, tagged message matching |
27 | # creating/using ports, tagged message matching |
29 | my $port = port; |
28 | my $port = port; |
30 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
29 | rcv $port, ping => sub { snd $_[0], "pong" }; |
31 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
30 | rcv $port, pong => sub { warn "pong received\n" }; |
32 | |
31 | |
33 | # create a port on another node |
32 | # create a port on another node |
34 | my $port = spawn $node, $initfunc, @initdata; |
33 | my $port = spawn $node, $initfunc, @initdata; |
35 | |
34 | |
36 | # monitoring |
35 | # monitoring |
… | |
… | |
38 | mon $port, $otherport # kill otherport on abnormal death |
37 | mon $port, $otherport # kill otherport on abnormal death |
39 | mon $port, $otherport, @msg # send message on death |
38 | mon $port, $otherport, @msg # send message on death |
40 | |
39 | |
41 | =head1 CURRENT STATUS |
40 | =head1 CURRENT STATUS |
42 | |
41 | |
|
|
42 | bin/aemp - stable. |
43 | AnyEvent::MP - stable API, should work |
43 | AnyEvent::MP - stable API, should work. |
44 | AnyEvent::MP::Intro - outdated |
44 | AnyEvent::MP::Intro - explains most concepts. |
45 | AnyEvent::MP::Kernel - WIP |
|
|
46 | AnyEvent::MP::Transport - mostly stable |
45 | AnyEvent::MP::Kernel - mostly stable. |
|
|
46 | AnyEvent::MP::Global - stable but incomplete, protocol not yet final. |
47 | |
47 | |
48 | stay tuned. |
48 | stay tuned. |
49 | |
49 | |
50 | =head1 DESCRIPTION |
50 | =head1 DESCRIPTION |
51 | |
51 | |
52 | This module (-family) implements a simple message passing framework. |
52 | This module (-family) implements a simple message passing framework. |
53 | |
53 | |
54 | Despite its simplicity, you can securely message other processes running |
54 | Despite its simplicity, you can securely message other processes running |
55 | on the same or other hosts. |
55 | on the same or other hosts, and you can supervise entities remotely. |
56 | |
56 | |
57 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
57 | For an introduction to this module family, see the L<AnyEvent::MP::Intro> |
58 | manual page. |
58 | manual page and the examples under F<eg/>. |
59 | |
|
|
60 | At the moment, this module family is severly broken and underdocumented, |
|
|
61 | so do not use. This was uploaded mainly to reserve the CPAN namespace - |
|
|
62 | stay tuned! |
|
|
63 | |
59 | |
64 | =head1 CONCEPTS |
60 | =head1 CONCEPTS |
65 | |
61 | |
66 | =over 4 |
62 | =over 4 |
67 | |
63 | |
68 | =item port |
64 | =item port |
69 | |
65 | |
70 | A port is something you can send messages to (with the C<snd> function). |
66 | Not to be confused with a TCP port, a "port" is something you can send |
|
|
67 | messages to (with the C<snd> function). |
71 | |
68 | |
72 | Ports allow you to register C<rcv> handlers that can match all or just |
69 | Ports allow you to register C<rcv> handlers that can match all or just |
73 | some messages. Messages send to ports will not be queued, regardless of |
70 | some messages. Messages send to ports will not be queued, regardless of |
74 | anything was listening for them or not. |
71 | anything was listening for them or not. |
75 | |
72 | |
76 | =item port ID - C<noderef#portname> |
73 | =item port ID - C<nodeid#portname> |
77 | |
74 | |
78 | A port ID is the concatenation of a noderef, a hash-mark (C<#>) as |
75 | A port ID is the concatenation of a node ID, a hash-mark (C<#>) as |
79 | separator, and a port name (a printable string of unspecified format). An |
76 | separator, and a port name (a printable string of unspecified format). |
80 | exception is the the node port, whose ID is identical to its node |
|
|
81 | reference. |
|
|
82 | |
77 | |
83 | =item node |
78 | =item node |
84 | |
79 | |
85 | A node is a single process containing at least one port - the node port, |
80 | A node is a single process containing at least one port - the node port, |
86 | which provides nodes to manage each other remotely, and to create new |
81 | which enables nodes to manage each other remotely, and to create new |
87 | ports. |
82 | ports. |
88 | |
83 | |
89 | Nodes are either private (single-process only), slaves (can only talk to |
84 | Nodes are either public (have one or more listening ports) or private |
90 | public nodes, but do not need an open port) or public nodes (connectable |
85 | (no listening ports). Private nodes cannot talk to other private nodes |
91 | from any other node). |
86 | currently. |
92 | |
87 | |
93 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
88 | =item node ID - C<[A-Z_][a-zA-Z0-9_\-.:]*> |
94 | |
89 | |
95 | A node ID is a string that uniquely identifies the node within a |
90 | A node ID is a string that uniquely identifies the node within a |
96 | network. Depending on the configuration used, node IDs can look like a |
91 | network. Depending on the configuration used, node IDs can look like a |
97 | hostname, a hostname and a port, or a random string. AnyEvent::MP itself |
92 | hostname, a hostname and a port, or a random string. AnyEvent::MP itself |
98 | doesn't interpret node IDs in any way. |
93 | doesn't interpret node IDs in any way. |
… | |
… | |
102 | Nodes can only talk to each other by creating some kind of connection to |
97 | Nodes can only talk to each other by creating some kind of connection to |
103 | each other. To do this, nodes should listen on one or more local transport |
98 | each other. To do this, nodes should listen on one or more local transport |
104 | endpoints - binds. Currently, only standard C<ip:port> specifications can |
99 | endpoints - binds. Currently, only standard C<ip:port> specifications can |
105 | be used, which specify TCP ports to listen on. |
100 | be used, which specify TCP ports to listen on. |
106 | |
101 | |
107 | =item seeds - C<host:port> |
102 | =item seed nodes |
108 | |
103 | |
109 | When a node starts, it knows nothing about the network. To teach the node |
104 | When a node starts, it knows nothing about the network. To teach the node |
110 | about the network it first has to contact some other node within the |
105 | about the network it first has to contact some other node within the |
111 | network. This node is called a seed. |
106 | network. This node is called a seed. |
112 | |
107 | |
113 | Seeds are transport endpoint(s) of as many nodes as one wants. Those nodes |
108 | Apart from the fact that other nodes know them as seed nodes and they have |
|
|
109 | to have fixed listening addresses, seed nodes are perfectly normal nodes - |
|
|
110 | any node can function as a seed node for others. |
|
|
111 | |
|
|
112 | In addition to discovering the network, seed nodes are also used to |
|
|
113 | maintain the network and to connect nodes that otherwise would have |
|
|
114 | trouble connecting. They form the backbone of an AnyEvent::MP network. |
|
|
115 | |
114 | are expected to be long-running, and at least one of those should always |
116 | Seed nodes are expected to be long-running, and at least one seed node |
115 | be available. When nodes run out of connections (e.g. due to a network |
117 | should always be available. They should also be relatively responsive - a |
116 | error), they try to re-establish connections to some seednodes again to |
118 | seed node that blocks for long periods will slow down everybody else. |
117 | join the network. |
119 | |
|
|
120 | =item seeds - C<host:port> |
|
|
121 | |
|
|
122 | Seeds are transport endpoint(s) (usually a hostname/IP address and a |
|
|
123 | TCP port) of nodes thta should be used as seed nodes. |
|
|
124 | |
|
|
125 | The nodes listening on those endpoints are expected to be long-running, |
|
|
126 | and at least one of those should always be available. When nodes run out |
|
|
127 | of connections (e.g. due to a network error), they try to re-establish |
|
|
128 | connections to some seednodes again to join the network. |
118 | |
129 | |
119 | =back |
130 | =back |
120 | |
131 | |
121 | =head1 VARIABLES/FUNCTIONS |
132 | =head1 VARIABLES/FUNCTIONS |
122 | |
133 | |
… | |
… | |
138 | |
149 | |
139 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
150 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
140 | |
151 | |
141 | our @EXPORT = qw( |
152 | our @EXPORT = qw( |
142 | NODE $NODE *SELF node_of after |
153 | NODE $NODE *SELF node_of after |
143 | resolve_node initialise_node |
154 | configure |
144 | snd rcv mon mon_guard kil reg psub spawn |
155 | snd rcv mon mon_guard kil reg psub spawn |
145 | port |
156 | port |
146 | ); |
157 | ); |
147 | |
158 | |
148 | our $SELF; |
159 | our $SELF; |
… | |
… | |
153 | kil $SELF, die => $msg; |
164 | kil $SELF, die => $msg; |
154 | } |
165 | } |
155 | |
166 | |
156 | =item $thisnode = NODE / $NODE |
167 | =item $thisnode = NODE / $NODE |
157 | |
168 | |
158 | The C<NODE> function returns, and the C<$NODE> variable contains the node |
169 | The C<NODE> function returns, and the C<$NODE> variable contains, the node |
159 | ID of the node running in the current process. This value is initialised by |
170 | ID of the node running in the current process. This value is initialised by |
160 | a call to C<initialise_node>. |
171 | a call to C<configure>. |
161 | |
172 | |
162 | =item $nodeid = node_of $port |
173 | =item $nodeid = node_of $port |
163 | |
174 | |
164 | Extracts and returns the node ID part from a port ID or a node ID. |
175 | Extracts and returns the node ID from a port ID or a node ID. |
165 | |
176 | |
166 | =item initialise_node $profile_name |
177 | =item configure $profile, key => value... |
|
|
178 | |
|
|
179 | =item configure key => value... |
167 | |
180 | |
168 | Before a node can talk to other nodes on the network (i.e. enter |
181 | Before a node can talk to other nodes on the network (i.e. enter |
169 | "distributed mode") it has to initialise itself - the minimum a node needs |
182 | "distributed mode") it has to configure itself - the minimum a node needs |
170 | to know is its own name, and optionally it should know the addresses of |
183 | to know is its own name, and optionally it should know the addresses of |
171 | some other nodes in the network to discover other nodes. |
184 | some other nodes in the network to discover other nodes. |
172 | |
185 | |
173 | This function initialises a node - it must be called exactly once (or |
186 | This function configures a node - it must be called exactly once (or |
174 | never) before calling other AnyEvent::MP functions. |
187 | never) before calling other AnyEvent::MP functions. |
175 | |
188 | |
176 | The first argument is a profile name. If it is C<undef> or missing, then |
189 | =over 4 |
177 | the current nodename will be used instead (i.e. F<uname -n>). |
|
|
178 | |
190 | |
|
|
191 | =item step 1, gathering configuration from profiles |
|
|
192 | |
179 | The function then looks up the profile in the aemp configuration (see the |
193 | The function first looks up a profile in the aemp configuration (see the |
180 | L<aemp> commandline utility). |
194 | L<aemp> commandline utility). The profile name can be specified via the |
|
|
195 | named C<profile> parameter or can simply be the first parameter). If it is |
|
|
196 | missing, then the nodename (F<uname -n>) will be used as profile name. |
|
|
197 | |
|
|
198 | The profile data is then gathered as follows: |
|
|
199 | |
|
|
200 | First, all remaining key => value pairs (all of which are conveniently |
|
|
201 | undocumented at the moment) will be interpreted as configuration |
|
|
202 | data. Then they will be overwritten by any values specified in the global |
|
|
203 | default configuration (see the F<aemp> utility), then the chain of |
|
|
204 | profiles chosen by the profile name (and any C<parent> attributes). |
|
|
205 | |
|
|
206 | That means that the values specified in the profile have highest priority |
|
|
207 | and the values specified directly via C<configure> have lowest priority, |
|
|
208 | and can only be used to specify defaults. |
181 | |
209 | |
182 | If the profile specifies a node ID, then this will become the node ID of |
210 | If the profile specifies a node ID, then this will become the node ID of |
183 | this process. If not, then the profile name will be used as node ID. The |
211 | this process. If not, then the profile name will be used as node ID. The |
184 | special node ID of C<anon/> will be replaced by a random node ID. |
212 | special node ID of C<anon/> will be replaced by a random node ID. |
|
|
213 | |
|
|
214 | =item step 2, bind listener sockets |
185 | |
215 | |
186 | The next step is to look up the binds in the profile, followed by binding |
216 | The next step is to look up the binds in the profile, followed by binding |
187 | aemp protocol listeners on all binds specified (it is possible and valid |
217 | aemp protocol listeners on all binds specified (it is possible and valid |
188 | to have no binds, meaning that the node cannot be contacted form the |
218 | to have no binds, meaning that the node cannot be contacted form the |
189 | outside. This means the node cannot talk to other nodes that also have no |
219 | outside. This means the node cannot talk to other nodes that also have no |
190 | binds, but it can still talk to all "normal" nodes). |
220 | binds, but it can still talk to all "normal" nodes). |
191 | |
221 | |
192 | If the profile does not specify a binds list, then the node ID will be |
222 | If the profile does not specify a binds list, then a default of C<*> is |
193 | treated as if it were of the form C<host:port>, which will be resolved and |
223 | used, meaning the node will bind on a dynamically-assigned port on every |
194 | used as binds list. |
224 | local IP address it finds. |
195 | |
225 | |
|
|
226 | =item step 3, connect to seed nodes |
|
|
227 | |
196 | Lastly, the seeds list from the profile is passed to the |
228 | As the last step, the seeds list from the profile is passed to the |
197 | L<AnyEvent::MP::Global> module, which will then use it to keep |
229 | L<AnyEvent::MP::Global> module, which will then use it to keep |
198 | connectivity with at least on of those seed nodes at any point in time. |
230 | connectivity with at least one node at any point in time. |
199 | |
231 | |
200 | Example: become a distributed node listening on the guessed noderef, or |
232 | =back |
201 | the one specified via C<aemp> for the current node. This should be the |
233 | |
|
|
234 | Example: become a distributed node using the locla node name as profile. |
202 | most common form of invocation for "daemon"-type nodes. |
235 | This should be the most common form of invocation for "daemon"-type nodes. |
203 | |
236 | |
204 | initialise_node; |
237 | configure |
205 | |
238 | |
206 | Example: become an anonymous node. This form is often used for commandline |
239 | Example: become an anonymous node. This form is often used for commandline |
207 | clients. |
240 | clients. |
208 | |
241 | |
209 | initialise_node "anon/"; |
242 | configure nodeid => "anon/"; |
210 | |
243 | |
211 | Example: become a distributed node. If there is no profile of the given |
244 | Example: configure a node using a profile called seed, which si suitable |
212 | name, or no binds list was specified, resolve C<localhost:4044> and bind |
245 | for a seed node as it binds on all local addresses on a fixed port (4040, |
213 | on the resulting addresses. |
246 | customary for aemp). |
214 | |
247 | |
215 | initialise_node "localhost:4044"; |
248 | # use the aemp commandline utility |
|
|
249 | # aemp profile seed nodeid anon/ binds '*:4040' |
|
|
250 | |
|
|
251 | # then use it |
|
|
252 | configure profile => "seed"; |
|
|
253 | |
|
|
254 | # or simply use aemp from the shell again: |
|
|
255 | # aemp run profile seed |
|
|
256 | |
|
|
257 | # or provide a nicer-to-remember nodeid |
|
|
258 | # aemp run profile seed nodeid "$(hostname)" |
216 | |
259 | |
217 | =item $SELF |
260 | =item $SELF |
218 | |
261 | |
219 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
262 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
220 | blocks. |
263 | blocks. |
221 | |
264 | |
222 | =item SELF, %SELF, @SELF... |
265 | =item *SELF, SELF, %SELF, @SELF... |
223 | |
266 | |
224 | Due to some quirks in how perl exports variables, it is impossible to |
267 | Due to some quirks in how perl exports variables, it is impossible to |
225 | just export C<$SELF>, all the symbols called C<SELF> are exported by this |
268 | just export C<$SELF>, all the symbols named C<SELF> are exported by this |
226 | module, but only C<$SELF> is currently used. |
269 | module, but only C<$SELF> is currently used. |
227 | |
270 | |
228 | =item snd $port, type => @data |
271 | =item snd $port, type => @data |
229 | |
272 | |
230 | =item snd $port, @msg |
273 | =item snd $port, @msg |
231 | |
274 | |
232 | Send the given message to the given port ID, which can identify either |
275 | Send the given message to the given port, which can identify either a |
233 | a local or a remote port, and must be a port ID. |
276 | local or a remote port, and must be a port ID. |
234 | |
277 | |
235 | While the message can be about anything, it is highly recommended to use a |
278 | While the message can be almost anything, it is highly recommended to |
236 | string as first element (a port ID, or some word that indicates a request |
279 | use a string as first element (a port ID, or some word that indicates a |
237 | type etc.). |
280 | request type etc.) and to consist if only simple perl values (scalars, |
|
|
281 | arrays, hashes) - if you think you need to pass an object, think again. |
238 | |
282 | |
239 | The message data effectively becomes read-only after a call to this |
283 | The message data logically becomes read-only after a call to this |
240 | function: modifying any argument is not allowed and can cause many |
284 | function: modifying any argument (or values referenced by them) is |
241 | problems. |
285 | forbidden, as there can be considerable time between the call to C<snd> |
|
|
286 | and the time the message is actually being serialised - in fact, it might |
|
|
287 | never be copied as within the same process it is simply handed to the |
|
|
288 | receiving port. |
242 | |
289 | |
243 | The type of data you can transfer depends on the transport protocol: when |
290 | The type of data you can transfer depends on the transport protocol: when |
244 | JSON is used, then only strings, numbers and arrays and hashes consisting |
291 | JSON is used, then only strings, numbers and arrays and hashes consisting |
245 | of those are allowed (no objects). When Storable is used, then anything |
292 | of those are allowed (no objects). When Storable is used, then anything |
246 | that Storable can serialise and deserialise is allowed, and for the local |
293 | that Storable can serialise and deserialise is allowed, and for the local |
247 | node, anything can be passed. |
294 | node, anything can be passed. Best rely only on the common denominator of |
|
|
295 | these. |
248 | |
296 | |
249 | =item $local_port = port |
297 | =item $local_port = port |
250 | |
298 | |
251 | Create a new local port object and returns its port ID. Initially it has |
299 | Create a new local port object and returns its port ID. Initially it has |
252 | no callbacks set and will throw an error when it receives messages. |
300 | no callbacks set and will throw an error when it receives messages. |
… | |
… | |
337 | |
385 | |
338 | =cut |
386 | =cut |
339 | |
387 | |
340 | sub rcv($@) { |
388 | sub rcv($@) { |
341 | my $port = shift; |
389 | my $port = shift; |
342 | my ($noderef, $portid) = split /#/, $port, 2; |
390 | my ($nodeid, $portid) = split /#/, $port, 2; |
343 | |
391 | |
344 | $NODE{$noderef} == $NODE{""} |
392 | $NODE{$nodeid} == $NODE{""} |
345 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
393 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
346 | |
394 | |
347 | while (@_) { |
395 | while (@_) { |
348 | if (ref $_[0]) { |
396 | if (ref $_[0]) { |
349 | if (my $self = $PORT_DATA{$portid}) { |
397 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
428 | $res |
476 | $res |
429 | } |
477 | } |
430 | } |
478 | } |
431 | } |
479 | } |
432 | |
480 | |
433 | =item $guard = mon $port, $cb->(@reason) |
481 | =item $guard = mon $port, $cb->(@reason) # call $cb when $port dies |
434 | |
482 | |
435 | =item $guard = mon $port, $rcvport |
483 | =item $guard = mon $port, $rcvport # kill $rcvport when $port dies |
436 | |
484 | |
437 | =item $guard = mon $port |
485 | =item $guard = mon $port # kill $SELF when $port dies |
438 | |
486 | |
439 | =item $guard = mon $port, $rcvport, @msg |
487 | =item $guard = mon $port, $rcvport, @msg # send a message when $port dies |
440 | |
488 | |
441 | Monitor the given port and do something when the port is killed or |
489 | Monitor the given port and do something when the port is killed or |
442 | messages to it were lost, and optionally return a guard that can be used |
490 | messages to it were lost, and optionally return a guard that can be used |
443 | to stop monitoring again. |
491 | to stop monitoring again. |
444 | |
|
|
445 | C<mon> effectively guarantees that, in the absence of hardware failures, |
|
|
446 | that after starting the monitor, either all messages sent to the port |
|
|
447 | will arrive, or the monitoring action will be invoked after possible |
|
|
448 | message loss has been detected. No messages will be lost "in between" |
|
|
449 | (after the first lost message no further messages will be received by the |
|
|
450 | port). After the monitoring action was invoked, further messages might get |
|
|
451 | delivered again. |
|
|
452 | |
|
|
453 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
454 | and will not trigger again. |
|
|
455 | |
492 | |
456 | In the first form (callback), the callback is simply called with any |
493 | In the first form (callback), the callback is simply called with any |
457 | number of C<@reason> elements (no @reason means that the port was deleted |
494 | number of C<@reason> elements (no @reason means that the port was deleted |
458 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
495 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
459 | C<eval> if unsure. |
496 | C<eval> if unsure. |
460 | |
497 | |
461 | In the second form (another port given), the other port (C<$rcvport>) |
498 | In the second form (another port given), the other port (C<$rcvport>) |
462 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
499 | will be C<kil>'ed with C<@reason>, if a @reason was specified, i.e. on |
463 | "normal" kils nothing happens, while under all other conditions, the other |
500 | "normal" kils nothing happens, while under all other conditions, the other |
464 | port is killed with the same reason. |
501 | port is killed with the same reason. |
465 | |
502 | |
466 | The third form (kill self) is the same as the second form, except that |
503 | The third form (kill self) is the same as the second form, except that |
467 | C<$rvport> defaults to C<$SELF>. |
504 | C<$rvport> defaults to C<$SELF>. |
468 | |
505 | |
469 | In the last form (message), a message of the form C<@msg, @reason> will be |
506 | In the last form (message), a message of the form C<@msg, @reason> will be |
470 | C<snd>. |
507 | C<snd>. |
|
|
508 | |
|
|
509 | Monitoring-actions are one-shot: once messages are lost (and a monitoring |
|
|
510 | alert was raised), they are removed and will not trigger again. |
471 | |
511 | |
472 | As a rule of thumb, monitoring requests should always monitor a port from |
512 | As a rule of thumb, monitoring requests should always monitor a port from |
473 | a local port (or callback). The reason is that kill messages might get |
513 | a local port (or callback). The reason is that kill messages might get |
474 | lost, just like any other message. Another less obvious reason is that |
514 | lost, just like any other message. Another less obvious reason is that |
475 | even monitoring requests can get lost (for exmaple, when the connection |
515 | even monitoring requests can get lost (for example, when the connection |
476 | to the other node goes down permanently). When monitoring a port locally |
516 | to the other node goes down permanently). When monitoring a port locally |
477 | these problems do not exist. |
517 | these problems do not exist. |
478 | |
518 | |
|
|
519 | C<mon> effectively guarantees that, in the absence of hardware failures, |
|
|
520 | after starting the monitor, either all messages sent to the port will |
|
|
521 | arrive, or the monitoring action will be invoked after possible message |
|
|
522 | loss has been detected. No messages will be lost "in between" (after |
|
|
523 | the first lost message no further messages will be received by the |
|
|
524 | port). After the monitoring action was invoked, further messages might get |
|
|
525 | delivered again. |
|
|
526 | |
|
|
527 | Inter-host-connection timeouts and monitoring depend on the transport |
|
|
528 | used. The only transport currently implemented is TCP, and AnyEvent::MP |
|
|
529 | relies on TCP to detect node-downs (this can take 10-15 minutes on a |
|
|
530 | non-idle connection, and usually around two hours for idle conenctions). |
|
|
531 | |
|
|
532 | This means that monitoring is good for program errors and cleaning up |
|
|
533 | stuff eventually, but they are no replacement for a timeout when you need |
|
|
534 | to ensure some maximum latency. |
|
|
535 | |
479 | Example: call a given callback when C<$port> is killed. |
536 | Example: call a given callback when C<$port> is killed. |
480 | |
537 | |
481 | mon $port, sub { warn "port died because of <@_>\n" }; |
538 | mon $port, sub { warn "port died because of <@_>\n" }; |
482 | |
539 | |
483 | Example: kill ourselves when C<$port> is killed abnormally. |
540 | Example: kill ourselves when C<$port> is killed abnormally. |
… | |
… | |
489 | mon $port, $self => "restart"; |
546 | mon $port, $self => "restart"; |
490 | |
547 | |
491 | =cut |
548 | =cut |
492 | |
549 | |
493 | sub mon { |
550 | sub mon { |
494 | my ($noderef, $port) = split /#/, shift, 2; |
551 | my ($nodeid, $port) = split /#/, shift, 2; |
495 | |
552 | |
496 | my $node = $NODE{$noderef} || add_node $noderef; |
553 | my $node = $NODE{$nodeid} || add_node $nodeid; |
497 | |
554 | |
498 | my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
555 | my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
499 | |
556 | |
500 | unless (ref $cb) { |
557 | unless (ref $cb) { |
501 | if (@_) { |
558 | if (@_) { |
… | |
… | |
521 | is killed, the references will be freed. |
578 | is killed, the references will be freed. |
522 | |
579 | |
523 | Optionally returns a guard that will stop the monitoring. |
580 | Optionally returns a guard that will stop the monitoring. |
524 | |
581 | |
525 | This function is useful when you create e.g. timers or other watchers and |
582 | This function is useful when you create e.g. timers or other watchers and |
526 | want to free them when the port gets killed: |
583 | want to free them when the port gets killed (note the use of C<psub>): |
527 | |
584 | |
528 | $port->rcv (start => sub { |
585 | $port->rcv (start => sub { |
529 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { |
586 | my $timer; $timer = mon_guard $port, AE::timer 1, 1, psub { |
530 | undef $timer if 0.9 < rand; |
587 | undef $timer if 0.9 < rand; |
531 | }); |
588 | }); |
532 | }); |
589 | }); |
533 | |
590 | |
534 | =cut |
591 | =cut |
… | |
… | |
543 | |
600 | |
544 | =item kil $port[, @reason] |
601 | =item kil $port[, @reason] |
545 | |
602 | |
546 | Kill the specified port with the given C<@reason>. |
603 | Kill the specified port with the given C<@reason>. |
547 | |
604 | |
548 | If no C<@reason> is specified, then the port is killed "normally" (linked |
605 | If no C<@reason> is specified, then the port is killed "normally" (ports |
549 | ports will not be kileld, or even notified). |
606 | monitoring other ports will not necessarily die because a port dies |
|
|
607 | "normally"). |
550 | |
608 | |
551 | Otherwise, linked ports get killed with the same reason (second form of |
609 | Otherwise, linked ports get killed with the same reason (second form of |
552 | C<mon>, see below). |
610 | C<mon>, see above). |
553 | |
611 | |
554 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
612 | Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks |
555 | will be reported as reason C<< die => $@ >>. |
613 | will be reported as reason C<< die => $@ >>. |
556 | |
614 | |
557 | Transport/communication errors are reported as C<< transport_error => |
615 | Transport/communication errors are reported as C<< transport_error => |
… | |
… | |
562 | =item $port = spawn $node, $initfunc[, @initdata] |
620 | =item $port = spawn $node, $initfunc[, @initdata] |
563 | |
621 | |
564 | Creates a port on the node C<$node> (which can also be a port ID, in which |
622 | Creates a port on the node C<$node> (which can also be a port ID, in which |
565 | case it's the node where that port resides). |
623 | case it's the node where that port resides). |
566 | |
624 | |
567 | The port ID of the newly created port is return immediately, and it is |
625 | The port ID of the newly created port is returned immediately, and it is |
568 | permissible to immediately start sending messages or monitor the port. |
626 | possible to immediately start sending messages or to monitor the port. |
569 | |
627 | |
570 | After the port has been created, the init function is |
628 | After the port has been created, the init function is called on the remote |
571 | called. This function must be a fully-qualified function name |
629 | node, in the same context as a C<rcv> callback. This function must be a |
572 | (e.g. C<MyApp::Chat::Server::init>). To specify a function in the main |
630 | fully-qualified function name (e.g. C<MyApp::Chat::Server::init>). To |
573 | program, use C<::name>. |
631 | specify a function in the main program, use C<::name>. |
574 | |
632 | |
575 | If the function doesn't exist, then the node tries to C<require> |
633 | If the function doesn't exist, then the node tries to C<require> |
576 | the package, then the package above the package and so on (e.g. |
634 | the package, then the package above the package and so on (e.g. |
577 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
635 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
578 | exists or it runs out of package names. |
636 | exists or it runs out of package names. |
579 | |
637 | |
580 | The init function is then called with the newly-created port as context |
638 | The init function is then called with the newly-created port as context |
581 | object (C<$SELF>) and the C<@initdata> values as arguments. |
639 | object (C<$SELF>) and the C<@initdata> values as arguments. It I<must> |
|
|
640 | call one of the C<rcv> functions to set callbacks on C<$SELF>, otherwise |
|
|
641 | the port might not get created. |
582 | |
642 | |
583 | A common idiom is to pass your own port, monitor the spawned port, and |
643 | A common idiom is to pass a local port, immediately monitor the spawned |
584 | in the init function, monitor the original port. This two-way monitoring |
644 | port, and in the remote init function, immediately monitor the passed |
585 | ensures that both ports get cleaned up when there is a problem. |
645 | local port. This two-way monitoring ensures that both ports get cleaned up |
|
|
646 | when there is a problem. |
|
|
647 | |
|
|
648 | C<spawn> guarantees that the C<$initfunc> has no visible effects on the |
|
|
649 | caller before C<spawn> returns (by delaying invocation when spawn is |
|
|
650 | called for the local node). |
586 | |
651 | |
587 | Example: spawn a chat server port on C<$othernode>. |
652 | Example: spawn a chat server port on C<$othernode>. |
588 | |
653 | |
589 | # this node, executed from within a port context: |
654 | # this node, executed from within a port context: |
590 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
655 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
… | |
… | |
605 | |
670 | |
606 | sub _spawn { |
671 | sub _spawn { |
607 | my $port = shift; |
672 | my $port = shift; |
608 | my $init = shift; |
673 | my $init = shift; |
609 | |
674 | |
|
|
675 | # rcv will create the actual port |
610 | local $SELF = "$NODE#$port"; |
676 | local $SELF = "$NODE#$port"; |
611 | eval { |
677 | eval { |
612 | &{ load_func $init } |
678 | &{ load_func $init } |
613 | }; |
679 | }; |
614 | _self_die if $@; |
680 | _self_die if $@; |
615 | } |
681 | } |
616 | |
682 | |
617 | sub spawn(@) { |
683 | sub spawn(@) { |
618 | my ($noderef, undef) = split /#/, shift, 2; |
684 | my ($nodeid, undef) = split /#/, shift, 2; |
619 | |
685 | |
620 | my $id = "$RUNIQ." . $ID++; |
686 | my $id = "$RUNIQ." . $ID++; |
621 | |
687 | |
622 | $_[0] =~ /::/ |
688 | $_[0] =~ /::/ |
623 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
689 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
624 | |
690 | |
625 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
691 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
626 | |
692 | |
627 | "$noderef#$id" |
693 | "$nodeid#$id" |
628 | } |
694 | } |
629 | |
695 | |
630 | =item after $timeout, @msg |
696 | =item after $timeout, @msg |
631 | |
697 | |
632 | =item after $timeout, $callback |
698 | =item after $timeout, $callback |
633 | |
699 | |
634 | Either sends the given message, or call the given callback, after the |
700 | Either sends the given message, or call the given callback, after the |
635 | specified number of seconds. |
701 | specified number of seconds. |
636 | |
702 | |
637 | This is simply a utility function that come sin handy at times. |
703 | This is simply a utility function that comes in handy at times - the |
|
|
704 | AnyEvent::MP author is not convinced of the wisdom of having it, though, |
|
|
705 | so it may go away in the future. |
638 | |
706 | |
639 | =cut |
707 | =cut |
640 | |
708 | |
641 | sub after($@) { |
709 | sub after($@) { |
642 | my ($timeout, @action) = @_; |
710 | my ($timeout, @action) = @_; |
… | |
… | |
669 | |
737 | |
670 | =item * Node IDs are arbitrary strings in AEMP. |
738 | =item * Node IDs are arbitrary strings in AEMP. |
671 | |
739 | |
672 | Erlang relies on special naming and DNS to work everywhere in the same |
740 | Erlang relies on special naming and DNS to work everywhere in the same |
673 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
741 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
674 | configuraiton or DNS), but will otherwise discover other odes itself. |
742 | configuration or DNS), but will otherwise discover other odes itself. |
675 | |
743 | |
676 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
744 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
677 | uses "local ports are like remote ports". |
745 | uses "local ports are like remote ports". |
678 | |
746 | |
679 | The failure modes for local ports are quite different (runtime errors |
747 | The failure modes for local ports are quite different (runtime errors |
… | |
… | |
692 | |
760 | |
693 | Erlang uses processes that selectively receive messages, and therefore |
761 | Erlang uses processes that selectively receive messages, and therefore |
694 | needs a queue. AEMP is event based, queuing messages would serve no |
762 | needs a queue. AEMP is event based, queuing messages would serve no |
695 | useful purpose. For the same reason the pattern-matching abilities of |
763 | useful purpose. For the same reason the pattern-matching abilities of |
696 | AnyEvent::MP are more limited, as there is little need to be able to |
764 | AnyEvent::MP are more limited, as there is little need to be able to |
697 | filter messages without dequeing them. |
765 | filter messages without dequeuing them. |
698 | |
766 | |
699 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
767 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
700 | |
768 | |
701 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
769 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
702 | |
770 | |
… | |
… | |
733 | =item * The AEMP protocol is optimised for both text-based and binary |
801 | =item * The AEMP protocol is optimised for both text-based and binary |
734 | communications. |
802 | communications. |
735 | |
803 | |
736 | The AEMP protocol, unlike the Erlang protocol, supports both programming |
804 | The AEMP protocol, unlike the Erlang protocol, supports both programming |
737 | language independent text-only protocols (good for debugging) and binary, |
805 | language independent text-only protocols (good for debugging) and binary, |
738 | language-specific serialisers (e.g. Storable). |
806 | language-specific serialisers (e.g. Storable). By default, unless TLS is |
|
|
807 | used, the protocol is actually completely text-based. |
739 | |
808 | |
740 | It has also been carefully designed to be implementable in other languages |
809 | It has also been carefully designed to be implementable in other languages |
741 | with a minimum of work while gracefully degrading functionality to make the |
810 | with a minimum of work while gracefully degrading functionality to make the |
742 | protocol simple. |
811 | protocol simple. |
743 | |
812 | |
… | |
… | |
749 | Erlang, as one can choose between automatic kill, exit message or callback |
818 | Erlang, as one can choose between automatic kill, exit message or callback |
750 | on a per-process basis. |
819 | on a per-process basis. |
751 | |
820 | |
752 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
821 | =item * Erlang tries to hide remote/local connections, AEMP does not. |
753 | |
822 | |
754 | Monitoring in Erlang is not an indicator of process death/crashes, |
823 | Monitoring in Erlang is not an indicator of process death/crashes, in the |
755 | as linking is (except linking is unreliable in Erlang). |
824 | same way as linking is (except linking is unreliable in Erlang). |
756 | |
825 | |
757 | In AEMP, you don't "look up" registered port names or send to named ports |
826 | In AEMP, you don't "look up" registered port names or send to named ports |
758 | that might or might not be persistent. Instead, you normally spawn a port |
827 | that might or might not be persistent. Instead, you normally spawn a port |
759 | on the remote node. The init function monitors the you, and you monitor |
828 | on the remote node. The init function monitors you, and you monitor the |
760 | the remote port. Since both monitors are local to the node, they are much |
829 | remote port. Since both monitors are local to the node, they are much more |
761 | more reliable. |
830 | reliable (no need for C<spawn_link>). |
762 | |
831 | |
763 | This also saves round-trips and avoids sending messages to the wrong port |
832 | This also saves round-trips and avoids sending messages to the wrong port |
764 | (hard to do in Erlang). |
833 | (hard to do in Erlang). |
765 | |
834 | |
766 | =back |
835 | =back |
767 | |
836 | |
768 | =head1 RATIONALE |
837 | =head1 RATIONALE |
769 | |
838 | |
770 | =over 4 |
839 | =over 4 |
771 | |
840 | |
772 | =item Why strings for ports and noderefs, why not objects? |
841 | =item Why strings for port and node IDs, why not objects? |
773 | |
842 | |
774 | We considered "objects", but found that the actual number of methods |
843 | We considered "objects", but found that the actual number of methods |
775 | thatc an be called are very low. Since port IDs and noderefs travel over |
844 | that can be called are quite low. Since port and node IDs travel over |
776 | the network frequently, the serialising/deserialising would add lots of |
845 | the network frequently, the serialising/deserialising would add lots of |
777 | overhead, as well as having to keep a proxy object. |
846 | overhead, as well as having to keep a proxy object everywhere. |
778 | |
847 | |
779 | Strings can easily be printed, easily serialised etc. and need no special |
848 | Strings can easily be printed, easily serialised etc. and need no special |
780 | procedures to be "valid". |
849 | procedures to be "valid". |
781 | |
850 | |
782 | And a a miniport consists of a single closure stored in a global hash - it |
851 | And as a result, a miniport consists of a single closure stored in a |
783 | can't become much cheaper. |
852 | global hash - it can't become much cheaper. |
784 | |
853 | |
785 | =item Why favour JSON, why not real serialising format such as Storable? |
854 | =item Why favour JSON, why not a real serialising format such as Storable? |
786 | |
855 | |
787 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
856 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
788 | format, but currently there is no way to make a node use Storable by |
857 | format, but currently there is no way to make a node use Storable by |
789 | default. |
858 | default (although all nodes will accept it). |
790 | |
859 | |
791 | The default framing protocol is JSON because a) JSON::XS is many times |
860 | The default framing protocol is JSON because a) JSON::XS is many times |
792 | faster for small messages and b) most importantly, after years of |
861 | faster for small messages and b) most importantly, after years of |
793 | experience we found that object serialisation is causing more problems |
862 | experience we found that object serialisation is causing more problems |
794 | than it gains: Just like function calls, objects simply do not travel |
863 | than it solves: Just like function calls, objects simply do not travel |
795 | easily over the network, mostly because they will always be a copy, so you |
864 | easily over the network, mostly because they will always be a copy, so you |
796 | always have to re-think your design. |
865 | always have to re-think your design. |
797 | |
866 | |
798 | Keeping your messages simple, concentrating on data structures rather than |
867 | Keeping your messages simple, concentrating on data structures rather than |
799 | objects, will keep your messages clean, tidy and efficient. |
868 | objects, will keep your messages clean, tidy and efficient. |
800 | |
869 | |
801 | =back |
870 | =back |
802 | |
871 | |
803 | =head1 SEE ALSO |
872 | =head1 SEE ALSO |
804 | |
873 | |
|
|
874 | L<AnyEvent::MP::Intro> - a gentle introduction. |
|
|
875 | |
|
|
876 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
|
|
877 | |
|
|
878 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
|
|
879 | your applications. |
|
|
880 | |
|
|
881 | L<AnyEvent::MP::LogCatcher> - simple service to display log messages from |
|
|
882 | all nodes. |
|
|
883 | |
805 | L<AnyEvent>. |
884 | L<AnyEvent>. |
806 | |
885 | |
807 | =head1 AUTHOR |
886 | =head1 AUTHOR |
808 | |
887 | |
809 | Marc Lehmann <schmorp@schmorp.de> |
888 | Marc Lehmann <schmorp@schmorp.de> |