1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::Xapcmd;
6 use PublicInbox::Spawn qw(which spawn);
8 use PublicInbox::Search;
9 use File::Temp qw(tempdir);
10 use File::Path qw(remove_tree);
12 # support testing with dev versions of Xapian which installs
13 # commands with a version number suffix (e.g. "xapian-compact-1.5")
14 our $XAPIAN_COMPACT = $ENV{XAPIAN_COMPACT} || 'xapian-compact';
16 sub commit_changes ($$$$) {
17 my ($ibx, $old, $new, $opt) = @_;
19 my $reindex = $opt->{reindex};
20 my $im = $ibx->importer(0);
21 $im->lock_acquire if $reindex;
23 my @st = stat($old) or die "failed to stat($old): $!\n";
25 my $over = "$old/over.sqlite3";
27 $over = PublicInbox::Over->new($over);
28 $over->connect->sqlite_backup_to_file("$new/over.sqlite3");
31 rename($old, "$new/old") or die "rename $old => $new/old: $!\n";
32 chmod($st[2] & 07777, $new) or die "chmod $old: $!\n";
33 rename($new, $old) or die "rename $new => $old: $!\n";
34 remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
37 $opt->{-skip_lock} = 1;
38 PublicInbox::Admin::index_inbox($ibx, $opt);
39 # implicit lock_release
46 my ($cmd, $env, $opt) = @_;
47 if (ref($cmd->[0]) eq 'CODE') {
48 my $cb = shift(@$cmd); # $cb = cpdb()
49 defined(my $pid = fork) or die "fork: $!";
50 return $pid if $pid > 0;
51 eval { $cb->($cmd, $env, $opt) };
55 spawn($cmd, $env, $opt);
59 sub runnable_or_die ($) {
61 which($exe) or die "$exe not found in PATH\n";
64 sub prepare_reindex ($$) {
65 my ($ibx, $reindex) = @_;
66 if ($ibx->{version} == 1) {
67 my $dir = $ibx->search->xdir(1);
68 my $xdb = Search::Xapian::Database->new($dir);
69 if (my $lc = $xdb->get_metadata('last_commit')) {
70 $reindex->{from} = $lc;
73 my $v2w = $ibx->importer(0);
75 $v2w->git_dir_latest(\$max) or return;
76 my $from = $reindex->{from};
78 my $v = PublicInbox::Search::SCHEMA_VERSION();
79 foreach my $i (0..$max) {
80 $from->[$i] = $mm->last_commit_xap($v, $i);
85 sub progress_prepare ($) {
88 open my $null, '>', '/dev/null' or
89 die "failed to open /dev/null: $!\n";
90 $opt->{1} = fileno($null);
91 $opt->{-dev_null} = $null;
93 $opt->{-progress} = 1;
98 my ($ibx, $cmd, $env, $opt) = @_;
99 progress_prepare($opt ||= {});
100 my $dir = $ibx->{mainrepo} or die "no mainrepo in inbox\n";
103 runnable_or_die($XAPIAN_COMPACT) if $opt->{compact};
105 my $reindex; # v1:{ from => $x40 }, v2:{ from => [ $x40, $x40, .. ] } }
106 my $from; # per-epoch ranges
108 if (ref($exe) eq 'CODE') {
110 $reindex = $opt->{reindex} = {};
111 $from = $reindex->{from} = [];
112 require Search::Xapian::WritableDatabase;
114 runnable_or_die($exe);
117 my $old = $ibx->search->xdir(1);
118 -d $old or die "$old does not exist\n";
119 my $new = tempdir("$pfx-XXXXXXXX", DIR => $dir);
120 my $v = $ibx->{version} ||= 1;
123 push @cmds, [@$cmd, $old, $new];
125 opendir my $dh, $old or die "Failed to opendir $old: $!\n";
126 while (defined(my $dn = readdir($dh))) {
127 if ($dn =~ /\A\d+\z/) {
128 push @cmds, [@$cmd, "$old/$dn", "$new/$dn"];
129 } elsif ($dn eq '.' || $dn eq '..') {
130 } elsif ($dn =~ /\Aover\.sqlite3/) {
132 warn "W: skipping unknown dir: $old/$dn\n"
135 die "No Xapian parts found in $old\n" unless @cmds;
137 my $im = $ibx->importer(0);
138 my $max = $opt->{jobs} || scalar(@cmds);
139 $ibx->with_umask(sub {
142 # fine-grained locking if we prepare for reindex
144 prepare_reindex($ibx, $reindex);
147 delete($ibx->{$_}) for (qw(mm over search)); # cleanup
150 while (scalar(keys(%pids)) < $max && scalar(@cmds)) {
152 $pids{xspawn($x, $env, $opt)} = $x;
155 while (scalar keys %pids) {
156 my $pid = waitpid(-1, 0);
157 my $x = delete $pids{$pid};
158 die join(' ', @$x)." failed: $?\n" if $?;
161 commit_changes($ibx, $old, $new, $opt);
165 sub cpdb_retryable ($$) {
166 my ($src, $pfx) = @_;
167 if (ref($@) eq 'Search::Xapian::DatabaseModifiedError') {
168 warn "$pfx Xapian DB modified, reopening and retrying\n";
173 warn "$pfx E: ", ref($@), "\n";
179 # Like copydatabase(1), this is horribly slow; and it doesn't seem due
180 # to the overhead of Perl.
182 my ($args, $env, $opt) = @_;
183 my ($old, $new) = @$args;
184 my $src = Search::Xapian::Database->new($old);
185 my $tmp = $opt->{compact} ? "$new.compact" : $new;
187 # like copydatabase(1), be sure we don't overwrite anything in case
189 my $creat = Search::Xapian::DB_CREATE();
190 my $dst = Search::Xapian::WritableDatabase->new($tmp, $creat);
193 my ($nr, $tot, $fmt); # progress output
197 # update the only metadata key for v1:
198 my $lc = $src->get_metadata('last_commit');
199 $dst->set_metadata('last_commit', $lc) if $lc;
201 $it = $src->postlist_begin('');
202 $end = $src->postlist_end('');
203 $pfx = (split('/', $old))[-1].':';
204 if ($opt->{-progress}) {
206 $tot = $src->get_doccount;
207 $fmt = "$pfx % ".length($tot)."u/$tot\n";
208 warn "$pfx copying $tot documents\n";
211 } while (cpdb_retryable($src, $pfx));
215 while ($it != $end) {
216 my $docid = $it->get_docid;
217 my $doc = $src->get_document($docid);
218 $dst->replace_document($docid, $doc);
220 if ($fmt && !(++$nr & 1023)) {
221 warn(sprintf($fmt, $nr));
225 # unlike copydatabase(1), we don't copy spelling
226 # and synonym data (or other user metadata) since
227 # the Perl APIs don't expose iterators for them
228 # (and public-inbox does not use those features)
230 } while (cpdb_retryable($src, $pfx));
232 warn(sprintf($fmt, $nr)) if $fmt;
233 return unless $opt->{compact};
235 $src = $dst = undef; # flushes and closes
236 $pfx = undef unless $fmt;
238 warn "$pfx compacting...\n" if $pfx;
239 # this is probably the best place to do xapian-compact
240 # since $dst isn't readable by HTTP or NNTP clients, yet:
241 my $cmd = [ $XAPIAN_COMPACT, '--no-renumber', $tmp, $new ];
243 foreach my $fd (0..2) {
244 defined(my $dst = $opt->{$fd}) or next;
249 if ($pfx && pipe($r, $w)) {
250 $rdr->{1} = fileno($w);
252 my $pid = spawn($cmd, $env, $rdr);
254 close $w or die "close: \$w: $!";
260 my $rp = waitpid($pid, 0);
261 if ($? || $rp != $pid) {
262 die join(' ', @$cmd)." failed: $? (pid=$pid, reaped=$rp)\n";
264 remove_tree($tmp) or die "failed to remove $tmp: $!\n";