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 | |
… | |
… | |
19 | use AnyEvent::Socket (); |
21 | use AnyEvent::Socket (); |
20 | |
22 | |
21 | use AnyEvent::MP::Transport (); |
23 | use AnyEvent::MP::Transport (); |
22 | |
24 | |
23 | sub new { |
25 | sub new { |
24 | my ($self, $noderef) = @_; |
26 | my ($self, $id) = @_; |
25 | |
27 | |
26 | $self = bless { noderef => $noderef }, $self; |
28 | $self = bless { id => $id }, $self; |
27 | |
29 | |
28 | $self->init; |
30 | $self->init; |
29 | $self->transport_reset; |
31 | $self->transport_reset; |
30 | |
32 | |
31 | $self |
33 | $self |
… | |
… | |
54 | |
56 | |
55 | $self->{send} = sub { |
57 | $self->{send} = sub { |
56 | push @{$self->{queue}}, shift; |
58 | push @{$self->{queue}}, shift; |
57 | $self->connect; |
59 | $self->connect; |
58 | }; |
60 | }; |
59 | |
|
|
60 | $self->connect |
|
|
61 | if $self->{autoconnect}; |
|
|
62 | } |
61 | } |
63 | |
62 | |
64 | # called only after successful handshake |
63 | # called each time we fail to establish a connection, |
|
|
64 | # or the existing connection failed |
65 | sub transport_error { |
65 | sub transport_error { |
66 | my ($self, @reason) = @_; |
66 | my ($self, @reason) = @_; |
67 | |
67 | |
68 | my $no_transport = !$self->{transport}; |
68 | my $no_transport = !$self->{transport}; |
69 | |
69 | |
… | |
… | |
71 | delete $self->{connect_to}; |
71 | delete $self->{connect_to}; |
72 | |
72 | |
73 | delete $self->{queue}; |
73 | delete $self->{queue}; |
74 | $self->transport_reset; |
74 | $self->transport_reset; |
75 | |
75 | |
|
|
76 | if (my $mon = delete $self->{lmon}) { |
|
|
77 | $_->(@reason) for map @$_, values %$mon; |
|
|
78 | } |
|
|
79 | |
76 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
80 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
77 | unless $no_transport; |
81 | unless $no_transport; |
78 | |
82 | |
79 | if (my $mon = delete $self->{lmon}) { |
83 | # if we are here and are idle, we nuke ourselves |
80 | $_->(@reason) for map @$_, values %$mon; |
84 | delete $AnyEvent::MP::Kernel::NODE{$self->{id}} |
81 | } |
85 | unless $self->{transport} || $self->{connect_to}; |
82 | } |
86 | } |
83 | |
87 | |
84 | # called after handshake was successful |
88 | # called after handshake was successful |
85 | sub transport_connect { |
89 | sub transport_connect { |
86 | my ($self, $transport) = @_; |
90 | my ($self, $transport) = @_; |
87 | |
91 | |
|
|
92 | delete $self->{trial}; |
|
|
93 | |
88 | $self->transport_error (transport_error => "switched connections") |
94 | $self->transport_error (transport_error => "switched connections") |
89 | if $self->{transport}; |
95 | if $self->{transport}; |
90 | |
96 | |
|
|
97 | delete $self->{connect_addr}; |
91 | delete $self->{connect_w}; |
98 | delete $self->{connect_w}; |
92 | delete $self->{connect_to}; |
99 | delete $self->{connect_to}; |
93 | |
100 | |
94 | $self->{transport} = $transport; |
101 | $self->{transport} = $transport; |
95 | |
102 | |
96 | my $transport_send = $transport->can ("send"); |
103 | my $transport_send = $transport->can ("send"); |
97 | |
104 | |
|
|
105 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
|
|
106 | |
98 | $self->{send} = sub { |
107 | $self->{send} = sub { |
99 | $transport_send->($transport, $_[0]); |
108 | $transport_send->($transport, $_[0]); |
100 | }; |
109 | }; |
101 | |
110 | |
102 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
|
|
103 | |
|
|
104 | $transport->send ($_) |
111 | $transport->send ($_) |
105 | for @{ delete $self->{queue} || [] }; |
112 | for @{ delete $self->{queue} || [] }; |
106 | } |
113 | } |
107 | |
114 | |
108 | sub connect { |
115 | sub connect { |
109 | my ($self) = @_; |
116 | my ($self, @addresses) = @_; |
110 | |
117 | |
111 | return if $self->{transport}; |
118 | return if $self->{transport}; |
112 | |
119 | |
113 | # just ignore connect requests for slave nodes - let's hope it connects to us instead |
|
|
114 | return if $self->{noderef} =~ /^slave\//; |
|
|
115 | |
|
|
116 | Scalar::Util::weaken $self; |
120 | Scalar::Util::weaken $self; |
117 | |
121 | |
|
|
122 | my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout}; |
|
|
123 | |
118 | $self->{connect_to} ||= AE::timer |
124 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
119 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
|
|
120 | 0, |
|
|
121 | sub { |
|
|
122 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect"); |
125 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
|
|
126 | }; |
|
|
127 | |
|
|
128 | return unless @addresses; |
|
|
129 | |
|
|
130 | if ($self->{connect_w}) { |
|
|
131 | # sometimes we get told about new addresses after we started to connect |
|
|
132 | unshift @{$self->{connect_addr}}, @addresses; |
|
|
133 | return; |
|
|
134 | } |
|
|
135 | |
|
|
136 | $self->{connect_addr} = \@addresses; # a bit weird, but efficient |
|
|
137 | |
|
|
138 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
139 | |
|
|
140 | my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval}; |
|
|
141 | |
|
|
142 | $interval = ($monitor - $interval) / @addresses |
|
|
143 | if ($monitor - $interval) / @addresses < $interval; |
|
|
144 | |
|
|
145 | $interval = 0.4 if $interval < 0.4; |
|
|
146 | |
|
|
147 | my @endpoints; |
|
|
148 | |
|
|
149 | $self->{connect_w} = AE::timer 0.050 * rand, $interval * (0.9 + 0.1 * rand), sub { |
|
|
150 | @endpoints = @addresses |
|
|
151 | unless @endpoints; |
|
|
152 | |
|
|
153 | my $endpoint = shift @endpoints; |
|
|
154 | |
|
|
155 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
156 | |
|
|
157 | $self->{trial}{$endpoint} ||= do { |
|
|
158 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
159 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
160 | |
|
|
161 | AnyEvent::MP::Transport::mp_connect |
|
|
162 | $host, $port, |
|
|
163 | sub { delete $self->{trial}{$endpoint} }, |
123 | }; |
164 | }; |
124 | |
|
|
125 | unless ($self->{connect_w}) { |
|
|
126 | my @endpoints; |
|
|
127 | my %trial; |
|
|
128 | |
|
|
129 | $self->{connect_w} = AE::timer |
|
|
130 | rand, |
|
|
131 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
132 | sub { |
|
|
133 | @endpoints = split /,/, $self->{noderef} |
|
|
134 | unless @endpoints; |
|
|
135 | |
|
|
136 | my $endpoint = shift @endpoints; |
|
|
137 | |
|
|
138 | $trial{$endpoint} ||= do { |
|
|
139 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
140 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{noderef}: not a resolved node reference."); |
|
|
141 | |
|
|
142 | AnyEvent::MP::Transport::mp_connect |
|
|
143 | $host, $port, |
|
|
144 | sub { delete $trial{$endpoint} } |
|
|
145 | ; |
|
|
146 | }; |
|
|
147 | } |
|
|
148 | ; |
|
|
149 | } |
165 | }; |
150 | } |
166 | } |
151 | |
167 | |
152 | sub kill { |
168 | sub kill { |
153 | my ($self, $port, @reason) = @_; |
169 | my ($self, $port, @reason) = @_; |
154 | |
170 | |
… | |
… | |
191 | |
207 | |
192 | sub connect { |
208 | sub connect { |
193 | # we are trivially connected |
209 | # we are trivially connected |
194 | } |
210 | } |
195 | |
211 | |
|
|
212 | # delay every so often to avoid recursion, also used to delay after spawn |
|
|
213 | our $DELAY = -50; |
|
|
214 | our @DELAY; |
|
|
215 | our $DELAY_W; |
|
|
216 | |
|
|
217 | sub _send_delayed { |
|
|
218 | local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE{""}; |
|
|
219 | (shift @DELAY)->() |
|
|
220 | while @DELAY; |
|
|
221 | undef $DELAY_W; |
|
|
222 | $DELAY = -50; |
|
|
223 | } |
|
|
224 | |
196 | sub transport_reset { |
225 | sub transport_reset { |
197 | my ($self) = @_; |
226 | my ($self) = @_; |
198 | |
227 | |
199 | Scalar::Util::weaken $self; |
228 | Scalar::Util::weaken $self; |
200 | |
229 | |
201 | $self->{send} = sub { |
230 | $self->{send} = sub { |
|
|
231 | if ($DELAY++ >= 0) { |
|
|
232 | my $msg = $_[0]; |
|
|
233 | push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) }; |
|
|
234 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
|
|
235 | return; |
|
|
236 | } |
|
|
237 | |
202 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
238 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
203 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
239 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
204 | }; |
240 | }; |
205 | } |
241 | } |
206 | |
242 | |
|
|
243 | sub transport_connect { |
|
|
244 | my ($self, $tp) = @_; |
|
|
245 | |
|
|
246 | $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
|
|
247 | } |
|
|
248 | |
207 | sub kill { |
249 | sub kill { |
208 | my ($self, $port, @reason) = @_; |
250 | my (undef, @args) = @_; |
209 | |
251 | |
210 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
252 | # we _always_ delay kil's, to avoid calling mon callbacks |
211 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
253 | # from anything but the event loop context. |
212 | |
254 | $DELAY = 1; |
213 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
255 | push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) }; |
214 | or !@reason |
256 | $DELAY_W ||= AE::timer 0, 0, \&_send_delayed; |
215 | or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
|
|
216 | |
|
|
217 | $_->(@reason) for values %$mon; |
|
|
218 | } |
257 | } |
219 | |
258 | |
220 | sub monitor { |
259 | sub monitor { |
221 | my ($self, $portid, $cb) = @_; |
260 | # maybe always delay, too? |
222 | |
261 | if ($DELAY_W) { |
223 | return $cb->(no_such_port => "cannot monitor nonexistent port") |
262 | my @args = @_; |
224 | unless exists $AnyEvent::MP::Kernel::PORT{$portid}; |
263 | push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) }; |
225 | |
264 | return; |
226 | $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0} = $cb; |
265 | } |
|
|
266 | &AnyEvent::MP::Kernel::_monitor; |
227 | } |
267 | } |
228 | |
268 | |
229 | sub unmonitor { |
269 | sub unmonitor { |
230 | my ($self, $portid, $cb) = @_; |
270 | # no need to always delay |
|
|
271 | if ($DELAY_W) { |
|
|
272 | my @args = @_; |
|
|
273 | push @DELAY, sub { AnyEvent::MP::Kernel::_unmonitor (@args) }; |
|
|
274 | return; |
|
|
275 | } |
231 | |
276 | |
232 | delete $AnyEvent::MP::Kernel::LMON{$portid}{$cb+0}; |
277 | &AnyEvent::MP::Kernel::_unmonitor; |
233 | } |
278 | } |
234 | |
279 | |
235 | =head1 SEE ALSO |
280 | =head1 SEE ALSO |
236 | |
281 | |
237 | L<AnyEvent::MP>. |
282 | L<AnyEvent::MP>. |