From 9ff42a0d6dd7adc8adddf99e2e3eaf1ecdaaf59c Mon Sep 17 00:00:00 2001 From: Trevor Clinkenbeard Date: Sun, 22 Nov 2026 16:31:21 -0800 Subject: [PATCH 1/2] Retry backup encryption key loads after disk errors --- fdbclient/BackupContainer.cpp | 1 - fdbclient/BackupContainerFileSystem.cpp | 38 +++++++++++++++++-- .../fdbclient/BackupContainerFileSystem.h | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/fdbclient/BackupContainer.cpp b/fdbclient/BackupContainer.cpp index 43866175239..fc129dc69a3 100644 --- a/fdbclient/BackupContainer.cpp +++ b/fdbclient/BackupContainer.cpp @@ -320,7 +320,6 @@ Reference IBackupContainer::openContainer(const std::string& u throw backup_invalid_url(); } - r->encryptionKeyFileName = encryptionKeyFileName; r->URL = url; return r; } catch (Error& e) { diff --git a/fdbclient/BackupContainerFileSystem.cpp b/fdbclient/BackupContainerFileSystem.cpp index 7d153c603b8..40158ba2817 100644 --- a/fdbclient/BackupContainerFileSystem.cpp +++ b/fdbclient/BackupContainerFileSystem.cpp @@ -1920,11 +1920,20 @@ class BackupContainerFileSystemImpl { throw; } } - // Containers retain this setup future, so recover disk errors before publishing a permanent failure. co_await delay(1.0); } } + static Future> getEncryptionKey( + Future>& key, + const std::string& keyFileName) { + // Share in-flight reads and keep using the first successfully loaded key for the life of the container. + if (key.isError() && key.getError().isDiskError()) { + key = readEncryptionKey(keyFileName); + } + return key; + } + static Future writeEncryptionMetadataIfNotExists(Reference bc, int encryptionBlockSize) { try { @@ -2377,14 +2386,18 @@ std::string BackupContainerFileSystem::encryptionMetadataFileName() { bool BackupContainerFileSystem::usesEncryption() const { return encryptionKey.isValid(); } + Future BackupContainerFileSystem::encryptionSetupComplete() const { - return usesEncryption() ? success(encryptionKey) : Future(); + return usesEncryption() + ? success(BackupContainerFileSystemImpl::getEncryptionKey(encryptionKey, encryptionKeyFileName.get())) + : Future(); } Future> BackupContainerFileSystem::encryptedFile(Future> file, AsyncFileEncrypted::Mode mode) const { - return map(success(file) && encryptionSetupComplete(), - [file, key = encryptionKey, mode, blockSize = encryptionBlockSize](Void) -> Reference { + auto key = BackupContainerFileSystemImpl::getEncryptionKey(encryptionKey, encryptionKeyFileName.get()); + return map(success(file) && success(key), + [file, key, mode, blockSize = encryptionBlockSize](Void) -> Reference { return makeReference(file.get(), mode, blockSize, key.get()); }); } @@ -2396,6 +2409,7 @@ Future BackupContainerFileSystem::writeEntireFileFallback(const std::strin } void BackupContainerFileSystem::setEncryptionKey(Optional const& encryptionKeyFileName) { + this->encryptionKeyFileName = encryptionKeyFileName; if (encryptionKeyFileName.present()) { encryptionKey = BackupContainerFileSystemImpl::readEncryptionKey(encryptionKeyFileName.get()); } @@ -2416,6 +2430,22 @@ int chooseFileSize(std::vector& sizes) { return deterministicRandom()->randomInt(0, 2e6); } +TEST_CASE("/backup/containers/encryptionKeyAfterReadFailure") { + const std::string keyFile = joinPath(params.getDataDir(), deterministicRandom()->randomUniqueID().toString()); + co_await BackupContainerFileSystem::createTestEncryptionKeyFile(keyFile); + Future> cached = io_timeout(); + auto first = BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile); + auto second = BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile); + co_await (success(first) && success(second)); + ASSERT(first.get() == second.get()); + ASSERT(BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile).get() == first.get()); + + cached = invalid_encryption_key_file(); + auto invalid = BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile); + ASSERT(invalid.isError()); + ASSERT_EQ(invalid.getError().code(), error_code_invalid_encryption_key_file); +} + TEST_CASE("/backup/containers/localdir/independentEncryptionKeys") { const std::string base = joinPath(params.getDataDir(), deterministicRandom()->randomUniqueID().toString()); auto open = [&](const std::string& name) { diff --git a/fdbclient/include/fdbclient/BackupContainerFileSystem.h b/fdbclient/include/fdbclient/BackupContainerFileSystem.h index 1195d62e52d..39d424f14e5 100644 --- a/fdbclient/include/fdbclient/BackupContainerFileSystem.h +++ b/fdbclient/include/fdbclient/BackupContainerFileSystem.h @@ -229,7 +229,7 @@ class BackupContainerFileSystem : public IBackupContainer { friend class BackupContainerFileSystemImpl; - Future> encryptionKey; + mutable Future> encryptionKey; }; #endif From 36c90a3371bbd67e8b22e674c2bb690825616556 Mon Sep 17 00:00:00 2001 From: Trevor Clinkenbeard Date: Wed, 2 Dec 2026 12:24:26 -0800 Subject: [PATCH 2/2] Keep backup agent status responsive during encryption key retries --- fdbbackup/backup.cpp | 2 +- fdbclient/BackupContainerFileSystem.cpp | 76 ++++++++++++------- fdbclient/include/fdbclient/BackupContainer.h | 3 + .../fdbclient/BackupContainerFileSystem.h | 4 +- 4 files changed, 57 insertions(+), 28 deletions(-) diff --git a/fdbbackup/backup.cpp b/fdbbackup/backup.cpp index e8df8070d1a..2efb847de95 100644 --- a/fdbbackup/backup.cpp +++ b/fdbbackup/backup.cpp @@ -1495,7 +1495,7 @@ AsyncResult getLayerStatus(Reference tr, // that's something that should never happen. Reference bc = TupleCodec>::unpack(value); if (encryptionKeyFileName.present()) { - encryptionSetupResults.push_back(bc->encryptionSetupComplete()); + encryptionSetupResults.push_back(bc->encryptionSetupStatus()); encryptionKeyPaths.push_back(encryptionKeyFileName.get()); } } catch (Error& e) { diff --git a/fdbclient/BackupContainerFileSystem.cpp b/fdbclient/BackupContainerFileSystem.cpp index 40158ba2817..e28916f9628 100644 --- a/fdbclient/BackupContainerFileSystem.cpp +++ b/fdbclient/BackupContainerFileSystem.cpp @@ -1924,15 +1924,7 @@ class BackupContainerFileSystemImpl { } } - static Future> getEncryptionKey( - Future>& key, - const std::string& keyFileName) { - // Share in-flight reads and keep using the first successfully loaded key for the life of the container. - if (key.isError() && key.getError().isDiskError()) { - key = readEncryptionKey(keyFileName); - } - return key; - } + static Future testEncryptionKeyAfterReadFailure(std::string path); static Future writeEncryptionMetadataIfNotExists(Reference bc, int encryptionBlockSize) { @@ -2387,15 +2379,30 @@ bool BackupContainerFileSystem::usesEncryption() const { return encryptionKey.isValid(); } +Future> BackupContainerFileSystem::getEncryptionKey() const { + if (encryptionKey.isError() && encryptionKey.getError().isDiskError()) { + lastKeyLoadError = encryptionKey.getError(); + encryptionKey = BackupContainerFileSystemImpl::readEncryptionKey(encryptionKeyFileName.get()); + } + return encryptionKey; +} + Future BackupContainerFileSystem::encryptionSetupComplete() const { - return usesEncryption() - ? success(BackupContainerFileSystemImpl::getEncryptionKey(encryptionKey, encryptionKeyFileName.get())) - : Future(); + return usesEncryption() ? success(getEncryptionKey()) : Future(); +} + +Future BackupContainerFileSystem::encryptionSetupStatus() const { + auto setup = encryptionSetupComplete(); + // Repeatedly waiting through the disk retry delays can exhaust every status transaction's lifetime. + if (lastKeyLoadError.isValid() && !setup.isReady()) { + return lastKeyLoadError; + } + return setup; } Future> BackupContainerFileSystem::encryptedFile(Future> file, AsyncFileEncrypted::Mode mode) const { - auto key = BackupContainerFileSystemImpl::getEncryptionKey(encryptionKey, encryptionKeyFileName.get()); + auto key = getEncryptionKey(); return map(success(file) && success(key), [file, key, mode, blockSize = encryptionBlockSize](Void) -> Reference { return makeReference(file.get(), mode, blockSize, key.get()); @@ -2419,6 +2426,34 @@ Future BackupContainerFileSystem::createTestEncryptionKeyFile(std::string return BackupContainerFileSystemImpl::createTestEncryptionKeyFile(filename); } +Future BackupContainerFileSystemImpl::testEncryptionKeyAfterReadFailure(std::string path) { + const std::string keyFile = path + ".key"; + co_await BackupContainerFileSystem::createTestEncryptionKeyFile(keyFile); + auto container = makeReference("file://" + path, keyFile, 4096); + co_await container->encryptionSetupStatus(); + + container->encryptionKey = io_timeout(); + auto status = container->encryptionSetupStatus(); + ASSERT(status.isReady()); + auto first = container->encryptionKey; + auto second = container->getEncryptionKey(); + if (!first.isReady()) { + ASSERT(status.isError()); + ASSERT_EQ(status.getError().code(), error_code_io_timeout); + ASSERT(container->encryptionSetupStatus().isError()); + ASSERT(!container->encryptionSetupComplete().isReady()); + } + co_await (success(first) && success(second)); + ASSERT(first.get() == second.get()); + ASSERT(container->getEncryptionKey().get() == first.get()); + ASSERT(container->encryptionSetupStatus().canGet()); + + container->encryptionKey = invalid_encryption_key_file(); + auto invalid = container->encryptionSetupStatus(); + ASSERT(invalid.isError()); + ASSERT_EQ(invalid.getError().code(), error_code_invalid_encryption_key_file); +} + namespace backup_test { int chooseFileSize(std::vector& sizes) { @@ -2431,19 +2466,8 @@ int chooseFileSize(std::vector& sizes) { } TEST_CASE("/backup/containers/encryptionKeyAfterReadFailure") { - const std::string keyFile = joinPath(params.getDataDir(), deterministicRandom()->randomUniqueID().toString()); - co_await BackupContainerFileSystem::createTestEncryptionKeyFile(keyFile); - Future> cached = io_timeout(); - auto first = BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile); - auto second = BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile); - co_await (success(first) && success(second)); - ASSERT(first.get() == second.get()); - ASSERT(BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile).get() == first.get()); - - cached = invalid_encryption_key_file(); - auto invalid = BackupContainerFileSystemImpl::getEncryptionKey(cached, keyFile); - ASSERT(invalid.isError()); - ASSERT_EQ(invalid.getError().code(), error_code_invalid_encryption_key_file); + co_await BackupContainerFileSystemImpl::testEncryptionKeyAfterReadFailure( + joinPath(params.getDataDir(), deterministicRandom()->randomUniqueID().toString())); } TEST_CASE("/backup/containers/localdir/independentEncryptionKeys") { diff --git a/fdbclient/include/fdbclient/BackupContainer.h b/fdbclient/include/fdbclient/BackupContainer.h index 1a780020b21..358f7ddf7a9 100644 --- a/fdbclient/include/fdbclient/BackupContainer.h +++ b/fdbclient/include/fdbclient/BackupContainer.h @@ -400,7 +400,10 @@ class IBackupContainer { static std::string lastOpenError; + // Wait for a usable key, retrying a previous disk failure. virtual Future encryptionSetupComplete() const = 0; + // Wait for the first key load. During a retry, report the last completed result. + virtual Future encryptionSetupStatus() const { return encryptionSetupComplete(); } virtual int getEncryptionBlockSize() const { return 0; } virtual void setEncryptionBlockSize(int blockSize) {} diff --git a/fdbclient/include/fdbclient/BackupContainerFileSystem.h b/fdbclient/include/fdbclient/BackupContainerFileSystem.h index 39d424f14e5..f4fef9bae75 100644 --- a/fdbclient/include/fdbclient/BackupContainerFileSystem.h +++ b/fdbclient/include/fdbclient/BackupContainerFileSystem.h @@ -175,8 +175,8 @@ class BackupContainerFileSystem : public IBackupContainer { Future writeEncryptionMetadata(int encryptionBlockSize) override; - // Waits for encryption initialization to complete by reading encryption key file during container opening. Future encryptionSetupComplete() const override; + Future encryptionSetupStatus() const override; int getEncryptionBlockSize() const override { return encryptionBlockSize; } void setEncryptionBlockSize(int blockSize) override { encryptionBlockSize = blockSize; } @@ -229,7 +229,9 @@ class BackupContainerFileSystem : public IBackupContainer { friend class BackupContainerFileSystemImpl; + Future> getEncryptionKey() const; mutable Future> encryptionKey; + mutable Error lastKeyLoadError; }; #endif