]> Sergey Matveev's repositories - public-inbox.git/commitdiff
v2writable: allow disabling parallelization
authorEric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Mon, 19 Mar 2018 08:14:56 +0000 (08:14 +0000)
committerEric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Mon, 19 Mar 2018 08:16:34 +0000 (08:16 +0000)
While parallel processes improves import speed for initial
imports; they are probably not necessary for daily mail imports
via WatchMaildir and certainly not for public-inbox-init.  Save
some memory for daily use and even helps improve readability of
some subroutines by showing which methods they call remotely.

lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/SearchIdxPart.pm
lib/PublicInbox/SearchIdxSkeleton.pm
lib/PublicInbox/V2Writable.pm
lib/PublicInbox/WatchMaildir.pm
script/public-inbox-init
t/v2writable.t

index 0b9fb4bce101b92dded63034d04b0373f8f59291..3d80b002f709448776aada0b03033ebf38a9bad4 100644 (file)
@@ -523,6 +523,7 @@ sub link_and_save {
        $doc->add_boolean_term('Q' . $_) foreach @$mids;
 
        my $vivified = 0;
+       $self->{skel} and die "Should not have read-only skel here\n";;
        foreach my $mid (@$mids) {
                $self->each_smsg_by_mid($mid, sub {
                        my ($cur) = @_;
@@ -887,24 +888,59 @@ sub DESTROY {
 # remote_* subs are only used by SearchIdxPart and SearchIdxSkeleton
 sub remote_commit {
        my ($self) = @_;
-       print { $self->{w} } "commit\n" or die "failed to write commit: $!";
+       if (my $w = $self->{w}) {
+               print $w "commit\n" or die "failed to write commit: $!";
+       } else {
+               $self->commit_txn_lazy;
+               if (my $skel = $self->{skeleton}) {
+                       $skel->commit_txn_lazy;
+               }
+       }
 }
 
 sub remote_close {
        my ($self) = @_;
-       my $pid = delete $self->{pid} or die "no process to wait on\n";
-       my $w = delete $self->{w} or die "no pipe to write to\n";
-       print $w "close\n" or die "failed to write to pid:$pid: $!\n";
-       close $w or die "failed to close pipe for pid:$pid: $!\n";
-       waitpid($pid, 0) == $pid or die "remote process did not finish";
-       $? == 0 or die ref($self)." pid:$pid exited with: $?";
+       if (my $w = delete $self->{w}) {
+               my $pid = delete $self->{pid} or die "no process to wait on\n";
+               print $w "close\n" or die "failed to write to pid:$pid: $!\n";
+               close $w or die "failed to close pipe for pid:$pid: $!\n";
+               waitpid($pid, 0) == $pid or die "remote process did not finish";
+               $? == 0 or die ref($self)." pid:$pid exited with: $?";
+       } else {
+               die "transaction in progress $self\n" if $self->{txn};
+               $self->_xdb_release if $self->{xdb};
+       }
 }
 
-# triggers remove_by_oid in partition or skeleton
 sub remote_remove {
        my ($self, $oid, $mid) = @_;
-       print { $self->{w} } "D $oid $mid\n" or
-                       die "failed to write remove $!";
+       if (my $w = $self->{w}) {
+               # triggers remove_by_oid in partition or skeleton
+               print $w "D $oid $mid\n" or die "failed to write remove $!";
+       } else {
+               $self->begin_txn_lazy;
+               $self->remove_by_oid($oid, $mid);
+       }
+}
+
+sub begin_txn_lazy {
+       my ($self) = @_;
+       return if $self->{txn};
+       my $xdb = $self->{xdb} || $self->_xdb_acquire;
+       $xdb->begin_transaction;
+       $self->{txn} = 1;
+}
+
+sub commit_txn_lazy {
+       my ($self) = @_;
+       delete $self->{txn} or return;
+       $self->{xdb}->commit_transaction;
+}
+
+sub worker_done {
+       my ($self) = @_;
+       die "$$ $0 xdb not released\n" if $self->{xdb};
+       die "$$ $0 still in transaction\n" if $self->{txn};
 }
 
 1;
index d8c8c8bb00e49a9a9a5b7177b0bab9d5f841452e..82f5c1bcfd1581adb583792d58eda88d4fa74a57 100644 (file)
@@ -9,6 +9,15 @@ sub new {
        my ($class, $v2writable, $part, $skel) = @_;
        my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $part);
        $self->{skeleton} = $skel;
+       # create the DB:
+       $self->_xdb_acquire;
+       $self->_xdb_release;
+       $self->spawn_worker($v2writable, $part) if $v2writable->{parallel};
+       $self;
+}
+
+sub spawn_worker {
+       my ($self, $v2writable, $part) = @_;
        my ($r, $w);
        pipe($r, $w) or die "pipe failed: $!\n";
        binmode $r, ':raw';
@@ -32,44 +41,30 @@ sub new {
        $self->{pid} = $pid;
        $self->{w} = $w;
        close $r;
-       $self;
 }
 
 sub partition_worker_loop ($$$) {
        my ($self, $r, $part) = @_;
        $0 = "pi-v2-partition[$part]";
-       my $xdb = $self->_xdb_acquire;
-       $xdb->begin_transaction;
-       my $txn = 1;
+       $self->begin_txn_lazy;
        while (my $line = $r->getline) {
                if ($line eq "commit\n") {
-                       $xdb->commit_transaction if $txn;
-                       $txn = undef;
+                       $self->commit_txn_lazy;
                        $self->{skeleton}->remote_commit;
                } elsif ($line eq "close\n") {
                        $self->_xdb_release;
-                       $xdb = $txn = undef;
                } elsif ($line eq "barrier\n") {
-                       $xdb->commit_transaction if $txn;
-                       $txn = undef;
+                       $self->commit_txn_lazy;
                        print { $self->{skeleton}->{w} } "barrier $part\n" or
                                        die "write failed to skeleton: $!\n";
                } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
                        my ($oid, $mid) = ($1, $2);
-                       $xdb ||= $self->_xdb_acquire;
-                       if (!$txn) {
-                               $xdb->begin_transaction;
-                               $txn = 1;
-                       }
+                       $self->begin_txn_lazy;
                        $self->remove_by_oid($oid, $mid);
                } else {
                        chomp $line;
                        my ($len, $artnum, $oid, $mid0) = split(/ /, $line);
-                       $xdb ||= $self->_xdb_acquire;
-                       if (!$txn) {
-                               $xdb->begin_transaction;
-                               $txn = 1;
-                       }
+                       $self->begin_txn_lazy;
                        my $n = read($r, my $msg, $len) or die "read: $!\n";
                        $n == $len or die "short read: $n != $len\n";
                        my $mime = PublicInbox::MIME->new(\$msg);
@@ -77,17 +72,21 @@ sub partition_worker_loop ($$$) {
                        $self->add_message($mime, $n, $artnum, $oid, $mid0);
                }
        }
-       warn "$$ still in transaction\n" if $txn;
-       warn "$$ xdb active\n" if $xdb;
+       $self->worker_done;
 }
 
 # called by V2Writable
 sub index_raw {
-       my ($self, $len, $msgref, $artnum, $object_id, $mid0) = @_;
-       my $w = $self->{w};
-       print $w "$len $artnum $object_id $mid0\n", $$msgref or die
-               "failed to write partition $!\n";
-       $w->flush or die "failed to flush: $!\n";
+       my ($self, $bytes, $msgref, $artnum, $oid, $mid0, $mime) = @_;
+       if (my $w = $self->{w}) {
+               print $w "$bytes $artnum $oid $mid0\n", $$msgref or die
+                       "failed to write partition $!\n";
+               $w->flush or die "failed to flush: $!\n";
+       } else {
+               $$msgref = undef;
+               $self->begin_txn_lazy;
+               $self->add_message($mime, $bytes, $artnum, $oid, $mid0);
+       }
 }
 
 sub atfork_child {
@@ -96,9 +95,14 @@ sub atfork_child {
 
 # called by V2Writable:
 sub remote_barrier {
-       my $w = $_[0]->{w};
-       print $w "barrier\n" or die "failed to print: $!";
-       $w->flush or die "failed to flush: $!";
+       my ($self) = @_;
+       if (my $w = $self->{w}) {
+               print $w "barrier\n" or die "failed to print: $!";
+               $w->flush or die "failed to flush: $!";
+       } else {
+               $self->commit_txn_lazy;
+               $self->{skeleton}->remote_commit;
+       }
 }
 
 1;
index 54a59ab02a61b5985a1a859c898ae86ca1d0af2a..ba43969631990c455b49ecd84616dadfaa22182f 100644 (file)
@@ -12,7 +12,12 @@ sub new {
        # create the DB:
        $self->_xdb_acquire;
        $self->_xdb_release;
+       $self->spawn_worker($v2writable) if $v2writable->{parallel};
+       $self
+}
 
+sub spawn_worker {
+       my ($self, $v2writable) = @_;
        my ($r, $w);
        pipe($r, $w) or die "pipe failed: $!\n";
        my ($barrier_wait, $barrier_note);
@@ -39,24 +44,19 @@ sub new {
 
        # lock on only exists in parent, not in worker
        $self->{lock_path} = $self->xdir . '/pi-v2-skeleton.lock';
-       $self;
 }
 
 sub skeleton_worker_loop {
        my ($self, $r, $barrier_note) = @_;
        $barrier_note->autoflush(1);
        $0 = 'pi-v2-skeleton';
-       my $xdb = $self->_xdb_acquire;
-       $xdb->begin_transaction;
-       my $txn = 1;
+       $self->begin_txn_lazy;
        my $barrier = undef;
        while (my $line = $r->getline) {
                if ($line eq "commit\n") {
-                       $xdb->commit_transaction if $txn;
-                       $txn = undef;
+                       $self->commit_txn_lazy;
                } elsif ($line eq "close\n") {
                        $self->_xdb_release;
-                       $xdb = $txn = undef;
                } elsif ($line =~ /\Abarrier_init (\d+)\n\z/) {
                        my $n = $1 - 1;
                        die "barrier in-progress\n" if defined $barrier;
@@ -67,18 +67,13 @@ sub skeleton_worker_loop {
                        delete $barrier->{$1} or die "unknown barrier: $part\n";
                        if ((scalar keys %$barrier) == 0) {
                                $barrier = undef;
-                               $xdb->commit_transaction if $txn;
-                               $txn = undef;
+                               $self->commit_txn_lazy;
                                print $barrier_note "barrier_done\n" or die
                                        "print failed to barrier note: $!";
                        }
                } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.*)\n\z/s) {
                        my ($oid, $mid) = ($1, $2);
-                       $xdb ||= $self->_xdb_acquire;
-                       if (!$txn) {
-                               $xdb->begin_transaction;
-                               $txn = 1;
-                       }
+                       $self->begin_txn_lazy;
                        $self->remove_by_oid($oid, $mid);
                } else {
                        my $len = int($line);
@@ -86,35 +81,34 @@ sub skeleton_worker_loop {
                        $n == $len or die "short read: $n != $len\n";
                        $msg = thaw($msg); # should raise on error
                        defined $msg or die "failed to thaw buffer\n";
-                       $xdb ||= $self->_xdb_acquire;
-                       if (!$txn) {
-                               $xdb->begin_transaction;
-                               $txn = 1;
-                       }
+                       $self->begin_txn_lazy;
                        eval { index_skeleton_real($self, $msg) };
                        warn "failed to index message <$msg->[-1]>: $@\n" if $@;
                }
        }
-       die "xdb not released\n" if $xdb;
-       die "in transaction\n" if $txn;
+       $self->worker_done;
 }
 
 # called by a partition worker
 sub index_skeleton {
        my ($self, $values) = @_;
-       my $w = $self->{w};
-       my $err;
-       my $str = freeze($values);
-       $str = length($str) . "\n" . $str;
+       if (my $w = $self->{w}) {
+               my $err;
+               my $str = freeze($values);
+               $str = length($str) . "\n" . $str;
 
-       # multiple processes write to the same pipe, so use flock
-       # We can't avoid this lock for <=PIPE_BUF writes, either,
-       # because those atomic writes can break up >PIPE_BUF ones
-       $self->lock_acquire;
-       print $w $str or $err = $!;
-       $self->lock_release;
+               # multiple processes write to the same pipe, so use flock
+               # We can't avoid this lock for <=PIPE_BUF writes, either,
+               # because those atomic writes can break up >PIPE_BUF ones
+               $self->lock_acquire;
+               print $w $str or $err = $!;
+               $self->lock_release;
 
-       die "print failed: $err\n" if $err;
+               die "print failed: $err\n" if $err;
+       } else {
+               $self->begin_txn_lazy;
+               index_skeleton_real($self, $values);
+       }
 }
 
 sub remote_remove {
@@ -148,7 +142,7 @@ sub index_skeleton_real ($$) {
 # write to the subprocess
 sub barrier_init {
        my ($self, $nparts) = @_;
-       my $w = $self->{w};
+       my $w = $self->{w} or return;
        my $err;
        $self->lock_acquire;
        print $w "barrier_init $nparts\n" or $err = "failed to write: $!\n";
@@ -158,7 +152,8 @@ sub barrier_init {
 
 sub barrier_wait {
        my ($self) = @_;
-       my $l = $self->{barrier_wait}->getline;
+       my $bw = $self->{barrier_wait} or return;
+       my $l = $bw->getline;
        $l eq "barrier_done\n" or die "bad response from barrier_wait: $l\n";
 }
 
index 261f9d91a0c7e7849eb21d22b6d48c6ffaf5b4ed..8e1363ea732895da043cb5080502d65afdbc66b1 100644 (file)
@@ -56,6 +56,7 @@ sub new {
                xap_rw => undef, # PublicInbox::V2SearchIdx
                xap_ro => undef,
                partitions => $nparts,
+               parallel => 1,
                transact_bytes => 0,
                lock_path => "$dir/inbox.lock",
                # limit each repo to 1GB or so
@@ -93,7 +94,7 @@ sub add {
        my $nparts = $self->{partitions};
        my $part = $num % $nparts;
        my $idx = $self->idx_part($part);
-       $idx->index_raw($len, $msgref, $num, $oid, $mid0);
+       $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
        my $n = $self->{transact_bytes} += $len;
        if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
                $self->checkpoint;
index 2808b7263109ebdf22953a707d38e6cc324e1874..e28e602ad026c3bb6b5790f8d33c12b137049762 100644 (file)
@@ -259,7 +259,9 @@ sub _importer_for {
                if ($v == 2) {
                        eval { require PublicInbox::V2Writable };
                        die "v2 not supported: $@\n" if $@;
-                       PublicInbox::V2Writable->new($inbox);
+                       my $v2w = PublicInbox::V2Writable->new($inbox);
+                       $v2w->{parallel} = 0;
+                       $v2w;
                } elsif ($v == 1) {
                        my $git = $inbox->git;
                        my $name = $inbox->{name};
index f7a60fbb9e829d61e411e0d0c99c9b933abe8d63..fdad1366475a079120f064667719543d73450910 100755 (executable)
@@ -83,8 +83,9 @@ if ($version >= 2) {
        };
        $ibx = PublicInbox::Inbox->new($ibx);
        my $v2w = PublicInbox::V2Writable->new($ibx, 1);
+       $v2w->{parallel} = 0;
+       $v2w->idx_init;
        $v2w->git_init(0);
-       $v2w->idx_init(0);
        $v2w->done;
 } elsif ($version == 1) {
        x(qw(git init -q --bare), $mainrepo);
index 771e8c1722efe7df3aa114ed144228c5bee89aa8..2088f3fe2f4de47c3560886d2e24477d9c628d1f 100644 (file)
@@ -108,10 +108,10 @@ if ('ensure git configs are correct') {
        ok($im->add($mime), 'message with multiple Message-ID');
        $im->done;
        my @found;
-       $ibx->search->reopen;
-       $ibx->search->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
+       my $srch = $ibx->search;
+       $srch->reopen->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
        is(scalar(@found), 1, 'message found by first MID');
-       $ibx->search->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
+       $srch->reopen->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
        is(scalar(@found), 2, 'message found by second MID');
        is($found[0]->{doc_id}, $found[1]->{doc_id}, 'same document');
        ok($found[1]->{doc_id} > 0, 'doc_id is positive');