]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
rename {pi_config} fields to {pi_cfg}
[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                 } else {
98                         chomp $line;
99                         my $eidx_key;
100                         if ($line =~ s/\AX=(.+)\0//) {
101                                 $eidx_key = $1;
102                                 $v2w->{current_info} =~ s/\0/\\0 /;
103                         }
104                         # n.b. $mid may contain spaces(!)
105                         my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
106                                 = split(/ /, $line, 8);
107                         $self->begin_txn_lazy;
108                         my $smsg = bless {
109                                 bytes => $bytes,
110                                 num => $num + 0,
111                                 blob => $oid,
112                                 mid => $mid,
113                                 tid => $tid,
114                                 ds => $ds,
115                                 ts => $ts,
116                         }, 'PublicInbox::Smsg';
117                         $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
118                         $self->add_message(eml($r, $len), $smsg);
119                 }
120         }
121         $self->worker_done;
122 }
123
124 sub index_raw {
125         my ($self, $msgref, $eml, $smsg, $ibx) = @_;
126         if (my $w = $self->{w}) {
127                 my @ekey = $ibx ? ('X='.$ibx->eidx_key."\0") : ();
128                 $msgref //= \($eml->as_string);
129                 $smsg->{raw_bytes} //= length($$msgref);
130                 # mid must be last, it can contain spaces (but not LF)
131                 print $w @ekey, join(' ', @$smsg{qw(raw_bytes bytes
132                                                 num blob ds ts tid mid)}),
133                         "\n", $$msgref or die "failed to write shard $!\n";
134         } else {
135                 if ($eml) {
136                         undef($$msgref) if $msgref;
137                 } else { # --xapian-only + --sequential-shard:
138                         $eml = PublicInbox::Eml->new($msgref);
139                 }
140                 $self->begin_txn_lazy;
141                 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
142                 $self->add_message($eml, $smsg);
143         }
144 }
145
146 sub shard_add_eidx_info {
147         my ($self, $docid, $eidx_key, $eml) = @_;
148         if (my $w = $self->{w}) {
149                 my $hdr = $eml->header_obj->as_string;
150                 my $len = length($hdr);
151                 print $w "+X $len $docid $eidx_key\n", $hdr or
152                         die "failed to write shard: $!";
153         } else {
154                 $self->add_eidx_info($docid, $eidx_key, $eml);
155         }
156 }
157
158 sub shard_remove_eidx_info {
159         my ($self, $docid, $eidx_key, $eml) = @_;
160         if (my $w = $self->{w}) {
161                 my $hdr = $eml ? $eml->header_obj->as_string : '';
162                 my $len = length($hdr);
163                 print $w "-X $len $docid $eidx_key\n", $hdr or
164                         die "failed to write shard: $!";
165         } else {
166                 $self->remove_eidx_info($docid, $eidx_key, $eml);
167         }
168 }
169
170 sub atfork_child {
171         close $_[0]->{w} or die "failed to close write pipe: $!\n";
172 }
173
174 sub shard_barrier {
175         my ($self) = @_;
176         if (my $w = $self->{w}) {
177                 print $w "barrier\n" or die "failed to print: $!";
178         } else {
179                 $self->commit_txn_lazy;
180         }
181 }
182
183 sub shard_commit {
184         my ($self) = @_;
185         if (my $w = $self->{w}) {
186                 print $w "commit\n" or die "failed to write commit: $!";
187         } else {
188                 $self->commit_txn_lazy;
189         }
190 }
191
192 sub shard_close {
193         my ($self) = @_;
194         if (my $w = delete $self->{w}) {
195                 my $pid = delete $self->{pid} or die "no process to wait on\n";
196                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
197                 close $w or die "failed to close pipe for pid:$pid: $!\n";
198                 waitpid($pid, 0) == $pid or die "remote process did not finish";
199                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
200         } else {
201                 die "transaction in progress $self\n" if $self->{txn};
202                 $self->idx_release if $self->{xdb};
203         }
204 }
205
206 sub shard_remove {
207         my ($self, $num) = @_;
208         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
209                 print $w "D $num\n" or die "failed to write remove $!";
210         } else { # same process
211                 $self->remove_by_docid($num);
212         }
213 }
214
215 sub shard_over_check {
216         my ($self, $over) = @_;
217         if (my $w = $self->{w}) { # triggers remove_by_docid in a shard child
218                 my ($over_fn) = $over->{dbh}->sqlite_db_filename;
219                 $over_fn =~ tr/\n/\0/;
220                 print $w "O $over_fn\n" or die "failed to write over $!";
221         } else {
222                 $self->over_check($over);
223         }
224 }
225
226 1;