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 | $self->init; |
|
|
30 | $self->transport_reset; |
|
|
31 | |
|
|
32 | $self |
|
|
33 | } |
|
|
34 | |
|
|
35 | sub init { |
|
|
36 | # |
|
|
37 | } |
|
|
38 | |
|
|
39 | sub send { |
|
|
40 | &{ shift->{send} } |
|
|
41 | } |
|
|
42 | |
|
|
43 | # nodes reachable via the network |
|
|
44 | package AnyEvent::MP::Node::Remote; # a remote node |
104 | |
45 | |
105 | use base "AnyEvent::MP::Node"; |
46 | use base "AnyEvent::MP::Node"; |
106 | |
47 | |
107 | sub send { |
48 | # called at init time, mostly sets {send} |
|
|
49 | sub transport_reset { |
108 | my ($self, $msg) = @_; |
50 | my ($self) = @_; |
109 | |
51 | |
110 | if ($self->{transport}) { |
52 | delete $self->{transport}; |
111 | $self->{transport}->send ($msg); |
53 | |
112 | } elsif ($self->{queue}) { |
54 | Scalar::Util::weaken $self; |
|
|
55 | |
|
|
56 | $self->{send} = sub { |
113 | push @{ $self->{queue} }, $msg; |
57 | push @{$self->{queue}}, shift; |
114 | } else { |
|
|
115 | $self->{queue} = [$msg]; |
|
|
116 | $self->connect; |
58 | $self->connect; |
117 | } |
59 | }; |
118 | } |
60 | } |
119 | |
61 | |
|
|
62 | # called each time we fail to establish a connection, |
|
|
63 | # or the existing connection failed |
120 | sub set_transport { |
64 | sub transport_error { |
|
|
65 | my ($self, @reason) = @_; |
|
|
66 | |
|
|
67 | my $no_transport = !$self->{transport}; |
|
|
68 | |
|
|
69 | delete $self->{connect_w}; |
|
|
70 | delete $self->{connect_to}; |
|
|
71 | |
|
|
72 | delete $self->{queue}; |
|
|
73 | $self->transport_reset; |
|
|
74 | |
|
|
75 | if (my $mon = delete $self->{lmon}) { |
|
|
76 | $_->(@reason) for map @$_, values %$mon; |
|
|
77 | } |
|
|
78 | |
|
|
79 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
|
|
80 | unless $no_transport; |
|
|
81 | |
|
|
82 | # if we are here and are idle, we nuke ourselves |
|
|
83 | delete $AnyEvent::MP::Kernel::NODE{$self->{id}} |
|
|
84 | unless $self->{transport} || $self->{connect_to}; |
|
|
85 | } |
|
|
86 | |
|
|
87 | # called after handshake was successful |
|
|
88 | sub transport_connect { |
121 | my ($self, $transport) = @_; |
89 | my ($self, $transport) = @_; |
122 | |
90 | |
123 | delete $self->{trial}; |
91 | delete $self->{trial}; |
|
|
92 | |
|
|
93 | $self->transport_error (transport_error => $self->{id}, "switched connections") |
|
|
94 | if $self->{transport}; |
|
|
95 | |
124 | delete $self->{next_connect}; |
96 | delete $self->{connect_w}; |
|
|
97 | delete $self->{connect_to}; |
125 | |
98 | |
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; |
99 | $self->{transport} = $transport; |
137 | |
100 | |
|
|
101 | my $transport_send = $transport->{send}; |
|
|
102 | |
|
|
103 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
|
|
104 | |
|
|
105 | $self->{send} = $transport_send; |
|
|
106 | |
138 | $transport->send ($_) |
107 | $transport_send->($_) |
139 | for @{ delete $self->{queue} || [] }; |
108 | for @{ delete $self->{queue} || [] }; |
140 | } |
|
|
141 | |
|
|
142 | sub clr_transport { |
|
|
143 | my ($self) = @_; |
|
|
144 | |
|
|
145 | delete $self->{transport}; |
|
|
146 | |
|
|
147 | $self->connect; |
|
|
148 | } |
109 | } |
149 | |
110 | |
150 | sub connect { |
111 | sub connect { |
151 | my ($self) = @_; |
112 | my ($self) = @_; |
152 | |
113 | |
|
|
114 | return if $self->{transport}; |
|
|
115 | return if $self->{connect_w}; |
|
|
116 | |
153 | Scalar::Util::weaken $self; |
117 | Scalar::Util::weaken $self; |
154 | |
118 | |
155 | unless (exists $self->{n_noderef}) { |
119 | $self->{connect_to} ||= AE::timer $AnyEvent::MP::Kernel::CONFIG->{connect_interval}, 0, sub { |
156 | return if $self->{n_noderef_}++; |
120 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
157 | (AnyEvent::MP::Node::normalise_noderef ($self->{noderef}))->cb (sub { |
|
|
158 | $self or return; |
|
|
159 | delete $self->{n_noderef_}; |
|
|
160 | my $noderef = shift->recv; |
|
|
161 | |
|
|
162 | $self->{n_noderef} = $noderef; |
|
|
163 | |
|
|
164 | $AnyEvent::MP::Base::NODE{$_} = $self |
|
|
165 | for split /,/, $noderef; |
|
|
166 | |
|
|
167 | $self->connect; |
|
|
168 | }); |
|
|
169 | return; |
|
|
170 | } |
121 | }; |
171 | |
122 | |
172 | $self->{retry} ||= [split /,/, $self->{n_noderef}]; |
123 | # maybe @$addresses? |
|
|
124 | my $addresses = $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}}; |
173 | |
125 | |
174 | my $endpoint = shift @{ $self->{retry} }; |
126 | if ($addresses) { |
|
|
127 | $self->connect_to ($addresses); |
|
|
128 | } else { |
|
|
129 | # on global nodes, all bets are off now - we either know the node, or we don't |
|
|
130 | unless ($AnyEvent::MP::Kernel::GLOBAL) { |
|
|
131 | AnyEvent::MP::Kernel::g_find ($self->{id}); |
|
|
132 | } |
|
|
133 | } |
|
|
134 | } |
175 | |
135 | |
176 | if (defined $endpoint) { |
136 | sub connect_to { |
|
|
137 | my ($self, $addresses) = @_; |
|
|
138 | |
|
|
139 | return if $self->{transport}; |
|
|
140 | return if $self->{connect_w}; |
|
|
141 | |
|
|
142 | return unless @$addresses; |
|
|
143 | |
|
|
144 | AE::log 9 => "connecting to $self->{id} with [@$addresses]"; |
|
|
145 | |
|
|
146 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
|
|
147 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
|
|
148 | |
|
|
149 | $interval = ($monitor - $interval) / @$addresses |
|
|
150 | if ($monitor - $interval) / @$addresses < $interval; |
|
|
151 | |
|
|
152 | $interval = 0.4 if $interval < 0.4; |
|
|
153 | |
|
|
154 | my @endpoints = reverse @$addresses; |
|
|
155 | |
|
|
156 | $self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub { |
|
|
157 | my $endpoint = pop @endpoints; |
|
|
158 | |
|
|
159 | AE::log 9 => "connecting to $self->{id} at $endpoint"; |
|
|
160 | |
177 | $self->{trial}{$endpoint} ||= do { |
161 | $self->{trial}{$endpoint} ||= do { |
178 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
162 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
179 | or return; |
163 | or return AE::log critical => "$self->{id}: '$endpoint' is not a resolved node reference."; |
180 | |
164 | |
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 |
165 | AnyEvent::MP::Transport::mp_connect |
187 | $host, $port, |
166 | $host, $port, |
188 | sub { |
|
|
189 | delete $self->{trial}{$endpoint} |
167 | sub { delete $self->{trial}{$endpoint} }, |
190 | unless @_; |
|
|
191 | $g = shift; |
|
|
192 | }; |
|
|
193 | ; |
|
|
194 | |
|
|
195 | [$w, \$g] |
|
|
196 | }; |
168 | }; |
197 | } else { |
|
|
198 | delete $self->{retry}; |
|
|
199 | } |
169 | }; |
|
|
170 | } |
200 | |
171 | |
201 | $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub { |
172 | sub kill { |
202 | $self->connect; |
173 | my ($self, $port, @reason) = @_; |
|
|
174 | |
|
|
175 | $self->{send} (["", kil => $port, @reason]); |
|
|
176 | } |
|
|
177 | |
|
|
178 | sub monitor { |
|
|
179 | my ($self, $portid, $cb) = @_; |
|
|
180 | |
|
|
181 | my $list = $self->{lmon}{$portid} ||= []; |
|
|
182 | |
|
|
183 | $self->send (["", mon1 => $portid]) |
|
|
184 | unless @$list || !length $portid; |
|
|
185 | |
|
|
186 | push @$list, $cb; |
|
|
187 | } |
|
|
188 | |
|
|
189 | sub unmonitor { |
|
|
190 | my ($self, $portid, $cb) = @_; |
|
|
191 | |
|
|
192 | my $list = $self->{lmon}{$portid} |
|
|
193 | or return; |
|
|
194 | |
|
|
195 | @$list = grep $_ != $cb, @$list; |
|
|
196 | |
|
|
197 | unless (@$list) { |
|
|
198 | $self->send (["", mon0 => $portid]); |
|
|
199 | delete $self->{monitor}{$portid}; |
203 | }; |
200 | } |
204 | } |
201 | } |
205 | |
202 | |
206 | package AnyEvent::MP::Node::Self; |
203 | package AnyEvent::MP::Node::Self; # the local node |
207 | |
204 | |
208 | use base "AnyEvent::MP::Node"; |
205 | use base "AnyEvent::MP::Node"; |
209 | |
206 | |
|
|
207 | sub connect { |
|
|
208 | # we are trivially connected |
|
|
209 | } |
|
|
210 | |
|
|
211 | # delay every so often to avoid recursion, also used to delay after spawn |
|
|
212 | our $DELAY = -50; |
|
|
213 | our @DELAY; |
|
|
214 | our $DELAY_W; |
|
|
215 | |
|
|
216 | our $send_delayed = sub { |
|
|
217 | $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
|
|
218 | (shift @DELAY)->() |
|
|
219 | while @DELAY; |
|
|
220 | undef $DELAY_W; |
|
|
221 | $DELAY = -50; |
|
|
222 | }; |
|
|
223 | |
210 | sub set_transport { |
224 | sub transport_reset { |
211 | die "FATAL error, set_transport was called"; |
225 | my ($self) = @_; |
212 | } |
|
|
213 | |
226 | |
214 | sub send { |
227 | Scalar::Util::weaken $self; |
|
|
228 | |
|
|
229 | $self->{send} = sub { |
|
|
230 | if (++$DELAY > 0) { |
|
|
231 | my $msg = $_[0]; |
|
|
232 | push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
|
|
233 | $DELAY_W ||= AE::timer 0, 0, $send_delayed; |
|
|
234 | return; |
|
|
235 | } |
|
|
236 | |
|
|
237 | local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE; |
215 | AnyEvent::MP::Base::_inject ($_[1]); |
238 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
|
|
239 | }; |
|
|
240 | } |
|
|
241 | |
|
|
242 | sub transport_connect { |
|
|
243 | my ($self, $tp) = @_; |
|
|
244 | |
|
|
245 | AE::log 9 => "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"; |
|
|
246 | } |
|
|
247 | |
|
|
248 | sub kill { |
|
|
249 | my (undef, @args) = @_; |
|
|
250 | |
|
|
251 | # we _always_ delay kil's, to avoid calling mon callbacks |
|
|
252 | # from anything but the event loop context. |
|
|
253 | $DELAY = 1; |
|
|
254 | push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) }; |
|
|
255 | $DELAY_W ||= AE::timer 0, 0, $send_delayed; |
|
|
256 | } |
|
|
257 | |
|
|
258 | sub monitor { |
|
|
259 | # maybe always delay, too? |
|
|
260 | if ($DELAY_W) { |
|
|
261 | my @args = @_; |
|
|
262 | push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) }; |
|
|
263 | return; |
|
|
264 | } |
|
|
265 | &AnyEvent::MP::Kernel::_monitor; |
|
|
266 | } |
|
|
267 | |
|
|
268 | sub unmonitor { |
|
|
269 | # no need to always delay |
|
|
270 | if ($DELAY_W) { |
|
|
271 | my @args = @_; |
|
|
272 | push @DELAY, sub { AnyEvent::MP::Kernel::_unmonitor (@args) }; |
|
|
273 | return; |
|
|
274 | } |
|
|
275 | |
|
|
276 | &AnyEvent::MP::Kernel::_unmonitor; |
216 | } |
277 | } |
217 | |
278 | |
218 | =head1 SEE ALSO |
279 | =head1 SEE ALSO |
219 | |
280 | |
220 | L<AnyEvent>. |
281 | L<AnyEvent::MP>. |
221 | |
282 | |
222 | =head1 AUTHOR |
283 | =head1 AUTHOR |
223 | |
284 | |
224 | Marc Lehmann <schmorp@schmorp.de> |
285 | Marc Lehmann <schmorp@schmorp.de> |
225 | http://home.schmorp.de/ |
286 | http://home.schmorp.de/ |