ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Global.pm
Revision: 1.66
Committed: Sat Mar 24 00:48:53 2012 UTC (12 years, 2 months ago) by root
Branch: MAIN
Changes since 1.65: +15 -17 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 AnyEvent::MP::Global - network backbone services
4
5 =head1 SYNOPSIS
6
7 use AnyEvent::MP::Global;
8
9 =head1 DESCRIPTION
10
11 This module is usually run (or started on) seed nodes and provides a
12 variety of services to connected nodes, such as the distributed database.
13
14 The global nodes form a fully-meshed network, that is, all global nodes
15 currently maintain connections to all other global nodes.
16
17 Loading this module (e.g. as a service) transforms the local node into a
18 global node. There are no user-servicable parts inside.
19
20 For a limited time, this module also exports some AEMP 1.x compatibility
21 functions (C<grp_reg>, C<grp_get> and C<grp_mon>).
22
23 =cut
24
25 package AnyEvent::MP::Global;
26
27 use common::sense;
28 use Carp ();
29
30 use AnyEvent ();
31
32 use AnyEvent::MP;
33 use AnyEvent::MP::Kernel;
34
35 AE::log 7 => "starting global service.";
36
37 #############################################################################
38 # node protocol parts for global nodes
39
40 package AnyEvent::MP::Kernel;
41
42 # TODO: this is ugly (classical use vars vs. our),
43 # maybe this should go into MP::Kernel
44
45 our %NODE;
46 our $NODE;
47
48 our $GLOBAL;
49 our $MASTER;
50 our $MASTER_TIMER;
51
52 our %GLOBAL_SLAVE;
53
54 our %GLOBAL_DB; # global db
55 our %LOCAL_DBS; # local databases of other global nodes
56 our %LOCAL_DB; # this node database
57
58 our $SRCNODE; # the origin node id
59 our %NODE_REQ;
60
61 # only in global code
62 our %GLOBAL_MON; # monitors {family}
63
64 # broadcasts a message to all other global nodes
65 sub g_broadcast {
66 snd $_, @_
67 for grep $_ ne $NODE && node_is_up $_, keys %{ $GLOBAL_DB{"'g"} }
68 }
69
70 # add/replace/del inside a family in the database
71 # @$del must not contain any key in %$set
72 sub g_upd {
73 my ($node, $family, $set, $del) = @_;
74
75 my $ldb = $LOCAL_DBS{$node}{$family} ||= {};
76 my $gdb = $GLOBAL_DB {$family} ||= {};
77
78 # add/replace keys
79 while (my ($k, $v) = each %$set) {
80 $ldb->{$k} =
81 $gdb->{$k} = $v;
82 }
83
84 my @del; # actual deletes
85
86 # take care of deletes
87 keydel:
88 for my $k (@$del) {
89 delete $ldb->{$k};
90 delete $gdb->{$k};
91
92 # check if some other node still has the key, then we don't delete, but change
93 for (values %LOCAL_DBS) {
94 if (exists $_->{$family}{$k}) {
95 $set->{$k} = $gdb->{$k} = $_->{$family}{$k};
96
97 next keydel;
98 }
99 }
100
101 push @del, $k;
102 }
103
104 # family could be empty now
105 delete $GLOBAL_DB{$family} unless %$gdb;
106 delete $LOCAL_DBS{$node}{$family} unless %$ldb;
107
108 g_broadcast g_upd => $family, $set, \@del
109 if exists $GLOBAL_SLAVE{$node};
110
111 # tell subscribers we have changed the family
112 snd $_ => g_chg2 => $family, $set, \@del
113 for keys %{ $GLOBAL_MON{$family} };
114 }
115
116 # set the whole (node-local) database - previous value must be empty
117 sub g_set($$) {
118 my ($node, $db) = @_;
119
120 while (my ($f, $k) = each %$db) {
121 g_upd $node, $f, $k;
122 }
123 }
124
125 # delete all keys from a database
126 sub g_clr($) {
127 my ($node) = @_;
128
129 my $db = $LOCAL_DBS{$node};
130
131 while (my ($f, $k) = each %$db) {
132 g_upd $node, $f, undef, [keys %$k];
133 }
134
135 delete $LOCAL_DBS{$node};
136 }
137
138 # gather node databases from slaves
139
140 # other node wants to make us the master
141 $NODE_REQ{g_slave} = sub {
142 my ($db) = @_
143 or return; # empty g_slave is used to start global service
144
145 my $node = $SRCNODE;
146 undef $GLOBAL_SLAVE{$node};
147 g_set $node, $db;
148 };
149
150 # other node (global and slave) sends us their database
151 $NODE_REQ{g_set} = sub {
152 &g_set ($SRCNODE, @_);
153 };
154
155 # other node (global and slave) sends us a family update
156 $NODE_REQ{g_upd} = sub {
157 &g_upd ($SRCNODE, @_);
158 };
159
160 # slave node wants to know the listeners of a node
161 $NODE_REQ{g_find} = sub {
162 my ($node) = @_;
163
164 snd $SRCNODE, g_found => $node, $GLOBAL_DB{"'l"}{$node};
165 };
166
167 $NODE_REQ{g_db_family} = sub {
168 my ($family, $id) = @_;
169 snd $SRCNODE, g_reply => $id, $GLOBAL_DB{$family} || {};
170 };
171
172 $NODE_REQ{g_db_keys} = sub {
173 my ($family, $id) = @_;
174 snd $SRCNODE, g_reply => $id, [keys %{ $GLOBAL_DB{$family} } ];
175 };
176
177 $NODE_REQ{g_db_values} = sub {
178 my ($family, $id) = @_;
179 snd $SRCNODE, g_reply => $id, [values %{ $GLOBAL_DB{$family} } ];
180 };
181
182 # monitoring
183
184 sub g_slave_disconnect($) {
185 my ($node) = @_;
186
187 g_clr $node;
188
189 if (my $mon = delete $GLOBAL_SLAVE{$node}) {
190 while (my ($f, $fv) = each %$mon) {
191 delete $GLOBAL_MON{$f}{$_}
192 for keys %$fv;
193
194 delete $GLOBAL_MON{$f}
195 unless %{ $GLOBAL_MON{$f} };
196 }
197 }
198 }
199
200 # g_mon0 family - stop monitoring
201 $NODE_REQ{g_mon0} = sub {
202 delete $GLOBAL_MON{$_[0]}{$SRCNODE};
203 delete $GLOBAL_MON{$_[0]} unless %{ $GLOBAL_MON{$_[0]} };
204
205 delete $GLOBAL_SLAVE{$SRCNODE}{$_[0]};
206 };
207
208 # g_mon1 family key - start monitoring
209 $NODE_REQ{g_mon1} = sub {
210 undef $GLOBAL_SLAVE{$SRCNODE}{$_[0]};
211 undef $GLOBAL_MON{$_[0]}{$SRCNODE};
212
213 snd $SRCNODE, g_chg1 => $_[0], $GLOBAL_DB{$_[0]};
214 };
215
216 #############################################################################
217 # switch to global mode
218
219 # maintain conenctions to all global nodes that we know of
220 db_mon "'g" => sub {
221 keepalive_add $_ for @{ $_[1] };
222 keepalive_del $_ for @{ $_[3] };
223 };
224
225 # delete slaves on node-down
226 # clear slave db on node-down
227 mon_nodes sub {
228 g_slave_disconnect $_[0] unless $_[1];
229 };
230
231 # tell everybody who connects that we are a global node
232 push @AnyEvent::MP::Transport::HOOK_GREET, sub {
233 $_[0]{local_greeting}{global} = 1;
234 };
235
236 # connect from a global node
237 sub g_global_connect {
238 my ($node) = @_;
239
240 # we need to set this currently, as to avoid race conditions
241 # because it takes a while until the other global node tells us it is global,
242 # but we need to send updates even before that.
243
244 undef $GLOBAL_DB {"'g"}{$node};
245 undef $LOCAL_DBS{$node}{"'g"}{$node};
246
247 # global nodes send all local databases, merged,
248 # as their local database to other global nodes
249 my %db;
250
251 for (values %LOCAL_DBS) {
252 while (my ($f, $fv) = each %$_) {
253 while (my ($k, $kv) = each %$fv) {
254 $db{$f}{$k} = $kv;
255 }
256 }
257 }
258
259 snd $node => g_set => \%db;
260 }
261
262 # send our database to every global node that connects
263 push @AnyEvent::MP::Transport::HOOK_CONNECT, sub {
264 return unless $_[0]{remote_greeting}{global};
265
266 g_global_connect $_[0]{remote_node};
267 };
268
269 # tell other global nodes that we are global now
270 # TODO: there is probably a race when two conencted nodes beocme global at the same time
271 # very ugly.
272 for (values %NODE) {
273 if ($_->{transport} && $_->{transport}{remote_greeting}{global}) {
274 snd $_->{id} => "g_global";
275 g_global_connect $_->{id};
276 }
277 }
278
279 $NODE_REQ{g_global} = sub {
280 g_slave_disconnect $SRCNODE;
281
282 my $node = $NODE{$SRCNODE};
283 $node->{transport}{remote_greeting}{global} = 1;
284
285 g_global_connect $SRCNODE;
286 };
287
288 # enable global mode
289 $GLOBAL = 1;
290
291 # global nodes are their own masters - this
292 # resends global requests and sets the local database.
293 master_set $NODE;
294
295 # now add us to the set of global nodes
296 db_set "'g" => $NODE => undef;
297
298 #############################################################################
299 # compatibility functions for aemp 1.0
300
301 package AnyEvent::MP::Global;
302
303 use base "Exporter";
304 our @EXPORT = qw(grp_reg grp_get grp_mon);
305
306 sub grp_reg($$) {
307 &db_reg
308 }
309
310 sub grp_get($) {
311 my @ports = keys %{ $AnyEvent::MP::Kernel::GLOBAL_DB{$_[0]} };
312
313 @ports ? \@ports : undef
314 }
315
316 sub grp_mon($$) {
317 my ($grp, $cb) = @_;
318
319 db_mon $grp => sub {
320 my ($ports, $add, $chg, $del) = @_;
321
322 $cb->([keys %$ports], $add, $del);
323 };
324 }
325
326 =head1 SEE ALSO
327
328 L<AnyEvent::MP>.
329
330 =head1 AUTHOR
331
332 Marc Lehmann <schmorp@schmorp.de>
333 http://home.schmorp.de/
334
335 =cut
336
337 1
338