]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
drop binmode usage
[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 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, $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         $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 sub shard_worker_loop ($$$$$) {
51         my ($self, $v2w, $r, $shard, $bnote) = @_;
52         $0 = "pi-v2-shard[$shard]";
53         $self->begin_txn_lazy;
54         while (my $line = readline($r)) {
55                 $v2w->{current_info} = "[$shard] $line";
56                 if ($line eq "commit\n") {
57                         $self->commit_txn_lazy;
58                 } elsif ($line eq "close\n") {
59                         $self->_xdb_release;
60                 } elsif ($line eq "barrier\n") {
61                         $self->commit_txn_lazy;
62                         # no need to lock < 512 bytes is atomic under POSIX
63                         print $bnote "barrier $shard\n" or
64                                         die "write failed for barrier $!\n";
65                 } elsif ($line =~ /\AD ([a-f0-9]{40,}) (.+)\n\z/s) {
66                         my ($oid, $mid) = ($1, $2);
67                         $self->begin_txn_lazy;
68                         $self->remove_by_oid($oid, $mid);
69                 } else {
70                         chomp $line;
71                         # n.b. $mid may contain spaces(!)
72                         my ($to_read, $bytes, $num, $blob, $ds, $ts, $mid) =
73                                                         split(/ /, $line, 7);
74                         $self->begin_txn_lazy;
75                         my $n = read($r, my $msg, $to_read) or die "read: $!\n";
76                         $n == $to_read or die "short read: $n != $to_read\n";
77                         my $mime = PublicInbox::Eml->new(\$msg);
78                         my $smsg = bless {
79                                 bytes => $bytes,
80                                 num => $num + 0,
81                                 blob => $blob,
82                                 mid => $mid,
83                                 ds => $ds,
84                                 ts => $ts,
85                         }, 'PublicInbox::Smsg';
86                         $self->add_message($mime, $smsg);
87                 }
88         }
89         $self->worker_done;
90 }
91
92 # called by V2Writable
93 sub index_raw {
94         my ($self, $msgref, $mime, $smsg) = @_;
95         if (my $w = $self->{w}) {
96                 # mid must be last, it can contain spaces (but not LF)
97                 print $w join(' ', @$smsg{qw(raw_bytes bytes
98                                                 num blob ds ts mid)}),
99                         "\n", $$msgref or die "failed to write shard $!\n";
100         } else {
101                 $$msgref = undef;
102                 $self->begin_txn_lazy;
103                 $self->add_message($mime, $smsg);
104         }
105 }
106
107 sub atfork_child {
108         close $_[0]->{w} or die "failed to close write pipe: $!\n";
109 }
110
111 # called by V2Writable:
112 sub remote_barrier {
113         my ($self) = @_;
114         if (my $w = $self->{w}) {
115                 print $w "barrier\n" or die "failed to print: $!";
116         } else {
117                 $self->commit_txn_lazy;
118         }
119 }
120
121 1;