]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # This interface wraps and mimics PublicInbox::Import
5 # Used to write to V2 inboxes (see L<public-inbox-v2-format(5)>).
6 package PublicInbox::V2Writable;
7 use strict;
8 use warnings;
9 use base qw(PublicInbox::Lock);
10 use PublicInbox::SearchIdxShard;
11 use PublicInbox::MIME;
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use PublicInbox::MID qw(mids references);
15 use PublicInbox::ContentId qw(content_id content_digest);
16 use PublicInbox::Inbox;
17 use PublicInbox::OverIdx;
18 use PublicInbox::Msgmap;
19 use PublicInbox::Spawn qw(spawn);
20 use PublicInbox::SearchIdx;
21 use IO::Handle;
22
23 # an estimate of the post-packed size to the raw uncompressed size
24 my $PACKING_FACTOR = 0.4;
25
26 # SATA storage lags behind what CPUs are capable of, so relying on
27 # nproc(1) can be misleading and having extra Xapian shards is a
28 # waste of FDs and space.  It can also lead to excessive IO latency
29 # and slow things down.  Users on NVME or other fast storage can
30 # use the NPROC env or switches in our script/public-inbox-* programs
31 # to increase Xapian shards
32 our $NPROC_MAX_DEFAULT = 4;
33
34 sub nproc_shards ($) {
35         my ($creat_opt) = @_;
36         if (ref($creat_opt) eq 'HASH') {
37                 if (defined(my $n = $creat_opt->{nproc})) {
38                         return $n
39                 }
40         }
41
42         my $n = $ENV{NPROC};
43         if (!$n) {
44                 chomp($n = `nproc 2>/dev/null`);
45                 # assume 2 cores if GNU nproc(1) is not available
46                 $n = 2 if !$n;
47                 $n = $NPROC_MAX_DEFAULT if $n > $NPROC_MAX_DEFAULT;
48         }
49
50         # subtract for the main process and git-fast-import
51         $n -= 1;
52         $n < 1 ? 1 : $n;
53 }
54
55 sub count_shards ($) {
56         my ($self) = @_;
57         my $n = 0;
58         my $xpfx = $self->{xpfx};
59
60         # always load existing shards in case core count changes:
61         # Also, shard count may change while -watch is running
62         # due to "xcpdb --reshard"
63         if (-d $xpfx) {
64                 foreach my $shard (<$xpfx/*>) {
65                         -d $shard && $shard =~ m!/[0-9]+\z! or next;
66                         eval {
67                                 Search::Xapian::Database->new($shard)->close;
68                                 $n++;
69                         };
70                 }
71         }
72         $n;
73 }
74
75 sub new {
76         # $creat may be any true value, or 0/undef.  A hashref is true,
77         # and $creat->{nproc} may be set to an integer
78         my ($class, $v2ibx, $creat) = @_;
79         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
80         unless (-d $dir) {
81                 if ($creat) {
82                         require File::Path;
83                         File::Path::mkpath($dir);
84                 } else {
85                         die "$dir does not exist\n";
86                 }
87         }
88
89         $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
90         $v2ibx->umask_prepare;
91
92         my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
93         my $self = {
94                 -inbox => $v2ibx,
95                 im => undef, #  PublicInbox::Import
96                 parallel => 1,
97                 transact_bytes => 0,
98                 current_info => '',
99                 xpfx => $xpfx,
100                 over => PublicInbox::OverIdx->new("$xpfx/over.sqlite3", 1),
101                 lock_path => "$dir/inbox.lock",
102                 # limit each git repo (epoch) to 1GB or so
103                 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
104                 last_commit => [], # git repo -> commit
105         };
106         $self->{shards} = count_shards($self) || nproc_shards($creat);
107         bless $self, $class;
108 }
109
110 # public (for now?)
111 sub init_inbox {
112         my ($self, $parallel, $skip_epoch) = @_;
113         $self->{parallel} = $parallel;
114         $self->idx_init;
115         my $epoch_max = -1;
116         git_dir_latest($self, \$epoch_max);
117         if (defined $skip_epoch && $epoch_max == -1) {
118                 $epoch_max = $skip_epoch;
119         }
120         $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
121         $self->done;
122 }
123
124 # returns undef on duplicate or spam
125 # mimics Import::add and wraps it for v2
126 sub add {
127         my ($self, $mime, $check_cb) = @_;
128         $self->{-inbox}->with_umask(sub {
129                 _add($self, $mime, $check_cb)
130         });
131 }
132
133 # indexes a message, returns true if checkpointing is needed
134 sub do_idx ($$$$$$$) {
135         my ($self, $msgref, $mime, $len, $num, $oid, $mid0) = @_;
136         $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
137         my $idx = idx_shard($self, $num % $self->{shards});
138         $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
139         my $n = $self->{transact_bytes} += $len;
140         $n >= (PublicInbox::SearchIdx::BATCH_BYTES * $self->{shards});
141 }
142
143 sub _add {
144         my ($self, $mime, $check_cb) = @_;
145
146         # spam check:
147         if ($check_cb) {
148                 $mime = $check_cb->($mime) or return;
149         }
150
151         # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
152         # as does SQLite 3.4.1+ (released in 2007-07-20), and
153         # Xapian 1.3.2+ (released 2015-03-15).
154         # For the most part, we can spawn git-fast-import without
155         # leaking FDs to it...
156         $self->idx_init;
157
158         my $mid0;
159         my $num = num_for($self, $mime, \$mid0);
160         defined $num or return; # duplicate
161         defined $mid0 or die "BUG: $mid0 undefined\n";
162         my $im = $self->importer;
163         my $cmt = $im->add($mime);
164         $cmt = $im->get_mark($cmt);
165         $self->{last_commit}->[$self->{epoch_max}] = $cmt;
166
167         my ($oid, $len, $msgref) = @{$im->{last_object}};
168         if (do_idx($self, $msgref, $mime, $len, $num, $oid, $mid0)) {
169                 $self->checkpoint;
170         }
171
172         $cmt;
173 }
174
175 sub num_for {
176         my ($self, $mime, $mid0) = @_;
177         my $mids = mids($mime->header_obj);
178         if (@$mids) {
179                 my $mid = $mids->[0];
180                 my $num = $self->{mm}->mid_insert($mid);
181                 if (defined $num) { # common case
182                         $$mid0 = $mid;
183                         return $num;
184                 };
185
186                 # crap, Message-ID is already known, hope somebody just resent:
187                 foreach my $m (@$mids) {
188                         # read-only lookup now safe to do after above barrier
189                         my $existing = lookup_content($self, $mime, $m);
190                         # easy, don't store duplicates
191                         # note: do not add more diagnostic info here since
192                         # it gets noisy on public-inbox-watch restarts
193                         return if $existing;
194                 }
195
196                 # AltId may pre-populate article numbers (e.g. X-Mail-Count
197                 # or NNTP article number), use that article number if it's
198                 # not in Over.
199                 my $altid = $self->{-inbox}->{altid};
200                 if ($altid && grep(/:file=msgmap\.sqlite3\z/, @$altid)) {
201                         my $num = $self->{mm}->num_for($mid);
202
203                         if (defined $num && !$self->{over}->get_art($num)) {
204                                 $$mid0 = $mid;
205                                 return $num;
206                         }
207                 }
208
209                 # very unlikely:
210                 warn "<$mid> reused for mismatched content\n";
211
212                 # try the rest of the mids
213                 for(my $i = $#$mids; $i >= 1; $i--) {
214                         my $m = $mids->[$i];
215                         $num = $self->{mm}->mid_insert($m);
216                         if (defined $num) {
217                                 warn "alternative <$m> for <$mid> found\n";
218                                 $$mid0 = $m;
219                                 return $num;
220                         }
221                 }
222         }
223         # none of the existing Message-IDs are good, generate a new one:
224         num_for_harder($self, $mime, $mid0);
225 }
226
227 sub num_for_harder {
228         my ($self, $mime, $mid0) = @_;
229
230         my $hdr = $mime->header_obj;
231         my $dig = content_digest($mime);
232         $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
233         my $num = $self->{mm}->mid_insert($$mid0);
234         unless (defined $num) {
235                 # it's hard to spoof the last Received: header
236                 my @recvd = $hdr->header_raw('Received');
237                 $dig->add("Received: $_") foreach (@recvd);
238                 $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
239                 $num = $self->{mm}->mid_insert($$mid0);
240
241                 # fall back to a random Message-ID and give up determinism:
242                 until (defined($num)) {
243                         $dig->add(rand);
244                         $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
245                         warn "using random Message-ID <$$mid0> as fallback\n";
246                         $num = $self->{mm}->mid_insert($$mid0);
247                 }
248         }
249         PublicInbox::Import::append_mid($hdr, $$mid0);
250         $num;
251 }
252
253 sub idx_shard {
254         my ($self, $shard_i) = @_;
255         $self->{idx_shards}->[$shard_i];
256 }
257
258 # idempotent
259 sub idx_init {
260         my ($self, $opt) = @_;
261         return if $self->{idx_shards};
262         my $ibx = $self->{-inbox};
263
264         # do not leak read-only FDs to child processes, we only have these
265         # FDs for duplicate detection so they should not be
266         # frequently activated.
267         delete $ibx->{$_} foreach (qw(git mm search));
268
269         my $indexlevel = $ibx->{indexlevel};
270         if ($indexlevel && $indexlevel eq 'basic') {
271                 $self->{parallel} = 0;
272         }
273
274         if ($self->{parallel}) {
275                 pipe(my ($r, $w)) or die "pipe failed: $!";
276                 # pipe for barrier notifications doesn't need to be big,
277                 # 1031: F_SETPIPE_SZ
278                 fcntl($w, 1031, 4096) if $^O eq 'linux';
279                 $self->{bnote} = [ $r, $w ];
280                 $w->autoflush(1);
281         }
282
283         my $over = $self->{over};
284         $ibx->umask_prepare;
285         $ibx->with_umask(sub {
286                 $self->lock_acquire unless ($opt && $opt->{-skip_lock});
287                 $over->create;
288
289                 # xcpdb can change shard count while -watch is idle
290                 my $nshards = count_shards($self);
291                 if ($nshards && $nshards != $self->{shards}) {
292                         $self->{shards} = $nshards;
293                 }
294
295                 # need to create all shards before initializing msgmap FD
296                 my $max = $self->{shards} - 1;
297
298                 # idx_shards must be visible to all forked processes
299                 my $idx = $self->{idx_shards} = [];
300                 for my $i (0..$max) {
301                         push @$idx, PublicInbox::SearchIdxShard->new($self, $i);
302                 }
303
304                 # Now that all subprocesses are up, we can open the FDs
305                 # for SQLite:
306                 my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
307                         "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
308                 $mm->{dbh}->begin_work;
309         });
310 }
311
312 # returns an array mapping [ epoch => latest_commit ]
313 # latest_commit may be undef if nothing was done to that epoch
314 # $replace_map = { $object_id => $strref, ... }
315 sub _replace_oids ($$$) {
316         my ($self, $mime, $replace_map) = @_;
317         $self->done;
318         my $pfx = "$self->{-inbox}->{mainrepo}/git";
319         my $rewrites = []; # epoch => commit
320         my $max = $self->{epoch_max};
321
322         unless (defined($max)) {
323                 defined(my $latest = git_dir_latest($self, \$max)) or return;
324                 $self->{epoch_max} = $max;
325         }
326
327         foreach my $i (0..$max) {
328                 my $git_dir = "$pfx/$i.git";
329                 -d $git_dir or next;
330                 my $git = PublicInbox::Git->new($git_dir);
331                 my $im = $self->import_init($git, 0, 1);
332                 $rewrites->[$i] = $im->replace_oids($mime, $replace_map);
333                 $im->done;
334         }
335         $rewrites;
336 }
337
338 sub content_ids ($) {
339         my ($mime) = @_;
340         my @cids = ( content_id($mime) );
341
342         # Email::MIME->as_string doesn't always round-trip, so we may
343         # use a second content_id
344         my $rt = content_id(PublicInbox::MIME->new(\($mime->as_string)));
345         push @cids, $rt if $cids[0] ne $rt;
346         \@cids;
347 }
348
349 sub content_matches ($$) {
350         my ($cids, $existing) = @_;
351         my $cid = content_id($existing);
352         foreach (@$cids) {
353                 return 1 if $_ eq $cid
354         }
355         0
356 }
357
358 # used for removing or replacing (purging)
359 sub rewrite_internal ($$;$$$) {
360         my ($self, $old_mime, $cmt_msg, $new_mime, $sref) = @_;
361         $self->idx_init;
362         my ($im, $need_reindex, $replace_map);
363         if ($sref) {
364                 $replace_map = {}; # oid => sref
365                 $need_reindex = [] if $new_mime;
366         } else {
367                 $im = $self->importer;
368         }
369         my $over = $self->{over};
370         my $cids = content_ids($old_mime);
371         my $removed;
372         my $mids = mids($old_mime->header_obj);
373
374         # We avoid introducing new blobs into git since the raw content
375         # can be slightly different, so we do not need the user-supplied
376         # message now that we have the mids and content_id
377         $old_mime = undef;
378         my $mark;
379
380         foreach my $mid (@$mids) {
381                 my %gone; # num => [ smsg, raw ]
382                 my ($id, $prev);
383                 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
384                         my $msg = get_blob($self, $smsg);
385                         if (!defined($msg)) {
386                                 warn "broken smsg for $mid\n";
387                                 next; # continue
388                         }
389                         my $orig = $$msg;
390                         my $cur = PublicInbox::MIME->new($msg);
391                         if (content_matches($cids, $cur)) {
392                                 $smsg->{mime} = $cur;
393                                 $gone{$smsg->{num}} = [ $smsg, \$orig ];
394                         }
395                 }
396                 my $n = scalar keys %gone;
397                 next unless $n;
398                 if ($n > 1) {
399                         warn "BUG: multiple articles linked to <$mid>\n",
400                                 join(',', sort keys %gone), "\n";
401                 }
402                 foreach my $num (keys %gone) {
403                         my ($smsg, $orig) = @{$gone{$num}};
404                         # $removed should only be set once assuming
405                         # no bugs in our deduplication code:
406                         $removed = $smsg;
407                         my $oid = $smsg->{blob};
408                         if ($replace_map) {
409                                 $replace_map->{$oid} = $sref;
410                         } else {
411                                 ($mark, undef) = $im->remove($orig, $cmt_msg);
412                         }
413                         $orig = undef;
414                         if ($need_reindex) { # ->replace
415                                 push @$need_reindex, $smsg;
416                         } else { # ->purge or ->remove
417                                 $self->{mm}->num_delete($num);
418                         }
419                         unindex_oid_remote($self, $oid, $mid);
420                 }
421         }
422
423         if (defined $mark) {
424                 my $cmt = $im->get_mark($mark);
425                 $self->{last_commit}->[$self->{epoch_max}] = $cmt;
426         }
427         if ($replace_map && scalar keys %$replace_map) {
428                 my $rewrites = _replace_oids($self, $new_mime, $replace_map);
429                 return { rewrites => $rewrites, need_reindex => $need_reindex };
430         }
431         $removed;
432 }
433
434 # public
435 sub remove {
436         my ($self, $mime, $cmt_msg) = @_;
437         $self->{-inbox}->with_umask(sub {
438                 rewrite_internal($self, $mime, $cmt_msg);
439         });
440 }
441
442 sub _replace ($$;$$) {
443         my ($self, $old_mime, $new_mime, $sref) = @_;
444         my $rewritten = $self->{-inbox}->with_umask(sub {
445                 rewrite_internal($self, $old_mime, undef, $new_mime, $sref);
446         }) or return;
447
448         my $rewrites = $rewritten->{rewrites};
449         # ->done is called if there are rewrites since we gc+prune from git
450         $self->idx_init if @$rewrites;
451
452         for my $i (0..$#$rewrites) {
453                 defined(my $cmt = $rewrites->[$i]) or next;
454                 $self->{last_commit}->[$i] = $cmt;
455         }
456         $rewritten;
457 }
458
459 # public
460 sub purge {
461         my ($self, $mime) = @_;
462         my $rewritten = _replace($self, $mime, undef, \'') or return;
463         $rewritten->{rewrites}
464 }
465
466 # returns the git object_id of $fh, does not write the object to FS
467 sub git_hash_raw ($$) {
468         my ($self, $raw) = @_;
469         # grab the expected OID we have to reindex:
470         open my $tmp_fh, '+>', undef or die "failed to open tmp: $!";
471         $tmp_fh->autoflush(1);
472         print $tmp_fh $$raw or die "print \$tmp_fh: $!";
473         sysseek($tmp_fh, 0, 0) or die "seek failed: $!";
474
475         my ($r, $w);
476         pipe($r, $w) or die "failed to create pipe: $!";
477         my $rdr = { 0 => fileno($tmp_fh), 1 => fileno($w) };
478         my $git_dir = $self->{-inbox}->git->{git_dir};
479         my $cmd = ['git', "--git-dir=$git_dir", qw(hash-object --stdin)];
480         my $pid = spawn($cmd, undef, $rdr);
481         close $w;
482         local $/ = "\n";
483         chomp(my $oid = <$r>);
484         waitpid($pid, 0) == $pid or die "git hash-object did not finish";
485         die "git hash-object failed: $?" if $?;
486         $oid =~ /\A[a-f0-9]{40}\z/ or die "OID not expected: $oid";
487         $oid;
488 }
489
490 sub _check_mids_match ($$$) {
491         my ($old_list, $new_list, $hdrs) = @_;
492         my %old_mids = map { $_ => 1 } @$old_list;
493         my %new_mids = map { $_ => 1 } @$new_list;
494         my @old = keys %old_mids;
495         my @new = keys %new_mids;
496         my $err = "$hdrs may not be changed when replacing\n";
497         die $err if scalar(@old) != scalar(@new);
498         delete @new_mids{@old};
499         delete @old_mids{@new};
500         die $err if (scalar(keys %old_mids) || scalar(keys %new_mids));
501 }
502
503 # Changing Message-IDs or References with ->replace isn't supported.
504 # The rules for dealing with messages with multiple or conflicting
505 # Message-IDs are pretty complex and rethreading hasn't been fully
506 # implemented, yet.
507 sub check_mids_match ($$) {
508         my ($old_mime, $new_mime) = @_;
509         my $old = $old_mime->header_obj;
510         my $new = $new_mime->header_obj;
511         _check_mids_match(mids($old), mids($new), 'Message-ID(s)');
512         _check_mids_match(references($old), references($new),
513                         'References/In-Reply-To');
514 }
515
516 # public
517 sub replace ($$$) {
518         my ($self, $old_mime, $new_mime) = @_;
519
520         check_mids_match($old_mime, $new_mime);
521
522         # mutt will always add Content-Length:, Status:, Lines: when editing
523         PublicInbox::Import::drop_unwanted_headers($new_mime);
524
525         my $raw = $new_mime->as_string;
526         my $expect_oid = git_hash_raw($self, \$raw);
527         my $rewritten = _replace($self, $old_mime, $new_mime, \$raw) or return;
528         my $need_reindex = $rewritten->{need_reindex};
529
530         # just in case we have bugs in deduplication code:
531         my $n = scalar(@$need_reindex);
532         if ($n > 1) {
533                 my $list = join(', ', map {
534                                         "$_->{num}: <$_->{mid}>"
535                                 } @$need_reindex);
536                 warn <<"";
537 W: rewritten $n messages matching content of original message (expected: 1).
538 W: possible bug in public-inbox, NNTP article IDs and Message-IDs follow:
539 W: $list
540
541         }
542
543         # make sure we really got the OID:
544         my ($oid, $type, $len) = $self->{-inbox}->git->check($expect_oid);
545         $oid eq $expect_oid or die "BUG: $expect_oid not found after replace";
546
547         # don't leak FDs to Xapian:
548         $self->{-inbox}->git->cleanup;
549
550         # reindex modified messages:
551         for my $smsg (@$need_reindex) {
552                 my $num = $smsg->{num};
553                 my $mid0 = $smsg->{mid};
554                 do_idx($self, \$raw, $new_mime, $len, $num, $oid, $mid0);
555         }
556         $rewritten->{rewrites};
557 }
558
559 sub last_epoch_commit ($$;$) {
560         my ($self, $i, $cmt) = @_;
561         my $v = PublicInbox::Search::SCHEMA_VERSION();
562         $self->{mm}->last_commit_xap($v, $i, $cmt);
563 }
564
565 sub set_last_commits ($) {
566         my ($self) = @_;
567         defined(my $epoch_max = $self->{epoch_max}) or return;
568         my $last_commit = $self->{last_commit};
569         foreach my $i (0..$epoch_max) {
570                 defined(my $cmt = $last_commit->[$i]) or next;
571                 $last_commit->[$i] = undef;
572                 last_epoch_commit($self, $i, $cmt);
573         }
574 }
575
576 sub barrier_init {
577         my ($self, $n) = @_;
578         $self->{bnote} or return;
579         --$n;
580         my $barrier = { map { $_ => 1 } (0..$n) };
581 }
582
583 sub barrier_wait {
584         my ($self, $barrier) = @_;
585         my $bnote = $self->{bnote} or return;
586         my $r = $bnote->[0];
587         while (scalar keys %$barrier) {
588                 defined(my $l = $r->getline) or die "EOF on barrier_wait: $!";
589                 $l =~ /\Abarrier (\d+)/ or die "bad line on barrier_wait: $l";
590                 delete $barrier->{$1} or die "bad shard[$1] on barrier wait";
591         }
592 }
593
594 # public
595 sub checkpoint ($;$) {
596         my ($self, $wait) = @_;
597
598         if (my $im = $self->{im}) {
599                 if ($wait) {
600                         $im->barrier;
601                 } else {
602                         $im->checkpoint;
603                 }
604         }
605         my $shards = $self->{idx_shards};
606         if ($shards) {
607                 my $dbh = $self->{mm}->{dbh};
608
609                 # SQLite msgmap data is second in importance
610                 $dbh->commit;
611
612                 # SQLite overview is third
613                 $self->{over}->commit_lazy;
614
615                 # Now deal with Xapian
616                 if ($wait) {
617                         my $barrier = $self->barrier_init(scalar @$shards);
618
619                         # each shard needs to issue a barrier command
620                         $_->remote_barrier for @$shards;
621
622                         # wait for each Xapian shard
623                         $self->barrier_wait($barrier);
624                 } else {
625                         $_->remote_commit for @$shards;
626                 }
627
628                 # last_commit is special, don't commit these until
629                 # remote shards are done:
630                 $dbh->begin_work;
631                 set_last_commits($self);
632                 $dbh->commit;
633
634                 $dbh->begin_work;
635         }
636         $self->{transact_bytes} = 0;
637 }
638
639 # issue a write barrier to ensure all data is visible to other processes
640 # and read-only ops.  Order of data importance is: git > SQLite > Xapian
641 # public
642 sub barrier { checkpoint($_[0], 1) };
643
644 # public
645 sub done {
646         my ($self) = @_;
647         my $im = delete $self->{im};
648         $im->done if $im; # PublicInbox::Import::done
649         checkpoint($self);
650         my $mm = delete $self->{mm};
651         $mm->{dbh}->commit if $mm;
652         my $shards = delete $self->{idx_shards};
653         if ($shards) {
654                 $_->remote_close for @$shards;
655         }
656         $self->{over}->disconnect;
657         delete $self->{bnote};
658         $self->{transact_bytes} = 0;
659         $self->lock_release if $shards;
660         $self->{-inbox}->git->cleanup;
661 }
662
663 sub fill_alternates ($$) {
664         my ($self, $epoch) = @_;
665
666         my $pfx = "$self->{-inbox}->{mainrepo}/git";
667         my $all = "$self->{-inbox}->{mainrepo}/all.git";
668         my @cmd;
669         unless (-d $all) {
670                 PublicInbox::Import::init_bare($all);
671         }
672         @cmd = (qw/git config/, "--file=$pfx/$epoch.git/config",
673                         'include.path', '../../all.git/config');
674         PublicInbox::Import::run_die(\@cmd);
675
676         my $alt = "$all/objects/info/alternates";
677         my %alts;
678         my @add;
679         if (-e $alt) {
680                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
681                 %alts = map { chomp; $_ => 1 } (<$fh>);
682         }
683         foreach my $i (0..$epoch) {
684                 my $dir = "../../git/$i.git/objects";
685                 push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
686         }
687         return unless @add;
688         open my $fh, '>>', $alt or die "open >> $alt: $!\n";
689         foreach my $dir (@add) {
690                 print $fh "$dir\n" or die "print >> $alt: $!\n";
691         }
692         close $fh or die "close $alt: $!\n";
693 }
694
695 sub git_init {
696         my ($self, $epoch) = @_;
697         my $git_dir = "$self->{-inbox}->{mainrepo}/git/$epoch.git";
698         my @cmd = (qw(git init --bare -q), $git_dir);
699         PublicInbox::Import::run_die(\@cmd);
700         fill_alternates($self, $epoch);
701         $git_dir
702 }
703
704 sub git_dir_latest {
705         my ($self, $max) = @_;
706         $$max = -1;
707         my $pfx = "$self->{-inbox}->{mainrepo}/git";
708         return unless -d $pfx;
709         my $latest;
710         opendir my $dh, $pfx or die "opendir $pfx: $!\n";
711         while (defined(my $git_dir = readdir($dh))) {
712                 $git_dir =~ m!\A([0-9]+)\.git\z! or next;
713                 if ($1 > $$max) {
714                         $$max = $1;
715                         $latest = "$pfx/$git_dir";
716                 }
717         }
718         $latest;
719 }
720
721 sub importer {
722         my ($self) = @_;
723         my $im = $self->{im};
724         if ($im) {
725                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
726                         return $im;
727                 } else {
728                         $self->{im} = undef;
729                         $im->done;
730                         $im = undef;
731                         $self->checkpoint;
732                         my $git_dir = $self->git_init(++$self->{epoch_max});
733                         my $git = PublicInbox::Git->new($git_dir);
734                         return $self->import_init($git, 0);
735                 }
736         }
737         my $epoch = 0;
738         my $max;
739         my $latest = git_dir_latest($self, \$max);
740         if (defined $latest) {
741                 my $git = PublicInbox::Git->new($latest);
742                 my $packed_bytes = $git->packed_bytes;
743                 my $unpacked_bytes = $packed_bytes / $PACKING_FACTOR;
744
745                 if ($unpacked_bytes >= $self->{rotate_bytes}) {
746                         $epoch = $max + 1;
747                 } else {
748                         $self->{epoch_max} = $max;
749                         return $self->import_init($git, $packed_bytes);
750                 }
751         }
752         $self->{epoch_max} = $epoch;
753         $latest = $self->git_init($epoch);
754         $self->import_init(PublicInbox::Git->new($latest), 0);
755 }
756
757 sub import_init {
758         my ($self, $git, $packed_bytes, $tmp) = @_;
759         my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
760         $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
761         $im->{want_object_info} = 1;
762         $im->{lock_path} = undef;
763         $im->{path_type} = 'v2';
764         $self->{im} = $im unless $tmp;
765         $im;
766 }
767
768 # XXX experimental
769 sub diff ($$$) {
770         my ($mid, $cur, $new) = @_;
771         use File::Temp qw(tempfile);
772
773         my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
774         print $ah $cur->as_string or die "print: $!";
775         close $ah or die "close: $!";
776         my ($bh, $bn) = tempfile('email-new-XXXXXXXX', TMPDIR => 1);
777         PublicInbox::Import::drop_unwanted_headers($new);
778         print $bh $new->as_string or die "print: $!";
779         close $bh or die "close: $!";
780         my $cmd = [ qw(diff -u), $an, $bn ];
781         print STDERR "# MID conflict <$mid>\n";
782         my $pid = spawn($cmd, undef, { 1 => 2 });
783         defined $pid or die "diff failed to spawn $!";
784         waitpid($pid, 0) == $pid or die "diff did not finish";
785         unlink($an, $bn);
786 }
787
788 sub get_blob ($$) {
789         my ($self, $smsg) = @_;
790         if (my $im = $self->{im}) {
791                 my $msg = $im->cat_blob($smsg->{blob});
792                 return $msg if $msg;
793         }
794         # older message, should be in alternates
795         my $ibx = $self->{-inbox};
796         $ibx->msg_by_smsg($smsg);
797 }
798
799 sub lookup_content ($$$) {
800         my ($self, $mime, $mid) = @_;
801         my $over = $self->{over};
802         my $cids = content_ids($mime);
803         my ($id, $prev);
804         while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
805                 my $msg = get_blob($self, $smsg);
806                 if (!defined($msg)) {
807                         warn "broken smsg for $mid\n";
808                         next;
809                 }
810                 my $cur = PublicInbox::MIME->new($msg);
811                 if (content_matches($cids, $cur)) {
812                         $smsg->{mime} = $cur;
813                         return $smsg;
814                 }
815
816
817                 # XXX DEBUG_DIFF is experimental and may be removed
818                 diff($mid, $cur, $mime) if $ENV{DEBUG_DIFF};
819         }
820         undef;
821 }
822
823 sub atfork_child {
824         my ($self) = @_;
825         my $fh = delete $self->{reindex_pipe};
826         close $fh if $fh;
827         if (my $shards = $self->{idx_shards}) {
828                 $_->atfork_child foreach @$shards;
829         }
830         if (my $im = $self->{im}) {
831                 $im->atfork_child;
832         }
833         die "unexpected mm" if $self->{mm};
834         close $self->{bnote}->[0] or die "close bnote[0]: $!\n";
835         $self->{bnote}->[1];
836 }
837
838 sub mark_deleted ($$$$) {
839         my ($self, $sync, $git, $oid) = @_;
840         my $msgref = $git->cat_file($oid);
841         my $mime = PublicInbox::MIME->new($$msgref);
842         my $mids = mids($mime->header_obj);
843         my $cid = content_id($mime);
844         foreach my $mid (@$mids) {
845                 $sync->{D}->{"$mid\0$cid"} = $oid;
846         }
847 }
848
849 sub reindex_oid ($$$$) {
850         my ($self, $sync, $git, $oid) = @_;
851         my $len;
852         my $msgref = $git->cat_file($oid, \$len);
853         my $mime = PublicInbox::MIME->new($$msgref);
854         my $mids = mids($mime->header_obj);
855         my $cid = content_id($mime);
856
857         # get the NNTP article number we used before, highest number wins
858         # and gets deleted from sync->{mm_tmp};
859         my $mid0;
860         my $num = -1;
861         my $del = 0;
862         foreach my $mid (@$mids) {
863                 $del += delete($sync->{D}->{"$mid\0$cid"}) ? 1 : 0;
864                 my $n = $sync->{mm_tmp}->num_for($mid);
865                 if (defined $n && $n > $num) {
866                         $mid0 = $mid;
867                         $num = $n;
868                         $self->{mm}->mid_set($num, $mid0);
869                 }
870         }
871         if (!defined($mid0) && !$del) {
872                 $num = $sync->{regen}--;
873                 die "BUG: ran out of article numbers\n" if $num <= 0;
874                 my $mm = $self->{mm};
875                 foreach my $mid (reverse @$mids) {
876                         if ($mm->mid_set($num, $mid) == 1) {
877                                 $mid0 = $mid;
878                                 last;
879                         }
880                 }
881                 if (!defined($mid0)) {
882                         my $id = '<' . join('> <', @$mids) . '>';
883                         warn "Message-ID $id unusable for $num\n";
884                         foreach my $mid (@$mids) {
885                                 defined(my $n = $mm->num_for($mid)) or next;
886                                 warn "#$n previously mapped for <$mid>\n";
887                         }
888                 }
889         }
890
891         if (!defined($mid0) || $del) {
892                 if (!defined($mid0) && $del) { # expected for deletes
893                         $num = $sync->{regen}--;
894                         $self->{mm}->num_highwater($num) if !$sync->{reindex};
895                         return
896                 }
897
898                 my $id = '<' . join('> <', @$mids) . '>';
899                 defined($mid0) or
900                         warn "Skipping $id, no article number found\n";
901                 if ($del && defined($mid0)) {
902                         warn "$id was deleted $del " .
903                                 "time(s) but mapped to article #$num\n";
904                 }
905                 return;
906
907         }
908         $sync->{mm_tmp}->mid_delete($mid0) or
909                 die "failed to delete <$mid0> for article #$num\n";
910         $sync->{nr}++;
911         if (do_idx($self, $msgref, $mime, $len, $num, $oid, $mid0)) {
912                 $git->cleanup;
913                 $sync->{mm_tmp}->atfork_prepare;
914                 $self->done; # release lock
915
916                 if (my $pr = $sync->{-opt}->{-progress}) {
917                         my ($bn) = (split('/', $git->{git_dir}))[-1];
918                         $pr->("$bn ".sprintf($sync->{-regen_fmt}, $sync->{nr}));
919                 }
920
921                 # allow -watch or -mda to write...
922                 $self->idx_init; # reacquire lock
923                 $sync->{mm_tmp}->atfork_parent;
924         }
925 }
926
927 # only update last_commit for $i on reindex iff newer than current
928 sub update_last_commit ($$$$) {
929         my ($self, $git, $i, $cmt) = @_;
930         my $last = last_epoch_commit($self, $i);
931         if (defined $last && is_ancestor($git, $last, $cmt)) {
932                 my @cmd = (qw(rev-list --count), "$last..$cmt");
933                 chomp(my $n = $git->qx(@cmd));
934                 return if $n ne '' && $n == 0;
935         }
936         last_epoch_commit($self, $i, $cmt);
937 }
938
939 sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
940
941 sub last_commits ($$) {
942         my ($self, $epoch_max) = @_;
943         my $heads = [];
944         for (my $i = $epoch_max; $i >= 0; $i--) {
945                 $heads->[$i] = last_epoch_commit($self, $i);
946         }
947         $heads;
948 }
949
950 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
951
952 # returns a revision range for git-log(1)
953 sub log_range ($$$$$) {
954         my ($self, $sync, $git, $i, $tip) = @_;
955         my $opt = $sync->{-opt};
956         my $pr = $opt->{-progress} if (($opt->{verbose} || 0) > 1);
957         my $cur = $sync->{ranges}->[$i] or do {
958                 $pr->("$i.git indexing all of $tip") if $pr;
959                 return $tip; # all of it
960         };
961
962         # fast equality check to avoid (v)fork+execve overhead
963         if ($cur eq $tip) {
964                 $sync->{ranges}->[$i] = undef;
965                 return;
966         }
967
968         my $range = "$cur..$tip";
969         $pr->("$i.git checking contiguity... ") if $pr;
970         if (is_ancestor($git, $cur, $tip)) { # common case
971                 $pr->("OK\n") if $pr;
972                 my $n = $git->qx(qw(rev-list --count), $range);
973                 chomp($n);
974                 if ($n == 0) {
975                         $sync->{ranges}->[$i] = undef;
976                         $pr->("$i.git has nothing new\n") if $pr;
977                         return; # nothing to do
978                 }
979                 $pr->("$i.git has $n changes since $cur\n") if $pr;
980         } else {
981                 $pr->("FAIL\n") if $pr;
982                 warn <<"";
983 discontiguous range: $range
984 Rewritten history? (in $git->{git_dir})
985
986                 chomp(my $base = $git->qx('merge-base', $tip, $cur));
987                 if ($base) {
988                         $range = "$base..$tip";
989                         warn "found merge-base: $base\n"
990                 } else {
991                         $range = $tip;
992                         warn "discarding history at $cur\n";
993                 }
994                 warn <<"";
995 reindexing $git->{git_dir} starting at
996 $range
997
998                 $sync->{unindex_range}->{$i} = "$base..$cur";
999         }
1000         $range;
1001 }
1002
1003 sub sync_prepare ($$$) {
1004         my ($self, $sync, $epoch_max) = @_;
1005         my $pr = $sync->{-opt}->{-progress};
1006         my $regen_max = 0;
1007         my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
1008
1009         # reindex stops at the current heads and we later rerun index_sync
1010         # without {reindex}
1011         my $reindex_heads = last_commits($self, $epoch_max) if $sync->{reindex};
1012
1013         for (my $i = $epoch_max; $i >= 0; $i--) {
1014                 die 'BUG: already indexing!' if $self->{reindex_pipe};
1015                 my $git_dir = git_dir_n($self, $i);
1016                 -d $git_dir or next; # missing epochs are fine
1017                 my $git = PublicInbox::Git->new($git_dir);
1018                 if ($reindex_heads) {
1019                         $head = $reindex_heads->[$i] or next;
1020                 }
1021                 chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
1022
1023                 next if $?; # new repo
1024                 my $range = log_range($self, $sync, $git, $i, $tip) or next;
1025                 $sync->{ranges}->[$i] = $range;
1026
1027                 # can't use 'rev-list --count' if we use --diff-filter
1028                 $pr->("$i.git counting $range ... ") if $pr;
1029                 my $n = 0;
1030                 my $fh = $git->popen(qw(log --pretty=tformat:%H
1031                                 --no-notes --no-color --no-renames
1032                                 --diff-filter=AM), $range, '--', 'm');
1033                 ++$n while <$fh>;
1034                 $pr->("$n\n") if $pr;
1035                 $regen_max += $n;
1036         }
1037
1038         return 0 if (!$regen_max && !keys(%{$self->{unindex_range}}));
1039
1040         # reindex should NOT see new commits anymore, if we do,
1041         # it's a problem and we need to notice it via die()
1042         my $pad = length($regen_max) + 1;
1043         $sync->{-regen_fmt} = "% ${pad}u/$regen_max\n";
1044         $sync->{nr} = 0;
1045         return -1 if $sync->{reindex};
1046         $regen_max + $self->{mm}->num_highwater() || 0;
1047 }
1048
1049 sub unindex_oid_remote ($$$) {
1050         my ($self, $oid, $mid) = @_;
1051         $_->remote_remove($oid, $mid) foreach @{$self->{idx_shards}};
1052         $self->{over}->remove_oid($oid, $mid);
1053 }
1054
1055 sub unindex_oid ($$$) {
1056         my ($self, $git, $oid) = @_;
1057         my $msgref = $git->cat_file($oid);
1058         my $mime = PublicInbox::MIME->new($msgref);
1059         my $mids = mids($mime->header_obj);
1060         $mime = $msgref = undef;
1061         my $over = $self->{over};
1062         foreach my $mid (@$mids) {
1063                 my %gone;
1064                 my ($id, $prev);
1065                 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
1066                         $gone{$smsg->{num}} = 1 if $oid eq $smsg->{blob};
1067                         1; # continue
1068                 }
1069                 my $n = scalar keys %gone;
1070                 next unless $n;
1071                 if ($n > 1) {
1072                         warn "BUG: multiple articles linked to $oid\n",
1073                                 join(',',sort keys %gone), "\n";
1074                 }
1075                 foreach my $num (keys %gone) {
1076                         $self->{unindexed}->{$_}++;
1077                         $self->{mm}->num_delete($num);
1078                 }
1079                 unindex_oid_remote($self, $oid, $mid);
1080         }
1081 }
1082
1083 my $x40 = qr/[a-f0-9]{40}/;
1084 sub unindex ($$$$) {
1085         my ($self, $sync, $git, $unindex_range) = @_;
1086         my $un = $self->{unindexed} ||= {}; # num => removal count
1087         my $before = scalar keys %$un;
1088         my @cmd = qw(log --raw -r
1089                         --no-notes --no-color --no-abbrev --no-renames);
1090         my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
1091         while (<$fh>) {
1092                 /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
1093                 unindex_oid($self, $git, $1);
1094         }
1095         delete $self->{reindex_pipe};
1096         $fh = undef;
1097
1098         return unless $sync->{-opt}->{prune};
1099         my $after = scalar keys %$un;
1100         return if $before == $after;
1101
1102         # ensure any blob can not longer be accessed via dumb HTTP
1103         PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
1104                 qw(-c gc.reflogExpire=now gc --prune=all)]);
1105 }
1106
1107 sub sync_ranges ($$$) {
1108         my ($self, $sync, $epoch_max) = @_;
1109         my $reindex = $sync->{reindex};
1110
1111         return last_commits($self, $epoch_max) unless $reindex;
1112         return [] if ref($reindex) ne 'HASH';
1113
1114         my $ranges = $reindex->{from}; # arrayref;
1115         if (ref($ranges) ne 'ARRAY') {
1116                 die 'BUG: $reindex->{from} not an ARRAY';
1117         }
1118         $ranges;
1119 }
1120
1121 sub index_epoch ($$$) {
1122         my ($self, $sync, $i) = @_;
1123
1124         my $git_dir = git_dir_n($self, $i);
1125         die 'BUG: already reindexing!' if $self->{reindex_pipe};
1126         -d $git_dir or return; # missing epochs are fine
1127         fill_alternates($self, $i);
1128         my $git = PublicInbox::Git->new($git_dir);
1129         if (my $unindex_range = delete $sync->{unindex_range}->{$i}) {
1130                 unindex($self, $sync, $git, $unindex_range);
1131         }
1132         defined(my $range = $sync->{ranges}->[$i]) or return;
1133         if (my $pr = $sync->{-opt}->{-progress}) {
1134                 $pr->("$i.git indexing $range\n");
1135         }
1136
1137         my @cmd = qw(log --raw -r --pretty=tformat:%H
1138                         --no-notes --no-color --no-abbrev --no-renames);
1139         my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
1140         my $cmt;
1141         while (<$fh>) {
1142                 chomp;
1143                 $self->{current_info} = "$i.git $_";
1144                 if (/\A$x40$/o && !defined($cmt)) {
1145                         $cmt = $_;
1146                 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
1147                         reindex_oid($self, $sync, $git, $1);
1148                 } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
1149                         mark_deleted($self, $sync, $git, $1);
1150                 }
1151         }
1152         $fh = undef;
1153         delete $self->{reindex_pipe};
1154         update_last_commit($self, $git, $i, $cmt) if defined $cmt;
1155 }
1156
1157 # public, called by public-inbox-index
1158 sub index_sync {
1159         my ($self, $opt) = @_;
1160         $opt ||= {};
1161         my $pr = $opt->{-progress};
1162         my $epoch_max;
1163         my $latest = git_dir_latest($self, \$epoch_max);
1164         return unless defined $latest;
1165         $self->idx_init($opt); # acquire lock
1166         my $sync = {
1167                 D => {}, # "$mid\0$cid" => $oid
1168                 unindex_range => {}, # EPOCH => oid_old..oid_new
1169                 reindex => $opt->{reindex},
1170                 -opt => $opt
1171         };
1172         $sync->{ranges} = sync_ranges($self, $sync, $epoch_max);
1173         $sync->{regen} = sync_prepare($self, $sync, $epoch_max);
1174
1175         if ($sync->{regen}) {
1176                 # tmp_clone seems to fail if inside a transaction, so
1177                 # we rollback here (because we opened {mm} for reading)
1178                 # Note: we do NOT rely on DBI transactions for atomicity;
1179                 # only for batch performance.
1180                 $self->{mm}->{dbh}->rollback;
1181                 $self->{mm}->{dbh}->begin_work;
1182                 $sync->{mm_tmp} = $self->{mm}->tmp_clone;
1183         }
1184
1185         # work backwards through history
1186         for (my $i = $epoch_max; $i >= 0; $i--) {
1187                 index_epoch($self, $sync, $i);
1188         }
1189
1190         # unindex is required for leftovers if "deletes" affect messages
1191         # in a previous fetch+index window:
1192         if (my @leftovers = values %{delete $sync->{D}}) {
1193                 my $git = $self->{-inbox}->git;
1194                 unindex_oid($self, $git, $_) for @leftovers;
1195                 $git->cleanup;
1196         }
1197         $self->done;
1198
1199         if (my $nr = $sync->{nr}) {
1200                 my $pr = $sync->{-opt}->{-progress};
1201                 $pr->('all.git '.sprintf($sync->{-regen_fmt}, $nr)) if $pr;
1202         }
1203
1204         # reindex does not pick up new changes, so we rerun w/o it:
1205         if ($opt->{reindex}) {
1206                 my %again = %$opt;
1207                 $sync = undef;
1208                 delete @again{qw(reindex -skip_lock)};
1209                 index_sync($self, \%again);
1210         }
1211 }
1212
1213 1;