mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 04:46:41 -05:00
fix: keep failed statvfs results in the cache and track entry validity
- A new entry was told apart from a fresh one only by its time of 0. With a timeout at least the current time in seconds, such as cache.statfs=4294967295, a new entry looked fresh before statvfs had run, so the zeroed struct was returned as success: every branch reported no free space and every create failed with ENOSPC, and a nonexistent path succeeded.3735d721applies cache.statfs from the mount options, which made it reachable from fstab. Give the entry a valid flag set by the first statvfs. -5e74822fstopped caching a failure as a success by retrying it on every call, under the exclusive lock. With a branch that fails slowly, such as an unreachable network mount, each caller then waited for its own failure one after the other: eight callers took 2.4s where 2.42.2 took 0.3s. Keep the failure, as an error, for the timeout like a success. It is still never returned as zeroed space. - Read the clock again once the lock is held, so a caller that waited doesn't compute a negative age against the entry another refreshed. - Tests: a failure is kept until the timeout, and timeouts of 4294967295 and UINT64_MAX still return real values and errors. Both fail without this change. Measure age with a steady clock, starting when statvfs returns. Even a refresh slower than the configured timeout can be shared by callers that waited for it; wall-clock adjustments cannot extend its lifetime.
This commit is contained in:
parent
64d3af02a3
commit
898ad5992d
3 changed files with 116 additions and 32 deletions
|
|
@ -118,7 +118,9 @@ A number of policies require looking up the available space of the
|
|||
branches being considered. This is accomplished by calling
|
||||
[statfs](https://man7.org/linux/man-pages/man2/statfs.2.html). This
|
||||
call however is a bit expensive so this cache reduces the overhead by
|
||||
limiting how often the calls are actually made.
|
||||
limiting how often the calls are actually made. Successful and failed
|
||||
lookups are cached for the same timeout. The timeout starts when the
|
||||
lookup finishes and is unaffected by changes to the system clock.
|
||||
|
||||
This will mean that if the available space of branches changed
|
||||
somewhat rapidly there is a risk of `create` or `mkdir` calls made
|
||||
|
|
|
|||
|
|
@ -22,19 +22,25 @@
|
|||
#include "statvfs_util.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <sys/statvfs.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
// The result of the last statvfs: the data, or the negative errno it
|
||||
// failed with. `valid` is false until the first statvfs has run, so a
|
||||
// freshly inserted entry is never taken for a fresh result whatever the
|
||||
// timeout.
|
||||
struct Element
|
||||
{
|
||||
u64 time;
|
||||
struct statvfs st;
|
||||
u64 time = 0;
|
||||
bool valid = false;
|
||||
int rv = 0;
|
||||
struct statvfs st = {};
|
||||
};
|
||||
|
||||
typedef std::unordered_map<std::string,Element> statvfs_cache;
|
||||
|
|
@ -52,11 +58,8 @@ static
|
|||
u64
|
||||
_get_time(void)
|
||||
{
|
||||
u64 rv;
|
||||
|
||||
rv = ::time(NULL);
|
||||
|
||||
return rv;
|
||||
using namespace std::chrono;
|
||||
return duration_cast<seconds>(steady_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
u64
|
||||
|
|
@ -71,51 +74,62 @@ fs::statvfs_cache_timeout(cu64 timeout_)
|
|||
g_timeout.store(timeout_,std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
static
|
||||
bool
|
||||
_fresh(const Element &e_,
|
||||
const u64 now_,
|
||||
const u64 timeout_)
|
||||
{
|
||||
return (e_.valid && ((now_ - e_.time) <= timeout_));
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_result(const Element &e_,
|
||||
struct statvfs *st_)
|
||||
{
|
||||
if(e_.rv < 0)
|
||||
return e_.rv;
|
||||
|
||||
*st_ = e_.st;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
fs::statvfs_cache(const std::string &path_,
|
||||
struct statvfs *st_)
|
||||
{
|
||||
u64 now;
|
||||
u64 timeout;
|
||||
|
||||
timeout = g_timeout.load(std::memory_order_relaxed);
|
||||
if(timeout == 0)
|
||||
return fs::statvfs(path_,st_);
|
||||
|
||||
now = ::_get_time();
|
||||
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lk(g_cache_mutex);
|
||||
|
||||
auto it = g_cache.find(path_);
|
||||
if((it != g_cache.end()) && ((now - it->second.time) <= timeout))
|
||||
{
|
||||
*st_ = it->second.st;
|
||||
return 0;
|
||||
}
|
||||
if((it != g_cache.end()) && ::_fresh(it->second,::_get_time(),timeout))
|
||||
return ::_result(it->second,st_);
|
||||
}
|
||||
|
||||
std::unique_lock<std::shared_mutex> lk(g_cache_mutex);
|
||||
|
||||
// Only a successful statvfs refreshes the entry. A failure is
|
||||
// returned to the caller and retried on the next call.
|
||||
// Another caller may have refreshed the entry while we waited.
|
||||
Element &e = g_cache[path_];
|
||||
if((now - e.time) > timeout)
|
||||
if(not ::_fresh(e,::_get_time(),timeout))
|
||||
{
|
||||
int rv;
|
||||
struct statvfs st;
|
||||
|
||||
rv = fs::statvfs(path_,&st);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
|
||||
e.time = now;
|
||||
e.st = st;
|
||||
// A failure is kept for the timeout like a success. Retrying it on
|
||||
// every call would hold this lock for a slow failing branch,
|
||||
// such as an unreachable network mount, one caller at a time.
|
||||
// It is never stored as zeroed space.
|
||||
e.rv = fs::statvfs(path_,&e.st);
|
||||
e.time = ::_get_time();
|
||||
e.valid = true;
|
||||
}
|
||||
|
||||
*st_ = e.st;
|
||||
|
||||
return 0;
|
||||
return ::_result(e,st_);
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
|||
|
|
@ -3815,6 +3815,72 @@ test_statvfs_cache_does_not_cache_failure()
|
|||
fs::statvfs_cache_timeout(0);
|
||||
}
|
||||
|
||||
// A failure is kept for the timeout like a success, so a branch that
|
||||
// fails slowly isn't asked again on every call. Create the directory
|
||||
// after the first failure: a cached failure still shows ENOENT, an
|
||||
// uncached call would succeed.
|
||||
void
|
||||
test_statvfs_cache_keeps_failure_for_timeout()
|
||||
{
|
||||
int rv;
|
||||
struct statvfs st;
|
||||
char tmp_template[] = "/tmp/mergerfs-test-statvfs-cache-XXXXXX";
|
||||
|
||||
if(::mkdtemp(tmp_template) == nullptr)
|
||||
{
|
||||
TEST_CHECK(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string path = std::string(tmp_template) + "/later";
|
||||
|
||||
fs::statvfs_cache_timeout(60);
|
||||
rv = fs::statvfs_cache(path,&st);
|
||||
TEST_CHECK(rv == -ENOENT);
|
||||
TEST_MSG("first rv: %d",rv);
|
||||
|
||||
TEST_CHECK(::mkdir(path.c_str(),0700) == 0);
|
||||
rv = fs::statvfs_cache(path,&st);
|
||||
TEST_CHECK(rv == -ENOENT);
|
||||
TEST_MSG("cached rv: %d",rv);
|
||||
|
||||
fs::statvfs_cache_timeout(0);
|
||||
rv = fs::statvfs_cache(path,&st);
|
||||
TEST_CHECK(rv == 0);
|
||||
TEST_MSG("uncached rv: %d",rv);
|
||||
|
||||
::rmdir(path.c_str());
|
||||
::rmdir(tmp_template);
|
||||
}
|
||||
|
||||
// A timeout of at least the current time in seconds made a new entry
|
||||
// look fresh before any statvfs had run, so every branch reported no
|
||||
// free space and a failure was reported as success.
|
||||
void
|
||||
test_statvfs_cache_huge_timeout()
|
||||
{
|
||||
int rv;
|
||||
struct statvfs st;
|
||||
|
||||
for(const u64 timeout : {(u64)4294967295ULL,(u64)~0ULL})
|
||||
{
|
||||
fs::statvfs_cache_timeout(timeout);
|
||||
|
||||
memset(&st,0,sizeof(st));
|
||||
rv = fs::statvfs_cache("/",&st);
|
||||
TEST_CHECK(rv == 0);
|
||||
TEST_CHECK(st.f_blocks > 0);
|
||||
TEST_MSG("timeout: %llu; rv: %d; blocks: %lu",
|
||||
(unsigned long long)timeout,rv,(unsigned long)st.f_blocks);
|
||||
|
||||
rv = fs::statvfs_cache("/nonexistent/mergerfs-statvfs-huge-timeout",&st);
|
||||
TEST_CHECK(rv == -ENOENT);
|
||||
TEST_MSG("timeout: %llu; missing rv: %d",(unsigned long long)timeout,rv);
|
||||
}
|
||||
|
||||
fs::statvfs_cache_timeout(0);
|
||||
}
|
||||
|
||||
void
|
||||
test_hashset_put_and_size()
|
||||
{
|
||||
|
|
@ -4524,6 +4590,8 @@ TEST_LIST =
|
|||
{"fs_movefile_and_open_same_branch",test_fs_movefile_and_open_same_branch},
|
||||
{"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts},
|
||||
{"statvfs_cache_does_not_cache_failure",test_statvfs_cache_does_not_cache_failure},
|
||||
{"statvfs_cache_keeps_failure_for_timeout",test_statvfs_cache_keeps_failure_for_timeout},
|
||||
{"statvfs_cache_huge_timeout",test_statvfs_cache_huge_timeout},
|
||||
{"str_eq_nullptr",test_str_eq_nullptr},
|
||||
{"str_startswith_char_nullptr",test_str_startswith_char_nullptr},
|
||||
{"str_from_u64_suffixes",test_str_from_u64_suffixes},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue