]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
searchidx: index THREADID in Xapian
[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 IO::Handle (); # autoflush
11 use PublicInbox::Eml;
12
13 sub new {
14         my ($class, $v2w, $shard) = @_;
15         my $ibx = $v2w->{ibx};
16         my $self = $class->SUPER::new($ibx, 1, $shard);
17         # create the DB before forking:
18         $self->idx_acquire;
19         $self->set_metadata_once;
20         $self->idx_release;
21         $self->spawn_worker($v2w, $shard) if $v2w->{parallel};
22         $self;
23 }
24
25 sub spawn_worker {
26         my ($self, $v2w, $shard) = @_;
27         my ($r, $w);
28         pipe($r, $w) or die "pipe failed: $!\n";
29         $w->autoflush(1);
30         my $pid = fork;
31         defined $pid or die "fork failed: $!\n";
32         if ($pid == 0) {
33                 my $bnote = $v2w->atfork_child;
34                 close $w or die "failed to close: $!";
35
36                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
37                 # speeds V2Writable batch imports across 8 cores by nearly 20%
38                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
39
40                 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
41                 die "worker $shard died: $@\n" if $@;
42                 die "unexpected MM $self->{mm}" if $self->{mm};
43                 exit;
44         }
45         $self->{pid} = $pid;
46         $self->{w} = $w;
47         close $r or die "failed to close: $!";
48 }
49
50 # this reads all the writes to $self->{w} from the parent process
51 sub shard_worker_loop ($$$$$) {
52         my ($self, $v2w, $r, $shard, $bnote) = @_;
53         $0 = "pi-v2-shard[$shard]";
54         $self->begin_txn_lazy;
55         while (my $line = readline($r)) {
56                 $v2w->{current_info} = "[$shard] $line";
57                 if ($line eq "commit\n") {
58                         $self->commit_txn_lazy;
59                 } elsif ($line eq "close\n") {
60                         $self->idx_release;
61                 } elsif ($line eq "barrier\n") {
62                         $self->commit_txn_lazy;
63                         # no need to lock < 512 bytes is atomic under POSIX
64                         print $bnote "barrier $shard\n" or
65                                         die "write failed for barrier $!\n";
66                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) ([0-9]+)\n\z/s) {
67                         $self->remove_by_oid($1, $2 + 0);
68                 } else {
69                         chomp $line;
70                         # n.b. $mid may contain spaces(!)
71                         my ($to_read, $bytes, $num, $blob, $ds, $ts, $tid, $mid)
72                                 = split(/ /, $line, 8);
73                         $self->begin_txn_lazy;
74                         my $n = read($r, my $msg, $to_read) or die "read: $!\n";
75                         $n == $to_read or die "short read: $n != $to_read\n";
76                         my $mime = PublicInbox::Eml->new(\$msg);
77                         my $smsg = bless {
78                                 bytes => $bytes,
79                                 num => $num + 0,
80                                 blob => $blob,
81                                 mid => $mid,
82                                 tid => $tid,
83                                 ds => $ds,
84                                 ts => $ts,
85                         }, 'PublicInbox::Smsg';
86                         $self->add_message($mime, $smsg);
87                 }
88         }
89         $self->worker_done;
90 }
91
92 sub index_raw {
93         my ($self, $msgref, $eml, $smsg) = @_;
94         if (my $w = $self->{w}) {
95                 # mid must be last, it can contain spaces (but not LF)
96                 print $w join(' ', @$smsg{qw(raw_bytes bytes
97                                                 num blob ds ts tid mid)}),
98                         "\n", $$msgref or die "failed to write shard $!\n";
99         } else {
100                 if ($eml) {
101                         undef $$msgref;
102                 } else { # --xapian-only + --sequential-shard:
103                         $eml = PublicInbox::Eml->new($msgref);
104                 }
105                 $self->begin_txn_lazy;
106                 $self->add_message($eml, $smsg);
107         }
108 }
109
110 sub atfork_child {
111         close $_[0]->{w} or die "failed to close write pipe: $!\n";
112 }
113
114 sub shard_barrier {
115         my ($self) = @_;
116         if (my $w = $self->{w}) {
117                 print $w "barrier\n" or die "failed to print: $!";
118         } else {
119                 $self->commit_txn_lazy;
120         }
121 }
122
123 sub shard_commit {
124         my ($self) = @_;
125         if (my $w = $self->{w}) {
126                 print $w "commit\n" or die "failed to write commit: $!";
127         } else {
128                 $self->commit_txn_lazy;
129         }
130 }
131
132 sub shard_close {
133         my ($self) = @_;
134         if (my $w = delete $self->{w}) {
135                 my $pid = delete $self->{pid} or die "no process to wait on\n";
136                 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
137                 close $w or die "failed to close pipe for pid:$pid: $!\n";
138                 waitpid($pid, 0) == $pid or die "remote process did not finish";
139                 $? == 0 or die ref($self)." pid:$pid exited with: $?";
140         } else {
141                 die "transaction in progress $self\n" if $self->{txn};
142                 $self->idx_release if $self->{xdb};
143         }
144 }
145
146 sub shard_remove {
147         my ($self, $oid, $num) = @_;
148         if (my $w = $self->{w}) { # triggers remove_by_oid in a shard child
149                 print $w "D $oid $num\n" or die "failed to write remove $!";
150         } else { # same process
151                 $self->remove_by_oid($oid, $num);
152         }
153 }
154
155 1;