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") {
54 my ($len, $artnum, $object_id) = split(/ /, $line);
55 $xdb ||= $self->_xdb_acquire;
57 $xdb->begin_transaction;
60 my $n = read($r, my $msg, $len) or die "read: $!\n";
61 $n == $len or die "short read: $n != $len\n";
62 my $mime = PublicInbox::MIME->new(\$msg);
63 $artnum = int($artnum);
64 $self->add_message($mime, $n, $artnum, $object_id);
67 warn "$$ still in transaction\n" if $txn;
68 warn "$$ xdb active\n" if $xdb;
71 # called by V2Writable
73 my ($self, $len, $msgref, $artnum, $object_id) = @_;
75 print $w "$len $artnum $object_id\n", $$msgref or die
76 "failed to write partition $!\n";
77 $w->flush or die "failed to flush: $!\n";
81 close $_[0]->{w} or die "failed to close write pipe: $!\n";