]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
v2writable: introduce $sync state and put mm_tmp in it
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index bc31ffa5edc79a0425baea3244836b926bb401ca..584767ae8c92f3408fafbc9aa792deacdead3d86 100644 (file)
@@ -24,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;
@@ -52,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) {
@@ -72,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",
@@ -79,18 +89,18 @@ 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;
 }
 
 sub init_inbox {
-       my ($self, $parallel, $skip) = @_;
+       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_max == -1) {
-               $epoch_max = $skip;
+       if (defined $skip_epoch && $epoch_max == -1) {
+               $epoch_max = $skip_epoch;
        }
        $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
        $self->done;
@@ -228,7 +238,7 @@ sub idx_part {
 
 # idempotent
 sub idx_init {
-       my ($self) = @_;
+       my ($self, $opt) = @_;
        return if $self->{idx_parts};
        my $ibx = $self->{-inbox};
 
@@ -254,7 +264,7 @@ sub idx_init {
        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
@@ -516,11 +526,7 @@ sub fill_alternates ($$) {
        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=$pfx/$epoch.git/config",
                        'include.path', '../../all.git/config');
@@ -700,7 +706,7 @@ sub mark_deleted {
 }
 
 sub reindex_oid {
-       my ($self, $mm_tmp, $D, $git, $oid, $regen, $reindex) = @_;
+       my ($self, $sync, $D, $git, $oid, $regen, $reindex) = @_;
        my $len;
        my $msgref = $git->cat_file($oid, \$len);
        my $mime = PublicInbox::MIME->new($$msgref);
@@ -708,13 +714,13 @@ 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);
+               my $n = $sync->{mm_tmp}->num_for($mid);
                if (defined $n && $n > $num) {
                        $mid0 = $mid;
                        $num = $n;
@@ -758,7 +764,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);
@@ -769,11 +775,14 @@ sub reindex_oid {
        my $n = $self->{transact_bytes} += $len;
        if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
                $git->cleanup;
-               $mm_tmp->atfork_prepare;
+               $sync->{mm_tmp}->atfork_prepare;
                $self->done; # release lock
+
+               # TODO: print progress info, here
+
                # allow -watch or -mda to write...
                $self->idx_init; # reacquire lock
-               $mm_tmp->atfork_parent;
+               $sync->{mm_tmp}->atfork_parent;
        }
 }
 
@@ -802,59 +811,73 @@ sub last_commits {
 
 *is_ancestor = *PublicInbox::SearchIdx::is_ancestor;
 
+# returns a revision range for git-log(1)
+sub log_range ($$$$$) {
+       my ($self, $git, $ranges, $i, $tip) = @_;
+       my $cur = $ranges->[$i] or return $tip; # all of it
+       my $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;
+                       return; # nothing to do
+               }
+       } else {
+               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
+
+               $self->{"unindex-range.$i"} = "$base..$cur";
+       }
+       $range;
+}
+
 sub index_prepare {
        my ($self, $opts, $epoch_max, $ranges) = @_;
+       my $pr = $opts->{-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 $opts->{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);
+               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;
-               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
-               }
+               next if $?; # new repo
+               my $range = log_range($self, $git, $ranges, $i, $tip) or next;
                $ranges->[$i] = $range;
 
                # can't use 'rev-list --count' if we use --diff-filter
+               $pr->("$i.git counting changes\n\t$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;
 }
@@ -917,30 +940,53 @@ sub unindex {
                qw(-c gc.reflogExpire=now gc --prune=all)]);
 }
 
+sub index_ranges ($$$) {
+       my ($self, $reindex, $epoch_max) = @_;
+       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;
+}
+
+# called for public-inbox-index
 sub index_sync {
        my ($self, $opts) = @_;
        $opts ||= {};
        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;
+       $self->idx_init($opts); # acquire lock
+       my $sync = {
+               mm_tmp => $self->{mm}->tmp_clone,
+       };
        my $reindex = $opts->{reindex};
-       my $ranges = $reindex ? [] : $self->last_commits($epoch_max);
+       my $ranges = index_ranges($self, $reindex, $epoch_max);
 
        my $high = $self->{mm}->num_highwater();
        my $regen = $self->index_prepare($opts, $epoch_max, $ranges);
-       $$regen += $high if $high;
+       if ($opts->{reindex}) {
+               # reindex should NOT see new commits anymore, if we do,
+               # it's a problem and we need to notice it via die()
+               $$regen = -1;
+       } else {
+               $$regen += $high;
+       }
+
        my $D = {}; # "$mid\0$cid" => $oid
        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;
@@ -948,10 +994,12 @@ sub index_sync {
                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,
+                               $self->reindex_oid($sync, $D, $git, $1,
                                                $regen, $reindex);
                        } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\td$/o) {
                                $self->mark_deleted($D, $git, $1);
@@ -970,6 +1018,14 @@ sub index_sync {
                $git->cleanup;
        }
        $self->done;
+
+       # reindex does not pick up new changes, so we rerun w/o it:
+       if ($opts->{reindex}) {
+               my %again = %$opts;
+               $sync = undef;
+               delete @again{qw(reindex -skip_lock)};
+               index_sync($self, \%again);
+       }
 }
 
 1;