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) = @_;
16 my $ibx = $v2w->{ibx};
17 my $self = $class->SUPER::new($ibx, 1, $shard);
18 # create the DB before forking:
20 $self->set_metadata_once;
22 $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
27 my ($self, $v2w, $shard) = @_;
29 pipe($r, $w) or die "pipe failed: $!\n";
32 defined $pid or die "fork failed: $!\n";
34 my $bnote = $v2w->atfork_child;
35 close $w or die "failed to close: $!";
37 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
38 # speeds V2Writable batch imports across 8 cores by nearly 20%
39 fcntl($r, 1031, 1048576) if $^O eq 'linux';
41 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
42 die "worker $shard died: $@\n" if $@;
43 die "unexpected MM $self->{mm}" if $self->{mm};
48 close $r or die "failed to close: $!";
53 my $n = read($r, my $bref, $len) or die "read: $!\n";
54 $n == $len or die "short read: $n != $len\n";
55 PublicInbox::Eml->new(\$bref);
58 # this reads all the writes to $self->{w} from the parent process
59 sub shard_worker_loop ($$$$$) {
60 my ($self, $v2w, $r, $shard, $bnote) = @_;
61 $0 = "pi-v2-shard[$shard]";
62 $self->begin_txn_lazy;
63 while (my $line = readline($r)) {
64 $v2w->{current_info} = "[$shard] $line";
65 if ($line eq "commit\n") {
66 $self->commit_txn_lazy;
67 } elsif ($line eq "close\n") {
69 } elsif ($line eq "barrier\n") {
70 $self->commit_txn_lazy;
71 # no need to lock < 512 bytes is atomic under POSIX
72 print $bnote "barrier $shard\n" or
73 die "write failed for barrier $!\n";
74 } elsif ($line =~ /\AD ([a-f0-9]{40,}) ([0-9]+)\n\z/s) {
75 $self->remove_by_oid($1, $2 + 0);
76 } elsif ($line =~ s/\A\+X //) {
77 my ($len, $docid, $xnum, $oid, $ng_or_dir) =
79 $self->add_xref3($docid, $xnum, $oid, $ng_or_dir,
81 } elsif ($line =~ s/\A-X //) {
82 my ($len, $docid, $xnum, $oid, $ng_or_dir) =
84 $self->remove_xref3($docid, $xnum, $oid,
85 $ng_or_dir, eml($r, $len));
88 # n.b. $mid may contain spaces(!)
89 my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
90 = split(/ /, $line, 8);
91 $self->begin_txn_lazy;
100 }, 'PublicInbox::Smsg';
101 $self->add_message(eml($r, $len), $smsg);
108 my ($self, $msgref, $eml, $smsg) = @_;
109 if (my $w = $self->{w}) {
110 # mid must be last, it can contain spaces (but not LF)
111 print $w join(' ', @$smsg{qw(raw_bytes bytes
112 num blob ds ts tid mid)}),
113 "\n", $$msgref or die "failed to write shard $!\n";
117 } else { # --xapian-only + --sequential-shard:
118 $eml = PublicInbox::Eml->new($msgref);
120 $self->begin_txn_lazy;
121 $self->add_message($eml, $smsg);
125 sub shard_add_xref3 {
126 my ($self, $docid, $xnum, $oid, $xibx, $eml) = @_;
127 my $ng_or_dir = $xibx->{newsgroup} // $xibx->{inboxdir};
128 if (my $w = $self->{w}) {
129 my $hdr = $eml->header_obj->as_string;
130 my $len = length($hdr);
131 print $w "+X $len $docid $xnum $oid $ng_or_dir\n", $hdr or
132 die "failed to write shard: $!";
134 $self->add_xref3($docid, $xnum, $oid, $ng_or_dir, $eml);
138 sub shard_remove_xref3 {
139 my ($self, $docid, $oid, $xibx, $eml) = @_;
140 my $ng_or_dir = $xibx->{newsgroup} // $xibx->{inboxdir};
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 $ng_or_dir\n", $hdr or
145 die "failed to write shard: $!";
147 $self->remove_xref3($docid, $oid, $ng_or_dir, $eml);
152 close $_[0]->{w} or die "failed to close write pipe: $!\n";
157 if (my $w = $self->{w}) {
158 print $w "barrier\n" or die "failed to print: $!";
160 $self->commit_txn_lazy;
166 if (my $w = $self->{w}) {
167 print $w "commit\n" or die "failed to write commit: $!";
169 $self->commit_txn_lazy;
175 if (my $w = delete $self->{w}) {
176 my $pid = delete $self->{pid} or die "no process to wait on\n";
177 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
178 close $w or die "failed to close pipe for pid:$pid: $!\n";
179 waitpid($pid, 0) == $pid or die "remote process did not finish";
180 $? == 0 or die ref($self)." pid:$pid exited with: $?";
182 die "transaction in progress $self\n" if $self->{txn};
183 $self->idx_release if $self->{xdb};
188 my ($self, $oid, $num) = @_;
189 if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
190 print $w "D $oid $num\n" or die "failed to write remove $!";
191 } else { # same process
192 $self->remove_by_oid($oid, $num);