Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions fs/cache/full_file_cache/cache_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ int FileCachePool::evict(std::string_view filename) {
lru_.mark_key_cleared(lruEntry->lruIter);
}
int err = 0;
{
auto cacheStore = static_cast<FileCacheStore*>(open(filePath, O_RDWR, 0644));
auto cacheStore = static_cast<FileCacheStore*>(open(filePath, O_RDWR, 0644));
if (cacheStore) {
DEFER(cacheStore->release());
photon::scoped_rwlock rl(cacheStore->rw_lock(), photon::WLOCK);
err = cacheStore->evict(0);
Expand Down Expand Up @@ -330,8 +330,9 @@ void FileCachePool::eviction() {
continue;
}

{
auto cacheStore = static_cast<FileCacheStore*>(open(fileName, O_RDWR, 0644));
int err = 0;
auto cacheStore = static_cast<FileCacheStore*>(open(fileName, O_RDWR, 0644));
if (cacheStore) {
DEFER(cacheStore->release());
photon::scoped_rwlock rl(cacheStore->rw_lock(), photon::WLOCK);
err = cacheStore->evict(0);
Expand Down
18 changes: 10 additions & 8 deletions fs/cache/full_file_cache/quota_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,17 @@ int QuotaFilePool::evict(std::string_view filename) {
return 0;
}
const auto& filePath = fileIter->first;
int err;
auto lruEntry = static_cast<QuotaLruEntry*>(fileIter->second.get());

int err = 0;
{
auto cacheStore = static_cast<FileCacheStore*>(open(filePath, O_RDWR, 0644));
DEFER(cacheStore->release());
photon::scoped_rwlock rl(cacheStore->rw_lock(), photon::WLOCK);
lru.mark_key_cleared(lruEntry->QuotaLruIter);
err = cacheStore->evict(0);
if (cacheStore) {
DEFER(cacheStore->release());
photon::scoped_rwlock rl(cacheStore->rw_lock(), photon::WLOCK);
lru.mark_key_cleared(lruEntry->QuotaLruIter);
err = cacheStore->evict(0);
}
if (err) {
ERRNO e;
LOG_ERROR("truncate(0) failed, name : `, ret : `, error code : `", filePath, err, ERRNO());
Expand Down Expand Up @@ -244,12 +246,12 @@ void QuotaFilePool::dirEviction() {
auto fileIter = dir->lru.back();
const auto& fileName = fileIter->first;
auto lruEntry = static_cast<QuotaLruEntry*>(fileIter->second.get());
int err;
int err = 0;
bool flags_dir_delete = false;

auto cacheStore = static_cast<FileCacheStore*>(open(fileName, O_RDWR, 0644));
DEFER(cacheStore->release());
{
if (cacheStore) {
DEFER(cacheStore->release());
photon::scoped_rwlock rl(cacheStore->rw_lock(), photon::WLOCK);
if (lruEntry->openCount==0){
dir->lru.mark_key_cleared(lruEntry->QuotaLruIter);
Expand Down
44 changes: 44 additions & 0 deletions fs/cache/test/cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,50 @@ TEST(CachePool, refill_range_non_aligned_tail) {
refillRangeNonAlignedTail(false);
}

TEST(CachePool, eviction_with_deleted_cache_dir) {
std::string root = "/tmp/ease/cache/evict_dir_deleted/";
SetupTestDir(root);
auto mediaFs = new_localfs_adaptor(root.c_str(), ioengine_libaio);
auto alignFs = new_aligned_fs_adaptor(mediaFs, 4 * 1024, true, true);
auto cacheAllocator = new AlignedAlloc(4 * 1024);
// capacity=1GB (waterMark≈900MB, riskMark≈950MB), short store TTL (1ms)
auto roCachedFs = new_full_file_cached_fs(nullptr, alignFs, 1024 * 1024,
1, 100 * 1000 * 1000, 128ull * 1024 * 1024, cacheAllocator, 0, nullptr, 1000);
auto cachePool = roCachedFs->get_pool();
DEFER({ delete cacheAllocator; delete roCachedFs; });

const size_t fileSize = 100 * 1024 * 1024;
IOVector buffer(*cacheAllocator);
buffer.push_back(fileSize);

// Phase 1: write 600MB (below riskMark, no eviction yet)
for (int i = 0; i < 6; i++) {
std::string name = "/file_" + std::to_string(i);
auto store = cachePool->open(name.c_str(), O_CREAT | O_RDWR, 0644);
ASSERT_NE(nullptr, store);
store->do_pwritev2(buffer.iovec(), buffer.iovcnt(), 0, 0);
store->release();
}

// Wait for first batch stores to expire from ObjectCache
photon::thread_usleep(100 * 1000);

// Externally delete all cached files
std::string cmd = "rm -rf " + root + "*";
ASSERT_NE(-1, system(cmd.c_str()));

// Phase 2: keep writing — totalUsed_ will exceed riskMark (~950MB),
// updateSpace triggers eviction which tries to open the deleted files.
// Must not crash.
for (int i = 6; i < 12; i++) {
std::string name = "/file_" + std::to_string(i);
auto store = cachePool->open(name.c_str(), O_CREAT | O_RDWR, 0644);
ASSERT_NE(nullptr, store);
store->do_pwritev2(buffer.iovec(), buffer.iovcnt(), 0, 0);
store->release();
}
}

}
}
int main(int argc, char** argv) {
Expand Down
Loading