From cfcc91297caa6aff4d7709ef33d37e12f36688d9 Mon Sep 17 00:00:00 2001 From: Hanqing Wu Date: Wed, 19 May 2021 12:27:34 +0800 Subject: [PATCH] client: fix segment fault in SourceReader After #172 and #209, source volume data of the clone volume can be read within the client through SourceReader, and each source volume was also opened and represented by a FileInstance, but `FileInstance::mdsclient_` points to `FileClient::mdsClient_`, so after FileClient is destroyed, `FileInstance::mdsclient_` becomes dangling pointer. To fix this problem, we let `FileClient::mdsClient_` be a shared_ptr, and each FileInstance holds ownership of it. Signed-off-by: Hanqing Wu --- src/client/client_metric.h | 4 +- src/client/file_instance.cpp | 34 +++--- src/client/file_instance.h | 8 +- src/client/libcurve_file.cpp | 37 +++--- src/client/libcurve_file.h | 28 +---- src/client/mds_client.cpp | 100 +++++++-------- src/client/mds_client.h | 29 +---- src/client/mds_client_base.cpp | 18 ++- src/client/mds_client_base.h | 15 +-- src/client/source_reader.cpp | 4 +- src/common/string_util.h | 6 + .../client_mdsclient_metacache_unittest.cpp | 1 - test/client/client_metric_test.cpp | 14 +-- test/client/client_session_unittest.cpp | 6 +- test/client/client_userinfo_unittest.cpp | 11 +- test/client/copyset_client_test.cpp | 6 +- test/client/file_instance_test.cpp | 17 ++- test/client/iotracker_splitor_unittest.cpp | 115 ++++++++---------- test/client/libcurve_interface_unittest.cpp | 14 +-- 19 files changed, 188 insertions(+), 279 deletions(-) diff --git a/src/client/client_metric.h b/src/client/client_metric.h index 876f83fd33..88508df5ab 100644 --- a/src/client/client_metric.h +++ b/src/client/client_metric.h @@ -30,6 +30,7 @@ #include "src/common/timeutility.h" #include "src/client/client_common.h" +#include "src/common/string_util.h" using curve::common::TimeUtility; @@ -200,8 +201,7 @@ struct MDSClientMetric { explicit MDSClientMetric(const std::string& prefix_ = "") : prefix(!prefix_.empty() ? prefix_ - : "curve_mds_client_" + - std::to_string(reinterpret_cast(this))), + : "curve_mds_client_" + common::ToHexString(this)), metaserverAddress(prefix, "current_metaserver_addr", GetStringValue, &metaserverAddr), openFile(prefix, "openFile"), diff --git a/src/client/file_instance.cpp b/src/client/file_instance.cpp index 3511e7744b..18c09c383c 100644 --- a/src/client/file_instance.cpp +++ b/src/client/file_instance.cpp @@ -24,6 +24,7 @@ #include #include +#include #include "proto/nameserver2.pb.h" #include "proto/topology.pb.h" @@ -50,7 +51,7 @@ FileInstance::FileInstance() readonly_(false) {} bool FileInstance::Initialize(const std::string& filename, - MDSClient* mdsclient, + std::shared_ptr mdsclient, const UserInfo_t& userinfo, const FileServiceOption& fileservicopt, bool readonly) { @@ -69,11 +70,12 @@ bool FileInstance::Initialize(const std::string& filename, } finfo_.userinfo = userinfo; - mdsclient_ = mdsclient; + mdsclient_ = std::move(mdsclient); finfo_.fullPathName = filename; - if (!iomanager4file_.Initialize(filename, fileopt_.ioOpt, mdsclient_)) { + if (!iomanager4file_.Initialize(filename, fileopt_.ioOpt, + mdsclient_.get())) { LOG(ERROR) << "Init io context manager failed, filename = " << filename; break; @@ -82,7 +84,8 @@ bool FileInstance::Initialize(const std::string& filename, iomanager4file_.UpdateFileInfo(finfo_); leaseExecutor_.reset(new (std::nothrow) LeaseExecutor( - fileopt_.leaseOpt, finfo_.userinfo, mdsclient_, &iomanager4file_)); + fileopt_.leaseOpt, finfo_.userinfo, mdsclient_.get(), + &iomanager4file_)); if (CURVE_UNLIKELY(leaseExecutor_ == nullptr)) { LOG(ERROR) << "Allocate LeaseExecutor failed, filename = " << filename; @@ -97,10 +100,13 @@ bool FileInstance::Initialize(const std::string& filename, void FileInstance::UnInitialize() { iomanager4file_.UnInitialize(); + + // release the ownership of mdsclient + mdsclient_.reset(); } int FileInstance::Read(char* buf, off_t offset, size_t length) { - return iomanager4file_.Read(buf, offset, length, mdsclient_); + return iomanager4file_.Read(buf, offset, length, mdsclient_.get()); } int FileInstance::Write(const char* buf, off_t offset, size_t len) { @@ -108,11 +114,11 @@ int FileInstance::Write(const char* buf, off_t offset, size_t len) { DVLOG(9) << "open with read only, do not support write!"; return -1; } - return iomanager4file_.Write(buf, offset, len, mdsclient_); + return iomanager4file_.Write(buf, offset, len, mdsclient_.get()); } int FileInstance::AioRead(CurveAioContext* aioctx, UserDataType dataType) { - return iomanager4file_.AioRead(aioctx, mdsclient_, dataType); + return iomanager4file_.AioRead(aioctx, mdsclient_.get(), dataType); } int FileInstance::AioWrite(CurveAioContext* aioctx, UserDataType dataType) { @@ -120,12 +126,12 @@ int FileInstance::AioWrite(CurveAioContext* aioctx, UserDataType dataType) { DVLOG(9) << "open with read only, do not support write!"; return -1; } - return iomanager4file_.AioWrite(aioctx, mdsclient_, dataType); + return iomanager4file_.AioWrite(aioctx, mdsclient_.get(), dataType); } int FileInstance::Discard(off_t offset, size_t length) { if (!readonly_) { - return iomanager4file_.Discard(offset, length, mdsclient_); + return iomanager4file_.Discard(offset, length, mdsclient_.get()); } LOG(ERROR) << "Open with read only, not support Discard"; @@ -134,7 +140,7 @@ int FileInstance::Discard(off_t offset, size_t length) { int FileInstance::AioDiscard(CurveAioContext* aioctx) { if (!readonly_) { - return iomanager4file_.AioDiscard(aioctx, mdsclient_); + return iomanager4file_.AioDiscard(aioctx, mdsclient_.get()); } LOG(ERROR) << "Open with read only, not support AioDiscard"; @@ -197,7 +203,7 @@ int FileInstance::Close() { FileInstance* FileInstance::NewInitedFileInstance( const FileServiceOption& fileServiceOption, - MDSClient* mdsClient, + std::shared_ptr mdsClient, const std::string& filename, const UserInfo& userInfo, bool readonly) { @@ -207,7 +213,7 @@ FileInstance* FileInstance::NewInitedFileInstance( return nullptr; } - bool ret = instance->Initialize(filename, mdsClient, userInfo, + bool ret = instance->Initialize(filename, std::move(mdsClient), userInfo, fileServiceOption, readonly); if (!ret) { LOG(ERROR) << "FileInstance initialize failed" @@ -222,11 +228,11 @@ FileInstance* FileInstance::NewInitedFileInstance( } FileInstance* FileInstance::Open4Readonly(const FileServiceOption& opt, - MDSClient* mdsclient, + std::shared_ptr mdsclient, const std::string& filename, const UserInfo& userInfo) { FileInstance* instance = FileInstance::NewInitedFileInstance( - opt, mdsclient, filename, userInfo, true); + opt, std::move(mdsclient), filename, userInfo, true); if (instance == nullptr) { LOG(ERROR) << "NewInitedFileInstance failed, filename = " << filename; return nullptr; diff --git a/src/client/file_instance.h b/src/client/file_instance.h index a4c0def626..6c6b77b326 100644 --- a/src/client/file_instance.h +++ b/src/client/file_instance.h @@ -52,7 +52,7 @@ class CURVE_CACHELINE_ALIGNMENT FileInstance { * @return: 成功返回true、否则返回false */ bool Initialize(const std::string& filename, - MDSClient* mdsclient, + std::shared_ptr mdsclient, const UserInfo_t& userinfo, const FileServiceOption& fileservicopt, bool readonly = false); @@ -152,13 +152,13 @@ class CURVE_CACHELINE_ALIGNMENT FileInstance { static FileInstance* NewInitedFileInstance( const FileServiceOption& fileServiceOption, - MDSClient* mdsClient, + std::shared_ptr mdsClient, const std::string& filename, const UserInfo& userInfo, bool readonly); static FileInstance* Open4Readonly(const FileServiceOption& opt, - MDSClient* mdsclient, + std::shared_ptr mdsclient, const std::string& filename, const UserInfo& userInfo); @@ -170,7 +170,7 @@ class CURVE_CACHELINE_ALIGNMENT FileInstance { FileServiceOption fileopt_; // MDSClient是FileInstance与mds通信的唯一出口 - MDSClient* mdsclient_; + std::shared_ptr mdsclient_; // 每个文件都持有与MDS通信的lease,LeaseExecutor是续约执行者 std::unique_ptr leaseExecutor_; diff --git a/src/client/libcurve_file.cpp b/src/client/libcurve_file.cpp index a364343909..5dc762ddc7 100644 --- a/src/client/libcurve_file.cpp +++ b/src/client/libcurve_file.cpp @@ -29,6 +29,7 @@ #include #include // NOLINT #include // NOLINT +#include #include "include/client/libcurve.h" #include "include/curve_compiler_specific.h" @@ -42,6 +43,7 @@ #include "src/common/curve_version.h" #include "src/common/net_common.h" #include "src/common/uuid.h" +#include "src/common/string_util.h" #define PORT_LIMIT 65535 @@ -96,11 +98,14 @@ void InitLogging(const std::string& confPath) { static LoggerGuard guard(confPath); } -FileClient::FileClient(): fdcount_(0), openedFileNum_("opened_file_num") { - inited_ = false; - mdsClient_ = nullptr; - fileserviceMap_.clear(); -} +FileClient::FileClient() + : rwlock_(), + fdcount_(0), + fileserviceMap_(), + clientconfig_(), + mdsClient_(), + inited_(false), + openedFileNum_(common::ToHexString(this)) {} int FileClient::Init(const std::string& configpath) { if (inited_) { @@ -116,17 +121,13 @@ int FileClient::Init(const std::string& configpath) { return -LIBCURVE_ERROR::FAILED; } - mdsClient_ = new (std::nothrow) MDSClient(); - if (mdsClient_ == nullptr) { - return -LIBCURVE_ERROR::FAILED; - } - auto ret = mdsClient_->Initialize(clientconfig_. - GetFileServiceOption().metaServerOpt); + auto tmpMdsClient = std::make_shared(); + + auto ret = tmpMdsClient->Initialize( + clientconfig_.GetFileServiceOption().metaServerOpt); if (LIBCURVE_ERROR::OK != ret) { LOG(ERROR) << "Init global mds client failed!"; - delete mdsClient_; - mdsClient_ = nullptr; return -LIBCURVE_ERROR::FAILED; } @@ -139,12 +140,10 @@ int FileClient::Init(const std::string& configpath) { bool rc = StartDummyServer(); if (rc == false) { - mdsClient_->UnInitialize(); - delete mdsClient_; - mdsClient_ = nullptr; return -LIBCURVE_ERROR::FAILED; } + mdsClient_ = std::move(tmpMdsClient); inited_ = true; return LIBCURVE_ERROR::OK; } @@ -161,11 +160,7 @@ void FileClient::UnInit() { } fileserviceMap_.clear(); - if (mdsClient_ != nullptr) { - mdsClient_->UnInitialize(); - delete mdsClient_; - mdsClient_ = nullptr; - } + mdsClient_.reset(); inited_ = false; } diff --git a/src/client/libcurve_file.h b/src/client/libcurve_file.h index e6321e2287..bb835cae60 100644 --- a/src/client/libcurve_file.h +++ b/src/client/libcurve_file.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "include/client/libcurve.h" #include "src/client/client_common.h" @@ -314,31 +315,6 @@ class FileClient { return openedFileNum_.get_value(); } - /** - * test use, set the mdsclient_ - */ - void SetMdsClient(MDSClient* client) { - mdsClient_ = client; - } - - /** - * test use, set the clientconfig_ - */ - void SetClientConfig(ClientConfig cfg) { - clientconfig_ = cfg; - } - - const ClientConfig& GetClientConfig() { - return clientconfig_; - } - - /** - * test use, get the fileserviceMap_ - */ - std::unordered_map& GetFileServiceMap() { - return fileserviceMap_; - } - private: bool StartDummyServer(); @@ -360,7 +336,7 @@ class FileClient { ClientConfig clientconfig_; // fileclient对应的全局mdsclient - MDSClient* mdsClient_; + std::shared_ptr mdsClient_; // 是否初始化成功 bool inited_; diff --git a/src/client/mds_client.cpp b/src/client/mds_client.cpp index bf57f55a24..ce9c253747 100644 --- a/src/client/mds_client.cpp +++ b/src/client/mds_client.cpp @@ -22,12 +22,12 @@ #include "src/client/mds_client.h" #include +#include #include #include #include "src/client/lease_executor.h" -#include "src/client/metacache.h" #include "src/common/net_common.h" #include "src/common/string_util.h" #include "src/common/timeutility.h" @@ -37,40 +37,22 @@ namespace client { using curve::common::NetCommon; using curve::common::TimeUtility; -using curve::common::ReadLockGuard; -using curve::common::WriteLockGuard; -using curve::mds::StatusCode; using curve::mds::FileInfo; using curve::mds::PageFileChunkInfo; using curve::mds::PageFileSegment; -using curve::mds::CreateFileResponse; -using curve::mds::DeleteFileResponse; -using curve::mds::RecoverFileResponse; -using curve::mds::GetFileInfoResponse; -using curve::mds::GetOrAllocateSegmentResponse; -using curve::mds::DeAllocateSegmentResponse; -using curve::mds::RenameFileResponse; -using curve::mds::ExtendFileResponse; -using curve::mds::ChangeOwnerResponse; -using curve::mds::ListDirResponse; -using curve::mds::CreateSnapShotResponse; -using curve::mds::ListSnapShotFileInfoResponse; -using curve::mds::DeleteSnapShotResponse; -using curve::mds::CheckSnapShotStatusResponse; -using curve::mds::SessionStatus; using curve::mds::ProtoSession; -using curve::mds::OpenFileResponse; -using curve::mds::CloseFileResponse; -using curve::mds::ReFreshSessionResponse; -using curve::mds::CreateCloneFileResponse; -using curve::mds::SetCloneFileStatusResponse; -using curve::mds::topology::CopySetServerInfo; +using curve::mds::StatusCode; using curve::mds::topology::ChunkServerLocation; -using curve::mds::topology::GetChunkServerListInCopySetsResponse; +using curve::mds::topology::CopySetServerInfo; MDSClient::MDSClient(const std::string& metricPrefix) - : mdsClientMetric_(metricPrefix) { - inited_ = false; + : inited_(false), + metaServerOpt_(), + mdsClientMetric_(metricPrefix), + rpcExcutor() {} + +MDSClient::~MDSClient() { + UnInitialize(); } LIBCURVE_ERROR MDSClient::Initialize(const MetaServerOption& metaServerOpt) { @@ -81,20 +63,22 @@ LIBCURVE_ERROR MDSClient::Initialize(const MetaServerOption& metaServerOpt) { metaServerOpt_ = metaServerOpt; - int rc = mdsClientBase_.Init(metaServerOpt_); - if (rc != 0) { - LOG(ERROR) << "mds client rpc base init failed!"; - return LIBCURVE_ERROR::FAILED; - } - rpcExcutor.SetOption(metaServerOpt); + std::ostringstream oss; + for (const auto& addr : metaServerOpt_.mdsAddrs) { + oss << " " << addr; + } + + LOG(INFO) << "MDSClient init success, addresses:" << oss.str(); inited_ = true; return LIBCURVE_ERROR::OK; } void MDSClient::UnInitialize() { inited_ = false; + + LOG(INFO) << "MDSClient uninit success"; } // rpc发送和mds地址切换状态机 @@ -275,7 +259,7 @@ LIBCURVE_ERROR MDSClient::OpenFile(const std::string& filename, OpenFileResponse response; mdsClientMetric_.openFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.openFile.latency); - mdsClientBase_.OpenFile(filename, userinfo, &response, cntl, channel); + MDSClientBase::OpenFile(filename, userinfo, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.openFile.eps.count << 1; @@ -339,7 +323,7 @@ LIBCURVE_ERROR MDSClient::CreateFile(const std::string& filename, CreateFileResponse response; mdsClientMetric_.createFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.createFile.latency); - mdsClientBase_.CreateFile(filename, userinfo, size, normalFile, + MDSClientBase::CreateFile(filename, userinfo, size, normalFile, stripeUnit, stripeCount, &response, cntl, channel); if (cntl->Failed()) { @@ -372,7 +356,7 @@ LIBCURVE_ERROR MDSClient::CloseFile(const std::string& filename, CloseFileResponse response; mdsClientMetric_.closeFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.closeFile.latency); - mdsClientBase_.CloseFile(filename, userinfo, sessionid, + MDSClientBase::CloseFile(filename, userinfo, sessionid, &response, cntl, channel); if (cntl->Failed()) { @@ -405,7 +389,7 @@ LIBCURVE_ERROR MDSClient::GetFileInfo(const std::string& filename, GetFileInfoResponse response; mdsClientMetric_.getFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.getFile.latency); - mdsClientBase_.GetFileInfo(filename, uinfo, &response, cntl, channel); + MDSClientBase::GetFileInfo(filename, uinfo, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.getFile.eps.count << 1; @@ -435,7 +419,7 @@ LIBCURVE_ERROR MDSClient::CreateSnapShot(const std::string& filename, uint64_t* seq) { auto task = RPCTaskDefine { CreateSnapShotResponse response; - mdsClientBase_.CreateSnapShot(filename, userinfo, + MDSClientBase::CreateSnapShot(filename, userinfo, &response, cntl, channel); if (cntl->Failed()) { @@ -489,7 +473,7 @@ LIBCURVE_ERROR MDSClient::DeleteSnapShot(const std::string& filename, uint64_t seq) { auto task = RPCTaskDefine { DeleteSnapShotResponse response; - mdsClientBase_.DeleteSnapShot(filename, userinfo, seq, + MDSClientBase::DeleteSnapShot(filename, userinfo, seq, &response, cntl, channel); if (cntl->Failed()) { @@ -519,7 +503,7 @@ LIBCURVE_ERROR MDSClient::ListSnapShot(const std::string& filename, std::map* snapif) { auto task = RPCTaskDefine { ListSnapShotFileInfoResponse response; - mdsClientBase_.ListSnapShot(filename, userinfo, seq, + MDSClientBase::ListSnapShot(filename, userinfo, seq, &response, cntl, channel); if (cntl->Failed()) { @@ -565,7 +549,7 @@ LIBCURVE_ERROR MDSClient::GetSnapshotSegmentInfo(const std::string& filename, SegmentInfo* segInfo) { auto task = RPCTaskDefine { GetOrAllocateSegmentResponse response; - mdsClientBase_.GetSnapshotSegmentInfo(filename, userinfo, seq, offset, + MDSClientBase::GetSnapshotSegmentInfo(filename, userinfo, seq, offset, &response, cntl, channel); if (cntl->Failed()) { LOG(WARNING) << "get snap file segment info failed, errcorde = " @@ -631,7 +615,7 @@ LIBCURVE_ERROR MDSClient::RefreshSession(const std::string& filename, ReFreshSessionResponse response; mdsClientMetric_.refreshSession.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.refreshSession.latency); - mdsClientBase_.RefreshSession(filename, userinfo, sessionid, + MDSClientBase::RefreshSession(filename, userinfo, sessionid, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.refreshSession.eps.count << 1; @@ -702,7 +686,7 @@ LIBCURVE_ERROR MDSClient::CheckSnapShotStatus(const std::string& filename, FileStatus* filestatus) { auto task = RPCTaskDefine { CheckSnapShotStatusResponse response; - mdsClientBase_.CheckSnapShotStatus(filename, userinfo, seq, + MDSClientBase::CheckSnapShotStatus(filename, userinfo, seq, &response, cntl, channel); if (cntl->Failed()) { @@ -738,7 +722,7 @@ LIBCURVE_ERROR MDSClient::GetServerList( GetChunkServerListInCopySetsResponse response; mdsClientMetric_.getServerList.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.getServerList.latency); - mdsClientBase_.GetServerList(logicalpooid, copysetidvec, &response, + MDSClientBase::GetServerList(logicalpooid, copysetidvec, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.getServerList.eps.count << 1; @@ -797,7 +781,7 @@ LIBCURVE_ERROR MDSClient::GetServerList( LIBCURVE_ERROR MDSClient::GetClusterInfo(ClusterContext* clsctx) { auto task = RPCTaskDefine { curve::mds::topology::GetClusterInfoResponse response; - mdsClientBase_.GetClusterInfo(&response, cntl, channel); + MDSClientBase::GetClusterInfo(&response, cntl, channel); if (cntl->Failed()) { LOG(WARNING) << "get cluster info from mds failed, status code = " @@ -825,7 +809,7 @@ LIBCURVE_ERROR MDSClient::CreateCloneFile(const std::string& source, FInfo* fileinfo) { auto task = RPCTaskDefine { CreateCloneFileResponse response; - mdsClientBase_.CreateCloneFile(source, destination, userinfo, size, sn, + MDSClientBase::CreateCloneFile(source, destination, userinfo, size, sn, chunksize, stripeUnit, stripeCount, &response, cntl, channel); if (cntl->Failed()) { @@ -877,7 +861,7 @@ LIBCURVE_ERROR MDSClient::SetCloneFileStatus(const std::string& filename, uint64_t fileID) { auto task = RPCTaskDefine { SetCloneFileStatusResponse response; - mdsClientBase_.SetCloneFileStatus(filename, filestatus, userinfo, + MDSClientBase::SetCloneFileStatus(filename, filestatus, userinfo, fileID, &response, cntl, channel); if (cntl->Failed()) { LOG(WARNING) << "SetCloneFileStatus invoke failed, errcorde = " @@ -910,7 +894,7 @@ LIBCURVE_ERROR MDSClient::GetOrAllocateSegment(bool allocate, GetOrAllocateSegmentResponse response; mdsClientMetric_.getOrAllocateSegment.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.getOrAllocateSegment.latency); - mdsClientBase_.GetOrAllocateSegment(allocate, offset, fi, + MDSClientBase::GetOrAllocateSegment(allocate, offset, fi, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.getOrAllocateSegment.eps.count << 1; @@ -970,7 +954,7 @@ LIBCURVE_ERROR MDSClient::DeAllocateSegment(const FInfo* fileInfo, DeAllocateSegmentResponse response; mdsClientMetric_.deAllocateSegment.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.deAllocateSegment.latency); - mdsClientBase_.DeAllocateSegment(fileInfo, offset, &response, cntl, + MDSClientBase::DeAllocateSegment(fileInfo, offset, &response, cntl, channel); if (cntl->Failed()) { @@ -1009,7 +993,7 @@ LIBCURVE_ERROR MDSClient::RenameFile(const UserInfo_t& userinfo, RenameFileResponse response; mdsClientMetric_.renameFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.renameFile.latency); - mdsClientBase_.RenameFile(userinfo, origin, destination, originId, + MDSClientBase::RenameFile(userinfo, origin, destination, originId, destinationId, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.renameFile.eps.count << 1; @@ -1050,7 +1034,7 @@ LIBCURVE_ERROR MDSClient::Extend(const std::string& filename, ExtendFileResponse response; mdsClientMetric_.extendFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.extendFile.latency); - mdsClientBase_.Extend(filename, userinfo, newsize, &response, + MDSClientBase::Extend(filename, userinfo, newsize, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.extendFile.eps.count << 1; @@ -1085,7 +1069,7 @@ LIBCURVE_ERROR MDSClient::DeleteFile(const std::string& filename, DeleteFileResponse response; mdsClientMetric_.deleteFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.deleteFile.latency); - mdsClientBase_.DeleteFile(filename, userinfo, deleteforce, + MDSClientBase::DeleteFile(filename, userinfo, deleteforce, fileid, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.deleteFile.eps.count << 1; @@ -1122,7 +1106,7 @@ LIBCURVE_ERROR MDSClient::RecoverFile(const std::string& filename, RecoverFileResponse response; mdsClientMetric_.recoverFile.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.recoverFile.latency); - mdsClientBase_.RecoverFile(filename, userinfo, fileid, + MDSClientBase::RecoverFile(filename, userinfo, fileid, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.recoverFile.eps.count << 1; @@ -1153,7 +1137,7 @@ LIBCURVE_ERROR MDSClient::ChangeOwner(const std::string& filename, ChangeOwnerResponse response; mdsClientMetric_.changeOwner.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.changeOwner.latency); - mdsClientBase_.ChangeOwner(filename, newOwner, userinfo, + MDSClientBase::ChangeOwner(filename, newOwner, userinfo, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.changeOwner.eps.count << 1; @@ -1191,7 +1175,7 @@ LIBCURVE_ERROR MDSClient::Listdir(const std::string& dirpath, ListDirResponse response; mdsClientMetric_.listDir.qps.count << 1; LatencyGuard lg(&mdsClientMetric_.listDir.latency); - mdsClientBase_.Listdir(dirpath, userinfo, &response, cntl, channel); + MDSClientBase::Listdir(dirpath, userinfo, &response, cntl, channel); if (cntl->Failed()) { mdsClientMetric_.listDir.eps.count << 1; @@ -1259,7 +1243,7 @@ LIBCURVE_ERROR MDSClient::GetChunkServerInfo(const ChunkServerAddr& csAddr, const std::string& ip = strs[0]; uint64_t port; curve::common::StringToUll(strs[1], &port); - mdsClientBase_.GetChunkServerInfo(ip, port, &response, cntl, channel); + MDSClientBase::GetChunkServerInfo(ip, port, &response, cntl, channel); if (cntl->Failed()) { LOG(WARNING) << "GetChunkServerInfo invoke failed, errcorde = " @@ -1304,7 +1288,7 @@ LIBCURVE_ERROR MDSClient::ListChunkServerInServer( mdsClientMetric_.listChunkserverInServer.qps.count << 1; LatencyGuard guard(&mdsClientMetric_.listChunkserverInServer.latency); - mdsClientBase_.ListChunkServerInServer( + MDSClientBase::ListChunkServerInServer( serverIp, &response, cntl, channel); if (cntl->Failed()) { diff --git a/src/client/mds_client.h b/src/client/mds_client.h index b60c4fdf1b..e6902ba20e 100644 --- a/src/client/mds_client.h +++ b/src/client/mds_client.h @@ -25,7 +25,6 @@ #include #include -#include #include #include @@ -39,25 +38,19 @@ #include "src/client/client_metric.h" #include "src/client/mds_client_base.h" #include "src/client/metacache_struct.h" -#include "src/common/concurrent/concurrent.h" -#include "src/common/concurrent/rw_lock.h" namespace curve { namespace client { -using curve::common::RWLock; -using curve::common::ReadLockGuard; -using curve::common::Authenticator; - -class MetaCache; struct LeaseRefreshResult; // MDSClient是client与MDS通信的唯一窗口 -class MDSClient { +class MDSClient : public MDSClientBase, + public std::enable_shared_from_this { public: explicit MDSClient(const std::string& metricPrefix = ""); - virtual ~MDSClient() = default; + virtual ~MDSClient(); using RPCFunc = std::function; @@ -407,18 +400,11 @@ class MDSClient { */ void UnInitialize(); - // 测试使用 - MDSClientMetric* GetMetric() { - return &mdsClientMetric_; - } - protected: class MDSRPCExcutor { public: - MDSRPCExcutor() { - cntlID_.store(1); - currentWorkingMDSAddrIndex_.store(0); - } + MDSRPCExcutor() + : metaServerOpt_(), currentWorkingMDSAddrIndex_(0), cntlID_(1) {} void SetOption(const MetaServerOption& option) { metaServerOpt_ = option; @@ -547,12 +533,9 @@ class MDSClient { // client与mds通信的metric统计 MDSClientMetric mdsClientMetric_; - // MDSClientBase是真正的rpc发送逻辑 - // MDSClient是在RPC上层的一些业务逻辑封装,比如metric,或者重试逻辑 - MDSClientBase mdsClientBase_; - MDSRPCExcutor rpcExcutor; }; + } // namespace client } // namespace curve diff --git a/src/client/mds_client_base.cpp b/src/client/mds_client_base.cpp index fe47c46520..80b44360b0 100644 --- a/src/client/mds_client_base.cpp +++ b/src/client/mds_client_base.cpp @@ -21,17 +21,16 @@ */ #include "src/client/mds_client_base.h" + +#include "src/common/authenticator.h" #include "src/common/curve_version.h" namespace curve { namespace client { -const char* kRootUserName = "root"; +using curve::common::Authenticator; -int MDSClientBase::Init(const MetaServerOption& metaServerOpt) { - metaServerOpt_ = metaServerOpt; - return 0; -} +const char* kRootUserName = "root"; void MDSClientBase::OpenFile(const std::string& filename, const UserInfo_t& userinfo, @@ -364,11 +363,10 @@ void MDSClientBase::GetOrAllocateSegment(bool allocate, request.set_allocateifnotexist(allocate); FillUserInfo(&request, fi->userinfo); - LOG(INFO) << "GetOrAllocateSegment: allocate = " << allocate - << ", owner = " << fi->owner - << ", offset = " << offset - << ", segment offset = " << seg_offset - << ", log id = " << cntl->log_id(); + LOG(INFO) << "GetOrAllocateSegment: filename = " << fi->fullPathName + << ", allocate = " << allocate << ", owner = " << fi->owner + << ", offset = " << offset << ", segment offset = " << seg_offset + << ", log id = " << cntl->log_id(); curve::mds::CurveFSService_Stub stub(channel); stub.GetOrAllocateSegment(cntl, &request, response, NULL); diff --git a/src/client/mds_client_base.h b/src/client/mds_client_base.h index 88584ff82b..44868036df 100644 --- a/src/client/mds_client_base.h +++ b/src/client/mds_client_base.h @@ -33,15 +33,11 @@ #include "proto/nameserver2.pb.h" #include "proto/topology.pb.h" #include "src/client/client_common.h" -#include "src/client/config_info.h" -#include "src/common/authenticator.h" #include "src/common/timeutility.h" namespace curve { namespace client { -using curve::common::Authenticator; - using curve::mds::OpenFileRequest; using curve::mds::OpenFileResponse; using curve::mds::CreateFileRequest; @@ -96,12 +92,6 @@ extern const char* kRootUserName; // 返回给调用者,有调用者处理 class MDSClientBase { public: - /** - * @param: metaServerOpt为mdsclient的配置信息 - * @return: 成功0, 否则-1 - */ - int Init(const MetaServerOption& metaServerOpt); - /** * 打开文件 * @param: filename是文件名 @@ -468,7 +458,7 @@ class MDSClientBase { * 为不同的request填充user信息 * @param: request是待填充的变量指针 */ - template + template void FillUserInfo(T* request, const UserInfo_t& userinfo) { uint64_t date = curve::common::TimeUtility::GetTimeofDayUs(); request->set_owner(userinfo.owner); @@ -482,9 +472,6 @@ class MDSClientBase { } std::string CalcSignature(const UserInfo& userinfo, uint64_t date) const; - - // 当前模块的初始化option配置 - MetaServerOption metaServerOpt_; }; } // namespace client diff --git a/src/client/source_reader.cpp b/src/client/source_reader.cpp index c67a23a02f..66af6a922d 100644 --- a/src/client/source_reader.cpp +++ b/src/client/source_reader.cpp @@ -122,8 +122,8 @@ SourceReader::ReadHandler* SourceReader::GetReadHandler( } } - FileInstance* instance = - FileInstance::Open4Readonly(fileOption_, mdsclient, fileName, userInfo); + FileInstance* instance = FileInstance::Open4Readonly( + fileOption_, mdsclient->shared_from_this(), fileName, userInfo); if (instance == nullptr) { return nullptr; } diff --git a/src/common/string_util.h b/src/common/string_util.h index 0462edcfc8..10f94f11cc 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -138,6 +138,12 @@ static bool StringToTime(const std::string& value, uint64_t* expireTime) { return true; } +inline std::string ToHexString(void* p) { + std::ostringstream oss; + oss << "0x" << std::hex << reinterpret_cast(p); + return oss.str(); +} + } // namespace common } // namespace curve diff --git a/test/client/client_mdsclient_metacache_unittest.cpp b/test/client/client_mdsclient_metacache_unittest.cpp index deadbdc54b..18d7d9fccf 100644 --- a/test/client/client_mdsclient_metacache_unittest.cpp +++ b/test/client/client_mdsclient_metacache_unittest.cpp @@ -123,7 +123,6 @@ class MDSClientTest : public ::testing::Test { } void TearDown() { - mdsclient_.UnInitialize(); UnInit(); ASSERT_EQ(0, server.Stop(0)); ASSERT_EQ(0, server.Join()); diff --git a/test/client/client_metric_test.cpp b/test/client/client_metric_test.cpp index 09247a2688..e488c38b50 100644 --- a/test/client/client_metric_test.cpp +++ b/test/client/client_metric_test.cpp @@ -68,8 +68,8 @@ TEST(MetricTest, ChunkServer_MetricTest) { metaopt.mdsRPCTimeoutMs = 500; metaopt.mdsRPCRetryIntervalUS = 200; - MDSClient mdsclient; - ASSERT_EQ(0, mdsclient.Initialize(metaopt)); + std::shared_ptr mdsclient = std::make_shared(); + ASSERT_EQ(0, mdsclient->Initialize(metaopt)); FLAGS_chunkserver_list = "127.0.0.1:9130:0,127.0.0.1:9131:0,127.0.0.1:9132:0"; // NOLINT @@ -101,7 +101,7 @@ TEST(MetricTest, ChunkServer_MetricTest) { auto opt = cc.GetFileServiceOption(); FileInstance fi; - ASSERT_TRUE(fi.Initialize(filename.c_str(), &mdsclient, userinfo, opt)); + ASSERT_TRUE(fi.Initialize(filename.c_str(), mdsclient, userinfo, opt)); FileMetric* fm = fi.GetIOManager4File()->GetMetric(); @@ -168,7 +168,6 @@ TEST(MetricTest, ChunkServer_MetricTest) { delete[] buffer; fi.UnInitialize(); mds.UnInitialize(); - mdsclient.UnInitialize(); } bool flag = false; @@ -186,8 +185,8 @@ TEST(MetricTest, SuspendRPC_MetricTest) { metaopt.mdsRPCTimeoutMs = 500; metaopt.mdsRPCRetryIntervalUS = 200; - MDSClient mdsclient; - ASSERT_EQ(0, mdsclient.Initialize(metaopt)); + std::shared_ptr mdsclient = std::make_shared(); + ASSERT_EQ(0, mdsclient->Initialize(metaopt)); FLAGS_chunkserver_list = "127.0.0.1:9130:0,127.0.0.1:9131:0,127.0.0.1:9132:0"; // NOLINT @@ -217,7 +216,7 @@ TEST(MetricTest, SuspendRPC_MetricTest) { ioSenderOpt.failRequestOpt.chunkserverMaxRPCTimeoutMS = 50; FileInstance fi; - ASSERT_TRUE(fi.Initialize(filename.c_str(), &mdsclient, userinfo, opt)); + ASSERT_TRUE(fi.Initialize(filename.c_str(), mdsclient, userinfo, opt)); FileMetric* fm = fi.GetIOManager4File()->GetMetric(); @@ -286,7 +285,6 @@ TEST(MetricTest, SuspendRPC_MetricTest) { delete[] buf1; fi.UnInitialize(); mds.UnInitialize(); - mdsclient.UnInitialize(); } TEST(MetricTest, MetricHelperTest) { diff --git a/test/client/client_session_unittest.cpp b/test/client/client_session_unittest.cpp index aa1590d1f8..4e0c96ec3a 100644 --- a/test/client/client_session_unittest.cpp +++ b/test/client/client_session_unittest.cpp @@ -94,9 +94,9 @@ TEST(ClientSession, LeaseTaskTest) { UserInfo_t userinfo; userinfo.owner = "userinfo"; - MDSClient mdsclient; - mdsclient.Initialize(cc.GetFileServiceOption().metaServerOpt); - ASSERT_TRUE(fileinstance.Initialize(filename, &mdsclient, userinfo, + std::shared_ptr mdsclient = std::make_shared(); + mdsclient->Initialize(cc.GetFileServiceOption().metaServerOpt); + ASSERT_TRUE(fileinstance.Initialize(filename, mdsclient, userinfo, cc.GetFileServiceOption())); brpc::Server server; diff --git a/test/client/client_userinfo_unittest.cpp b/test/client/client_userinfo_unittest.cpp index 8bc0306e4a..59c0ceaa36 100644 --- a/test/client/client_userinfo_unittest.cpp +++ b/test/client/client_userinfo_unittest.cpp @@ -94,13 +94,13 @@ TEST_F(CurveClientUserAuthFail, CurveClientUserAuthFailTest) { userinfo.owner = "userinfo"; UserInfo_t emptyuserinfo; - MDSClient mdsclient; - mdsclient.Initialize(cc.GetFileServiceOption().metaServerOpt); + std::shared_ptr mdsclient = std::make_shared(); + mdsclient->Initialize(cc.GetFileServiceOption().metaServerOpt); FileInstance fileinstance; - ASSERT_FALSE(fileinstance.Initialize(filename, &mdsclient, emptyuserinfo, - cc.GetFileServiceOption())); - ASSERT_TRUE(fileinstance.Initialize(filename, &mdsclient, userinfo, + ASSERT_FALSE(fileinstance.Initialize(filename, mdsclient, emptyuserinfo, + cc.GetFileServiceOption())); + ASSERT_TRUE(fileinstance.Initialize(filename, mdsclient, userinfo, cc.GetFileServiceOption())); // set openfile response @@ -209,7 +209,6 @@ TEST_F(CurveClientUserAuthFail, CurveClientUserAuthFailTest) { ASSERT_EQ(-LIBCURVE_ERROR::AUTHFAIL, fileinstance.Close()); fileinstance.UnInitialize(); - mdsclient.UnInitialize(); UnInit(); } diff --git a/test/client/copyset_client_test.cpp b/test/client/copyset_client_test.cpp index bd003de82f..307c3070c2 100644 --- a/test/client/copyset_client_test.cpp +++ b/test/client/copyset_client_test.cpp @@ -3552,10 +3552,10 @@ TEST(ChunkServerBackwardTest, ChunkServerBackwardTest) { UserInfo userinfo; userinfo.owner = "userinfo"; - MDSClient mdsclient; + std::shared_ptr mdsclient = std::make_shared(); ASSERT_EQ(LIBCURVE_ERROR::OK, - mdsclient.Initialize(cc.GetFileServiceOption().metaServerOpt)); - ASSERT_TRUE(fileinstance.Initialize("/test", &mdsclient, userinfo, + mdsclient->Initialize(cc.GetFileServiceOption().metaServerOpt)); + ASSERT_TRUE(fileinstance.Initialize("/test", mdsclient, userinfo, cc.GetFileServiceOption())); // create fake chunkserver service diff --git a/test/client/file_instance_test.cpp b/test/client/file_instance_test.cpp index eeaf2948e2..5476f6ce55 100644 --- a/test/client/file_instance_test.cpp +++ b/test/client/file_instance_test.cpp @@ -29,12 +29,12 @@ namespace client { TEST(FileInstanceTest, CommonTest) { UserInfo userInfo{"test", "passwd"}; - MDSClient mdsClient; + std::shared_ptr mdsclient = std::make_shared(); // user info invlaid FileInstance fi; - ASSERT_FALSE(fi.Initialize( - "/test", &mdsClient, UserInfo{}, FileServiceOption{})); + ASSERT_FALSE( + fi.Initialize("/test", mdsclient, UserInfo{}, FileServiceOption{})); // mdsclient is nullptr FileInstance fi2; @@ -47,13 +47,12 @@ TEST(FileInstanceTest, CommonTest) { opts.ioOpt.taskThreadOpt.isolationTaskQueueCapacity = 0; opts.ioOpt.taskThreadOpt.isolationTaskThreadPoolSize = 0; - ASSERT_FALSE(fi3.Initialize( - "/test", &mdsClient, userInfo, opts)); + ASSERT_FALSE(fi3.Initialize("/test", mdsclient, userInfo, opts)); // readonly FileInstance fi4; - ASSERT_TRUE(fi4.Initialize( - "/test", &mdsClient, userInfo, FileServiceOption{}, true)); + ASSERT_TRUE(fi4.Initialize("/test", mdsclient, userInfo, + FileServiceOption{}, true)); ASSERT_EQ(-1, fi4.Write("", 0, 0)); fi4.UnInitialize(); @@ -62,12 +61,12 @@ TEST(FileInstanceTest, CommonTest) { TEST(FileInstanceTest, OpenReadonlyAndDiscardTest) { FileInstance instance; FileServiceOption opt; - MDSClient mdsClient; + std::shared_ptr mdsclient = std::make_shared(); UserInfo userInfo{"hello", "world"}; ASSERT_TRUE( instance.Initialize("/FileInstanceTest-OpenReadonlyAndDiscardTest", - &mdsClient, userInfo, opt, true)); + mdsclient, userInfo, opt, true)); ASSERT_EQ(-1, instance.Discard(0, 0)); diff --git a/test/client/iotracker_splitor_unittest.cpp b/test/client/iotracker_splitor_unittest.cpp index 9acaeacdb4..ec36caa322 100644 --- a/test/client/iotracker_splitor_unittest.cpp +++ b/test/client/iotracker_splitor_unittest.cpp @@ -105,8 +105,9 @@ class IOTrackerSplitorTest : public ::testing::Test { userinfo.owner = "userinfo"; userinfo.password = "12345"; - mdsclient_.Initialize(fopt.metaServerOpt); - fileinstance_->Initialize("/test", &mdsclient_, userinfo, fopt); + mdsclient_ = std::make_shared(); + mdsclient_->Initialize(fopt.metaServerOpt); + fileinstance_->Initialize("/test", mdsclient_, userinfo, fopt); InsertMetaCache(); SourceReader::GetInstance().SetOption(fopt); @@ -118,7 +119,7 @@ class IOTrackerSplitorTest : public ::testing::Test { writeData.clear(); fileinstance_->Close(); fileinstance_->UnInitialize(); - mdsclient_.UnInitialize(); + mdsclient_.reset(); delete fileinstance_; SourceReader::GetInstance().Stop(); @@ -294,7 +295,7 @@ class IOTrackerSplitorTest : public ::testing::Test { fi.segmentsize = 1 * 1024 * 1024 * 1024ul; SegmentInfo sinfo; LogicalPoolCopysetIDInfo_t lpcsIDInfo; - mdsclient_.GetOrAllocateSegment(true, 0, &fi, &sinfo); + mdsclient_->GetOrAllocateSegment(true, 0, &fi, &sinfo); int count = 0; for (auto iter : sinfo.chunkvec) { uint64_t index = (sinfo.startoffset + count*fi.chunksize ) @@ -304,8 +305,8 @@ class IOTrackerSplitorTest : public ::testing::Test { } std::vector cpinfoVec; - mdsclient_.GetServerList(lpcsIDInfo.lpid, - lpcsIDInfo.cpidVec, &cpinfoVec); + mdsclient_->GetServerList(lpcsIDInfo.lpid, lpcsIDInfo.cpidVec, + &cpinfoVec); for (auto iter : cpinfoVec) { mc->UpdateCopysetInfo(lpcsIDInfo.lpid, iter.cpid_, iter); @@ -321,7 +322,7 @@ class IOTrackerSplitorTest : public ::testing::Test { FileClient *fileClient_; UserInfo_t userinfo; - MDSClient mdsclient_; + std::shared_ptr mdsclient_; FileServiceOption fopt; FileInstance *fileinstance_; brpc::Server server; @@ -350,7 +351,7 @@ TEST_F(IOTrackerSplitorTest, AsyncStartRead) { ioreadflag = false; char* data = static_cast(aioctx.buf); - iomana->AioRead(&aioctx, &mdsclient_, UserDataType::RawBuffer); + iomana->AioRead(&aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(readmtx); @@ -392,7 +393,7 @@ TEST_F(IOTrackerSplitorTest, AsyncStartWrite) { fi.chunksize = 4 * 1024 * 1024; fi.segmentsize = 1 * 1024 * 1024 * 1024ul; iowriteflag = false; - iomana->AioWrite(&aioctx, &mdsclient_, UserDataType::RawBuffer); + iomana->AioWrite(&aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(writemtx); @@ -426,7 +427,7 @@ TEST_F(IOTrackerSplitorTest, StartRead) { char* data = new char[length]; auto threadfunc = [&]() { - iomana->Read(data, offset, length, &mdsclient_); + iomana->Read(data, offset, length, mdsclient_.get()); }; std::thread process(threadfunc); @@ -461,7 +462,7 @@ TEST_F(IOTrackerSplitorTest, StartWrite) { memset(buf + 4 * 1024 + chunk_size, 'c', 4 * 1024); auto threadfunc = [&]() { - iomana->Write(buf, offset, length, &mdsclient_); + iomana->Write(buf, offset, length, mdsclient_.get()); }; std::thread process(threadfunc); @@ -498,7 +499,7 @@ TEST_F(IOTrackerSplitorTest, ManagerAsyncStartRead) { ioreadflag = false; char* data = static_cast(aioctx->buf); - ioctxmana->AioRead(aioctx, &mdsclient_, UserDataType::RawBuffer); + ioctxmana->AioRead(aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(readmtx); @@ -535,7 +536,7 @@ TEST_F(IOTrackerSplitorTest, ManagerAsyncStartWrite) { memset(data + 4 * 1024 + chunk_size, 'c', 4 * 1024); iowriteflag = false; - ioctxmana->AioWrite(aioctx, &mdsclient_, UserDataType::RawBuffer); + ioctxmana->AioWrite(aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(writemtx); @@ -663,10 +664,8 @@ TEST_F(IOTrackerSplitorTest, ManagerStartRead) { char* data = new char[length]; auto threadfunc = [&]() { - ASSERT_EQ(length, ioctxmana->Read(data, - offset, - length, - &mdsclient_)); + ASSERT_EQ(length, + ioctxmana->Read(data, offset, length, mdsclient_.get())); }; std::thread process(threadfunc); @@ -700,10 +699,7 @@ TEST_F(IOTrackerSplitorTest, ManagerStartWrite) { memset(buf + 4 * 1024 + chunk_size, 'c', 4 * 1024); auto threadfunc = [&]() { - ioctxmana->Write(buf, - offset, - length, - &mdsclient_); + ioctxmana->Write(buf, offset, length, mdsclient_.get()); }; std::thread process(threadfunc); @@ -736,7 +732,7 @@ TEST_F(IOTrackerSplitorTest, ExceptionTest_TEST) { rootuserinfo.owner = "root"; rootuserinfo.password = "root_password"; - ASSERT_TRUE(fileserv->Initialize("/test", &mdsclient_, rootuserinfo, fopt)); + ASSERT_TRUE(fileserv->Initialize("/test", mdsclient_, rootuserinfo, fopt)); ASSERT_EQ(LIBCURVE_ERROR::OK, fileserv->Open("1_userinfo_.txt", userinfo)); curve::client::IOManager4File* iomana = fileserv->GetIOManager4File(); MetaCache* mc = fileserv->GetIOManager4File()->GetMetaCache(); @@ -756,8 +752,7 @@ TEST_F(IOTrackerSplitorTest, ExceptionTest_TEST) { auto threadfunc = [&]() { iotracker->SetUserDataType(UserDataType::RawBuffer); - iotracker->StartWrite( - nullptr, offset, length, &mdsclient_, &fi); + iotracker->StartWrite(nullptr, offset, length, mdsclient_.get(), &fi); }; std::thread process(threadfunc); @@ -765,7 +760,7 @@ TEST_F(IOTrackerSplitorTest, ExceptionTest_TEST) { uint64_t off = 4 * 1024 * 1024 * 1024ul - 4 * 1024; uint64_t len = 4 * 1024 * 1024 + 8 * 1024; - iomana->Write(buf, off, len, &mdsclient_); + iomana->Write(buf, off, len, mdsclient_.get()); if (process.joinable()) { process.join(); @@ -799,10 +794,8 @@ TEST_F(IOTrackerSplitorTest, BoundaryTEST) { memset(buf + 4 * 1024 + chunk_size, 'c', 4 * 1024); auto threadfunc = [&]() { - ASSERT_EQ(-1 * LIBCURVE_ERROR::FAILED, ioctxmana->Write(buf, - offset, - length, - &mdsclient_)); + ASSERT_EQ(-1 * LIBCURVE_ERROR::FAILED, + ioctxmana->Write(buf, offset, length, mdsclient_.get())); }; std::thread process(threadfunc); @@ -843,13 +836,9 @@ TEST_F(IOTrackerSplitorTest, largeIOTest) { std::vector reqlist; auto dataCopy = writeData; - ASSERT_EQ(0, curve::client::Splitor::IO2ChunkRequests(iotracker, mc, - &reqlist, - &dataCopy, - offset, - length, - &mdsclient_, - &fi)); + ASSERT_EQ(0, curve::client::Splitor::IO2ChunkRequests( + iotracker, mc, &reqlist, &dataCopy, offset, length, + mdsclient_.get(), &fi)); ASSERT_EQ(2, reqlist.size()); RequestContext* first = reqlist.front(); @@ -896,7 +885,7 @@ TEST_F(IOTrackerSplitorTest, InvalidParam) { ASSERT_EQ(-1, curve::client::Splitor::IO2ChunkRequests( nullptr, mc, &reqlist, &iobuf, offset, length, - &mdsclient_, &fi)); + mdsclient_.get(), &fi)); ASSERT_EQ(-1, curve::client::Splitor::SingleChunkIO2ChunkRequests( nullptr, mc, @@ -904,7 +893,7 @@ TEST_F(IOTrackerSplitorTest, InvalidParam) { ASSERT_EQ(-1, curve::client::Splitor::IO2ChunkRequests( iotracker, nullptr, &reqlist, &iobuf, offset, length, - &mdsclient_, nullptr)); + mdsclient_.get(), nullptr)); ASSERT_EQ(-1, curve::client::Splitor::SingleChunkIO2ChunkRequests( iotracker, nullptr, @@ -912,7 +901,7 @@ TEST_F(IOTrackerSplitorTest, InvalidParam) { ASSERT_EQ(-1, curve::client::Splitor::IO2ChunkRequests( iotracker, mc, &reqlist, &iobuf, offset, length, - &mdsclient_, nullptr)); + mdsclient_.get(), nullptr)); ASSERT_EQ( -1, curve::client::Splitor::IO2ChunkRequests( @@ -924,7 +913,7 @@ TEST_F(IOTrackerSplitorTest, InvalidParam) { ASSERT_EQ(-1, curve::client::Splitor::IO2ChunkRequests( iotracker, mc, nullptr, &iobuf, offset, length, - &mdsclient_, nullptr)); + mdsclient_.get(), nullptr)); ASSERT_EQ(-1, curve::client::Splitor::SingleChunkIO2ChunkRequests( iotracker, mc, @@ -932,7 +921,7 @@ TEST_F(IOTrackerSplitorTest, InvalidParam) { ASSERT_EQ(-1, curve::client::Splitor::IO2ChunkRequests( iotracker, mc, &reqlist, nullptr, offset, length, - &mdsclient_, nullptr)); + mdsclient_.get(), nullptr)); iotracker->SetOpType(OpType::WRITE); ASSERT_EQ(-1, @@ -943,7 +932,7 @@ TEST_F(IOTrackerSplitorTest, InvalidParam) { iotracker->SetOpType(OpType::WRITE); ASSERT_EQ(-1, curve::client::Splitor::IO2ChunkRequests( iotracker, mc, &reqlist, nullptr, offset, length, - &mdsclient_, &fi)); + mdsclient_.get(), &fi)); // ASSERT_EQ(-1, Splitor::CalcDiscardSegments(nullptr)); @@ -1077,13 +1066,9 @@ TEST_F(IOTrackerSplitorTest, stripeTest) { mc->UpdateChunkInfoByIndex(1, chinfo1); std::vector reqlist; - ASSERT_EQ(0, curve::client::Splitor::IO2ChunkRequests(iotracker, mc, - &reqlist, - &dataCopy, - offset, - length, - &mdsclient_, - &fi)); + ASSERT_EQ(0, curve::client::Splitor::IO2ChunkRequests( + iotracker, mc, &reqlist, &dataCopy, offset, length, + mdsclient_.get(), &fi)); ASSERT_EQ(2, reqlist.size()); RequestContext* first = reqlist.front(); @@ -1110,13 +1095,9 @@ TEST_F(IOTrackerSplitorTest, stripeTest) { dataCopy.append(buf, length); mc->UpdateChunkInfoByIndex(3, chinfo); mc->UpdateChunkInfoByIndex(4, chinfo1); - ASSERT_EQ(0, curve::client::Splitor::IO2ChunkRequests(iotracker, mc, - &reqlist, - &dataCopy, - offset, - length, - &mdsclient_, - &fi)); + ASSERT_EQ(0, curve::client::Splitor::IO2ChunkRequests( + iotracker, mc, &reqlist, &dataCopy, offset, length, + mdsclient_.get(), &fi)); ASSERT_EQ(2, reqlist.size()); first = reqlist.front(); reqlist.erase(reqlist.begin()); @@ -1168,7 +1149,7 @@ TEST_F(IOTrackerSplitorTest, TestDisableStripeForStripeFile) { std::vector reqlist; ASSERT_EQ(0, Splitor::IO2ChunkRequests(&ioTracker, cache, &reqlist, &dataCopy, - offset, length, &mdsclient_, &fi)); + offset, length, mdsclient_.get(), &fi)); ASSERT_EQ(2, reqlist.size()); auto* first = reqlist[0]; @@ -1202,7 +1183,7 @@ TEST_F(IOTrackerSplitorTest, StartReadNotAllocateSegment) { char* data = new char[length]; auto threadfunc = [&]() { - iomana->Read(data, offset, length, &mdsclient_); + iomana->Read(data, offset, length, mdsclient_.get()); }; std::thread process(threadfunc); @@ -1235,7 +1216,7 @@ TEST_F(IOTrackerSplitorTest, AsyncStartReadNotAllocateSegment) { ioreadflag = false; char* data = static_cast(aioctx.buf); - iomana->AioRead(&aioctx, &mdsclient_, UserDataType::RawBuffer); + iomana->AioRead(&aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(readmtx); @@ -1266,7 +1247,7 @@ TEST_F(IOTrackerSplitorTest, StartReadNotAllocateSegment2) { char* data = new char[length]; auto threadfunc = [&]() { - iomana->Read(data, offset, length, &mdsclient_); + iomana->Read(data, offset, length, mdsclient_.get()); }; std::thread process(threadfunc); @@ -1305,7 +1286,7 @@ TEST_F(IOTrackerSplitorTest, AsyncStartReadNotAllocateSegment2) { ioreadflag = false; char* data = static_cast(aioctx.buf); - iomana->AioRead(&aioctx, &mdsclient_, UserDataType::RawBuffer); + iomana->AioRead(&aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(readmtx); @@ -1333,8 +1314,8 @@ TEST_F(IOTrackerSplitorTest, StartReadNotAllocateSegmentFromOrigin) { FileInstance* fileinstance2 = new FileInstance(); userinfo.owner = "cloneuser-test1"; userinfo.password = "12345"; - mdsclient_.Initialize(fopt.metaServerOpt); - fileinstance2->Initialize("/clonesource", &mdsclient_, userinfo, fopt); + mdsclient_->Initialize(fopt.metaServerOpt); + fileinstance2->Initialize("/clonesource", mdsclient_, userinfo, fopt); MockRequestScheduler* mockschuler2 = new MockRequestScheduler; mockschuler2->DelegateToFake(); @@ -1374,7 +1355,7 @@ TEST_F(IOTrackerSplitorTest, StartReadNotAllocateSegmentFromOrigin) { char* data = new char[length]; auto threadfunc = [&]() { - iomana->Read(data, offset, length, &mdsclient_); + iomana->Read(data, offset, length, mdsclient_.get()); }; std::thread process(threadfunc); @@ -1407,8 +1388,8 @@ TEST_F(IOTrackerSplitorTest, AsyncStartReadNotAllocateSegmentFromOrigin) { FileInstance* fileinstance2 = new FileInstance(); userinfo.owner = "cloneuser-test2"; userinfo.password = "12345"; - mdsclient_.Initialize(fopt.metaServerOpt); - fileinstance2->Initialize("/clonesource", &mdsclient_, userinfo, fopt); + mdsclient_->Initialize(fopt.metaServerOpt); + fileinstance2->Initialize("/clonesource", mdsclient_, userinfo, fopt); MockRequestScheduler* mockschuler2 = new MockRequestScheduler; mockschuler2->DelegateToFake(); @@ -1453,7 +1434,7 @@ TEST_F(IOTrackerSplitorTest, AsyncStartReadNotAllocateSegmentFromOrigin) { ioreadflag = false; char* data = static_cast(aioctx.buf); - iomana->AioRead(&aioctx, &mdsclient_, UserDataType::RawBuffer); + iomana->AioRead(&aioctx, mdsclient_.get(), UserDataType::RawBuffer); { std::unique_lock lk(readmtx); diff --git a/test/client/libcurve_interface_unittest.cpp b/test/client/libcurve_interface_unittest.cpp index 63d9cbf9da..3905da3ead 100644 --- a/test/client/libcurve_interface_unittest.cpp +++ b/test/client/libcurve_interface_unittest.cpp @@ -652,7 +652,7 @@ TEST(TestLibcurveInterface, UnstableChunkserverTest) { std::string filename = "/1_userinfo_"; UserInfo_t userinfo; - MDSClient mdsclient_; + std::shared_ptr mdsclient_ = std::make_shared(); FileServiceOption fopt; FileInstance fileinstance_; @@ -682,9 +682,9 @@ TEST(TestLibcurveInterface, UnstableChunkserverTest) { // fopt.ioOpt.ioSenderOpt.failRequestOpt); LOG(INFO) << "here"; - mdsclient_.Initialize(fopt.metaServerOpt); + mdsclient_->Initialize(fopt.metaServerOpt); fileinstance_.Initialize( - "/UnstableChunkserverTest", &mdsclient_, userinfo, fopt); + "/UnstableChunkserverTest", mdsclient_, userinfo, fopt); // 设置leaderid EndPoint ep; @@ -832,7 +832,6 @@ TEST(TestLibcurveInterface, UnstableChunkserverTest) { fileinstance_.Close(); fileinstance_.UnInitialize(); - mdsclient_.UnInitialize(); mds.UnInitialize(); delete[] buffer; } @@ -841,7 +840,7 @@ TEST(TestLibcurveInterface, ResumeTimeoutBackoff) { std::string filename = "/1_userinfo_"; UserInfo_t userinfo; - MDSClient mdsclient_; + std::shared_ptr mdsclient_ = std::make_shared(); FileServiceOption fopt; FileInstance fileinstance_; @@ -867,9 +866,9 @@ TEST(TestLibcurveInterface, ResumeTimeoutBackoff) { fopt.leaseOpt.mdsRefreshTimesPerLease = 4; fopt.ioOpt.metaCacheOpt.chunkserverUnstableOption.maxStableChunkServerTimeoutTimes = 10; // NOLINT - mdsclient_.Initialize(fopt.metaServerOpt); + mdsclient_->Initialize(fopt.metaServerOpt); fileinstance_.Initialize( - "/ResumeTimeoutBackoff", &mdsclient_, userinfo, fopt); + "/ResumeTimeoutBackoff", mdsclient_, userinfo, fopt); // 设置leaderid EndPoint ep; @@ -945,7 +944,6 @@ TEST(TestLibcurveInterface, ResumeTimeoutBackoff) { fileinstance_.Close(); fileinstance_.UnInitialize(); - mdsclient_.UnInitialize(); mds.UnInitialize(); delete[] buffer; }