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) = @_; |
|
|
67 | |
|
|
68 | my $no_transport = !$self->{transport}; |
|
|
69 | |
|
|
70 | delete $self->{connect_w}; |
|
|
71 | delete $self->{connect_to}; |
58 | |
72 | |
59 | delete $self->{queue}; |
73 | delete $self->{queue}; |
60 | $self->transport_reset; |
74 | $self->transport_reset; |
61 | |
75 | |
62 | if (my $mon = delete $self->{lmon}) { |
76 | if (my $mon = delete $self->{lmon}) { |
63 | $_->(@reason) for map @$_, values %$mon; |
77 | $_->(@reason) for map @$_, values %$mon; |
64 | } |
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}; |
65 | } |
86 | } |
66 | |
87 | |
67 | # called after handshake was successful |
88 | # called after handshake was successful |
68 | sub transport_connect { |
89 | sub transport_connect { |
69 | my ($self, $transport) = @_; |
90 | my ($self, $transport) = @_; |
70 | |
91 | |
71 | $self->transport_reset |
92 | delete $self->{trial}; |
|
|
93 | |
|
|
94 | $self->transport_error (transport_error => "switched connections") |
72 | if $self->{transport}; |
95 | if $self->{transport}; |
73 | |
96 | |
74 | delete $self->{connect_w}; |
97 | delete $self->{connect_w}; |
75 | delete $self->{connect_to}; |
98 | delete $self->{connect_to}; |
76 | |
99 | |
… | |
… | |
80 | |
103 | |
81 | $self->{send} = sub { |
104 | $self->{send} = sub { |
82 | $transport_send->($transport, $_[0]); |
105 | $transport_send->($transport, $_[0]); |
83 | }; |
106 | }; |
84 | |
107 | |
|
|
108 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1); |
|
|
109 | |
85 | $transport->send ($_) |
110 | $transport->send ($_) |
86 | for @{ delete $self->{queue} || [] }; |
111 | for @{ delete $self->{queue} || [] }; |
87 | } |
112 | } |
88 | |
113 | |
89 | sub connect { |
114 | sub connect { |
90 | my ($self) = @_; |
115 | my ($self, @addresses) = @_; |
|
|
116 | |
|
|
117 | return if $self->{transport}; |
91 | |
118 | |
92 | Scalar::Util::weaken $self; |
119 | Scalar::Util::weaken $self; |
93 | |
120 | |
94 | $self->{connect_to} ||= AE::timer |
|
|
95 | $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; |
96 | 0, |
122 | |
97 | sub { |
123 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
98 | $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} }, |
99 | }; |
156 | }; |
100 | |
|
|
101 | unless ($self->{connect_w}) { |
|
|
102 | my @endpoints; |
|
|
103 | my %trial; |
|
|
104 | |
|
|
105 | $self->{connect_w} = AE::timer |
|
|
106 | 0, |
|
|
107 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
108 | sub { |
|
|
109 | @endpoints = split /,/, $self->{noderef} |
|
|
110 | unless @endpoints; |
|
|
111 | |
|
|
112 | my $endpoint = shift @endpoints; |
|
|
113 | |
|
|
114 | $trial{$endpoint} ||= do { |
|
|
115 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
116 | or return $AnyEvent::MP::Kernel::WARN->("$self->{noderef}: not a resolved node reference."); |
|
|
117 | |
|
|
118 | AnyEvent::MP::Transport::mp_connect |
|
|
119 | $host, $port, |
|
|
120 | sub { delete $trial{$endpoint} } |
|
|
121 | ; |
|
|
122 | }; |
|
|
123 | } |
|
|
124 | ; |
|
|
125 | } |
157 | }; |
126 | } |
158 | } |
127 | |
159 | |
128 | sub kill { |
160 | sub kill { |
129 | my ($self, $port, @reason) = @_; |
161 | my ($self, $port, @reason) = @_; |
130 | |
162 | |
… | |
… | |
135 | my ($self, $portid, $cb) = @_; |
167 | my ($self, $portid, $cb) = @_; |
136 | |
168 | |
137 | my $list = $self->{lmon}{$portid} ||= []; |
169 | my $list = $self->{lmon}{$portid} ||= []; |
138 | |
170 | |
139 | $self->send (["", mon1 => $portid]) |
171 | $self->send (["", mon1 => $portid]) |
140 | unless @$list; |
172 | unless @$list || !length $portid; |
141 | |
173 | |
142 | push @$list, $cb; |
174 | push @$list, $cb; |
143 | } |
175 | } |
144 | |
176 | |
145 | sub unmonitor { |
177 | sub unmonitor { |
… | |
… | |
154 | $self->send (["", mon0 => $portid]); |
186 | $self->send (["", mon0 => $portid]); |
155 | delete $self->{monitor}{$portid}; |
187 | delete $self->{monitor}{$portid}; |
156 | } |
188 | } |
157 | } |
189 | } |
158 | |
190 | |
|
|
191 | # used for direct slave connections as well |
159 | package AnyEvent::MP::Node::Direct; |
192 | package AnyEvent::MP::Node::Direct; |
160 | |
193 | |
161 | use base "AnyEvent::MP::Node::External"; |
194 | use base "AnyEvent::MP::Node::External"; |
162 | |
195 | |
163 | package AnyEvent::MP::Node::Indirect; |
196 | package AnyEvent::MP::Node::Self; |
164 | |
197 | |
165 | use base "AnyEvent::MP::Node::Direct"; |
198 | use base "AnyEvent::MP::Node"; |
166 | |
199 | |
167 | sub connect { |
200 | sub connect { |
168 | my ($self) = @_; |
201 | # we are trivially connected |
169 | |
|
|
170 | $self->transport_error (transport_error => $self->{noderef}, "unable to connect to indirect node"); |
|
|
171 | } |
202 | } |
172 | |
|
|
173 | package AnyEvent::MP::Node::Self; |
|
|
174 | |
|
|
175 | use base "AnyEvent::MP::Node"; |
|
|
176 | |
203 | |
177 | sub transport_reset { |
204 | sub transport_reset { |
178 | my ($self) = @_; |
205 | my ($self) = @_; |
179 | |
206 | |
180 | Scalar::Util::weaken $self; |
207 | Scalar::Util::weaken $self; |
… | |
… | |
183 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
210 | local $AnyEvent::MP::Kernel::SRCNODE = $self; |
184 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
211 | AnyEvent::MP::Kernel::_inject (@{ $_[0] }); |
185 | }; |
212 | }; |
186 | } |
213 | } |
187 | |
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 | |
188 | sub kill { |
221 | sub kill { |
189 | my ($self, $port, @reason) = @_; |
222 | my ($self, $port, @reason) = @_; |
190 | |
223 | |
191 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
224 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
192 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
225 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
193 | |
226 | |
194 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
227 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
195 | or !@reason |
228 | or !@reason |
196 | or $AnyEvent::MP::Kernel::WARN->("unmonitored local port $port died with reason: @reason"); |
229 | or $AnyEvent::MP::Kernel::WARN->(8, "unmonitored local port $port died with reason: @reason"); |
197 | |
230 | |
198 | $_->(@reason) for values %$mon; |
231 | $_->(@reason) for values %$mon; |
199 | } |
232 | } |
200 | |
233 | |
201 | sub monitor { |
234 | sub monitor { |