From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 5AC0720380 for ; Thu, 23 May 2019 09:37:11 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 17/26] xapcmd: avoid EXDEV when finalizing changes Date: Thu, 23 May 2019 09:36:55 +0000 Message-Id: <20190523093704.18367-18-e@80x24.org> In-Reply-To: <20190523093704.18367-1-e@80x24.org> References: <20190523093704.18367-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: By creating temporary directories as deep as possible, we can allow v2 repositories to have `xap$SCHEMA_VERSION' (e.g. `xap15') reside on a separate FS. We also check st_dev ahead-of-time to avoid doing work which will fail with EXDEV. Of course, another process may still move/change things around. --- lib/PublicInbox/Xapcmd.pm | 59 ++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/lib/PublicInbox/Xapcmd.pm b/lib/PublicInbox/Xapcmd.pm index 697221d..860f90a 100644 --- a/lib/PublicInbox/Xapcmd.pm +++ b/lib/PublicInbox/Xapcmd.pm @@ -8,31 +8,36 @@ use PublicInbox::Over; use PublicInbox::Search; use File::Temp qw(tempdir); use File::Path qw(remove_tree); +use File::Basename qw(dirname); # support testing with dev versions of Xapian which installs # commands with a version number suffix (e.g. "xapian-compact-1.5") our $XAPIAN_COMPACT = $ENV{XAPIAN_COMPACT} || 'xapian-compact'; -sub commit_changes ($$$$) { - my ($ibx, $old, $new, $opt) = @_; +sub commit_changes ($$$) { + my ($ibx, $tmp, $opt) = @_; my $reindex = $opt->{reindex}; my $im = $ibx->importer(0); $im->lock_acquire if $reindex; - my @st = stat($old) or die "failed to stat($old): $!\n"; + while (my ($old, $new) = each %$tmp) { + my @st = stat($old) or die "failed to stat($old): $!\n"; - my $over = "$old/over.sqlite3"; - if (-f $over) { - $over = PublicInbox::Over->new($over); - $over->connect->sqlite_backup_to_file("$new/over.sqlite3"); - $over = undef; - } - rename($old, "$new/old") or die "rename $old => $new/old: $!\n"; - chmod($st[2] & 07777, $new) or die "chmod $old: $!\n"; - rename($new, $old) or die "rename $new => $old: $!\n"; - remove_tree("$old/old") or die "failed to remove $old/old: $!\n"; + my $over = "$old/over.sqlite3"; + if (-f $over) { # only for v1, v2 over is untouched + $over = PublicInbox::Over->new($over); + my $tmp_over = "$new/over.sqlite3"; + $over->connect->sqlite_backup_to_file($tmp_over); + $over = undef; + } + rename($old, "$new/old") or die "rename $old => $new/old: $!\n"; + chmod($st[2] & 07777, $new) or die "chmod $old: $!\n"; + rename($new, $old) or die "rename $new => $old: $!\n"; + my $prev = "$old/old"; + remove_tree($prev) or die "failed to remove $prev: $!\n"; + } if ($reindex) { $opt->{-skip_lock} = 1; PublicInbox::Admin::index_inbox($ibx, $opt); @@ -94,19 +99,23 @@ sub progress_prepare ($) { } } +sub same_fs_or_die ($$) { + my ($x, $y) = @_; + return if ((stat($x))[0] == (stat($y))[0]); # 0 - st_dev + die "$x and $y reside on different filesystems\n"; +} + sub run { my ($ibx, $cmd, $env, $opt) = @_; progress_prepare($opt ||= {}); my $dir = $ibx->{mainrepo} or die "no mainrepo in inbox\n"; my $exe = $cmd->[0]; - my $pfx = $exe; runnable_or_die($XAPIAN_COMPACT) if $opt->{compact}; my $reindex; # v1:{ from => $x40 }, v2:{ from => [ $x40, $x40, .. ] } } my $from; # per-epoch ranges if (ref($exe) eq 'CODE') { - $pfx = 'CODE'; $reindex = $opt->{reindex} = {}; $from = $reindex->{from} = []; require Search::Xapian::WritableDatabase; @@ -116,16 +125,28 @@ sub run { $ibx->umask_prepare; my $old = $ibx->search->xdir(1); -d $old or die "$old does not exist\n"; - my $new = tempdir("$pfx-XXXXXXXX", DIR => $dir); + + my $tmp = {}; # old partition => new (tmp) partition my $v = $ibx->{version} ||= 1; my @cmds; + + # we want temporary directories to be as deep as possible, + # so v2 partitions can keep "xap$SCHEMA_VERSION" on a separate FS. if ($v == 1) { - push @cmds, [@$cmd, $old, $new]; + my $old_parent = dirname($old); + same_fs_or_die($old_parent, $old); + $tmp->{$old} = tempdir('xapcmd-XXXXXXXX', DIR => $old_parent); + push @cmds, [ @$cmd, $old, $tmp->{$old} ]; } else { opendir my $dh, $old or die "Failed to opendir $old: $!\n"; while (defined(my $dn = readdir($dh))) { if ($dn =~ /\A\d+\z/) { - push @cmds, [@$cmd, "$old/$dn", "$new/$dn"]; + my $tmpl = "$dn-XXXXXXXX"; + my $dst = tempdir($tmpl, DIR => $old); + same_fs_or_die($old, $dst); + my $cur = "$old/$dn"; + push @cmds, [@$cmd, $cur, $dst ]; + $tmp->{$cur} = $dst; } elsif ($dn eq '.' || $dn eq '..') { } elsif ($dn =~ /\Aover\.sqlite3/) { } else { @@ -158,7 +179,7 @@ sub run { die join(' ', @$x)." failed: $?\n" if $?; } } - commit_changes($ibx, $old, $new, $opt); + commit_changes($ibx, $tmp, $opt); }); } -- EW