]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
ds: add_timer: allow passing arg to callback.
[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 # used to interface with a single Xapian shard in V2 repos.
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 warnings;
9 use base qw(PublicInbox::SearchIdx);
10 use IO::Handle (); # autoflush
11 use PublicInbox::Eml;
12
13 sub new {
14         my ($class, $v2writable, $shard) = @_;
15         my $ibx = $v2writable->{-inbox};
16         my $self = $class->SUPER::new($ibx, 1, $shard);
17         # create the DB before forking:
18         $self->_xdb_acquire;
19         $self->set_indexlevel;
20         $self->_xdb_release;
21         $self->spawn_worker($v2writable, $shard) if $v2writable->{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         binmode $r, ':raw';
30         binmode $w, ':raw';
31         $w->autoflush(1);
32         my $pid = fork;
33         defined $pid or die "fork failed: $!\n";
34         if ($pid == 0) {
35                 my $bnote = $v2w->atfork_child;
36                 close $w or die "failed to close: $!";
37
38                 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
39                 # speeds V2Writable batch imports across 8 cores by nearly 20%
40                 fcntl($r, 1031, 1048576) if $^O eq 'linux';
41
42                 eval { shard_worker_loop($self, $v2w, $r, $shard, $bnote) };
43                 die "worker $shard died: $@\n" if $@;
44                 die "unexpected MM $self->{mm}" if $self->{mm};
45                 exit;
46         }
47         $self->{pid} = $pid;
48         $self->{w} = $w;
49         close $r or die "failed to close: $!";
50 }
51
52 sub shard_worker_loop ($$$$$) {
53         my ($self, $v2w, $r, $shard, $bnote) = @_;
54         $0 = "pi-v2-shard[$shard]";
55         $self->begin_txn_lazy;
56         while (my $line = readline($r)) {
57                 $v2w->{current_info} = "[$shard] $line";
58                 if ($line eq "commit\n") {
59                         $self->commit_txn_lazy;
60                 } elsif ($line eq "close\n") {
61                         $self->_xdb_release;
62                 } elsif ($line eq "barrier\n") {
63                         $self->commit_txn_lazy;
64                         # no need to lock < 512 bytes is atomic under POSIX
65                         print $bnote "barrier $shard\n" or
66                                         die "write failed for barrier $!\n";
67                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
68                         my ($oid, $mid) = ($1, $2);
69                         $self->begin_txn_lazy;
70                         $self->remove_by_oid($oid, $mid);
71                 } else {
72                         chomp $line;
73                         # n.b. $mid may contain spaces(!)
74                         my ($to_read, $bytes, $num, $blob, $ds, $ts, $mid) =
75                                                         split(/ /, $line, 7);
76                         $self->begin_txn_lazy;
77                         my $n = read($r, my $msg, $to_read) or die "read: $!\n";
78                         $n == $to_read or die "short read: $n != $to_read\n";
79                         my $mime = PublicInbox::Eml->new(\$msg);
80                         my $smsg = bless {
81                                 bytes => $bytes,
82                                 num => $num + 0,
83                                 blob => $blob,
84                                 mid => $mid,
85                                 ds => $ds,
86                                 ts => $ts,
87                         }, 'PublicInbox::Smsg';
88                         $self->add_message($mime, $smsg);
89                 }
90         }
91         $self->worker_done;
92 }
93
94 # called by V2Writable
95 sub index_raw {
96         my ($self, $msgref, $mime, $smsg) = @_;
97         if (my $w = $self->{w}) {
98                 # mid must be last, it can contain spaces (but not LF)
99                 print $w join(' ', @$smsg{qw(raw_bytes bytes
100                                                 num blob ds ts mid)}),
101                         "\n", $$msgref or die "failed to write shard $!\n";
102         } else {
103                 $$msgref = undef;
104                 $self->begin_txn_lazy;
105                 $self->add_message($mime, $smsg);
106         }
107 }
108
109 sub atfork_child {
110         close $_[0]->{w} or die "failed to close write pipe: $!\n";
111 }
112
113 # called by V2Writable:
114 sub remote_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 1;