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.121 by root, Tue Feb 28 18:37:24 2012 UTC vs.
Revision 1.127 by root, Sat Mar 3 20:35:10 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() {
227 230
228This function configures a node - it must be called exactly once (or 231This function configures a node - it must be called exactly once (or
229never) before calling other AnyEvent::MP functions. 232never) before calling other AnyEvent::MP functions.
230 233
231The key/value pairs are basically the same ones as documented for the 234The key/value pairs are basically the same ones as documented for the
232F<aemp> command line utility (sans the set/del prefix), with two additions: 235F<aemp> command line utility (sans the set/del prefix), with these additions:
233 236
234=over 4 237=over 4
235 238
236=item norc => $boolean (default false) 239=item norc => $boolean (default false)
237 240
242=item force => $boolean (default false) 245=item force => $boolean (default false)
243 246
244IF true, then the values specified in the C<configure> will take 247IF true, then the values specified in the C<configure> will take
245precedence over any values configured via the rc file. The default is for 248precedence over any values configured via the rc file. The default is for
246the rc file to override any options specified in the program. 249the rc file to override any options specified in the program.
250
251=item secure => $pass->($nodeid)
252
253In addition to specifying a boolean, you can specify a code reference that
254is called for every remote execution attempt - the execution request is
255granted iff the callback returns a true value.
256
257See F<semp setsecure> for more info.
247 258
248=back 259=back
249 260
250=over 4 261=over 4
251 262
267That means that the values specified in the profile have highest priority 278That means that the values specified in the profile have highest priority
268and the values specified directly via C<configure> have lowest priority, 279and the values specified directly via C<configure> have lowest priority,
269and can only be used to specify defaults. 280and can only be used to specify defaults.
270 281
271If the profile specifies a node ID, then this will become the node ID of 282If 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 283this 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. 284a unique randoms tring (C</%u>) appended.
285
286The node ID can contain some C<%> sequences that are expanded: C<%n>
287is expanded to the local nodename, C<%u> is replaced by a random
288strign to make the node unique. For example, the F<aemp> commandline
289utility uses C<aemp/%n/%u> as nodename, which might expand to
290C<aemp/cerebro/ZQDGSIkRhEZQDGSIkRhE>.
274 291
275=item step 2, bind listener sockets 292=item step 2, bind listener sockets
276 293
277The next step is to look up the binds in the profile, followed by binding 294The 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 295aemp protocol listeners on all binds specified (it is possible and valid
295Example: become a distributed node using the local node name as profile. 312Example: become a distributed node using the local node name as profile.
296This should be the most common form of invocation for "daemon"-type nodes. 313This should be the most common form of invocation for "daemon"-type nodes.
297 314
298 configure 315 configure
299 316
300Example: become an anonymous node. This form is often used for commandline 317Example: become a semi-anonymous node. This form is often used for
301clients. 318commandline clients.
302 319
303 configure nodeid => "anon/"; 320 configure nodeid => "myscript/%n/%u";
304 321
305Example: configure a node using a profile called seed, which is suitable 322Example: 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, 323for a seed node as it binds on all local addresses on a fixed port (4040,
307customary for aemp). 324customary for aemp).
308 325
309 # use the aemp commandline utility 326 # use the aemp commandline utility
310 # aemp profile seed nodeid anon/ binds '*:4040' 327 # aemp profile seed binds '*:4040'
311 328
312 # then use it 329 # then use it
313 configure profile => "seed"; 330 configure profile => "seed";
314 331
315 # or simply use aemp from the shell again: 332 # or simply use aemp from the shell again:
385sub _kilme { 402sub _kilme {
386 die "received message on port without callback"; 403 die "received message on port without callback";
387} 404}
388 405
389sub port(;&) { 406sub port(;&) {
390 my $id = "$UNIQ." . ++$ID; 407 my $id = $UNIQ . ++$ID;
391 my $port = "$NODE#$id"; 408 my $port = "$NODE#$id";
392 409
393 rcv $port, shift || \&_kilme; 410 rcv $port, shift || \&_kilme;
394 411
395 $port 412 $port
668 } 685 }
669 686
670 $node->monitor ($port, $cb); 687 $node->monitor ($port, $cb);
671 688
672 defined wantarray 689 defined wantarray
673 and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) 690 and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) })
674} 691}
675 692
676=item $guard = mon_guard $port, $ref, $ref... 693=item $guard = mon_guard $port, $ref, $ref...
677 694
678Monitors the given C<$port> and keeps the passed references. When the port 695Monitors the given C<$port> and keeps the passed references. When the port
782} 799}
783 800
784sub spawn(@) { 801sub spawn(@) {
785 my ($nodeid, undef) = split /#/, shift, 2; 802 my ($nodeid, undef) = split /#/, shift, 2;
786 803
787 my $id = "$RUNIQ." . ++$ID; 804 my $id = $RUNIQ . ++$ID;
788 805
789 $_[0] =~ /::/ 806 $_[0] =~ /::/
790 or Carp::croak "spawn init function must be a fully-qualified name, caught"; 807 or Carp::croak "spawn init function must be a fully-qualified name, caught";
791 808
792 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; 809 snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_;
871 $port 888 $port
872} 889}
873 890
874=back 891=back
875 892
893=head1 DISTRIBUTED DATABASE
894
895AnyEvent::MP comes with a simple distributed database. The database will
896be mirrored asynchronously at all global nodes. Other nodes bind to one of
897the global nodes for their needs.
898
899The database consists of a two-level hash - a hash contains a hash which
900contains values.
901
902The top level hash key is called "family", and the second-level hash key
903is called "subkey" or simply "key".
904
905The family must be alphanumeric, i.e. start with a letter and consist
906of letters, digits, underscores and colons (C<[A-Za-z][A-Za-z0-9_:]*>,
907pretty much like Perl module names.
908
909As the family namespace is global, it is recommended to prefix family names
910with the name of the application or module using it.
911
912The subkeys must be non-empty strings, with no further restrictions.
913
914The values should preferably be strings, but other perl scalars should
915work as well (such as undef, arrays and hashes).
916
917Every database entry is owned by one node - adding the same family/subkey
918combination on multiple nodes will not cause discomfort for AnyEvent::MP,
919but the result might be nondeterministic, i.e. the key might have
920different values on different nodes.
921
922Different subkeys in the same family can be owned by different nodes
923without problems, and in fact, this is the common method to create worker
924pools. For example, a worker port for image scaling might do this:
925
926 db_set my_image_scalers => $port;
927
928And clients looking for an image scaler will want to get the
929C<my_image_scalers> keys:
930
931 db_keys "my_image_scalers" => 60 => sub {
932 #d##TODO#
933
934=over
935
936=item db_set $family => $subkey [=> $value]
937
938Sets (or replaces) a key to the database - if C<$value> is omitted,
939C<undef> is used instead.
940
941=item db_del $family => $subkey
942
943Deletes a key from the database.
944
945=item $guard = db_reg $family => $subkey [=> $value]
946
947Sets the key on the database and returns a guard. When the guard is
948destroyed, the key is deleted from the database. If C<$value> is missing,
949then C<undef> is used.
950
951=cut
952
953=back
954
876=head1 AnyEvent::MP vs. Distributed Erlang 955=head1 AnyEvent::MP vs. Distributed Erlang
877 956
878AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node 957AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
879== aemp node, Erlang process == aemp port), so many of the documents and 958== aemp node, Erlang process == aemp port), so many of the documents and
880programming techniques employed by Erlang apply to AnyEvent::MP. Here is a 959programming techniques employed by Erlang apply to AnyEvent::MP. Here is a

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines