]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
searchidxshard: chomp $eidx_key from pipe
[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         my $n = read($r, my $bref, $len) or die "read: $!\n";
61         $n == $len or die "short read: $n != $len\n";
62         PublicInbox::Eml->new(\$bref);
63 }
64
65 # this reads all the writes to $self->{w} from the parent process
66 sub shard_worker_loop ($$$$$) {
67         my ($self, $v2w, $r, $shard, $bnote) = @_;
68         $0 = "shard[$shard]";
69         $self->begin_txn_lazy;
70         while (my $line = readline($r)) {
71                 $v2w->{current_info} = "[$shard] $line";
72                 if ($line eq "commit\n") {
73                         $self->commit_txn_lazy;
74                 } elsif ($line eq "close\n") {
75                         $self->idx_release;
76                 } elsif ($line eq "barrier\n") {
77                         $self->commit_txn_lazy;
78                         # no need to lock < 512 bytes is atomic under POSIX
79                         print $bnote "barrier $shard\n" or
80                                         die "write failed for barrier $!\n";
81                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) ([0-9]+)\n\z/s) {
82                         $self->remove_by_oid($1, $2 + 0);
83                 } elsif ($line =~ s/\A\+X //) {
84                         my ($len, $docid, $oid, $eidx_key) =
85                                                         split(/ /, $line, 4);
86                         chomp $eidx_key;
87                         $self->add_eidx_info($docid, $oid, $eidx_key,
88                                                         eml($r, $len));
89                 } elsif ($line =~ s/\A-X //) {
90                         my ($len, $docid, $oid, $eidx_key) =
91                                                         split(/ /, $line, 4);
92                         chomp $eidx_key;
93                         $self->remove_eidx_info($docid, $oid, $eidx_key,
94                                                         eml($r, $len));
95                 } else {
96                         chomp $line;
97                         my $eidx_key;
98                         if ($line =~ s/\AX=(.+)\0//) {
99                                 $eidx_key = $1;
100                                 $v2w->{current_info} =~ s/\0/\\0 /;
101                         }
102                         # n.b. $mid may contain spaces(!)
103                         my ($len, $bytes, $num, $oid, $ds, $ts, $tid, $mid)
104                                 = split(/ /, $line, 8);
105                         $self->begin_txn_lazy;
106                         my $smsg = bless {
107                                 bytes => $bytes,
108                                 num => $num + 0,
109                                 blob => $oid,
110                                 mid => $mid,
111                                 tid => $tid,
112                                 ds => $ds,
113                                 ts => $ts,
114                         }, 'PublicInbox::Smsg';
115                         $smsg->{eidx_key} = $eidx_key if defined($eidx_key);
116                         $self->add_message(eml($r, $len), $smsg);
117                 }
118         }
119         $self->worker_done;
120 }
121
122 sub index_raw {
123         my ($self, $msgref, $eml, $smsg, $ibx) = @_;
124         if (my $w = $self->{w}) {
125                 my @ekey = $ibx ? ('X='.$ibx->eidx_key."\0") : ();
126                 $msgref //= \($eml->as_string);
127                 $smsg->{raw_bytes} //= length($$msgref);
128                 # mid must be last, it can contain spaces (but not LF)
129                 print $w @ekey, join(' ', @$smsg{qw(raw_bytes bytes
130                                                 num blob ds ts tid mid)}),
131                         "\n", $$msgref or die "failed to write shard $!\n";
132         } else {
133                 if ($eml) {
134                         undef($$msgref) if $msgref;
135                 } else { # --xapian-only + --sequential-shard:
136                         $eml = PublicInbox::Eml->new($msgref);
137                 }
138                 $self->begin_txn_lazy;
139                 $smsg->{eidx_key} = $ibx->eidx_key if $ibx;
140                 $self->add_message($eml, $smsg);
141         }
142 }
143
144 sub shard_add_eidx_info {
145         my ($self, $docid, $oid, $xibx, $eml) = @_;
146         my $eidx_key = $xibx->eidx_key;
147         if (my $w = $self->{w}) {
148                 my $hdr = $eml->header_obj->as_string;
149                 my $len = length($hdr);
150                 print $w "+X $len $docid $oid $eidx_key\n", $hdr or
151                         die "failed to write shard: $!";
152         } else {
153                 $self->add_eidx_info($docid, $oid, $eidx_key, $eml);
154         }
155 }
156
157 sub shard_remove_eidx_info {
158         my ($self, $docid, $oid, $xibx, $eml) = @_;
159         my $eidx_key = $xibx->eidx_key;
160         if (my $w = $self->{w}) {
161                 my $hdr = $eml->header_obj->as_string;
162                 my $len = length($hdr);
163                 print $w "-X $len $docid $oid $eidx_key\n", $hdr or
164                         die "failed to write shard: $!";
165         } else {
166                 $self->remove_eidx_info($docid, $oid, $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, $oid, $num) = @_;
208         if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
209                 print $w "D $oid $num\n" or die "failed to write remove $!";
210         } else { # same process
211                 $self->remove_by_oid($oid, $num);
212         }
213 }
214
215 1;