From 6d96e791f9bb8cd25cbdb1c3fecfcaad9fff6a20 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 24 Oct 2025 23:10:06 +0200 Subject: optimize filecas write file (#613) * try to move file into place before trying speculative remove of target file --- src/zenstore/cache/cachedisklayer.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'src/zenstore/cache/cachedisklayer.cpp') diff --git a/src/zenstore/cache/cachedisklayer.cpp b/src/zenstore/cache/cachedisklayer.cpp index c2e811003..67f587a7c 100644 --- a/src/zenstore/cache/cachedisklayer.cpp +++ b/src/zenstore/cache/cachedisklayer.cpp @@ -2724,14 +2724,19 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c RwLock::ExclusiveLockScope ValueLock(LockForHash(HashKey)); - // We do a speculative remove of the file instead of probing with a exists call and check the error code instead - RemoveFile(FsPath, Ec); + // Assume parent directory exists + DataFile.MoveTemporaryIntoPlace(FsPath, Ec); if (Ec) { - if (Ec.value() != ENOENT) + CreateDirectories(FsPath.parent_path()); + + // Try again after we or someone else created the directory + Ec.clear(); + DataFile.MoveTemporaryIntoPlace(FsPath, Ec); + + if (Ec) { - ZEN_WARN("Failed to remove file '{}' for put in '{}', reason: '{}', retrying.", FsPath, m_BucketDir, Ec.message()); - Sleep(100); + // We do a speculative remove of the file instead of probing with a exists call and check the error code instead Ec.clear(); RemoveFile(FsPath, Ec); if (Ec && Ec.value() != ENOENT) @@ -2739,17 +2744,6 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c throw std::system_error(Ec, fmt::format("Failed to remove file '{}' for put in '{}'", FsPath, m_BucketDir)); } } - } - - // Assume parent directory exists - DataFile.MoveTemporaryIntoPlace(FsPath, Ec); - if (Ec) - { - CreateDirectories(FsPath.parent_path()); - - // Try again after we or someone else created the directory - Ec.clear(); - DataFile.MoveTemporaryIntoPlace(FsPath, Ec); // Retry if we still fail to handle contention to file system uint32_t RetriesLeft = 3; -- cgit v1.2.3