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