]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
v2writable: deduplicate detection on add
[public-inbox.git] / lib / PublicInbox / V2Writable.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
4 # This interface wraps and mimics PublicInbox::Import
5 package PublicInbox::V2Writable;
6 use strict;
7 use warnings;
8 use Fcntl qw(:flock :DEFAULT);
9 use PublicInbox::SearchIdxPart;
10 use PublicInbox::SearchIdxSkeleton;
11 use PublicInbox::MIME;
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use PublicInbox::MID qw(mid_clean mid_mime);
15 use PublicInbox::ContentId qw(content_id);
16 use PublicInbox::Inbox;
17
18 # an estimate of the post-packed size to the raw uncompressed size
19 my $PACKING_FACTOR = 0.4;
20
21 # assume 2 cores if GNU nproc(1) is not available
22 my $NPROC = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
23
24 sub new {
25         my ($class, $v2ibx, $creat) = @_;
26         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
27         unless (-d $dir) {
28                 if ($creat) {
29                         require File::Path;
30                         File::Path::mkpath($dir);
31                 } else {
32                         die "$dir does not exist\n";
33                 }
34         }
35         my $self = {
36                 -inbox => $v2ibx,
37                 im => undef, #  PublicInbox::Import
38                 xap_rw => undef, # PublicInbox::V2SearchIdx
39                 xap_ro => undef,
40                 partitions => $NPROC,
41                 transact_bytes => 0,
42                 # limit each repo to 1GB or so
43                 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
44         };
45         bless $self, $class
46 }
47
48 # returns undef on duplicate or spam
49 # mimics Import::add and wraps it for v2
50 sub add {
51         my ($self, $mime, $check_cb) = @_;
52
53         # spam check:
54         if ($check_cb) {
55                 $mime = $check_cb->($mime) or return;
56         }
57
58         # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
59         # as does SQLite 3.4.1+ (released in 2007-07-20), and
60         # Xapian 1.3.2+ (released 2015-03-15).
61         # For the most part, we can spawn git-fast-import without
62         # leaking FDs to it...
63         $self->idx_init;
64
65         my $mid = mid_clean(mid_mime($mime));
66         my $num = $self->{skel}->{mm}->mid_insert($mid);
67         if (!defined($num)) { # mid is already known
68                 $self->done; # ensure all subprocesses are done writing
69
70                 my $existing = $self->lookup_content($mime);
71                 warn "<$mid> resent\n" if $existing;
72                 return if $existing; # easy, don't store duplicates
73
74                 # reuse NNTP article number?
75                 warn "<$mid> reused for mismatched content\n";
76                 $self->idx_init;
77                 $num = $self->{skel}->{mm}->num_for($mid);
78         }
79
80         my $im = $self->importer;
81         my $cmt = $im->add($mime);
82         $cmt = $im->get_mark($cmt);
83         my $oid = $im->{last_object_id};
84         my ($len, $msgref) = @{$im->{last_object}};
85
86         my $nparts = $self->{partitions};
87         my $part = $num % $nparts;
88         my $idx = $self->idx_part($part);
89         $idx->index_raw($len, $msgref, $num, $oid);
90         my $n = $self->{transact_bytes} += $len;
91         if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
92                 $self->checkpoint;
93         }
94
95         $mime;
96 }
97
98 sub idx_part {
99         my ($self, $part) = @_;
100         $self->{idx_parts}->[$part];
101 }
102
103 # idempotent
104 sub idx_init {
105         my ($self) = @_;
106         return if $self->{idx_parts};
107         my $ibx = $self->{-inbox};
108
109         # do not leak read-only FDs to child processes, we only have these
110         # FDs for duplicate detection so they should not be
111         # frequently activated.
112         delete $ibx->{$_} foreach (qw(git mm search));
113
114         # first time initialization, first we create the skeleton pipe:
115         my $skel = $self->{skel} = PublicInbox::SearchIdxSkeleton->new($self);
116
117         # need to create all parts before initializing msgmap FD
118         my $max = $self->{partitions} - 1;
119         my $idx = $self->{idx_parts} = [];
120         for my $i (0..$max) {
121                 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $skel);
122         }
123
124         # Now that all subprocesses are up, we can open the FD for SQLite:
125         $skel->_msgmap_init->{dbh}->begin_work;
126 }
127
128 sub remove {
129         my ($self, $mime, $msg) = @_;
130         my $existing = $self->lookup_content($mime) or return;
131
132         # don't touch ghosts or already junked messages
133         return unless $existing->type eq 'mail';
134
135         # always write removals to the current (latest) git repo since
136         # we process chronologically
137         my $im = $self->importer;
138         my ($cmt, undef) = $im->remove($mime, $msg);
139         $cmt = $im->get_mark($cmt);
140         $self->unindex_msg($existing, $cmt);
141 }
142
143 sub done {
144         my ($self) = @_;
145         my $im = delete $self->{im};
146         $im->done if $im; # PublicInbox::Import::done
147         $self->searchidx_checkpoint(0);
148 }
149
150 sub checkpoint {
151         my ($self) = @_;
152         my $im = $self->{im};
153         $im->checkpoint if $im; # PublicInbox::Import::checkpoint
154         $self->searchidx_checkpoint(1);
155 }
156
157 sub searchidx_checkpoint {
158         my ($self, $more) = @_;
159
160         # order matters, we can only close {skel} after all partitions
161         # are done because the partitions also write to {skel}
162         if (my $parts = $self->{idx_parts}) {
163                 foreach my $idx (@$parts) {
164                         $idx->remote_commit; # propagates commit to skel
165                         $idx->remote_close unless $more;
166                 }
167                 delete $self->{idx_parts} unless $more;
168         }
169
170         if (my $skel = $self->{skel}) {
171                 my $dbh = $skel->{mm}->{dbh};
172                 $dbh->commit;
173                 if ($more) {
174                         $dbh->begin_work;
175                 } else {
176                         $skel->remote_commit; # XXX should be unnecessary...
177                         $skel->remote_close;
178                         delete $self->{skel};
179                 }
180         }
181         $self->{transact_bytes} = 0;
182 }
183
184 sub git_init {
185         my ($self, $new) = @_;
186         my $pfx = "$self->{-inbox}->{mainrepo}/git";
187         my $git_dir = "$pfx/$new.git";
188         die "$git_dir exists\n" if -e $git_dir;
189         my @cmd = (qw(git init --bare -q), $git_dir);
190         PublicInbox::Import::run_die(\@cmd);
191         @cmd = (qw/git config/, "--file=$git_dir/config",
192                         'repack.writeBitmaps', 'true');
193         PublicInbox::Import::run_die(\@cmd);
194
195         my $all = "$self->{-inbox}->{mainrepo}/all.git";
196         unless (-d $all) {
197                 @cmd = (qw(git init --bare -q), $all);
198                 PublicInbox::Import::run_die(\@cmd);
199         }
200
201         my $alt = "$all/objects/info/alternates";
202         my $new_obj_dir = "../../git/$new.git/objects";
203         my %alts;
204         if (-e $alt) {
205                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
206                 %alts = map { chomp; $_ => 1 } (<$fh>);
207         }
208         return $git_dir if $alts{$new_obj_dir};
209         open my $fh, '>>', $alt or die "open >> $alt: $!\n";
210         print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
211         close $fh or die "close $alt: $!\n";
212         $git_dir
213 }
214
215 sub importer {
216         my ($self) = @_;
217         my $im = $self->{im};
218         if ($im) {
219                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
220                         return $im;
221                 } else {
222                         $self->{im} = undef;
223                         $im->done;
224                         $self->searchidx_checkpoint(1);
225                         $im = undef;
226                         my $git_dir = $self->git_init(++$self->{max_git});
227                         my $git = PublicInbox::Git->new($git_dir);
228                         return $self->import_init($git, 0);
229                 }
230         }
231         my $latest;
232         my $max = -1;
233         my $new = 0;
234         my $pfx = "$self->{-inbox}->{mainrepo}/git";
235         if (-d $pfx) {
236                 foreach my $git_dir (glob("$pfx/*.git")) {
237                         $git_dir =~ m!/(\d+)\.git\z! or next;
238                         my $n = $1;
239                         if ($n > $max) {
240                                 $max = $n;
241                                 $latest = $git_dir;
242                         }
243                 }
244         }
245         if (defined $latest) {
246                 my $git = PublicInbox::Git->new($latest);
247                 my $packed_bytes = $git->packed_bytes;
248                 if ($packed_bytes >= $self->{rotate_bytes}) {
249                         $new = $max + 1;
250                 } else {
251                         $self->{max_git} = $max;
252                         return $self->import_init($git, $packed_bytes);
253                 }
254         }
255         $self->{max_git} = $new;
256         $latest = $self->git_init($new);
257         $self->import_init(PublicInbox::Git->new($latest), 0);
258 }
259
260 sub import_init {
261         my ($self, $git, $packed_bytes) = @_;
262         my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
263         $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
264         $im->{want_object_id} = 1;
265         $im->{ssoma_lock} = 0;
266         $im->{path_type} = 'v2';
267         $self->{im} = $im;
268 }
269
270 sub lookup_content {
271         my ($self, $mime) = @_;
272         my $ibx = $self->{-inbox};
273
274         my $srch = $ibx->search;
275         my $cid = content_id($mime);
276         my $found;
277         my $mid = mid_mime($mime);
278         $srch->each_smsg_by_mid($mid, sub {
279                 my ($smsg) = @_;
280                 $smsg->load_expand;
281                 my $msg = $ibx->msg_by_smsg($smsg);
282                 if (!defined($msg)) {
283                         warn "broken smsg for $mid\n";
284                         return 1; # continue
285                 }
286                 my $cur = PublicInbox::MIME->new($msg);
287                 if (content_id($cur) eq $cid) {
288                         $smsg->{mime} = $cur;
289                         $found = $smsg;
290                         return 0; # break out of loop
291                 }
292                 1; # continue
293         });
294         $found;
295 }
296
297 sub atfork_child {
298         my ($self) = @_;
299         if (my $parts = $self->{idx_parts}) {
300                 $_->atfork_child foreach @$parts;
301         }
302         if (my $im = $self->{im}) {
303                 $im->atfork_child;
304         }
305 }
306
307 1;