1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # This interface wraps and mimics PublicInbox::Import
5 package PublicInbox::V2Writable;
8 use base qw(PublicInbox::Lock);
9 use PublicInbox::SearchIdxPart;
10 use PublicInbox::SearchIdxSkeleton;
11 use PublicInbox::MIME;
13 use PublicInbox::Import;
14 use PublicInbox::MID qw(mids);
15 use PublicInbox::ContentId qw(content_id content_digest);
16 use PublicInbox::Inbox;
18 # an estimate of the post-packed size to the raw uncompressed size
19 my $PACKING_FACTOR = 0.4;
21 # assume 2 cores if GNU nproc(1) is not available
23 int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
27 my ($class, $v2ibx, $creat) = @_;
28 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
32 File::Path::mkpath($dir);
34 die "$dir does not exist\n";
39 my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
41 # always load existing partitions in case core count changes:
43 foreach my $part (<$xpfx/*>) {
44 -d $part && $part =~ m!/\d+\z! or next;
46 Search::Xapian::Database->new($part)->close;
51 $nparts = nproc() if ($nparts == 0);
55 im => undef, # PublicInbox::Import
56 xap_rw => undef, # PublicInbox::V2SearchIdx
58 partitions => $nparts,
60 lock_path => "$dir/inbox.lock",
61 # limit each repo to 1GB or so
62 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
67 # returns undef on duplicate or spam
68 # mimics Import::add and wraps it for v2
70 my ($self, $mime, $check_cb) = @_;
74 $mime = $check_cb->($mime) or return;
77 # All pipes (> $^F) known to Perl 5.6+ have FD_CLOEXEC set,
78 # as does SQLite 3.4.1+ (released in 2007-07-20), and
79 # Xapian 1.3.2+ (released 2015-03-15).
80 # For the most part, we can spawn git-fast-import without
81 # leaking FDs to it...
85 my $num = num_for($self, $mime, \$mid0);
86 defined $num or return; # duplicate
87 defined $mid0 or die "BUG: $mid0 undefined\n";
88 my $im = $self->importer;
89 my $cmt = $im->add($mime);
90 $cmt = $im->get_mark($cmt);
91 my ($oid, $len, $msgref) = @{$im->{last_object}};
93 my $nparts = $self->{partitions};
94 my $part = $num % $nparts;
95 my $idx = $self->idx_part($part);
96 $idx->index_raw($len, $msgref, $num, $oid, $mid0);
97 my $n = $self->{transact_bytes} += $len;
98 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
106 my ($self, $mime, $mid0) = @_;
107 my $mids = mids($mime->header_obj);
109 my $mid = $mids->[0];
110 my $num = $self->{skel}->{mm}->mid_insert($mid);
111 if (defined $num) { # common case
116 # crap, Message-ID is already known, hope somebody just resent:
118 foreach my $m (@$mids) {
119 # read-only lookup now safe to do after above barrier
120 my $existing = $self->lookup_content($mime, $m);
122 warn "<$m> resent\n";
123 return; # easy, don't store duplicates
128 warn "<$mid> reused for mismatched content\n";
130 # try the rest of the mids
131 foreach my $i (1..$#$mids) {
133 $num = $self->{skel}->{mm}->mid_insert($m);
135 warn "alternative <$m> for <$mid> found\n";
141 # none of the existing Message-IDs are good, generate a new one:
142 num_for_harder($self, $mime, $mid0);
146 my ($self, $mime, $mid0) = @_;
148 my $hdr = $mime->header_obj;
149 my $dig = content_digest($mime);
150 $$mid0 = PublicInbox::Import::digest2mid($dig);
151 my $num = $self->{skel}->{mm}->mid_insert($$mid0);
152 unless (defined $num) {
153 # it's hard to spoof the last Received: header
154 my @recvd = $hdr->header_raw('Received');
155 $dig->add("Received: $_") foreach (@recvd);
156 $$mid0 = PublicInbox::Import::digest2mid($dig);
157 $num = $self->{skel}->{mm}->mid_insert($$mid0);
159 # fall back to a random Message-ID and give up determinism:
160 until (defined($num)) {
162 $$mid0 = PublicInbox::Import::digest2mid($dig);
163 warn "using random Message-ID <$$mid0> as fallback\n";
164 $num = $self->{skel}->{mm}->mid_insert($$mid0);
167 my @cur = $hdr->header_raw('Message-Id');
168 $hdr->header_set('Message-Id', "<$$mid0>", @cur);
173 my ($self, $part) = @_;
174 $self->{idx_parts}->[$part];
180 return if $self->{idx_parts};
181 my $ibx = $self->{-inbox};
183 # do not leak read-only FDs to child processes, we only have these
184 # FDs for duplicate detection so they should not be
185 # frequently activated.
186 delete $ibx->{$_} foreach (qw(git mm search));
190 # first time initialization, first we create the skeleton pipe:
191 my $skel = $self->{skel} = PublicInbox::SearchIdxSkeleton->new($self);
193 # need to create all parts before initializing msgmap FD
194 my $max = $self->{partitions} - 1;
195 my $idx = $self->{idx_parts} = [];
196 for my $i (0..$max) {
197 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $skel);
200 # Now that all subprocesses are up, we can open the FD for SQLite:
201 $skel->_msgmap_init->{dbh}->begin_work;
205 my ($self, $mime, $cmt_msg) = @_;
208 my $im = $self->importer;
209 my $ibx = $self->{-inbox};
210 my $srch = $ibx->search;
211 my $cid = content_id($mime);
212 my $skel = $self->{skel};
213 my $parts = $self->{idx_parts};
214 my $mm = $skel->{mm};
216 my $mids = mids($mime->header_obj);
217 foreach my $mid (@$mids) {
218 $srch->reopen->each_smsg_by_mid($mid, sub {
221 my $msg = $ibx->msg_by_smsg($smsg);
222 if (!defined($msg)) {
223 warn "broken smsg for $mid\n";
227 my $cur = PublicInbox::MIME->new($msg);
228 if (content_id($cur) eq $cid) {
229 $mm->num_delete($smsg->num);
230 # $removed should only be set once assuming
231 # no bugs in our deduplication code:
233 $removed->{mime} = $cur;
234 $im->remove(\$orig, $cmt_msg);
236 $removed->num; # memoize this for callers
238 my $oid = $smsg->{blob};
239 foreach my $idx (@$parts, $skel) {
240 $idx->remote_remove($oid, $mid);
252 my $locked = defined $self->{idx_parts};
253 my $im = delete $self->{im};
254 $im->done if $im; # PublicInbox::Import::done
255 $self->searchidx_checkpoint(0);
256 $self->lock_release if $locked;
261 my $im = $self->{im};
262 $im->checkpoint if $im; # PublicInbox::Import::checkpoint
263 $self->searchidx_checkpoint(1);
266 # issue a write barrier to ensure all data is visible to other processes
267 # and read-only ops. Order of data importance is: git > SQLite > Xapian
271 if (my $im = $self->{im}) {
274 my $skel = $self->{skel};
275 my $parts = $self->{idx_parts};
276 if ($parts && $skel) {
277 my $dbh = $skel->{mm}->{dbh};
278 $dbh->commit; # SQLite data is second in importance
280 # Now deal with Xapian
281 $skel->barrier_init(scalar(@$parts));
282 # each partition needs to issue a barrier command to skel:
283 $_->barrier foreach @$parts;
285 $skel->barrier_wait; # wait for each Xapian partition
289 $self->{transact_bytes} = 0;
292 sub searchidx_checkpoint {
293 my ($self, $more) = @_;
295 # order matters, we can only close {skel} after all partitions
296 # are done because the partitions also write to {skel}
297 if (my $parts = $self->{idx_parts}) {
298 foreach my $idx (@$parts) {
299 $idx->remote_commit; # propagates commit to skel
300 $idx->remote_close unless $more;
302 delete $self->{idx_parts} unless $more;
305 if (my $skel = $self->{skel}) {
306 my $dbh = $skel->{mm}->{dbh};
312 delete $self->{skel};
315 $self->{transact_bytes} = 0;
319 my ($self, $new) = @_;
320 my $pfx = "$self->{-inbox}->{mainrepo}/git";
321 my $git_dir = "$pfx/$new.git";
322 die "$git_dir exists\n" if -e $git_dir;
323 my @cmd = (qw(git init --bare -q), $git_dir);
324 PublicInbox::Import::run_die(\@cmd);
326 my $all = "$self->{-inbox}->{mainrepo}/all.git";
328 @cmd = (qw(git init --bare -q), $all);
329 PublicInbox::Import::run_die(\@cmd);
330 @cmd = (qw/git config/, "--file=$all/config",
331 'repack.writeBitmaps', 'true');
332 PublicInbox::Import::run_die(\@cmd);
335 @cmd = (qw/git config/, "--file=$git_dir/config",
336 'include.path', '../../all.git/config');
337 PublicInbox::Import::run_die(\@cmd);
339 my $alt = "$all/objects/info/alternates";
340 my $new_obj_dir = "../../git/$new.git/objects";
343 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
344 %alts = map { chomp; $_ => 1 } (<$fh>);
346 return $git_dir if $alts{$new_obj_dir};
347 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
348 print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
349 close $fh or die "close $alt: $!\n";
355 my $im = $self->{im};
357 if ($im->{bytes_added} < $self->{rotate_bytes}) {
362 $self->searchidx_checkpoint(1);
364 my $git_dir = $self->git_init(++$self->{max_git});
365 my $git = PublicInbox::Git->new($git_dir);
366 return $self->import_init($git, 0);
372 my $pfx = "$self->{-inbox}->{mainrepo}/git";
374 foreach my $git_dir (glob("$pfx/*.git")) {
375 $git_dir =~ m!/(\d+)\.git\z! or next;
383 if (defined $latest) {
384 my $git = PublicInbox::Git->new($latest);
385 my $packed_bytes = $git->packed_bytes;
386 if ($packed_bytes >= $self->{rotate_bytes}) {
389 $self->{max_git} = $max;
390 return $self->import_init($git, $packed_bytes);
393 $self->{max_git} = $new;
394 $latest = $self->git_init($new);
395 $self->import_init(PublicInbox::Git->new($latest), 0);
399 my ($self, $git, $packed_bytes) = @_;
400 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
401 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
402 $im->{want_object_info} = 1;
403 $im->{lock_path} = undef;
404 $im->{path_type} = 'v2';
409 my ($self, $mime, $mid) = @_;
410 my $ibx = $self->{-inbox};
412 my $srch = $ibx->search->reopen;
413 my $cid = content_id($mime);
415 $srch->each_smsg_by_mid($mid, sub {
418 my $msg = $ibx->msg_by_smsg($smsg);
419 if (!defined($msg)) {
420 warn "broken smsg for $mid\n";
423 my $cur = PublicInbox::MIME->new($msg);
424 if (content_id($cur) eq $cid) {
425 $smsg->{mime} = $cur;
427 return 0; # break out of loop
436 if (my $parts = $self->{idx_parts}) {
437 $_->atfork_child foreach @$parts;
439 if (my $im = $self->{im}) {