1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::SearchIdxPart;
6 use base qw(PublicInbox::SearchIdx);
9 my ($class, $v2writable, $part, $skel) = @_;
10 my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $part);
11 $self->{skeleton} = $skel;
13 pipe($r, $w) or die "pipe failed: $!\n";
17 defined $pid or die "fork failed: $!\n";
19 $v2writable->atfork_child;
23 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
24 # speeds V2Writable batch imports across 8 cores by nearly 20%
25 fcntl($r, 1031, 1048576) if $^O eq 'linux';
27 eval { partition_worker_loop($self, $r, $part) };
28 die "worker $part died: $@\n" if $@;
29 die "unexpected MM $self->{mm}" if $self->{mm};
38 sub partition_worker_loop ($$$) {
39 my ($self, $r, $part) = @_;
40 $0 = "pi-v2-partition[$part]";
41 my $xdb = $self->_xdb_acquire;
42 $xdb->begin_transaction;
44 while (my $line = $r->getline) {
45 if ($line eq "commit\n") {
46 $xdb->commit_transaction if $txn;
48 $self->{skeleton}->remote_commit;
49 } elsif ($line eq "close\n") {
52 } elsif ($line eq "barrier\n") {
53 $xdb->commit_transaction if $txn;
55 print { $self->{skeleton}->{w} } "barrier $part\n" or
56 die "write failed to skeleton: $!\n";
57 } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
58 my ($oid, $mid) = ($1, $2);
59 $xdb ||= $self->_xdb_acquire;
61 $xdb->begin_transaction;
64 $self->remove_by_oid($oid, $mid);
67 my ($len, $artnum, $oid, $mid0) = split(/ /, $line);
68 $xdb ||= $self->_xdb_acquire;
70 $xdb->begin_transaction;
73 my $n = read($r, my $msg, $len) or die "read: $!\n";
74 $n == $len or die "short read: $n != $len\n";
75 my $mime = PublicInbox::MIME->new(\$msg);
76 $artnum = int($artnum);
77 $self->add_message($mime, $n, $artnum, $oid, $mid0);
80 warn "$$ still in transaction\n" if $txn;
81 warn "$$ xdb active\n" if $xdb;
84 # called by V2Writable
86 my ($self, $len, $msgref, $artnum, $object_id, $mid0) = @_;
88 print $w "$len $artnum $object_id $mid0\n", $$msgref or die
89 "failed to write partition $!\n";
90 $w->flush or die "failed to flush: $!\n";
94 close $_[0]->{w} or die "failed to close write pipe: $!\n";
97 # called by V2Writable:
100 print $w "barrier\n" or die "failed to print: $!";
101 $w->flush or die "failed to flush: $!";