… | |
… | |
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 | configure; |
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; |
… | |
… | |
40 | |
39 | |
41 | =head1 CURRENT STATUS |
40 | =head1 CURRENT STATUS |
42 | |
41 | |
43 | bin/aemp - stable. |
42 | bin/aemp - stable. |
44 | AnyEvent::MP - stable API, should work. |
43 | AnyEvent::MP - stable API, should work. |
45 | AnyEvent::MP::Intro - uptodate, but incomplete. |
44 | AnyEvent::MP::Intro - explains most concepts. |
46 | AnyEvent::MP::Kernel - mostly stable. |
45 | AnyEvent::MP::Kernel - mostly stable. |
47 | AnyEvent::MP::Global - stable API, protocol not yet final. |
46 | AnyEvent::MP::Global - stable but incomplete, protocol not yet final. |
48 | |
47 | |
49 | stay tuned. |
48 | stay tuned. |
50 | |
49 | |
51 | =head1 DESCRIPTION |
50 | =head1 DESCRIPTION |
52 | |
51 | |
53 | This module (-family) implements a simple message passing framework. |
52 | This module (-family) implements a simple message passing framework. |
54 | |
53 | |
… | |
… | |
56 | on the same or other hosts, and you can supervise entities remotely. |
55 | on the same or other hosts, and you can supervise entities remotely. |
57 | |
56 | |
58 | 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> |
59 | manual page and the examples under F<eg/>. |
58 | manual page and the examples under F<eg/>. |
60 | |
59 | |
61 | At the moment, this module family is a bit underdocumented. |
|
|
62 | |
|
|
63 | =head1 CONCEPTS |
60 | =head1 CONCEPTS |
64 | |
61 | |
65 | =over 4 |
62 | =over 4 |
66 | |
63 | |
67 | =item port |
64 | =item port |
68 | |
65 | |
69 | 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). |
70 | |
68 | |
71 | 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 |
72 | 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 |
73 | anything was listening for them or not. |
71 | anything was listening for them or not. |
74 | |
72 | |
… | |
… | |
161 | |
159 | |
162 | =item $nodeid = node_of $port |
160 | =item $nodeid = node_of $port |
163 | |
161 | |
164 | Extracts and returns the node ID from a port ID or a node ID. |
162 | Extracts and returns the node ID from a port ID or a node ID. |
165 | |
163 | |
|
|
164 | =item configure $profile, key => value... |
|
|
165 | |
166 | =item configure key => value... |
166 | =item configure key => value... |
167 | |
167 | |
168 | Before a node can talk to other nodes on the network (i.e. enter |
168 | Before a node can talk to other nodes on the network (i.e. enter |
169 | "distributed mode") it has to configure itself - the minimum a node needs |
169 | "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 |
170 | to know is its own name, and optionally it should know the addresses of |
… | |
… | |
177 | |
177 | |
178 | =item step 1, gathering configuration from profiles |
178 | =item step 1, gathering configuration from profiles |
179 | |
179 | |
180 | The function first looks up a profile in the aemp configuration (see the |
180 | The function first looks up a profile in the aemp configuration (see the |
181 | L<aemp> commandline utility). The profile name can be specified via the |
181 | L<aemp> commandline utility). The profile name can be specified via the |
182 | named C<profile> parameter. If it is missing, then the nodename (F<uname |
182 | named C<profile> parameter or can simply be the first parameter). If it is |
183 | -n>) will be used as profile name. |
183 | missing, then the nodename (F<uname -n>) will be used as profile name. |
184 | |
184 | |
185 | The profile data is then gathered as follows: |
185 | The profile data is then gathered as follows: |
186 | |
186 | |
187 | First, all remaining key => value pairs (all of which are conviniently |
187 | First, all remaining key => value pairs (all of which are conveniently |
188 | undocumented at the moment) will be interpreted as configuration |
188 | undocumented at the moment) will be interpreted as configuration |
189 | data. Then they will be overwritten by any values specified in the global |
189 | data. Then they will be overwritten by any values specified in the global |
190 | default configuration (see the F<aemp> utility), then the chain of |
190 | default configuration (see the F<aemp> utility), then the chain of |
191 | profiles chosen by the profile name (and any C<parent> attributes). |
191 | profiles chosen by the profile name (and any C<parent> attributes). |
192 | |
192 | |
… | |
… | |
231 | Example: configure a node using a profile called seed, which si suitable |
231 | Example: configure a node using a profile called seed, which si suitable |
232 | for a seed node as it binds on all local addresses on a fixed port (4040, |
232 | for a seed node as it binds on all local addresses on a fixed port (4040, |
233 | customary for aemp). |
233 | customary for aemp). |
234 | |
234 | |
235 | # use the aemp commandline utility |
235 | # use the aemp commandline utility |
236 | # aemp profile seed setnodeid anon/ setbinds '*:4040' |
236 | # aemp profile seed nodeid anon/ binds '*:4040' |
237 | |
237 | |
238 | # then use it |
238 | # then use it |
239 | configure profile => "seed"; |
239 | configure profile => "seed"; |
240 | |
240 | |
241 | # or simply use aemp from the shell again: |
241 | # or simply use aemp from the shell again: |
… | |
… | |
372 | |
372 | |
373 | =cut |
373 | =cut |
374 | |
374 | |
375 | sub rcv($@) { |
375 | sub rcv($@) { |
376 | my $port = shift; |
376 | my $port = shift; |
377 | my ($noderef, $portid) = split /#/, $port, 2; |
377 | my ($nodeid, $portid) = split /#/, $port, 2; |
378 | |
378 | |
379 | $NODE{$noderef} == $NODE{""} |
379 | $NODE{$nodeid} == $NODE{""} |
380 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
380 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
381 | |
381 | |
382 | while (@_) { |
382 | while (@_) { |
383 | if (ref $_[0]) { |
383 | if (ref $_[0]) { |
384 | if (my $self = $PORT_DATA{$portid}) { |
384 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
475 | |
475 | |
476 | Monitor the given port and do something when the port is killed or |
476 | Monitor the given port and do something when the port is killed or |
477 | messages to it were lost, and optionally return a guard that can be used |
477 | messages to it were lost, and optionally return a guard that can be used |
478 | to stop monitoring again. |
478 | to stop monitoring again. |
479 | |
479 | |
|
|
480 | In the first form (callback), the callback is simply called with any |
|
|
481 | number of C<@reason> elements (no @reason means that the port was deleted |
|
|
482 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
|
|
483 | C<eval> if unsure. |
|
|
484 | |
|
|
485 | In the second form (another port given), the other port (C<$rcvport>) |
|
|
486 | will be C<kil>'ed with C<@reason>, if a @reason was specified, i.e. on |
|
|
487 | "normal" kils nothing happens, while under all other conditions, the other |
|
|
488 | port is killed with the same reason. |
|
|
489 | |
|
|
490 | The third form (kill self) is the same as the second form, except that |
|
|
491 | C<$rvport> defaults to C<$SELF>. |
|
|
492 | |
|
|
493 | In the last form (message), a message of the form C<@msg, @reason> will be |
|
|
494 | C<snd>. |
|
|
495 | |
|
|
496 | Monitoring-actions are one-shot: once messages are lost (and a monitoring |
|
|
497 | alert was raised), they are removed and will not trigger again. |
|
|
498 | |
|
|
499 | As a rule of thumb, monitoring requests should always monitor a port from |
|
|
500 | a local port (or callback). The reason is that kill messages might get |
|
|
501 | lost, just like any other message. Another less obvious reason is that |
|
|
502 | even monitoring requests can get lost (for example, when the connection |
|
|
503 | to the other node goes down permanently). When monitoring a port locally |
|
|
504 | these problems do not exist. |
|
|
505 | |
480 | C<mon> effectively guarantees that, in the absence of hardware failures, |
506 | C<mon> effectively guarantees that, in the absence of hardware failures, |
481 | after starting the monitor, either all messages sent to the port will |
507 | after starting the monitor, either all messages sent to the port will |
482 | arrive, or the monitoring action will be invoked after possible message |
508 | arrive, or the monitoring action will be invoked after possible message |
483 | loss has been detected. No messages will be lost "in between" (after |
509 | loss has been detected. No messages will be lost "in between" (after |
484 | the first lost message no further messages will be received by the |
510 | the first lost message no further messages will be received by the |
485 | port). After the monitoring action was invoked, further messages might get |
511 | port). After the monitoring action was invoked, further messages might get |
486 | delivered again. |
512 | delivered again. |
487 | |
513 | |
488 | Note that monitoring-actions are one-shot: once messages are lost (and a |
514 | Inter-host-connection timeouts and monitoring depend on the transport |
489 | monitoring alert was raised), they are removed and will not trigger again. |
515 | used. The only transport currently implemented is TCP, and AnyEvent::MP |
|
|
516 | relies on TCP to detect node-downs (this can take 10-15 minutes on a |
|
|
517 | non-idle connection, and usually around two hours for idle conenctions). |
490 | |
518 | |
491 | In the first form (callback), the callback is simply called with any |
519 | This means that monitoring is good for program errors and cleaning up |
492 | number of C<@reason> elements (no @reason means that the port was deleted |
520 | stuff eventually, but they are no replacement for a timeout when you need |
493 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
521 | to ensure some maximum latency. |
494 | C<eval> if unsure. |
|
|
495 | |
|
|
496 | In the second form (another port given), the other port (C<$rcvport>) |
|
|
497 | will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on |
|
|
498 | "normal" kils nothing happens, while under all other conditions, the other |
|
|
499 | port is killed with the same reason. |
|
|
500 | |
|
|
501 | The third form (kill self) is the same as the second form, except that |
|
|
502 | C<$rvport> defaults to C<$SELF>. |
|
|
503 | |
|
|
504 | In the last form (message), a message of the form C<@msg, @reason> will be |
|
|
505 | C<snd>. |
|
|
506 | |
|
|
507 | As a rule of thumb, monitoring requests should always monitor a port from |
|
|
508 | a local port (or callback). The reason is that kill messages might get |
|
|
509 | lost, just like any other message. Another less obvious reason is that |
|
|
510 | even monitoring requests can get lost (for exmaple, when the connection |
|
|
511 | to the other node goes down permanently). When monitoring a port locally |
|
|
512 | these problems do not exist. |
|
|
513 | |
522 | |
514 | Example: call a given callback when C<$port> is killed. |
523 | Example: call a given callback when C<$port> is killed. |
515 | |
524 | |
516 | mon $port, sub { warn "port died because of <@_>\n" }; |
525 | mon $port, sub { warn "port died because of <@_>\n" }; |
517 | |
526 | |
… | |
… | |
524 | mon $port, $self => "restart"; |
533 | mon $port, $self => "restart"; |
525 | |
534 | |
526 | =cut |
535 | =cut |
527 | |
536 | |
528 | sub mon { |
537 | sub mon { |
529 | my ($noderef, $port) = split /#/, shift, 2; |
538 | my ($nodeid, $port) = split /#/, shift, 2; |
530 | |
539 | |
531 | my $node = $NODE{$noderef} || add_node $noderef; |
540 | my $node = $NODE{$nodeid} || add_node $nodeid; |
532 | |
541 | |
533 | my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
542 | my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; |
534 | |
543 | |
535 | unless (ref $cb) { |
544 | unless (ref $cb) { |
536 | if (@_) { |
545 | if (@_) { |
… | |
… | |
650 | }; |
659 | }; |
651 | _self_die if $@; |
660 | _self_die if $@; |
652 | } |
661 | } |
653 | |
662 | |
654 | sub spawn(@) { |
663 | sub spawn(@) { |
655 | my ($noderef, undef) = split /#/, shift, 2; |
664 | my ($nodeid, undef) = split /#/, shift, 2; |
656 | |
665 | |
657 | my $id = "$RUNIQ." . $ID++; |
666 | my $id = "$RUNIQ." . $ID++; |
658 | |
667 | |
659 | $_[0] =~ /::/ |
668 | $_[0] =~ /::/ |
660 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
669 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
661 | |
670 | |
662 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
671 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
663 | |
672 | |
664 | "$noderef#$id" |
673 | "$nodeid#$id" |
665 | } |
674 | } |
666 | |
675 | |
667 | =item after $timeout, @msg |
676 | =item after $timeout, @msg |
668 | |
677 | |
669 | =item after $timeout, $callback |
678 | =item after $timeout, $callback |
… | |
… | |
708 | |
717 | |
709 | =item * Node IDs are arbitrary strings in AEMP. |
718 | =item * Node IDs are arbitrary strings in AEMP. |
710 | |
719 | |
711 | Erlang relies on special naming and DNS to work everywhere in the same |
720 | Erlang relies on special naming and DNS to work everywhere in the same |
712 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
721 | way. AEMP relies on each node somehow knowing its own address(es) (e.g. by |
713 | configuraiton or DNS), but will otherwise discover other odes itself. |
722 | configuration or DNS), but will otherwise discover other odes itself. |
714 | |
723 | |
715 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
724 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
716 | uses "local ports are like remote ports". |
725 | uses "local ports are like remote ports". |
717 | |
726 | |
718 | The failure modes for local ports are quite different (runtime errors |
727 | The failure modes for local ports are quite different (runtime errors |
… | |
… | |
731 | |
740 | |
732 | Erlang uses processes that selectively receive messages, and therefore |
741 | Erlang uses processes that selectively receive messages, and therefore |
733 | needs a queue. AEMP is event based, queuing messages would serve no |
742 | needs a queue. AEMP is event based, queuing messages would serve no |
734 | useful purpose. For the same reason the pattern-matching abilities of |
743 | useful purpose. For the same reason the pattern-matching abilities of |
735 | AnyEvent::MP are more limited, as there is little need to be able to |
744 | AnyEvent::MP are more limited, as there is little need to be able to |
736 | filter messages without dequeing them. |
745 | filter messages without dequeuing them. |
737 | |
746 | |
738 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
747 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
739 | |
748 | |
740 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
749 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
741 | |
750 | |