]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei q: parallelize Maildir and mbox writing
[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 use PublicInbox::OpPipe;
13 use PublicInbox::Import;
14 use File::Temp 0.19 (); # 0.19 for ->newdir
15 use File::Spec ();
16
17 sub new {
18         my ($class) = @_;
19         PublicInbox::Search::load_xapian();
20         bless {
21                 qp_flags => $PublicInbox::Search::QP_FLAGS |
22                                 PublicInbox::Search::FLAG_PURE_NOT(),
23         }, $class
24 }
25
26 sub attach_external {
27         my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
28
29         if (!$ibxish->can('over') || !$ibxish->over) {
30                 return push(@{$self->{remotes}}, $ibxish)
31         }
32         my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
33         my $srch = $ibxish->search or
34                 return warn("$desc not indexed for Xapian\n");
35         my @shards = $srch->xdb_shards_flat or
36                 return warn("$desc has no Xapian shardsXapian\n");
37
38         if (delete $self->{xdb}) { # XXX: do we need this?
39                 # clobber existing {xdb} if amending
40                 my $expect = delete $self->{nshard};
41                 my $shards = delete $self->{shards_flat};
42                 scalar(@$shards) == $expect or die
43                         "BUG: {nshard}$expect != shards=".scalar(@$shards);
44
45                 my $prev = {};
46                 for my $old_ibxish (@{$self->{shard2ibx}}) {
47                         next if $prev == $old_ibxish;
48                         $prev = $old_ibxish;
49                         my @shards = $old_ibxish->search->xdb_shards_flat;
50                         push @{$self->{shards_flat}}, @shards;
51                 }
52                 my $nr = scalar(@{$self->{shards_flat}});
53                 $nr == $expect or die
54                         "BUG: reloaded $nr shards, expected $expect"
55         }
56         push @{$self->{shards_flat}}, @shards;
57         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
58 }
59
60 # returns a list of local inboxes (or count in scalar context)
61 sub locals {
62         my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
63         values %uniq;
64 }
65
66 # called by PublicInbox::Search::xdb
67 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
68
69 # like over->get_art
70 sub smsg_for {
71         my ($self, $mitem) = @_;
72         # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
73         my $nshard = $self->{nshard};
74         my $docid = $mitem->get_docid;
75         my $shard = ($docid - 1) % $nshard;
76         my $num = int(($docid - 1) / $nshard) + 1;
77         my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
78         $smsg->{docid} = $docid;
79         $smsg;
80 }
81
82 sub recent {
83         my ($self, $qstr, $opt) = @_;
84         $opt //= {};
85         $opt->{relevance} //= -2;
86         $self->mset($qstr //= 'bytes:1..', $opt);
87 }
88
89 sub over {}
90
91 sub _mset_more ($$) {
92         my ($mset, $mo) = @_;
93         my $size = $mset->size;
94         $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
95 }
96
97 sub query_thread_mset { # for --thread
98         my ($self, $lei, $ibxish) = @_;
99         my %sig = $lei->atfork_child_wq($self);
100         local @SIG{keys %sig} = values %sig;
101
102         my ($srch, $over) = ($ibxish->search, $ibxish->over);
103         unless ($srch && $over) {
104                 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
105                 warn "$desc not indexed by Xapian\n";
106                 return;
107         }
108         my $mo = { %{$lei->{mset_opt}} };
109         my $mset;
110         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $ibxish);
111         my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
112         $dedupe->prepare_dedupe;
113         do {
114                 $mset = $srch->mset($mo->{qstr}, $mo);
115                 my $ids = $srch->mset_to_artnums($mset, $mo);
116                 my $ctx = { ids => $ids };
117                 my $i = 0;
118                 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
119                 while ($over->expand_thread($ctx)) {
120                         for my $n (@{$ctx->{xids}}) {
121                                 my $smsg = $over->get_art($n) or next;
122                                 next if $dedupe->is_smsg_dup($smsg);
123                                 my $mitem = delete $n2item{$smsg->{num}};
124                                 $each_smsg->($smsg, $mitem);
125                         }
126                         @{$ctx->{xids}} = ();
127                 }
128         } while (_mset_more($mset, $mo));
129         undef $each_smsg; # drops @io for l2m->{each_smsg_done}
130         $lei->{ovv}->ovv_atexit_child($lei);
131 }
132
133 sub query_mset { # non-parallel for non-"--thread" users
134         my ($self, $lei, $srcs) = @_;
135         my %sig = $lei->atfork_child_wq($self);
136         local @SIG{keys %sig} = values %sig;
137         my $mo = { %{$lei->{mset_opt}} };
138         my $mset;
139         $self->attach_external($_) for @$srcs;
140         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $self);
141         my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
142         $dedupe->prepare_dedupe;
143         do {
144                 $mset = $self->mset($mo->{qstr}, $mo);
145                 for my $it ($mset->items) {
146                         my $smsg = smsg_for($self, $it) or next;
147                         next if $dedupe->is_smsg_dup($smsg);
148                         $each_smsg->($smsg, $it);
149                 }
150         } while (_mset_more($mset, $mo));
151         undef $each_smsg; # drops @io for l2m->{each_smsg_done}
152         $lei->{ovv}->ovv_atexit_child($lei);
153 }
154
155 sub git {
156         my ($self) = @_;
157         my (%seen, @dirs);
158         my $tmp = File::Temp->newdir('lei_xsrch_git-XXXXXXXX', TMPDIR => 1);
159         for my $ibx (@{$self->{shard2ibx} // []}) {
160                 my $d = File::Spec->canonpath($ibx->git->{git_dir});
161                 $seen{$d} //= push @dirs, "$d/objects\n"
162         }
163         my $git_dir = $tmp->dirname;
164         PublicInbox::Import::init_bare($git_dir);
165         my $f = "$git_dir/objects/info/alternates";
166         open my $alt, '>', $f or die "open($f): $!";
167         print $alt @dirs or die "print $f: $!";
168         close $alt or die "close $f: $!";
169         my $git = PublicInbox::Git->new($git_dir);
170         $git->{-tmp} = $tmp;
171         $git;
172 }
173
174 sub query_done { # EOF callback
175         my ($self, $lei) = @_;
176         my $l2m = delete $lei->{l2m};
177         if (my $pids = delete $self->{l2m_pids}) {
178                 my $ipc_worker_reap = $self->can('ipc_worker_reap');
179                 dwaitpid($_, $ipc_worker_reap, $l2m) for @$pids;
180         }
181         $lei->{ovv}->ovv_end($lei);
182         $lei->start_mua if $l2m && !$l2m->lock_free;
183         $lei->dclose;
184 }
185
186 sub start_query { # always runs in main (lei-daemon) process
187         my ($self, $io, $lei, $srcs) = @_;
188         if (my $l2m = $lei->{l2m}) {
189                 $lei->{1} = $io->[1];
190                 $l2m->post_augment($lei);
191                 $io->[1] = delete $lei->{1};
192                 $lei->start_mua($io->[3]) if $l2m->lock_free;
193         }
194         my $remotes = $self->{remotes} // [];
195         if ($lei->{opt}->{thread}) {
196                 for my $ibxish (@$srcs) {
197                         $self->wq_do('query_thread_mset', $io, $lei, $ibxish);
198                 }
199         } else {
200                 $self->wq_do('query_mset', $io, $lei, $srcs);
201         }
202         # TODO
203         for my $rmt (@$remotes) {
204                 $self->wq_do('query_thread_mbox', $io, $lei, $rmt);
205         }
206         close $io->[0]; # qry_status_wr
207         @$io = ();
208 }
209
210 sub query_prepare { # wq_do
211         my ($self, $lei) = @_;
212         my %sig = $lei->atfork_child_wq($self);
213         local @SIG{keys %sig} = values %sig;
214         if (my $l2m = $lei->{l2m}) {
215                 eval { $l2m->do_augment($lei) };
216                 return $lei->fail($@) if $@;
217         }
218         # trigger PublicInbox::OpPipe->event_step
219         my $qry_status_wr = $lei->{0} or
220                 return $lei->fail('BUG: qry_status_wr missing');
221         $qry_status_wr->autoflush(1);
222         print $qry_status_wr '.' or # this should never fail...
223                 return $lei->fail("BUG? print qry_status_wr: $!");
224 }
225
226 sub do_query {
227         my ($self, $lei_orig, $srcs) = @_;
228         my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
229         $io[0] = undef;
230         pipe(my $qry_status_rd, $io[0]) or die "pipe $!";
231
232         $lei_orig->event_step_init; # wait for shutdowns
233         my $op_map = { '' => [ \&query_done, $self, $lei_orig ] };
234         my $in_loop = exists $lei_orig->{sock};
235         my $opp = PublicInbox::OpPipe->new($qry_status_rd, $op_map, $in_loop);
236         my $l2m = $lei->{l2m};
237         if ($l2m) {
238                 $l2m->pre_augment($lei_orig); # may redirect $lei->{1} for mbox
239                 $io[1] = $lei_orig->{1};
240                 $op_map->{'.'} = [ \&start_query, $self, \@io, $lei, $srcs ];
241                 $self->wq_do('query_prepare', \@io, $lei);
242                 $opp->event_step if !$in_loop;
243         } else {
244                 start_query($self, \@io, $lei, $srcs);
245         }
246         unless ($in_loop) {
247                 my @pids = $self->wq_close;
248                 # for the $lei->atfork_child_wq PIPE handler:
249                 $op_map->{'!'} = [ \&CORE::kill, 'TERM', @pids ];
250                 $opp->event_step;
251                 my $ipc_worker_reap = $self->can('ipc_worker_reap');
252                 if (my $l2m_pids = delete $self->{l2m_pids}) {
253                         dwaitpid($_, $ipc_worker_reap, $l2m) for @$l2m_pids;
254                 }
255                 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
256         }
257 }
258
259 sub ipc_atfork_prepare {
260         my ($self) = @_;
261         # (qry_status_wr, stdout|mbox, stderr, 3: sock, 4: $l2m->{-wq_s1})
262         $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= +<&=]);
263         $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
264 }
265
266 1;