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
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:
21 $self->set_metadata_once;
23 $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
28 my ($self, $v2w, $shard) = @_;
30 pipe($r, $w) or die "pipe failed: $!\n";
33 defined $pid or die "fork failed: $!\n";
35 my $bnote = $v2w->atfork_child;
36 close $w or die "failed to close: $!";
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';
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};
49 close $r or die "failed to close: $!";
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);
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) = @_;
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") {
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) =
80 $self->add_eidx_info($docid, $oid, $eidx_key,
82 } elsif ($line =~ s/\A-X //) {
83 my ($len, $docid, $oid, $eidx_key) =
85 $self->remove_eidx_info($docid, $oid, $eidx_key,
90 if ($line =~ s/\AX(.+)\0//) {
93 # n.b. $mid may contain spaces(!)
94 my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
95 = split(/ /, $line, 8);
96 $self->begin_txn_lazy;
105 }, 'PublicInbox::Smsg';
106 $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
107 $self->add_message(eml($r, $len), $smsg);
114 my ($self, $msgref, $eml, $smsg, $ibx) = @_;
115 if (my $w = $self->{w}) {
117 print $w 'X', $ibx->eidx_key, "\0" or die
118 "failed to write shard: $!\n";
120 $msgref //= \($eml->as_string);
121 $smsg->{raw_bytes} //= length($$msgref);
122 # mid must be last, it can contain spaces (but not LF)
123 print $w join(' ', @$smsg{qw(raw_bytes bytes
124 num blob ds ts tid mid)}),
125 "\n", $$msgref or die "failed to write shard $!\n";
128 undef($$msgref) if $msgref;
129 } else { # --xapian-only + --sequential-shard:
130 $eml = PublicInbox::Eml->new($msgref);
132 $self->begin_txn_lazy;
133 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
134 $self->add_message($eml, $smsg);
138 sub shard_add_eidx_info {
139 my ($self, $docid, $oid, $xibx, $eml) = @_;
140 my $eidx_key = $xibx->eidx_key;
141 if (my $w = $self->{w}) {
142 my $hdr = $eml->header_obj->as_string;
143 my $len = length($hdr);
144 print $w "+X $len $docid $oid $eidx_key\n", $hdr or
145 die "failed to write shard: $!";
147 $self->add_eidx_info($docid, $oid, $eidx_key, $eml);
151 sub shard_remove_eidx_info {
152 my ($self, $docid, $oid, $xibx, $eml) = @_;
153 my $eidx_key = $xibx->eidx_key;
154 if (my $w = $self->{w}) {
155 my $hdr = $eml->header_obj->as_string;
156 my $len = length($hdr);
157 print $w "-X $len $docid $oid $eidx_key\n", $hdr or
158 die "failed to write shard: $!";
160 $self->remove_eidx_info($docid, $oid, $eidx_key, $eml);
165 close $_[0]->{w} or die "failed to close write pipe: $!\n";
170 if (my $w = $self->{w}) {
171 print $w "barrier\n" or die "failed to print: $!";
173 $self->commit_txn_lazy;
179 if (my $w = $self->{w}) {
180 print $w "commit\n" or die "failed to write commit: $!";
182 $self->commit_txn_lazy;
188 if (my $w = delete $self->{w}) {
189 my $pid = delete $self->{pid} or die "no process to wait on\n";
190 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
191 close $w or die "failed to close pipe for pid:$pid: $!\n";
192 waitpid($pid, 0) == $pid or die "remote process did not finish";
193 $? == 0 or die ref($self)." pid:$pid exited with: $?";
195 die "transaction in progress $self\n" if $self->{txn};
196 $self->idx_release if $self->{xdb};
201 my ($self, $oid, $num) = @_;
202 if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
203 print $w "D $oid $num\n" or die "failed to write remove $!";
204 } else { # same process
205 $self->remove_by_oid($oid, $num);