]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxShard.pm
searchidxshard: replace index_raw with index_eml
[public-inbox.git] / lib / PublicInbox / SearchIdxShard.pm
1 # Copyright (C) 2018-2021 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 PublicInbox::IPC);
10 use PublicInbox::OnDestroy;
11
12 sub new {
13         my ($class, $v2w, $shard) = @_; # v2w may be ExtSearchIdx
14         my $ibx = $v2w->{ibx};
15         my $self = $ibx ? $class->SUPER::new($ibx, 1, $shard)
16                         : $class->eidx_shard_new($v2w, $shard);
17         # create the DB before forking:
18         $self->idx_acquire;
19         $self->set_metadata_once;
20         $self->idx_release;
21         if ($v2w->{parallel}) {
22                 local $self->{-v2w_afc} = $v2w;
23                 $self->ipc_worker_spawn("shard[$shard]");
24         }
25         $self;
26 }
27
28 sub _worker_done {
29         my ($self) = @_;
30         if ($self->need_xapian) {
31                 die "$$ $0 xdb not released\n" if $self->{xdb};
32         }
33         die "$$ $0 still in transaction\n" if $self->{txn};
34 }
35
36 sub ipc_atfork_child { # called automatically before ipc_worker_loop
37         my ($self) = @_;
38         my $v2w = delete $self->{-v2w_afc} or die 'BUG: {-v2w_afc} missing';
39         $v2w->atfork_child; # calls shard_atfork_child on our siblings
40         $v2w->{current_info} = "[$self->{shard}]"; # for $SIG{__WARN__}
41         $self->begin_txn_lazy;
42         # caller must capture this:
43         PublicInbox::OnDestroy->new($$, \&_worker_done, $self);
44 }
45
46 sub index_eml {
47         my ($self, $eml, $smsg, $eidx_key) = @_;
48         $smsg->{eidx_key} = $eidx_key if defined $eidx_key;
49         $self->ipc_do('add_message', $eml, $smsg);
50 }
51
52 # needed when there's multiple IPC workers and the parent forking
53 # causes newer siblings to inherit older siblings sockets
54 sub shard_atfork_child {
55         my ($self) = @_;
56         my $pid = delete($self->{-ipc_worker_pid}) or
57                         die "BUG: $$ no -ipc_worker_pid";
58         my $s1 = delete($self->{-ipc_sock}) or die "BUG: $$ no -ipc_sock";
59         $pid == $$ and die "BUG: $$ shard_atfork_child called on itself";
60         close($s1) or die "close -ipc_sock: $!";
61 }
62
63 # wait for return to determine when ipc_do('commit_txn_lazy') is done
64 sub echo {
65         shift;
66         "@_";
67 }
68
69 sub idx_close {
70         my ($self) = @_;
71         die "transaction in progress $self\n" if $self->{txn};
72         $self->idx_release if $self->{xdb};
73 }
74
75 sub shard_close {
76         my ($self) = @_;
77         $self->ipc_do('idx_close');
78         $self->ipc_worker_stop;
79 }
80
81 sub shard_over_check {
82         my ($self, $over) = @_;
83         if ($self->{-ipc_sock} && $over->{dbh}) {
84                 # can't send DB handles over IPC
85                 $over = ref($over)->new($over->{dbh}->sqlite_db_filename);
86         }
87         $self->ipc_do('over_check', $over);
88 }
89
90 1;