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 | |
|
|
30 | $self->init; |
28 | $self->transport_reset; |
31 | $self->transport_reset; |
29 | |
32 | |
30 | $self |
33 | $self |
|
|
34 | } |
|
|
35 | |
|
|
36 | sub init { |
|
|
37 | # |
31 | } |
38 | } |
32 | |
39 | |
33 | sub send { |
40 | sub send { |
34 | &{ shift->{send} } |
41 | &{ shift->{send} } |
35 | } |
42 | } |
36 | |
43 | |
|
|
44 | # nodes reachable via the network |
37 | package AnyEvent::MP::Node::External; |
45 | package AnyEvent::MP::Node::External; |
38 | |
46 | |
39 | use base "AnyEvent::MP::Node"; |
47 | use base "AnyEvent::MP::Node"; |
40 | |
48 | |
41 | # called at init time, mostly sets {send} |
49 | # called at init time, mostly sets {send} |
… | |
… | |
50 | push @{$self->{queue}}, shift; |
58 | push @{$self->{queue}}, shift; |
51 | $self->connect; |
59 | $self->connect; |
52 | }; |
60 | }; |
53 | } |
61 | } |
54 | |
62 | |
55 | # called only after successful handshake |
63 | # called each time we fail to establish a connection, |
|
|
64 | # or the existing connection failed |
56 | sub transport_error { |
65 | sub transport_error { |
57 | my ($self, @reason) = @_; |
66 | my ($self, @reason) = @_; |
58 | |
67 | |
59 | my $no_transport = !$self->{transport}; |
68 | my $no_transport = !$self->{transport}; |
60 | |
69 | |
61 | delete $self->{connect_w}; |
70 | delete $self->{connect_w}; |
62 | delete $self->{connect_to}; |
71 | delete $self->{connect_to}; |
63 | |
72 | |
64 | delete $self->{queue}; |
73 | delete $self->{queue}; |
65 | $self->transport_reset; |
74 | $self->transport_reset; |
66 | |
|
|
67 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
|
|
68 | unless $no_transport; |
|
|
69 | |
75 | |
70 | if (my $mon = delete $self->{lmon}) { |
76 | if (my $mon = delete $self->{lmon}) { |
71 | $_->(@reason) for map @$_, values %$mon; |
77 | $_->(@reason) for map @$_, values %$mon; |
72 | } |
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}; |
73 | } |
86 | } |
74 | |
87 | |
75 | # called after handshake was successful |
88 | # called after handshake was successful |
76 | sub transport_connect { |
89 | sub transport_connect { |
77 | my ($self, $transport) = @_; |
90 | my ($self, $transport) = @_; |
78 | |
91 | |
|
|
92 | delete $self->{trial}; |
|
|
93 | |
79 | $self->transport_error (transport_error => "switched connections") |
94 | $self->transport_error (transport_error => "switched connections") |
80 | if $self->{transport}; |
95 | if $self->{transport}; |
81 | |
96 | |
82 | delete $self->{connect_w}; |
97 | delete $self->{connect_w}; |
83 | delete $self->{connect_to}; |
98 | delete $self->{connect_to}; |
… | |
… | |
95 | $transport->send ($_) |
110 | $transport->send ($_) |
96 | for @{ delete $self->{queue} || [] }; |
111 | for @{ delete $self->{queue} || [] }; |
97 | } |
112 | } |
98 | |
113 | |
99 | sub connect { |
114 | sub connect { |
100 | my ($self) = @_; |
115 | my ($self, @addresses) = @_; |
|
|
116 | |
|
|
117 | return if $self->{transport}; |
101 | |
118 | |
102 | Scalar::Util::weaken $self; |
119 | Scalar::Util::weaken $self; |
103 | |
120 | |
104 | $self->{connect_to} ||= AE::timer |
|
|
105 | $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT, |
121 | my $monitor = $AnyEvent::MP::Config::CFG{monitor_timeout} || $AnyEvent::MP::Kernel::MONITOR_TIMEOUT; |
106 | 0, |
122 | |
107 | sub { |
123 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
108 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect"); |
124 | $self->transport_error (transport_error => $self->{id}, "unable to connect"); |
|
|
125 | }; |
|
|
126 | |
|
|
127 | return unless @addresses; |
|
|
128 | return if $self->{connect_w}; |
|
|
129 | |
|
|
130 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
131 | |
|
|
132 | my $interval = $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL; |
|
|
133 | |
|
|
134 | $interval = ($monitor - $interval) / @addresses |
|
|
135 | if ($monitor - $interval) / @addresses < $interval; |
|
|
136 | |
|
|
137 | $interval = 0.4 if $interval < 0.4; |
|
|
138 | |
|
|
139 | my @endpoints; |
|
|
140 | |
|
|
141 | $self->{connect_w} = AE::timer 0.050 * rand, $interval * (0.9 + 0.1 * rand), sub { |
|
|
142 | @endpoints = @addresses |
|
|
143 | unless @endpoints; |
|
|
144 | |
|
|
145 | my $endpoint = shift @endpoints; |
|
|
146 | |
|
|
147 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
148 | |
|
|
149 | $self->{trial}{$endpoint} ||= do { |
|
|
150 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
151 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
152 | |
|
|
153 | AnyEvent::MP::Transport::mp_connect |
|
|
154 | $host, $port, |
|
|
155 | sub { delete $self->{trial}{$endpoint} }, |
109 | }; |
156 | }; |
110 | |
|
|
111 | unless ($self->{connect_w}) { |
|
|
112 | my @endpoints; |
|
|
113 | my %trial; |
|
|
114 | |
|
|
115 | $self->{connect_w} = AE::timer |
|
|
116 | 0, |
|
|
117 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
118 | sub { |
|
|
119 | @endpoints = split /,/, $self->{noderef} |
|
|
120 | unless @endpoints; |
|
|
121 | |
|
|
122 | my $endpoint = shift @endpoints; |
|
|
123 | |
|
|
124 | $trial{$endpoint} ||= do { |
|
|
125 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
126 | or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference."); |
|
|
127 | |
|
|
128 | AnyEvent::MP::Transport::mp_connect |
|
|
129 | $host, $port, |
|
|
130 | sub { delete $trial{$endpoint} } |
|
|
131 | ; |
|
|
132 | }; |
|
|
133 | } |
|
|
134 | ; |
|
|
135 | } |
157 | }; |
136 | } |
158 | } |
137 | |
159 | |
138 | sub kill { |
160 | sub kill { |
139 | my ($self, $port, @reason) = @_; |
161 | my ($self, $port, @reason) = @_; |
140 | |
162 | |
… | |
… | |
145 | my ($self, $portid, $cb) = @_; |
167 | my ($self, $portid, $cb) = @_; |
146 | |
168 | |
147 | my $list = $self->{lmon}{$portid} ||= []; |
169 | my $list = $self->{lmon}{$portid} ||= []; |
148 | |
170 | |
149 | $self->send (["", mon1 => $portid]) |
171 | $self->send (["", mon1 => $portid]) |
150 | unless @$list; |
172 | unless @$list || !length $portid; |
151 | |
173 | |
152 | push @$list, $cb; |
174 | push @$list, $cb; |
153 | } |
175 | } |
154 | |
176 | |
155 | sub unmonitor { |
177 | sub unmonitor { |
… | |
… | |
164 | $self->send (["", mon0 => $portid]); |
186 | $self->send (["", mon0 => $portid]); |
165 | delete $self->{monitor}{$portid}; |
187 | delete $self->{monitor}{$portid}; |
166 | } |
188 | } |
167 | } |
189 | } |
168 | |
190 | |
|
|
191 | # used for direct slave connections as well |
169 | package AnyEvent::MP::Node::Direct; |
192 | package AnyEvent::MP::Node::Direct; |
170 | |
193 | |
171 | use base "AnyEvent::MP::Node::External"; |
194 | use base "AnyEvent::MP::Node::External"; |
172 | |
|
|
173 | package AnyEvent::MP::Node::Indirect; |
|
|
174 | |
|
|
175 | use base "AnyEvent::MP::Node::Direct"; |
|
|
176 | |
|
|
177 | sub transport_reset { |
|
|
178 | my ($self) = @_; |
|
|
179 | |
|
|
180 | # as an optimisation, immediately nuke slave nodes |
|
|
181 | delete $AnyEvent::MP::Kernel::NODE{$self->{noderef}} |
|
|
182 | if $self->{transport}; |
|
|
183 | |
|
|
184 | $self->SUPER::transport_reset; |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub connect { |
|
|
188 | my ($self) = @_; |
|
|
189 | |
|
|
190 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node"); |
|
|
191 | } |
|
|
192 | |
195 | |
193 | package AnyEvent::MP::Node::Self; |
196 | package AnyEvent::MP::Node::Self; |
194 | |
197 | |
195 | use base "AnyEvent::MP::Node"; |
198 | use base "AnyEvent::MP::Node"; |
196 | |
199 | |
… | |
… | |
207 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
210 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
208 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
211 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
209 | }; |
212 | }; |
210 | } |
213 | } |
211 | |
214 | |
|
|
215 | sub transport_connect { |
|
|
216 | my ($self, $tp) = @_; |
|
|
217 | |
|
|
218 | $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})"); |
|
|
219 | } |
|
|
220 | |
212 | sub kill { |
221 | sub kill { |
213 | my ($self, $port, @reason) = @_; |
222 | my ($self, $port, @reason) = @_; |
214 | |
223 | |
215 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
224 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
216 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
225 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
217 | |
226 | |
218 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
227 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
219 | or !@reason |
228 | or !@reason |
220 | or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason"); |
229 | or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
221 | |
230 | |
222 | $_->(@reason) for values %$mon; |
231 | $_->(@reason) for values %$mon; |
223 | } |
232 | } |
224 | |
233 | |
225 | sub monitor { |
234 | sub monitor { |