1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::MP::Node - a single processing node (CPU/process...) |
3 | AnyEvent::MP::Node - represent a node |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::MP::Node; |
7 | use AnyEvent::MP::Node; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
|
|
10 | |
|
|
11 | This is an internal utility module, horrible to look at, so don't. |
10 | |
12 | |
11 | =cut |
13 | =cut |
12 | |
14 | |
13 | package AnyEvent::MP::Node; |
15 | package AnyEvent::MP::Node; |
14 | |
16 | |
… | |
… | |
18 | use AnyEvent::Util (); |
20 | use AnyEvent::Util (); |
19 | use AnyEvent::Socket (); |
21 | use AnyEvent::Socket (); |
20 | |
22 | |
21 | use AnyEvent::MP::Transport (); |
23 | use AnyEvent::MP::Transport (); |
22 | |
24 | |
23 | use base Exporter::; |
25 | sub new { |
|
|
26 | my ($self, $id) = @_; |
24 | |
27 | |
25 | our $VERSION = '0.0'; |
28 | $self = bless { id => $id }, $self; |
26 | |
29 | |
27 | our $DEFAULT_PORT = "4040"; |
30 | $self->init; |
|
|
31 | $self->transport_reset; |
28 | |
32 | |
29 | sub normalise_noderef($) { |
33 | $self |
|
|
34 | } |
|
|
35 | |
|
|
36 | sub init { |
|
|
37 | # |
|
|
38 | } |
|
|
39 | |
|
|
40 | sub send { |
|
|
41 | &{ shift->{send} } |
|
|
42 | } |
|
|
43 | |
|
|
44 | # nodes reachable via the network |
|
|
45 | package AnyEvent::MP::Node::Remote; |
|
|
46 | |
|
|
47 | use base "AnyEvent::MP::Node"; |
|
|
48 | |
|
|
49 | # called at init time, mostly sets {send} |
|
|
50 | sub transport_reset { |
30 | my ($noderef) = @_; |
51 | my ($self) = @_; |
31 | |
52 | |
32 | my $cv = AE::cv; |
53 | delete $self->{transport}; |
33 | my @res; |
|
|
34 | |
54 | |
35 | $cv->begin (sub { |
55 | Scalar::Util::weaken $self; |
36 | my %seen; |
56 | |
37 | my @refs; |
57 | $self->{send} = sub { |
38 | for (sort { $a->[0] <=> $b->[0] } @res) { |
58 | push @{$self->{queue}}, shift; |
39 | push @refs, $_->[1] unless $seen{$_->[1]}++ |
59 | $self->connect; |
|
|
60 | }; |
|
|
61 | } |
|
|
62 | |
|
|
63 | # called each time we fail to establish a connection, |
|
|
64 | # or the existing connection failed |
|
|
65 | sub transport_error { |
|
|
66 | my ($self, @reason) = @_; |
|
|
67 | |
|
|
68 | my $no_transport = !$self->{transport}; |
|
|
69 | |
|
|
70 | delete $self->{connect_w}; |
|
|
71 | delete $self->{connect_to}; |
|
|
72 | |
|
|
73 | delete $self->{queue}; |
|
|
74 | $self->transport_reset; |
|
|
75 | |
|
|
76 | if (my $mon = delete $self->{lmon}) { |
|
|
77 | $_->(@reason) for map @$_, values %$mon; |
|
|
78 | } |
|
|
79 | |
|
|
80 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
|
|
81 | unless $no_transport; |
|
|
82 | |
|
|
83 | # if we are here and are idle, we nuke ourselves |
|
|
84 | delete $AnyEvent::MP::Kernel::NODE{$self->{id}} |
|
|
85 | unless $self->{transport} || $self->{connect_to}; |
|
|
86 | } |
|
|
87 | |
|
|
88 | # called after handshake was successful |
|
|
89 | sub transport_connect { |
|
|
90 | my ($self, $transport) = @_; |
|
|
91 | |
|
|
92 | delete $self->{trial}; |
|
|
93 | |
|
|
94 | $self->transport_error (transport_error => $self->{id}, "switched connections") |
|
|
95 | if $self->{transport}; |
|
|
96 | |
|
|
97 | delete $self->{connect_addr}; |
|
|
98 | delete $self->{connect_w}; |
|
|
99 | delete $self->{connect_to}; |
|
|
100 | |
|
|
101 | $self->{transport} = $transport; |
|
|
102 | |
|
|
103 | my $transport_send = $transport->{send}; |
|
|
104 | |
|
|
105 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
|
|
106 | |
|
|
107 | $self->{send} = $transport_send; |
|
|
108 | |
|
|
109 | $transport_send->($_) |
|
|
110 | for @{ delete $self->{queue} || [] }; |
|
|
111 | } |
|
|
112 | |
|
|
113 | sub connect { |
|
|
114 | my ($self) = @_; |
|
|
115 | |
|
|
116 | return if $self->{transport}; |
|
|
117 | |
|
|
118 | Scalar::Util::weaken $self; |
|
|
119 | |
|
|
120 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
|
|
121 | |
|
|
122 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
|
|
123 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
|
|
124 | }; |
|
|
125 | |
|
|
126 | # maybe @$addresses? |
|
|
127 | my @addresses = @{ |
|
|
128 | $AnyEvent::MP::Kernel::GLOBAL_ADDR->{$self->{id}} |
|
|
129 | || $AnyEvent::MP::Kernel::NODE_ADDR->{$self->{id}} |
|
|
130 | }; |
|
|
131 | |
|
|
132 | unless (@addresses) { |
|
|
133 | # on global nodes, all betsa re off now - we either know the node, or we don't |
|
|
134 | unless ($AnyEvent::MP::Kernel::GLOBAL) { |
|
|
135 | $self->{connect_w} = AnyEvent::MP::Kernel::global_req ( |
|
|
136 | global_find => $self->{id}, |
|
|
137 | sub { |
|
|
138 | return unless $self; # just to be sure |
|
|
139 | return unless @{ $_[0] }; |
|
|
140 | |
|
|
141 | local $AnyEvent::MP::Kernel::GLOBAL_ADDR->{$self->{id}} = $_[0]; #d# UGLY |
|
|
142 | $self->connect; |
|
|
143 | } |
|
|
144 | ); |
40 | } |
145 | } |
41 | shift->send (join ",", @refs); |
146 | |
|
|
147 | return; |
42 | }); |
148 | } |
43 | |
149 | |
44 | $noderef = $DEFAULT_PORT unless length $noderef; |
150 | $self->{connect_addr} = \@addresses; # a bit weird, but efficient |
45 | |
151 | |
46 | my $idx; |
152 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
47 | for my $t (split /,/, $noderef) { |
|
|
48 | my $pri = ++$idx; |
|
|
49 | |
|
|
50 | #TODO: this should be outside normalise_noderef and in become_public |
|
|
51 | if ($t =~ /^\d*$/) { |
|
|
52 | require POSIX; |
|
|
53 | my $nodename = (POSIX::uname ())[1]; |
|
|
54 | |
153 | |
55 | $cv->begin; |
154 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
56 | AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub { |
|
|
57 | for (@_) { |
|
|
58 | my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3]; |
|
|
59 | push @res, [ |
|
|
60 | $pri += 1e-5, |
|
|
61 | AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service |
|
|
62 | ]; |
|
|
63 | } |
|
|
64 | $cv->end; |
|
|
65 | }; |
|
|
66 | |
155 | |
67 | # my (undef, undef, undef, undef, @ipv4) = gethostbyname $nodename; |
156 | $interval = ($monitor - $interval) / @addresses |
68 | # |
157 | if ($monitor - $interval) / @addresses < $interval; |
69 | # for (@ipv4) { |
158 | |
70 | # push @res, [ |
159 | $interval = 0.4 if $interval < 0.4; |
71 | # $pri, |
160 | |
72 | # AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $_, $t || $DEFAULT_PORT, |
161 | my @endpoints; |
73 | # ]; |
162 | |
74 | # } |
163 | $self->{connect_w} = AE::timer 0.050 * rand, $interval * (0.9 + 0.1 * rand), sub { |
75 | } else { |
164 | @endpoints = @addresses |
|
|
165 | unless @endpoints; |
|
|
166 | |
|
|
167 | my $endpoint = shift @endpoints; |
|
|
168 | |
|
|
169 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
170 | |
|
|
171 | $self->{trial}{$endpoint} ||= do { |
76 | my ($host, $port) = AnyEvent::Socket::parse_hostport $t, "aemp=$DEFAULT_PORT" |
172 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
77 | or Carp::croak "$t: unparsable transport descriptor"; |
173 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
78 | |
174 | |
79 | $cv->begin; |
175 | AnyEvent::MP::Transport::mp_connect |
80 | AnyEvent::Socket::resolve_sockaddr $host, $port, "tcp", 0, undef, sub { |
176 | $host, $port, |
81 | for (@_) { |
177 | sub { delete $self->{trial}{$endpoint} }, |
82 | my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3]; |
|
|
83 | push @res, [ |
|
|
84 | $pri += 1e-5, |
|
|
85 | AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service |
|
|
86 | ]; |
|
|
87 | } |
|
|
88 | $cv->end; |
|
|
89 | } |
|
|
90 | } |
178 | }; |
91 | } |
179 | }; |
92 | |
|
|
93 | $cv->end; |
|
|
94 | |
|
|
95 | $cv |
|
|
96 | } |
|
|
97 | |
|
|
98 | sub new { |
|
|
99 | my ($class, $noderef) = @_; |
|
|
100 | |
|
|
101 | bless { noderef => $noderef }, $class |
|
|
102 | } |
|
|
103 | |
|
|
104 | package AnyEvent::MP::Node::Direct; |
|
|
105 | |
|
|
106 | use base "AnyEvent::MP::Node"; |
|
|
107 | |
|
|
108 | sub send { |
|
|
109 | my ($self, $msg) = @_; |
|
|
110 | |
|
|
111 | if ($self->{transport}) { |
|
|
112 | $self->{transport}->send ($msg); |
|
|
113 | } elsif ($self->{queue}) { |
|
|
114 | push @{ $self->{queue} }, $msg; |
|
|
115 | } else { |
|
|
116 | $self->{queue} = [$msg]; |
|
|
117 | $self->connect; |
|
|
118 | } |
|
|
119 | } |
180 | } |
120 | |
181 | |
121 | sub kill { |
182 | sub kill { |
122 | my ($self, $port, @reason) = @_; |
183 | my ($self, $port, @reason) = @_; |
123 | |
184 | |
124 | $self->send (["", kil => $port, @reason]); |
185 | $self->{send} (["", kil => $port, @reason]); |
125 | } |
186 | } |
126 | |
187 | |
127 | sub monitor { |
188 | sub monitor { |
128 | my ($self, $portid, $cb) = @_; |
189 | my ($self, $portid, $cb) = @_; |
129 | |
190 | |
130 | return $cb->("node failed conenction") |
|
|
131 | if $self->{failed}; |
|
|
132 | |
|
|
133 | my $list = $self->{lmon}{$portid} ||= []; |
191 | my $list = $self->{lmon}{$portid} ||= []; |
134 | |
192 | |
135 | $self->send (["", mon1 => $portid]) |
193 | $self->send (["", mon1 => $portid]) |
136 | unless @$list; |
194 | unless @$list || !length $portid; |
137 | |
195 | |
138 | push @$list, $cb; |
196 | push @$list, $cb; |
139 | } |
197 | } |
140 | |
198 | |
141 | sub unmonitor { |
199 | sub unmonitor { |
… | |
… | |
150 | $self->send (["", mon0 => $portid]); |
208 | $self->send (["", mon0 => $portid]); |
151 | delete $self->{monitor}{$portid}; |
209 | delete $self->{monitor}{$portid}; |
152 | } |
210 | } |
153 | } |
211 | } |
154 | |
212 | |
155 | sub set_transport { |
213 | package AnyEvent::MP::Node::Self; |
156 | my ($self, $transport) = @_; |
|
|
157 | |
214 | |
158 | $self->clr_transport |
215 | use base "AnyEvent::MP::Node"; |
159 | if $self->{transport}; |
|
|
160 | |
|
|
161 | if ( |
|
|
162 | exists $self->{remote_uniq} |
|
|
163 | && $self->{remote_uniq} ne $transport->{remote_uniq} |
|
|
164 | ) { |
|
|
165 | # uniq changed, different node |
|
|
166 | $self->fail ("node restart detected"); |
|
|
167 | } |
|
|
168 | |
|
|
169 | delete $self->{trial}; |
|
|
170 | delete $self->{next_connect}; |
|
|
171 | delete $self->{failed}; |
|
|
172 | |
|
|
173 | $self->{remote_uniq} = $transport->{remote_uniq}; |
|
|
174 | $self->{transport} = $transport; |
|
|
175 | |
|
|
176 | $transport->send ($_) |
|
|
177 | for @{ delete $self->{queue} || [] }; |
|
|
178 | } |
|
|
179 | |
|
|
180 | sub fail { |
|
|
181 | my ($self, @reason) = @_; |
|
|
182 | |
|
|
183 | delete $self->{queue}; |
|
|
184 | |
|
|
185 | $self->{failed} = 1; |
|
|
186 | |
|
|
187 | if (my $mon = delete $self->{lmon}) { |
|
|
188 | $_->(@reason) for map @$_, values %$mon; |
|
|
189 | } |
|
|
190 | } |
|
|
191 | |
|
|
192 | sub clr_transport { |
|
|
193 | my ($self, @reason) = @_; |
|
|
194 | |
|
|
195 | delete $self->{transport}; |
|
|
196 | $self->connect; |
|
|
197 | } |
|
|
198 | |
216 | |
199 | sub connect { |
217 | sub connect { |
|
|
218 | # we are trivially connected |
|
|
219 | } |
|
|
220 | |
|
|
221 | # delay every so often to avoid recursion, also used to delay after spawn |
|
|
222 | our $DELAY = -50; |
|
|
223 | our @DELAY; |
|
|
224 | our $DELAY_W; |
|
|
225 | |
|
|
226 | sub _send_delayed { |
|
|
227 | local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE{""}; |
|
|
228 | (shift @DELAY)->() |
|
|
229 | while @DELAY; |
|
|
230 | undef $DELAY_W; |
|
|
231 | $DELAY = -50; |
|
|
232 | } |
|
|
233 | |
|
|
234 | sub transport_reset { |
200 | my ($self) = @_; |
235 | my ($self) = @_; |
201 | |
236 | |
202 | Scalar::Util::weaken $self; |
237 | Scalar::Util::weaken $self; |
203 | |
238 | |
204 | unless (exists $self->{n_noderef}) { |
239 | $self->{send} = sub { |
205 | return if $self->{n_noderef_}++; |
240 | if ($DELAY++ >= 0) { |
206 | (AnyEvent::MP::Node::normalise_noderef ($self->{noderef}))->cb (sub { |
241 | my $msg = $_[0]; |
|
|
242 | push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
|
|
243 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
207 | $self or return; |
244 | return; |
208 | delete $self->{n_noderef_}; |
|
|
209 | my $noderef = shift->recv; |
|
|
210 | |
|
|
211 | $self->{n_noderef} = $noderef; |
|
|
212 | |
|
|
213 | $AnyEvent::MP::Base::NODE{$_} = $self |
|
|
214 | for split /,/, $noderef; |
|
|
215 | |
|
|
216 | $self->connect; |
|
|
217 | }); |
245 | } |
|
|
246 | |
|
|
247 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
|
|
248 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
|
|
249 | }; |
|
|
250 | } |
|
|
251 | |
|
|
252 | sub transport_connect { |
|
|
253 | my ($self, $tp) = @_; |
|
|
254 | |
|
|
255 | $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
|
|
256 | } |
|
|
257 | |
|
|
258 | sub kill { |
|
|
259 | my (undef, @args) = @_; |
|
|
260 | |
|
|
261 | # we _always_ delay kil's, to avoid calling mon callbacks |
|
|
262 | # from anything but the event loop context. |
|
|
263 | $DELAY = 1; |
|
|
264 | push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) }; |
|
|
265 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
|
|
266 | } |
|
|
267 | |
|
|
268 | sub monitor { |
|
|
269 | # maybe always delay, too? |
|
|
270 | if ($DELAY_W) { |
|
|
271 | my @args = @_; |
|
|
272 | push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) }; |
218 | return; |
273 | return; |
219 | } |
274 | } |
220 | |
275 | &AnyEvent::MP::Kernel::_monitor; |
221 | $self->{retry} ||= [split /,/, $self->{n_noderef}]; |
|
|
222 | |
|
|
223 | my $endpoint = shift @{ $self->{retry} }; |
|
|
224 | |
|
|
225 | if (defined $endpoint) { |
|
|
226 | $self->{trial}{$endpoint} ||= do { |
|
|
227 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
228 | or return; |
|
|
229 | |
|
|
230 | my ($w, $g); |
|
|
231 | |
|
|
232 | $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub { |
|
|
233 | delete $self->{trial}{$endpoint}; |
|
|
234 | }; |
|
|
235 | $g = AnyEvent::MP::Transport::mp_connect |
|
|
236 | $host, $port, |
|
|
237 | sub { |
|
|
238 | delete $self->{trial}{$endpoint} |
|
|
239 | unless @_; |
|
|
240 | $g = shift; |
|
|
241 | }; |
|
|
242 | ; |
|
|
243 | |
|
|
244 | [$w, \$g] |
|
|
245 | }; |
|
|
246 | } else { |
|
|
247 | delete $self->{retry}; |
|
|
248 | } |
|
|
249 | |
|
|
250 | $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub { |
|
|
251 | $self->connect; |
|
|
252 | }; |
|
|
253 | } |
|
|
254 | |
|
|
255 | package AnyEvent::MP::Node::Self; |
|
|
256 | |
|
|
257 | use base "AnyEvent::MP::Node"; |
|
|
258 | |
|
|
259 | sub set_transport { |
|
|
260 | die "FATAL error, set_transport was called"; |
|
|
261 | } |
|
|
262 | |
|
|
263 | sub send { |
|
|
264 | local $AnyEvent::MP::Base::SRCNODE = $_[0]; |
|
|
265 | AnyEvent::MP::Base::_inject (@{ $_[1] }); |
|
|
266 | } |
|
|
267 | |
|
|
268 | sub kill { |
|
|
269 | my ($self, $port, @reason) = @_; |
|
|
270 | |
|
|
271 | delete $AnyEvent::MP::Base::PORT{$port}; |
|
|
272 | delete $AnyEvent::MP::Base::PORT_DATA{$port}; |
|
|
273 | |
|
|
274 | my $mon = delete $AnyEvent::MP::Base::LMON{$port} |
|
|
275 | or !@reason |
|
|
276 | or $AnyEvent::MP::Base::WARN->("unmonitored local port $port died with reason: @reason"); |
|
|
277 | |
|
|
278 | $_->(@reason) for values %$mon; |
|
|
279 | } |
|
|
280 | |
|
|
281 | sub monitor { |
|
|
282 | my ($self, $portid, $cb) = @_; |
|
|
283 | |
|
|
284 | return $cb->() |
|
|
285 | unless exists $AnyEvent::MP::Base::PORT{$portid}; |
|
|
286 | |
|
|
287 | $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb; |
|
|
288 | } |
276 | } |
289 | |
277 | |
290 | sub unmonitor { |
278 | sub unmonitor { |
291 | my ($self, $portid, $cb) = @_; |
279 | # no need to always delay |
|
|
280 | if ($DELAY_W) { |
|
|
281 | my @args = @_; |
|
|
282 | push @DELAY, sub { AnyEvent::MP::Kernel::_unmonitor (@args) }; |
|
|
283 | return; |
|
|
284 | } |
292 | |
285 | |
293 | delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0}; |
286 | &AnyEvent::MP::Kernel::_unmonitor; |
294 | } |
287 | } |
295 | |
288 | |
296 | =head1 SEE ALSO |
289 | =head1 SEE ALSO |
297 | |
290 | |
298 | L<AnyEvent>. |
291 | L<AnyEvent::MP>. |
299 | |
292 | |
300 | =head1 AUTHOR |
293 | =head1 AUTHOR |
301 | |
294 | |
302 | Marc Lehmann <schmorp@schmorp.de> |
295 | Marc Lehmann <schmorp@schmorp.de> |
303 | http://home.schmorp.de/ |
296 | http://home.schmorp.de/ |