]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Xapcmd.pm
doc: lei-q: document SEARCH TERMS prefixes
[public-inbox.git] / lib / PublicInbox / Xapcmd.pm
index 8b8958c71ffbc6749427215664469eab2ae1378e..44e0f8e58dca6ac1374ce01d953252a9f7a4bb8c 100644 (file)
@@ -8,7 +8,6 @@ use PublicInbox::Over;
 use PublicInbox::SearchIdx;
 use File::Temp 0.19 (); # ->newdir
 use File::Path qw(remove_tree);
-use File::Basename qw(dirname);
 use POSIX qw(WNOHANG _exit);
 
 # support testing with dev versions of Xapian which installs
@@ -21,13 +20,25 @@ sub commit_changes ($$$$) {
        my $reshard = $opt->{reshard};
 
        $SIG{INT} or die 'BUG: $SIG{INT} not handled';
-       my @old_shard;
-       my $over_chg;
-
-       while (my ($old, $newdir) = each %$tmp) {
+       my (@old_shard, $over_chg);
+
+       # Sort shards highest-to-lowest, since ->xdb_shards_flat
+       # determines the number of shards to load based on the max;
+       # and we'd rather xdb_shards_flat to momentarily fail rather
+       # than load out-of-date shards
+       my @order = sort {
+               my ($x) = ($a =~ m!/([0-9]+)/*\z!);
+               my ($y) = ($b =~ m!/([0-9]+)/*\z!);
+               ($y // -1) <=> ($x // -1) # we may have non-shards
+       } keys %$tmp;
+
+       my ($dname) = ($order[0] =~ m!(.*/)[^/]+/*\z!);
+       my $mode = (stat($dname))[2];
+       for my $old (@order) {
                next if $old eq ''; # no invalid paths
-               my @st = stat($old);
-               if (!@st && !defined($opt->{reshard})) {
+               my $newdir = $tmp->{$old};
+               my $have_old = -e $old;
+               if (!$have_old && !defined($opt->{reshard})) {
                        die "failed to stat($old): $!";
                }
 
@@ -47,17 +58,13 @@ sub commit_changes ($$$$) {
                        next;
                }
 
-               if (@st) {
-                       chmod($st[2] & 07777, $new) or die "chmod $old: $!\n";
+               chmod($mode & 07777, $new) or die "chmod($new): $!\n";
+               if ($have_old) {
                        rename($old, "$new/old") or
                                        die "rename $old => $new/old: $!\n";
                }
                rename($new, $old) or die "rename $new => $old: $!\n";
-               if (@st) {
-                       my $prev = "$old/old";
-                       remove_tree($prev) or
-                               die "failed to remove $prev: $!\n";
-               }
+               push @old_shard, "$old/old" if $have_old;
        }
 
        # trigger ->check_inodes in read-only daemons
@@ -150,7 +157,7 @@ sub process_queue {
 
        # run in parallel:
        my %pids;
-       local %SIG = %SIG;
+       local @SIG{keys %SIG} = values %SIG;
        setup_signals(\&kill_pids, \%pids);
        while (@$queue) {
                while (scalar(keys(%pids)) < $max && scalar(@$queue)) {
@@ -199,7 +206,7 @@ sub prepare_run {
                        warn
 "--reshard=$reshard ignored for v1 $ibx->{inboxdir}\n";
                }
-               my $dir = dirname($old);
+               my ($dir) = ($old =~ m!(.*?/)[^/]+/*\z!);
                same_fs_or_die($dir, $old);
                my $v = PublicInbox::Search::SCHEMA_VERSION();
                my $wip = File::Temp->newdir("xapian$v-XXXX", DIR => $dir);
@@ -286,7 +293,7 @@ sub run {
                PublicInbox::SearchIdx::load_xapian_writable();
        }
 
-       local %SIG = %SIG;
+       local @SIG{keys %SIG} = values %SIG;
        setup_signals();
        $ibx->with_umask(\&_run, $ibx, $cb, $opt);
 }
@@ -344,7 +351,7 @@ sub compact ($$) { # cb_spawn callback
        $pr->("$pfx `".join(' ', @$cmd)."'\n") if $pr;
        push @$cmd, $src, $dst;
        my ($rd, $pid);
-       local %SIG = %SIG;
+       local @SIG{keys %SIG} = values %SIG;
        setup_signals(\&kill_compact, \$pid);
        ($rd, $pid) = popen_rd($cmd, undef, $rdr);
        while (<$rd>) {
@@ -429,9 +436,9 @@ sub cpdb ($$) { # cb_spawn callback
        }
 
        my ($tmp, $ft);
-       local %SIG = %SIG;
+       local @SIG{keys %SIG} = values %SIG;
        if ($opt->{compact}) {
-               my $dir = dirname($new);
+               my ($dir) = ($new =~ m!(.*?/)[^/]+/*\z!);
                same_fs_or_die($dir, $new);
                $ft = File::Temp->newdir("$new.compact-XXXX", DIR => $dir);
                setup_signals();