]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearchIdx.pm
extsearchidx: set current_info in warning callbacks
[public-inbox.git] / lib / PublicInbox / ExtSearchIdx.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Detached/external index cross inbox search indexing support
5 # read-write counterpart to PublicInbox::ExtSearch
6 #
7 # It's based on the same ideas as public-inbox-v2-format(5) using
8 # over.sqlite3 for dedupe and sharded Xapian.  msgmap.sqlite3 is
9 # missing, so there is no Message-ID conflict resolution, meaning
10 # no NNTP support for now.
11 #
12 # v2 has a 1:1 mapping of index:inbox or msgmap for NNTP support.
13 # This is intended to be an M:N index:inbox mapping, but it'll likely
14 # be 1:N in common practice (M==1)
15
16 package PublicInbox::ExtSearchIdx;
17 use strict;
18 use v5.10.1;
19 use parent qw(PublicInbox::ExtSearch PublicInbox::Lock);
20 use Carp qw(croak carp);
21 use PublicInbox::Search;
22 use PublicInbox::SearchIdx qw(crlf_adjust prepare_stack is_ancestor);
23 use PublicInbox::OverIdx;
24 use PublicInbox::MID qw(mids);
25 use PublicInbox::V2Writable;
26 use PublicInbox::InboxWritable;
27 use PublicInbox::ContentHash qw(content_hash);
28 use PublicInbox::Eml;
29 use File::Spec;
30
31 sub new {
32         my (undef, $dir, $opt) = @_;
33         my $l = $opt->{indexlevel} // 'full';
34         $l !~ $PublicInbox::SearchIdx::INDEXLEVELS and
35                 die "invalid indexlevel=$l\n";
36         $l eq 'basic' and die "E: indexlevel=basic not yet supported\n";
37         my $self = bless {
38                 xpfx => "$dir/ei".PublicInbox::Search::SCHEMA_VERSION,
39                 topdir => $dir,
40                 creat => $opt->{creat},
41                 ibx_map => {}, # (newsgroup//inboxdir) => $ibx
42                 ibx_list => [],
43                 indexlevel => $l,
44                 transact_bytes => 0,
45                 total_bytes => 0,
46                 current_info => '',
47                 parallel => 1,
48                 lock_path => "$dir/ei.lock",
49         }, __PACKAGE__;
50         $self->{shards} = $self->count_shards || nproc_shards($opt->{creat});
51         my $oidx = PublicInbox::OverIdx->new("$self->{xpfx}/over.sqlite3");
52         $oidx->{-no_fsync} = 1 if $opt->{-no_fsync};
53         $self->{oidx} = $oidx;
54         $self
55 }
56
57 sub attach_inbox {
58         my ($self, $ibx) = @_;
59         $ibx = PublicInbox::InboxWritable->new($ibx);
60         my $key = $ibx->eidx_key;
61         if (!$ibx->over || !$ibx->mm) {
62                 warn "W: skipping $key (unindexed)\n";
63                 return;
64         }
65         if (!defined($ibx->uidvalidity)) {
66                 warn "W: skipping $key (no UIDVALIDITY)\n";
67                 return;
68         }
69         my $ibxdir = File::Spec->canonpath($ibx->{inboxdir});
70         if ($ibxdir ne $ibx->{inboxdir}) {
71                 warn "W: `$ibx->{inboxdir}' canonicalized to `$ibxdir'\n";
72                 $ibx->{inboxdir} = $ibxdir;
73         }
74         $ibx = PublicInbox::InboxWritable->new($ibx);
75         $self->{ibx_map}->{$key} //= do {
76                 push @{$self->{ibx_list}}, $ibx;
77                 $ibx;
78         }
79 }
80
81 sub _ibx_attach { # each_inbox callback
82         my ($ibx, $self) = @_;
83         attach_inbox($self, $ibx);
84 }
85
86 sub attach_config {
87         my ($self, $cfg) = @_;
88         $cfg->each_inbox(\&_ibx_attach, $self);
89 }
90
91 sub git_blob_digest ($) {
92         my ($bref) = @_;
93         my $dig = Digest::SHA->new(1); # XXX SHA256 later
94         $dig->add('blob '.length($$bref)."\0");
95         $dig->add($$bref);
96         $dig;
97 }
98
99 sub is_bad_blob ($$$$) {
100         my ($oid, $type, $size, $expect_oid) = @_;
101         if ($type ne 'blob') {
102                 carp "W: $expect_oid is not a blob (type=$type)";
103                 return 1;
104         }
105         croak "BUG: $oid != $expect_oid" if $oid ne $expect_oid;
106         $size == 0 ? 1 : 0; # size == 0 means purged
107 }
108
109 sub do_xpost ($$) {
110         my ($req, $smsg) = @_;
111         my $self = $req->{self};
112         my $docid = $smsg->{num};
113         my $idx = $self->idx_shard($docid);
114         my $oid = $req->{oid};
115         my $xibx = $req->{ibx};
116         my $eml = $req->{eml};
117         if (my $new_smsg = $req->{new_smsg}) { # 'm' on cross-posted message
118                 my $xnum = $req->{xnum};
119                 $self->{oidx}->add_xref3($docid, $xnum, $oid, $xibx->eidx_key);
120                 $idx->shard_add_eidx_info($docid, $oid, $xibx, $eml);
121         } else { # 'd'
122                 $self->{oidx}->remove_xref3($docid, $oid, $xibx->eidx_key);
123                 $idx->shard_remove_eidx_info($docid, $oid, $xibx, $eml);
124         }
125 }
126
127 # called by V2Writable::sync_prepare
128 sub artnum_max { $_[0]->{oidx}->eidx_max }
129
130 sub index_unseen ($) {
131         my ($req) = @_;
132         my $new_smsg = $req->{new_smsg} or die 'BUG: {new_smsg} unset';
133         my $eml = delete $req->{eml};
134         $new_smsg->populate($eml, $req);
135         my $self = $req->{self};
136         my $docid = $self->{oidx}->adj_counter('eidx_docid', '+');
137         $new_smsg->{num} = $docid;
138         my $idx = $self->idx_shard($docid);
139         $self->{oidx}->add_overview($eml, $new_smsg);
140         my $oid = $new_smsg->{blob};
141         my $ibx = delete $req->{ibx} or die 'BUG: {ibx} unset';
142         $self->{oidx}->add_xref3($docid, $req->{xnum}, $oid, $ibx->eidx_key);
143         $idx->index_raw(undef, $eml, $new_smsg, $ibx);
144 }
145
146 sub do_finalize ($) {
147         my ($req) = @_;
148         if (my $indexed = $req->{indexed}) {
149                 do_xpost($req, $_) for @$indexed;
150         } elsif (exists $req->{new_smsg}) { # totally unseen messsage
151                 index_unseen($req);
152         } else {
153                 warn "W: ignoring delete $req->{oid} (not found)\n";
154         }
155 }
156
157 sub do_step ($) { # main iterator for adding messages to the index
158         my ($req) = @_;
159         my $self = $req->{self};
160         while (1) {
161                 if (my $next_arg = $req->{next_arg}) {
162                         if (my $smsg = $self->{oidx}->next_by_mid(@$next_arg)) {
163                                 $req->{cur_smsg} = $smsg;
164                                 $self->git->cat_async($smsg->{blob},
165                                                         \&ck_existing, $req);
166                                 return; # ck_existing calls do_step
167                         }
168                         delete $req->{cur_smsg};
169                         delete $req->{next_arg};
170                 }
171                 my $mid = shift(@{$req->{mids}});
172                 last unless defined $mid;
173                 my ($id, $prev);
174                 $req->{next_arg} = [ $mid, \$id, \$prev ];
175                 # loop again
176         }
177         do_finalize($req);
178 }
179
180 sub _blob_missing ($) { # called when req->{cur_smsg}->{blob} is bad
181         my ($req) = @_;
182         my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
183         my $self = $req->{self};
184         my $xref3 = $self->{oidx}->get_xref3($smsg->{num});
185         my @keep = grep(!/:$smsg->{blob}\z/, @$xref3);
186         if (@keep) {
187                 $keep[0] =~ /:([a-f0-9]{40,}+)\z/ or
188                         die "BUG: xref $keep[0] has no OID";
189                 my $oidhex = $1;
190                 $self->{oidx}->remove_xref3($smsg->{num}, $smsg->{blob});
191                 my $upd = $self->{oidx}->update_blob($smsg, $oidhex);
192                 my $saved = $self->{oidx}->get_art($smsg->{num});
193         } else {
194                 $self->{oidx}->delete_by_num($smsg->{num});
195         }
196 }
197
198 sub ck_existing { # git->cat_async callback
199         my ($bref, $oid, $type, $size, $req) = @_;
200         my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
201         if ($type eq 'missing') {
202                 _blob_missing($req);
203         } elsif (!is_bad_blob($oid, $type, $size, $smsg->{blob})) {
204                 my $cur = PublicInbox::Eml->new($bref);
205                 if (content_hash($cur) eq $req->{chash}) {
206                         push @{$req->{indexed}}, $smsg; # for do_xpost
207                 } # else { index_unseen later }
208         }
209         do_step($req);
210 }
211
212 # is the messages visible in the inbox currently being indexed?
213 # return the number if so
214 sub cur_ibx_xnum ($$) {
215         my ($req, $bref) = @_;
216         my $ibx = $req->{ibx} or die 'BUG: current {ibx} missing';
217
218         # XXX overkill?
219         git_blob_digest($bref)->hexdigest eq $req->{oid} or die
220                 "BUG: blob mismatch $req->{oid}";
221
222         $req->{eml} = PublicInbox::Eml->new($bref);
223         $req->{chash} = content_hash($req->{eml});
224         $req->{mids} = mids($req->{eml});
225         my @q = @{$req->{mids}}; # copy
226         while (defined(my $mid = shift @q)) {
227                 my ($id, $prev);
228                 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
229                         return $x->{num} if $x->{blob} eq $req->{oid};
230                 }
231         }
232         undef;
233 }
234
235 sub index_oid { # git->cat_async callback for 'm'
236         my ($bref, $oid, $type, $size, $req) = @_;
237         return if is_bad_blob($oid, $type, $size, $req->{oid});
238         my $new_smsg = $req->{new_smsg} = bless {
239                 blob => $oid,
240         }, 'PublicInbox::Smsg';
241         $new_smsg->{bytes} = $size + crlf_adjust($$bref);
242         defined($req->{xnum} = cur_ibx_xnum($req, $bref)) or return;
243         do_step($req);
244 }
245
246 sub unindex_oid { # git->cat_async callback for 'd'
247         my ($bref, $oid, $type, $size, $req) = @_;
248         return if is_bad_blob($oid, $type, $size, $req->{oid});
249         return if defined(cur_ibx_xnum($req, $bref)); # was re-added
250         do_step($req);
251 }
252
253 # overrides V2Writable::last_commits, called by sync_ranges via sync_prepare
254 sub last_commits {
255         my ($self, $sync) = @_;
256         my $heads = [];
257         my $ekey = $sync->{ibx}->eidx_key;
258         my $uv = $sync->{ibx}->uidvalidity;
259         for my $i (0..$sync->{epoch_max}) {
260                 $heads->[$i] = $self->{oidx}->eidx_meta("lc-v2:$ekey//$uv;$i");
261         }
262         $heads;
263 }
264
265 sub _sync_inbox ($$$) {
266         my ($self, $opt, $ibx) = @_;
267         my $sync = {
268                 need_checkpoint => \(my $bool = 0),
269                 reindex => $opt->{reindex},
270                 -opt => $opt,
271                 self => $self,
272                 ibx => $ibx,
273         };
274         my $v = $ibx->version;
275         my $ekey = $ibx->eidx_key;
276         if ($v == 2) {
277                 my $epoch_max;
278                 defined($ibx->git_dir_latest(\$epoch_max)) or return;
279                 $sync->{epoch_max} = $epoch_max;
280                 sync_prepare($self, $sync) or return; # fills $sync->{todo}
281         } elsif ($v == 1) {
282                 my $uv = $ibx->uidvalidity;
283                 my $lc = $self->{oidx}->eidx_meta("lc-v1:$ekey//$uv");
284                 my $stk = prepare_stack($sync, $lc ? "$lc..HEAD" : 'HEAD');
285                 my $unit = { stack => $stk, git => $ibx->git };
286                 push @{$sync->{todo}}, $unit;
287         } else {
288                 warn "E: $ekey unsupported inbox version (v$v)\n";
289                 return;
290         }
291         index_todo($self, $sync, $_) for @{$sync->{todo}};
292 }
293
294 sub eidx_sync { # main entry point
295         my ($self, $opt) = @_;
296         $self->idx_init($opt); # acquire lock via V2Writable::_idx_init
297         $self->{oidx}->rethread_prepare($opt);
298
299         my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
300         local $self->{current_info} = '';
301         local $SIG{__WARN__} = sub {
302                 $warn_cb->($self->{current_info}, ': ', @_);
303         };
304         _sync_inbox($self, $opt, $_) for (@{$self->{ibx_list}});
305
306         $self->{oidx}->rethread_done($opt);
307
308         PublicInbox::V2Writable::done($self);
309 }
310
311 sub update_last_commit { # overrides V2Writable
312         my ($self, $sync, $unit, $latest_cmt) = @_;
313         return unless defined $latest_cmt;
314
315         $self->git->async_wait_all;
316         my $ibx = $sync->{ibx} or die 'BUG: {ibx} missing';
317         my $ekey = $ibx->eidx_key;
318         my $uv = $ibx->uidvalidity;
319         my $epoch = $unit->{epoch};
320         my $meta_key;
321         my $v = $ibx->version;
322         if ($v == 2) {
323                 die 'No {epoch} for v2 unit' unless defined $epoch;
324                 $meta_key = "lc-v2:$ekey//$uv;$epoch";
325         } elsif ($v == 1) {
326                 die 'Unexpected {epoch} for v1 unit' if defined $epoch;
327                 $meta_key = "lc-v1:$ekey//$uv";
328         } else {
329                 die "Unsupported inbox version: $v";
330         }
331         my $last = $self->{oidx}->eidx_meta($meta_key);
332         if (defined $last && is_ancestor($unit->{git}, $last, $latest_cmt)) {
333                 my @cmd = (qw(rev-list --count), "$last..$latest_cmt");
334                 chomp(my $n = $unit->{git}->qx(@cmd));
335                 return if $n ne '' && $n == 0;
336         }
337         $self->{oidx}->eidx_meta($meta_key, $latest_cmt);
338 }
339
340 sub idx_init { # similar to V2Writable
341         my ($self, $opt) = @_;
342         return if $self->{idx_shards};
343
344         $self->git->cleanup;
345
346         my $ALL = $self->git->{git_dir}; # ALL.git
347         PublicInbox::Import::init_bare($ALL) unless -d $ALL;
348         my $info_dir = "$ALL/objects/info";
349         my $alt = "$info_dir/alternates";
350         my $mode = 0644;
351         my (%old, @old, %new, @new);
352         if (-e $alt) {
353                 open(my $fh, '<', $alt) or die "open $alt: $!";
354                 $mode = (stat($fh))[2] & 07777;
355                 while (<$fh>) {
356                         push @old, $_ if !$old{$_}++;
357                 }
358         }
359         for my $ibx (@{$self->{ibx_list}}) {
360                 my $line = $ibx->git->{git_dir} . "/objects\n";
361                 next if $old{$line};
362                 $new{$line} = 1;
363                 push @new, $line;
364         }
365         push @old, @new;
366         PublicInbox::V2Writable::write_alternates($info_dir, $mode, \@old);
367         $self->parallel_init($self->{indexlevel});
368         $self->umask_prepare;
369         $self->with_umask(\&PublicInbox::V2Writable::_idx_init, $self, $opt);
370         $self->{oidx}->begin_lazy;
371         $self->{oidx}->eidx_prep;
372 }
373
374 no warnings 'once';
375 *done = \&PublicInbox::V2Writable::done;
376 *umask_prepare = \&PublicInbox::InboxWritable::umask_prepare;
377 *with_umask = \&PublicInbox::InboxWritable::with_umask;
378 *parallel_init = \&PublicInbox::V2Writable::parallel_init;
379 *nproc_shards = \&PublicInbox::V2Writable::nproc_shards;
380 *sync_prepare = \&PublicInbox::V2Writable::sync_prepare;
381 *index_todo = \&PublicInbox::V2Writable::index_todo;
382 *count_shards = \&PublicInbox::V2Writable::count_shards;
383 *atfork_child = \&PublicInbox::V2Writable::atfork_child;
384 *idx_shard = \&PublicInbox::V2Writable::idx_shard;
385
386 1;