]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
treewide: "require" + "use" cleanup and docs
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 502824c85327bc8c06d52a636c113a7384e23f94..6021de4496a5e08b2504bbf5498a0d0d112427c6 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # This interface wraps and mimics PublicInbox::Import
@@ -18,13 +18,14 @@ use PublicInbox::OverIdx;
 use PublicInbox::Msgmap;
 use PublicInbox::Spawn qw(spawn);
 use PublicInbox::SearchIdx;
-use IO::Handle;
+use IO::Handle; # ->autoflush
+use File::Temp qw(tempfile);
 
 # an estimate of the post-packed size to the raw uncompressed size
 my $PACKING_FACTOR = 0.4;
 
 # SATA storage lags behind what CPUs are capable of, so relying on
-# nproc(1) can be misleading and having extra Xapian partions is a
+# nproc(1) can be misleading and having extra Xapian shards is a
 # waste of FDs and space.  It can also lead to excessive IO latency
 # and slow things down.  Users on NVME or other fast storage can
 # use the NPROC env or switches in our script/public-inbox-* programs
@@ -57,14 +58,17 @@ sub count_shards ($) {
        my $n = 0;
        my $xpfx = $self->{xpfx};
 
-       # always load existing partitions in case core count changes:
+       # always load existing shards in case core count changes:
        # Also, shard count may change while -watch is running
        # due to "xcpdb --reshard"
        if (-d $xpfx) {
+               require PublicInbox::Search;
+               PublicInbox::Search::load_xapian();
+               my $XapianDatabase = $PublicInbox::Search::X{Database};
                foreach my $shard (<$xpfx/*>) {
                        -d $shard && $shard =~ m!/[0-9]+\z! or next;
                        eval {
-                               Search::Xapian::Database->new($shard)->close;
+                               $XapianDatabase->new($shard)->close;
                                $n++;
                        };
                }
@@ -76,7 +80,8 @@ sub new {
        # $creat may be any true value, or 0/undef.  A hashref is true,
        # and $creat->{nproc} may be set to an integer
        my ($class, $v2ibx, $creat) = @_;
-       my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
+       $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
+       my $dir = $v2ibx->assert_usable_dir;
        unless (-d $dir) {
                if ($creat) {
                        require File::Path;
@@ -85,8 +90,6 @@ sub new {
                        die "$dir does not exist\n";
                }
        }
-
-       $v2ibx = PublicInbox::InboxWritable->new($v2ibx);
        $v2ibx->umask_prepare;
 
        my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
@@ -155,8 +158,7 @@ sub _add {
        # leaking FDs to it...
        $self->idx_init;
 
-       my $mid0;
-       my $num = num_for($self, $mime, \$mid0);
+       my ($num, $mid0) = v2_num_for($self, $mime);
        defined $num or return; # duplicate
        defined $mid0 or die "BUG: $mid0 undefined\n";
        my $im = $self->importer;
@@ -172,16 +174,15 @@ sub _add {
        $cmt;
 }
 
-sub num_for {
-       my ($self, $mime, $mid0) = @_;
+sub v2_num_for {
+       my ($self, $mime) = @_;
        my $mids = mids($mime->header_obj);
        if (@$mids) {
                my $mid = $mids->[0];
                my $num = $self->{mm}->mid_insert($mid);
                if (defined $num) { # common case
-                       $$mid0 = $mid;
-                       return $num;
-               };
+                       return ($num, $mid);
+               }
 
                # crap, Message-ID is already known, hope somebody just resent:
                foreach my $m (@$mids) {
@@ -190,7 +191,7 @@ sub num_for {
                        # easy, don't store duplicates
                        # note: do not add more diagnostic info here since
                        # it gets noisy on public-inbox-watch restarts
-                       return if $existing;
+                       return () if $existing;
                }
 
                # AltId may pre-populate article numbers (e.g. X-Mail-Count
@@ -201,8 +202,7 @@ sub num_for {
                        my $num = $self->{mm}->num_for($mid);
 
                        if (defined $num && !$self->{over}->get_art($num)) {
-                               $$mid0 = $mid;
-                               return $num;
+                               return ($num, $mid);
                        }
                }
 
@@ -215,39 +215,38 @@ sub num_for {
                        $num = $self->{mm}->mid_insert($m);
                        if (defined $num) {
                                warn "alternative <$m> for <$mid> found\n";
-                               $$mid0 = $m;
-                               return $num;
+                               return ($num, $m);
                        }
                }
        }
        # none of the existing Message-IDs are good, generate a new one:
-       num_for_harder($self, $mime, $mid0);
+       v2_num_for_harder($self, $mime);
 }
 
-sub num_for_harder {
-       my ($self, $mime, $mid0) = @_;
+sub v2_num_for_harder {
+       my ($self, $mime) = @_;
 
        my $hdr = $mime->header_obj;
        my $dig = content_digest($mime);
-       $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
-       my $num = $self->{mm}->mid_insert($$mid0);
+       my $mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
+       my $num = $self->{mm}->mid_insert($mid0);
        unless (defined $num) {
                # it's hard to spoof the last Received: header
                my @recvd = $hdr->header_raw('Received');
                $dig->add("Received: $_") foreach (@recvd);
-               $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
-               $num = $self->{mm}->mid_insert($$mid0);
+               $mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
+               $num = $self->{mm}->mid_insert($mid0);
 
                # fall back to a random Message-ID and give up determinism:
                until (defined($num)) {
                        $dig->add(rand);
-                       $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
-                       warn "using random Message-ID <$$mid0> as fallback\n";
-                       $num = $self->{mm}->mid_insert($$mid0);
+                       $mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
+                       warn "using random Message-ID <$mid0> as fallback\n";
+                       $num = $self->{mm}->mid_insert($mid0);
                }
        }
-       PublicInbox::Import::append_mid($hdr, $$mid0);
-       $num;
+       PublicInbox::Import::append_mid($hdr, $mid0);
+       ($num, $mid0);
 }
 
 sub idx_shard {
@@ -292,7 +291,7 @@ sub idx_init {
                        $self->{shards} = $nshards;
                }
 
-               # need to create all parts before initializing msgmap FD
+               # need to create all shards before initializing msgmap FD
                my $max = $self->{shards} - 1;
 
                # idx_shards must be visible to all forked processes
@@ -304,7 +303,7 @@ sub idx_init {
                # Now that all subprocesses are up, we can open the FDs
                # for SQLite:
                my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
-                       "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
+                       "$self->{-inbox}->{inboxdir}/msgmap.sqlite3", 1);
                $mm->{dbh}->begin_work;
        });
 }
@@ -315,7 +314,7 @@ sub idx_init {
 sub _replace_oids ($$$) {
        my ($self, $mime, $replace_map) = @_;
        $self->done;
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
+       my $pfx = "$self->{-inbox}->{inboxdir}/git";
        my $rewrites = []; # epoch => commit
        my $max = $self->{epoch_max};
 
@@ -474,7 +473,7 @@ sub git_hash_raw ($$) {
 
        my ($r, $w);
        pipe($r, $w) or die "failed to create pipe: $!";
-       my $rdr = { 0 => fileno($tmp_fh), 1 => fileno($w) };
+       my $rdr = { 0 => $tmp_fh, 1 => $w };
        my $git_dir = $self->{-inbox}->git->{git_dir};
        my $cmd = ['git', "--git-dir=$git_dir", qw(hash-object --stdin)];
        my $pid = spawn($cmd, undef, $rdr);
@@ -556,7 +555,7 @@ W: $list
        $rewritten->{rewrites};
 }
 
-sub last_commit_part ($$;$) {
+sub last_epoch_commit ($$;$) {
        my ($self, $i, $cmt) = @_;
        my $v = PublicInbox::Search::SCHEMA_VERSION();
        $self->{mm}->last_commit_xap($v, $i, $cmt);
@@ -569,7 +568,7 @@ sub set_last_commits ($) {
        foreach my $i (0..$epoch_max) {
                defined(my $cmt = $last_commit->[$i]) or next;
                $last_commit->[$i] = undef;
-               last_commit_part($self, $i, $cmt);
+               last_epoch_commit($self, $i, $cmt);
        }
 }
 
@@ -616,17 +615,17 @@ sub checkpoint ($;$) {
                if ($wait) {
                        my $barrier = $self->barrier_init(scalar @$shards);
 
-                       # each partition needs to issue a barrier command
+                       # each shard needs to issue a barrier command
                        $_->remote_barrier for @$shards;
 
-                       # wait for each Xapian partition
+                       # wait for each Xapian shard
                        $self->barrier_wait($barrier);
                } else {
                        $_->remote_commit for @$shards;
                }
 
                # last_commit is special, don't commit these until
-               # remote partitions are done:
+               # remote shards are done:
                $dbh->begin_work;
                set_last_commits($self);
                $dbh->commit;
@@ -663,16 +662,12 @@ sub done {
 sub fill_alternates ($$) {
        my ($self, $epoch) = @_;
 
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
-       my $all = "$self->{-inbox}->{mainrepo}/all.git";
-       my @cmd;
+       my $pfx = "$self->{-inbox}->{inboxdir}/git";
+       my $all = "$self->{-inbox}->{inboxdir}/all.git";
+
        unless (-d $all) {
                PublicInbox::Import::init_bare($all);
        }
-       @cmd = (qw/git config/, "--file=$pfx/$epoch.git/config",
-                       'include.path', '../../all.git/config');
-       PublicInbox::Import::run_die(\@cmd);
-
        my $alt = "$all/objects/info/alternates";
        my %alts;
        my @add;
@@ -694,9 +689,12 @@ sub fill_alternates ($$) {
 
 sub git_init {
        my ($self, $epoch) = @_;
-       my $git_dir = "$self->{-inbox}->{mainrepo}/git/$epoch.git";
+       my $git_dir = "$self->{-inbox}->{inboxdir}/git/$epoch.git";
        my @cmd = (qw(git init --bare -q), $git_dir);
        PublicInbox::Import::run_die(\@cmd);
+       @cmd = (qw/git config/, "--file=$git_dir/config",
+                       'include.path', '../../all.git/config');
+       PublicInbox::Import::run_die(\@cmd);
        fill_alternates($self, $epoch);
        $git_dir
 }
@@ -704,7 +702,7 @@ sub git_init {
 sub git_dir_latest {
        my ($self, $max) = @_;
        $$max = -1;
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
+       my $pfx = "$self->{-inbox}->{inboxdir}/git";
        return unless -d $pfx;
        my $latest;
        opendir my $dh, $pfx or die "opendir $pfx: $!\n";
@@ -768,7 +766,6 @@ sub import_init {
 # XXX experimental
 sub diff ($$$) {
        my ($mid, $cur, $new) = @_;
-       use File::Temp qw(tempfile);
 
        my ($ah, $an) = tempfile('email-cur-XXXXXXXX', TMPDIR => 1);
        print $ah $cur->as_string or die "print: $!";
@@ -846,103 +843,206 @@ sub mark_deleted ($$$$) {
        }
 }
 
-sub reindex_oid ($$$$) {
-       my ($self, $sync, $git, $oid) = @_;
-       my $len;
+sub reindex_checkpoint ($$$) {
+       my ($self, $sync, $git) = @_;
+
+       $git->cleanup;
+       $sync->{mm_tmp}->atfork_prepare;
+       $self->done; # release lock
+
+       if (my $pr = $sync->{-opt}->{-progress}) {
+               my ($bn) = (split('/', $git->{git_dir}))[-1];
+               $pr->("$bn ".sprintf($sync->{-regen_fmt}, $sync->{nr}));
+       }
+
+       # allow -watch or -mda to write...
+       $self->idx_init; # reacquire lock
+       $sync->{mm_tmp}->atfork_parent;
+}
+
+# only for a few odd messages with multiple Message-IDs
+sub reindex_oid_m ($$$$;$) {
+       my ($self, $sync, $git, $oid, $regen_num) = @_;
+       $self->{current_info} = "multi_mid $oid";
+       my ($num, $mid0, $len);
        my $msgref = $git->cat_file($oid, \$len);
        my $mime = PublicInbox::MIME->new($$msgref);
        my $mids = mids($mime->header_obj);
        my $cid = content_id($mime);
+       die "BUG: reindex_oid_m called for <=1 mids" if scalar(@$mids) <= 1;
 
-       # get the NNTP article number we used before, highest number wins
-       # and gets deleted from sync->{mm_tmp};
-       my $mid0;
-       my $num = -1;
-       my $del = 0;
-       foreach my $mid (@$mids) {
-               $del += delete($sync->{D}->{"$mid\0$cid"}) ? 1 : 0;
-               my $n = $sync->{mm_tmp}->num_for($mid);
-               if (defined $n && $n > $num) {
-                       $mid0 = $mid;
-                       $num = $n;
-                       $self->{mm}->mid_set($num, $mid0);
+       for my $mid (reverse @$mids) {
+               delete($sync->{D}->{"$mid\0$cid"}) and
+                       die "BUG: reindex_oid should handle <$mid> delete";
+       }
+       my $over = $self->{over};
+       for my $mid (reverse @$mids) {
+               ($num, $mid0) = $over->num_mid0_for_oid($oid, $mid);
+               next unless defined $num;
+               if (defined($regen_num) && $regen_num != $num) {
+                       die "BUG: regen(#$regen_num) != over(#$num)";
                }
        }
-       if (!defined($mid0) && !$del) {
-               $num = $sync->{regen}--;
-               die "BUG: ran out of article numbers\n" if $num <= 0;
-               my $mm = $self->{mm};
-               foreach my $mid (reverse @$mids) {
-                       if ($mm->mid_set($num, $mid) == 1) {
-                               $mid0 = $mid;
-                               last;
-                       }
+       unless (defined($num)) {
+               for my $mid (reverse @$mids) {
+                       # is this a number we got before?
+                       my $n = $sync->{mm_tmp}->num_for($mid);
+                       next unless defined $n;
+                       next if defined($regen_num) && $regen_num != $n;
+                       ($num, $mid0) = ($n, $mid);
+                       last;
+               }
+       }
+       if (defined($num)) {
+               $sync->{mm_tmp}->num_delete($num);
+       } elsif (defined $regen_num) {
+               $num = $regen_num;
+               for my $mid (reverse @$mids) {
+                       $self->{mm}->mid_set($num, $mid) == 1 or next;
+                       $mid0 = $mid;
+                       last;
                }
-               if (!defined($mid0)) {
-                       my $id = '<' . join('> <', @$mids) . '>';
-                       warn "Message-ID $id unusable for $num\n";
-                       foreach my $mid (@$mids) {
-                               defined(my $n = $mm->num_for($mid)) or next;
-                               warn "#$n previously mapped for <$mid>\n";
+               unless (defined $mid0) {
+                       warn "E: cannot regen #$num\n";
+                       return;
+               }
+       } else { # fixup bugs in old mirrors on reindex
+               for my $mid (reverse @$mids) {
+                       $num = $self->{mm}->mid_insert($mid);
+                       next unless defined $num;
+                       $mid0 = $mid;
+                       last;
+               }
+               if (defined $mid0) {
+                       if ($sync->{reindex}) {
+                               warn "reindex added #$num <$mid0>\n";
                        }
+               } else {
+                       warn "E: cannot find article #\n";
+                       return;
                }
        }
+       $sync->{nr}++;
+       if (do_idx($self, $msgref, $mime, $len, $num, $oid, $mid0)) {
+               reindex_checkpoint($self, $sync, $git);
+       }
+}
 
-       if (!defined($mid0) || $del) {
-               if (!defined($mid0) && $del) { # expected for deletes
-                       $num = $sync->{regen}--;
-                       $self->{mm}->num_highwater($num) if !$sync->{reindex};
-                       return
+sub check_unindexed ($$$) {
+       my ($self, $num, $mid0) = @_;
+       my $unindexed = $self->{unindexed} // {};
+       my $n = delete($unindexed->{$mid0});
+       defined $n or return;
+       if ($n != $num) {
+               die "BUG: unindexed $n != $num <$mid0>\n";
+       } else {
+               $self->{mm}->mid_set($num, $mid0);
+       }
+}
+
+# reuse Msgmap to store num => oid mapping (rather than num => mid)
+sub multi_mid_q_new () {
+       my ($fh, $fn) = tempfile('multi_mid-XXXXXXX', EXLOCK => 0, TMPDIR => 1);
+       my $multi_mid = PublicInbox::Msgmap->new_file($fn, 1);
+       $multi_mid->{dbh}->do('PRAGMA synchronous = OFF');
+       # for Msgmap->DESTROY:
+       $multi_mid->{tmp_name} = $fn;
+       $multi_mid->{pid} = $$;
+       close $fh or die "failed to close $fn: $!";
+       $multi_mid
+}
+
+sub multi_mid_q_push ($$) {
+       my ($sync, $oid) = @_;
+       my $multi_mid = $sync->{multi_mid} //= multi_mid_q_new();
+       if ($sync->{reindex}) { # no regen on reindex
+               $multi_mid->mid_insert($oid);
+       } else {
+               my $num = $sync->{regen}--;
+               die "BUG: ran out of article numbers" if $num <= 0;
+               $multi_mid->mid_set($num, $oid);
+       }
+}
+
+sub reindex_oid ($$$$) {
+       my ($self, $sync, $git, $oid) = @_;
+       my ($num, $mid0, $len);
+       my $msgref = $git->cat_file($oid, \$len);
+       return if $len == 0; # purged
+       my $mime = PublicInbox::MIME->new($$msgref);
+       my $mids = mids($mime->header_obj);
+       my $cid = content_id($mime);
+
+       if (scalar(@$mids) == 0) {
+               warn "E: $oid has no Message-ID, skipping\n";
+               return;
+       } elsif (scalar(@$mids) == 1) {
+               my $mid = $mids->[0];
+
+               # was the file previously marked as deleted?, skip if so
+               if (delete($sync->{D}->{"$mid\0$cid"})) {
+                       if (!$sync->{reindex}) {
+                               $num = $sync->{regen}--;
+                               $self->{mm}->num_highwater($num);
+                       }
+                       return;
                }
 
-               my $id = '<' . join('> <', @$mids) . '>';
-               defined($mid0) or
-                       warn "Skipping $id, no article number found\n";
-               if ($del && defined($mid0)) {
-                       warn "$id was deleted $del " .
-                               "time(s) but mapped to article #$num\n";
+               # is this a number we got before?
+               $num = $sync->{mm_tmp}->num_for($mid);
+               if (defined $num) {
+                       $mid0 = $mid;
+                       check_unindexed($self, $num, $mid0);
+               } else {
+                       $num = $sync->{regen}--;
+                       die "BUG: ran out of article numbers" if $num <= 0;
+                       if ($self->{mm}->mid_set($num, $mid) != 1) {
+                               warn "E: unable to assign $num => <$mid>\n";
+                               return;
+                       }
+                       $mid0 = $mid;
+               }
+       } else { # multiple MIDs are a weird case:
+               my $del = 0;
+               for (@$mids) {
+                       $del += delete($sync->{D}->{"$_\0$cid"}) // 0;
+               }
+               if ($del) {
+                       unindex_oid_remote($self, $oid, $_) for @$mids;
+                       # do not delete from {mm_tmp}, since another
+                       # single-MID message may use it.
+               } else { # handle them at the end:
+                       multi_mid_q_push($sync, $oid);
                }
                return;
-
        }
        $sync->{mm_tmp}->mid_delete($mid0) or
                die "failed to delete <$mid0> for article #$num\n";
        $sync->{nr}++;
        if (do_idx($self, $msgref, $mime, $len, $num, $oid, $mid0)) {
-               $git->cleanup;
-               $sync->{mm_tmp}->atfork_prepare;
-               $self->done; # release lock
-
-               if (my $pr = $sync->{-opt}->{-progress}) {
-                       my ($bn) = (split('/', $git->{git_dir}))[-1];
-                       $pr->("$bn ".sprintf($sync->{-regen_fmt}, $sync->{nr}));
-               }
-
-               # allow -watch or -mda to write...
-               $self->idx_init; # reacquire lock
-               $sync->{mm_tmp}->atfork_parent;
+               reindex_checkpoint($self, $sync, $git);
        }
 }
 
 # only update last_commit for $i on reindex iff newer than current
 sub update_last_commit ($$$$) {
        my ($self, $git, $i, $cmt) = @_;
-       my $last = last_commit_part($self, $i);
+       my $last = last_epoch_commit($self, $i);
        if (defined $last && is_ancestor($git, $last, $cmt)) {
                my @cmd = (qw(rev-list --count), "$last..$cmt");
                chomp(my $n = $git->qx(@cmd));
                return if $n ne '' && $n == 0;
        }
-       last_commit_part($self, $i, $cmt);
+       last_epoch_commit($self, $i, $cmt);
 }
 
-sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
+sub git_dir_n ($$) { "$_[0]->{-inbox}->{inboxdir}/git/$_[1].git" }
 
 sub last_commits ($$) {
        my ($self, $epoch_max) = @_;
        my $heads = [];
        for (my $i = $epoch_max; $i >= 0; $i--) {
-               $heads->[$i] = last_commit_part($self, $i);
+               $heads->[$i] = last_epoch_commit($self, $i);
        }
        $heads;
 }
@@ -1013,7 +1113,7 @@ sub sync_prepare ($$$) {
        for (my $i = $epoch_max; $i >= 0; $i--) {
                die 'BUG: already indexing!' if $self->{reindex_pipe};
                my $git_dir = git_dir_n($self, $i);
-               -d $git_dir or next; # missing parts are fine
+               -d $git_dir or next; # missing epochs are fine
                my $git = PublicInbox::Git->new($git_dir);
                if ($reindex_heads) {
                        $head = $reindex_heads->[$i] or next;
@@ -1031,6 +1131,7 @@ sub sync_prepare ($$$) {
                                --no-notes --no-color --no-renames
                                --diff-filter=AM), $range, '--', 'm');
                ++$n while <$fh>;
+               close $fh or die "git log failed: \$?=$?";
                $pr->("$n\n") if $pr;
                $regen_max += $n;
        }
@@ -1052,8 +1153,9 @@ sub unindex_oid_remote ($$$) {
        $self->{over}->remove_oid($oid, $mid);
 }
 
-sub unindex_oid ($$$) {
-       my ($self, $git, $oid) = @_;
+sub unindex_oid ($$$;$) {
+       my ($self, $git, $oid, $unindexed) = @_;
+       my $mm = $self->{mm};
        my $msgref = $git->cat_file($oid);
        my $mime = PublicInbox::MIME->new($msgref);
        my $mids = mids($mime->header_obj);
@@ -1073,8 +1175,11 @@ sub unindex_oid ($$$) {
                                join(',',sort keys %gone), "\n";
                }
                foreach my $num (keys %gone) {
-                       $self->{unindexed}->{$_}++;
-                       $self->{mm}->num_delete($num);
+                       if ($unindexed) {
+                               my $mid0 = $mm->mid_for($num);
+                               $unindexed->{$mid0} = $num;
+                       }
+                       $mm->num_delete($num);
                }
                unindex_oid_remote($self, $oid, $mid);
        }
@@ -1083,25 +1188,26 @@ sub unindex_oid ($$$) {
 my $x40 = qr/[a-f0-9]{40}/;
 sub unindex ($$$$) {
        my ($self, $sync, $git, $unindex_range) = @_;
-       my $un = $self->{unindexed} ||= {}; # num => removal count
-       my $before = scalar keys %$un;
+       my $unindexed = $self->{unindexed} ||= {}; # $mid0 => $num
+       my $before = scalar keys %$unindexed;
+       # order does not matter, here:
        my @cmd = qw(log --raw -r
                        --no-notes --no-color --no-abbrev --no-renames);
        my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
        while (<$fh>) {
                /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
-               unindex_oid($self, $git, $1);
+               unindex_oid($self, $git, $1, $unindexed);
        }
        delete $self->{reindex_pipe};
-       $fh = undef;
+       close $fh or die "git log failed: \$?=$?";
 
        return unless $sync->{-opt}->{prune};
-       my $after = scalar keys %$un;
+       my $after = scalar keys %$unindexed;
        return if $before == $after;
 
        # ensure any blob can not longer be accessed via dumb HTTP
        PublicInbox::Import::run_die(['git', "--git-dir=$git->{git_dir}",
-               qw(-c gc.reflogExpire=now gc --prune=all)]);
+               qw(-c gc.reflogExpire=now gc --prune=all --quiet)]);
 }
 
 sub sync_ranges ($$$) {
@@ -1123,7 +1229,7 @@ sub index_epoch ($$$) {
 
        my $git_dir = git_dir_n($self, $i);
        die 'BUG: already reindexing!' if $self->{reindex_pipe};
-       -d $git_dir or return; # missing parts are fine
+       -d $git_dir or return; # missing epochs are fine
        fill_alternates($self, $i);
        my $git = PublicInbox::Git->new($git_dir);
        if (my $unindex_range = delete $sync->{unindex_range}->{$i}) {
@@ -1149,7 +1255,7 @@ sub index_epoch ($$$) {
                        mark_deleted($self, $sync, $git, $1);
                }
        }
-       $fh = undef;
+       close $fh or die "git log failed: \$?=$?";
        delete $self->{reindex_pipe};
        update_last_commit($self, $git, $i, $cmt) if defined $cmt;
 }
@@ -1189,11 +1295,34 @@ sub index_sync {
 
        # unindex is required for leftovers if "deletes" affect messages
        # in a previous fetch+index window:
+       my $git;
        if (my @leftovers = values %{delete $sync->{D}}) {
-               my $git = $self->{-inbox}->git;
-               unindex_oid($self, $git, $_) for @leftovers;
-               $git->cleanup;
+               $git = $self->{-inbox}->git;
+               for my $oid (@leftovers) {
+                       $self->{current_info} = "leftover $oid";
+                       unindex_oid($self, $git, $oid);
+               }
+       }
+       if (my $multi_mid = delete $sync->{multi_mid}) {
+               $git //= $self->{-inbox}->git;
+               my ($min, $max) = $multi_mid->minmax;
+               if ($sync->{reindex}) {
+                       # we may need to create new Message-IDs if mirrors
+                       # were initially indexed with old versions
+                       for (my $i = $max; $i >= $min; $i--) {
+                               my $oid = $multi_mid->mid_for($i);
+                               next unless defined $oid;
+                               reindex_oid_m($self, $sync, $git, $oid);
+                       }
+               } else { # regen on initial index
+                       for my $num ($min..$max) {
+                               my $oid = $multi_mid->mid_for($num);
+                               next unless defined $oid;
+                               reindex_oid_m($self, $sync, $git, $oid, $num);
+                       }
+               }
        }
+       $git->cleanup if $git;
        $self->done;
 
        if (my $nr = $sync->{nr}) {