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.15 by root, Sat Aug 8 21:56:29 2009 UTC vs.
Revision 1.62 by root, Thu Mar 22 00:48:29 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
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
27sub new { 25sub new {
28 my ($class, $noderef) = @_; 26 my ($self, $id) = @_;
29 27
30 bless { noderef => $noderef }, $class 28 $self = bless { id => $id }, $self;
31}
32 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
33package AnyEvent::MP::Node::Direct; 45package AnyEvent::MP::Node::Remote;
34 46
35use base "AnyEvent::MP::Node"; 47use base "AnyEvent::MP::Node";
36 48
37sub send { 49# called at init time, mostly sets {send}
50sub transport_reset {
38 my ($self, $msg) = @_; 51 my ($self) = @_;
39 52
40 if ($self->{transport}) { 53 delete $self->{transport};
41 $self->{transport}->send ($msg); 54
42 } elsif ($self->{queue}) { 55 Scalar::Util::weaken $self;
56
57 $self->{send} = sub {
43 push @{ $self->{queue} }, $msg; 58 push @{$self->{queue}}, shift;
59 $self->connect;
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 => $self->{id}, "switched connections")
95 if $self->{transport};
96
97 delete $self->{connect_w};
98 delete $self->{connect_to};
99
100 $self->{transport} = $transport;
101
102 my $transport_send = $transport->{send};
103
104 AnyEvent::MP::Kernel::_inject_nodeevent ($self, 1);
105
106 $self->{send} = $transport_send;
107
108 $transport_send->($_)
109 for @{ delete $self->{queue} || [] };
110}
111
112sub connect {
113 my ($self) = @_;
114
115 return if $self->{transport};
116 return if $self->{connect_w};
117
118 Scalar::Util::weaken $self;
119
120 $self->{connect_to} ||= AE::timer $AnyEvent::MP::Kernel::CONFIG->{connect_interval}, 0, sub {
121 $self->transport_error (transport_error => $self->{id}, "unable to connect");
122 };
123
124 # maybe @$addresses?
125 my $addresses = $AnyEvent::MP::Kernel::GLOBAL_DB{"'l"}{$self->{id}};
126
127 if ($addresses) {
128 $self->connect_to ($addresses);
44 } else { 129 } else {
45 $self->{queue} = [$msg]; 130 # on global nodes, all bets are off now - we either know the node, or we don't
46 $self->connect; 131 unless ($AnyEvent::MP::Kernel::GLOBAL) {
132 AnyEvent::MP::Kernel::g_find ($self->{id});
133 }
134 }
135}
136
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 AE::log 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 AE::log 9 => "connecting to $self->{id} at $endpoint";
161
162 $self->{trial}{$endpoint} ||= do {
163 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
164 or return AE::log critical => "$self->{id}: '$endpoint' is not a resolved node reference.";
165
166 AnyEvent::MP::Transport::mp_connect
167 $host, $port,
168 sub { delete $self->{trial}{$endpoint} },
169 };
47 } 170 };
48} 171}
49 172
50sub kill { 173sub kill {
51 my ($self, $port, @reason) = @_; 174 my ($self, $port, @reason) = @_;
52 175
53 $self->send (["", kil => $port, @reason]); 176 $self->{send} (["", kil => $port, @reason]);
54} 177}
55 178
56sub monitor { 179sub monitor {
57 my ($self, $portid, $cb) = @_; 180 my ($self, $portid, $cb) = @_;
58 181
59 my $list = $self->{lmon}{$portid} ||= []; 182 my $list = $self->{lmon}{$portid} ||= [];
60 183
61 $self->send (["", mon1 => $portid]) 184 $self->send (["", mon1 => $portid])
62 unless @$list; 185 unless @$list || !length $portid;
63 186
64 push @$list, $cb; 187 push @$list, $cb;
65} 188}
66 189
67sub unmonitor { 190sub unmonitor {
76 $self->send (["", mon0 => $portid]); 199 $self->send (["", mon0 => $portid]);
77 delete $self->{monitor}{$portid}; 200 delete $self->{monitor}{$portid};
78 } 201 }
79} 202}
80 203
81sub set_transport { 204package AnyEvent::MP::Node::Self;
82 my ($self, $transport) = @_;
83 205
84 $self->clr_transport 206use base "AnyEvent::MP::Node";
85 if $self->{transport};
86
87 delete $self->{trial};
88 delete $self->{next_connect};
89
90 $self->{transport} = $transport;
91
92 $transport->send ($_)
93 for @{ delete $self->{queue} || [] };
94}
95
96sub fail {
97 my ($self, @reason) = @_;
98
99 delete $self->{queue};
100
101 if (my $mon = delete $self->{lmon}) {
102 $_->(@reason) for map @$_, values %$mon;
103 }
104}
105
106sub clr_transport {
107 my ($self, @reason) = @_;
108
109 delete $self->{transport};
110 $self->connect;
111}
112 207
113sub connect { 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
217our $send_delayed = sub {
218 $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE;
219 (shift @DELAY)->()
220 while @DELAY;
221 undef $DELAY_W;
222 $DELAY = -50;
223};
224
225sub transport_reset {
114 my ($self) = @_; 226 my ($self) = @_;
115 227
116 Scalar::Util::weaken $self; 228 Scalar::Util::weaken $self;
117 229
118 $self->{retry} ||= [split /,/, $self->{noderef}]; 230 $self->{send} = sub {
119 231 if (++$DELAY > 0) {
120 my $endpoint = shift @{ $self->{retry} }; 232 my $msg = $_[0];
121 233 push @DELAY, sub { AnyEvent::MP::Kernel::_inject (@$msg) };
122 if (defined $endpoint) { 234 $DELAY_W ||= AE::timer 0, 0, $send_delayed;
123 $self->{trial}{$endpoint} ||= do {
124 my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
125 or return $AnyEvent::MP::Base::WARN->("$self->{noderef}: not a resolved node reference.");
126
127 my ($w, $g);
128
129 $w = AE::timer $AnyEvent::MP::Base::CONNECT_TIMEOUT, 0, sub {
130 delete $self->{trial}{$endpoint};
131 };
132 $g = AnyEvent::MP::Transport::mp_connect
133 $host, $port,
134 sub {
135 delete $self->{trial}{$endpoint}
136 unless @_;
137 $g = shift;
138 };
139 ; 235 return;
140
141 [$w, \$g]
142 }; 236 }
143 } else { 237
144 delete $self->{retry}; 238 local $AnyEvent::MP::Kernel::SRCNODE = $AnyEvent::MP::Kernel::NODE;
145 $self->fail (transport_error => $self->{noderef}, "unable to connect"); 239 AnyEvent::MP::Kernel::_inject (@{ $_[0] });
146 } 240 };
147
148 $self->{next_connect} = AE::timer $AnyEvent::MP::Base::CONNECT_INTERVAL, 0, sub {
149 $self->connect;
150 };
151} 241}
152 242
153package AnyEvent::MP::Node::Slave; 243sub transport_connect {
154
155use base "AnyEvent::MP::Node::Direct";
156
157sub connect {
158 my ($self) = @_; 244 my ($self, $tp) = @_;
159 245
160 $self->fail (transport_error => $self->{noderef}, "unable to connect to slave node"); 246 AE::log 9 => "I refuse to talk to myself ($tp->{peerhost}:$tp->{peerport})";
161}
162
163package AnyEvent::MP::Node::Self;
164
165use base "AnyEvent::MP::Node";
166
167sub set_transport {
168 Carp::confess "FATAL error, set_transport was called on local node";
169}
170
171sub send {
172 local $AnyEvent::MP::Base::SRCNODE = $_[0];
173 AnyEvent::MP::Base::_inject (@{ $_[1] });
174} 247}
175 248
176sub kill { 249sub kill {
177 my ($self, $port, @reason) = @_; 250 my (undef, @args) = @_;
178 251
179 delete $AnyEvent::MP::Base::PORT{$port}; 252 # we _always_ delay kil's, to avoid calling mon callbacks
180 delete $AnyEvent::MP::Base::PORT_DATA{$port}; 253 # from anything but the event loop context.
181 254 $DELAY = 1;
182 my $mon = delete $AnyEvent::MP::Base::LMON{$port} 255 push @DELAY, sub { AnyEvent::MP::Kernel::_kill (@args) };
183 or !@reason 256 $DELAY_W ||= AE::timer 0, 0, $send_delayed;
184 or $AnyEvent::MP::Base::WARN->("unmonitored local port $port died with reason: @reason");
185
186 $_->(@reason) for values %$mon;
187} 257}
188 258
189sub monitor { 259sub monitor {
190 my ($self, $portid, $cb) = @_; 260 # maybe always delay, too?
191 261 if ($DELAY_W) {
192 return $cb->(no_such_port => "cannot monitor nonexistent port") 262 my @args = @_;
193 unless exists $AnyEvent::MP::Base::PORT{$portid}; 263 push @DELAY, sub { AnyEvent::MP::Kernel::_monitor (@args) };
194 264 return;
195 $AnyEvent::MP::Base::LMON{$portid}{$cb+0} = $cb; 265 }
266 &AnyEvent::MP::Kernel::_monitor;
196} 267}
197 268
198sub unmonitor { 269sub unmonitor {
199 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 }
200 276
201 delete $AnyEvent::MP::Base::LMON{$portid}{$cb+0}; 277 &AnyEvent::MP::Kernel::_unmonitor;
202} 278}
203 279
204=head1 SEE ALSO 280=head1 SEE ALSO
205 281
206L<AnyEvent>. 282L<AnyEvent::MP>.
207 283
208=head1 AUTHOR 284=head1 AUTHOR
209 285
210 Marc Lehmann <schmorp@schmorp.de> 286 Marc Lehmann <schmorp@schmorp.de>
211 http://home.schmorp.de/ 287 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines