]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
ipc: children don't kill on DESTROY, reduce FD sharing
[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 PublicInbox::DS qw(dwaitpid);
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         my $dd = $lei->{dd};
108         $dd->prepare_dedupe;
109         do {
110                 $mset = $srch->mset($mo->{qstr}, $mo);
111                 my $ids = $srch->mset_to_artnums($mset, $mo);
112                 my $ctx = { ids => $ids };
113                 my $i = 0;
114                 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
115                 while ($over->expand_thread($ctx)) {
116                         for my $n (@{$ctx->{xids}}) {
117                                 my $smsg = $over->get_art($n) or next;
118                                 next if $dd->is_smsg_dup($smsg);
119                                 my $mitem = delete $n2item{$smsg->{num}};
120                                 $each_smsg->($smsg, $mitem);
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         my $dd = $lei->{dd};
137         $dd->prepare_dedupe;
138         do {
139                 $mset = $self->mset($mo->{qstr}, $mo);
140                 for my $it ($mset->items) {
141                         my $smsg = smsg_for($self, $it) or next;
142                         next if $dd->is_smsg_dup($smsg);
143                         $each_smsg->($smsg, $it);
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         my $remotes = $self->{remotes} // [];
159         pipe(my ($eof_wait, $qry_done)) or die "pipe $!";
160         $io[0] = $qry_done; # don't need stdin
161
162         if ($lei->{opt}->{thread}) {
163                 $lei->{-parallel} = scalar(@$remotes) + scalar(@$srcs) - 1;
164                 for my $ibxish (@$srcs) {
165                         $self->wq_do('query_thread_mset', \@io, $lei, $ibxish);
166                 }
167         } else {
168                 $lei->{-parallel} = scalar(@$remotes);
169                 $self->wq_do('query_mset', \@io, $lei, $srcs);
170         }
171         # TODO
172         for my $rmt (@$remotes) {
173                 $self->wq_do('query_thread_mbox', \@io, $lei, $rmt);
174         }
175         @io = ();
176         close $qry_done; # fully closed when children are done
177
178         # query_done will run when query_*mset close $qry_done
179         if ($lei_orig->{sock}) { # watch for client premature exit
180                 require PublicInbox::EOFpipe;
181                 PublicInbox::EOFpipe->new($eof_wait, \&query_done, $lei_orig);
182                 $lei_orig->{lxs} = $self;
183                 $lei_orig->event_step_init;
184         } else {
185                 my @pids = $self->wq_close;
186                 # wait for close($lei->{0})
187                 if (read($eof_wait, my $buf, 1)) {
188                         # if we get a SIGPIPE from one, kill the rest
189                         kill('TERM', @pids) if $buf eq '!';
190                 }
191                 my $ipc_worker_reap = $self->can('ipc_worker_reap');
192                 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
193                 query_done($lei_orig); # may SIGPIPE
194         }
195 }
196
197 sub ipc_atfork_prepare {
198         my ($self) = @_;
199         $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&=]);
200         $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
201 }
202
203 1;