]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei_mail_sync: do not use transactions
authorEric Wong <e@80x24.org>
Thu, 2 Sep 2021 10:17:56 +0000 (10:17 +0000)
committerEric Wong <e@80x24.org>
Thu, 2 Sep 2021 10:18:36 +0000 (10:18 +0000)
For lei-index to work in parallel with MUA access and upcoming
inotify-based updates, mail_sync.sqlite3 needs to always be
up-to-date to read-only worker processes (ahead of everything
else).  So rely on the default auto-commit behavior and hope
SQLite WAL can reduce some of the overheads involved with
writes.

lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/LeiStore.pm
t/lei_mail_sync.t

index f8834a27720145e0d4f3a09bd47919a0d5cc96a2..5a10c127c1f8e2db1c080f20530ed90385c3d461 100644 (file)
@@ -32,9 +32,7 @@ sub new {
        bless { filename => $f, fmap => {} }, $cls;
 }
 
-sub lms_commit { delete($_[0]->{dbh})->commit }
-
-sub lms_begin { ($_[0]->{dbh} //= dbh_new($_[0], 1))->begin_work };
+sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0], 1)) };
 
 sub create_tables {
        my ($dbh) = @_;
@@ -468,14 +466,4 @@ sub imap_oid {
        $oidbin ? unpack('H*', $oidbin) : undef;
 }
 
-# FIXED? something with "lei <up|q>" is causing uncommitted transaction
-# TODO: remove soon
-sub DESTROY {
-       my ($self) = @_;
-       my $dbh = delete($self->{dbh}) or return;
-       return if $dbh->{ReadOnly};
-       undef $dbh;
-       warn "BUG $$ $0 $self {dbh} OPEN ppid=".getppid.' '.Carp::longmess();
-}
-
 1;
index ab39043e9048ff68e357ac0693b0a4e7e7ccb93e..6c557d9976f078ac799c64579d4ed8e955e53001 100644 (file)
@@ -202,7 +202,7 @@ sub _lms_rw ($) {
                require PublicInbox::LeiMailSync;
                my $f = "$self->{priv_eidx}->{topdir}/mail_sync.sqlite3";
                my $lms = PublicInbox::LeiMailSync->new($f);
-               $lms->lms_begin;
+               $lms->lms_write_prepare;
                $lms;
        };
 }
@@ -450,9 +450,7 @@ sub checkpoint {
        if (my $im = $self->{im}) {
                $wait ? $im->barrier : $im->checkpoint;
        }
-       if (my $lms = delete $self->{lms}) {
-               $lms->lms_commit;
-       }
+       delete $self->{lms};
        $self->{priv_eidx}->checkpoint($wait);
 }
 
@@ -481,9 +479,7 @@ sub done {
                        warn $err;
                }
        }
-       if (my $lms = delete $self->{lms}) {
-               $lms->lms_commit;
-       }
+       delete $self->{lms};
        $self->{priv_eidx}->done; # V2Writable::done
        xchg_stderr($self);
        die $err if $err;
index 5daa49cd9e6c233e69a42caf8c4e10aea8c0aa55..4439b818168024b1f28d388f706bb7ef4a9c9055 100644 (file)
@@ -9,17 +9,15 @@ require_ok 'PublicInbox::LeiMailSync';
 my ($dir, $for_destroy) = tmpdir();
 my $lms = PublicInbox::LeiMailSync->new("$dir/t.sqlite3");
 
-$lms->lms_begin;
-$lms->lms_commit;
+$lms->lms_write_prepare;
 my $ro = PublicInbox::LeiMailSync->new("$dir/t.sqlite3");
 is_deeply([$ro->folders], [], 'no folders, yet');
 
 my $imap = 'imaps://bob@[::1]/INBOX;UIDVALIDITY=9';
-$lms->lms_begin;
+$lms->lms_write_prepare;
 my $deadbeef = "\xde\xad\xbe\xef";
 is($lms->set_src($deadbeef, $imap, 1), 1, 'set IMAP once');
 ok($lms->set_src($deadbeef, $imap, 1) == 0, 'set IMAP idempotently');
-$lms->lms_commit;
 is_deeply([$ro->folders], [$imap], 'IMAP folder added');
 is_deeply([$ro->folders($imap)], [$imap], 'IMAP folder with full GLOB');
 is_deeply([$ro->folders('imaps://bob@[::1]/INBOX')], [$imap],
@@ -30,24 +28,21 @@ is_deeply($ro->locations_for($deadbeef),
 
 my $maildir = 'maildir:/home/user/md';
 my $fname = 'foo:2,S';
-$lms->lms_begin;
+$lms->lms_write_prepare;
 ok($lms->set_src($deadbeef, $maildir, \$fname), 'set Maildir once');
 ok($lms->set_src($deadbeef, $maildir, \$fname) == 0, 'set Maildir again');
-$lms->lms_commit;
 is_deeply($ro->locations_for($deadbeef),
        { $imap => [ 1 ], $maildir => [ $fname ] },
        'locations_for w/ maildir + imap');
 
 if ('mess things up pretend old bug') {
-       $lms->lms_begin;
+       $lms->lms_write_prepare;
        $lms->{dbh}->do('UPDATE folders SET loc = ? WHERE loc = ?', undef,
                        "$maildir/", $maildir);
        ok(delete $lms->{fmap}, 'clear folder map');
-       $lms->lms_commit;
 
-       $lms->lms_begin;
+       $lms->lms_write_prepare;
        ok($lms->set_src($deadbeef, $maildir, \$fname), 'set Maildir once');
-       $lms->lms_commit;
 };
 
 is_deeply([sort($ro->folders)], [$imap, $maildir], 'both folders shown');
@@ -70,12 +65,11 @@ is_deeply($ro->location_stats($maildir), { 'name.count' => 1 },
 is_deeply($ro->location_stats($imap),
        { 'uid.count' => 1, 'uid.max' => 1, 'uid.min' => 1 },
        'IMAP location stats');
-$lms->lms_begin;
+$lms->lms_write_prepare;
 is($lms->clear_src($imap, 1), 1, 'clear_src on IMAP');
 is($lms->clear_src($maildir, \$fname), 1, 'clear_src on Maildir');
 ok($lms->clear_src($imap, 1) == 0, 'clear_src again on IMAP');
 ok($lms->clear_src($maildir, \$fname) == 0, 'clear_src again on Maildir');
-$lms->lms_commit;
 is_deeply($ro->location_stats($maildir), {}, 'nothing left');
 
 done_testing;