1 # Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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;
9 use parent qw(PublicInbox::SearchIdx);
11 use IO::Handle (); # autoflush
13 use PublicInbox::Sigfd;
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:
22 $self->set_metadata_once;
24 $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
29 my ($self, $v2w, $shard) = @_;
31 pipe($r, $w) or die "pipe failed: $!\n";
33 my $oldset = PublicInbox::Sigfd::block_signals();
35 defined $pid or die "fork failed: $!\n";
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: $!";
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';
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};
53 PublicInbox::Sigfd::sig_setmask($oldset);
56 close $r or die "failed to close: $!";
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);
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) = @_;
71 $self->begin_txn_lazy;
72 while (my $line = readline($r)) {
74 $v2w->{current_info} = "[$shard] $line";
75 if ($line eq 'commit') {
76 $self->commit_txn_lazy;
77 } elsif ($line eq 'close') {
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,
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]+)//) {
101 $over_fn =~ tr/\0/\n/;
102 $self->over_check(PublicInbox::Over->new($over_fn));
105 if ($line =~ s/\AX=(.+)\0//) {
107 $v2w->{current_info} =~ s/\0/\\0 /;
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;
121 }, 'PublicInbox::Smsg';
122 $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
123 $self->add_message(eml($r, $len), $smsg);
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";
141 undef($$msgref) if $msgref;
142 } else { # --xapian-only + --sequential-shard:
143 $eml = PublicInbox::Eml->new($msgref);
145 $self->begin_txn_lazy;
146 $smsg->{eidx_key} = $eidx_key if defined $eidx_key;
147 $self->add_message($eml, $smsg);
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: $!";
159 $self->add_eidx_info($docid, $eidx_key, $eml);
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: $!";
171 $self->remove_eidx_info($docid, $eidx_key, $eml);
176 close $_[0]->{w} or die "failed to close write pipe: $!\n";
181 if (my $w = $self->{w}) {
182 print $w "barrier\n" or die "failed to print: $!";
184 $self->commit_txn_lazy;
190 if (my $w = $self->{w}) {
191 print $w "commit\n" or die "failed to write commit: $!";
193 $self->commit_txn_lazy;
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: $?";
206 die "transaction in progress $self\n" if $self->{txn};
207 $self->idx_release if $self->{xdb};
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);
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);
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);
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);
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 $!";
254 $self->over_check($over);