]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
index: support --rethread switch to fix old indices
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index a0f041dd3dc0d287918142a39ceecff2b0abda3d..16556ddc209d7ab4d3cdcfc2d3d5fd91c4aa052e 100644 (file)
@@ -5,16 +5,15 @@
 # Used to write to V2 inboxes (see L<public-inbox-v2-format(5)>).
 package PublicInbox::V2Writable;
 use strict;
-use warnings;
-use base qw(PublicInbox::Lock);
-use 5.010_001;
+use v5.10.1;
+use parent qw(PublicInbox::Lock);
 use PublicInbox::SearchIdxShard;
 use PublicInbox::Eml;
 use PublicInbox::Git;
 use PublicInbox::Import;
 use PublicInbox::MID qw(mids references);
 use PublicInbox::ContentHash qw(content_hash content_digest);
-use PublicInbox::Inbox;
+use PublicInbox::InboxWritable;
 use PublicInbox::OverIdx;
 use PublicInbox::Msgmap;
 use PublicInbox::Spawn qw(spawn popen_rd);
@@ -113,6 +112,7 @@ sub new {
                im => undef, #  PublicInbox::Import
                parallel => 1,
                transact_bytes => 0,
+               total_bytes => 0,
                current_info => '',
                xpfx => $xpfx,
                over => PublicInbox::OverIdx->new("$xpfx/over.sqlite3", 1),
@@ -147,10 +147,8 @@ sub init_inbox {
 # returns undef on duplicate or spam
 # mimics Import::add and wraps it for v2
 sub add {
-       my ($self, $mime, $check_cb) = @_;
-       $self->{-inbox}->with_umask(sub {
-               _add($self, $mime, $check_cb)
-       });
+       my ($self, $eml, $check_cb) = @_;
+       $self->{-inbox}->with_umask(\&_add, $self, $eml, $check_cb);
 }
 
 # indexes a message, returns true if checkpointing is needed
@@ -170,7 +168,7 @@ sub _add {
 
        # spam check:
        if ($check_cb) {
-               $mime = $check_cb->($mime) or return;
+               $mime = $check_cb->($mime, $self->{-inbox}) or return;
        }
 
        # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
@@ -276,6 +274,28 @@ sub idx_shard {
        $self->{idx_shards}->[$shard_i];
 }
 
+sub _idx_init { # with_umask callback
+       my ($self, $opt) = @_;
+       $self->lock_acquire unless $opt && $opt->{-skip_lock};
+       $self->{over}->create;
+
+       # xcpdb can change shard count while -watch is idle
+       my $nshards = count_shards($self);
+       $self->{shards} = $nshards if $nshards && $nshards != $self->{shards};
+
+       # need to create all shards before initializing msgmap FD
+       # idx_shards must be visible to all forked processes
+       my $max = $self->{shards} - 1;
+       my $idx = $self->{idx_shards} = [];
+       push @$idx, PublicInbox::SearchIdxShard->new($self, $_) for (0..$max);
+
+       # Now that all subprocesses are up, we can open the FDs
+       # for SQLite:
+       my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
+               "$self->{-inbox}->{inboxdir}/msgmap.sqlite3", 1);
+       $mm->{dbh}->begin_work;
+}
+
 # idempotent
 sub idx_init {
        my ($self, $opt) = @_;
@@ -285,13 +305,10 @@ sub idx_init {
        # do not leak read-only FDs to child processes, we only have these
        # FDs for duplicate detection so they should not be
        # frequently activated.
+       # delete @$ibx{qw(git mm search)};
        delete $ibx->{$_} foreach (qw(git mm search));
 
-       my $indexlevel = $ibx->{indexlevel};
-       if ($indexlevel && $indexlevel eq 'basic') {
-               $self->{parallel} = 0;
-       }
-
+       $self->{parallel} = 0 if ($ibx->{indexlevel}//'') eq 'basic';
        if ($self->{parallel}) {
                pipe(my ($r, $w)) or die "pipe failed: $!";
                # pipe for barrier notifications doesn't need to be big,
@@ -301,33 +318,8 @@ sub idx_init {
                $w->autoflush(1);
        }
 
-       my $over = $self->{over};
        $ibx->umask_prepare;
-       $ibx->with_umask(sub {
-               $self->lock_acquire unless ($opt && $opt->{-skip_lock});
-               $over->create;
-
-               # xcpdb can change shard count while -watch is idle
-               my $nshards = count_shards($self);
-               if ($nshards && $nshards != $self->{shards}) {
-                       $self->{shards} = $nshards;
-               }
-
-               # need to create all shards before initializing msgmap FD
-               my $max = $self->{shards} - 1;
-
-               # idx_shards must be visible to all forked processes
-               my $idx = $self->{idx_shards} = [];
-               for my $i (0..$max) {
-                       push @$idx, PublicInbox::SearchIdxShard->new($self, $i);
-               }
-
-               # Now that all subprocesses are up, we can open the FDs
-               # for SQLite:
-               my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
-                       "$self->{-inbox}->{inboxdir}/msgmap.sqlite3", 1);
-               $mm->{dbh}->begin_work;
-       });
+       $ibx->with_umask(\&_idx_init, $self, $opt);
 }
 
 # returns an array mapping [ epoch => latest_commit ]
@@ -379,24 +371,24 @@ sub content_matches ($$) {
 
 # used for removing or replacing (purging)
 sub rewrite_internal ($$;$$$) {
-       my ($self, $old_mime, $cmt_msg, $new_mime, $sref) = @_;
+       my ($self, $old_eml, $cmt_msg, $new_eml, $sref) = @_;
        $self->idx_init;
        my ($im, $need_reindex, $replace_map);
        if ($sref) {
                $replace_map = {}; # oid => sref
-               $need_reindex = [] if $new_mime;
+               $need_reindex = [] if $new_eml;
        } else {
                $im = $self->importer;
        }
        my $over = $self->{over};
-       my $chashes = content_hashes($old_mime);
-       my @removed;
-       my $mids = mids($old_mime->header_obj);
+       my $chashes = content_hashes($old_eml);
+       my $removed = [];
+       my $mids = mids($old_eml->header_obj);
 
        # We avoid introducing new blobs into git since the raw content
        # can be slightly different, so we do not need the user-supplied
        # message now that we have the mids and content_hash
-       $old_mime = undef;
+       $old_eml = undef;
        my $mark;
 
        foreach my $mid (@$mids) {
@@ -422,15 +414,15 @@ sub rewrite_internal ($$;$$$) {
                }
                foreach my $num (keys %gone) {
                        my ($smsg, $mime, $orig) = @{$gone{$num}};
-                       # @removed should only be set once assuming
+                       # $removed should only be set once assuming
                        # no bugs in our deduplication code:
-                       @removed = (undef, $mime, $smsg);
+                       $removed = [ undef, $mime, $smsg ];
                        my $oid = $smsg->{blob};
                        if ($replace_map) {
                                $replace_map->{$oid} = $sref;
                        } else {
                                ($mark, undef) = $im->remove($orig, $cmt_msg);
-                               $removed[0] = $mark;
+                               $removed->[0] = $mark;
                        }
                        $orig = undef;
                        if ($need_reindex) { # ->replace
@@ -447,28 +439,26 @@ sub rewrite_internal ($$;$$$) {
                $self->{last_commit}->[$self->{epoch_max}] = $cmt;
        }
        if ($replace_map && scalar keys %$replace_map) {
-               my $rewrites = _replace_oids($self, $new_mime, $replace_map);
+               my $rewrites = _replace_oids($self, $new_eml, $replace_map);
                return { rewrites => $rewrites, need_reindex => $need_reindex };
        }
-       defined($mark) ? @removed : undef;
+       defined($mark) ? $removed : undef;
 }
 
 # public (see PublicInbox::Import->remove), but note the 3rd element
 # (retval[2]) is not part of the stable API shared with Import->remove
 sub remove {
-       my ($self, $mime, $cmt_msg) = @_;
-       my @ret;
-       $self->{-inbox}->with_umask(sub {
-               @ret = rewrite_internal($self, $mime, $cmt_msg);
-       });
-       defined($ret[0]) ? @ret : undef;
+       my ($self, $eml, $cmt_msg) = @_;
+       my $r = $self->{-inbox}->with_umask(\&rewrite_internal,
+                                               $self, $eml, $cmt_msg);
+       defined($r) && defined($r->[0]) ? @$r: undef;
 }
 
 sub _replace ($$;$$) {
-       my ($self, $old_mime, $new_mime, $sref) = @_;
-       my $rewritten = $self->{-inbox}->with_umask(sub {
-               rewrite_internal($self, $old_mime, undef, $new_mime, $sref);
-       }) or return;
+       my ($self, $old_eml, $new_eml, $sref) = @_;
+       my $arg = [ $self, $old_eml, undef, $new_eml, $sref ];
+       my $rewritten = $self->{-inbox}->with_umask(\&rewrite_internal,
+                       $self, $old_eml, undef, $new_eml, $sref) or return;
 
        my $rewrites = $rewritten->{rewrites};
        # ->done is called if there are rewrites since we gc+prune from git
@@ -492,14 +482,12 @@ sub purge {
 sub git_hash_raw ($$) {
        my ($self, $raw) = @_;
        # grab the expected OID we have to reindex:
-       open my $tmp_fh, '+>', undef or die "failed to open tmp: $!";
-       $tmp_fh->autoflush(1);
-       print $tmp_fh $$raw or die "print \$tmp_fh: $!";
-       sysseek($tmp_fh, 0, 0) or die "seek failed: $!";
-
+       pipe(my($in, $w)) or die "pipe: $!";
        my $git_dir = $self->{-inbox}->git->{git_dir};
        my $cmd = ['git', "--git-dir=$git_dir", qw(hash-object --stdin)];
-       my $r = popen_rd($cmd, undef, { 0 => $tmp_fh });
+       my $r = popen_rd($cmd, undef, { 0 => $in });
+       print $w $$raw or die "print \$w: $!";
+       close $w or die "close \$w: $!";
        local $/ = "\n";
        chomp(my $oid = <$r>);
        close $r or die "git hash-object failed: $?";
@@ -659,6 +647,7 @@ sub checkpoint ($;$) {
 
                $dbh->begin_work;
        }
+       $self->{total_bytes} += $self->{transact_bytes};
        $self->{transact_bytes} = 0;
 }
 
@@ -681,8 +670,9 @@ sub done {
        }
        $self->{over}->disconnect;
        delete $self->{bnote};
-       $self->{transact_bytes} = 0;
-       $self->lock_release if $shards;
+       my $nbytes = $self->{total_bytes};
+       $self->{total_bytes} = 0;
+       $self->lock_release(!!$nbytes) if $shards;
        $self->{-inbox}->git->cleanup;
 }
 
@@ -1193,8 +1183,11 @@ sub sync_prepare ($$$) {
 
 sub unindex_oid_remote ($$$) {
        my ($self, $oid, $mid) = @_;
-       $_->remote_remove($oid, $mid) foreach @{$self->{idx_shards}};
-       $self->{over}->remove_oid($oid, $mid);
+       my @removed = $self->{over}->remove_oid($oid, $mid);
+       for my $num (@removed) {
+               my $idx = idx_shard($self, $num % $self->{shards});
+               $idx->remote_remove($oid, $num);
+       }
 }
 
 sub unindex_oid ($$$;$) {
@@ -1315,6 +1308,7 @@ sub index_sync {
        my $latest = git_dir_latest($self, \$epoch_max);
        return unless defined $latest;
        $self->idx_init($opt); # acquire lock
+       $self->{over}->rethread_prepare($opt);
        my $sync = {
                D => {}, # "$mid\0$chash" => $oid
                unindex_range => {}, # EPOCH => oid_old..oid_new
@@ -1377,12 +1371,13 @@ sub index_sync {
                my $pr = $sync->{-opt}->{-progress};
                $pr->('all.git '.sprintf($sync->{-regen_fmt}, $nr)) if $pr;
        }
+       $self->{over}->rethread_done($opt);
 
        # 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)};
+               delete @again{qw(rethread reindex -skip_lock)};
                index_sync($self, \%again);
        }
 }