]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
searchidxshard: allow msgref to be undef
[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, $ng_or_dir) =
78                                                         split(/ /, $line, 5);
79                         $self->add_xref3($docid, $xnum, $oid, $ng_or_dir,
80                                                 eml($r, $len));
81                 } elsif ($line =~ s/\A-X //) {
82                         my ($len, $docid, $xnum, $oid, $ng_or_dir) =
83                                                         split(/ /, $line, 5);
84                         $self->remove_xref3($docid, $xnum, $oid,
85                                                 $ng_or_dir, eml($r, $len));
86                 } else {
87                         chomp $line;
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;
92                         my $smsg = bless {
93                                 bytes => $bytes,
94                                 num => $num + 0,
95                                 blob => $oid,
96                                 mid => $mid,
97                                 tid => $tid,
98                                 ds => $ds,
99                                 ts => $ts,
100                         }, 'PublicInbox::Smsg';
101                         $self->add_message(eml($r, $len), $smsg);
102                 }
103         }
104         $self->worker_done;
105 }
106
107 sub index_raw {
108         my ($self, $msgref, $eml, $smsg) = @_;
109         if (my $w = $self->{w}) {
110                 $msgref //= \($eml->as_string);
111                 $smsg->{raw_bytes} //= length($$msgref);
112                 # mid must be last, it can contain spaces (but not LF)
113                 print $w join(' ', @$smsg{qw(raw_bytes bytes
114                                                 num blob ds ts tid mid)}),
115                         "\n", $$msgref or die "failed to write shard $!\n";
116         } else {
117                 if ($eml) {
118                         undef($$msgref) if $msgref;
119                 } else { # --xapian-only + --sequential-shard:
120                         $eml = PublicInbox::Eml->new($msgref);
121                 }
122                 $self->begin_txn_lazy;
123                 $self->add_message($eml, $smsg);
124         }
125 }
126
127 sub shard_add_xref3 {
128         my ($self, $docid, $xnum, $oid, $xibx, $eml) = @_;
129         my $ng_or_dir = $xibx->{newsgroup} // $xibx->{inboxdir};
130         if (my $w = $self->{w}) {
131                 my $hdr = $eml->header_obj->as_string;
132                 my $len = length($hdr);
133                 print $w "+X $len $docid $xnum $oid $ng_or_dir\n", $hdr or
134                         die "failed to write shard: $!";
135         } else {
136                 $self->add_xref3($docid, $xnum, $oid, $ng_or_dir, $eml);
137         }
138 }
139
140 sub shard_remove_xref3 {
141         my ($self, $docid, $oid, $xibx, $eml) = @_;
142         my $ng_or_dir = $xibx->{newsgroup} // $xibx->{inboxdir};
143         if (my $w = $self->{w}) {
144                 my $hdr = $eml->header_obj->as_string;
145                 my $len = length($hdr);
146                 print $w "-X $len $docid $oid $ng_or_dir\n", $hdr or
147                         die "failed to write shard: $!";
148         } else {
149                 $self->remove_xref3($docid, $oid, $ng_or_dir, $eml);
150         }
151 }
152
153 sub atfork_child {
154         close $_[0]->{w} or die "failed to close write pipe: $!\n";
155 }
156
157 sub shard_barrier {
158         my ($self) = @_;
159         if (my $w = $self->{w}) {
160                 print $w "barrier\n" or die "failed to print: $!";
161         } else {
162                 $self->commit_txn_lazy;
163         }
164 }
165
166 sub shard_commit {
167         my ($self) = @_;
168         if (my $w = $self->{w}) {
169                 print $w "commit\n" or die "failed to write commit: $!";
170         } else {
171                 $self->commit_txn_lazy;
172         }
173 }
174
175 sub shard_close {
176         my ($self) = @_;
177         if (my $w = delete $self->{w}) {
178                 my $pid = delete $self->{pid} or die "no process to wait on\n";
179                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
180                 close $w or die "failed to close pipe for pid:$pid: $!\n";
181                 waitpid($pid, 0) == $pid or die "remote process did not finish";
182                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
183         } else {
184                 die "transaction in progress $self\n" if $self->{txn};
185                 $self->idx_release if $self->{xdb};
186         }
187 }
188
189 sub shard_remove {
190         my ($self, $oid, $num) = @_;
191         if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
192                 print $w "D $oid $num\n" or die "failed to write remove $!";
193         } else { # same process
194                 $self->remove_by_oid($oid, $num);
195         }
196 }
197
198 1;