]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
v2writable: show progress updates for index_sync
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 92d2672c78c478f886a3bd7d5f9cb8c238f3456b..6b01171271475bdf6f082f25c12f7388d1f7f611 100644 (file)
@@ -2,6 +2,7 @@
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # This interface wraps and mimics PublicInbox::Import
+# Used to write to V2 inboxes (see L<public-inbox-v2-format(5)>).
 package PublicInbox::V2Writable;
 use strict;
 use warnings;
@@ -23,7 +24,14 @@ use IO::Handle;
 my $PACKING_FACTOR = 0.4;
 
 # assume 2 cores if GNU nproc(1) is not available
-sub nproc_parts () {
+sub nproc_parts ($) {
+       my ($creat_opt) = @_;
+       if (ref($creat_opt) eq 'HASH') {
+               if (defined(my $n = $creat_opt->{nproc})) {
+                       return $n
+               }
+       }
+
        my $n = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
        # subtract for the main process and git-fast-import
        $n -= 1;
@@ -51,6 +59,8 @@ sub count_partitions ($) {
 }
 
 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";
        unless (-d $dir) {
@@ -71,6 +81,7 @@ sub new {
                im => undef, #  PublicInbox::Import
                parallel => 1,
                transact_bytes => 0,
+               current_info => '',
                xpfx => $xpfx,
                over => PublicInbox::OverIdx->new("$xpfx/over.sqlite3", 1),
                lock_path => "$dir/inbox.lock",
@@ -78,16 +89,20 @@ sub new {
                rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
                last_commit => [], # git repo -> commit
        };
-       $self->{partitions} = count_partitions($self) || nproc_parts();
+       $self->{partitions} = count_partitions($self) || nproc_parts($creat);
        bless $self, $class;
 }
 
+# public (for now?)
 sub init_inbox {
-       my ($self, $parallel) = @_;
+       my ($self, $parallel, $skip_epoch) = @_;
        $self->{parallel} = $parallel;
        $self->idx_init;
        my $epoch_max = -1;
        git_dir_latest($self, \$epoch_max);
+       if (defined $skip_epoch && $epoch_max == -1) {
+               $epoch_max = $skip_epoch;
+       }
        $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
        $self->done;
 }
@@ -153,13 +168,26 @@ sub num_for {
                # crap, Message-ID is already known, hope somebody just resent:
                foreach my $m (@$mids) {
                        # read-only lookup now safe to do after above barrier
-                       my $existing = $self->lookup_content($mime, $m);
+                       my $existing = lookup_content($self, $mime, $m);
                        # 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;
                }
 
+               # AltId may pre-populate article numbers (e.g. X-Mail-Count
+               # or NNTP article number), use that article number if it's
+               # not in Over.
+               my $altid = $self->{-inbox}->{altid};
+               if ($altid && grep(/:file=msgmap\.sqlite3\z/, @$altid)) {
+                       my $num = $self->{mm}->num_for($mid);
+
+                       if (defined $num && !$self->{over}->get_art($num)) {
+                               $$mid0 = $mid;
+                               return $num;
+                       }
+               }
+
                # very unlikely:
                warn "<$mid> reused for mismatched content\n";
 
@@ -211,7 +239,7 @@ sub idx_part {
 
 # idempotent
 sub idx_init {
-       my ($self) = @_;
+       my ($self, $opt) = @_;
        return if $self->{idx_parts};
        my $ibx = $self->{-inbox};
 
@@ -220,16 +248,24 @@ sub idx_init {
        # frequently activated.
        delete $ibx->{$_} foreach (qw(git mm search));
 
-       if ($self->{parallel}) {
-               pipe(my ($r, $w)) or die "pipe failed: $!";
-               $self->{bnote} = [ $r, $w ];
-               $w->autoflush(1);
-       }
+       my $indexlevel = $ibx->{indexlevel};
+       if ($indexlevel && $indexlevel eq 'basic') {
+               $self->{parallel} = 0;
+       }
+
+       if ($self->{parallel}) {
+               pipe(my ($r, $w)) or die "pipe failed: $!";
+               # pipe for barrier notifications doesn't need to be big,
+               # 1031: F_SETPIPE_SZ
+               fcntl($w, 1031, 4096) if $^O eq 'linux';
+               $self->{bnote} = [ $r, $w ];
+               $w->autoflush(1);
+       }
 
        my $over = $self->{over};
        $ibx->umask_prepare;
        $ibx->with_umask(sub {
-               $self->lock_acquire;
+               $self->lock_acquire unless ($opt && $opt->{-skip_lock});
                $over->create;
 
                # -compact can change partition count while -watch is idle
@@ -255,15 +291,24 @@ sub idx_init {
        });
 }
 
-sub purge_oids {
+sub purge_oids ($$) {
        my ($self, $purge) = @_; # $purge = { $object_id => 1, ... }
        $self->done;
        my $pfx = "$self->{-inbox}->{mainrepo}/git";
        my $purges = [];
-       foreach my $i (0..$self->{epoch_max}) {
-               my $git = PublicInbox::Git->new("$pfx/$i.git");
+       my $max = $self->{epoch_max};
+
+       unless (defined($max)) {
+               defined(my $latest = git_dir_latest($self, \$max)) or return;
+               $self->{epoch_max} = $max;
+       }
+       foreach my $i (0..$max) {
+               my $git_dir = "$pfx/$i.git";
+               -d $git_dir or next;
+               my $git = PublicInbox::Git->new($git_dir);
                my $im = $self->import_init($git, 0, 1);
                $purges->[$i] = $im->purge_oids($purge);
+               $im->done;
        }
        $purges;
 }
@@ -288,7 +333,7 @@ sub content_matches ($$) {
        0
 }
 
-sub remove_internal {
+sub remove_internal ($$$$) {
        my ($self, $mime, $cmt_msg, $purge) = @_;
        $self->idx_init;
        my $im = $self->importer unless $purge;
@@ -340,7 +385,7 @@ sub remove_internal {
                                ($mark, undef) = $im->remove($orig, $cmt_msg);
                        }
                        $orig = undef;
-                       $self->unindex_oid_remote($oid, $mid);
+                       unindex_oid_remote($self, $oid, $mid);
                }
        }
 
@@ -354,18 +399,20 @@ sub remove_internal {
        $removed;
 }
 
+# public
 sub remove {
        my ($self, $mime, $cmt_msg) = @_;
        $self->{-inbox}->with_umask(sub {
-               remove_internal($self, $mime, $cmt_msg);
+               remove_internal($self, $mime, $cmt_msg, undef);
        });
 }
 
+# public
 sub purge {
        my ($self, $mime) = @_;
        my $purges = $self->{-inbox}->with_umask(sub {
                remove_internal($self, $mime, undef, {});
-       });
+       }) or return;
        $self->idx_init if @$purges; # ->done is called on purges
        for my $i (0..$#$purges) {
                defined(my $cmt = $purges->[$i]) or next;
@@ -409,6 +456,7 @@ sub barrier_wait {
        }
 }
 
+# public
 sub checkpoint ($;$) {
        my ($self, $wait) = @_;
 
@@ -455,8 +503,10 @@ sub checkpoint ($;$) {
 
 # issue a write barrier to ensure all data is visible to other processes
 # and read-only ops.  Order of data importance is: git > SQLite > Xapian
+# public
 sub barrier { checkpoint($_[0], 1) };
 
+# public
 sub done {
        my ($self) = @_;
        my $im = delete $self->{im};
@@ -472,25 +522,19 @@ sub done {
        delete $self->{bnote};
        $self->{transact_bytes} = 0;
        $self->lock_release if $parts;
+       $self->{-inbox}->git->cleanup;
 }
 
-sub git_init {
+sub fill_alternates ($$) {
        my ($self, $epoch) = @_;
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
-       my $git_dir = "$pfx/$epoch.git";
-       my @cmd = (qw(git init --bare -q), $git_dir);
-       PublicInbox::Import::run_die(\@cmd);
 
+       my $pfx = "$self->{-inbox}->{mainrepo}/git";
        my $all = "$self->{-inbox}->{mainrepo}/all.git";
+       my @cmd;
        unless (-d $all) {
-               @cmd = (qw(git init --bare -q), $all);
-               PublicInbox::Import::run_die(\@cmd);
-               @cmd = (qw/git config/, "--file=$all/config",
-                               'repack.writeBitmaps', 'true');
-               PublicInbox::Import::run_die(\@cmd);
+               PublicInbox::Import::init_bare($all);
        }
-
-       @cmd = (qw/git config/, "--file=$git_dir/config",
+       @cmd = (qw/git config/, "--file=$pfx/$epoch.git/config",
                        'include.path', '../../all.git/config');
        PublicInbox::Import::run_die(\@cmd);
 
@@ -505,12 +549,20 @@ sub git_init {
                my $dir = "../../git/$i.git/objects";
                push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
        }
-       return $git_dir unless @add;
+       return unless @add;
        open my $fh, '>>', $alt or die "open >> $alt: $!\n";
        foreach my $dir (@add) {
                print $fh "$dir\n" or die "print >> $alt: $!\n";
        }
        close $fh or die "close $alt: $!\n";
+}
+
+sub git_init {
+       my ($self, $epoch) = @_;
+       my $git_dir = "$self->{-inbox}->{mainrepo}/git/$epoch.git";
+       my @cmd = (qw(git init --bare -q), $git_dir);
+       PublicInbox::Import::run_die(\@cmd);
+       fill_alternates($self, $epoch);
        $git_dir
 }
 
@@ -553,7 +605,9 @@ sub importer {
        if (defined $latest) {
                my $git = PublicInbox::Git->new($latest);
                my $packed_bytes = $git->packed_bytes;
-               if ($packed_bytes >= $self->{rotate_bytes}) {
+               my $unpacked_bytes = $packed_bytes / $PACKING_FACTOR;
+
+               if ($unpacked_bytes >= $self->{rotate_bytes}) {
                        $epoch = $max + 1;
                } else {
                        $self->{epoch_max} = $max;
@@ -607,7 +661,7 @@ sub get_blob ($$) {
        $ibx->msg_by_smsg($smsg);
 }
 
-sub lookup_content {
+sub lookup_content ($$$) {
        my ($self, $mime, $mid) = @_;
        my $over = $self->{over};
        my $cids = content_ids($mime);
@@ -646,19 +700,19 @@ sub atfork_child {
        $self->{bnote}->[1];
 }
 
-sub mark_deleted {
-       my ($self, $D, $git, $oid) = @_;
+sub mark_deleted ($$$$) {
+       my ($self, $sync, $git, $oid) = @_;
        my $msgref = $git->cat_file($oid);
        my $mime = PublicInbox::MIME->new($$msgref);
        my $mids = mids($mime->header_obj);
        my $cid = content_id($mime);
        foreach my $mid (@$mids) {
-               $D->{"$mid\0$cid"} = $oid;
+               $sync->{D}->{"$mid\0$cid"} = $oid;
        }
 }
 
-sub reindex_oid {
-       my ($self, $mm_tmp, $D, $git, $oid, $regen, $reindex) = @_;
+sub reindex_oid ($$$$) {
+       my ($self, $sync, $git, $oid) = @_;
        my $len;
        my $msgref = $git->cat_file($oid, \$len);
        my $mime = PublicInbox::MIME->new($$msgref);
@@ -666,20 +720,21 @@ sub reindex_oid {
        my $cid = content_id($mime);
 
        # get the NNTP article number we used before, highest number wins
-       # and gets deleted from mm_tmp;
+       # and gets deleted from sync->{mm_tmp};
        my $mid0;
        my $num = -1;
        my $del = 0;
        foreach my $mid (@$mids) {
-               $del += delete($D->{"$mid\0$cid"}) ? 1 : 0;
-               my $n = $mm_tmp->num_for($mid);
+               $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);
                }
        }
-       if (!defined($mid0) && $regen && !$del) {
-               $num = $$regen--;
+       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) {
@@ -700,8 +755,8 @@ sub reindex_oid {
 
        if (!defined($mid0) || $del) {
                if (!defined($mid0) && $del) { # expected for deletes
-                       $num = $$regen--;
-                       $self->{mm}->num_highwater($num) unless $reindex;
+                       $num = $sync->{regen}--;
+                       $self->{mm}->num_highwater($num) if !$sync->{reindex};
                        return
                }
 
@@ -715,7 +770,7 @@ sub reindex_oid {
                return;
 
        }
-       $mm_tmp->mid_delete($mid0) or
+       $sync->{mm_tmp}->mid_delete($mid0) or
                die "failed to delete <$mid0> for article #$num\n";
 
        $self->{over}->add_overview($mime, $len, $num, $oid, $mid0);
@@ -724,18 +779,25 @@ sub reindex_oid {
        my $idx = $self->idx_part($part);
        $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
        my $n = $self->{transact_bytes} += $len;
+       $sync->{nr}++;
        if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
                $git->cleanup;
-               $mm_tmp->atfork_prepare;
+               $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
-               $mm_tmp->atfork_parent;
+               $sync->{mm_tmp}->atfork_parent;
        }
 }
 
 # only update last_commit for $i on reindex iff newer than current
-sub update_last_commit {
+sub update_last_commit ($$$$) {
        my ($self, $git, $i, $cmt) = @_;
        my $last = last_commit_part($self, $i);
        if (defined $last && is_ancestor($git, $last, $cmt)) {
@@ -748,7 +810,7 @@ sub update_last_commit {
 
 sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
 
-sub last_commits {
+sub last_commits ($$) {
        my ($self, $epoch_max) = @_;
        my $heads = [];
        for (my $i = $epoch_max; $i >= 0; $i--) {
@@ -759,69 +821,101 @@ sub last_commits {
 
 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
 
-sub index_prepare {
-       my ($self, $opts, $epoch_max, $ranges) = @_;
+# returns a revision range for git-log(1)
+sub log_range ($$$$$) {
+       my ($self, $sync, $git, $i, $tip) = @_;
+       my $opt = $sync->{-opt};
+       my $pr = $opt->{-progress} if (($opt->{verbose} || 0) > 1);
+       my $cur = $sync->{ranges}->[$i] or do {
+               $pr->("$i.git indexing all of $tip") if $pr;
+               return $tip; # all of it
+       };
+
+       my $range = "$cur..$tip";
+       $pr->("$i.git checking contiguity... ") if $pr;
+       if (is_ancestor($git, $cur, $tip)) { # common case
+               $pr->("OK\n") if $pr;
+               my $n = $git->qx(qw(rev-list --count), $range);
+               chomp($n);
+               if ($n == 0) {
+                       $sync->{ranges}->[$i] = undef;
+                       $pr->("$i.git has nothing new\n") if $pr;
+                       return; # nothing to do
+               }
+               $pr->("$i.git has $n changes since $cur\n") if $pr;
+       } else {
+               $pr->("FAIL\n") if $pr;
+               warn <<"";
+discontiguous range: $range
+Rewritten history? (in $git->{git_dir})
+
+               chomp(my $base = $git->qx('merge-base', $tip, $cur));
+               if ($base) {
+                       $range = "$base..$tip";
+                       warn "found merge-base: $base\n"
+               } else {
+                       $range = $tip;
+                       warn "discarding history at $cur\n";
+               }
+               warn <<"";
+reindexing $git->{git_dir} starting at
+$range
+
+               $sync->{"unindex-range.$i"} = "$base..$cur";
+       }
+       $range;
+}
+
+sub sync_prepare ($$$) {
+       my ($self, $sync, $epoch_max) = @_;
+       my $pr = $sync->{-opt}->{-progress};
        my $regen_max = 0;
        my $head = $self->{-inbox}->{ref_head} || 'refs/heads/master';
+
+       # reindex stops at the current heads and we later rerun index_sync
+       # without {reindex}
+       my $reindex_heads = last_commits($self, $epoch_max) if $sync->{reindex};
+
        for (my $i = $epoch_max; $i >= 0; $i--) {
-               die "already indexing!\n" if $self->{index_pipe};
+               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
                my $git = PublicInbox::Git->new($git_dir);
-               chomp(my $tip = $git->qx('rev-parse', $head));
-               my $range;
-               if (defined(my $cur = $ranges->[$i])) {
-                       $range = "$cur..$tip";
-                       if (is_ancestor($git, $cur, $tip)) { # common case
-                               my $n = $git->qx(qw(rev-list --count), $range);
-                               chomp($n);
-                               if ($n == 0) {
-                                       $ranges->[$i] = undef;
-                                       next;
-                               }
-                       } else {
-                               warn <<"";
-discontiguous range: $range
-Rewritten history? (in $git_dir)
-
-                               my $base = $git->qx('merge-base', $tip, $cur);
-                               chomp $base;
-                               if ($base) {
-                                       $range = "$base..$tip";
-                                       warn "found merge-base: $base\n"
-                               } else {
-                                       $range = $tip;
-                                       warn <<"";
-discarding history at $cur
-
-                               }
-                               warn <<"";
-reindexing $git_dir starting at
-$range
-
-                               $self->{"unindex-range.$i"} = "$base..$cur";
-                       }
-               } else {
-                       $range = $tip; # all of it
+               if ($reindex_heads) {
+                       $head = $reindex_heads->[$i] or next;
                }
-               $ranges->[$i] = $range;
+               chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
+
+               next if $?; # new repo
+               my $range = log_range($self, $sync, $git, $i, $tip) or next;
+               $sync->{ranges}->[$i] = $range;
 
                # can't use 'rev-list --count' if we use --diff-filter
+               $pr->("$i.git counting $range ... ") if $pr;
+               my $n = 0;
                my $fh = $git->popen(qw(log --pretty=tformat:%H
                                --no-notes --no-color --no-renames
                                --diff-filter=AM), $range, '--', 'm');
-               ++$regen_max while <$fh>;
+               ++$n while <$fh>;
+               $pr->("$n\n") if $pr;
+               $regen_max += $n;
        }
-       \$regen_max;
+       # reindex should NOT see new commits anymore, if we do,
+       # it's a problem and we need to notice it via die()
+       my $pad = length($regen_max) + 1;
+       $sync->{-regen_fmt} = "% ${pad}u/$regen_max\n";
+       $sync->{nr} = 0;
+       return -1 if $sync->{reindex};
+       $regen_max + $self->{mm}->num_highwater() || 0;
 }
 
-sub unindex_oid_remote {
+sub unindex_oid_remote ($$$) {
        my ($self, $oid, $mid) = @_;
        $_->remote_remove($oid, $mid) foreach @{$self->{idx_parts}};
        $self->{over}->remove_oid($oid, $mid);
 }
 
-sub unindex_oid {
+sub unindex_oid ($$$) {
        my ($self, $git, $oid) = @_;
        my $msgref = $git->cat_file($oid);
        my $mime = PublicInbox::MIME->new($msgref);
@@ -841,14 +935,17 @@ sub unindex_oid {
                        warn "BUG: multiple articles linked to $oid\n",
                                join(',',sort keys %gone), "\n";
                }
-               $self->{unindexed}->{$_}++ foreach keys %gone;
-               $self->unindex_oid_remote($oid, $mid);
+               foreach my $num (keys %gone) {
+                       $self->{unindexed}->{$_}++;
+                       $self->{mm}->num_delete($num);
+               }
+               unindex_oid_remote($self, $oid, $mid);
        }
 }
 
 my $x40 = qr/[a-f0-9]{40}/;
-sub unindex {
-       my ($self, $opts, $git, $unindex_range) = @_;
+sub unindex ($$$$) {
+       my ($self, $sync, $git, $unindex_range) = @_;
        my $un = $self->{unindexed} ||= {}; # num => removal count
        my $before = scalar keys %$un;
        my @cmd = qw(log --raw -r
@@ -856,12 +953,12 @@ sub unindex {
        my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $unindex_range);
        while (<$fh>) {
                /\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o or next;
-               $self->unindex_oid($git, $1);
+               unindex_oid($self, $git, $1);
        }
        delete $self->{reindex_pipe};
        $fh = undef;
 
-       return unless $opts->{prune};
+       return unless $sync->{-opt}->{prune};
        my $after = scalar keys %$un;
        return if $before == $after;
 
@@ -870,59 +967,89 @@ sub unindex {
                qw(-c gc.reflogExpire=now gc --prune=all)]);
 }
 
+sub sync_ranges ($$$) {
+       my ($self, $sync, $epoch_max) = @_;
+       my $reindex = $sync->{reindex};
+
+       return last_commits($self, $epoch_max) unless $reindex;
+       return [] if ref($reindex) ne 'HASH';
+
+       my $ranges = $reindex->{from}; # arrayref;
+       if (ref($ranges) ne 'ARRAY') {
+               die 'BUG: $reindex->{from} not an ARRAY';
+       }
+       $ranges;
+}
+
+# public, called by public-inbox-index
 sub index_sync {
-       my ($self, $opts) = @_;
-       $opts ||= {};
+       my ($self, $opt) = @_;
+       $opt ||= {};
+       my $pr = $opt->{-progress};
        my $epoch_max;
        my $latest = git_dir_latest($self, \$epoch_max);
        return unless defined $latest;
-       $self->idx_init; # acquire lock
-       my $mm_tmp = $self->{mm}->tmp_clone;
-       my $reindex = $opts->{reindex};
-       my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
-
-       my $high = $self->{mm}->num_highwater();
-       my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
-       $$regen += $high if $high;
-       my $D = {}; # "$mid\0$cid" => $oid
+       $self->idx_init($opt); # acquire lock
+       my $sync = {
+               mm_tmp => $self->{mm}->tmp_clone,
+               D => {}, # "$mid\0$cid" => $oid
+               reindex => $opt->{reindex},
+               -opt => $opt
+       };
+       $sync->{ranges} = sync_ranges($self, $sync, $epoch_max);
+       $sync->{regen} = sync_prepare($self, $sync, $epoch_max);
+
        my @cmd = qw(log --raw -r --pretty=tformat:%H
                        --no-notes --no-color --no-abbrev --no-renames);
 
        # work backwards through history
-       my $last_commit = [];
        for (my $i = $epoch_max; $i >= 0; $i--) {
                my $git_dir = git_dir_n($self, $i);
-               die "already reindexing!\n" if delete $self->{reindex_pipe};
+               die 'BUG: already reindexing!' if $self->{reindex_pipe};
                -d $git_dir or next; # missing parts are fine
+               fill_alternates($self, $i);
                my $git = PublicInbox::Git->new($git_dir);
-               my $unindex = delete $self->{"unindex-range.$i"};
-               $self->unindex($opts, $git, $unindex) if $unindex;
-               defined(my $range = $ranges->[$i]) or next;
+               my $unindex_range = delete $sync->{"unindex-range.$i"};
+               unindex($self, $sync, $git, $unindex_range) if $unindex_range;
+               defined(my $range = $sync->{ranges}->[$i]) or next;
+               $pr->("$i.git indexing $range\n") if $pr;
                my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
                my $cmt;
                while (<$fh>) {
+                       chomp;
+                       $self->{current_info} = "$i.git $_";
                        if (/\A$x40$/o && !defined($cmt)) {
-                               chomp($cmt = $_);
+                               $cmt = $_;
                        } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
-                               $self->reindex_oid($mm_tmp, $D, $git, $1,
-                                               $regen, $reindex);
+                               reindex_oid($self, $sync, $git, $1);
                        } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
-                               $self->mark_deleted($D, $git, $1);
+                               mark_deleted($self, $sync, $git, $1);
                        }
                }
                $fh = undef;
                delete $self->{reindex_pipe};
-               $self->update_last_commit($git, $i, $cmt) if defined $cmt;
+               update_last_commit($self, $git, $i, $cmt) if defined $cmt;
        }
 
        # unindex is required for leftovers if "deletes" affect messages
        # in a previous fetch+index window:
-       if (scalar keys %$D) {
+       if (my @leftovers = values %{delete $sync->{D}}) {
                my $git = $self->{-inbox}->git;
-               $self->unindex_oid($git, $_) for values %$D;
+               unindex_oid($self, $git, $_) for @leftovers;
                $git->cleanup;
        }
        $self->done;
+       if (my $pr = $sync->{-opt}->{-progress}) {
+               $pr->('all.git '.sprintf($sync->{-regen_fmt}, $sync->{nr}));
+       }
+
+       # reindex does not pick up new changes, so we rerun w/o it:
+       if ($opt->{reindex}) {
+               my %again = %$opt;
+               $sync = undef;
+               delete @again{qw(reindex -skip_lock)};
+               index_sync($self, \%again);
+       }
 }
 
 1;