Skip to content

Commit

Permalink
[bugfix] full file cache on XFS
Browse files Browse the repository at this point in the history
Signed-off-by: zhuangbowei.zbw <zhuangbowei.zbw@alibaba-inc.com>
  • Loading branch information
WaberZhuang committed Dec 4, 2023
1 parent d272e1c commit f0e58df
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/overlaybd/cache/full_file_cache/cache_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ void FileCachePool::eviction() {

isFull_ = true;

auto full_file_truncate = [mediaFs_ = mediaFs_](const string_key &fileName) -> int {
int err;
struct stat st;
err = mediaFs_->stat(fileName.data(), &st);
if (err) {
return err;
}
err = mediaFs_->truncate(fileName.data(), 0);
if (err) {
return err;
}
err = mediaFs_->truncate(fileName.data(), st.st_size);
return err;
};

while (actualEvict > 0 && !lru_.empty() && !exit_) {
auto fileIter = lru_.back();
const auto &fileName = fileIter->first;
Expand All @@ -231,7 +246,7 @@ void FileCachePool::eviction() {

{
photon::scoped_rwlock rl(lruEntry->rw_lock_, photon::WLOCK);
err = mediaFs_->truncate(fileName.data(), 0);
err = full_file_truncate(fileName);
}

if (err) {
Expand Down
19 changes: 19 additions & 0 deletions src/overlaybd/cache/full_file_cache/cache_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,26 @@ ssize_t FileCacheStore::do_pwritev2(const struct iovec *iov, int iovcnt, off_t o
return ret;
}

int FileCacheStore::truncate_once() {
if (truncate_done) {
return 0;
}
photon::scoped_lock lock(truncate_mutex);
if (truncate_done) {
return 0;
}
auto ret = localFile_->ftruncate(cached_size_);
if (ret) {
LOG_ERRNO_RETURN(0, -1, "truncate media file failed: ", VALUE(ret));
}
truncate_done = true;
return 0;
}

std::pair<off_t, size_t> FileCacheStore::queryRefillRange(off_t offset, size_t size) {
if (truncate_once() != 0) {
LOG_ERRNO_RETURN(0, std::make_pair(-1, 0), "truncate failed");
}
ScopedRangeLock lock(rangeLock_, offset, size);
off_t alignLeft = align_down(offset, kBlockSize);
off_t alignRight = align_up(offset + size, kBlockSize);
Expand Down
5 changes: 5 additions & 0 deletions src/overlaybd/cache/full_file_cache/cache_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class FileCacheStore : public FileSystem::ICacheStore {
RangeLock rangeLock_;

ssize_t do_pwritev(const struct iovec *iov, int iovcnt, off_t offset);

private:
bool truncate_done = false;
photon::mutex truncate_mutex;
int truncate_once();
};

} // namespace Cache

0 comments on commit f0e58df

Please sign in to comment.