We'll give workqueues a broadcast mechanism to ensure all
workers see a certain message. We'll also tag each worker
with {-wq_worker_nr} in preparation for work distribution.
This is intended to avoid extra connection and fork() costs
from LeiAuth in a future commit.
}
sub wq_worker_loop ($) {
}
sub wq_worker_loop ($) {
+ my ($self, $bcast_a) = @_;
my $wqw = PublicInbox::WQWorker->new($self);
my $wqw = PublicInbox::WQWorker->new($self);
+ PublicInbox::WQWorker->new($self, '-wq_bcast2');
PublicInbox::DS->SetPostLoopCallback(sub { $wqw->{sock} });
PublicInbox::DS->EventLoop;
PublicInbox::DS->Reset;
PublicInbox::DS->SetPostLoopCallback(sub { $wqw->{sock} });
PublicInbox::DS->EventLoop;
PublicInbox::DS->Reset;
recv_and_run($self, delete $self->{0}, $len, 1);
}
recv_and_run($self, delete $self->{0}, $len, 1);
}
+sub wq_broadcast {
+ my ($self, $sub, @args) = @_;
+ if (my $wkr = $self->{-wq_workers}) {
+ for my $bcast1 (values %$wkr) {
+ my $buf = ipc_freeze([$sub, @args]);
+ send($bcast1, $buf, MSG_EOR) // croak "send: $!";
+ # XXX shouldn't have to deal with EMSGSIZE here...
+ }
+ } else {
+ eval { $self->$sub(@args) };
+ warn "wq_broadcast: $@" if $@;
+ }
+}
+
sub wq_io_do { # always async
my ($self, $sub, $ios, @args) = @_;
if (my $s1 = $self->{-wq_s1}) { # run in worker
sub wq_io_do { # always async
my ($self, $sub, $ios, @args) = @_;
if (my $s1 = $self->{-wq_s1}) { # run in worker
sub _wq_worker_start ($$$) {
my ($self, $oldset, $fields) = @_;
sub _wq_worker_start ($$$) {
my ($self, $oldset, $fields) = @_;
+ my ($bcast1, $bcast2);
+ socketpair($bcast1, $bcast2, AF_UNIX, $SEQPACKET, 0) or
+ die "socketpair: $!";
my $seed = rand(0xffffffff);
my $pid = fork // die "fork: $!";
if ($pid == 0) {
srand($seed);
my $seed = rand(0xffffffff);
my $pid = fork // die "fork: $!";
if ($pid == 0) {
srand($seed);
eval { PublicInbox::DS->Reset };
eval { PublicInbox::DS->Reset };
- delete @$self{qw(-wq_s1 -wq_workers -wq_ppid)};
+ delete @$self{qw(-wq_s1 -wq_ppid)};
+ $self->{-wq_worker_nr} =
+ keys %{delete($self->{-wq_workers}) // {}};
$SIG{$_} = 'IGNORE' for (qw(PIPE));
$SIG{$_} = 'DEFAULT' for (qw(TTOU TTIN TERM QUIT INT CHLD));
$SIG{$_} = 'IGNORE' for (qw(PIPE));
$SIG{$_} = 'DEFAULT' for (qw(TTOU TTIN TERM QUIT INT CHLD));
- local $0 = $self->{-wq_ident};
+ local $0 = "$self->{-wq_ident} $self->{-wq_worker_nr}";
# ensure we properly exit even if warn() dies:
my $end = PublicInbox::OnDestroy->new($$, sub { exit(!!$@) });
eval {
# ensure we properly exit even if warn() dies:
my $end = PublicInbox::OnDestroy->new($$, sub { exit(!!$@) });
eval {
my $on_destroy = $self->ipc_atfork_child;
local %SIG = %SIG;
PublicInbox::DS::sig_setmask($oldset);
my $on_destroy = $self->ipc_atfork_child;
local %SIG = %SIG;
PublicInbox::DS::sig_setmask($oldset);
+ $self->{-wq_bcast2} = $bcast2;
wq_worker_loop($self);
};
warn "worker $self->{-wq_ident} PID:$$ died: $@" if $@;
undef $end; # trigger exit
} else {
wq_worker_loop($self);
};
warn "worker $self->{-wq_ident} PID:$$ died: $@" if $@;
undef $end; # trigger exit
} else {
- $self->{-wq_workers}->{$pid} = \undef;
+ $self->{-wq_workers}->{$pid} = $bcast1;
use IO::Handle (); # blocking
sub new {
use IO::Handle (); # blocking
sub new {
- my (undef, $wq) = @_;
- my $s2 = $wq->{-wq_s2} // die 'BUG: no -wq_s2';
+ my ($cls, $wq, $field) = @_;
+ my $s2 = $wq->{$field // '-wq_s2'} // die "BUG: no {$field}";
- my $self = bless { sock => $s2, wq => $wq }, __PACKAGE__;
+ my $self = bless { sock => $s2, wq => $wq }, $cls;
$self->SUPER::new($s2, EPOLLEXCLUSIVE|EPOLLIN|EPOLLET);
$self;
}
$self->SUPER::new($s2, EPOLLEXCLUSIVE|EPOLLIN|EPOLLET);
$self;
}
} while ($n);
return if !defined($n) && $! == EAGAIN; # likely
warn "wq worker error: $!\n" if !defined($n) && $! != ECONNRESET;
} while ($n);
return if !defined($n) && $! == EAGAIN; # likely
warn "wq worker error: $!\n" if !defined($n) && $! != ECONNRESET;
- $self->{wq}->wq_atexit_child;
+ $self->{wq}->wq_atexit_child if $self->{sock} == $self->{wq}->{-wq_s2};
$self->close; # PublicInbox::DS::close
}
$self->close; # PublicInbox::DS::close
}
use Digest::SHA qw(sha1_hex);
require_mods(qw(Storable||Sereal));
require_ok 'PublicInbox::IPC';
use Digest::SHA qw(sha1_hex);
require_mods(qw(Storable||Sereal));
require_ok 'PublicInbox::IPC';
+my ($tmpdir, $for_destroy) = tmpdir();
state $once = eval <<'';
package PublicInbox::IPC;
use strict;
state $once = eval <<'';
package PublicInbox::IPC;
use strict;
print { $self->{1} } sha1_hex($buf), "\n";
$self->{1}->flush;
}
print { $self->{1} } sha1_hex($buf), "\n";
$self->{1}->flush;
}
+sub test_append_pid {
+ my ($self, $file) = @_;
+ open my $fh, '>>', $file or die "open: $!";
+ $fh->autoflush(1);
+ print $fh "$$\n" or die "print: $!";
+}
1;
my $ipc = bless {}, 'PublicInbox::IPC';
1;
my $ipc = bless {}, 'PublicInbox::IPC';
defined($pid) or BAIL_OUT 'no spawn, no test';
is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
$test->('worker');
defined($pid) or BAIL_OUT 'no spawn, no test';
is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
$test->('worker');
- {
- my ($tmp, $for_destroy) = tmpdir();
- $ipc->ipc_lock_init("$tmp/lock");
- is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
- }
+ $ipc->ipc_lock_init("$tmpdir/lock");
+ is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
$ipc->ipc_worker_stop;
ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
}
$ipc->ipc_worker_stop;
ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
}
$SIG{__WARN__} = 'DEFAULT';
is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
is($ipc->wq_workers, 1, '1 worker started');
$SIG{__WARN__} = 'DEFAULT';
is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
is($ipc->wq_workers, 1, '1 worker started');
- SKIP: {
- $ipc->WQ_MAX_WORKERS > 1 or
- skip 'Inline::C or Socket::MsgHdr not available', 4;
- $ipc->wq_worker_incr;
- is($ipc->wq_workers, 2, 'worker count bumped');
- $ipc->wq_worker_decr;
- $ipc->wq_worker_decr_wait(10);
- is($ipc->wq_workers, 1, 'worker count lowered');
- is($ipc->wq_workers(2), 2, 'worker count set');
- is($ipc->wq_workers, 2, 'worker count stayed set');
- }
+
+ $ipc->wq_worker_incr;
+ is($ipc->wq_workers, 2, 'worker count bumped');
+ $ipc->wq_worker_decr;
+ $ipc->wq_worker_decr_wait(10);
+ is($ipc->wq_workers, 1, 'worker count lowered');
+ is($ipc->wq_workers(2), 2, 'worker count set');
+ is($ipc->wq_workers, 2, 'worker count stayed set');
+
+ $ipc->wq_broadcast('test_append_pid', "$tmpdir/append_pid");
+ open my $fh, '<', "$tmpdir/append_pid" or BAIL_OUT "open: $!";
+ chomp(my @pids = <$fh>);
+ my %pids = map { $_ => 1 } grep(/\A[0-9]+\z/, @pids);
+ is(scalar keys %pids, 2, 'broadcast hit both PIDs');
is($ipc->wq_workers, undef, 'workers undef after close');
}
is($ipc->wq_workers, undef, 'workers undef after close');
}