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.6 by root, Mon Aug 3 21:35:03 2009 UTC vs.
Revision 1.50 by root, Wed Dec 30 15:49:05 2009 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
11This is an internal utility module, horrible to look at, so don't.
10 12
11=cut 13=cut
12 14
13package AnyEvent::MP::Node; 15package AnyEvent::MP::Node;
14 16
18use AnyEvent::Util (); 20use AnyEvent::Util ();
19use AnyEvent::Socket (); 21use AnyEvent::Socket ();
20 22
21use AnyEvent::MP::Transport (); 23use AnyEvent::MP::Transport ();
22 24
23use base Exporter::;
24
25our $VERSION = '0.0';
26
27our $DEFAULT_PORT = "4040";
28
29sub normalise_noderef($) {
30 my ($noderef) = @_;
31
32 my $cv = AE::cv;
33 my @res;
34
35 $cv->begin (sub {
36 my %seen;
37 my @refs;
38 for (sort { $a->[0] <=> $b->[0] } @res) {
39 push @refs, $_->[1] unless $seen{$_->[1]}++
40 }
41 shift->send (join ",", @refs);
42 });
43
44 $noderef = $DEFAULT_PORT unless length $noderef;
45
46 my $idx;
47 for my $t (split /,/, $noderef) {
48 my $pri = ++$idx;
49
50 #TODO: this should be outside normalise_noderef and in become_public
51 if ($t =~ /^\d*$/) {
52 require POSIX;
53 my $nodename = (POSIX::uname ())[1];
54
55 $cv->begin;
56 AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub {
57 for (@_) {
58 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
59 push @res, [
60 $pri += 1e-5,
61 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
62 ];
63 }
64 $cv->end;
65 };
66
67# my (undef, undef, undef, undef, @ipv4) = gethostbyname $nodename;
68#
69# for (@ipv4) {
70# push @res, [
71# $pri,
72# AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $_, $t || $DEFAULT_PORT,
73# ];
74# }
75 } else {
76 my ($host, $port) = AnyEvent::Socket::parse_hostport $t, "aemp=$DEFAULT_PORT"
77 or Carp::croak "$t: unparsable transport descriptor";
78
79 $cv->begin;
80 AnyEvent::Socket::resolve_sockaddr $host, $port, "tcp", 0, undef, sub {
81 for (@_) {
82 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
83 push @res, [
84 $pri += 1e-5,
85 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
86 ];
87 }
88 $cv->end;
89 }
90 }
91 }
92
93 $cv->end;
94
95 $cv
96}
97
98sub new { 25sub new {
99 my ($class, $noderef) = @_; 26 my ($self, $id) = @_;
100 27
101 bless { noderef => $noderef }, $class 28 $self = bless { id => $id }, $self;
102}
103 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
104package AnyEvent::MP::Node::Direct; 45package AnyEvent::MP::Node::External;
105 46
106use base "AnyEvent::MP::Node"; 47use base "AnyEvent::MP::Node";
107 48
108sub send { 49# called at init time, mostly sets {send}
50sub transport_reset {
109 my ($self, $msg) = @_; 51 my ($self) = @_;
110 52
111 if ($self->{transport}) { 53 delete $self->{transport};
112 $self->{transport}->send ($msg); 54
113 } elsif ($self->{queue}) { 55 Scalar::Util::weaken $self;
56
57 $self->{send} = sub {
114 push @{ $self->{queue} }, $msg; 58 push @{$self->{queue}}, shift;
115 } else {
116 $self->{queue} = [$msg];
117 $self->connect; 59 $self->connect;
118 } 60 };
61}
62
63# called each time we fail to establish a connection,
64# or the existing connection failed
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 {
90 my ($self, $transport) = @_;
91
92 delete $self->{trial};
93
94 $self->transport_error (transport_error => "switched connections")
95 if $self->{transport};
96
97 delete $self->{connect_addr};
98 delete $self->{connect_w};
99 delete $self->{connect_to};
100
101 $self->{transport} = $transport;
102
103 my $transport_send = $transport->can ("send");
104
105 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
106
107 $self->{send} = sub {
108 $transport_send->($transport, $_[0]);
109 };
110
111 $transport->send ($_)
112 for @{ delete $self->{queue} || [] };
113}
114
115sub connect {
116 my ($self, @addresses) = @_;
117
118 return if $self->{transport};
119
120 Scalar::Util::weaken $self;
121
122 my $monitor = $AnyEvent::MP::Kernel::CONFIG->{monitor_timeout};
123
124 $self->{connect_to} ||= AE::timer $monitor, 0, sub {
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} },
164 };
165 };
166}
167
168sub kill {
169 my ($self, $port, @reason) = @_;
170
171 $self->send (["", kil => $port, @reason]);
119} 172}
120 173
121sub monitor { 174sub monitor {
122 my ($self, $portid, $cb) = @_; 175 my ($self, $portid, $cb) = @_;
123 176
124 return $cb->()
125 if $self->{failed};
126
127 my $list = $self->{lmon}{$portid} ||= []; 177 my $list = $self->{lmon}{$portid} ||= [];
128 178
129 $self->send (["", mon1 => $portid]) 179 $self->send (["", mon1 => $portid])
130 unless @$list; 180 unless @$list || !length $portid;
131 181
132 push @$list, $cb; 182 push @$list, $cb;
133} 183}
134 184
135sub unmonitor { 185sub unmonitor {
144 $self->send (["", mon0 => $portid]); 194 $self->send (["", mon0 => $portid]);
145 delete $self->{monitor}{$portid}; 195 delete $self->{monitor}{$portid};
146 } 196 }
147} 197}
148 198
199# used for direct slave connections as well
200package AnyEvent::MP::Node::Direct;
201
202use base "AnyEvent::MP::Node::External";
203
204package AnyEvent::MP::Node::Self;
205
206use base "AnyEvent::MP::Node";
207
208sub connect {
209 # we are trivially connected
210}
211
212# delay every so often to avoid recursion, also used to delay after spawn
213our $DELAY = -50;
214our @DELAY;
215our $DELAY_W;
216
217sub _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
149sub set_transport { 225sub transport_reset {
150 my ($self, $transport) = @_;
151
152 $self->clr_transport
153 if $self->{transport};
154
155 delete $self->{trial};
156 delete $self->{next_connect};
157 delete $self->{failed};
158
159 if (
160 exists $self->{remote_uniq}
161 && $self->{remote_uniq} ne $transport->{remote_uniq}
162 ) {
163 # uniq changed, drop queue
164 delete $self->{queue};
165 #TODO: "DOWN"
166 }
167
168 $self->{remote_uniq} = $transport->{remote_uniq};
169 $self->{transport} = $transport;
170
171 $transport->send ($_)
172 for @{ delete $self->{queue} || [] };
173}
174
175sub clr_transport {
176 my ($self) = @_; 226 my ($self) = @_;
177 227
178 delete $self->{transport};
179 $self->{failed} = 1;
180
181 if (my $mon = delete $self->{monitor}) {
182 $_->() for map @$_, values %$mon;
183 }
184
185 $self->connect;
186}
187
188sub connect {
189 my ($self) = @_;
190
191 Scalar::Util::weaken $self; 228 Scalar::Util::weaken $self;
192 229
193 unless (exists $self->{n_noderef}) { 230 $self->{send} = sub {
194 return if $self->{n_noderef_}++; 231 if ($DELAY++ >= 0) {
195 (AnyEvent::MP::Node::normalise_noderef ($self->{noderef}))->cb (sub { 232 my $msg = $_[0];
233 push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) };
234 $DELAY_W ||= AE::timer 0, 0, \&_send_delayed;
196 $self or return; 235 return;
197 delete $self->{n_noderef_};
198 my $noderef = shift->recv;
199
200 $self->{n_noderef} = $noderef;
201
202 $AnyEvent::MP::Base::NODE{$_} = $self
203 for split /,/, $noderef;
204
205 $self->connect;
206 }); 236 }
237
238 local $AnyEvent::MP::Kernel::SRCNODE = $self;
239 AnyEvent::MP::Kernel::_inject (@{ $_[0] });
240 };
241}
242
243sub 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
249sub kill {
250 my (undef, @args) = @_;
251
252 # we _always_ delay kil's, to avoid calling mon callbacks
253 # from anything but the event loop context.
254 $DELAY = 1;
255 push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) };
256 $DELAY_W ||= AE::timer 0, 0, \&_send_delayed;
257}
258
259sub monitor {
260 # maybe always delay, too?
261 if ($DELAY_W) {
262 my @args = @_;
263 push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) };
207 return; 264 return;
208 } 265 }
209 266 &AnyEvent::MP::Kernel::_monitor;
210 $self->{retry} ||= [split /,/, $self->{n_noderef}];
211
212 my $endpoint = shift @{ $self->{retry} };
213
214 if (defined $endpoint) {
215 $self->{trial}{$endpoint} ||= do {
216 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
217 or return;
218
219 my ($w, $g);
220
221 $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub {
222 delete $self->{trial}{$endpoint};
223 };
224 $g = AnyEvent::MP::Transport::mp_connect
225 $host, $port,
226 sub {
227 delete $self->{trial}{$endpoint}
228 unless @_;
229 $g = shift;
230 };
231 ;
232
233 [$w, \$g]
234 };
235 } else {
236 delete $self->{retry};
237 }
238
239 $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub {
240 $self->connect;
241 };
242}
243
244package AnyEvent::MP::Node::Self;
245
246use base "AnyEvent::MP::Node";
247
248sub set_transport {
249 die "FATAL error, set_transport was called";
250}
251
252sub send {
253 local $AnyEvent::MP::Base::SRCNODE = $_[0];
254 AnyEvent::MP::Base::_inject (@{ $_[1] });
255}
256
257sub monitor {
258 my ($self, $portid, $cb) = @_;
259
260 return $cb->()
261 unless exists $AnyEvent::MP::Base::PORT{$portid};
262
263 $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb;
264} 267}
265 268
266sub unmonitor { 269sub unmonitor {
267 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 }
268 276
269 delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0}; 277 &AnyEvent::MP::Kernel::_unmonitor;
270} 278}
271 279
272=head1 SEE ALSO 280=head1 SEE ALSO
273 281
274L<AnyEvent>. 282L<AnyEvent::MP>.
275 283
276=head1 AUTHOR 284=head1 AUTHOR
277 285
278 Marc Lehmann <schmorp@schmorp.de> 286 Marc Lehmann <schmorp@schmorp.de>
279 http://home.schmorp.de/ 287 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines