]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxPart.pm
82f5c1bcfd1581adb583792d58eda88d4fa74a57
[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, $skel) = @_;
10         my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $part);
11         $self->{skeleton} = $skel;
12         # create the DB:
13         $self->_xdb_acquire;
14         $self->_xdb_release;
15         $self->spawn_worker($v2writable, $part) if $v2writable->{parallel};
16         $self;
17 }
18
19 sub spawn_worker {
20         my ($self, $v2writable, $part) = @_;
21         my ($r, $w);
22         pipe($r, $w) or die "pipe failed: $!\n";
23         binmode $r, ':raw';
24         binmode $w, ':raw';
25         my $pid = fork;
26         defined $pid or die "fork failed: $!\n";
27         if ($pid == 0) {
28                 $v2writable->atfork_child;
29                 $v2writable = undef;
30                 close $w;
31
32                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
33                 # speeds V2Writable batch imports across 8 cores by nearly 20%
34                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
35
36                 eval { partition_worker_loop($self, $r, $part) };
37                 die "worker $part died: $@\n" if $@;
38                 die "unexpected MM $self->{mm}" if $self->{mm};
39                 exit;
40         }
41         $self->{pid} = $pid;
42         $self->{w} = $w;
43         close $r;
44 }
45
46 sub partition_worker_loop ($$$) {
47         my ($self, $r, $part) = @_;
48         $0 = "pi-v2-partition[$part]";
49         $self->begin_txn_lazy;
50         while (my $line = $r->getline) {
51                 if ($line eq "commit\n") {
52                         $self->commit_txn_lazy;
53                         $self->{skeleton}->remote_commit;
54                 } elsif ($line eq "close\n") {
55                         $self->_xdb_release;
56                 } elsif ($line eq "barrier\n") {
57                         $self->commit_txn_lazy;
58                         print { $self->{skeleton}->{w} } "barrier $part\n" or
59                                         die "write failed to skeleton: $!\n";
60                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
61                         my ($oid, $mid) = ($1, $2);
62                         $self->begin_txn_lazy;
63                         $self->remove_by_oid($oid, $mid);
64                 } else {
65                         chomp $line;
66                         my ($len, $artnum, $oid, $mid0) = split(/ /, $line);
67                         $self->begin_txn_lazy;
68                         my $n = read($r, my $msg, $len) or die "read: $!\n";
69                         $n == $len or die "short read: $n != $len\n";
70                         my $mime = PublicInbox::MIME->new(\$msg);
71                         $artnum = int($artnum);
72                         $self->add_message($mime, $n, $artnum, $oid, $mid0);
73                 }
74         }
75         $self->worker_done;
76 }
77
78 # called by V2Writable
79 sub index_raw {
80         my ($self, $bytes, $msgref, $artnum, $oid, $mid0, $mime) = @_;
81         if (my $w = $self->{w}) {
82                 print $w "$bytes $artnum $oid $mid0\n", $$msgref or die
83                         "failed to write partition $!\n";
84                 $w->flush or die "failed to flush: $!\n";
85         } else {
86                 $$msgref = undef;
87                 $self->begin_txn_lazy;
88                 $self->add_message($mime, $bytes, $artnum, $oid, $mid0);
89         }
90 }
91
92 sub atfork_child {
93         close $_[0]->{w} or die "failed to close write pipe: $!\n";
94 }
95
96 # called by V2Writable:
97 sub remote_barrier {
98         my ($self) = @_;
99         if (my $w = $self->{w}) {
100                 print $w "barrier\n" or die "failed to print: $!";
101                 $w->flush or die "failed to flush: $!";
102         } else {
103                 $self->commit_txn_lazy;
104                 $self->{skeleton}->remote_commit;
105         }
106 }
107
108 1;