user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/3] xcpdb: avoid race when shards are added
Date: Thu, 23 Sep 2021 05:53:03 +0000	[thread overview]
Message-ID: <20210923055303.6409-4-e@80x24.org> (raw)
In-Reply-To: <20210923055303.6409-1-e@80x24.org>

It's possible for the rename() sequence to cause read-only
daemons using ->xdb_shards_flat to load an incomplete set of
contiguous shards and get invalid docids for search results.

With this change, we favor the case where search is momentarily
unavailable rather than giving wrong results during the small
window where Xapcmd->commit_changes runs.
---
 lib/PublicInbox/Xapcmd.pm | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/lib/PublicInbox/Xapcmd.pm b/lib/PublicInbox/Xapcmd.pm
index daef896c..44e0f8e5 100644
--- a/lib/PublicInbox/Xapcmd.pm
+++ b/lib/PublicInbox/Xapcmd.pm
@@ -20,16 +20,23 @@ sub commit_changes ($$$$) {
 	my $reshard = $opt->{reshard};
 
 	$SIG{INT} or die 'BUG: $SIG{INT} not handled';
-	my @old_shard;
-	my $over_chg;
-	my $mode;
-
-	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
-		$mode //= do {
-			my ($dname) = ($old =~ m!(.*/)[^/]+/*\z!);
-			(stat($dname))[2];
-		};
+		my $newdir = $tmp->{$old};
 		my $have_old = -e $old;
 		if (!$have_old && !defined($opt->{reshard})) {
 			die "failed to stat($old): $!";
@@ -57,11 +64,7 @@ sub commit_changes ($$$$) {
 					die "rename $old => $new/old: $!\n";
 		}
 		rename($new, $old) or die "rename $new => $old: $!\n";
-		if ($have_old) {
-			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

      parent reply	other threads:[~2021-09-23  5:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-23  5:53 [PATCH 0/3] xcpdb-related fixes Eric Wong
2021-09-23  5:53 ` [PATCH 1/3] test_common: reset umask on non-forking run_script Eric Wong
2021-09-23  5:53 ` [PATCH 2/3] xcpdb: -R$SHARDS creates new shards with correct perms Eric Wong
2021-09-23  5:53 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210923055303.6409-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).