]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxPart.pm
c166078331a6f59d690182369d56a01a25657648
[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         my ($r, $w);
13         pipe($r, $w) or die "pipe failed: $!\n";
14         binmode $r, ':raw';
15         binmode $w, ':raw';
16         my $pid = fork;
17         defined $pid or die "fork failed: $!\n";
18         if ($pid == 0) {
19                 $v2writable->atfork_child;
20                 $v2writable = undef;
21                 close $w;
22
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';
26
27                 eval { partition_worker_loop($self, $r, $part) };
28                 die "worker $part died: $@\n" if $@;
29                 die "unexpected MM $self->{mm}" if $self->{mm};
30                 exit;
31         }
32         $self->{pid} = $pid;
33         $self->{w} = $w;
34         close $r;
35         $self;
36 }
37
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;
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                         $self->{skeleton}->remote_commit;
49                 } elsif ($line eq "close\n") {
50                         $self->_xdb_release;
51                         $xdb = $txn = undef;
52                 } elsif ($line eq "barrier\n") {
53                         $xdb->commit_transaction if $txn;
54                         $txn = undef;
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;
60                         if (!$txn) {
61                                 $xdb->begin_transaction;
62                                 $txn = 1;
63                         }
64                         $self->remove_by_oid($oid, $mid);
65                 } else {
66                         chomp $line;
67                         my ($len, $artnum, $oid, $mid0) = split(/ /, $line);
68                         $xdb ||= $self->_xdb_acquire;
69                         if (!$txn) {
70                                 $xdb->begin_transaction;
71                                 $txn = 1;
72                         }
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);
78                 }
79         }
80         warn "$$ still in transaction\n" if $txn;
81         warn "$$ xdb active\n" if $xdb;
82 }
83
84 # called by V2Writable
85 sub index_raw {
86         my ($self, $len, $msgref, $artnum, $object_id, $mid0) = @_;
87         my $w = $self->{w};
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";
91 }
92
93 sub atfork_child {
94         close $_[0]->{w} or die "failed to close write pipe: $!\n";
95 }
96
97 # called by V2Writable:
98 sub barrier {
99         my $w = $_[0]->{w};
100         print $w "barrier\n" or die "failed to print: $!";
101         $w->flush or die "failed to flush: $!";
102 }
103
104 1;