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>
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 # 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: $!";
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';
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};
52 PublicInbox::Sigfd::sig_setmask($oldset);
55 close $r or die "failed to close: $!";
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);
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) = @_;
70 $self->begin_txn_lazy;
71 while (my $line = readline($r)) {
72 $v2w->{current_info} = "[$shard] $line";
73 if ($line eq "commit\n") {
74 $self->commit_txn_lazy;
75 } elsif ($line eq "close\n") {
77 } elsif ($line eq "barrier\n") {
78 $self->commit_txn_lazy;
79 # no need to lock < 512 bytes is atomic under POSIX
80 print $bnote "barrier $shard\n" or
81 die "write failed for barrier $!\n";
82 } elsif ($line =~ /\AD ([a-f0-9]{40,}) ([0-9]+)\n\z/s) {
83 $self->remove_by_oid($1, $2 + 0);
84 } elsif ($line =~ s/\A\+X //) {
85 my ($len, $docid, $oid, $eidx_key) =
88 $self->add_eidx_info($docid, $oid, $eidx_key,
90 } elsif ($line =~ s/\A-X //) {
91 my ($len, $docid, $oid, $eidx_key) =
94 $self->remove_eidx_info($docid, $oid, $eidx_key,
96 } elsif ($line =~ s/\AO ([^\n]+)\n//) {
98 $over_fn =~ tr/\0/\n/;
99 $self->over_check(PublicInbox::Over->new($over_fn));
103 if ($line =~ s/\AX=(.+)\0//) {
105 $v2w->{current_info} =~ s/\0/\\0 /;
107 # n.b. $mid may contain spaces(!)
108 my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
109 = split(/ /, $line, 8);
110 $self->begin_txn_lazy;
119 }, 'PublicInbox::Smsg';
120 $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
121 $self->add_message(eml($r, $len), $smsg);
128 my ($self, $msgref, $eml, $smsg, $ibx) = @_;
129 if (my $w = $self->{w}) {
130 my @ekey = $ibx ? ('X='.$ibx->eidx_key."\0") : ();
131 $msgref //= \($eml->as_string);
132 $smsg->{raw_bytes} //= length($$msgref);
133 # mid must be last, it can contain spaces (but not LF)
134 print $w @ekey, join(' ', @$smsg{qw(raw_bytes bytes
135 num blob ds ts tid mid)}),
136 "\n", $$msgref or die "failed to write shard $!\n";
139 undef($$msgref) if $msgref;
140 } else { # --xapian-only + --sequential-shard:
141 $eml = PublicInbox::Eml->new($msgref);
143 $self->begin_txn_lazy;
144 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
145 $self->add_message($eml, $smsg);
149 sub shard_add_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: $!";
158 $self->add_eidx_info($docid, $oid, $eidx_key, $eml);
162 sub shard_remove_eidx_info {
163 my ($self, $docid, $oid, $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 $oid $eidx_key\n", $hdr or
168 die "failed to write shard: $!";
170 $self->remove_eidx_info($docid, $oid, $eidx_key, $eml);
175 close $_[0]->{w} or die "failed to close write pipe: $!\n";
180 if (my $w = $self->{w}) {
181 print $w "barrier\n" or die "failed to print: $!";
183 $self->commit_txn_lazy;
189 if (my $w = $self->{w}) {
190 print $w "commit\n" or die "failed to write commit: $!";
192 $self->commit_txn_lazy;
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: $?";
205 die "transaction in progress $self\n" if $self->{txn};
206 $self->idx_release if $self->{xdb};
211 my ($self, $oid, $num) = @_;
212 if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
213 print $w "D $oid $num\n" or die "failed to write remove $!";
214 } else { # same process
215 $self->remove_by_oid($oid, $num);
219 sub shard_over_check {
220 my ($self, $over) = @_;
221 if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
222 my ($over_fn) = $over->{dbh}->sqlite_db_filename;
223 $over_fn =~ tr/\n/\0/;
224 print $w "O $over_fn\n" or die "failed to write over $!";
226 $self->over_check($over);