From e1af3f397a67a9a18cdbe01eeae8451f3deb90c2 Mon Sep 17 00:00:00 2001 From: baijiaruo Date: Mon, 14 Mar 2022 19:03:03 +0800 Subject: [PATCH] curvefs/client: fix the problem that chunkCacheManager has been released in advance during the Release process of dataCache --- curvefs/src/client/s3/client_s3_cache_manager.cpp | 11 +++++------ curvefs/src/client/s3/client_s3_cache_manager.h | 7 ++++--- curvefs/test/client/fs_cache_manager_test.cpp | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/curvefs/src/client/s3/client_s3_cache_manager.cpp b/curvefs/src/client/s3/client_s3_cache_manager.cpp index 95e8c27284..a7d7b7c124 100644 --- a/curvefs/src/client/s3/client_s3_cache_manager.cpp +++ b/curvefs/src/client/s3/client_s3_cache_manager.cpp @@ -527,7 +527,7 @@ int FileCacheManager::ReadFromS3(const std::vector &requests, << ", len: " << (*responses)[i].GetBufLen(); } DataCachePtr dataCache = std::make_shared( - s3ClientAdaptor_, chunkCacheManager.get(), chunkPos, + s3ClientAdaptor_, chunkCacheManager, chunkPos, (*responses)[i].GetBufLen(), (*responses)[i].GetDataBuf()); chunkCacheManager->AddReadDataCache(dataCache); i++; @@ -1212,11 +1212,10 @@ DataCachePtr ChunkCacheManager::FindWriteableDataCache( void ChunkCacheManager::WriteNewDataCache(S3ClientAdaptorImpl *s3ClientAdaptor, uint32_t chunkPos, uint32_t len, const char *data) { - DataCachePtr dataCache = - std::make_shared(s3ClientAdaptor, this, chunkPos, len, data); + DataCachePtr dataCache = std::make_shared( + s3ClientAdaptor, this->shared_from_this(), chunkPos, len, data); VLOG(9) << "WriteNewDataCache chunkPos:" << chunkPos << ", len:" << len - << ", new len:" << dataCache->GetLen() - << ",chunkIndex:" << index_; + << ", new len:" << dataCache->GetLen() << ",chunkIndex:" << index_; WriteLockGuard writeLockGuard(rwLockWrite_); auto ret = dataWCacheMap_.emplace(chunkPos, dataCache); @@ -1426,7 +1425,7 @@ void ChunkCacheManager::UpdateWriteCacheMap(uint64_t oldChunkPos, } DataCache::DataCache(S3ClientAdaptorImpl *s3ClientAdaptor, - ChunkCacheManager *chunkCacheManager, uint64_t chunkPos, + ChunkCacheManagerPtr chunkCacheManager, uint64_t chunkPos, uint64_t len, const char *data) : s3ClientAdaptor_(s3ClientAdaptor), chunkCacheManager_(chunkCacheManager), dirty_(true), delete_(false), inReadCache_(false) { diff --git a/curvefs/src/client/s3/client_s3_cache_manager.h b/curvefs/src/client/s3/client_s3_cache_manager.h index 9c8dbc1b00..9d61749b08 100644 --- a/curvefs/src/client/s3/client_s3_cache_manager.h +++ b/curvefs/src/client/s3/client_s3_cache_manager.h @@ -98,7 +98,7 @@ using PageDataMap = std::map; class DataCache : public std::enable_shared_from_this { public: DataCache(S3ClientAdaptorImpl *s3ClientAdaptor, - ChunkCacheManager *chunkCacheManager, uint64_t chunkPos, + ChunkCacheManagerPtr chunkCacheManager, uint64_t chunkPos, uint64_t len, const char *data); virtual ~DataCache() { auto iter = dataMap_.begin(); @@ -171,7 +171,7 @@ class DataCache : public std::enable_shared_from_this { private: S3ClientAdaptorImpl *s3ClientAdaptor_; - ChunkCacheManager* chunkCacheManager_; + ChunkCacheManagerPtr chunkCacheManager_; uint64_t chunkPos_; // useful chunkPos uint64_t len_; // useful len uint64_t actualChunkPos_; // after alignment the actual chunkPos @@ -203,7 +203,8 @@ class S3ReadResponse { uint64_t len_; }; -class ChunkCacheManager { +class ChunkCacheManager + : public std::enable_shared_from_this { public: ChunkCacheManager(uint64_t index, S3ClientAdaptorImpl *s3ClientAdaptor) : index_(index), s3ClientAdaptor_(s3ClientAdaptor) {} diff --git a/curvefs/test/client/fs_cache_manager_test.cpp b/curvefs/test/client/fs_cache_manager_test.cpp index d18ffe801d..c46317ef14 100644 --- a/curvefs/test/client/fs_cache_manager_test.cpp +++ b/curvefs/test/client/fs_cache_manager_test.cpp @@ -59,7 +59,7 @@ TEST(FsCacheManagerTest, test_read_lru_cache_size) { for (size_t i = 0; i < maxReadCacheByte / smallDataCacheByte; ++i) { manager.Set(std::make_shared(s3ClientAdaptor_, - mockCacheMgr.get(), 0, + mockCacheMgr, 0, smallDataCacheByte, buf), &outIter); } @@ -73,7 +73,7 @@ TEST(FsCacheManagerTest, test_read_lru_cache_size) { .Times(expectCallTimes) .WillRepeatedly(Invoke([&counter](uint64_t) { counter.Signal(); })); manager.Set(std::make_shared(s3ClientAdaptor_, - mockCacheMgr.get(), 0, + mockCacheMgr, 0, dataCacheByte, buf), &outIter); @@ -89,7 +89,7 @@ TEST(FsCacheManagerTest, test_read_lru_cache_size) { .WillRepeatedly(Invoke([&counter](uint64_t) { counter.Signal(); })); manager.Set(std::make_shared(s3ClientAdaptor_, - mockCacheMgr.get(), 0, + mockCacheMgr, 0, dataCacheByte, buf), &outIter); counter.Wait();