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