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