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 7, 2023
1 parent d272e1c commit dfe3574
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/overlaybd/cache/full_file_cache/cache_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ void FileCachePool::eviction() {
{
photon::scoped_rwlock rl(lruEntry->rw_lock_, photon::WLOCK);
err = mediaFs_->truncate(fileName.data(), 0);
lruEntry->truncate_done = false;
}

if (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/overlaybd/cache/full_file_cache/cache_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ class FileCachePool : public FileSystem::ICachePool {

struct LruEntry {
LruEntry(uint32_t lruIt, int openCnt, uint64_t fileSize)
: lruIter(lruIt), openCount(openCnt), size(fileSize) {
: lruIter(lruIt), openCount(openCnt), size(fileSize), truncate_done(false) {
}
~LruEntry() = default;
uint32_t lruIter;
int openCount;
uint64_t size;
photon::rwlock rw_lock_;
bool truncate_done;
};

// Normally, fileIndex(std::map) always keep growing, so its iterators always
Expand Down
17 changes: 17 additions & 0 deletions src/overlaybd/cache/full_file_cache/cache_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ FileCacheStore::~FileCacheStore() {
cachePool_->removeOpenFile(iterator_);
}

ICacheStore::try_preadv_result FileCacheStore::try_preadv2(const struct iovec *iov, int iovcnt,
off_t offset, int flags) {
auto lruEntry = static_cast<FileCachePool::LruEntry *>(iterator_->second.get());
photon::scoped_rwlock rl(lruEntry->rw_lock_, photon::RLOCK);
return this->ICacheStore::try_preadv2(iov, iovcnt, offset, flags);
}

ssize_t FileCacheStore::do_preadv2(const struct iovec *iov, int iovcnt, off_t offset, int flags) {
// TODO(suoshi.yf): maybe a new interface for updating lru is better for avoiding
// multiple cacheStore preadvs but cacheFile preadv only once
Expand All @@ -60,6 +67,16 @@ ssize_t FileCacheStore::do_preadv2(const struct iovec *iov, int iovcnt, off_t of
ssize_t FileCacheStore::do_pwritev(const struct iovec *iov, int iovcnt, off_t offset) {
ssize_t ret;
iovector_view view((iovec *)iov, iovcnt);
auto lruEntry = static_cast<FileCachePool::LruEntry *>(iterator_->second.get());
photon::scoped_rwlock rl(lruEntry->rw_lock_, photon::RLOCK);
if (!lruEntry->truncate_done) {
// May repeated ftruncate() here, but it doesn't matter
ret = localFile_->ftruncate(actual_size_);
if (ret) {
LOG_ERRNO_RETURN(0, -1, "failed to truncate media file: ", VALUE(ret));
}
lruEntry->truncate_done = true;
}
ScopedRangeLock lock(rangeLock_, offset, view.sum());
SCOPE_AUDIT_THRESHOLD(10UL * 1000, "file:write", AU_FILEOP("", offset, ret));
ret = localFile_->pwritev(iov, iovcnt, offset);
Expand Down
2 changes: 2 additions & 0 deletions src/overlaybd/cache/full_file_cache/cache_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class FileCacheStore : public FileSystem::ICacheStore {
size_t refillUnit, FileIterator iterator);
~FileCacheStore();

try_preadv_result try_preadv2(const struct iovec *iov, int iovcnt, off_t offset, int flags) override;

ssize_t do_preadv2(const struct iovec *iov, int iovcnt, off_t offset, int flags) override;

ssize_t do_pwritev2(const struct iovec *iov, int iovcnt, off_t offset, int flags) override;
Expand Down

0 comments on commit dfe3574

Please sign in to comment.