]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
searchidxshard: ensure we set indexlevel on shard[0]
[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 $ibx = $v2writable->{-inbox};
15         my $self = $class->SUPER::new($ibx, 1, $shard);
16         # create the DB before forking:
17         $self->_xdb_acquire;
18         $self->set_indexlevel;
19         $self->_xdb_release;
20         $self->spawn_worker($v2writable, $shard) if $v2writable->{parallel};
21         $self;
22 }
23
24 sub spawn_worker {
25         my ($self, $v2w, $shard) = @_;
26         my ($r, $w);
27         pipe($r, $w) or die "pipe failed: $!\n";
28         binmode $r, ':raw';
29         binmode $w, ':raw';
30         $w->autoflush(1);
31         my $pid = fork;
32         defined $pid or die "fork failed: $!\n";
33         if ($pid == 0) {
34                 my $bnote = $v2w->atfork_child;
35                 close $w or die "failed to close: $!";
36
37                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
38                 # speeds V2Writable batch imports across 8 cores by nearly 20%
39                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
40
41                 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
42                 die "worker $shard died: $@\n" if $@;
43                 die "unexpected MM $self->{mm}" if $self->{mm};
44                 exit;
45         }
46         $self->{pid} = $pid;
47         $self->{w} = $w;
48         close $r or die "failed to close: $!";
49 }
50
51 sub shard_worker_loop ($$$$$) {
52         my ($self, $v2w, $r, $shard, $bnote) = @_;
53         $0 = "pi-v2-shard[$shard]";
54         $self->begin_txn_lazy;
55         while (my $line = $r->getline) {
56                 $v2w->{current_info} = "[$shard] $line";
57                 if ($line eq "commit\n") {
58                         $self->commit_txn_lazy;
59                 } elsif ($line eq "close\n") {
60                         $self->_xdb_release;
61                 } elsif ($line eq "barrier\n") {
62                         $self->commit_txn_lazy;
63                         # no need to lock < 512 bytes is atomic under POSIX
64                         print $bnote "barrier $shard\n" or
65                                         die "write failed for barrier $!\n";
66                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
67                         my ($oid, $mid) = ($1, $2);
68                         $self->begin_txn_lazy;
69                         $self->remove_by_oid($oid, $mid);
70                 } else {
71                         chomp $line;
72                         my ($bytes, $num, $blob, $mid, $ds, $ts) =
73                                                         split(/ /, $line);
74                         $self->begin_txn_lazy;
75                         my $n = read($r, my $msg, $bytes) or die "read: $!\n";
76                         $n == $bytes or die "short read: $n != $bytes\n";
77                         my $mime = PublicInbox::MIME->new(\$msg);
78                         my $smsg = bless {
79                                 bytes => $bytes,
80                                 num => $num + 0,
81                                 blob => $blob,
82                                 mid => $mid,
83                                 ds => $ds,
84                                 ts => $ts,
85                         }, 'PublicInbox::Smsg';
86                         $self->add_message($mime, $smsg);
87                 }
88         }
89         $self->worker_done;
90 }
91
92 # called by V2Writable
93 sub index_raw {
94         my ($self, $msgref, $mime, $smsg) = @_;
95         if (my $w = $self->{w}) {
96                 print $w join(' ', @$smsg{qw(bytes num blob mid ds ts)}),
97                         "\n", $$msgref or die "failed to write shard $!\n";
98         } else {
99                 $$msgref = undef;
100                 $self->begin_txn_lazy;
101                 $self->add_message($mime, $smsg);
102         }
103 }
104
105 sub atfork_child {
106         close $_[0]->{w} or die "failed to close write pipe: $!\n";
107 }
108
109 # called by V2Writable:
110 sub remote_barrier {
111         my ($self) = @_;
112         if (my $w = $self->{w}) {
113                 print $w "barrier\n" or die "failed to print: $!";
114         } else {
115                 $self->commit_txn_lazy;
116         }
117 }
118
119 1;