]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
a41477cd2e5295879769e408138e16189babff67
[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 use PublicInbox::Sigfd;
14
15 sub new {
16         my ($class, $v2w, $shard) = @_; # v2w may be ExtSearchIdx
17         my $ibx = $v2w->{ibx};
18         my $self = $ibx ? $class->SUPER::new($ibx, 1, $shard)
19                         : $class->eidx_shard_new($v2w, $shard);
20         # create the DB before forking:
21         $self->idx_acquire;
22         $self->set_metadata_once;
23         $self->idx_release;
24         $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
25         $self;
26 }
27
28 sub spawn_worker {
29         my ($self, $v2w, $shard) = @_;
30         my ($r, $w);
31         pipe($r, $w) or die "pipe failed: $!\n";
32         $w->autoflush(1);
33         my $oldset = PublicInbox::Sigfd::block_signals();
34         my $pid = fork;
35         defined $pid or die "fork failed: $!\n";
36         if ($pid == 0) {
37                 eval { PublicInbox::DS->Reset };
38                 # these signals are localized in parent
39                 $SIG{$_} = 'IGNORE' for (qw(TERM INT QUIT));
40                 PublicInbox::Sigfd::sig_setmask($oldset);
41                 my $bnote = $v2w->atfork_child;
42                 close $w or die "failed to close: $!";
43
44                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
45                 # speeds V2Writable batch imports across 8 cores by nearly 20%
46                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
47
48                 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
49                 die "worker $shard died: $@\n" if $@;
50                 die "unexpected MM $self->{mm}" if $self->{mm};
51                 exit;
52         }
53         PublicInbox::Sigfd::sig_setmask($oldset);
54         $self->{pid} = $pid;
55         $self->{w} = $w;
56         close $r or die "failed to close: $!";
57 }
58
59 sub eml ($$) {
60         my ($r, $len) = @_;
61         return if $len == 0;
62         my $n = read($r, my $bref, $len) or die "read: $!\n";
63         $n == $len or die "short read: $n != $len\n";
64         PublicInbox::Eml->new(\$bref);
65 }
66
67 # this reads all the writes to $self->{w} from the parent process
68 sub shard_worker_loop ($$$$$) {
69         my ($self, $v2w, $r, $shard, $bnote) = @_;
70         $0 = "shard[$shard]";
71         $self->begin_txn_lazy;
72         while (my $line = readline($r)) {
73                 chomp $line;
74                 $v2w->{current_info} = "[$shard] $line";
75                 if ($line eq 'commit') {
76                         $self->commit_txn_lazy;
77                 } elsif ($line eq 'close') {
78                         $self->idx_release;
79                 } elsif ($line eq 'barrier') {
80                         $self->commit_txn_lazy;
81                         # no need to lock < 512 bytes is atomic under POSIX
82                         print $bnote "barrier $shard\n" or
83                                         die "write failed for barrier $!\n";
84                 } elsif ($line =~ /\AD ([0-9]+)\z/s) {
85                         $self->remove_by_docid($1 + 0);
86                 } elsif ($line =~ s/\A\+X //) {
87                         my ($len, $docid, $eidx_key) = split(/ /, $line, 3);
88                         $self->add_eidx_info($docid, $eidx_key, eml($r, $len));
89                 } elsif ($line =~ s/\A-X //) {
90                         my ($len, $docid, $eidx_key) = split(/ /, $line, 3);
91                         $self->remove_eidx_info($docid, $eidx_key,
92                                                         eml($r, $len));
93                 } elsif ($line =~ s/\A=K (\d+) //) {
94                         $self->set_keywords($1 + 0, split(/ /, $line));
95                 } elsif ($line =~ s/\A-K (\d+) //) {
96                         $self->remove_keywords($1 + 0, split(/ /, $line));
97                 } elsif ($line =~ s/\A\+K (\d+) //) {
98                         $self->add_keywords($1 + 0, split(/ /, $line));
99                 } elsif ($line =~ s/\AO ([^\n]+)//) {
100                         my $over_fn = $1;
101                         $over_fn =~ tr/\0/\n/;
102                         $self->over_check(PublicInbox::Over->new($over_fn));
103                 } else {
104                         my $eidx_key;
105                         if ($line =~ s/\AX=(.+)\0//) {
106                                 $eidx_key = $1;
107                                 $v2w->{current_info} =~ s/\0/\\0 /;
108                         }
109                         # n.b. $mid may contain spaces(!)
110                         my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
111                                 = split(/ /, $line, 8);
112                         $self->begin_txn_lazy;
113                         my $smsg = bless {
114                                 bytes => $bytes,
115                                 num => $num + 0,
116                                 blob => $oid,
117                                 mid => $mid,
118                                 tid => $tid,
119                                 ds => $ds,
120                                 ts => $ts,
121                         }, 'PublicInbox::Smsg';
122                         $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
123                         $self->add_message(eml($r, $len), $smsg);
124                 }
125         }
126         $self->worker_done;
127 }
128
129 sub index_raw {
130         my ($self, $msgref, $eml, $smsg, $eidx_key) = @_;
131         if (my $w = $self->{w}) {
132                 my @ekey = defined($eidx_key) ? ("X=$eidx_key\0") : ();
133                 $msgref //= \($eml->as_string);
134                 $smsg->{raw_bytes} //= length($$msgref);
135                 # mid must be last, it can contain spaces (but not LF)
136                 print $w @ekey, join(' ', @$smsg{qw(raw_bytes bytes
137                                                 num blob ds ts tid mid)}),
138                         "\n", $$msgref or die "failed to write shard $!\n";
139         } else {
140                 if ($eml) {
141                         undef($$msgref) if $msgref;
142                 } else { # --xapian-only + --sequential-shard:
143                         $eml = PublicInbox::Eml->new($msgref);
144                 }
145                 $self->begin_txn_lazy;
146                 $smsg->{eidx_key} = $eidx_key if defined $eidx_key;
147                 $self->add_message($eml, $smsg);
148         }
149 }
150
151 sub shard_add_eidx_info {
152         my ($self, $docid, $eidx_key, $eml) = @_;
153         if (my $w = $self->{w}) {
154                 my $hdr = $eml->header_obj->as_string;
155                 my $len = length($hdr);
156                 print $w "+X $len $docid $eidx_key\n", $hdr or
157                         die "failed to write shard: $!";
158         } else {
159                 $self->add_eidx_info($docid, $eidx_key, $eml);
160         }
161 }
162
163 sub shard_remove_eidx_info {
164         my ($self, $docid, $eidx_key, $eml) = @_;
165         if (my $w = $self->{w}) {
166                 my $hdr = $eml ? $eml->header_obj->as_string : '';
167                 my $len = length($hdr);
168                 print $w "-X $len $docid $eidx_key\n", $hdr or
169                         die "failed to write shard: $!";
170         } else {
171                 $self->remove_eidx_info($docid, $eidx_key, $eml);
172         }
173 }
174
175 sub atfork_child {
176         close $_[0]->{w} or die "failed to close write pipe: $!\n";
177 }
178
179 sub shard_barrier {
180         my ($self) = @_;
181         if (my $w = $self->{w}) {
182                 print $w "barrier\n" or die "failed to print: $!";
183         } else {
184                 $self->commit_txn_lazy;
185         }
186 }
187
188 sub shard_commit {
189         my ($self) = @_;
190         if (my $w = $self->{w}) {
191                 print $w "commit\n" or die "failed to write commit: $!";
192         } else {
193                 $self->commit_txn_lazy;
194         }
195 }
196
197 sub shard_close {
198         my ($self) = @_;
199         if (my $w = delete $self->{w}) {
200                 my $pid = delete $self->{pid} or die "no process to wait on\n";
201                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
202                 close $w or die "failed to close pipe for pid:$pid: $!\n";
203                 waitpid($pid, 0) == $pid or die "remote process did not finish";
204                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
205         } else {
206                 die "transaction in progress $self\n" if $self->{txn};
207                 $self->idx_release if $self->{xdb};
208         }
209 }
210
211 sub shard_remove {
212         my ($self, $num) = @_;
213         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
214                 print $w "D $num\n" or die "failed to write remove $!";
215         } else { # same process
216                 $self->remove_by_docid($num);
217         }
218 }
219
220 sub shard_set_keywords {
221         my ($self, $docid, @kw) = @_;
222         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
223                 print $w "=K $docid @kw\n" or die "failed to write: $!";
224         } else { # same process
225                 $self->set_keywords($docid, @kw);
226         }
227 }
228
229 sub shard_remove_keywords {
230         my ($self, $docid, @kw) = @_;
231         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
232                 print $w "-K $docid @kw\n" or die "failed to write: $!";
233         } else { # same process
234                 $self->remove_keywords($docid, @kw);
235         }
236 }
237
238 sub shard_add_keywords {
239         my ($self, $docid, @kw) = @_;
240         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
241                 print $w "+K $docid @kw\n" or die "failed to write: $!";
242         } else { # same process
243                 $self->add_keywords($docid, @kw);
244         }
245 }
246
247 sub shard_over_check {
248         my ($self, $over) = @_;
249         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
250                 my ($over_fn) = $over->{dbh}->sqlite_db_filename;
251                 $over_fn =~ tr/\n/\0/;
252                 print $w "O $over_fn\n" or die "failed to write over $!";
253         } else {
254                 $self->over_check($over);
255         }
256 }
257
258 1;