]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
*idx: pass $smsg in more places instead of many args
[public-inbox.git] / lib / PublicInbox / SearchIdxShard.pm
1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # used to interface with a single Xapian shard in V2 repos.
5 # See L<public-inbox-v2-format(5)> for more info on how we shard Xapian
6 package PublicInbox::SearchIdxShard;
7 use strict;
8 use warnings;
9 use base qw(PublicInbox::SearchIdx);
10 use IO::Handle (); # autoflush
11
12 sub new {
13         my ($class, $v2writable, $shard) = @_;
14         my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $shard);
15         # create the DB before forking:
16         $self->_xdb_acquire;
17         $self->_xdb_release;
18         $self->spawn_worker($v2writable, $shard) if $v2writable->{parallel};
19         $self;
20 }
21
22 sub spawn_worker {
23         my ($self, $v2w, $shard) = @_;
24         my ($r, $w);
25         pipe($r, $w) or die "pipe failed: $!\n";
26         binmode $r, ':raw';
27         binmode $w, ':raw';
28         $w->autoflush(1);
29         my $pid = fork;
30         defined $pid or die "fork failed: $!\n";
31         if ($pid == 0) {
32                 my $bnote = $v2w->atfork_child;
33                 close $w or die "failed to close: $!";
34
35                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
36                 # speeds V2Writable batch imports across 8 cores by nearly 20%
37                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
38
39                 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
40                 die "worker $shard died: $@\n" if $@;
41                 die "unexpected MM $self->{mm}" if $self->{mm};
42                 exit;
43         }
44         $self->{pid} = $pid;
45         $self->{w} = $w;
46         close $r or die "failed to close: $!";
47 }
48
49 sub shard_worker_loop ($$$$$) {
50         my ($self, $v2w, $r, $shard, $bnote) = @_;
51         $0 = "pi-v2-shard[$shard]";
52         $self->begin_txn_lazy;
53         while (my $line = $r->getline) {
54                 $v2w->{current_info} = "[$shard] $line";
55                 if ($line eq "commit\n") {
56                         $self->commit_txn_lazy;
57                 } elsif ($line eq "close\n") {
58                         $self->_xdb_release;
59                 } elsif ($line eq "barrier\n") {
60                         $self->commit_txn_lazy;
61                         # no need to lock < 512 bytes is atomic under POSIX
62                         print $bnote "barrier $shard\n" or
63                                         die "write failed for barrier $!\n";
64                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
65                         my ($oid, $mid) = ($1, $2);
66                         $self->begin_txn_lazy;
67                         $self->remove_by_oid($oid, $mid);
68                 } else {
69                         chomp $line;
70                         my ($len, $artnum, $oid, $mid0, $autime, $cotime) =
71                                                         split(/ /, $line);
72                         $self->begin_txn_lazy;
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->{autime} = $autime;
78                         $self->{cotime} = $cotime;
79                         my $smsg = bless {
80                                 bytes => $len,
81                                 num => $artnum,
82                                 blob => $oid,
83                                 mid => $mid0,
84                         }, 'PublicInbox::Smsg';
85                         $self->add_message($mime, $smsg);
86                 }
87         }
88         $self->worker_done;
89 }
90
91 # called by V2Writable
92 sub index_raw {
93         my ($self, $bytes, $msgref, $artnum, $oid, $mid0, $mime, $times) = @_;
94         my $at = $times->{autime} // time;
95         my $ct = $times->{cotime} // time;
96         if (my $w = $self->{w}) {
97                 print $w "$bytes $artnum $oid $mid0 $at $ct\n", $$msgref or
98                         die "failed to write shard $!\n";
99         } else {
100                 $$msgref = undef;
101                 $self->begin_txn_lazy;
102                 $self->{autime} = $at;
103                 $self->{cotime} = $ct;
104                 my $smsg = bless {
105                         bytes => $bytes,
106                         num => $artnum,
107                         blob => $oid,
108                         mid => $mid0,
109                 }, 'PublicInbox::Smsg';
110                 $self->add_message($mime, $smsg);
111         }
112 }
113
114 sub atfork_child {
115         close $_[0]->{w} or die "failed to close write pipe: $!\n";
116 }
117
118 # called by V2Writable:
119 sub remote_barrier {
120         my ($self) = @_;
121         if (my $w = $self->{w}) {
122                 print $w "barrier\n" or die "failed to print: $!";
123         } else {
124                 $self->commit_txn_lazy;
125         }
126 }
127
128 1;