]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei: test SIGPIPE, stop xsearch workers on client abort
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Combine any combination of PublicInbox::Search,
5 # PublicInbox::ExtSearch, and PublicInbox::LeiSearch objects
6 # into one Xapian DB
7 package PublicInbox::LeiXSearch;
8 use strict;
9 use v5.10.1;
10 use parent qw(PublicInbox::LeiSearch PublicInbox::IPC);
11 use Sys::Syslog qw(syslog);
12
13 sub new {
14         my ($class) = @_;
15         PublicInbox::Search::load_xapian();
16         bless {
17                 qp_flags => $PublicInbox::Search::QP_FLAGS |
18                                 PublicInbox::Search::FLAG_PURE_NOT(),
19         }, $class
20 }
21
22 sub attach_external {
23         my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
24
25         if (!$ibxish->can('over') || !$ibxish->over) {
26                 return push(@{$self->{remotes}}, $ibxish)
27         }
28         my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
29         my $srch = $ibxish->search or
30                 return warn("$desc not indexed for Xapian\n");
31         my @shards = $srch->xdb_shards_flat or
32                 return warn("$desc has no Xapian shardsXapian\n");
33
34         if (delete $self->{xdb}) { # XXX: do we need this?
35                 # clobber existing {xdb} if amending
36                 my $expect = delete $self->{nshard};
37                 my $shards = delete $self->{shards_flat};
38                 scalar(@$shards) == $expect or die
39                         "BUG: {nshard}$expect != shards=".scalar(@$shards);
40
41                 my $prev = {};
42                 for my $old_ibxish (@{$self->{shard2ibx}}) {
43                         next if $prev == $old_ibxish;
44                         $prev = $old_ibxish;
45                         my @shards = $old_ibxish->search->xdb_shards_flat;
46                         push @{$self->{shards_flat}}, @shards;
47                 }
48                 my $nr = scalar(@{$self->{shards_flat}});
49                 $nr == $expect or die
50                         "BUG: reloaded $nr shards, expected $expect"
51         }
52         push @{$self->{shards_flat}}, @shards;
53         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
54 }
55
56 # returns a list of local inboxes (or count in scalar context)
57 sub locals {
58         my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
59         values %uniq;
60 }
61
62 # called by PublicInbox::Search::xdb
63 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
64
65 # like over->get_art
66 sub smsg_for {
67         my ($self, $mitem) = @_;
68         # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
69         my $nshard = $self->{nshard};
70         my $docid = $mitem->get_docid;
71         my $shard = ($docid - 1) % $nshard;
72         my $num = int(($docid - 1) / $nshard) + 1;
73         my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
74         $smsg->{docid} = $docid;
75         $smsg;
76 }
77
78 sub recent {
79         my ($self, $qstr, $opt) = @_;
80         $opt //= {};
81         $opt->{relevance} //= -2;
82         $self->mset($qstr //= 'bytes:1..', $opt);
83 }
84
85 sub over {}
86
87 sub _mset_more ($$) {
88         my ($mset, $mo) = @_;
89         my $size = $mset->size;
90         $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
91 }
92
93 sub query_thread_mset { # for --thread
94         my ($self, $lei, $ibxish) = @_;
95         my %sig = $lei->atfork_child_wq($self);
96         local @SIG{keys %sig} = values %sig;
97
98         my ($srch, $over) = ($ibxish->search, $ibxish->over);
99         unless ($srch && $over) {
100                 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
101                 warn "$desc not indexed by Xapian\n";
102                 return;
103         }
104         my $mo = { %{$lei->{mset_opt}} };
105         my $mset;
106         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
107         do {
108                 $mset = $srch->mset($mo->{qstr}, $mo);
109                 my $ids = $srch->mset_to_artnums($mset, $mo);
110                 my $ctx = { ids => $ids };
111                 my $i = 0;
112                 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
113                 while ($over->expand_thread($ctx)) {
114                         for my $n (@{$ctx->{xids}}) {
115                                 my $smsg = $over->get_art($n) or next;
116                                 # next if $dd->is_smsg_dup($smsg); TODO
117                                 my $mitem = delete $n2item{$smsg->{num}};
118                                 $each_smsg->($smsg, $mitem);
119                                 # $self->out($buf .= $ORS);
120                                 # $emit_cb->($smsg);
121                         }
122                         @{$ctx->{xids}} = ();
123                 }
124         } while (_mset_more($mset, $mo));
125         $lei->{ovv}->ovv_atexit_child($lei);
126 }
127
128 sub query_mset { # non-parallel for non-"--thread" users
129         my ($self, $lei, $srcs) = @_;
130         my %sig = $lei->atfork_child_wq($self);
131         local @SIG{keys %sig} = values %sig;
132         my $mo = { %{$lei->{mset_opt}} };
133         my $mset;
134         $self->attach_external($_) for @$srcs;
135         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
136         do {
137                 $mset = $self->mset($mo->{qstr}, $mo);
138                 for my $it ($mset->items) {
139                         my $smsg = smsg_for($self, $it) or next;
140                         # next if $dd->is_smsg_dup($smsg);
141                         $each_smsg->($smsg, $it);
142                         # $self->out($buf .= $ORS) if defined $buf;
143                         #$emit_cb->($smsg);
144                 }
145         } while (_mset_more($mset, $mo));
146         $lei->{ovv}->ovv_atexit_child($lei);
147 }
148
149 sub query_done { # PublicInbox::EOFpipe callback
150         my ($lei) = @_;
151         $lei->{ovv}->ovv_end($lei);
152         $lei->dclose;
153 }
154
155 sub do_query {
156         my ($self, $lei_orig, $srcs) = @_;
157         my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
158
159         pipe(my ($eof_wait, $qry_done)) or die "pipe $!";
160         $io[0] = $qry_done; # don't need stdin
161         $io[1]->autoflush(1);
162         $io[2]->autoflush(1);
163         if ($lei->{opt}->{thread}) {
164                 for my $ibxish (@$srcs) {
165                         $self->wq_do('query_thread_mset', \@io, $lei, $ibxish);
166                 }
167         } else {
168                 $self->wq_do('query_mset', \@io, $lei, $srcs);
169         }
170         # TODO
171         for my $rmt (@{$self->{remotes} // []}) {
172                 $self->wq_do('query_thread_mbox', \@io, $lei, $rmt);
173         }
174         @io = ();
175         close $qry_done; # fully closed when children are done
176
177         # query_done will run when query_*mset close $qry_done
178         if ($lei_orig->{sock}) { # watch for client premature exit
179                 require PublicInbox::EOFpipe;
180                 PublicInbox::EOFpipe->new($eof_wait, \&query_done, $lei_orig);
181                 $lei_orig->{lxs} = $self;
182                 $lei_orig->event_step_init;
183         } else {
184                 $self->wq_close;
185                 read($eof_wait, my $buf, 1); # wait for close($lei->{0})
186                 query_done($lei_orig); # may SIGPIPE
187         }
188 }
189
190 sub ipc_atfork_child {
191         my ($self) = @_;
192         $SIG{__WARN__} = sub { syslog('warning', "@_") };
193         $self->SUPER::ipc_atfork_child; # PublicInbox::IPC
194 }
195
196 sub ipc_atfork_prepare {
197         my ($self) = @_;
198         $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&=]);
199         $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
200 }
201
202 1;