| 1 | package MR::Pending; | ||
| 2 | use Mouse; | ||
| 3 | use Time::HiRes qw/time/; | ||
| 4 | |||
| 5 | has maxtime => ( | ||
| 6 | is => 'rw', | ||
| 7 | isa => 'Num', | ||
| 8 | predicate => "_has_maxtime", | ||
| 9 | default => 6.0, | ||
| 10 | ); | ||
| 11 | |||
| 12 | has itertime => ( | ||
| 13 | is => 'rw', | ||
| 14 | isa => 'Num', | ||
| 15 | predicate => "_has_itertime", | ||
| 16 | default => 0.1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has name => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Str', | ||
| 22 | required => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has onidle => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'CodeRef', | ||
| 28 | predicate => "_has_onidle", | ||
| 29 | ); | ||
| 30 | |||
| 31 | has _pending => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'HashRef[MR::Pending::Item]', | ||
| 34 | default => sub { {} }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has exceptions => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Int', | ||
| 40 | default => 0, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has _exceptions => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => 'ArrayRef', | ||
| 46 | default => sub { [] }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | has _waitresult => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => 'ArrayRef', | ||
| 52 | ); | ||
| 53 | |||
| 54 | around BUILDARGS => sub { | ||
| 55 | my $orig = shift; | ||
| 56 | my $class = shift; | ||
| 57 | my %args = @_; | ||
| 58 | if(my $p = delete $args{pending}) { | ||
| 59 | $args{_pending} = { map { $_->id => $_ } @$p }; | ||
| 60 | } | ||
| 61 | $class->$orig(%args); | ||
| 62 | }; | ||
| 63 | |||
| 64 | sub runcatch { | ||
| 65 | my ($self, $code, @param) = @_; | ||
| 66 | my $ret; | ||
| 67 | unless(eval { $ret = &$code(@param); 1 }) { | ||
| 68 | push @{$self->_exceptions}, $@; | ||
| 69 | $self->exceptions($self->exceptions + 1); | ||
| 70 | } | ||
| 71 | return $ret; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub add { | ||
| 75 | my ($self, @p) = @_; | ||
| 76 | my $p = $self->_pending; | ||
| 77 | for my $add (@p) { | ||
| 78 | die if exists $p->{$add->id}; | ||
| 79 | $p->{$add->id} = $add; | ||
| 80 | } | ||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub remove { | ||
| 85 | my ($self, @p) = @_; | ||
| 86 | my $p = $self->_pending; | ||
| 87 | for my $del (@p) { | ||
| 88 | die unless exists $p->{$del->id}; | ||
| 89 | delete $p->{$del->id}; | ||
| 90 | } | ||
| 91 | return $self; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub send { | ||
| 95 | my ($self) = @_; | ||
| 96 | my $pending = $self->_pending; | ||
| 97 | foreach my $shard ( grep { $pending->{$_}->is_sleeping } keys %$pending ) { | ||
| 98 | my $pend = $pending->{$shard}; | ||
| 99 | if ($pend->try < $pend->retry) { | ||
| 100 | next unless $pend->is_timeout; | ||
| 101 | $pend->set_pending_mode(scalar $self->runcatch($pend->onretry, ($pend->id, $pend, $self))); | ||
| 102 | } else { | ||
| 103 | delete $pending->{$shard}; | ||
| 104 | $self->runcatch($pend->onerror, ($pend->id, "no success after @{[$pend->try]} retries", $pend, $self)); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub wait { | ||
| 111 | my ($self) = @_; | ||
| 112 | my $pending = $self->_pending; | ||
| 113 | |||
| 114 | my $in = ''; | ||
| 115 | vec($in, $_->fileno, 1) = 1 for grep { $_->is_pending } values %$pending; | ||
| 116 | |||
| 117 | my $n; | ||
| 118 | while(1) { | ||
| 119 | my $ein = my $rin = $in; | ||
| 120 | $n = CORE::select($rin, undef, $ein, $self->itertime); | ||
| 121 | $self->_waitresult([$rin,$ein]); | ||
| 122 | if ($n < 0) { | ||
| 123 | next if $!{EINTR}; | ||
| 124 | warn $self->name.": select() failed: $!"; | ||
| 125 | return undef; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | if ($n == 0) { | ||
| 130 | $self->runcatch($self->onidle, ($self)) if $self->_has_onidle; | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | return $n; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub recv { | ||
| 138 | my ($self) = @_; | ||
| 139 | my $pending = $self->_pending; | ||
| 140 | my ($rin, $ein) = @{$self->_waitresult}; | ||
| 141 | |||
| 142 | for my $shard (grep { $pending->{$_}->is_pending } keys %$pending) { | ||
| 143 | my $pend = $pending->{$shard}; | ||
| 144 | my $fileno = $pend->fileno; | ||
| 145 | if (vec($rin, $fileno, 1)) { | ||
| 146 | if (my $list = $pend->continue) { | ||
| 147 | if (ref $list) { | ||
| 148 | if(defined(my $okay = $self->runcatch($pend->onok, ($pend->id, $list, $pend, $self)))) { | ||
| 149 | if($okay) { | ||
| 150 | delete $pending->{$shard}; | ||
| 151 | } else { | ||
| 152 | $pend->set_sleeping_mode; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } else { | ||
| 157 | $pend->close("error while receiving (".$pend->last_error.")"); | ||
| 158 | } | ||
| 159 | } elsif (vec($ein, $fileno, 1)) { | ||
| 160 | $pend->close("connection reset (".$pend->last_error.")"); | ||
| 161 | } elsif ($pend->is_timeout) { | ||
| 162 | $pend->close("timeout (".$pend->last_error.")"); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | return $self; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub finish { | ||
| 170 | my ($self) = @_; | ||
| 171 | my $timeout = !$self->exceptions; | ||
| 172 | my $pending = $self->_pending; | ||
| 173 | for my $shard (grep { !$pending->{$_}->is_done } keys %$pending) { | ||
| 174 | my $pend = delete $pending->{$shard}; | ||
| 175 | $pend->close($timeout ? "timeout" : "aborted due to external exception"); | ||
| 176 | $self->runcatch($pend->onerror, ($pend->id, "timeout", $pend, $self)) if $timeout; | ||
| 177 | } | ||
| 178 | return $self; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub iter { | ||
| 182 | my ($self) = @_; | ||
| 183 | |||
| 184 | $self->send or return; | ||
| 185 | return if $self->exceptions; | ||
| 186 | |||
| 187 | my $res = $self->wait; | ||
| 188 | return if $self->exceptions; | ||
| 189 | return unless defined $res; | ||
| 190 | return 1 unless $res; | ||
| 191 | |||
| 192 | $self->recv or return; | ||
| 193 | return if $self->exceptions; | ||
| 194 | |||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub work { | ||
| 199 | my ($self) = @_; | ||
| 200 | |||
| 201 | my $pending = $self->_pending; | ||
| 202 | my $time0 = time; | ||
| 203 | |||
| 204 | while(%$pending and time() - $time0 <= $self->maxtime) { | ||
| 205 | last unless $self->iter; | ||
| 206 | } | ||
| 207 | $self->finish; | ||
| 208 | $self->check_exceptions('raise'); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub check_exceptions { | ||
| 212 | my ($self, $raise) = @_; | ||
| 213 | my $e = $self->_exceptions; | ||
| 214 | return unless $e && @$e; | ||
| 215 | my $str = "$$: PENDING EXCEPTIONS BEGIN\n".join("\n$$:###################\n", @$e)."$$: PENDING EXCEPTIONS END"; | ||
| 216 | die $str if $raise; | ||
| 217 | warn $str if defined $raise; | ||
| 218 | return $str; | ||
| 219 | } | ||
| 220 | |||
| 221 | no Mouse; | ||
| 222 | __PACKAGE__->meta->make_immutable(); | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | package MR::Pending::Item; | ||
| 228 | use Mouse; | ||
| 229 | use Time::HiRes qw/time/; | ||
| 230 | use Carp; | ||
| 231 | |||
| 232 | has id => ( | ||
| 233 | is => 'ro', | ||
| 234 | isa => 'Str', | ||
| 235 | required => 1, | ||
| 236 | ); | ||
| 237 | |||
| 238 | has $_ => ( | ||
| 239 | is => 'ro', | ||
| 240 | isa => 'CodeRef', | ||
| 241 | predicate => "_has_$_", | ||
| 242 | required => 1, | ||
| 243 | ) for qw/onok onerror onretry/; | ||
| 244 | |||
| 245 | has $_ => ( | ||
| 246 | is => 'rw', | ||
| 247 | isa => 'Num', | ||
| 248 | predicate => "_has_$_", | ||
| 249 | ) for qw/timeout retry_delay/; | ||
| 250 | |||
| 251 | has retry => ( | ||
| 252 | is => 'rw', | ||
| 253 | isa => 'Int', | ||
| 254 | predicate => "_has_retry", | ||
| 255 | ); | ||
| 256 | |||
| 257 | has status_unknown => ( | ||
| 258 | is => 'rw', | ||
| 259 | isa => 'Bool', | ||
| 260 | default => 0, | ||
| 261 | ); | ||
| 262 | |||
| 263 | has _done => ( | ||
| 264 | is => 'rw', | ||
| 265 | isa => 'Bool', | ||
| 266 | default => 0, | ||
| 267 | ); | ||
| 268 | |||
| 269 | has _time => ( | ||
| 270 | is => 'rw', | ||
| 271 | isa => 'Num', | ||
| 272 | default => 0, | ||
| 273 | ); | ||
| 274 | |||
| 275 | has _connection => ( | ||
| 276 | is => 'rw', | ||
| 277 | isa => 'Maybe[MR::IProto::Connection::Sync]', | ||
| 278 | clearer => '_clear__connection', | ||
| 279 | predicate=> '_has__connection', | ||
| 280 | handles => [qw/last_error/], | ||
| 281 | ); | ||
| 282 | |||
| 283 | has fileno => ( | ||
| 284 | is => 'ro', | ||
| 285 | isa => 'Int', | ||
| 286 | lazy => 1, | ||
| 287 | default => sub { Carp::confess "not connected!" unless $_[0]->_connection; $_[0]->_connection->fh->fileno }, | ||
| 288 | clearer => '_clear_fileno', | ||
| 289 | ); | ||
| 290 | |||
| 291 | has _continue => ( | ||
| 292 | is => 'rw', | ||
| 293 | isa => 'Maybe[CodeRef]', | ||
| 294 | clearer => '_clear__continue', | ||
| 295 | ); | ||
| 296 | |||
| 297 | has _postprocess => ( | ||
| 298 | is => 'rw', | ||
| 299 | isa => 'Maybe[CodeRef]', | ||
| 300 | clearer => '_clear__postprocess', | ||
| 301 | ); | ||
| 302 | |||
| 303 | has try => ( | ||
| 304 | is => 'ro', | ||
| 305 | isa => 'Int', | ||
| 306 | default => 0, | ||
| 307 | writer => '_set_try', | ||
| 308 | ); | ||
| 309 | |||
| 310 | # has bornat => ( | ||
| 311 | # is => 'ro', | ||
| 312 | # isa => 'Str', | ||
| 313 | # default => sub { "[".join("-", $_[0], $$, time(), Carp::longmess())."]"; }, | ||
| 314 | # ); | ||
| 315 | |||
| 316 | sub is_done { return $_[0]->_done } | ||
| 317 | sub is_pending { return !$_[0]->_done && $_[0]->_has__connection } | ||
| 318 | sub is_sleeping { return !$_[0]->_done && !$_[0]->_has__connection } | ||
| 319 | |||
| 320 | sub set_pending_mode { | ||
| 321 | my ($self, $cont) = @_; | ||
| 322 | $self->_done(0); | ||
| 323 | $self->_clear__connection; | ||
| 324 | $self->_clear__continue; | ||
| 325 | $self->_clear__postprocess; | ||
| 326 | $self->_clear_fileno; | ||
| 327 | if($cont) { | ||
| 328 | $self->_connection($cont->{connection}); | ||
| 329 | $self->_continue($cont->{continue}); | ||
| 330 | $self->_postprocess($cont->{postprocess}); | ||
| 331 | } | ||
| 332 | if (@_ > 1) { | ||
| 333 | $self->status_unknown(0); | ||
| 334 | $self->_set_try($self->try + 1); | ||
| 335 | } | ||
| 336 | $self->_time(time); | ||
| 337 | return $self; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub set_sleeping_mode { | ||
| 341 | $_[0]->set_pending_mode; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub is_timeout { | ||
| 345 | my ($self, $timeout) = @_; | ||
| 346 | $timeout ||= $self->is_pending ? $self->timeout : $self->retry_delay; | ||
| 347 | return time() - $self->_time > $timeout; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub continue { | ||
| 351 | my ($self) = @_; | ||
| 352 | my $is_cont = 0; | ||
| 353 | my @list; | ||
| 354 | if (eval{@list = $self->_continue->($is_cont); 1}) { | ||
| 355 | if ($is_cont) { | ||
| 356 | $self->_clear_fileno; | ||
| 357 | $self->_connection($list[0]->{connection}); | ||
| 358 | $self->_continue($list[0]->{continue}); | ||
| 359 | $self->_time(time); | ||
| 360 | return 1; | ||
| 361 | } else { | ||
| 362 | $self->_done(1); | ||
| 363 | if (my $pp = $self->_postprocess) { | ||
| 364 | &$pp(\@list); | ||
| 365 | } | ||
| 366 | return \@list; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | return 0; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub close { | ||
| 373 | my ($self, $reason) = @_; | ||
| 374 | if ($self->is_pending) { | ||
| 375 | $self->_connection->Close($reason); | ||
| 376 | $self->status_unknown(1); | ||
| 377 | } | ||
| 378 | $self->set_sleeping_mode; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub DEMOLISH { | ||
| 382 | my ($self) = @_; | ||
| 383 | warn "$$ FORGOTTEN $self" if $self->is_pending; | ||
| 384 | #Carp::cluck "$$ FORGOTTEN $self" if $self->is_pending; | ||
| 385 | } | ||
| 386 | |||
| 387 | no Mouse; | ||
| 388 | __PACKAGE__->meta->make_immutable(); | ||
| 389 | |||
| 390 | 1; |