ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MP/MP.pm
(Generate patch)

Comparing cvsroot/AnyEvent-MP/MP.pm (file contents):
Revision 1.121 by root, Tue Feb 28 18:37:24 2012 UTC vs.
Revision 1.126 by root, Sat Mar 3 19:43:41 2012 UTC

82 82
83Ports are represented by (printable) strings called "port IDs". 83Ports are represented by (printable) strings called "port IDs".
84 84
85=item port ID - C<nodeid#portname> 85=item port ID - C<nodeid#portname>
86 86
87A port ID is the concatenation of a node ID, a hash-mark (C<#>) as 87A port ID is the concatenation of a node ID, a hash-mark (C<#>)
88separator, and a port name (a printable string of unspecified format). 88as separator, and a port name (a printable string of unspecified
89format created by AnyEvent::MP).
89 90
90=item node 91=item node
91 92
92A node is a single process containing at least one port - the node port, 93A node is a single process containing at least one port - the node port,
93which enables nodes to manage each other remotely, and to create new 94which enables nodes to manage each other remotely, and to create new
184use common::sense; 185use common::sense;
185 186
186use Carp (); 187use Carp ();
187 188
188use AE (); 189use AE ();
190use Guard ();
189 191
190use base "Exporter"; 192use base "Exporter";
191 193
192our $VERSION = $AnyEvent::MP::Config::VERSION; 194our $VERSION = $AnyEvent::MP::Config::VERSION;
193 195
194our @EXPORT = qw( 196our @EXPORT = qw(
195 NODE $NODE *SELF node_of after 197 NODE $NODE *SELF node_of after
196 configure 198 configure
197 snd rcv mon mon_guard kil psub peval spawn cal 199 snd rcv mon mon_guard kil psub peval spawn cal
198 port 200 port
201 db_set db_del db_reg
199); 202);
200 203
201our $SELF; 204our $SELF;
202 205
203sub _self_die() { 206sub _self_die() {
267That means that the values specified in the profile have highest priority 270That means that the values specified in the profile have highest priority
268and the values specified directly via C<configure> have lowest priority, 271and the values specified directly via C<configure> have lowest priority,
269and can only be used to specify defaults. 272and can only be used to specify defaults.
270 273
271If the profile specifies a node ID, then this will become the node ID of 274If the profile specifies a node ID, then this will become the node ID of
272this process. If not, then the profile name will be used as node ID. The 275this process. If not, then the profile name will be used as node ID, with
273special node ID of C<anon/> will be replaced by a random node ID. 276a unique randoms tring (C</%u>) appended.
277
278The node ID can contain some C<%> sequences that are expanded: C<%n>
279is expanded to the local nodename, C<%u> is replaced by a random
280strign to make the node unique. For example, the F<aemp> commandline
281utility uses C<aemp/%n/%u> as nodename, which might expand to
282C<aemp/cerebro/ZQDGSIkRhEZQDGSIkRhE>.
274 283
275=item step 2, bind listener sockets 284=item step 2, bind listener sockets
276 285
277The next step is to look up the binds in the profile, followed by binding 286The next step is to look up the binds in the profile, followed by binding
278aemp protocol listeners on all binds specified (it is possible and valid 287aemp protocol listeners on all binds specified (it is possible and valid
295Example: become a distributed node using the local node name as profile. 304Example: become a distributed node using the local node name as profile.
296This should be the most common form of invocation for "daemon"-type nodes. 305This should be the most common form of invocation for "daemon"-type nodes.
297 306
298 configure 307 configure
299 308
300Example: become an anonymous node. This form is often used for commandline 309Example: become a semi-anonymous node. This form is often used for
301clients. 310commandline clients.
302 311
303 configure nodeid => "anon/"; 312 configure nodeid => "myscript/%n/%u";
304 313
305Example: configure a node using a profile called seed, which is suitable 314Example: configure a node using a profile called seed, which is suitable
306for a seed node as it binds on all local addresses on a fixed port (4040, 315for a seed node as it binds on all local addresses on a fixed port (4040,
307customary for aemp). 316customary for aemp).
308 317
309 # use the aemp commandline utility 318 # use the aemp commandline utility
310 # aemp profile seed nodeid anon/ binds '*:4040' 319 # aemp profile seed binds '*:4040'
311 320
312 # then use it 321 # then use it
313 configure profile => "seed"; 322 configure profile => "seed";
314 323
315 # or simply use aemp from the shell again: 324 # or simply use aemp from the shell again:
385sub _kilme { 394sub _kilme {
386 die "received message on port without callback"; 395 die "received message on port without callback";
387} 396}
388 397
389sub port(;&) { 398sub port(;&) {
390 my $id = "$UNIQ." . ++$ID; 399 my $id = $UNIQ . ++$ID;
391 my $port = "$NODE#$id"; 400 my $port = "$NODE#$id";
392 401
393 rcv $port, shift || \&_kilme; 402 rcv $port, shift || \&_kilme;
394 403
395 $port 404 $port
668 } 677 }
669 678
670 $node->monitor ($port, $cb); 679 $node->monitor ($port, $cb);
671 680
672 defined wantarray 681 defined wantarray
673 and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) 682 and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) })
674} 683}
675 684
676=item $guard = mon_guard $port, $ref, $ref... 685=item $guard = mon_guard $port, $ref, $ref...
677 686
678Monitors the given C<$port> and keeps the passed references. When the port 687Monitors the given C<$port> and keeps the passed references. When the port
782} 791}
783 792
784sub spawn(@) { 793sub spawn(@) {
785 my ($nodeid, undef) = split /#/, shift, 2; 794 my ($nodeid, undef) = split /#/, shift, 2;
786 795
787 my $id = "$RUNIQ." . ++$ID; 796 my $id = $RUNIQ . ++$ID;
788 797
789 $_[0] =~ /::/ 798 $_[0] =~ /::/
790 or Carp::croak "spawn init function must be a fully-qualified name, caught"; 799 or Carp::croak "spawn init function must be a fully-qualified name, caught";
791 800
792 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; 801 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_;
871 $port 880 $port
872} 881}
873 882
874=back 883=back
875 884
885=head1 DISTRIBUTED DATABASE
886
887AnyEvent::MP comes with a simple distributed database. The database will
888be mirrored asynchronously at all global nodes. Other nodes bind to one of
889the global nodes for their needs.
890
891The database consists of a two-level hash - a hash contains a hash which
892contains values.
893
894The top level hash key is called "family", and the second-level hash key
895is called "subkey" or simply "key".
896
897The family must be alphanumeric, i.e. start with a letter and consist
898of letters, digits, underscores and colons (C<[A-Za-z][A-Za-z0-9_:]*>,
899pretty much like Perl module names.
900
901As the family namespace is global, it is recommended to prefix family names
902with the name of the application or module using it.
903
904The subkeys must be non-empty strings, with no further restrictions.
905
906The values should preferably be strings, but other perl scalars should
907work as well (such as undef, arrays and hashes).
908
909Every database entry is owned by one node - adding the same family/subkey
910combination on multiple nodes will not cause discomfort for AnyEvent::MP,
911but the result might be nondeterministic, i.e. the key might have
912different values on different nodes.
913
914Different subkeys in the same family can be owned by different nodes
915without problems, and in fact, this is the common method to create worker
916pools. For example, a worker port for image scaling might do this:
917
918 db_set my_image_scalers => $port;
919
920And clients looking for an image scaler will want to get the
921C<my_image_scalers> keys:
922
923 db_keys "my_image_scalers" => 60 => sub {
924 #d##TODO#
925
926=over
927
928=item db_set $family => $subkey [=> $value]
929
930Sets (or replaces) a key to the database - if C<$value> is omitted,
931C<undef> is used instead.
932
933=item db_del $family => $subkey
934
935Deletes a key from the database.
936
937=item $guard = db_reg $family => $subkey [=> $value]
938
939Sets the key on the database and returns a guard. When the guard is
940destroyed, the key is deleted from the database. If C<$value> is missing,
941then C<undef> is used.
942
943=cut
944
945=back
946
876=head1 AnyEvent::MP vs. Distributed Erlang 947=head1 AnyEvent::MP vs. Distributed Erlang
877 948
878AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node 949AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
879== aemp node, Erlang process == aemp port), so many of the documents and 950== aemp node, Erlang process == aemp port), so many of the documents and
880programming techniques employed by Erlang apply to AnyEvent::MP. Here is a 951programming techniques employed by Erlang apply to AnyEvent::MP. Here is a

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines