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