]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
searchidxshard: reduce syscalls when writing ->eidx_key
[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 # Internal interface for a single Xapian shard in V2 inboxes.
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 v5.10.1;
9 use parent qw(PublicInbox::SearchIdx);
10 use bytes qw(length);
11 use IO::Handle (); # autoflush
12 use PublicInbox::Eml;
13
14 sub new {
15         my ($class, $v2w, $shard) = @_; # v2w may be ExtSearchIdx
16         my $ibx = $v2w->{ibx};
17         my $self = $ibx ? $class->SUPER::new($ibx, 1, $shard)
18                         : $class->eidx_shard_new($v2w, $shard);
19         # create the DB before forking:
20         $self->idx_acquire;
21         $self->set_metadata_once;
22         $self->idx_release;
23         $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
24         $self;
25 }
26
27 sub spawn_worker {
28         my ($self, $v2w, $shard) = @_;
29         my ($r, $w);
30         pipe($r, $w) or die "pipe failed: $!\n";
31         $w->autoflush(1);
32         my $pid = fork;
33         defined $pid or die "fork failed: $!\n";
34         if ($pid == 0) {
35                 my $bnote = $v2w->atfork_child;
36                 close $w or die "failed to close: $!";
37
38                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
39                 # speeds V2Writable batch imports across 8 cores by nearly 20%
40                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
41
42                 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
43                 die "worker $shard died: $@\n" if $@;
44                 die "unexpected MM $self->{mm}" if $self->{mm};
45                 exit;
46         }
47         $self->{pid} = $pid;
48         $self->{w} = $w;
49         close $r or die "failed to close: $!";
50 }
51
52 sub eml ($$) {
53         my ($r, $len) = @_;
54         my $n = read($r, my $bref, $len) or die "read: $!\n";
55         $n == $len or die "short read: $n != $len\n";
56         PublicInbox::Eml->new(\$bref);
57 }
58
59 # this reads all the writes to $self->{w} from the parent process
60 sub shard_worker_loop ($$$$$) {
61         my ($self, $v2w, $r, $shard, $bnote) = @_;
62         $0 = "shard[$shard]";
63         $self->begin_txn_lazy;
64         while (my $line = readline($r)) {
65                 $v2w->{current_info} = "[$shard] $line";
66                 if ($line eq "commit\n") {
67                         $self->commit_txn_lazy;
68                 } elsif ($line eq "close\n") {
69                         $self->idx_release;
70                 } elsif ($line eq "barrier\n") {
71                         $self->commit_txn_lazy;
72                         # no need to lock < 512 bytes is atomic under POSIX
73                         print $bnote "barrier $shard\n" or
74                                         die "write failed for barrier $!\n";
75                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) ([0-9]+)\n\z/s) {
76                         $self->remove_by_oid($1, $2 + 0);
77                 } elsif ($line =~ s/\A\+X //) {
78                         my ($len, $docid, $oid, $eidx_key) =
79                                                         split(/ /, $line, 4);
80                         $self->add_eidx_info($docid, $oid, $eidx_key,
81                                                         eml($r, $len));
82                 } elsif ($line =~ s/\A-X //) {
83                         my ($len, $docid, $oid, $eidx_key) =
84                                                         split(/ /, $line, 4);
85                         $self->remove_eidx_info($docid, $oid, $eidx_key,
86                                                         eml($r, $len));
87                 } else {
88                         chomp $line;
89                         my $eidx_key;
90                         if ($line =~ s/\AX=(.+)\0//) {
91                                 $eidx_key = $1;
92                                 $v2w->{current_info} =~ s/\0/\\0/;
93                         }
94                         # n.b. $mid may contain spaces(!)
95                         my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
96                                 = split(/ /, $line, 8);
97                         $self->begin_txn_lazy;
98                         my $smsg = bless {
99                                 bytes => $bytes,
100                                 num => $num + 0,
101                                 blob => $oid,
102                                 mid => $mid,
103                                 tid => $tid,
104                                 ds => $ds,
105                                 ts => $ts,
106                         }, 'PublicInbox::Smsg';
107                         $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
108                         $self->add_message(eml($r, $len), $smsg);
109                 }
110         }
111         $self->worker_done;
112 }
113
114 sub index_raw {
115         my ($self, $msgref, $eml, $smsg, $ibx) = @_;
116         if (my $w = $self->{w}) {
117                 my @ekey = $ibx ? ('X='.$ibx->eidx_key."\0") : ();
118                 $msgref //= \($eml->as_string);
119                 $smsg->{raw_bytes} //= length($$msgref);
120                 # mid must be last, it can contain spaces (but not LF)
121                 print $w @ekey, join(' ', @$smsg{qw(raw_bytes bytes
122                                                 num blob ds ts tid mid)}),
123                         "\n", $$msgref or die "failed to write shard $!\n";
124         } else {
125                 if ($eml) {
126                         undef($$msgref) if $msgref;
127                 } else { # --xapian-only + --sequential-shard:
128                         $eml = PublicInbox::Eml->new($msgref);
129                 }
130                 $self->begin_txn_lazy;
131                 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
132                 $self->add_message($eml, $smsg);
133         }
134 }
135
136 sub shard_add_eidx_info {
137         my ($self, $docid, $oid, $xibx, $eml) = @_;
138         my $eidx_key = $xibx->eidx_key;
139         if (my $w = $self->{w}) {
140                 my $hdr = $eml->header_obj->as_string;
141                 my $len = length($hdr);
142                 print $w "+X $len $docid $oid $eidx_key\n", $hdr or
143                         die "failed to write shard: $!";
144         } else {
145                 $self->add_eidx_info($docid, $oid, $eidx_key, $eml);
146         }
147 }
148
149 sub shard_remove_eidx_info {
150         my ($self, $docid, $oid, $xibx, $eml) = @_;
151         my $eidx_key = $xibx->eidx_key;
152         if (my $w = $self->{w}) {
153                 my $hdr = $eml->header_obj->as_string;
154                 my $len = length($hdr);
155                 print $w "-X $len $docid $oid $eidx_key\n", $hdr or
156                         die "failed to write shard: $!";
157         } else {
158                 $self->remove_eidx_info($docid, $oid, $eidx_key, $eml);
159         }
160 }
161
162 sub atfork_child {
163         close $_[0]->{w} or die "failed to close write pipe: $!\n";
164 }
165
166 sub shard_barrier {
167         my ($self) = @_;
168         if (my $w = $self->{w}) {
169                 print $w "barrier\n" or die "failed to print: $!";
170         } else {
171                 $self->commit_txn_lazy;
172         }
173 }
174
175 sub shard_commit {
176         my ($self) = @_;
177         if (my $w = $self->{w}) {
178                 print $w "commit\n" or die "failed to write commit: $!";
179         } else {
180                 $self->commit_txn_lazy;
181         }
182 }
183
184 sub shard_close {
185         my ($self) = @_;
186         if (my $w = delete $self->{w}) {
187                 my $pid = delete $self->{pid} or die "no process to wait on\n";
188                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
189                 close $w or die "failed to close pipe for pid:$pid: $!\n";
190                 waitpid($pid, 0) == $pid or die "remote process did not finish";
191                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
192         } else {
193                 die "transaction in progress $self\n" if $self->{txn};
194                 $self->idx_release if $self->{xdb};
195         }
196 }
197
198 sub shard_remove {
199         my ($self, $oid, $num) = @_;
200         if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
201                 print $w "D $oid $num\n" or die "failed to write remove $!";
202         } else { # same process
203                 $self->remove_by_oid($oid, $num);
204         }
205 }
206
207 1;