/usr/share/perl5/Gearman/Taskset.pm is in libgearman-client-perl 2.004.012-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | package Gearman::Taskset;
use version ();
$Gearman::Taskset::VERSION = version->declare("2.004.012");
use strict;
use warnings;
=head1 NAME
Gearman::Taskset - a taskset in Gearman, from the point of view of a L<Gearman::Client>
=head1 SYNOPSIS
use Gearman::Client;
my $client = Gearman::Client->new;
# waiting on a set of tasks in parallel
my $ts = $client->new_task_set;
$ts->add_task( "add" => "1+2", {...});
$ts->wait();
=head1 DESCRIPTION
Gearman::Taskset is a L<Gearman::Client>'s representation of tasks queue
=head1 METHODS
=cut
use fields (
qw/
waiting
client
need_handle
default_sock
default_sockaddr
loaned_sock
cancelled
hooks
/
);
use Carp ();
use Gearman::Util ();
use Gearman::ResponseParser::Taskset;
use IO::Select;
# i thought about weakening taskset's client, but might be too weak.
use Scalar::Util ();
use Socket ();
use Storable ();
use Time::HiRes ();
=head2 new($client)
=cut
sub new {
my ($self, $client) = @_;
(Scalar::Util::blessed($client) && $client->isa("Gearman::Client"))
|| Carp::croak
"provided client argument is not a Gearman::Client reference";
unless (ref $self) {
$self = fields::new($self);
}
# { handle => [Task, ...] }
$self->{waiting} = {};
$self->{need_handle} = [];
$self->{client} = $client;
# { hostport => socket }
$self->{loaned_sock} = {};
# bool, if taskset has been cancelled mid-processing
$self->{cancelled} = 0;
# { hookname => coderef }
$self->{hooks} = {};
# default socket (non-merged requests)
$self->{default_sock} = undef;
# $self->client()->_js_str($self->{default_sock});
$self->{default_sockaddr} = undef;
return $self;
} ## end sub new
sub DESTROY {
my $self = shift;
# During global cleanup this may be called out of order, and the client my not exist in the taskset.
return unless $self->client;
if ($self->{default_sock}) {
$self->client->_sock_cache($self->{default_sockaddr},
$self->{default_sock});
}
keys %{ $self->{loaned_sock} };
while (my ($hp, $sock) = each %{ $self->{loaned_sock} }) {
$self->client->_sock_cache($hp, $sock);
}
} ## end sub DESTROY
#=head2 run_hook($name)
#
#run a hook callback if defined
#
#=cut
sub run_hook {
my ($self, $name) = (shift, shift);
($name && $self->{hooks}->{$name}) || return;
eval { $self->{hooks}->{$name}->(@_) };
warn "Gearman::Taskset hook '$name' threw error: $@\n" if $@;
} ## end sub run_hook
#=head2 add_hook($name, [$cb])
#
#add a hook
#
#=cut
sub add_hook {
my ($self, $name, $cb) = @_;
$name || return;
if ($cb) {
$self->{hooks}->{$name} = $cb;
}
else {
delete $self->{hooks}->{$name};
}
} ## end sub add_hook
#=head2 client ()
#
#B<return> L<Gearman::Client>
#
#=cut
# this method is part of the "Taskset" interface, also implemented by
# Gearman::Client::Async, where no tasksets make sense, so instead the
# Gearman::Client::Async object itself is also its taskset. (the
# client tracks all tasks). so don't change this, without being aware
# of Gearman::Client::Async. similarly, don't access $ts->{client} without
# going via this accessor.
sub client {
return shift->{client};
}
#=head2 cancel()
#
#Close sockets, cleanup internals.
#
#=cut
sub cancel {
my $self = shift;
$self->{cancelled} = 1;
if ($self->{default_sock}) {
close($self->{default_sock});
$self->{default_sock} = undef;
}
foreach my $sock (values %{ $self->{loaned_sock} }) {
$sock->close;
}
$self->{client} = undef;
$self->{loaned_sock} = {};
$self->{need_handle} = [];
$self->{waiting} = {};
} ## end sub cancel
#
# _get_loaned_sock($js)
#
sub _get_loaned_sock {
my ($self, $js) = @_;
my $js_str = $self->client()->_js_str($js);
if (my $sock = $self->{loaned_sock}{$js_str}) {
return $sock if $sock->connected;
delete $self->{loaned_sock}{$js_str};
}
my $sock = $self->client()->_get_js_sock($js);
return $self->{loaned_sock}{$js_str} = $sock;
} ## end sub _get_loaned_sock
=head2 wait(%opts)
Waits for a response from the job server for any of the tasks listed
in the taskset. Will call the I<on_*> handlers for each of the tasks
that have been completed, updated, etc. Doesn't return until
everything has finished running or failing.
=cut
sub wait {
my ($self, %opts) = @_;
my ($timeout, $given_timeout_s);
if (exists $opts{timeout}) {
$timeout = delete $opts{timeout};
if (defined $timeout) {
## keep the given timeout value for the failure reason
# Handles issue #35
# https://github.com/p-alik/perl-Gearman/issues/35
$given_timeout_s = $timeout;
$timeout += Time::HiRes::time();
}
}
Carp::carp "Unknown options: "
. join(',', keys %opts)
. " passed to Taskset->wait."
if keys %opts;
# fd -> Gearman::ResponseParser object
my %parser;
my $cb = sub {
my ($fd) = shift;
my $parser = $parser{$fd} ||= Gearman::ResponseParser::Taskset->new(
source => $fd,
taskset => $self
);
eval {
$parser->parse_sock($fd);
1;
} or do {
# TODO this should remove the fd from the list, and reassign any tasks to other jobserver, or bail.
# We're not in an accessible place here, so if all job servers fail we must die to prevent hanging.
Carp::croak("Job server failure: $@");
} ## end do
};
my $io = IO::Select->new($self->{default_sock},
values %{ $self->{loaned_sock} });
my $pending_sock;
foreach ($io->handles) {
(ref($_) eq "IO::Socket::SSL" && $_->pending()) || next;
$pending_sock = $_;
last;
}
if ($pending_sock) {
return $cb->($pending_sock);
}
while (!$self->{cancelled} && keys %{ $self->{waiting} }) {
my $time_left = $timeout ? $timeout - Time::HiRes::time() : 0.5;
my $nfound = select($io->bits(), undef, undef, $time_left);
if ($timeout && $time_left <= 0) {
## Attempt to fix
# https://github.com/p-alik/perl-Gearman/issues/33
# Mark all tasks of that taskset failed.
# Get all waiting tasks and call their "fail" method one by one
# with the failure reason.
for (values %{ $self->{waiting} }) {
for (@$_) {
my $func = $_->func;
## use the given timeout here
# Handles issue #35
# https://github.com/p-alik/perl-Gearman/issues/35
$_->fail("Task $func elapsed timeout [${given_timeout_s}s]");
}
} ## end for (values %{ $self->{...}})
$self->cancel;
return;
} ## end if ($timeout && $time_left...)
next if !$nfound;
foreach my $fd ($io->can_read()) {
$cb->($fd);
}
} ## end while (!$self->{cancelled...})
} ## end sub wait
=head2 add_task(Gearman::Task)
=head2 add_task($func, <$scalar | $scalarref>, <$uniq | $opts_hr>
Adds a task to the taskset. Three different calling conventions are available.
C<$opts_hr> see L<Gearman::Task>
=cut
sub add_task {
my $self = shift;
my $task = $self->client()->_get_task_from_args(@_);
$task->taskset($self);
$self->run_hook('add_task', $self, $task);
my $jssock = $task->{jssock};
return $task->fail("undefined jssock") unless ($jssock);
my $req = $task->pack_submit_packet($self->client);
Gearman::Util::send_req($jssock, \$req)
|| Carp::croak "Error sending data to job server";
push @{ $self->{need_handle} }, $task;
while (@{ $self->{need_handle} }) {
my $rv
= $self->_wait_for_packet($jssock,
$self->client()->{command_timeout});
if (!$rv) {
# ditch it, it failed.
# this will resubmit it if it failed.
shift @{ $self->{need_handle} };
return $task->fail(
join(' ',
"no rv on waiting for packet",
defined($rv) ? $rv : $!)
);
} ## end if (!$rv)
} ## end while (@{ $self->{need_handle...}})
return $task->handle;
} ## end sub add_task
#
# _get_default_sock()
# used in Gearman::Task->taskset only
#
sub _get_default_sock {
my $self = shift;
return $self->{default_sock} if $self->{default_sock};
my $getter = sub {
my $js = shift;
return $self->{loaned_sock}{$js}
|| $self->client()->_get_js_sock($js);
};
my ($js, $jss) = $self->client()->_get_random_js_sock($getter);
return unless $jss;
my $js_str = $self->client()->_js_str($js);
$self->{loaned_sock}{$js_str} ||= $jss;
$self->{default_sock} = $jss;
$self->{default_sockaddr} = $js_str;
return $jss;
} ## end sub _get_default_sock
#
# _get_hashed_sock($hv)
#
# only used in Gearman::Task->taskset only
#
# return a socket
sub _get_hashed_sock {
my $self = shift;
my $hv = shift;
my ($js_count, @job_servers)
= ($self->client()->{js_count}, $self->client()->job_servers());
my $sock;
for (my $off = 0; $off < $js_count; $off++) {
my $idx = ($hv + $off) % ($js_count);
$sock = $self->_get_loaned_sock($job_servers[$idx]);
last;
}
return $sock;
} ## end sub _get_hashed_sock
#
# _wait_for_packet($sock, $timeout)
#
# $sock socket to singularly read from
#
# returns boolean when given a sock to wait on.
# otherwise, return value is undefined.
sub _wait_for_packet {
my ($self, $sock, $timeout) = @_;
my $res = Gearman::Util::read_res_packet($sock, \my $err, $timeout);
$err && Carp::croak("reading response packet failed: $err");
return $res ? $self->process_packet($res, $sock) : 0;
} ## end sub _wait_for_packet
#
# _is_port($sock)
#
# return hostport || ipport
#
sub _ip_port {
my ($self, $sock) = @_;
$sock || return;
my $pn = getpeername($sock);
$pn || return;
# look for a hostport in loaned_sock
my $hostport;
my @k = keys %{ $self->{loaned_sock} };
while (!$hostport && (my $hp = shift @k)) {
my $s = $self->{loaned_sock}->{$hp};
$s || next;
if ($sock == $s) {
$hostport = $hp;
# last;
}
} ## end while (!$hostport && (my ...))
# hopefully it solves client->get_status mismatch
$hostport && return $hostport;
my $fam = Socket::sockaddr_family($pn);
my ($port, $iaddr)
= ($fam == Socket::AF_INET6)
? Socket::sockaddr_in6($pn)
: Socket::sockaddr_in($pn);
my $addr = Socket::inet_ntop($fam, $iaddr);
return join ':', $addr, $port;
} ## end sub _ip_port
#
# _fail_jshandle($shandle, $type, [$message])
#
# note the failure of a task given by its jobserver-specific handle
#
sub _fail_jshandle {
my ($self, $shandle, $type, $msg) = @_;
$shandle
or Carp::croak "_fail_jshandle() called without shandle parameter";
my $task_list = $self->{waiting}{$shandle}
or Carp::croak "Got $type for unknown handle: $shandle";
my $task = shift @{$task_list};
(Scalar::Util::blessed($task) && $task->isa("Gearman::Task"))
|| Carp::croak "task_list is empty on $type for handle $shandle\n";
$task->fail($msg || "jshandle fail");
delete $self->{waiting}{$shandle} unless @{$task_list};
} ## end sub _fail_jshandle
#=head2 process_packet($res, $sock)
#
# process response packet
#
#=cut
sub process_packet {
my ($self, $res, $sock) = @_;
my $qr = qr/(.+?)\0/;
my %assert = (
task => sub {
my ($task, $msg) = @_;
(Scalar::Util::blessed($task) && $task->isa("Gearman::Task"))
|| Carp::croak $msg;
}
);
my %type = (
job_created => sub {
my ($blob) = shift;
my $task = shift @{ $self->{need_handle} };
$assert{task}
->($task, "Got an unexpected job_created notification");
my $shandle = $blob;
my $ipport = $self->_ip_port($sock);
# did sock become disconnected in the meantime?
if (!$ipport) {
$self->_fail_jshandle($shandle, "job_created");
return 1;
}
$task->handle("$ipport//$shandle");
return 1 if $task->{background};
push @{ $self->{waiting}{$shandle} ||= [] }, $task;
return 1;
},
work_complete => sub {
my ($blob) = shift;
($blob =~ /^$qr/)
or Carp::croak "Bogus work_complete from server";
$blob =~ s/^$qr//;
my $shandle = $1;
my $task_list = $self->{waiting}{$shandle};
my $task = shift @{$task_list};
$assert{task}->(
$task,
"task_list is empty on work_complete for handle $shandle"
);
$task->complete(\$blob);
delete $self->{waiting}{$shandle} unless @{$task_list};
return 1;
},
work_data => sub {
my ($blob) = shift;
$blob =~ s/^(.+?)\0//
or Carp::croak "Bogus work_data from server";
my $shandle = $1;
my $task_list = $self->{waiting}{$shandle};
my $task = $task_list->[0];
$assert{task}->($task,
"task_list is empty on work_data for handle $shandle");
$task->data(\$blob);
return 1;
},
work_warning => sub {
my ($blob) = shift;
$blob =~ s/^(.+?)\0//
or Carp::croak "Bogus work_warning from server";
my $shandle = $1;
my $task_list = $self->{waiting}{$shandle};
my $task = $task_list->[0];
$assert{task}->(
$task, "task_list is empty on work_warning for handle $shandle"
);
$task->warning(\$blob);
return 1;
},
work_exception => sub {
my ($blob) = shift;
($blob =~ /^$qr/)
or Carp::croak "Bogus work_exception from server";
$blob =~ s/^$qr//;
my $shandle = $1;
my $task_list = $self->{waiting}{$shandle};
my $task = shift @{$task_list};
$assert{task}->(
$task,
"task_list is empty on work_exception for handle $shandle"
);
#FIXME we have to freeze $blob because Task->exception expected it in this form.
# The only reason I see to do it so, is Worker->work implementation. With Gearman::Server it uses nfreeze for exception value.
$task->exception(\Storable::freeze(\$blob));
delete $self->{waiting}{$shandle} unless @{$task_list};
return 1;
},
work_fail => sub {
$self->_fail_jshandle(shift, "work_fail");
return 1;
},
work_status => sub {
my ($blob) = shift;
my ($shandle, $nu, $de) = split(/\0/, $blob);
my $task_list = $self->{waiting}{$shandle};
ref($task_list) eq "ARRAY" && scalar(@{$task_list})
or Carp::croak "Got work_status for unknown handle: $shandle";
# FIXME: the server is (probably) sending a work_status packet for each
# interested client, even if the clients are the same, so probably need
# to fix the server not to do that. just put this FIXME here for now,
# though really it's a server issue.
foreach my $task (@{$task_list}) {
$task->status($nu, $de);
}
return 1;
},
);
defined($type{ $res->{type} })
|| Carp::croak
"Unimplemented packet type: $res->{type} [${$res->{blobref}}]";
return $type{ $res->{type} }->(${ $res->{blobref} });
} ## end sub process_packet
1;
|