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

Comparing AnyEvent-MP/MP/Intro.pod (file contents):
Revision 1.51 by root, Wed Mar 21 00:14:25 2012 UTC vs.
Revision 1.52 by root, Wed Mar 21 01:14:12 2012 UTC

612The C<peval> function temporarily replaces C<$SELF> by the given C<$port> 612The C<peval> function temporarily replaces C<$SELF> by the given C<$port>
613and then executes the given sub in a port context. 613and then executes the given sub in a port context.
614 614
615=head3 Network Errors and the AEMP Guarantee 615=head3 Network Errors and the AEMP Guarantee
616 616
617I mentioned another important source of monitoring failures: network 617Earlier we mentioned another important source of monitoring failures:
618problems. When a node loses connection to another node, it will invoke all 618network problems. When a node loses connection to another node, it will
619monitoring actions as if the port was killed, even if it is possible that 619invoke all monitoring actions, just as if the port was killed, I<even if
620the port is still happily alive on another node (not being able to talk to 620it is possible that the port is still happily alive on another node> (not
621a node means we have no clue what's going on with it, it could be crashed, 621being able to talk to a node means we have no clue what's going on with
622but also still running without knowing we lost the connection). 622it, it could be crashed, but also still running without knowing we lost
623the connection).
623 624
624So another way to view monitors is "notify me when some of my messages 625So another way to view monitors is: "notify me when some of my messages
625couldn't be delivered". AEMP has a guarantee about message delivery to a 626couldn't be delivered". AEMP has a guarantee about message delivery to a
626port: After starting a monitor, any message sent to a port will either 627port: After starting a monitor, any message sent to a port will either
627be delivered, or, when it is lost, any further messages will also be lost 628be delivered, or, when it is lost, any further messages will also be lost
628until the monitoring action is invoked. After that, further messages 629until the monitoring action is invoked. After that, further messages
629I<might> get delivered again. 630I<might> get delivered again.
630 631
631This doesn't sound like a very big guarantee, but it is kind of the best 632This doesn't sound like a very big guarantee, but it is kind of the best
632you can get while staying sane: Specifically, it means that there will 633you can get while staying sane: Specifically, it means that there will be
633be no "holes" in the message sequence: all messages sent are delivered 634no "holes" in the message sequence: all messages sent are delivered in
634in order, without any missing in between, and when some were lost, you 635order, without any of them missing in between, and when some were lost,
635I<will> be notified of that, so you can take recovery action. 636you I<will> be notified of that, so you can take recovery action.
636 637
637And, obviously, the guarantee only works in the presence of 638And, obviously, the guarantee only works in the presence of
638correctly-working hardware, and no relevant bugs inside AEMP itself. 639correctly-working hardware, and no relevant bugs inside AEMP itself.
639 640
640=head3 Supervising 641=head3 Supervising
641 642
642OK, so how is this crashing-everything-stuff going to make applications 643OK, so how is this crashing-everything-stuff going to make applications
643I<more> stable? Well, in fact, the goal is not really to make them more 644I<more> stable? Well, in fact, the goal is not really to make them
644stable, but to make them more resilient against actual errors and 645more stable, but to make them more resilient against actual errors
645crashes. And this is not done by crashing I<everything>, but by crashing 646and crashes. And this is not done by crashing I<everything>, but by
646everything except a I<supervisor>. 647crashing everything except a I<supervisor> that then cleans up and sgtarts
648everything again.
647 649
648A supervisor is simply some code that ensures that an application (or a 650A supervisor is simply some code that ensures that an application (or a
649part of it) is running, and if it crashes, is restarted properly. That is, 651part of it) is running, and if it crashes, is restarted properly. That is,
650it supervises a service by starting and restarting it, as necessary. 652it supervises a service by starting and restarting it, as necessary.
651 653
742 }; 744 };
743 745
744And finally, the server registers itself in the server group, so that 746And finally, the server registers itself in the server group, so that
745clients can find it: 747clients can find it:
746 748
747 grp_reg eg_chat_server => $server; 749 db_set eg_chat_server => $server;
748 750
749Well, well... and where is this supervisor stuff? Well... we cheated, 751Well, well... and where is this supervisor stuff? Well... we cheated,
750it's not there. To not overcomplicate the example, we only put it into 752it's not there. To not overcomplicate the example, we only put it into
751the..... CLIENT! 753the..... CLIENT!
752 754
802expected as the only command line argument) in C<$nick>, for further 804expected as the only command line argument) in C<$nick>, for further
803usage. 805usage.
804 806
805The next relevant thing is... finally... the supervisor: 807The next relevant thing is... finally... the supervisor:
806 808
807#todo#d#
808 sub server_connect { 809 sub server_connect {
809 my $servernodes = grp_get "eg_chat_server" 810 my $db_mon;
810 or return after 1, \&server_connect; 811 $db_mon = db_mon eg_chat_server => sub {
812 return unless %{ $_[0] };
813 undef $db_mon; # stop monitoring
811 814
812This looks up the server in the C<eg_chat_server> global group. If it 815This monitors the C<eg_chat_server> database family. It waits until a
813cannot find it (which is likely when the node is just starting up), 816chat server becomes available. When that happens, it "connects" to it
814it will wait a second and then retry. This "wait a bit and retry" 817by creating a client port that receives and prints chat messages, and
815is an important pattern, as distributed programming means lots of 818monitoring it:
816things are going on asynchronously. In practise, one should use a more
817intelligent algorithm, to possibly warn after an excessive number of
818retries. Hopefully future versions of AnyEvent::MP will offer some
819predefined supervisors, for now you will have to code it on your own.
820
821Next it creates a local port for the server to send messages to, and
822monitors it. When the port is killed, it will print "disconnected" and
823tell the supervisor function to retry again.
824 819
825 $client = port { print "\r \r@_\n> " }; 820 $client = port { print "\r \r@_\n> " };
826 mon $client, sub { 821 mon $client, sub {
827 print "\rdisconnected @_\n"; 822 print "\rdisconnected @_\n";
828 &server_connect; 823 &server_connect;
829 }; 824 };
830 825
826If the client port dies (for whatever reason), the "supervisor" will start
827looking for a server again - the semantics of C<db_mon> ensure that it
828will immediately find it if there is a server port.
829
831Then everything is ready: the client will send a C<join> message with it's 830After this, everything is ready: the client will send a C<join> message
832local port to the server, and start monitoring it: 831with its local port to the server, and start monitoring it:
833 832
834 $server = $servernodes->[0]; 833 $server = (keys %{ $_[0] })[0];
834
835 snd $server, join => $client, $nick; 835 snd $server, join => $client, $nick;
836 mon $server, $client; 836 mon $server, $client;
837 } 837 }
838 838
839The monitor will ensure that if the server crashes or goes away, the 839This second monitor will ensure that, when the server port crashes or goes
840client will be killed as well. This tells the user that the client was 840away (e.g. due to network problems), the client port will be killed as
841disconnected, and will then start to connect the server again. 841well. This tells the user that the client was disconnected, and will then
842start to connect the server again.
842 843
843The rest of the program deals with the boring details of actually invoking 844The rest of the program deals with the boring details of actually invoking
844the supervisor function to start the whole client process and handle the 845the supervisor function to start the whole client process and handle the
845actual terminal input, sending it to the server. 846actual terminal input, sending it to the server.
847
848Now... the "supervisor" in this example is a bit of a cheat - it doesn't
849really clean up much (because the cleanup done by AnyEvent::MP suffices),
850and there isn't much of a restarting action either - if the server isn't
851there because it crashed, well, it isn't there.
852
853In the real world, one would often add a timeout that would trigger when
854the server couldn't be found within some time limit, and then complain,
855or even try to start a new server. Or the supervisor would have to do
856some real cleanups, such as rolling back database transactions when the
857database thread crashes. For this simple chat server, however, this simple
858supervisor works fine. Hopefully future versions of AnyEvent::MP will
859offer some predefined supervisors, for now you will have to code it on
860your own.
846 861
847You should now try to start the server and one or more clients in different 862You should now try to start the server and one or more clients in different
848terminal windows (and the seed node): 863terminal windows (and the seed node):
849 864
850 perl eg/chat_client nick1 865 perl eg/chat_client nick1
862sides can take corrective action. Exceptions are "servers" that serve 877sides can take corrective action. Exceptions are "servers" that serve
863multiple clients at once and might only wish to clean up, and supervisors, 878multiple clients at once and might only wish to clean up, and supervisors,
864who of course should not normally get killed (unless they, too, have a 879who of course should not normally get killed (unless they, too, have a
865supervisor). 880supervisor).
866 881
867If you often think in object-oriented terms, then treat a port as an 882If you often think in object-oriented terms, then you can think of a port
868object, C<port> is the constructor, the receive callbacks set by C<rcv> 883as an object: C<port> is the constructor, the receive callbacks set by
869act as methods, the C<kil> function becomes the explicit destructor and 884C<rcv> act as methods, the C<kil> function becomes the explicit destructor
870C<mon> installs a destructor hook. Unlike conventional object oriented 885and C<mon> installs a destructor hook. Unlike conventional object oriented
871programming, it can make sense to exchange ports more freely (for example, 886programming, it can make sense to exchange port IDs more freely (for
872to monitor one port from another). 887example, to monitor one port from another), because it is cheap to send
888port IDs over the network, and AnyEvent::MP blurs the distinction between
889local and remote ports.
873 890
874There is ample room for improvement: the server should probably remember 891Lastly, there is ample room for improvement in this example: the server
875the nickname in the C<join> handler instead of expecting it in every chat 892should probably remember the nickname in the C<join> handler instead of
876message, it should probably monitor itself, and the client should not try 893expecting it in every chat message, it should probably monitor itself, and
877to send any messages unless a server is actually connected. 894the client should not try to send any messages unless a server is actually
895connected.
878 896
879=head1 PART 3: TIMTOWTDI: Virtual Connections 897=head1 PART 3: TIMTOWTDI: Virtual Connections
880 898
881The chat system developed in the previous sections is very "traditional" 899The chat system developed in the previous sections is very "traditional"
882in a way: you start some server(s) and some clients statically and they 900in a way: you start some server(s) and some clients statically and they
883start talking to each other. 901start talking to each other.
884 902
885Sometimes applications work more like "services": They can run on almost 903Sometimes applications work more like "services": They can run on almost
886any node and talks to itself on other nodes. The L<AnyEvent::MP::Global> 904any node and even talk to copies of themselves on other nodes in case they
887service for example monitors nodes joining the network and starts itself 905are distributed. The L<AnyEvent::MP::Global> service for example monitors
888automatically on other nodes (if it isn't running already). 906nodes joining the network and sometimes even starts itself on other nodes.
889 907
890A good way to design such applications is to put them into a module and 908One good way to design such services is to put them into a module and
891create "virtual connections" to other nodes - we call this the "bridge 909create "virtual connections" to other nodes. We call this the "bridge
892head" method, because you start by creating a remote port (the bridge 910head" method, because you start by I<creating a remote port> (the bridge
893head) and from that you start to bootstrap your application. 911head) and from that you start to bootstrap your application.
894 912
895Since that sounds rather theoretical, let's redesign the chat server and 913Since that sounds rather theoretical, let us redesign the chat server and
896client using this design method. 914client using this design method.
897 915
898Here is the server: 916As usual, we start with the full program - here is the server:
899 917
900 use common::sense; 918 use common::sense;
901 use AnyEvent::MP; 919 use AnyEvent::MP;
902 use AnyEvent::MP::Global;
903 920
904 configure; 921 configure;
905 922
906 grp_reg eg_chat_server2 => $NODE; 923 db_set eg_chat_server2 => $NODE;
907 924
908 my %clients; 925 my %clients;
909 926
910 sub msg { 927 sub msg {
911 print "relaying: $_[0]\n"; 928 print "relaying: $_[0]\n";
915 932
916 sub client_connect { 933 sub client_connect {
917 my ($client, $nick) = @_; 934 my ($client, $nick) = @_;
918 935
919 mon $client; 936 mon $client;
920 mon $client, sub { 937 mon $client, psub {
921 delete $clients{$client}; 938 delete $clients{$client};
922 msg "$nick (quits, @_)"; 939 msg "$nick (quits, @_)";
923 }; 940 };
924 941
925 $clients{$client} = $client; 942 $clients{$client} = $client;
932 warn "server ready.\n"; 949 warn "server ready.\n";
933 950
934 AnyEvent->condvar->recv; 951 AnyEvent->condvar->recv;
935 952
936It starts out not much different then the previous example, except that 953It starts out not much different then the previous example, except that
937this time, we register the node port in the global group and not any port 954this time, we register the node port in the database and not a port we
938we created - the clients only want to know which node the server should be 955created - the clients only want to know which node the server should
956be running on, and there can only be one such server (or service) per
939running on. In fact, they could also use some kind of election mechanism, 957node. In fact, the clients could also use some kind of election mechanism,
940to find the node with lowest load or something like that. 958to find the node with lowest node ID, or lowest load, or something like
959that.
941 960
942The more interesting change is that indeed no server port is created - 961The much more interesting difference to the previous server is that
943the server consists only of code, and "does" nothing by itself. All it 962indeed no server port is created - the server consists only of code,
944does is define a function C<client_connect>, which expects a client port 963and "does" nothing by itself. All it "does" is to define a function
945and a nick name as arguments. It then monitors the client port and binds 964named C<client_connect>, which expects a client port and a nick name as
946a receive callback on C<$SELF>, which expects messages that in turn are 965arguments. It then monitors the client port and binds a receive callback
947broadcast to all clients. 966on C<$SELF>, which expects messages that in turn are broadcast to all
967clients.
948 968
949The two C<mon> calls are a bit tricky - the first C<mon> is a shorthand 969The two C<mon> calls are a bit tricky - the first C<mon> is a shorthand
950for C<mon $client, $SELF>. The second does the normal "client has gone 970for C<mon $client, $SELF>. The second does the normal "client has gone
951away" clean-up action. Both could actually be rolled into one C<mon> 971away" clean-up action.
952action.
953 972
954C<$SELF> is a good hint that something interesting is going on. And 973The last line, the C<rcv $SELF>, is a good hint that something interesting
955indeed, when looking at the client code, there is a new function, 974is going on. And indeed, when looking at the client code, you can see a
956C<spawn>: 975new function, C<spawn>:
976#todo#
957 977
958 use common::sense; 978 use common::sense;
959 use AnyEvent::MP; 979 use AnyEvent::MP;
960 use AnyEvent::MP::Global; 980 use AnyEvent::MP::Global;
961 981
1004 $server = spawn $servernodes->[0], "::client_connect", $client, $nick; 1024 $server = spawn $servernodes->[0], "::client_connect", $client, $nick;
1005 mon $server, $client; 1025 mon $server, $client;
1006 1026
1007And of course the first thing after creating it is monitoring it. 1027And of course the first thing after creating it is monitoring it.
1008 1028
1009The C<spawn> function creates a new port on a remote node and returns 1029Phew, let's go through this in slow motion: the C<spawn> function creates
1010its port ID. After creating the port it calls a function on the remote 1030a new port on a remote node and returns its port ID. After creating
1011node, passing any remaining arguments to it, and - most importantly - 1031the port it calls a function on the remote node, passing any remaining
1012executes the function within the context of the new port, so it can be 1032arguments to it, and - most importantly - executes the function within
1013manipulated by referring to C<$SELF>. The init function can reside in a 1033the context of the new port, so it can be manipulated by referring to
1014module (actually it normally I<should> reside in a module) - AnyEvent::MP 1034C<$SELF>. The init function can reside in a module (actually it normally
1015will automatically load the module if the function isn't defined. 1035I<should> reside in a module) - AnyEvent::MP will automatically load the
1036module if the function isn't defined.
1016 1037
1017The C<spawn> function returns immediately, which means you can instantly 1038The C<spawn> function returns immediately, which means you can instantly
1018send messages to the port, long before the remote node has even heard 1039send messages to the port, long before the remote node has even heard
1019of our request to create a port on it. In fact, the remote node might 1040of our request to create a port on it. In fact, the remote node might
1020not even be running. Despite these troubling facts, everything should 1041not even be running. Despite these troubling facts, everything should
1029cleaned up on connection loss. When the remote node comes up again and our 1050cleaned up on connection loss. When the remote node comes up again and our
1030monitoring message can be delivered, it will instantly fail because the 1051monitoring message can be delivered, it will instantly fail because the
1031port has been cleaned up in the meantime. 1052port has been cleaned up in the meantime.
1032 1053
1033If your head is spinning by now, that's fine - just keep in mind, after 1054If your head is spinning by now, that's fine - just keep in mind, after
1034creating a port, monitor it on the local node, and monitor "the other 1055creating a port using C<spawn>, monitor it on the local node, and monitor
1035side" from the remote node, and all will be cleaned up just fine. 1056"the other side" from the remote node, and all will be cleaned up just
1057fine.
1036 1058
1037=head2 Services 1059=head2 Services
1038 1060
1039Above it was mentioned that C<spawn> automatically loads modules, and this 1061Above it was mentioned that C<spawn> automatically loads modules, and this
1040can be exploited in various ways. 1062can be exploited in various ways.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines