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 | |
… | |
… | |
56 | push @{$self->{queue}}, shift; |
58 | push @{$self->{queue}}, shift; |
57 | $self->connect; |
59 | $self->connect; |
58 | }; |
60 | }; |
59 | } |
61 | } |
60 | |
62 | |
61 | # called only after successful handshake |
63 | # called each time we fail to establish a connection, |
|
|
64 | # or the existing connection failed |
62 | sub transport_error { |
65 | sub transport_error { |
63 | my ($self, @reason) = @_; |
66 | my ($self, @reason) = @_; |
64 | |
67 | |
65 | my $no_transport = !$self->{transport}; |
68 | my $no_transport = !$self->{transport}; |
66 | |
69 | |
… | |
… | |
74 | $_->(@reason) for map @$_, values %$mon; |
77 | $_->(@reason) for map @$_, values %$mon; |
75 | } |
78 | } |
76 | |
79 | |
77 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
80 | AnyEvent::MP::Kernel::_inject_nodeevent ($self, 0, @reason) |
78 | unless $no_transport; |
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}; |
79 | } |
86 | } |
80 | |
87 | |
81 | # called after handshake was successful |
88 | # called after handshake was successful |
82 | sub transport_connect { |
89 | sub transport_connect { |
83 | my ($self, $transport) = @_; |
90 | my ($self, $transport) = @_; |
… | |
… | |
109 | |
116 | |
110 | return if $self->{transport}; |
117 | return if $self->{transport}; |
111 | |
118 | |
112 | Scalar::Util::weaken $self; |
119 | Scalar::Util::weaken $self; |
113 | |
120 | |
114 | $self->{connect_to} ||= AE::timer |
|
|
115 | $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; |
116 | 0, |
122 | |
117 | sub { |
123 | $self->{connect_to} ||= AE::timer $monitor, 0, sub { |
118 | $self->transport_error (transport_error => $self->{id}, "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} }, |
119 | }; |
156 | }; |
120 | |
|
|
121 | return unless @addresses; |
|
|
122 | |
|
|
123 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]"); |
|
|
124 | |
|
|
125 | unless ($self->{connect_w}) { |
|
|
126 | my @endpoints; |
|
|
127 | |
|
|
128 | $self->{connect_w} = AE::timer |
|
|
129 | rand, |
|
|
130 | $AnyEvent::MP::Config::CFG{connect_interval} || $AnyEvent::MP::Kernel::CONNECT_INTERVAL, |
|
|
131 | sub { |
|
|
132 | @endpoints = @addresses |
|
|
133 | unless @endpoints; |
|
|
134 | |
|
|
135 | my $endpoint = shift @endpoints; |
|
|
136 | |
|
|
137 | $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint"); |
|
|
138 | |
|
|
139 | $self->{trial}{$endpoint} ||= do { |
|
|
140 | my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint |
|
|
141 | or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference."); |
|
|
142 | |
|
|
143 | AnyEvent::MP::Transport::mp_connect |
|
|
144 | $host, $port, |
|
|
145 | sub { delete $self->{trial}{$endpoint} }, |
|
|
146 | }; |
|
|
147 | } |
|
|
148 | ; |
|
|
149 | } |
157 | }; |
150 | } |
158 | } |
151 | |
159 | |
152 | sub kill { |
160 | sub kill { |
153 | my ($self, $port, @reason) = @_; |
161 | my ($self, $port, @reason) = @_; |
154 | |
162 | |
… | |
… | |
216 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
224 | delete $AnyEvent::MP::Kernel::PORT{$port}; |
217 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
225 | delete $AnyEvent::MP::Kernel::PORT_DATA{$port}; |
218 | |
226 | |
219 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
227 | my $mon = delete $AnyEvent::MP::Kernel::LMON{$port} |
220 | or !@reason |
228 | or !@reason |
221 | or $AnyEvent::MP::Kernel::WARN->(2, "unmonitored local port $port died with reason: @reason"); |
229 | or $AnyEvent::MP::Kernel::WARN->(8, "unmonitored local port $port died with reason: @reason"); |
222 | |
230 | |
223 | $_->(@reason) for values %$mon; |
231 | $_->(@reason) for values %$mon; |
224 | } |
232 | } |
225 | |
233 | |
226 | sub monitor { |
234 | sub monitor { |