ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Node.pm
(Generate patch)

Comparing AnyEvent-MP/MP/Node.pm (file contents):
Revision 1.5 by root, Sun Aug 2 14:44:37 2009 UTC vs.
Revision 1.59 by root, Sat Mar 3 11:38:43 2012 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::MP::Node - a single processing node (CPU/process...) 3AnyEvent::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
11This is an internal utility module, horrible to look at, so don't.
12
11=cut 13=cut
12 14
13package AnyEvent::MP::Node; 15package AnyEvent::MP::Node;
14 16
15use common::sense; 17use common::sense;
16 18
17use AE (); 19use AE ();
20use AnyEvent::Util ();
18use AnyEvent::Socket (); 21use AnyEvent::Socket ();
19 22
20use AnyEvent::MP::Transport (); 23use AnyEvent::MP::Transport ();
21 24
22use base Exporter::;
23
24our $VERSION = '0.0';
25
26our $DEFAULT_PORT = "4040";
27
28sub 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
97sub new { 25sub 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
36sub init {
37 #
38}
39
40sub send {
41 &{ shift->{send} }
42}
43
44# nodes reachable via the network
103package AnyEvent::MP::Node::Direct; 45package AnyEvent::MP::Node::Remote;
104 46
105use base "AnyEvent::MP::Node"; 47use base "AnyEvent::MP::Node";
106 48
107sub send { 49# called at init time, mostly sets {send}
50sub 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
120sub set_transport { 65sub 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
89sub 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
124 delete $self->{next_connect}; 97 delete $self->{connect_w};
98 delete $self->{connect_to};
125 99
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; 100 $self->{transport} = $transport;
137 101
102 my $transport_send = $transport->{send};
103
104 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
105
106 $self->{send} = $transport_send;
107
138 $transport->send ($_) 108 $transport_send->($_)
139 for @{ delete $self->{queue} || [] }; 109 for @{ delete $self->{queue} || [] };
140}
141
142sub clr_transport {
143 my ($self) = @_;
144
145 delete $self->{transport};
146
147 $self->connect;
148} 110}
149 111
150sub connect { 112sub connect {
151 my ($self) = @_; 113 my ($self) = @_;
152 114
115 return if $self->{transport};
116 return if $self->{connect_w};
117
153 Scalar::Util::weaken $self; 118 Scalar::Util::weaken $self;
154 119
155 unless (exists $self->{n_noderef}) { 120 $self->{connect_to} ||= AE::timer $AnyEvent::MP::Kernel::CONFIG->{connect_interval}, 0, sub {
156 return if $self->{n_noderef_}++; 121 $self->transport_error (transport_error => $self->{id}, "unable to connect");
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
162 $self->{n_noderef} = $noderef;
163
164 $AnyEvent::MP::Base::NODE{$_} = $self
165 for split /,/, $noderef;
166
167 $self->connect;
168 });
169 return;
170 } 122 };
171 123
172 $self->{retry} ||= [split /,/, $self->{n_noderef}]; 124 # maybe @$addresses?
125 my $addresses = $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}};
173 126
174 my $endpoint = shift @{ $self->{retry} }; 127 if ($addresses) {
128 $self->connect_to ($addresses);
129 } else {
130 # on global nodes, all bets are off now - we either know the node, or we don't
131 unless ($AnyEvent::MP::Kernel::GLOBAL) {
132 AnyEvent::MP::Kernel::g_find ($self->{id});
133 }
134 }
135}
175 136
176 if (defined $endpoint) { 137sub connect_to {
138 my ($self, $addresses) = @_;
139
140 return if $self->{transport};
141 return if $self->{connect_w};
142
143 return unless @$addresses;
144
145 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@$addresses]");
146
147 my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout};
148 my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval};
149
150 $interval = ($monitor - $interval) / @$addresses
151 if ($monitor - $interval) / @$addresses < $interval;
152
153 $interval = 0.4 if $interval < 0.4;
154
155 my @endpoints = reverse @$addresses;
156
157 $self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub {
158 my $endpoint = pop @endpoints;
159
160 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint");
161
177 $self->{trial}{$endpoint} ||= do { 162 $self->{trial}{$endpoint} ||= do {
178 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint 163 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
179 or return; 164 or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference.");
180 165
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 166 AnyEvent::MP::Transport::mp_connect
187 $host, $port, 167 $host, $port,
188 sub {
189 delete $self->{trial}{$endpoint} 168 sub { delete $self->{trial}{$endpoint} },
190 unless @_;
191 $g = shift;
192 };
193 ;
194
195 [$w, \$g]
196 }; 169 };
197 } else {
198 delete $self->{retry};
199 } 170 };
171}
200 172
201 $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub { 173sub connect {
202 $self->connect; 174 my ($self) = @_;
175
176 return if $self->{transport};
177 return if $self->{connect_w};
178
179 Scalar::Util::weaken $self;
180
181 my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout};
182
183 $self->{connect_to} ||= AE::timer $monitor, 0, sub {
184 $self->transport_error (transport_error => $self->{id}, "unable to connect");
185 };
186
187 # maybe @$addresses?
188 my @addresses = @{ $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}} };
189
190 unless (@addresses) {
191 # on global nodes, all bets are off now - we either know the node, or we don't
192 unless ($AnyEvent::MP::Kernel::GLOBAL) {
193 AnyEvent::MP::Kernel::g_find ($self->{id});
194 }
195 return;
203 }; 196 }
197
198 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} with [@addresses]");
199
200 my $interval = $AnyEvent::MP::Kernel::CONFIG->{connect_interval};
201
202 $interval = ($monitor - $interval) / @addresses
203 if ($monitor - $interval) / @addresses < $interval;
204
205 $interval = 0.4 if $interval < 0.4;
206
207 my @endpoints;
208
209 $self->{connect_w} = AE::timer 0, $interval * (0.9 + 0.1 * rand), sub {
210 @endpoints = @addresses
211 unless @endpoints;
212
213 my $endpoint = shift @endpoints;
214
215 $AnyEvent::MP::Kernel::WARN->(9, "connecting to $self->{id} at $endpoint");
216
217 $self->{trial}{$endpoint} ||= do {
218 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
219 or return $AnyEvent::MP::Kernel::WARN->(1, "$self->{id}: not a resolved node reference.");
220
221 AnyEvent::MP::Transport::mp_connect
222 $host, $port,
223 sub { delete $self->{trial}{$endpoint} },
224 };
225 };
226}
227
228sub kill {
229 my ($self, $port, @reason) = @_;
230
231 $self->{send} (["", kil => $port, @reason]);
232}
233
234sub monitor {
235 my ($self, $portid, $cb) = @_;
236
237 my $list = $self->{lmon}{$portid} ||= [];
238
239 $self->send (["", mon1 => $portid])
240 unless @$list || !length $portid;
241
242 push @$list, $cb;
243}
244
245sub unmonitor {
246 my ($self, $portid, $cb) = @_;
247
248 my $list = $self->{lmon}{$portid}
249 or return;
250
251 @$list = grep $_ != $cb, @$list;
252
253 unless (@$list) {
254 $self->send (["", mon0 => $portid]);
255 delete $self->{monitor}{$portid};
256 }
204} 257}
205 258
206package AnyEvent::MP::Node::Self; 259package AnyEvent::MP::Node::Self;
207 260
208use base "AnyEvent::MP::Node"; 261use base "AnyEvent::MP::Node";
209 262
263sub connect {
264 # we are trivially connected
265}
266
267# delay every so often to avoid recursion, also used to delay after spawn
268our $DELAY = -50;
269our @DELAY;
270our $DELAY_W;
271
272sub _send_delayed {
273 local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE{""};
274 (shift @DELAY)->()
275 while @DELAY;
276 undef $DELAY_W;
277 $DELAY = -50;
278}
279
210sub set_transport { 280sub transport_reset {
211 die "FATAL error, set_transport was called"; 281 my ($self) = @_;
212}
213 282
214sub send { 283 Scalar::Util::weaken $self;
284
285 $self->{send} = sub {
286 if ($DELAY++ >= 0) {
287 my $msg = $_[0];
288 push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) };
289 $DELAY_W ||= AE::timer 0, 0, \&_send_delayed;
290 return;
291 }
292
293 local $AnyEvent::MP::Kernel::SRCNODE = $self;
215 AnyEvent::MP::Base::_inject ($_[1]); 294 AnyEvent::MP::Kernel::_inject (@{ $_[0] });
295 };
296}
297
298sub transport_connect {
299 my ($self, $tp) = @_;
300
301 $AnyEvent::MP::Kernel::WARN->(9, "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})");
302}
303
304sub kill {
305 my (undef, @args) = @_;
306
307 # we _always_ delay kil's, to avoid calling mon callbacks
308 # from anything but the event loop context.
309 $DELAY = 1;
310 push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) };
311 $DELAY_W ||= AE::timer 0, 0, \&_send_delayed;
312}
313
314sub monitor {
315 # maybe always delay, too?
316 if ($DELAY_W) {
317 my @args = @_;
318 push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) };
319 return;
320 }
321 &AnyEvent::MP::Kernel::_monitor;
322}
323
324sub unmonitor {
325 # no need to always delay
326 if ($DELAY_W) {
327 my @args = @_;
328 push @DELAY, sub { AnyEvent::MP::Kernel::_unmonitor (@args) };
329 return;
330 }
331
332 &AnyEvent::MP::Kernel::_unmonitor;
216} 333}
217 334
218=head1 SEE ALSO 335=head1 SEE ALSO
219 336
220L<AnyEvent>. 337L<AnyEvent::MP>.
221 338
222=head1 AUTHOR 339=head1 AUTHOR
223 340
224 Marc Lehmann <schmorp@schmorp.de> 341 Marc Lehmann <schmorp@schmorp.de>
225 http://home.schmorp.de/ 342 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines