]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
extindex: preliminary --reindex support
[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 use PublicInbox::Sigfd;
14
15 sub new {
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:
21         $self->idx_acquire;
22         $self->set_metadata_once;
23         $self->idx_release;
24         $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
25         $self;
26 }
27
28 sub spawn_worker {
29         my ($self, $v2w, $shard) = @_;
30         my ($r, $w);
31         pipe($r, $w) or die "pipe failed: $!\n";
32         $w->autoflush(1);
33         my $oldset = PublicInbox::Sigfd::block_signals();
34         my $pid = fork;
35         defined $pid or die "fork failed: $!\n";
36         if ($pid == 0) {
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: $!";
42
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';
46
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};
50                 exit;
51         }
52         PublicInbox::Sigfd::sig_setmask($oldset);
53         $self->{pid} = $pid;
54         $self->{w} = $w;
55         close $r or die "failed to close: $!";
56 }
57
58 sub eml ($$) {
59         my ($r, $len) = @_;
60         return if $len == 0;
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);
64 }
65
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) = @_;
69         $0 = "shard[$shard]";
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") {
76                         $self->idx_release;
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 ([0-9]+)\n\z/s) {
83                         $self->remove_by_docid($1 + 0);
84                 } elsif ($line =~ s/\A\+X //) {
85                         my ($len, $docid, $eidx_key) = split(/ /, $line, 3);
86                         chomp $eidx_key;
87                         $self->add_eidx_info($docid, $eidx_key, eml($r, $len));
88                 } elsif ($line =~ s/\A-X //) {
89                         my ($len, $docid, $eidx_key) = split(/ /, $line, 3);
90                         chomp $eidx_key;
91                         $self->remove_eidx_info($docid, $eidx_key,
92                                                         eml($r, $len));
93                 } elsif ($line =~ s/\AO ([^\n]+)\n//) {
94                         my $over_fn = $1;
95                         $over_fn =~ tr/\0/\n/;
96                         $self->over_check(PublicInbox::Over->new($over_fn));
97                 } elsif ($line =~ /\AE ([0-9]+)\n/) {
98                         $self->reindex_docid($1 + 0);
99                 } else {
100                         chomp $line;
101                         my $eidx_key;
102                         if ($line =~ s/\AX=(.+)\0//) {
103                                 $eidx_key = $1;
104                                 $v2w->{current_info} =~ s/\0/\\0 /;
105                         }
106                         # n.b. $mid may contain spaces(!)
107                         my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
108                                 = split(/ /, $line, 8);
109                         $self->begin_txn_lazy;
110                         my $smsg = bless {
111                                 bytes => $bytes,
112                                 num => $num + 0,
113                                 blob => $oid,
114                                 mid => $mid,
115                                 tid => $tid,
116                                 ds => $ds,
117                                 ts => $ts,
118                         }, 'PublicInbox::Smsg';
119                         $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
120                         $self->add_message(eml($r, $len), $smsg);
121                 }
122         }
123         $self->worker_done;
124 }
125
126 sub index_raw {
127         my ($self, $msgref, $eml, $smsg, $ibx) = @_;
128         if (my $w = $self->{w}) {
129                 my @ekey = $ibx ? ('X='.$ibx->eidx_key."\0") : ();
130                 $msgref //= \($eml->as_string);
131                 $smsg->{raw_bytes} //= length($$msgref);
132                 # mid must be last, it can contain spaces (but not LF)
133                 print $w @ekey, join(' ', @$smsg{qw(raw_bytes bytes
134                                                 num blob ds ts tid mid)}),
135                         "\n", $$msgref or die "failed to write shard $!\n";
136         } else {
137                 if ($eml) {
138                         undef($$msgref) if $msgref;
139                 } else { # --xapian-only + --sequential-shard:
140                         $eml = PublicInbox::Eml->new($msgref);
141                 }
142                 $self->begin_txn_lazy;
143                 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
144                 $self->add_message($eml, $smsg);
145         }
146 }
147
148 sub shard_add_eidx_info {
149         my ($self, $docid, $eidx_key, $eml) = @_;
150         if (my $w = $self->{w}) {
151                 my $hdr = $eml->header_obj->as_string;
152                 my $len = length($hdr);
153                 print $w "+X $len $docid $eidx_key\n", $hdr or
154                         die "failed to write shard: $!";
155         } else {
156                 $self->add_eidx_info($docid, $eidx_key, $eml);
157         }
158 }
159
160 sub shard_remove_eidx_info {
161         my ($self, $docid, $eidx_key, $eml) = @_;
162         if (my $w = $self->{w}) {
163                 my $hdr = $eml ? $eml->header_obj->as_string : '';
164                 my $len = length($hdr);
165                 print $w "-X $len $docid $eidx_key\n", $hdr or
166                         die "failed to write shard: $!";
167         } else {
168                 $self->remove_eidx_info($docid, $eidx_key, $eml);
169         }
170 }
171
172 sub atfork_child {
173         close $_[0]->{w} or die "failed to close write pipe: $!\n";
174 }
175
176 sub shard_barrier {
177         my ($self) = @_;
178         if (my $w = $self->{w}) {
179                 print $w "barrier\n" or die "failed to print: $!";
180         } else {
181                 $self->commit_txn_lazy;
182         }
183 }
184
185 sub shard_commit {
186         my ($self) = @_;
187         if (my $w = $self->{w}) {
188                 print $w "commit\n" or die "failed to write commit: $!";
189         } else {
190                 $self->commit_txn_lazy;
191         }
192 }
193
194 sub shard_close {
195         my ($self) = @_;
196         if (my $w = delete $self->{w}) {
197                 my $pid = delete $self->{pid} or die "no process to wait on\n";
198                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
199                 close $w or die "failed to close pipe for pid:$pid: $!\n";
200                 waitpid($pid, 0) == $pid or die "remote process did not finish";
201                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
202         } else {
203                 die "transaction in progress $self\n" if $self->{txn};
204                 $self->idx_release if $self->{xdb};
205         }
206 }
207
208 sub shard_remove {
209         my ($self, $num) = @_;
210         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
211                 print $w "D $num\n" or die "failed to write remove $!";
212         } else { # same process
213                 $self->remove_by_docid($num);
214         }
215 }
216
217 sub shard_over_check {
218         my ($self, $over) = @_;
219         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
220                 my ($over_fn) = $over->{dbh}->sqlite_db_filename;
221                 $over_fn =~ tr/\n/\0/;
222                 print $w "O $over_fn\n" or die "failed to write over $!";
223         } else {
224                 $self->over_check($over);
225         }
226 }
227
228 sub shard_reindex_docid {
229         my ($self, $docid) = @_;
230         if (my $w = $self->{w}) {
231                 print $w "E $docid\n" or die "failed to write to shard: $!";
232         } else {
233                 $self->reindex_docid($docid);
234         }
235 }
236
237 1;