]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiPmdir.pm
lei import: speed up repeated Maildir imports
[public-inbox.git] / lib / PublicInbox / LeiPmdir.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # WQ worker for dealing with parallel Maildir reads;
5 # this does NOT use the {shard_info} field of LeiToMail
6 # (and we may remove {shard_info})
7 # WQ key: {pmd}
8 package PublicInbox::LeiPmdir;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::IPC);
12
13 sub new {
14         my ($cls, $lei, $ipt) = @_;
15         my $self = bless { -wq_ident => 'lei Maildir worker' }, $cls;
16         my $jobs = $lei->{opt}->{jobs};
17         $jobs =~ /\A[0-9]+,([0-9]+)\z/ and $jobs = $1;
18         my $nproc = $jobs // do {
19                 # untested with >=4 CPUs, though I suspect I/O latency
20                 # of SATA SSD storage will make >=4 processes unnecessary,
21                 # here.  NVMe users may wish to use '-j'
22                 my $n = $self->detect_nproc;
23                 $n = 4 if $n > 4;
24         };
25         my ($op_c, $ops) = $lei->workers_start($self, $nproc,
26                 undef, { ipt => $ipt }); # LeiInput subclass
27         $op_c->{ops} = $ops; # for PktOp->event_step
28         $lei->{pmd} = $self;
29 }
30
31 sub ipc_atfork_child {
32         my ($self) = @_;
33         my $lei = $self->{lei};
34         $lei->_lei_atfork_child;
35         my $ipt = $self->{ipt} // die 'BUG: no self->{ipt}';
36         $ipt->{lei} = $lei;
37         $ipt->{sto} = $lei->{sto} // die 'BUG: no lei->{sto}';
38         $ipt->{lse} = $ipt->{sto}->search;
39         $ipt->{over} = $ipt->{lse}->over;
40         $ipt->{-lms_ro} //= $ipt->{lse}->lms; # may be undef or '0'
41         $self->SUPER::ipc_atfork_child;
42 }
43
44 sub each_mdir_fn { # maildir_each_file callback
45         my ($f, $self, @args) = @_;
46         $self->wq_io_do('mdir_iter', [], $f, @args);
47 }
48
49 sub mdir_iter { # via wq_io_do
50         my ($self, $f, @args) = @_;
51         $self->{ipt}->pmdir_cb($f, @args);
52 }
53
54 sub pmd_done_wait {
55         my ($arg, $pid) = @_;
56         my ($self, $lei) = @$arg;
57         my $wait = $lei->{sto}->ipc_do('done');
58         $lei->can('wq_done_wait')->($arg, $pid);
59 }
60
61 sub _lei_wq_eof { # EOF callback for main lei daemon
62         my ($lei) = @_;
63         my $pmd = delete $lei->{pmd} or return $lei->fail;
64         $pmd->wq_wait_old(\&pmd_done_wait, $lei);
65 }
66
67 1;