]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxThread.pm
v2writable: cleanup unused pipes in partitions
[public-inbox.git] / lib / PublicInbox / SearchIdxThread.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::SearchIdxThread;
4 use strict;
5 use warnings;
6 use base qw(PublicInbox::SearchIdx);
7 use Storable qw(freeze thaw);
8
9 sub new {
10         my ($class, $v2writable) = @_;
11         my $self = $class->SUPER::new($v2writable->{-inbox}, 1, 'all');
12         # create the DB:
13         $self->_xdb_acquire;
14         $self->_xdb_release;
15
16         my ($r, $w);
17         pipe($r, $w) or die "pipe failed: $!\n";
18         binmode $r, ':raw';
19         binmode $w, ':raw';
20         my $pid = fork;
21         defined $pid or die "fork failed: $!\n";
22         if ($pid == 0) {
23                 $v2writable->atfork_child;
24                 $v2writable = undef;
25                 close $w;
26                 eval { thread_worker_loop($self, $r) };
27                 die "thread worker died: $@\n" if $@;
28                 exit;
29         }
30         $self->{w} = $w;
31         $self->{pid} = $pid;
32         close $r;
33
34         $w->autoflush(1);
35
36         # lock on only exists in parent, not in worker
37         my $l = $self->{lock_path} = $self->xdir . '/thread.lock';
38         open my $fh, '>>', $l or die "failed to create $l: $!\n";
39         $self;
40 }
41
42 sub thread_worker_loop {
43         my ($self, $r) = @_;
44         my $msg;
45         my $xdb = $self->_xdb_acquire;
46         $xdb->begin_transaction;
47         my $txn = 1;
48         while (my $line = $r->getline) {
49                 if ($line eq "commit\n") {
50                         $xdb->commit_transaction if $txn;
51                         $txn = undef;
52                 } elsif ($line eq "close\n") {
53                         $self->_xdb_release;
54                         $xdb = $txn = undef;
55                 } else {
56                         read($r, $msg, $line) or die "read failed: $!\n";
57                         $msg = thaw($msg); # should raise on error
58                         defined $msg or die "failed to thaw buffer\n";
59                         if (!$txn) {
60                                 $xdb->begin_transaction;
61                                 $txn = 1;
62                         }
63                         eval { $self->thread_msg_real(@$msg) };
64                         warn "failed to index message <$msg->[0]>: $@\n" if $@;
65                 }
66         }
67 }
68
69 # called by a partition worker
70 sub thread_msg {
71         my ($self, $mid, $ts, $xpath, $doc_data) = @_;
72         my $w = $self->{w};
73         my $err;
74         my $str = freeze([ $mid, $ts, $xpath, $doc_data ]);
75         my $len = length($str) . "\n";
76
77         # multiple processes write to the same pipe, so use flock
78         $self->_lock_acquire;
79         print $w $len, $str or $err = $!;
80         $self->_lock_release;
81
82         die "print failed: $err\n" if $err;
83 }
84
85 sub thread_msg_real {
86         my ($self, $mid, $ts, $xpath, $doc_data) = @_;
87         my $smsg = $self->lookup_message($mid);
88         my ($old_tid, $doc_id);
89         if ($smsg) {
90                 # convert a ghost to a regular message
91                 # it will also clobber any existing regular message
92                 $doc_id = $smsg->{doc_id};
93                 $old_tid = $smsg->thread_id;
94         } else {
95                 $smsg = PublicInbox::SearchMsg->new(undef);
96                 $smsg->{mid} = $mid;
97         }
98         my $doc = $smsg->{doc};
99         $doc->add_term('XPATH' . $xpath) if defined $xpath;
100         $doc->add_term('XMID' . $mid);
101         $doc->set_data($doc_data);
102         $smsg->{ts} = $ts;
103         my @refs = ($smsg->references =~ /<([^>]+)>/g);
104         $self->link_message($smsg, \@refs, $old_tid);
105         my $db = $self->{xdb};
106         if (defined $doc_id) {
107                 $db->replace_document($doc_id, $doc);
108         } else {
109                 $doc_id = $db->add_document($doc);
110         }
111 }
112
113 1;