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

Comparing AnyEvent-MP/MP.pm (file contents):
Revision 1.120 by root, Sun Feb 26 11:12:54 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
175 176
176=cut 177=cut
177 178
178package AnyEvent::MP; 179package AnyEvent::MP;
179 180
181use AnyEvent::MP::Config ();
180use AnyEvent::MP::Kernel; 182use AnyEvent::MP::Kernel;
183use AnyEvent::MP::Kernel qw(%NODE %PORT %PORT_DATA $UNIQ $RUNIQ $ID);
181 184
182use common::sense; 185use common::sense;
183 186
184use Carp (); 187use Carp ();
185 188
186use AE (); 189use AE ();
190use Guard ();
187 191
188use base "Exporter"; 192use base "Exporter";
189 193
190our $VERSION = '1.30'; 194our $VERSION = $AnyEvent::MP::Config::VERSION;
191 195
192our @EXPORT = qw( 196our @EXPORT = qw(
193 NODE $NODE *SELF node_of after 197 NODE $NODE *SELF node_of after
194 configure 198 configure
195 snd rcv mon mon_guard kil psub peval spawn cal 199 snd rcv mon mon_guard kil psub peval spawn cal
196 port 200 port
201 db_set db_del db_reg
197); 202);
198 203
199our $SELF; 204our $SELF;
200 205
201sub _self_die() { 206sub _self_die() {
221Before a node can talk to other nodes on the network (i.e. enter 226Before a node can talk to other nodes on the network (i.e. enter
222"distributed mode") it has to configure itself - the minimum a node needs 227"distributed mode") it has to configure itself - the minimum a node needs
223to know is its own name, and optionally it should know the addresses of 228to know is its own name, and optionally it should know the addresses of
224some other nodes in the network to discover other nodes. 229some other nodes in the network to discover other nodes.
225 230
226The key/value pairs are basically the same ones as documented for the
227F<aemp> command line utility (sans the set/del prefix).
228
229This function configures a node - it must be called exactly once (or 231This function configures a node - it must be called exactly once (or
230never) before calling other AnyEvent::MP functions. 232never) before calling other AnyEvent::MP functions.
233
234The key/value pairs are basically the same ones as documented for the
235F<aemp> command line utility (sans the set/del prefix), with two additions:
236
237=over 4
238
239=item norc => $boolean (default false)
240
241If true, then the rc file (e.g. F<~/.perl-anyevent-mp>) will I<not>
242be consulted - all configuraiton options must be specified in the
243C<configure> call.
244
245=item force => $boolean (default false)
246
247IF true, then the values specified in the C<configure> will take
248precedence over any values configured via the rc file. The default is for
249the rc file to override any options specified in the program.
250
251=back
231 252
232=over 4 253=over 4
233 254
234=item step 1, gathering configuration from profiles 255=item step 1, gathering configuration from profiles
235 256
249That means that the values specified in the profile have highest priority 270That means that the values specified in the profile have highest priority
250and the values specified directly via C<configure> have lowest priority, 271and the values specified directly via C<configure> have lowest priority,
251and can only be used to specify defaults. 272and can only be used to specify defaults.
252 273
253If 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
254this 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
255special 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>.
256 283
257=item step 2, bind listener sockets 284=item step 2, bind listener sockets
258 285
259The 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
260aemp protocol listeners on all binds specified (it is possible and valid 287aemp protocol listeners on all binds specified (it is possible and valid
277Example: become a distributed node using the local node name as profile. 304Example: become a distributed node using the local node name as profile.
278This 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.
279 306
280 configure 307 configure
281 308
282Example: become an anonymous node. This form is often used for commandline 309Example: become a semi-anonymous node. This form is often used for
283clients. 310commandline clients.
284 311
285 configure nodeid => "anon/"; 312 configure nodeid => "myscript/%n/%u";
286 313
287Example: configure a node using a profile called seed, which is suitable 314Example: configure a node using a profile called seed, which is suitable
288for 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,
289customary for aemp). 316customary for aemp).
290 317
291 # use the aemp commandline utility 318 # use the aemp commandline utility
292 # aemp profile seed nodeid anon/ binds '*:4040' 319 # aemp profile seed binds '*:4040'
293 320
294 # then use it 321 # then use it
295 configure profile => "seed"; 322 configure profile => "seed";
296 323
297 # or simply use aemp from the shell again: 324 # or simply use aemp from the shell again:
367sub _kilme { 394sub _kilme {
368 die "received message on port without callback"; 395 die "received message on port without callback";
369} 396}
370 397
371sub port(;&) { 398sub port(;&) {
372 my $id = "$UNIQ." . $ID++; 399 my $id = $UNIQ . ++$ID;
373 my $port = "$NODE#$id"; 400 my $port = "$NODE#$id";
374 401
375 rcv $port, shift || \&_kilme; 402 rcv $port, shift || \&_kilme;
376 403
377 $port 404 $port
650 } 677 }
651 678
652 $node->monitor ($port, $cb); 679 $node->monitor ($port, $cb);
653 680
654 defined wantarray 681 defined wantarray
655 and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) 682 and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) })
656} 683}
657 684
658=item $guard = mon_guard $port, $ref, $ref... 685=item $guard = mon_guard $port, $ref, $ref...
659 686
660Monitors 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
764} 791}
765 792
766sub spawn(@) { 793sub spawn(@) {
767 my ($nodeid, undef) = split /#/, shift, 2; 794 my ($nodeid, undef) = split /#/, shift, 2;
768 795
769 my $id = "$RUNIQ." . $ID++; 796 my $id = $RUNIQ . ++$ID;
770 797
771 $_[0] =~ /::/ 798 $_[0] =~ /::/
772 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";
773 800
774 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; 801 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_;
775 802
776 "$nodeid#$id" 803 "$nodeid#$id"
777} 804}
805
778 806
779=item after $timeout, @msg 807=item after $timeout, @msg
780 808
781=item after $timeout, $callback 809=item after $timeout, $callback
782 810
852 $port 880 $port
853} 881}
854 882
855=back 883=back
856 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
857=head1 AnyEvent::MP vs. Distributed Erlang 947=head1 AnyEvent::MP vs. Distributed Erlang
858 948
859AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node 949AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
860== 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
861programming 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