]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
mm: min/max: return 0 instead of undef
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index ca60f2a16f88334eed088a363b6bf8a0b395ff96..7bef1c89a21ef220933ff67bd9992cd72b7a1ab1 100644 (file)
@@ -567,7 +567,7 @@ sub last_epoch_commit ($$;$) {
        $self->{mm}->last_commit_xap($v, $i, $cmt);
 }
 
-sub set_last_commits ($) {
+sub set_last_commits ($) { # this is NOT for ExtSearchIdx
        my ($self) = @_;
        defined(my $epoch_max = $self->{epoch_max}) or return;
        my $last_commit = $self->{last_commit};
@@ -620,17 +620,20 @@ sub checkpoint ($;$) {
 
                # Now deal with Xapian
                if ($wait) {
-                       my $barrier = $self->barrier_init(scalar @$shards);
+                       my $barrier = barrier_init($self, scalar @$shards);
 
                        # each shard needs to issue a barrier command
                        $_->shard_barrier for @$shards;
 
                        # wait for each Xapian shard
-                       $self->barrier_wait($barrier);
+                       barrier_wait($self, $barrier);
                } else {
                        $_->shard_commit for @$shards;
                }
 
+               my $midx = $self->{midx}; # misc index
+               $midx->commit_txn if $midx;
+
                # last_commit is special, don't commit these until
                # Xapian shards are done:
                $dbh->begin_work if $dbh;
@@ -639,6 +642,7 @@ sub checkpoint ($;$) {
                        $dbh->commit;
                        $dbh->begin_work;
                }
+               $midx->begin_txn if $midx;
        }
        $self->{total_bytes} += $self->{transact_bytes};
        $self->{transact_bytes} = 0;
@@ -678,6 +682,7 @@ sub done {
        }
        eval { $self->{oidx}->dbh_close };
        $err .= "over close: $@\n" if $@;
+       delete $self->{midx};
        delete $self->{bnote};
        my $nbytes = $self->{total_bytes};
        $self->{total_bytes} = 0;
@@ -860,11 +865,17 @@ sub atfork_child {
 sub reindex_checkpoint ($$) {
        my ($self, $sync) = @_;
 
-       $self->git->cleanup; # *async_wait
+       $self->git->async_wait_all;
+       $self->update_last_commit($sync);
        ${$sync->{need_checkpoint}} = 0;
        my $mm_tmp = $sync->{mm_tmp};
        $mm_tmp->atfork_prepare if $mm_tmp;
-       $self->done; # release lock
+       die 'BUG: {im} during reindex' if $self->{im};
+       if ($self->{ibx_map}) {
+               checkpoint($self, 1); # no need to release lock on pure index
+       } else {
+               $self->done; # release lock
+       }
 
        if (my $pr = $sync->{-opt}->{-progress}) {
                $pr->(sprintf($sync->{-regen_fmt}, ${$sync->{nr}}));
@@ -877,12 +888,13 @@ sub reindex_checkpoint ($$) {
 
 sub index_oid { # cat_async callback
        my ($bref, $oid, $type, $size, $arg) = @_;
+       my $self = $arg->{self};
+       local $self->{current_info} = "$self->{current_info} $oid";
        return if $size == 0; # purged
        my ($num, $mid0);
        my $eml = PublicInbox::Eml->new($$bref);
        my $mids = mids($eml);
        my $chash = content_hash($eml);
-       my $self = $arg->{self};
 
        if (scalar(@$mids) == 0) {
                warn "E: $oid has no Message-ID, skipping\n";
@@ -949,19 +961,22 @@ sub index_oid { # cat_async callback
        if (do_idx($self, $bref, $eml, $smsg)) {
                ${$arg->{need_checkpoint}} = 1;
        }
+       ${$arg->{latest_cmt}} = $arg->{cur_cmt} // die 'BUG: {cur_cmt} missing';
 }
 
 # only update last_commit for $i on reindex iff newer than current
-# $sync will be used by subclasses
 sub update_last_commit {
-       my ($self, $sync, $git, $i, $cmt) = @_;
-       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));
+       my ($self, $sync, $stk) = @_;
+       my $unit = $sync->{unit} // return;
+       my $latest_cmt = $stk ? $stk->{latest_cmt} : ${$sync->{latest_cmt}};
+       defined($latest_cmt) or return;
+       my $last = last_epoch_commit($self, $unit->{epoch});
+       if (defined $last && is_ancestor($self->git, $last, $latest_cmt)) {
+               my @cmd = (qw(rev-list --count), "$last..$latest_cmt");
+               chomp(my $n = $unit->{git}->qx(@cmd));
                return if $n ne '' && $n == 0;
        }
-       last_epoch_commit($self, $i, $cmt);
+       last_epoch_commit($self, $unit->{epoch}, $latest_cmt);
 }
 
 sub last_commits {
@@ -974,10 +989,11 @@ sub last_commits {
 }
 
 # returns a revision range for git-log(1)
-sub log_range ($$$$) {
-       my ($sync, $git, $i, $tip) = @_;
+sub log_range ($$$) {
+       my ($sync, $unit, $tip) = @_;
        my $opt = $sync->{-opt};
        my $pr = $opt->{-progress} if (($opt->{verbose} || 0) > 1);
+       my $i = $unit->{epoch};
        my $cur = $sync->{ranges}->[$i] or do {
                $pr->("$i.git indexing all of $tip\n") if $pr;
                return $tip; # all of it
@@ -991,7 +1007,8 @@ sub log_range ($$$$) {
 
        my $range = "$cur..$tip";
        $pr->("$i.git checking contiguity... ") if $pr;
-       if (is_ancestor($git, $cur, $tip)) { # common case
+       my $git = $unit->{git};
+       if (is_ancestor($sync->{self}->git, $cur, $tip)) { # common case
                $pr->("OK\n") if $pr;
                my $n = $git->qx(qw(rev-list --count), $range);
                chomp($n);
@@ -1016,10 +1033,17 @@ Rewritten history? (in $git->{git_dir})
                        warn "discarding history at $cur\n";
                }
                warn <<"";
-reindexing $git->{git_dir} starting at
-$range
-
-               $sync->{unindex_range}->{$i} = "$base..$cur";
+reindexing $git->{git_dir}
+starting at $range
+
+               # $cur^0 may no longer exist if pruned by git
+               if ($git->qx(qw(rev-parse -q --verify), "$cur^0")) {
+                       $unit->{unindex_range} = "$base..$cur";
+               } elsif ($base && $git->qx(qw(rev-parse -q --verify), $base)) {
+                       $unit->{unindex_range} = "$base..";
+               } else {
+                       warn "W: unable to unindex before $range\n";
+               }
        }
        $range;
 }
@@ -1033,49 +1057,63 @@ sub sync_prepare ($$) {
        my $pr = $sync->{-opt}->{-progress};
        my $regen_max = 0;
        my $head = $sync->{ibx}->{ref_head} || 'HEAD';
+       my $pfx;
+       if ($pr) {
+               ($pfx) = ($sync->{ibx}->{inboxdir} =~ m!([^/]+)\z!g);
+               $pfx //= $sync->{ibx}->{inboxdir};
+       }
 
        # reindex stops at the current heads and we later rerun index_sync
        # without {reindex}
        my $reindex_heads = $self->last_commits($sync) if $sync->{reindex};
 
+       if ($sync->{max_size} = $sync->{-opt}->{max_size}) {
+               $sync->{index_oid} = $self->can('index_oid');
+       }
        for (my $i = $sync->{epoch_max}; $i >= 0; $i--) {
                my $git_dir = $sync->{ibx}->git_dir_n($i);
                -d $git_dir or next; # missing epochs are fine
                my $git = PublicInbox::Git->new($git_dir);
+               my $unit = { git => $git, epoch => $i };
                if ($reindex_heads) {
                        $head = $reindex_heads->[$i] or next;
                }
                chomp(my $tip = $git->qx(qw(rev-parse -q --verify), $head));
-
                next if $?; # new repo
-               my $range = log_range($sync, $git, $i, $tip) or next;
+
+               my $range = log_range($sync, $unit, $tip) or next;
                # can't use 'rev-list --count' if we use --diff-filter
-               $pr->("$i.git counting $range ... ") if $pr;
+               $pr->("$pfx $i.git counting $range ... ") if $pr;
                # Don't bump num_highwater on --reindex by using {D}.
                # We intentionally do NOT use {D} in the non-reindex case
                # because we want NNTP article number gaps from unindexed
                # messages to show up in mirrors, too.
                $sync->{D} //= $sync->{reindex} ? {} : undef; # OID_BIN => NR
                my $stk = log2stack($sync, $git, $range);
+               return 0 if $sync->{quit};
                my $nr = $stk ? $stk->num_records : 0;
                $pr->("$nr\n") if $pr;
-               $sync->{stacks}->[$i] = $stk if $stk;
+               $unit->{stack} = $stk; # may be undef
+               unshift @{$sync->{todo}}, $unit;
                $regen_max += $nr;
        }
+       return 0 if $sync->{quit};
 
        # XXX this should not happen unless somebody bypasses checks in
        # our code and blindly injects "d" file history into git repos
        if (my @leftovers = keys %{delete($sync->{D}) // {}}) {
                warn('W: unindexing '.scalar(@leftovers)." leftovers\n");
-               my $arg = { self => $self };
+               local $self->{current_info} = 'leftover ';
                my $unindex_oid = $self->can('unindex_oid');
                for my $oid (@leftovers) {
                        $oid = unpack('H*', $oid);
-                       $self->{current_info} = "leftover $oid";
-                       $self->git->cat_async($oid, $unindex_oid, $arg);
+                       my $req = { %$sync, oid => $oid };
+                       $self->git->cat_async($oid, $unindex_oid, $req);
+                       last if $sync->{quit};
                }
                $self->git->cat_async_wait;
        }
+       return 0 if $sync->{quit};
        if (!$regen_max) {
                $sync->{-regen_fmt} = "%u/?\n";
                return 0;
@@ -1102,6 +1140,7 @@ sub unindex_oid_aux ($$$) {
 sub unindex_oid ($$;$) { # git->cat_async callback
        my ($bref, $oid, $type, $size, $sync) = @_;
        my $self = $sync->{self};
+       local $self->{current_info} = "$self->{current_info} $oid";
        my $unindexed = $sync->{in_unindex} ? $sync->{unindexed} : undef;
        my $mm = $self->{mm};
        my $mids = mids(PublicInbox::Eml->new($bref));
@@ -1133,19 +1172,19 @@ sub git { $_[0]->{ibx}->git }
 
 # this is rare, it only happens when we get discontiguous history in
 # a mirror because the source used -purge or -edit
-sub unindex_epoch ($$$$) {
-       my ($self, $sync, $git, $unindex_range) = @_;
+sub unindex_todo ($$$) {
+       my ($self, $sync, $unit) = @_;
+       my $unindex_range = delete($unit->{unindex_range}) // return;
        my $unindexed = $sync->{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 = $git->popen(@cmd, $unindex_range);
+       my $fh = $unit->{git}->popen(qw(log --raw -r --no-notes --no-color
+                               --no-abbrev --no-renames), $unindex_range);
        local $sync->{in_unindex} = 1;
        my $unindex_oid = $self->can('unindex_oid');
        while (<$fh>) {
                /\A:\d{6} 100644 $OID ($OID) [AM]\tm$/o or next;
-               $self->git->cat_async($1, $unindex_oid, $sync);
+               $self->git->cat_async($1, $unindex_oid, { %$sync, oid => $1 });
        }
        close $fh or die "git log failed: \$?=$?";
        $self->git->cat_async_wait;
@@ -1155,7 +1194,8 @@ sub unindex_epoch ($$$$) {
        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}",
+       PublicInbox::Import::run_die(['git',
+               "--git-dir=$unit->{git}->{git_dir}",
                qw(-c gc.reflogExpire=now gc --prune=all --quiet)]);
 }
 
@@ -1203,23 +1243,32 @@ sub index_xap_step ($$$;$) {
        }
 }
 
-sub index_epoch ($$$) {
-       my ($self, $sync, $i) = @_;
-
-       my $git_dir = $sync->{ibx}->git_dir_n($i);
-       -d $git_dir or return; # missing epochs are fine
-       my $git = PublicInbox::Git->new($git_dir);
-       if (my $unindex_range = delete $sync->{unindex_range}->{$i}) { # rare
-               unindex_epoch($self, $sync, $git, $unindex_range);
-       }
-       defined(my $stk = $sync->{stacks}->[$i]) or return;
-       $sync->{stacks}->[$i] = undef;
+sub index_todo ($$$) {
+       my ($self, $sync, $unit) = @_;
+       return if $sync->{quit};
+       unindex_todo($self, $sync, $unit);
+       my $stk = delete($unit->{stack}) or return;
        my $all = $self->git;
        my $index_oid = $self->can('index_oid');
        my $unindex_oid = $self->can('unindex_oid');
-       while (my ($f, $at, $ct, $oid) = $stk->pop_rec) {
-               $self->{current_info} = "$i.git $oid";
-               my $req = { %$sync, autime => $at, cotime => $ct, oid => $oid };
+       my $pfx;
+       if ($unit->{git}->{git_dir} =~ m!/([^/]+)/git/([0-9]+\.git)\z!) {
+               $pfx = "$1 $2"; # v2
+       } else { # v1
+               ($pfx) = ($unit->{git}->{git_dir} =~ m!/([^/]+)\z!g);
+               $pfx //= $unit->{git}->{git_dir};
+       }
+       local $self->{current_info} = "$pfx ";
+       local $sync->{latest_cmt} = \(my $latest_cmt);
+       local $sync->{unit} = $unit;
+       while (my ($f, $at, $ct, $oid, $cmt) = $stk->pop_rec) {
+               my $req = {
+                       %$sync,
+                       autime => $at,
+                       cotime => $ct,
+                       oid => $oid,
+                       cur_cmt => $cmt
+               };
                if ($f eq 'm') {
                        if ($sync->{max_size}) {
                                $all->check_async($oid, \&check_size, $req);
@@ -1229,12 +1278,18 @@ sub index_epoch ($$$) {
                } elsif ($f eq 'd') {
                        $all->cat_async($oid, $unindex_oid, $req);
                }
+               if ($sync->{quit}) {
+                       warn "waiting to quit...\n";
+                       $all->async_wait_all;
+                       $self->update_last_commit($sync);
+                       return;
+               }
                if (${$sync->{need_checkpoint}}) {
                        reindex_checkpoint($self, $sync);
                }
        }
        $all->async_wait_all;
-       $self->update_last_commit($sync, $git, $i, $stk->{latest_cmt});
+       $self->update_last_commit($sync, $stk);
 }
 
 sub xapian_only {
@@ -1289,13 +1344,17 @@ sub index_sync {
        $self->{oidx}->rethread_prepare($opt);
        my $sync = {
                need_checkpoint => \(my $bool = 0),
-               unindex_range => {}, # EPOCH => oid_old..oid_new
                reindex => $opt->{reindex},
                -opt => $opt,
                self => $self,
                ibx => $self->{ibx},
                epoch_max => $epoch_max,
        };
+       my $quit = sub { $sync->{quit} = 1 };
+       local $SIG{QUIT} = $quit;
+       local $SIG{INT} = $quit;
+       local $SIG{TERM} = $quit;
+
        if (sync_prepare($self, $sync)) {
                # tmp_clone seems to fail if inside a transaction, so
                # we rollback here (because we opened {mm} for reading)
@@ -1308,16 +1367,13 @@ sub index_sync {
 
                # xapian_only works incrementally w/o --reindex
                if ($seq && !$opt->{reindex}) {
-                       $art_beg = $sync->{mm_tmp}->max;
-                       $art_beg++ if defined($art_beg);
+                       $art_beg = $sync->{mm_tmp}->max || -1;
+                       $art_beg++;
                }
        }
-       if ($sync->{max_size} = $opt->{max_size}) {
-               $sync->{index_oid} = $self->can('index_oid');
-       }
        # work forwards through history
-       index_epoch($self, $sync, $_) for (0..$epoch_max);
-       $self->{oidx}->rethread_done($opt);
+       index_todo($self, $sync, $_) for @{delete($sync->{todo}) // []};
+       $self->{oidx}->rethread_done($opt) unless $sync->{quit};
        $self->done;
 
        if (my $nr = $sync->{nr}) {