]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
inboxwritable: eidx_key for external index
[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
14 sub new {
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:
19         $self->idx_acquire;
20         $self->set_metadata_once;
21         $self->idx_release;
22         $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
23         $self;
24 }
25
26 sub spawn_worker {
27         my ($self, $v2w, $shard) = @_;
28         my ($r, $w);
29         pipe($r, $w) or die "pipe failed: $!\n";
30         $w->autoflush(1);
31         my $pid = fork;
32         defined $pid or die "fork failed: $!\n";
33         if ($pid == 0) {
34                 my $bnote = $v2w->atfork_child;
35                 close $w or die "failed to close: $!";
36
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';
40
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};
44                 exit;
45         }
46         $self->{pid} = $pid;
47         $self->{w} = $w;
48         close $r or die "failed to close: $!";
49 }
50
51 sub eml ($$) {
52         my ($r, $len) = @_;
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);
56 }
57
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") {
68                         $self->idx_release;
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, $eidx_key) =
78                                                         split(/ /, $line, 5);
79                         $self->add_xref3($docid, $xnum, $oid, $eidx_key,
80                                                 eml($r, $len));
81                 } elsif ($line =~ s/\A-X //) {
82                         my ($len, $docid, $xnum, $oid, $eidx_key) =
83                                                         split(/ /, $line, 5);
84                         $self->remove_xref3($docid, $xnum, $oid,
85                                                 $eidx_key, eml($r, $len));
86                 } else {
87                         chomp $line;
88                         my $eidx_key;
89                         if ($line =~ s/\AX(.+)\0//) {
90                                 $eidx_key = $1;
91                         }
92                         # n.b. $mid may contain spaces(!)
93                         my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
94                                 = split(/ /, $line, 8);
95                         $self->begin_txn_lazy;
96                         my $smsg = bless {
97                                 bytes => $bytes,
98                                 num => $num + 0,
99                                 blob => $oid,
100                                 mid => $mid,
101                                 tid => $tid,
102                                 ds => $ds,
103                                 ts => $ts,
104                         }, 'PublicInbox::Smsg';
105                         $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
106                         $self->add_message(eml($r, $len), $smsg);
107                 }
108         }
109         $self->worker_done;
110 }
111
112 sub index_raw {
113         my ($self, $msgref, $eml, $smsg, $ibx) = @_;
114         if (my $w = $self->{w}) {
115                 if ($ibx) {
116                         print $w 'X', $ibx->eidx_key, "\0" or die
117                                 "failed to write shard: $!\n";
118                 }
119                 $msgref //= \($eml->as_string);
120                 $smsg->{raw_bytes} //= length($$msgref);
121                 # mid must be last, it can contain spaces (but not LF)
122                 print $w join(' ', @$smsg{qw(raw_bytes bytes
123                                                 num blob ds ts tid mid)}),
124                         "\n", $$msgref or die "failed to write shard $!\n";
125         } else {
126                 if ($eml) {
127                         undef($$msgref) if $msgref;
128                 } else { # --xapian-only + --sequential-shard:
129                         $eml = PublicInbox::Eml->new($msgref);
130                 }
131                 $self->begin_txn_lazy;
132                 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
133                 $self->add_message($eml, $smsg);
134         }
135 }
136
137 sub shard_add_xref3 {
138         my ($self, $docid, $xnum, $oid, $xibx, $eml) = @_;
139         my $eidx_key = $xibx->eidx_key;
140         if (my $w = $self->{w}) {
141                 my $hdr = $eml->header_obj->as_string;
142                 my $len = length($hdr);
143                 print $w "+X $len $docid $xnum $oid $eidx_key\n", $hdr or
144                         die "failed to write shard: $!";
145         } else {
146                 $self->add_xref3($docid, $xnum, $oid, $eidx_key, $eml);
147         }
148 }
149
150 sub shard_remove_xref3 {
151         my ($self, $docid, $oid, $xibx, $eml) = @_;
152         my $eidx_key = $xibx->eidx_key;
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 $oid $eidx_key\n", $hdr or
157                         die "failed to write shard: $!";
158         } else {
159                 $self->remove_xref3($docid, $oid, $eidx_key, $eml);
160         }
161 }
162
163 sub atfork_child {
164         close $_[0]->{w} or die "failed to close write pipe: $!\n";
165 }
166
167 sub shard_barrier {
168         my ($self) = @_;
169         if (my $w = $self->{w}) {
170                 print $w "barrier\n" or die "failed to print: $!";
171         } else {
172                 $self->commit_txn_lazy;
173         }
174 }
175
176 sub shard_commit {
177         my ($self) = @_;
178         if (my $w = $self->{w}) {
179                 print $w "commit\n" or die "failed to write commit: $!";
180         } else {
181                 $self->commit_txn_lazy;
182         }
183 }
184
185 sub shard_close {
186         my ($self) = @_;
187         if (my $w = delete $self->{w}) {
188                 my $pid = delete $self->{pid} or die "no process to wait on\n";
189                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
190                 close $w or die "failed to close pipe for pid:$pid: $!\n";
191                 waitpid($pid, 0) == $pid or die "remote process did not finish";
192                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
193         } else {
194                 die "transaction in progress $self\n" if $self->{txn};
195                 $self->idx_release if $self->{xdb};
196         }
197 }
198
199 sub shard_remove {
200         my ($self, $oid, $num) = @_;
201         if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
202                 print $w "D $oid $num\n" or die "failed to write remove $!";
203         } else { # same process
204                 $self->remove_by_oid($oid, $num);
205         }
206 }
207
208 1;