… | |
… | |
82 | |
82 | |
83 | Ports are represented by (printable) strings called "port IDs". |
83 | Ports 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 | |
87 | A port ID is the concatenation of a node ID, a hash-mark (C<#>) as |
87 | A port ID is the concatenation of a node ID, a hash-mark (C<#>) |
88 | separator, and a port name (a printable string of unspecified format). |
88 | as separator, and a port name (a printable string of unspecified |
|
|
89 | format created by AnyEvent::MP). |
89 | |
90 | |
90 | =item node |
91 | =item node |
91 | |
92 | |
92 | A node is a single process containing at least one port - the node port, |
93 | A node is a single process containing at least one port - the node port, |
93 | which enables nodes to manage each other remotely, and to create new |
94 | which enables nodes to manage each other remotely, and to create new |
… | |
… | |
175 | |
176 | |
176 | =cut |
177 | =cut |
177 | |
178 | |
178 | package AnyEvent::MP; |
179 | package AnyEvent::MP; |
179 | |
180 | |
|
|
181 | use AnyEvent::MP::Config (); |
180 | use AnyEvent::MP::Kernel; |
182 | use AnyEvent::MP::Kernel; |
|
|
183 | use AnyEvent::MP::Kernel qw(%NODE %PORT %PORT_DATA $UNIQ $RUNIQ $ID); |
181 | |
184 | |
182 | use common::sense; |
185 | use common::sense; |
183 | |
186 | |
184 | use Carp (); |
187 | use Carp (); |
185 | |
188 | |
186 | use AE (); |
189 | use AE (); |
|
|
190 | use Guard (); |
187 | |
191 | |
188 | use base "Exporter"; |
192 | use base "Exporter"; |
189 | |
193 | |
190 | our $VERSION = '1.30'; |
194 | our $VERSION = $AnyEvent::MP::Config::VERSION; |
191 | |
195 | |
192 | our @EXPORT = qw( |
196 | our @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 | |
199 | our $SELF; |
204 | our $SELF; |
200 | |
205 | |
201 | sub _self_die() { |
206 | sub _self_die() { |
… | |
… | |
221 | Before a node can talk to other nodes on the network (i.e. enter |
226 | Before 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 |
223 | to know is its own name, and optionally it should know the addresses of |
228 | to know is its own name, and optionally it should know the addresses of |
224 | some other nodes in the network to discover other nodes. |
229 | some other nodes in the network to discover other nodes. |
225 | |
230 | |
226 | The key/value pairs are basically the same ones as documented for the |
|
|
227 | F<aemp> command line utility (sans the set/del prefix). |
|
|
228 | |
|
|
229 | This function configures a node - it must be called exactly once (or |
231 | This function configures a node - it must be called exactly once (or |
230 | never) before calling other AnyEvent::MP functions. |
232 | never) before calling other AnyEvent::MP functions. |
|
|
233 | |
|
|
234 | The key/value pairs are basically the same ones as documented for the |
|
|
235 | F<aemp> command line utility (sans the set/del prefix), with these additions: |
|
|
236 | |
|
|
237 | =over 4 |
|
|
238 | |
|
|
239 | =item norc => $boolean (default false) |
|
|
240 | |
|
|
241 | If true, then the rc file (e.g. F<~/.perl-anyevent-mp>) will I<not> |
|
|
242 | be consulted - all configuraiton options must be specified in the |
|
|
243 | C<configure> call. |
|
|
244 | |
|
|
245 | =item force => $boolean (default false) |
|
|
246 | |
|
|
247 | IF true, then the values specified in the C<configure> will take |
|
|
248 | precedence over any values configured via the rc file. The default is for |
|
|
249 | the rc file to override any options specified in the program. |
|
|
250 | |
|
|
251 | =item secure => $pass->($nodeid) |
|
|
252 | |
|
|
253 | In addition to specifying a boolean, you can specify a code reference that |
|
|
254 | is called for every remote execution attempt - the execution request is |
|
|
255 | granted iff the callback returns a true value. |
|
|
256 | |
|
|
257 | See F<semp setsecure> for more info. |
|
|
258 | |
|
|
259 | =back |
231 | |
260 | |
232 | =over 4 |
261 | =over 4 |
233 | |
262 | |
234 | =item step 1, gathering configuration from profiles |
263 | =item step 1, gathering configuration from profiles |
235 | |
264 | |
… | |
… | |
249 | That means that the values specified in the profile have highest priority |
278 | That means that the values specified in the profile have highest priority |
250 | and the values specified directly via C<configure> have lowest priority, |
279 | and the values specified directly via C<configure> have lowest priority, |
251 | and can only be used to specify defaults. |
280 | and can only be used to specify defaults. |
252 | |
281 | |
253 | If the profile specifies a node ID, then this will become the node ID of |
282 | If the profile specifies a node ID, then this will become the node ID of |
254 | this process. If not, then the profile name will be used as node ID. The |
283 | this process. If not, then the profile name will be used as node ID, with |
255 | special node ID of C<anon/> will be replaced by a random node ID. |
284 | a unique randoms tring (C</%u>) appended. |
|
|
285 | |
|
|
286 | The node ID can contain some C<%> sequences that are expanded: C<%n> |
|
|
287 | is expanded to the local nodename, C<%u> is replaced by a random |
|
|
288 | strign to make the node unique. For example, the F<aemp> commandline |
|
|
289 | utility uses C<aemp/%n/%u> as nodename, which might expand to |
|
|
290 | C<aemp/cerebro/ZQDGSIkRhEZQDGSIkRhE>. |
256 | |
291 | |
257 | =item step 2, bind listener sockets |
292 | =item step 2, bind listener sockets |
258 | |
293 | |
259 | The next step is to look up the binds in the profile, followed by binding |
294 | The next step is to look up the binds in the profile, followed by binding |
260 | aemp protocol listeners on all binds specified (it is possible and valid |
295 | aemp protocol listeners on all binds specified (it is possible and valid |
… | |
… | |
277 | Example: become a distributed node using the local node name as profile. |
312 | Example: become a distributed node using the local node name as profile. |
278 | This should be the most common form of invocation for "daemon"-type nodes. |
313 | This should be the most common form of invocation for "daemon"-type nodes. |
279 | |
314 | |
280 | configure |
315 | configure |
281 | |
316 | |
282 | Example: become an anonymous node. This form is often used for commandline |
317 | Example: become a semi-anonymous node. This form is often used for |
283 | clients. |
318 | commandline clients. |
284 | |
319 | |
285 | configure nodeid => "anon/"; |
320 | configure nodeid => "myscript/%n/%u"; |
286 | |
321 | |
287 | Example: configure a node using a profile called seed, which si suitable |
322 | Example: configure a node using a profile called seed, which is suitable |
288 | for a seed node as it binds on all local addresses on a fixed port (4040, |
323 | for a seed node as it binds on all local addresses on a fixed port (4040, |
289 | customary for aemp). |
324 | customary for aemp). |
290 | |
325 | |
291 | # use the aemp commandline utility |
326 | # use the aemp commandline utility |
292 | # aemp profile seed nodeid anon/ binds '*:4040' |
327 | # aemp profile seed binds '*:4040' |
293 | |
328 | |
294 | # then use it |
329 | # then use it |
295 | configure profile => "seed"; |
330 | configure profile => "seed"; |
296 | |
331 | |
297 | # or simply use aemp from the shell again: |
332 | # or simply use aemp from the shell again: |
… | |
… | |
367 | sub _kilme { |
402 | sub _kilme { |
368 | die "received message on port without callback"; |
403 | die "received message on port without callback"; |
369 | } |
404 | } |
370 | |
405 | |
371 | sub port(;&) { |
406 | sub port(;&) { |
372 | my $id = "$UNIQ." . $ID++; |
407 | my $id = $UNIQ . ++$ID; |
373 | my $port = "$NODE#$id"; |
408 | my $port = "$NODE#$id"; |
374 | |
409 | |
375 | rcv $port, shift || \&_kilme; |
410 | rcv $port, shift || \&_kilme; |
376 | |
411 | |
377 | $port |
412 | $port |
… | |
… | |
650 | } |
685 | } |
651 | |
686 | |
652 | $node->monitor ($port, $cb); |
687 | $node->monitor ($port, $cb); |
653 | |
688 | |
654 | defined wantarray |
689 | defined wantarray |
655 | and ($cb += 0, AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }) |
690 | and ($cb += 0, Guard::guard { $node->unmonitor ($port, $cb) }) |
656 | } |
691 | } |
657 | |
692 | |
658 | =item $guard = mon_guard $port, $ref, $ref... |
693 | =item $guard = mon_guard $port, $ref, $ref... |
659 | |
694 | |
660 | Monitors the given C<$port> and keeps the passed references. When the port |
695 | Monitors the given C<$port> and keeps the passed references. When the port |
… | |
… | |
764 | } |
799 | } |
765 | |
800 | |
766 | sub spawn(@) { |
801 | sub spawn(@) { |
767 | my ($nodeid, undef) = split /#/, shift, 2; |
802 | my ($nodeid, undef) = split /#/, shift, 2; |
768 | |
803 | |
769 | my $id = "$RUNIQ." . $ID++; |
804 | my $id = $RUNIQ . ++$ID; |
770 | |
805 | |
771 | $_[0] =~ /::/ |
806 | $_[0] =~ /::/ |
772 | 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"; |
773 | |
808 | |
774 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
809 | snd_to_func $nodeid, "AnyEvent::MP::_spawn" => $id, @_; |
775 | |
810 | |
776 | "$nodeid#$id" |
811 | "$nodeid#$id" |
777 | } |
812 | } |
|
|
813 | |
778 | |
814 | |
779 | =item after $timeout, @msg |
815 | =item after $timeout, @msg |
780 | |
816 | |
781 | =item after $timeout, $callback |
817 | =item after $timeout, $callback |
782 | |
818 | |
… | |
… | |
852 | $port |
888 | $port |
853 | } |
889 | } |
854 | |
890 | |
855 | =back |
891 | =back |
856 | |
892 | |
|
|
893 | =head1 DISTRIBUTED DATABASE |
|
|
894 | |
|
|
895 | AnyEvent::MP comes with a simple distributed database. The database will |
|
|
896 | be mirrored asynchronously at all global nodes. Other nodes bind to one of |
|
|
897 | the global nodes for their needs. |
|
|
898 | |
|
|
899 | The database consists of a two-level hash - a hash contains a hash which |
|
|
900 | contains values. |
|
|
901 | |
|
|
902 | The top level hash key is called "family", and the second-level hash key |
|
|
903 | is called "subkey" or simply "key". |
|
|
904 | |
|
|
905 | The family must be alphanumeric, i.e. start with a letter and consist |
|
|
906 | of letters, digits, underscores and colons (C<[A-Za-z][A-Za-z0-9_:]*>, |
|
|
907 | pretty much like Perl module names. |
|
|
908 | |
|
|
909 | As the family namespace is global, it is recommended to prefix family names |
|
|
910 | with the name of the application or module using it. |
|
|
911 | |
|
|
912 | The subkeys must be non-empty strings, with no further restrictions. |
|
|
913 | |
|
|
914 | The values should preferably be strings, but other perl scalars should |
|
|
915 | work as well (such as undef, arrays and hashes). |
|
|
916 | |
|
|
917 | Every database entry is owned by one node - adding the same family/subkey |
|
|
918 | combination on multiple nodes will not cause discomfort for AnyEvent::MP, |
|
|
919 | but the result might be nondeterministic, i.e. the key might have |
|
|
920 | different values on different nodes. |
|
|
921 | |
|
|
922 | Different subkeys in the same family can be owned by different nodes |
|
|
923 | without problems, and in fact, this is the common method to create worker |
|
|
924 | pools. For example, a worker port for image scaling might do this: |
|
|
925 | |
|
|
926 | db_set my_image_scalers => $port; |
|
|
927 | |
|
|
928 | And clients looking for an image scaler will want to get the |
|
|
929 | C<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 | |
|
|
938 | Sets (or replaces) a key to the database - if C<$value> is omitted, |
|
|
939 | C<undef> is used instead. |
|
|
940 | |
|
|
941 | =item db_del $family => $subkey |
|
|
942 | |
|
|
943 | Deletes a key from the database. |
|
|
944 | |
|
|
945 | =item $guard = db_reg $family => $subkey [=> $value] |
|
|
946 | |
|
|
947 | Sets the key on the database and returns a guard. When the guard is |
|
|
948 | destroyed, the key is deleted from the database. If C<$value> is missing, |
|
|
949 | then C<undef> is used. |
|
|
950 | |
|
|
951 | =cut |
|
|
952 | |
|
|
953 | =back |
|
|
954 | |
857 | =head1 AnyEvent::MP vs. Distributed Erlang |
955 | =head1 AnyEvent::MP vs. Distributed Erlang |
858 | |
956 | |
859 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
957 | AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node |
860 | == 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 |
861 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
959 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |