]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiXSearch.pm
lei: give workers their own process group
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
index 99f887ce730f50f21c45f742576164c528217415..668d0b6e5df3154f2e927a38d8786269f4f8e48f 100644 (file)
@@ -16,6 +16,7 @@ use PublicInbox::Spawn qw(popen_rd spawn which);
 use PublicInbox::MID qw(mids);
 use PublicInbox::Smsg;
 use PublicInbox::Eml;
+use PublicInbox::LEI;
 use Fcntl qw(SEEK_SET F_SETFL O_APPEND O_RDWR);
 use PublicInbox::ContentHash qw(git_sha);
 use POSIX qw(strftime);
@@ -32,7 +33,7 @@ sub new {
 sub attach_external {
        my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
        my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
-       my $srch = $ibxish->search or
+       my $srch = $ibxish->search //
                return warn("$desc not indexed for Xapian ($@ $!)\n");
        my @shards = $srch->xdb_shards_flat or
                return warn("$desc has no Xapian shards\n");
@@ -183,11 +184,10 @@ sub query_one_mset { # for --threads and l2m w/o sort
        my $maxk = "external.$dir.maxuid";
        my $stop_at = $lss ? $lss->{-cfg}->{$maxk} : undef;
        if (defined $stop_at) {
-               die "$maxk=$stop_at has multiple values" if ref $stop_at;
-               my @e;
-               local $SIG{__WARN__} = sub { push @e, @_ };
-               $stop_at += 0;
-               return warn("$maxk=$stop_at: @e") if @e;
+               ref($stop_at) and
+                       return warn("$maxk=$stop_at has multiple values\n");
+               ($stop_at =~ /[^0-9]/) and
+                       return warn("$maxk=$stop_at not numeric\n");
        }
        my $first_ids;
        do {
@@ -391,12 +391,11 @@ sub query_remote_mboxrd {
                }
                $err = '';
                if (-s $cerr) {
-                       seek($cerr, 0, SEEK_SET) or
-                                       $lei->err("seek($cmd stderr): $!");
+                       seek($cerr, 0, SEEK_SET) //
+                                       warn "seek($cmd stderr): $!";
                        $err = do { local $/; <$cerr> } //
-                                       "read($cmd stderr): $!";
-                       truncate($cerr, 0) or
-                                       $lei->err("truncate($cmd stderr): $!");
+                                       warn "read($cmd stderr): $!";
+                       truncate($cerr, 0) // warn "truncate($cmd stderr): $!";
                }
                next if (($? >> 8) == 22 && $err =~ /\b404\b/);
                $uri->query_form(q => $qstr);
@@ -416,14 +415,14 @@ sub xsearch_done_wait { # dwaitpid callback
 
 sub query_done { # EOF callback for main daemon
        my ($lei) = @_;
+       local $PublicInbox::LEI::current_lei = $lei;
        my $l2m = delete $lei->{l2m};
        $l2m->wq_wait_old(\&xsearch_done_wait, $lei) if $l2m;
        if (my $lxs = delete $lei->{lxs}) {
                $lxs->wq_wait_old(\&xsearch_done_wait, $lei);
        }
-       if ($lei->{opt}->{'mail-sync'} && !$lei->{sto}) {
+       ($lei->{opt}->{'mail-sync'} && !$lei->{sto}) and
                warn "BUG: {sto} missing with --mail-sync";
-       }
        $lei->sto_done_request if $lei->{sto};
        my $wait = $lei->{v2w} ? $lei->{v2w}->wq_do('done') : undef;
        $lei->{ovv}->ovv_end($lei);
@@ -462,6 +461,7 @@ Error closing $lei->{ovv}->{dst}: $!
 
 sub do_post_augment {
        my ($lei) = @_;
+       local $PublicInbox::LEI::current_lei = $lei;
        my $l2m = $lei->{l2m} or return; # client disconnected
        $lei->fchdir or return;
        my $err;
@@ -469,7 +469,7 @@ sub do_post_augment {
        $err = $@;
        if ($err) {
                if (my $lxs = delete $lei->{lxs}) {
-                       $lxs->wq_kill;
+                       $lxs->wq_kill('-TERM');
                        $lxs->wq_close(0, undef, $lei);
                }
                $lei->fail("$err");
@@ -497,9 +497,10 @@ sub concurrency {
        $nl + $nr;
 }
 
-sub start_query ($;$) { # always runs in main (lei-daemon) process
-       my ($self, $l2m) = @_;
-       if ($self->{opt_threads} || ($l2m && !$self->{opt_sort})) {
+sub start_query ($$) { # always runs in main (lei-daemon) process
+       my ($self, $lei) = @_;
+       local $PublicInbox::LEI::current_lei = $lei;
+       if ($self->{opt_threads} || ($lei->{l2m} && !$self->{opt_sort})) {
                for my $ibxish (locals($self)) {
                        $self->wq_io_do('query_one_mset', [], $ibxish);
                }
@@ -521,9 +522,10 @@ sub start_query ($;$) { # always runs in main (lei-daemon) process
 }
 
 sub incr_start_query { # called whenever an l2m shard starts do_post_auth
-       my ($self, $l2m) = @_;
+       my ($self, $lei) = @_;
+       my $l2m = $lei->{l2m};
        return if ++$self->{nr_start_query} != $l2m->{-wq_nr_workers};
-       start_query($self, $l2m);
+       start_query($self, $lei);
 }
 
 sub ipc_atfork_child {
@@ -536,16 +538,16 @@ sub do_query {
        my ($self, $lei) = @_;
        my $l2m = $lei->{l2m};
        my $ops = {
-               '|' => [ $lei->can('sigpipe_handler'), $lei ],
-               '!' => [ $lei->can('fail_handler'), $lei ],
-               '.' => [ \&do_post_augment, $lei ],
-               '+' => [ \&incr_post_augment, $lei ],
+               'sigpipe_handler' => [ $lei ],
+               'fail_handler' => [ $lei ],
+               'do_post_augment' => [ \&do_post_augment, $lei ],
+               'incr_post_augment' => [ \&incr_post_augment, $lei ],
                '' => [ \&query_done, $lei ],
                'mset_progress' => [ \&mset_progress, $lei ],
                'l2m_progress' => [ \&l2m_progress, $lei ],
-               'x_it' => [ $lei->can('x_it'), $lei ],
-               'child_error' => [ $lei->can('child_error'), $lei ],
-               'incr_start_query' => [ \&incr_start_query, $self, $l2m ],
+               'x_it' => [ $lei ],
+               'child_error' => [ $lei ],
+               'incr_start_query' => [ $self, $lei ],
        };
        $lei->{auth}->op_merge($ops, $l2m) if $l2m && $lei->{auth};
        my $end = $lei->pkt_op_pair;
@@ -586,7 +588,7 @@ sub do_query {
        if ($l2m) {
                $l2m->net_merge_all_done($lei) unless $lei->{auth};
        } else {
-               start_query($self);
+               start_query($self, $lei);
        }
        $lei->event_step_init; # wait for shutdowns
        $lei->wait_wq_events($op_c, $ops);