]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxPart.pm
v2writable: cleanup unused pipes in partitions
[public-inbox.git] / lib / PublicInbox / SearchIdxPart.pm
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;
4 use strict;
5 use warnings;
6 use base qw(PublicInbox::SearchIdx);
7
8 sub new {
9         my ($class, $v2writable, $part, $threader) = @_;
10         my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $part);
11         $self->{threader} = $threader;
12         my ($r, $w);
13         pipe($r, $w) or die "pipe failed: $!\n";
14         my $pid = fork;
15         defined $pid or die "fork failed: $!\n";
16         if ($pid == 0) {
17                 $v2writable->atfork_child;
18                 $v2writable = undef;
19                 close $w;
20
21                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
22                 # speeds V2Writable batch imports across 8 cores by nearly 20%
23                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
24
25                 eval { partition_worker_loop($self, $r) };
26                 die "worker $part died: $@\n" if $@;
27                 die "unexpected MM $self->{mm}" if $self->{mm};
28                 exit;
29         }
30         $self->{pid} = $pid;
31         $self->{w} = $w;
32         close $r;
33         $self;
34 }
35
36 sub partition_worker_loop ($$) {
37         my ($self, $r) = @_;
38         my $xdb = $self->_xdb_acquire;
39         $xdb->begin_transaction;
40         my $txn = 1;
41         while (my $line = $r->getline) {
42                 if ($line eq "commit\n") {
43                         $xdb->commit_transaction if $txn;
44                         $txn = undef;
45                 } elsif ($line eq "close\n") {
46                         $self->_xdb_release;
47                         $xdb = $txn = undef;
48                 } else {
49                         my ($len, $artnum, $object_id) = split(/ /, $line);
50                         $xdb ||= $self->_xdb_acquire;
51                         if (!$txn) {
52                                 $xdb->begin_transaction;
53                                 $txn = 1;
54                         }
55                         my $n = read($r, my $msg, $len) or die "read: $!\n";
56                         $n == $len or die "short read: $n != $len\n";
57                         my $mime = PublicInbox::MIME->new(\$msg);
58                         $self->index_blob($mime, $len, $artnum, $object_id);
59                 }
60         }
61         warn "$$ still in transaction\n" if $txn;
62         warn "$$ xdb active\n" if $xdb;
63 }
64
65 # called by V2Writable
66 sub index_raw {
67         my ($self, $len, $msgref, $artnum, $object_id) = @_;
68         my $w = $self->{w};
69         print $w "$len $artnum $object_id\n", $$msgref or die
70                 "failed to write partition $!\n";
71         $w->flush or die "failed to flush: $!\n";
72 }
73
74 sub atfork_child {
75         close $_[0]->{w} or die "failed to close write pipe: $!\n";
76 }
77
78 1;