diff --git a/curvefs/src/metaserver/copyset/copyset_node.cpp b/curvefs/src/metaserver/copyset/copyset_node.cpp index 45245195b5..a51ba6e34e 100644 --- a/curvefs/src/metaserver/copyset/copyset_node.cpp +++ b/curvefs/src/metaserver/copyset/copyset_node.cpp @@ -92,9 +92,15 @@ CopysetNode::CopysetNode(PoolId poolId, CopysetId copysetId, confChangeMtx_(), ongoingConfChange_(), metric_(absl::make_unique(poolId_, copysetId_)), - isLoading_(false) {} + isLoading_(false), + enableSnapshot_(false), + snapshotCv_(), + snapshotLock_(), + snapshotTask_(), + snapshotFuture_() {} CopysetNode::~CopysetNode() { + StopSnapshotTask(); Stop(); raftNode_.reset(); applyQueue_.reset(); @@ -155,6 +161,8 @@ bool CopysetNode::Init(const CopysetNodeOptions& options) { peerId_ = braft::PeerId(addr, 0); raftNode_ = absl::make_unique(groupId_, peerId_); + // init snapshot task + StartSnapshotTask(); return true; } @@ -362,6 +370,79 @@ class OnSnapshotSaveDoneClosureImpl : public OnSnapshotSaveDoneClosure { } // namespace +void CopysetNode::SnapshotTaskRun() { + while (enableSnapshot_) { + std::function task; + { + std::unique_lock lock(snapshotLock_); + snapshotCv_.wait(lock); + std::swap(task, snapshotTask_); + } + if (task) { + task(); + } + } +} + +void CopysetNode::StartSnapshotTask() { + enableSnapshot_ = true; + { + std::unique_lock lock(snapshotLock_); + snapshotFuture_ = std::async(std::launch::async, + &CopysetNode::SnapshotTaskRun, + this); + } +} + +void CopysetNode::StopSnapshotTask() { + enableSnapshot_ = false; + std::future future; + { + std::lock_guard lock(snapshotLock_); + std::swap(future, snapshotFuture_); + snapshotCv_.notify_one(); + } + if (future.valid()) { + future.wait(); + } +} + +void CopysetNode::DoSnapshot(OnSnapshotSaveDoneClosure* done) { + // NOTE: save metadata cannot be asynchronous + // we need maintain the consistency with + // raft snapshot metadata + std::vector files; + brpc::ClosureGuard doneGuard(done); + auto *writer = done->GetSnapshotWriter(); + if (!metaStore_->SaveMeta(writer->get_path(), &files)) { + done->SetError(MetaStatusCode::SAVE_META_FAIL); + LOG(ERROR) << "Save meta store metadata failed"; + return; + } + // asynchronous save data + { + std::lock_guard lock(snapshotLock_); + snapshotTask_ = [files, done, this]() mutable { + brpc::ClosureGuard doneGuard(done); + auto *writer = done->GetSnapshotWriter(); + // save data files + if (!metaStore_->SaveData(writer->get_path(), &files)) { + done->SetError(MetaStatusCode::SAVE_META_FAIL); + LOG(ERROR) << "Save meta store data failed"; + return; + } + // add files to snapshot writer + // file is a relative path under the given directory + for (const auto &f : files) { + writer->add_file(f); + } + done->SetSuccess(); + }; + snapshotCv_.notify_one(); + } + doneGuard.release(); +} + void CopysetNode::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done) { LOG(INFO) << "Copyset " << name_ << " saving snapshot to '" @@ -390,12 +471,7 @@ void CopysetNode::on_snapshot_save(braft::SnapshotWriter* writer, writer->add_file(kConfEpochFilename); - // TODO(wuhanqing): MetaStore::Save will start a thread and do task - // asynchronously, after task completed it will call - // OnSnapshotSaveDoneImpl::Run - // BUT, this manner is not so clear, maybe it better to make thing - // asynchronous directly in here - metaStore_->Save(writer->get_path(), new OnSnapshotSaveDoneClosureImpl( + DoSnapshot(new OnSnapshotSaveDoneClosureImpl( this, writer, done, metricCtx)); doneGuard.release(); diff --git a/curvefs/src/metaserver/copyset/copyset_node.h b/curvefs/src/metaserver/copyset/copyset_node.h index ef6c09b655..08ea052a37 100644 --- a/curvefs/src/metaserver/copyset/copyset_node.h +++ b/curvefs/src/metaserver/copyset/copyset_node.h @@ -31,6 +31,7 @@ #include #include #include +#include #include "curvefs/src/metaserver/common/types.h" #include "curvefs/src/metaserver/copyset/concurrent_apply_queue.h" @@ -220,6 +221,16 @@ class CopysetNode : public braft::StateMachine { FRIEND_TEST(CopysetNodeBlockGroupTest, Test_AggregateBlockStatInfo); + private: + // for snapshot + void SnapshotTaskRun(); + + void StartSnapshotTask(); + + void StopSnapshotTask(); + + void DoSnapshot(OnSnapshotSaveDoneClosure* done); + private: const PoolId poolId_; const CopysetId copysetId_; @@ -267,6 +278,22 @@ class CopysetNode : public braft::StateMachine { std::unique_ptr metric_; std::atomic isLoading_; + + // for asynchronous snapshot + std::atomic_bool enableSnapshot_; + + std::condition_variable snapshotCv_; + + mutable std::mutex snapshotLock_; + + // NOTE: maintain a queue is unnecessary + // we only need one item + std::function snapshotTask_; + // NOTE: we need to use a thread to + // save snapshot, and this thread + // will blocking on `KVStorage::Checkpoint()` + // so we cannot use coroutine(bthread) + std::future snapshotFuture_; }; inline void CopysetNode::Propose(const braft::Task& task) { diff --git a/curvefs/src/metaserver/copyset/meta_operator.h b/curvefs/src/metaserver/copyset/meta_operator.h index 70cef6c3e3..12dd566184 100644 --- a/curvefs/src/metaserver/copyset/meta_operator.h +++ b/curvefs/src/metaserver/copyset/meta_operator.h @@ -37,23 +37,31 @@ namespace copyset { class MetaOperator { public: - MetaOperator(CopysetNode *node, google::protobuf::RpcController *cntl, - const google::protobuf::Message *request, - google::protobuf::Message *response, - google::protobuf::Closure *done) - : node_(node), cntl_(cntl), request_(request), response_(response), - done_(done), ownRequest_(false) {} - - MetaOperator(CopysetNode *node, const google::protobuf::Message *request, + MetaOperator(CopysetNode* node, google::protobuf::RpcController* cntl, + const google::protobuf::Message* request, + google::protobuf::Message* response, + google::protobuf::Closure* done) + : node_(node), + cntl_(cntl), + request_(request), + response_(response), + done_(done), + ownRequest_(false) {} + + MetaOperator(CopysetNode* node, const google::protobuf::Message* request, bool ownRequest = true) - : node_(node), cntl_(nullptr), request_(request), response_(nullptr), - done_(nullptr), ownRequest_(ownRequest) {} + : node_(node), + cntl_(nullptr), + request_(request), + response_(nullptr), + done_(nullptr), + ownRequest_(ownRequest) {} virtual ~MetaOperator(); - MetaOperator(const MetaOperator &) = delete; + MetaOperator(const MetaOperator&) = delete; - MetaOperator &operator=(const MetaOperator &) = delete; + MetaOperator& operator=(const MetaOperator&) = delete; /** * @brief Propose current operator to Raft @@ -63,11 +71,13 @@ class MetaOperator { /** * @brief Return internal closure */ - google::protobuf::Closure *Closure() const { return done_; } + google::protobuf::Closure* Closure() const { + return done_; + } void RedirectRequest(); - virtual void OnApply(int64_t index, google::protobuf::Closure *done, + virtual void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) = 0; virtual void OnApplyFromLog(int64_t index, uint64_t startTimeUs) = 0; @@ -99,7 +109,9 @@ class MetaOperator { /** * @brief Check whether current copyset node is leader */ - bool IsLeaderTerm() const { return node_->IsLeaderTerm(); } + bool IsLeaderTerm() const { + return node_->IsLeaderTerm(); + } /** * @brief Propose current operator to braft::Task @@ -127,19 +139,19 @@ class MetaOperator { virtual bool CanBypassPropose() const { return false; } protected: - CopysetNode *node_; + CopysetNode* node_; // rpc controller - google::protobuf::RpcController *cntl_; + google::protobuf::RpcController* cntl_; // rpc request - const google::protobuf::Message *request_; + const google::protobuf::Message* request_; // rpc response - google::protobuf::Message *response_; + google::protobuf::Message* response_; // rpc closure or meta service closure - google::protobuf::Closure *done_; + google::protobuf::Closure* done_; // whether own request, if true, delete request when destory const bool ownRequest_; @@ -152,7 +164,7 @@ class GetDentryOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -173,7 +185,7 @@ class ListDentryOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -194,7 +206,7 @@ class CreateDentryOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -213,7 +225,7 @@ class DeleteDentryOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -232,7 +244,7 @@ class GetInodeOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -253,7 +265,7 @@ class BatchGetInodeAttrOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -274,7 +286,7 @@ class BatchGetXAttrOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -295,7 +307,7 @@ class CreateInodeOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -314,7 +326,7 @@ class UpdateInodeOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -333,7 +345,7 @@ class GetOrModifyS3ChunkInfoOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -352,7 +364,7 @@ class DeleteInodeOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -371,7 +383,7 @@ class CreateRootInodeOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -390,7 +402,7 @@ class CreateManageInodeOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -409,7 +421,7 @@ class UpdateInodeS3VersionOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -428,7 +440,7 @@ class CreatePartitionOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -447,7 +459,7 @@ class DeletePartitionOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -466,7 +478,7 @@ class PrepareRenameTxOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -485,7 +497,7 @@ class GetVolumeExtentOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -506,7 +518,7 @@ class UpdateVolumeExtentOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; @@ -525,7 +537,7 @@ class UpdateDeallocatableBlockGroupOperator : public MetaOperator { public: using MetaOperator::MetaOperator; - void OnApply(int64_t index, google::protobuf::Closure *done, + void OnApply(int64_t index, google::protobuf::Closure* done, uint64_t startTimeUs) override; void OnApplyFromLog(int64_t index, uint64_t startTimeUs) override; diff --git a/curvefs/src/metaserver/dentry_manager.cpp b/curvefs/src/metaserver/dentry_manager.cpp index 14641f6da5..c104e6f432 100644 --- a/curvefs/src/metaserver/dentry_manager.cpp +++ b/curvefs/src/metaserver/dentry_manager.cpp @@ -33,20 +33,27 @@ DentryManager::DentryManager(std::shared_ptr dentryStorage, std::shared_ptr txManager) : dentryStorage_(dentryStorage), txManager_(txManager), appliedIndex_(-1) { // for compatibility, we initialize applied index to -1 - dentryStorage_->GetAppliedIndex(&appliedIndex_); } -void DentryManager::Log4Dentry(const std::string &request, - const Dentry &dentry) { +bool DentryManager::Init() { + if (dentryStorage_->Init()) { + auto s = dentryStorage_->GetAppliedIndex(&appliedIndex_); + return s.ok() || s.IsNotFound(); + } + return false; +} + +void DentryManager::Log4Dentry(const std::string& request, + const Dentry& dentry) { VLOG(9) << "Receive " << request << " request, dentry = (" << dentry.ShortDebugString() << ")"; } -void DentryManager::Log4Code(const std::string &request, MetaStatusCode rc) { - auto succ = - (rc == MetaStatusCode::OK || rc == MetaStatusCode::IDEMPOTENCE_OK || - (rc == MetaStatusCode::NOT_FOUND && - (request == "ListDentry" || request == "GetDentry"))); +void DentryManager::Log4Code(const std::string& request, MetaStatusCode rc) { + auto succ = (rc == MetaStatusCode::OK || + rc == MetaStatusCode::IDEMPOTENCE_OK || + (rc == MetaStatusCode::NOT_FOUND && + (request == "ListDentry" || request == "GetDentry"))); std::ostringstream message; message << request << " " << (succ ? "success" : "fail") << ", retCode = " << MetaStatusCode_Name(rc); @@ -58,7 +65,7 @@ void DentryManager::Log4Code(const std::string &request, MetaStatusCode rc) { } } -MetaStatusCode DentryManager::CreateDentry(const Dentry &dentry, +MetaStatusCode DentryManager::CreateDentry(const Dentry& dentry, std::int64_t logIndex) { if (logIndex <= appliedIndex_) { LOG(INFO) << "Log entry already be applied, index = " << logIndex @@ -71,7 +78,7 @@ MetaStatusCode DentryManager::CreateDentry(const Dentry &dentry, return rc; } -MetaStatusCode DentryManager::CreateDentry(const DentryVec &vec, bool merge, +MetaStatusCode DentryManager::CreateDentry(const DentryVec& vec, bool merge, std::int64_t logIndex) { if (logIndex <= appliedIndex_) { LOG(INFO) << "Log entry already be applied, index = " << logIndex @@ -85,7 +92,7 @@ MetaStatusCode DentryManager::CreateDentry(const DentryVec &vec, bool merge, return rc; } -MetaStatusCode DentryManager::DeleteDentry(const Dentry &dentry, +MetaStatusCode DentryManager::DeleteDentry(const Dentry& dentry, std::int64_t logIndex) { if (logIndex <= appliedIndex_) { LOG(INFO) << "Log entry already be applied, index = " << logIndex @@ -98,15 +105,15 @@ MetaStatusCode DentryManager::DeleteDentry(const Dentry &dentry, return rc; } -MetaStatusCode DentryManager::GetDentry(Dentry *dentry) { +MetaStatusCode DentryManager::GetDentry(Dentry* dentry) { Log4Dentry("GetDentry", *dentry); MetaStatusCode rc = dentryStorage_->Get(dentry); Log4Code("GetDentry", rc); return rc; } -MetaStatusCode DentryManager::ListDentry(const Dentry &dentry, - std::vector *dentrys, +MetaStatusCode DentryManager::ListDentry(const Dentry& dentry, + std::vector* dentrys, uint32_t limit, bool onlyDir) { Log4Dentry("ListDentry", dentry); MetaStatusCode rc = dentryStorage_->List(dentry, dentrys, limit, onlyDir); @@ -119,9 +126,9 @@ void DentryManager::ClearDentry() { LOG(INFO) << "ClearDentry ok"; } -MetaStatusCode DentryManager::HandleRenameTx(const std::vector &dentrys, +MetaStatusCode DentryManager::HandleRenameTx(const std::vector& dentrys, std::int64_t logIndex) { - for (const auto &dentry : dentrys) { + for (const auto& dentry : dentrys) { Log4Dentry("HandleRenameTx", dentry); } auto rc = txManager_->HandleRenameTx(dentrys, logIndex); diff --git a/curvefs/src/metaserver/dentry_manager.h b/curvefs/src/metaserver/dentry_manager.h index 7a163d082d..7d774aa53a 100644 --- a/curvefs/src/metaserver/dentry_manager.h +++ b/curvefs/src/metaserver/dentry_manager.h @@ -40,28 +40,30 @@ class DentryManager { DentryManager(std::shared_ptr dentryStorage, std::shared_ptr txManger); - MetaStatusCode CreateDentry(const Dentry &dentry, std::int64_t logIndex); + bool Init(); + + MetaStatusCode CreateDentry(const Dentry& dentry, std::int64_t logIndex); // only invoked from snapshot loadding - MetaStatusCode CreateDentry(const DentryVec &vec, bool merge, + MetaStatusCode CreateDentry(const DentryVec& vec, bool merge, std::int64_t logIndex); - MetaStatusCode DeleteDentry(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode DeleteDentry(const Dentry& dentry, std::int64_t logIndex); - MetaStatusCode GetDentry(Dentry *dentry); + MetaStatusCode GetDentry(Dentry* dentry); - MetaStatusCode ListDentry(const Dentry &dentry, - std::vector *dentrys, uint32_t limit, + MetaStatusCode ListDentry(const Dentry& dentry, + std::vector* dentrys, uint32_t limit, bool onlyDir = false); void ClearDentry(); - MetaStatusCode HandleRenameTx(const std::vector &dentrys, + MetaStatusCode HandleRenameTx(const std::vector& dentrys, std::int64_t logIndex); private: - void Log4Dentry(const std::string &request, const Dentry &dentry); - void Log4Code(const std::string &request, MetaStatusCode rc); + void Log4Dentry(const std::string& request, const Dentry& dentry); + void Log4Code(const std::string& request, MetaStatusCode rc); private: std::shared_ptr dentryStorage_; diff --git a/curvefs/src/metaserver/dentry_storage.cpp b/curvefs/src/metaserver/dentry_storage.cpp index 501ad32e52..f713df3458 100644 --- a/curvefs/src/metaserver/dentry_storage.cpp +++ b/curvefs/src/metaserver/dentry_storage.cpp @@ -43,30 +43,32 @@ using ::curvefs::metaserver::storage::Prefix4AllDentry; using ::curvefs::metaserver::storage::Prefix4SameParentDentry; using ::curvefs::metaserver::storage::Status; -bool operator==(const Dentry &lhs, const Dentry &rhs) { - return EQUAL(fsid) && EQUAL(parentinodeid) && EQUAL(name) && EQUAL(txid) && - EQUAL(inodeid) && EQUAL(flag); +bool operator==(const Dentry& lhs, const Dentry& rhs) { + return EQUAL(fsid) && EQUAL(parentinodeid) && EQUAL(name) && + EQUAL(txid) && EQUAL(inodeid) && EQUAL(flag); } -bool operator<(const Dentry &lhs, const Dentry &rhs) { - return LESS(fsid) || LESS2(fsid, parentinodeid) || +bool operator<(const Dentry& lhs, const Dentry& rhs) { + return LESS(fsid) || + LESS2(fsid, parentinodeid) || LESS3(fsid, parentinodeid, name) || LESS4(fsid, parentinodeid, name, txid); } -static bool BelongSomeOne(const Dentry &lhs, const Dentry &rhs) { - return EQUAL(fsid) && EQUAL(parentinodeid) && EQUAL(name) && EQUAL(inodeid); +static bool BelongSomeOne(const Dentry& lhs, const Dentry& rhs) { + return EQUAL(fsid) && EQUAL(parentinodeid) && EQUAL(name) && + EQUAL(inodeid); } -static bool HasDeleteMarkFlag(const Dentry &dentry) { +static bool HasDeleteMarkFlag(const Dentry& dentry) { return (dentry.flag() & DentryFlag::DELETE_MARK_FLAG) != 0; } -DentryVector::DentryVector(DentryVec *vec) +DentryVector::DentryVector(DentryVec* vec) : vec_(vec), nPendingAdd_(0), nPendingDel_(0) {} -void DentryVector::Insert(const Dentry &dentry) { - for (const Dentry &item : vec_->dentrys()) { +void DentryVector::Insert(const Dentry& dentry) { + for (const Dentry& item : vec_->dentrys()) { if (item == dentry) { return; } @@ -75,7 +77,7 @@ void DentryVector::Insert(const Dentry &dentry) { nPendingAdd_ += 1; } -void DentryVector::Delete(const Dentry &dentry) { +void DentryVector::Delete(const Dentry& dentry) { for (int i = 0; i < vec_->dentrys_size(); i++) { if (vec_->dentrys(i) == dentry) { vec_->mutable_dentrys()->DeleteSubrange(i, 1); @@ -85,22 +87,22 @@ void DentryVector::Delete(const Dentry &dentry) { } } -void DentryVector::Merge(const DentryVec &src) { - for (const auto &dentry : src.dentrys()) { +void DentryVector::Merge(const DentryVec& src) { + for (const auto& dentry : src.dentrys()) { vec_->add_dentrys()->CopyFrom(dentry); } nPendingAdd_ = src.dentrys_size(); } -void DentryVector::Filter(uint64_t maxTxId, BTree *btree) { - for (const Dentry &dentry : vec_->dentrys()) { +void DentryVector::Filter(uint64_t maxTxId, BTree* btree) { + for (const Dentry& dentry : vec_->dentrys()) { if (dentry.txid() <= maxTxId) { btree->insert(dentry); } } } -void DentryVector::Confirm(uint64_t *count) { +void DentryVector::Confirm(uint64_t* count) { if (nPendingDel_ > *count + nPendingAdd_) { LOG(ERROR) << "there are multi delete, count = " << *count << ", nPendingAdd = " << nPendingAdd_ @@ -111,13 +113,19 @@ void DentryVector::Confirm(uint64_t *count) { *count = *count + nPendingAdd_ - nPendingDel_; } -DentryList::DentryList(std::vector *list, uint32_t limit, - const std::string &exclude, uint64_t maxTxId, +DentryList::DentryList(std::vector* list, + uint32_t limit, + const std::string& exclude, + uint64_t maxTxId, bool onlyDir) - : list_(list), size_(0), limit_(limit), exclude_(exclude), - maxTxId_(maxTxId), onlyDir_(onlyDir) {} - -void DentryList::PushBack(DentryVec *vec) { + : list_(list), + size_(0), + limit_(limit), + exclude_(exclude), + maxTxId_(maxTxId), + onlyDir_(onlyDir) {} + +void DentryList::PushBack(DentryVec* vec) { // NOTE: it's a cheap operation becacuse the size of // dentryVec must less than 2 BTree dentrys; @@ -145,9 +153,22 @@ void DentryList::PushBack(DentryVec *vec) { VLOG(9) << "Push dentry, dentry = (" << last->ShortDebugString() << ")"; } -uint32_t DentryList::Size() { return size_; } +uint32_t DentryList::Size() { + return size_; +} -bool DentryList::IsFull() { return limit_ != 0 && size_ >= limit_; } +bool DentryList::IsFull() { + return limit_ != 0 && size_ >= limit_; +} + +bool DentryStorage::Init() { + auto s = GetDentryCount(&nDentry_); + if (s.ok() || s.IsNotFound()) { + s = GetHandleTxIndex(&handleTxIndex_); + return s.ok() || s.IsNotFound(); + } + return false; +} DentryStorage::DentryStorage(std::shared_ptr kvStorage, std::shared_ptr nameGenerator, @@ -161,19 +182,16 @@ DentryStorage::DentryStorage(std::shared_ptr kvStorage, // we cannot ignore `nDentry` argument // try get dentry count for rocksdb // if we got it, replace old value - GetDentryCount(&nDentry_); - - GetHandleTxIndex(&handleTxIndex_); } -std::string DentryStorage::DentryKey(const Dentry &dentry) { +std::string DentryStorage::DentryKey(const Dentry& dentry) { Key4Dentry key(dentry.fsid(), dentry.parentinodeid(), dentry.name()); return conv_.SerializeToString(key); } -bool DentryStorage::CompressDentry(storage::StorageTransaction *txn, - DentryVec *vec, BTree *dentrys, - std::uint64_t *outCount) { +bool DentryStorage::CompressDentry(storage::StorageTransaction* txn, + DentryVec* vec, BTree* dentrys, + std::uint64_t* outCount) { DentryVector vector(vec); std::vector deleted; if (dentrys->size() == 2) { @@ -182,7 +200,7 @@ bool DentryStorage::CompressDentry(storage::StorageTransaction *txn, if (HasDeleteMarkFlag(*dentrys->rbegin())) { deleted.push_back(*dentrys->rbegin()); } - for (const auto &dentry : deleted) { + for (const auto& dentry : deleted) { vector.Delete(dentry); } Status s; @@ -215,7 +233,7 @@ bool DentryStorage::CompressDentry(storage::StorageTransaction *txn, return true; } -bool DentryStorage::CompressDentry(DentryVec *vec, BTree *dentrys) { +bool DentryStorage::CompressDentry(DentryVec* vec, BTree* dentrys) { DentryVector vector(vec); std::vector deleted; if (dentrys->size() == 2) { @@ -224,7 +242,7 @@ bool DentryStorage::CompressDentry(DentryVec *vec, BTree *dentrys) { if (HasDeleteMarkFlag(*dentrys->rbegin())) { deleted.push_back(*dentrys->rbegin()); } - for (const auto &dentry : deleted) { + for (const auto& dentry : deleted) { vector.Delete(dentry); } @@ -245,8 +263,8 @@ bool DentryStorage::CompressDentry(DentryVec *vec, BTree *dentrys) { // NOTE: Find() return the dentry which has the latest txid, // and it will clean the old txid's dentry if you specify compress to true -MetaStatusCode DentryStorage::Find(const Dentry &in, Dentry *out, - DentryVec *vec) { +MetaStatusCode DentryStorage::Find(const Dentry& in, Dentry* out, + DentryVec* vec) { std::string skey = DentryKey(in); Status s = kvStorage_->SGet(table4Dentry_, skey, vec); if (s.IsNotFound()) { @@ -282,10 +300,10 @@ MetaStatusCode DentryStorage::Find(const Dentry &in, Dentry *out, // and it will clean the old txid's dentry if you specify compressOutCount to // non-nullptr compressOutCount must point to a variable that value is equal // with `nDentry_` -MetaStatusCode DentryStorage::Find(storage::StorageTransaction *txn, - const Dentry &in, Dentry *out, - DentryVec *vec, - std::uint64_t *compressOutCount) { +MetaStatusCode DentryStorage::Find(storage::StorageTransaction* txn, + const Dentry& in, Dentry* out, + DentryVec* vec, + std::uint64_t* compressOutCount) { std::string skey = DentryKey(in); Status s = txn->SGet(table4Dentry_, skey, vec); if (s.IsNotFound()) { @@ -321,18 +339,17 @@ MetaStatusCode DentryStorage::Find(storage::StorageTransaction *txn, return rc; } -bool DentryStorage::GetAppliedIndex(std::int64_t *index) { +storage::Status DentryStorage::GetAppliedIndex(std::int64_t* index) { common::AppliedIndex val; auto s = kvStorage_->SGet(table4AppliedIndex_, "dentry", &val); if (s.ok()) { *index = val.index(); - return true; } - return false; + return s; } storage::Status -DentryStorage::SetAppliedIndex(storage::StorageTransaction *transaction, +DentryStorage::SetAppliedIndex(storage::StorageTransaction* transaction, std::int64_t index) { common::AppliedIndex val; val.set_index(index); @@ -340,12 +357,12 @@ DentryStorage::SetAppliedIndex(storage::StorageTransaction *transaction, } storage::Status -DentryStorage::DelAppliedIndex(storage::StorageTransaction *transaction) { +DentryStorage::DelAppliedIndex(storage::StorageTransaction* transaction) { return transaction->SDel(table4AppliedIndex_, "dentry"); } storage::Status -DentryStorage::SetHandleTxIndex(storage::StorageTransaction *transaction, +DentryStorage::SetHandleTxIndex(storage::StorageTransaction* transaction, std::int64_t index) { common::AppliedIndex val; val.set_index(index); @@ -353,34 +370,34 @@ DentryStorage::SetHandleTxIndex(storage::StorageTransaction *transaction, } storage::Status -DentryStorage::DelHandleTxIndex(storage::StorageTransaction *transaction) { +DentryStorage::DelHandleTxIndex(storage::StorageTransaction* transaction) { return transaction->SDel(table4AppliedIndex_, "handleTx"); } -bool DentryStorage::GetPendingTx(metaserver::TransactionRequest *request) { - auto s = kvStorage_->SGet(table4Transaction_, "transaction", request); - return s.ok(); +storage::Status +DentryStorage::GetPendingTx(metaserver::TransactionRequest* request) { + return kvStorage_->SGet(table4Transaction_, "transaction", request); } storage::Status -DentryStorage::SetPendingTx(storage::StorageTransaction *txn, - const metaserver::TransactionRequest &request) { +DentryStorage::SetPendingTx(storage::StorageTransaction* txn, + const metaserver::TransactionRequest& request) { return txn->SSet(table4Transaction_, "transaction", request); } -storage::Status DentryStorage::DelPendingTx(storage::StorageTransaction *txn) { +storage::Status DentryStorage::DelPendingTx(storage::StorageTransaction* txn) { return txn->SDel(table4Transaction_, "transaction"); } storage::Status -DentryStorage::ClearPendingTx(storage::StorageTransaction *txn) { +DentryStorage::ClearPendingTx(storage::StorageTransaction* txn) { metaserver::TransactionRequest request; request.set_type(static_cast(TxType::None)); request.set_rawpayload(""); return txn->SSet(table4Transaction_, "transaction", request); } -storage::Status DentryStorage::SetDentryCount(storage::StorageTransaction *txn, +storage::Status DentryStorage::SetDentryCount(storage::StorageTransaction* txn, std::uint64_t count) { common::ItemCount val; val.set_count(count); @@ -388,31 +405,29 @@ storage::Status DentryStorage::SetDentryCount(storage::StorageTransaction *txn, } storage::Status -DentryStorage::DelDentryCount(storage::StorageTransaction *txn) { +DentryStorage::DelDentryCount(storage::StorageTransaction* txn) { return txn->SDel(table4DentryCount_, "count"); } -bool DentryStorage::GetDentryCount(std::uint64_t *count) { +storage::Status DentryStorage::GetDentryCount(std::uint64_t* count) { common::ItemCount val; auto s = kvStorage_->SGet(table4DentryCount_, "count", &val); if (s.ok()) { *count = val.count(); - return true; } - return false; + return s; } -bool DentryStorage::GetHandleTxIndex(std::int64_t *index) { +storage::Status DentryStorage::GetHandleTxIndex(std::int64_t* index) { common::AppliedIndex val; auto s = kvStorage_->SGet(table4AppliedIndex_, "handleTx", &val); if (s.ok()) { *index = val.index(); - return true; } - return false; + return s; } -MetaStatusCode DentryStorage::Insert(const Dentry &dentry, +MetaStatusCode DentryStorage::Insert(const Dentry& dentry, std::int64_t logIndex) { WriteLockGuard lg(rwLock_); @@ -503,7 +518,7 @@ MetaStatusCode DentryStorage::Insert(const Dentry &dentry, return MetaStatusCode::OK; } -MetaStatusCode DentryStorage::Insert(const DentryVec &vec, bool merge, +MetaStatusCode DentryStorage::Insert(const DentryVec& vec, bool merge, std::int64_t logIndex) { WriteLockGuard lg(rwLock_); @@ -572,7 +587,7 @@ MetaStatusCode DentryStorage::Insert(const DentryVec &vec, bool merge, return MetaStatusCode::OK; } -MetaStatusCode DentryStorage::Delete(const Dentry &dentry, +MetaStatusCode DentryStorage::Delete(const Dentry& dentry, std::int64_t logIndex) { WriteLockGuard lg(rwLock_); @@ -660,7 +675,7 @@ MetaStatusCode DentryStorage::Delete(const Dentry &dentry, return MetaStatusCode::OK; } -MetaStatusCode DentryStorage::Get(Dentry *dentry) { +MetaStatusCode DentryStorage::Get(Dentry* dentry) { ReadLockGuard lg(rwLock_); Dentry out; @@ -677,8 +692,8 @@ MetaStatusCode DentryStorage::Get(Dentry *dentry) { return MetaStatusCode::OK; } -MetaStatusCode DentryStorage::List(const Dentry &dentry, - std::vector *dentrys, uint32_t limit, +MetaStatusCode DentryStorage::List(const Dentry& dentry, + std::vector* dentrys, uint32_t limit, bool onlyDir) { // TODO(all): consider store dir dentry and file dentry separately ReadLockGuard lg(rwLock_); @@ -737,8 +752,8 @@ MetaStatusCode DentryStorage::List(const Dentry &dentry, } MetaStatusCode -DentryStorage::PrepareTx(const std::vector &dentrys, - const metaserver::TransactionRequest &txRequest, +DentryStorage::PrepareTx(const std::vector& dentrys, + const metaserver::TransactionRequest& txRequest, std::int64_t logIndex) { WriteLockGuard lg(rwLock_); std::uint64_t count = nDentry_; @@ -748,7 +763,7 @@ DentryStorage::PrepareTx(const std::vector &dentrys, LOG(ERROR) << "Begin transaction failed"; return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - for (const auto &dentry : dentrys) { + for (const auto& dentry : dentrys) { DentryVec vec; DentryVector vector(&vec); std::string skey = DentryKey(dentry); @@ -801,7 +816,7 @@ DentryStorage::PrepareTx(const std::vector &dentrys, return MetaStatusCode::OK; } -MetaStatusCode DentryStorage::CommitTx(const std::vector &dentrys, +MetaStatusCode DentryStorage::CommitTx(const std::vector& dentrys, std::int64_t logIndex) { if (logIndex <= handleTxIndex_) { // NOTE: if we enter here @@ -823,7 +838,7 @@ MetaStatusCode DentryStorage::CommitTx(const std::vector &dentrys, return MetaStatusCode::STORAGE_INTERNAL_ERROR; } std::uint64_t count = nDentry_; - for (const auto &dentry : dentrys) { + for (const auto& dentry : dentrys) { Dentry out; DentryVec vec; std::string skey = DentryKey(dentry); @@ -866,7 +881,7 @@ MetaStatusCode DentryStorage::CommitTx(const std::vector &dentrys, return MetaStatusCode::OK; } -MetaStatusCode DentryStorage::RollbackTx(const std::vector &dentrys, +MetaStatusCode DentryStorage::RollbackTx(const std::vector& dentrys, std::int64_t logIndex) { if (logIndex <= handleTxIndex_) { // NOTE: if we enter here @@ -888,7 +903,7 @@ MetaStatusCode DentryStorage::RollbackTx(const std::vector &dentrys, return MetaStatusCode::STORAGE_INTERNAL_ERROR; } std::uint64_t count = nDentry_; - for (const auto &dentry : dentrys) { + for (const auto& dentry : dentrys) { DentryVec vec; DentryVector vector(&vec); std::string skey = DentryKey(dentry); diff --git a/curvefs/src/metaserver/dentry_storage.h b/curvefs/src/metaserver/dentry_storage.h index 80052e737c..fc8dd04a63 100644 --- a/curvefs/src/metaserver/dentry_storage.h +++ b/curvefs/src/metaserver/dentry_storage.h @@ -46,49 +46,49 @@ using ::curvefs::metaserver::storage::NameGenerator; using KVStorage = ::curvefs::metaserver::storage::KVStorage; using BTree = absl::btree_set; -#define EQUAL(a) (lhs.a() == rhs.a()) -#define LESS(a) (lhs.a() < rhs.a()) -#define LESS2(a, b) (EQUAL(a) && LESS(b)) -#define LESS3(a, b, c) (EQUAL(a) && LESS2(b, c)) +#define EQUAL(a) (lhs.a() == rhs.a()) +#define LESS(a) (lhs.a() < rhs.a()) +#define LESS2(a, b) (EQUAL(a) && LESS(b)) +#define LESS3(a, b, c) (EQUAL(a) && LESS2(b, c)) #define LESS4(a, b, c, d) (EQUAL(a) && LESS3(b, c, d)) -bool operator==(const Dentry &lhs, const Dentry &rhs); +bool operator==(const Dentry& lhs, const Dentry& rhs); -bool operator<(const Dentry &lhs, const Dentry &rhs); +bool operator<(const Dentry& lhs, const Dentry& rhs); class DentryVector { public: - explicit DentryVector(DentryVec *vec); + explicit DentryVector(DentryVec* vec); - void Insert(const Dentry &dentry); + void Insert(const Dentry& dentry); - void Delete(const Dentry &dentry); + void Delete(const Dentry& dentry); - void Merge(const DentryVec &src); + void Merge(const DentryVec& src); - void Filter(uint64_t maxTxId, BTree *btree); + void Filter(uint64_t maxTxId, BTree* btree); - void Confirm(uint64_t *count); + void Confirm(uint64_t* count); private: - DentryVec *vec_; + DentryVec* vec_; uint64_t nPendingAdd_; uint64_t nPendingDel_; }; class DentryList { public: - DentryList(std::vector *list, uint32_t limit, - const std::string &exclude, uint64_t maxTxId, bool onlyDir); + DentryList(std::vector* list, uint32_t limit, + const std::string& exclude, uint64_t maxTxId, bool onlyDir); - void PushBack(DentryVec *vec); + void PushBack(DentryVec* vec); uint32_t Size(); bool IsFull(); private: - std::vector *list_; + std::vector* list_; uint32_t size_; uint32_t limit_; std::string exclude_; @@ -98,33 +98,33 @@ class DentryList { class DentryStorage { public: - bool GetAppliedIndex(std::int64_t *index); - DentryStorage(std::shared_ptr kvStorage, std::shared_ptr nameGenerator, uint64_t nDentry); - MetaStatusCode Insert(const Dentry &dentry, std::int64_t logIndex); + bool Init(); + + MetaStatusCode Insert(const Dentry& dentry, std::int64_t logIndex); // only for loadding from snapshot - MetaStatusCode Insert(const DentryVec &vec, bool merge, + MetaStatusCode Insert(const DentryVec& vec, bool merge, std::int64_t logIndex); - MetaStatusCode Delete(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode Delete(const Dentry& dentry, std::int64_t logIndex); - MetaStatusCode Get(Dentry *dentry); + MetaStatusCode Get(Dentry* dentry); - MetaStatusCode List(const Dentry &dentry, std::vector *dentrys, + MetaStatusCode List(const Dentry& dentry, std::vector* dentrys, uint32_t limit, bool onlyDir = false); - MetaStatusCode PrepareTx(const std::vector &dentrys, - const metaserver::TransactionRequest &txRequest, + MetaStatusCode PrepareTx(const std::vector& dentrys, + const metaserver::TransactionRequest& txRequest, std::int64_t logIndex); - MetaStatusCode CommitTx(const std::vector &dentrys, + MetaStatusCode CommitTx(const std::vector& dentrys, std::int64_t logIndex); - MetaStatusCode RollbackTx(const std::vector &dentrys, + MetaStatusCode RollbackTx(const std::vector& dentrys, std::int64_t logIndex); std::shared_ptr GetAll(); @@ -135,47 +135,49 @@ class DentryStorage { MetaStatusCode Clear(); - bool GetPendingTx(metaserver::TransactionRequest *request); + storage::Status GetPendingTx(metaserver::TransactionRequest* request); + + storage::Status GetAppliedIndex(std::int64_t* index); private: - std::string DentryKey(const Dentry &entry); + std::string DentryKey(const Dentry& entry); - bool CompressDentry(DentryVec *vec, BTree *dentrys); + bool CompressDentry(DentryVec* vec, BTree* dentrys); - bool CompressDentry(storage::StorageTransaction *txn, DentryVec *vec, - BTree *dentrys, std::uint64_t *outCount); + bool CompressDentry(storage::StorageTransaction* txn, DentryVec* vec, + BTree* dentrys, std::uint64_t* outCount); - MetaStatusCode Find(const Dentry &in, Dentry *out, DentryVec *vec); + MetaStatusCode Find(const Dentry& in, Dentry* out, DentryVec* vec); - MetaStatusCode Find(storage::StorageTransaction *txn, const Dentry &in, - Dentry *out, DentryVec *vec, - std::uint64_t *compressOutCount); + MetaStatusCode Find(storage::StorageTransaction* txn, const Dentry& in, + Dentry* out, DentryVec* vec, + std::uint64_t* compressOutCount); - storage::Status SetAppliedIndex(storage::StorageTransaction *transaction, + storage::Status SetAppliedIndex(storage::StorageTransaction* transaction, std::int64_t index); - storage::Status DelAppliedIndex(storage::StorageTransaction *transaction); + storage::Status DelAppliedIndex(storage::StorageTransaction* transaction); - storage::Status SetHandleTxIndex(storage::StorageTransaction *transaction, + storage::Status SetHandleTxIndex(storage::StorageTransaction* transaction, std::int64_t index); - storage::Status DelHandleTxIndex(storage::StorageTransaction *transaction); + storage::Status DelHandleTxIndex(storage::StorageTransaction* transaction); - storage::Status SetPendingTx(storage::StorageTransaction *transaction, - const metaserver::TransactionRequest &request); + storage::Status SetPendingTx(storage::StorageTransaction* transaction, + const metaserver::TransactionRequest& request); - storage::Status DelPendingTx(storage::StorageTransaction *transaction); + storage::Status DelPendingTx(storage::StorageTransaction* transaction); - storage::Status ClearPendingTx(storage::StorageTransaction *transaction); + storage::Status ClearPendingTx(storage::StorageTransaction* transaction); - storage::Status SetDentryCount(storage::StorageTransaction *transaction, + storage::Status SetDentryCount(storage::StorageTransaction* transaction, std::uint64_t count); - storage::Status DelDentryCount(storage::StorageTransaction *transaction); + storage::Status DelDentryCount(storage::StorageTransaction* transaction); - bool GetDentryCount(std::uint64_t *count); + storage::Status GetDentryCount(std::uint64_t* count); - bool GetHandleTxIndex(std::int64_t *count); + storage::Status GetHandleTxIndex(std::int64_t* count); private: RWLock rwLock_; diff --git a/curvefs/src/metaserver/inode_manager.cpp b/curvefs/src/metaserver/inode_manager.cpp index 5308923301..68d5556ad8 100644 --- a/curvefs/src/metaserver/inode_manager.cpp +++ b/curvefs/src/metaserver/inode_manager.cpp @@ -43,6 +43,14 @@ using ::google::protobuf::util::MessageDifferencer; namespace curvefs { namespace metaserver { +bool InodeManager::Init() { + if (inodeStorage_->Init()) { + auto s = inodeStorage_->GetAppliedIndex(&appliedIndex_); + return s.ok() || s.IsNotFound(); + } + return false; +} + MetaStatusCode InodeManager::CreateInode(uint64_t inodeId, const InodeParam ¶m, Inode *newInode, @@ -326,10 +334,10 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest &request, bool needUpdate = false; bool needAddTrash = false; -#define UPDATE_INODE(param) \ - if (request.has_##param()) { \ - old.set_##param(request.param()); \ - needUpdate = true; \ +#define UPDATE_INODE(param) \ + if (request.has_##param()) { \ + old.set_##param(request.param()); \ + needUpdate = true; \ } UPDATE_INODE(length) diff --git a/curvefs/src/metaserver/inode_manager.h b/curvefs/src/metaserver/inode_manager.h index da70c73e4f..7276157084 100644 --- a/curvefs/src/metaserver/inode_manager.h +++ b/curvefs/src/metaserver/inode_manager.h @@ -68,9 +68,10 @@ class InodeManager { : inodeStorage_(inodeStorage), trash_(trash), type2InodeNum_(type2InodeNum), appliedIndex_(-1) { // for compatibility, we initialize applied index to -1 - inodeStorage_->GetAppliedIndex(&appliedIndex_); } + bool Init(); + MetaStatusCode CreateInode(uint64_t inodeId, const InodeParam ¶m, Inode *inode, std::int64_t logIndex); MetaStatusCode CreateRootInode(const InodeParam ¶m, diff --git a/curvefs/src/metaserver/inode_storage.cpp b/curvefs/src/metaserver/inode_storage.cpp index 949eb611c2..a385ec848b 100644 --- a/curvefs/src/metaserver/inode_storage.cpp +++ b/curvefs/src/metaserver/inode_storage.cpp @@ -74,34 +74,37 @@ InodeStorage::InodeStorage(std::shared_ptr kvStorage, nInode_(nInode), conv_() { // NOTE: for compatibility with older versions // we cannot ignore `nInode` argument +} + +bool InodeStorage::Init() { // try get inode count for rocksdb // if we got it, replace old value - GetInodeCount(&nInode_); + auto s = GetInodeCount(&nInode_); + return s.ok() || s.IsNotFound(); } -bool InodeStorage::GetInodeCount(std::size_t *count) { +storage::Status InodeStorage::GetInodeCount(std::size_t* count) { common::ItemCount val; auto s = kvStorage_->SGet(table4InodeCount_, "count", &val); if (s.ok()) { *count = static_cast(val.count()); - return true; } - return false; + return s; } -storage::Status InodeStorage::SetInodeCount(storage::StorageTransaction *txn, +storage::Status InodeStorage::SetInodeCount(storage::StorageTransaction* txn, std::size_t count) { common::ItemCount val; val.set_count(count); return txn->SSet(table4InodeCount_, "count", val); } -storage::Status InodeStorage::DelInodeCount(storage::StorageTransaction *txn) { +storage::Status InodeStorage::DelInodeCount(storage::StorageTransaction* txn) { return txn->SDel(table4InodeCount_, "count"); } storage::Status -InodeStorage::SetAppliedIndex(storage::StorageTransaction *transaction, +InodeStorage::SetAppliedIndex(storage::StorageTransaction* transaction, std::int64_t index) { common::AppliedIndex val; val.set_index(index); @@ -109,21 +112,20 @@ InodeStorage::SetAppliedIndex(storage::StorageTransaction *transaction, } storage::Status -InodeStorage::DelAppliedIndex(storage::StorageTransaction *transaction) { +InodeStorage::DelAppliedIndex(storage::StorageTransaction* transaction) { return transaction->SDel(table4AppliedIndex_, "inode"); } -bool InodeStorage::GetAppliedIndex(std::int64_t *index) { +storage::Status InodeStorage::GetAppliedIndex(std::int64_t* index) { common::AppliedIndex val; auto s = kvStorage_->SGet(table4AppliedIndex_, "inode", &val); if (s.ok()) { *index = val.index(); - return true; } - return false; + return s; } -MetaStatusCode InodeStorage::Insert(const Inode &inode, std::int64_t logIndex) { +MetaStatusCode InodeStorage::Insert(const Inode& inode, std::int64_t logIndex) { WriteLockGuard lg(rwLock_); Key4Inode key(inode.fsid(), inode.inodeid()); std::string skey = conv_.SerializeToString(key); @@ -180,7 +182,7 @@ MetaStatusCode InodeStorage::Insert(const Inode &inode, std::int64_t logIndex) { return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode InodeStorage::Get(const Key4Inode &key, Inode *inode) { +MetaStatusCode InodeStorage::Get(const Key4Inode& key, Inode* inode) { ReadLockGuard lg(rwLock_); std::string skey = conv_.SerializeToString(key); Status s = kvStorage_->HGet(table4Inode_, skey, inode); @@ -196,7 +198,7 @@ MetaStatusCode InodeStorage::Get(const Key4Inode &key, Inode *inode) { return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode InodeStorage::GetAttr(const Key4Inode &key, InodeAttr *attr) { +MetaStatusCode InodeStorage::GetAttr(const Key4Inode& key, InodeAttr* attr) { ReadLockGuard lg(rwLock_); Inode inode; std::string skey = conv_.SerializeToString(key); @@ -238,7 +240,7 @@ MetaStatusCode InodeStorage::GetAttr(const Key4Inode &key, InodeAttr *attr) { return MetaStatusCode::OK; } -MetaStatusCode InodeStorage::GetXAttr(const Key4Inode &key, XAttr *xattr) { +MetaStatusCode InodeStorage::GetXAttr(const Key4Inode& key, XAttr* xattr) { ReadLockGuard lg(rwLock_); Inode inode; std::string skey = conv_.SerializeToString(key); @@ -255,7 +257,7 @@ MetaStatusCode InodeStorage::GetXAttr(const Key4Inode &key, XAttr *xattr) { return MetaStatusCode::OK; } -MetaStatusCode InodeStorage::ForceDelete(const Key4Inode &key) { +MetaStatusCode InodeStorage::ForceDelete(const Key4Inode& key) { WriteLockGuard lg(rwLock_); std::string skey = conv_.SerializeToString(key); std::shared_ptr txn = @@ -304,7 +306,7 @@ MetaStatusCode InodeStorage::ForceDelete(const Key4Inode &key) { return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode InodeStorage::Delete(const Key4Inode &key, +MetaStatusCode InodeStorage::Delete(const Key4Inode& key, std::int64_t logIndex) { WriteLockGuard lg(rwLock_); std::string skey = conv_.SerializeToString(key); @@ -364,8 +366,8 @@ MetaStatusCode InodeStorage::Delete(const Key4Inode &key, } MetaStatusCode -InodeStorage::Update(std::shared_ptr *txn, - const Inode &inode, std::int64_t logIndex, +InodeStorage::Update(std::shared_ptr* txn, + const Inode& inode, std::int64_t logIndex, bool inodeDeallocate) { if (*txn == nullptr) { *txn = kvStorage_->BeginTransaction(); @@ -406,7 +408,7 @@ InodeStorage::Update(std::shared_ptr *txn, return MetaStatusCode::OK; } -MetaStatusCode InodeStorage::Update(const Inode &inode, std::int64_t logIndex, +MetaStatusCode InodeStorage::Update(const Inode& inode, std::int64_t logIndex, bool inodeDeallocate) { std::shared_ptr txn; if (Update(&txn, inode, logIndex, inodeDeallocate) == MetaStatusCode::OK) { @@ -432,7 +434,7 @@ std::shared_ptr InodeStorage::GetAllInode() { return kvStorage_->HGetAll(table4Inode_); } -bool InodeStorage::GetAllInodeId(std::list *ids) { +bool InodeStorage::GetAllInodeId(std::list* ids) { ReadLockGuard lg(rwLock_); auto iterator = GetAllInode(); if (iterator->Status() != 0) { @@ -598,9 +600,11 @@ uint64_t InodeStorage::GetInodeS3MetaSize(uint32_t fsId, uint64_t inodeId) { } MetaStatusCode -InodeStorage::AddS3ChunkInfoList(Transaction txn, uint32_t fsId, - uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2add) { +InodeStorage::AddS3ChunkInfoList(Transaction txn, + uint32_t fsId, + uint64_t inodeId, + uint64_t chunkIndex, + const S3ChunkInfoList* list2add) { if (nullptr == list2add || list2add->s3chunks_size() == 0) { return MetaStatusCode::OK; } @@ -609,8 +613,8 @@ InodeStorage::AddS3ChunkInfoList(Transaction txn, uint32_t fsId, uint64_t firstChunkId = list2add->s3chunks(0).chunkid(); uint64_t lastChunkId = list2add->s3chunks(size - 1).chunkid(); - Key4S3ChunkInfoList key(fsId, inodeId, chunkIndex, firstChunkId, - lastChunkId, size); + Key4S3ChunkInfoList key(fsId, inodeId, chunkIndex, + firstChunkId, lastChunkId, size); std::string skey = conv_.SerializeToString(key); Status s; if (txn) { @@ -624,7 +628,7 @@ InodeStorage::AddS3ChunkInfoList(Transaction txn, uint32_t fsId, MetaStatusCode InodeStorage::DelS3ChunkInfoList(Transaction txn, uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2del) { + const S3ChunkInfoList* list2del) { if (nullptr == list2del || list2del->s3chunks_size() == 0) { return MetaStatusCode::OK; } @@ -662,13 +666,13 @@ InodeStorage::DelS3ChunkInfoList(Transaction txn, uint32_t fsId, } else if (delLastChunkId < key.firstChunkId) { continue; } else { - LOG(ERROR) << "wrong delete list range (" << delFirstChunkId << "," - << delLastChunkId << "), skey=" << skey; + LOG(ERROR) << "wrong delete list range (" << delFirstChunkId + << "," << delLastChunkId << "), skey=" << skey; return MetaStatusCode::STORAGE_INTERNAL_ERROR; } } - for (const auto &skey : key2del) { + for (const auto& skey : key2del) { if (!txn->SDel(table4S3ChunkInfo_, skey).ok()) { LOG(ERROR) << "Delete key failed, skey=" << skey; return MetaStatusCode::STORAGE_INTERNAL_ERROR; @@ -678,9 +682,13 @@ InodeStorage::DelS3ChunkInfoList(Transaction txn, uint32_t fsId, } MetaStatusCode InodeStorage::ModifyInodeS3ChunkInfoList( - std::shared_ptr *txn, uint32_t fsId, uint64_t inodeId, - uint64_t chunkIndex, const S3ChunkInfoList *list2add, - const S3ChunkInfoList *list2del, std::int64_t logIndex) { + std::shared_ptr* txn, + uint32_t fsId, + uint64_t inodeId, + uint64_t chunkIndex, + const S3ChunkInfoList* list2add, + const S3ChunkInfoList* list2del, + std::int64_t logIndex) { if (*txn == nullptr) { *txn = kvStorage_->BeginTransaction(); if (*txn == nullptr) { @@ -721,7 +729,7 @@ MetaStatusCode InodeStorage::ModifyInodeS3ChunkInfoList( MetaStatusCode InodeStorage::ModifyInodeS3ChunkInfoList( uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2add, const S3ChunkInfoList *list2del, + const S3ChunkInfoList* list2add, const S3ChunkInfoList* list2del, std::int64_t logIndex) { std::shared_ptr txn; MetaStatusCode rc = ModifyInodeS3ChunkInfoList( @@ -741,7 +749,7 @@ MetaStatusCode InodeStorage::ModifyInodeS3ChunkInfoList( MetaStatusCode InodeStorage::PaddingInodeS3ChunkInfo(int32_t fsId, uint64_t inodeId, - S3ChunkInfoMap *m, + S3ChunkInfoMap* m, uint64_t limit) { ReadLockGuard lg(rwLock_); if (limit != 0 && GetInodeS3MetaSize(fsId, inodeId) > limit) { @@ -754,7 +762,7 @@ MetaStatusCode InodeStorage::PaddingInodeS3ChunkInfo(int32_t fsId, return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - auto merge = [](const S3ChunkInfoList &from, S3ChunkInfoList *to) { + auto merge = [](const S3ChunkInfoList& from, S3ChunkInfoList* to) { for (int i = 0; i < from.s3chunks_size(); i++) { auto chunkinfo = to->add_s3chunks(); chunkinfo->CopyFrom(from.s3chunks(i)); @@ -803,8 +811,8 @@ std::shared_ptr InodeStorage::GetAllVolumeExtentList() { } MetaStatusCode InodeStorage::UpdateVolumeExtentSlice( - std::shared_ptr *txn, uint32_t fsId, - uint64_t inodeId, const VolumeExtentSlice &slice, std::int64_t logIndex) { + std::shared_ptr* txn, uint32_t fsId, + uint64_t inodeId, const VolumeExtentSlice& slice, std::int64_t logIndex) { WriteLockGuard guard(rwLock_); if (*txn == nullptr) { *txn = kvStorage_->BeginTransaction(); @@ -833,7 +841,7 @@ MetaStatusCode InodeStorage::UpdateVolumeExtentSlice( MetaStatusCode InodeStorage::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, + const VolumeExtentSlice& slice, std::int64_t logIndex) { std::shared_ptr txn; auto rc = UpdateVolumeExtentSlice(&txn, fsId, inodeId, slice, logIndex); @@ -856,13 +864,13 @@ InodeStorage::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, MetaStatusCode InodeStorage::GetAllVolumeExtent(uint32_t fsId, uint64_t inodeId, - VolumeExtentSliceList *extents) { + VolumeExtentSliceList* extents) { ReadLockGuard guard(rwLock_); auto key = conv_.SerializeToString(Prefix4InodeVolumeExtent{fsId, inodeId}); auto iter = kvStorage_->SSeek(table4VolumeExtent_, key); for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { - auto *slice = extents->add_slices(); + auto* slice = extents->add_slices(); if (iter->RawValue()) { slice->CopyFrom(*iter->RawValue()); continue; @@ -889,7 +897,7 @@ std::shared_ptr InodeStorage::GetAllVolumeExtent(uint32_t fsId, MetaStatusCode InodeStorage::GetVolumeExtentByOffset(uint32_t fsId, uint64_t inodeId, uint64_t offset, - VolumeExtentSlice *slice) { + VolumeExtentSlice* slice) { ReadLockGuard guard(RWLock); auto key = conv_.SerializeToString(Key4VolumeExtentSlice{fsId, inodeId, offset}); @@ -906,7 +914,7 @@ MetaStatusCode InodeStorage::GetVolumeExtentByOffset(uint32_t fsId, } MetaStatusCode InodeStorage::GetAllBlockGroup( - std::vector *deallocatableBlockGroupVec) { + std::vector* deallocatableBlockGroupVec) { auto iter = kvStorage_->HGetAll(table4DeallocatableBlockGroup_); if (iter->Status() != 0) { LOG(ERROR) << "InodeStorage failed to get iterator for all " @@ -932,14 +940,14 @@ MetaStatusCode InodeStorage::GetAllBlockGroup( } MetaStatusCode InodeStorage::UpdateDeallocatableBlockGroup( - uint32_t fsId, const DeallocatableBlockGroupVec &update, + uint32_t fsId, const DeallocatableBlockGroupVec& update, std::int64_t logIndex) { auto txn = kvStorage_->BeginTransaction(); MetaStatusCode st = MetaStatusCode::OK; std::string step; - for (auto &item : update) { + for (auto& item : update) { Key4DeallocatableBlockGroup key(fsId, item.blockgroupoffset()); std::string skey(key.SerializeToString()); @@ -1001,8 +1009,8 @@ MetaStatusCode InodeStorage::UpdateDeallocatableBlockGroup( MetaStatusCode InodeStorage::Increase(Transaction txn, uint32_t fsId, - const IncreaseDeallocatableBlockGroup &increase, - DeallocatableBlockGroup *out) { + const IncreaseDeallocatableBlockGroup& increase, + DeallocatableBlockGroup* out) { MetaStatusCode st = MetaStatusCode::OK; // update DeallocatableBlockGroup @@ -1015,14 +1023,14 @@ InodeStorage::Increase(Transaction txn, uint32_t fsId, std::set unique_elements(out->inodeidlist().begin(), out->inodeidlist().end()); out->mutable_inodeidlist()->Clear(); - for (auto &elem : unique_elements) { + for (auto& elem : unique_elements) { out->mutable_inodeidlist()->Add(elem); } VLOG(6) << "InodeStorage handle increase set out=" << out->DebugString(); // remove related inode in table4DeallocatableInode_ - for (auto &inodeid : increase.inodeidlistadd()) { + for (auto& inodeid : increase.inodeidlistadd()) { auto s = txn->HDel(table4DeallocatableInode_, conv_.SerializeToString(Key4Inode{fsId, inodeid})); if (!s.ok()) { @@ -1040,8 +1048,8 @@ InodeStorage::Increase(Transaction txn, uint32_t fsId, } MetaStatusCode -InodeStorage::Decrease(const DecreaseDeallocatableBlockGroup &decrease, - DeallocatableBlockGroup *out) { +InodeStorage::Decrease(const DecreaseDeallocatableBlockGroup& decrease, + DeallocatableBlockGroup* out) { VLOG(6) << "InodeStorage handle increase=" << decrease.DebugString(); if (!out->IsInitialized() || !out->has_deallocatablesize()) { LOG(ERROR) @@ -1078,8 +1086,8 @@ InodeStorage::Decrease(const DecreaseDeallocatableBlockGroup &decrease, return MetaStatusCode::OK; } -MetaStatusCode InodeStorage::Mark(const MarkDeallocatableBlockGroup &mark, - DeallocatableBlockGroup *out) { +MetaStatusCode InodeStorage::Mark(const MarkDeallocatableBlockGroup& mark, + DeallocatableBlockGroup* out) { MetaStatusCode st = MetaStatusCode::OK; VLOG(6) << "InodeStorage handle mark=" << mark.DebugString(); diff --git a/curvefs/src/metaserver/inode_storage.h b/curvefs/src/metaserver/inode_storage.h index afcb3ddcf7..4f4f0c74bc 100644 --- a/curvefs/src/metaserver/inode_storage.h +++ b/curvefs/src/metaserver/inode_storage.h @@ -36,6 +36,7 @@ #include "absl/container/btree_set.h" #include "absl/container/btree_map.h" +#include "curvefs/src/metaserver/storage/status.h" #include "src/common/concurrent/rw_lock.h" #include "curvefs/proto/metaserver.pb.h" #include "curvefs/src/metaserver/storage/converter.h" @@ -63,7 +64,9 @@ class InodeStorage { InodeStorage(std::shared_ptr kvStorage, std::shared_ptr nameGenerator, uint64_t nInode); - bool GetAppliedIndex(std::int64_t *index); + storage::Status GetAppliedIndex(std::int64_t* index); + + bool Init(); /** * @brief insert inode to storage @@ -71,7 +74,7 @@ class InodeStorage { * @param[in] logIndex: the index of raft log * @return If inode exist, return INODE_EXIST; else insert and return OK */ - MetaStatusCode Insert(const Inode &inode, std::int64_t logIndex); + MetaStatusCode Insert(const Inode& inode, std::int64_t logIndex); /** * @brief get inode from storage @@ -79,7 +82,7 @@ class InodeStorage { * @param[out] inode: the inode got * @return If inode not exist, return NOT_FOUND; else return OK */ - MetaStatusCode Get(const Key4Inode &key, Inode *inode); + MetaStatusCode Get(const Key4Inode& key, Inode* inode); /** * @brief get inode attribute from storage @@ -87,7 +90,7 @@ class InodeStorage { * @param[out] attr: the inode attribute got * @return If inode not exist, return NOT_FOUND; else return OK */ - MetaStatusCode GetAttr(const Key4Inode &key, InodeAttr *attr); + MetaStatusCode GetAttr(const Key4Inode& key, InodeAttr* attr); /** * @brief get inode extended attributes from storage @@ -95,7 +98,7 @@ class InodeStorage { * @param[out] attr: the inode extended attribute got * @return If inode not exist, return NOT_FOUND; else return OK */ - MetaStatusCode GetXAttr(const Key4Inode &key, XAttr *xattr); + MetaStatusCode GetXAttr(const Key4Inode& key, XAttr* xattr); /** * @brief delete inode from storage @@ -103,9 +106,9 @@ class InodeStorage { * @param[in] logIndex: the index of raft log * @return If inode not exist, return NOT_FOUND; else return OK */ - MetaStatusCode Delete(const Key4Inode &key, std::int64_t logIndex); + MetaStatusCode Delete(const Key4Inode& key, std::int64_t logIndex); - MetaStatusCode ForceDelete(const Key4Inode &key); + MetaStatusCode ForceDelete(const Key4Inode& key); /** * @brief update inode from storage @@ -113,16 +116,16 @@ class InodeStorage { * @param[in] inodeDeallocate: Whether the inode needs to deallocate space * @return If inode not exist, return NOT_FOUND; else replace and return OK */ - MetaStatusCode Update(const Inode &inode, std::int64_t logIndex, + MetaStatusCode Update(const Inode& inode, std::int64_t logIndex, bool inodeDeallocate = false); - MetaStatusCode Update(std::shared_ptr *txn, - const Inode &inode, std::int64_t logIndex, + MetaStatusCode Update(std::shared_ptr* txn, + const Inode& inode, std::int64_t logIndex, bool inodeDeallocate = false); std::shared_ptr GetAllInode(); - bool GetAllInodeId(std::list *ids); + bool GetAllInodeId(std::list* ids); // NOTE: the return size is accurate under normal cluster status, // but under abnormal status, the return size maybe less than @@ -136,17 +139,17 @@ class InodeStorage { // s3chunkinfo MetaStatusCode ModifyInodeS3ChunkInfoList(uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2add, - const S3ChunkInfoList *list2del, + const S3ChunkInfoList* list2add, + const S3ChunkInfoList* list2del, std::int64_t logIndex); MetaStatusCode ModifyInodeS3ChunkInfoList( - std::shared_ptr *txn, uint32_t fsId, - uint64_t inodeId, uint64_t chunkIndex, const S3ChunkInfoList *list2add, - const S3ChunkInfoList *list2del, std::int64_t logIndex); + std::shared_ptr* txn, uint32_t fsId, + uint64_t inodeId, uint64_t chunkIndex, const S3ChunkInfoList* list2add, + const S3ChunkInfoList* list2del, std::int64_t logIndex); MetaStatusCode PaddingInodeS3ChunkInfo(int32_t fsId, uint64_t inodeId, - S3ChunkInfoMap *m, + S3ChunkInfoMap* m, uint64_t limit = 0); std::shared_ptr GetInodeS3ChunkInfoList(uint32_t fsId, @@ -158,34 +161,34 @@ class InodeStorage { std::shared_ptr GetAllVolumeExtentList(); MetaStatusCode UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, + const VolumeExtentSlice& slice, std::int64_t logIndex); MetaStatusCode - UpdateVolumeExtentSlice(std::shared_ptr *txn, + UpdateVolumeExtentSlice(std::shared_ptr* txn, uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, + const VolumeExtentSlice& slice, std::int64_t logIndex); MetaStatusCode GetAllVolumeExtent(uint32_t fsId, uint64_t inodeId, - VolumeExtentSliceList *extents); + VolumeExtentSliceList* extents); std::shared_ptr GetAllVolumeExtent(uint32_t fsId, uint64_t inodeId); MetaStatusCode GetVolumeExtentByOffset(uint32_t fsId, uint64_t inodeId, uint64_t offset, - VolumeExtentSlice *slice); + VolumeExtentSlice* slice); // use the transaction to delete {inodes} in the deallocatable_inode_list // and update the statistics of each item of blockgroup_list MetaStatusCode UpdateDeallocatableBlockGroup(uint32_t fsId, - const DeallocatableBlockGroupVec &update, + const DeallocatableBlockGroupVec& update, std::int64_t logIndex); MetaStatusCode GetAllBlockGroup( - std::vector *deallocatableBlockGroupVec); + std::vector* deallocatableBlockGroupVec); private: MetaStatusCode UpdateInodeS3MetaSize(Transaction txn, uint32_t fsId, @@ -196,33 +199,33 @@ class InodeStorage { MetaStatusCode DelS3ChunkInfoList(Transaction txn, uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2del); + const S3ChunkInfoList* list2del); MetaStatusCode AddS3ChunkInfoList(Transaction txn, uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2add); + const S3ChunkInfoList* list2add); MetaStatusCode Increase(Transaction txn, uint32_t fsId, - const IncreaseDeallocatableBlockGroup &increase, - DeallocatableBlockGroup *out); + const IncreaseDeallocatableBlockGroup& increase, + DeallocatableBlockGroup* out); - MetaStatusCode Decrease(const DecreaseDeallocatableBlockGroup &decrease, - DeallocatableBlockGroup *out); + MetaStatusCode Decrease(const DecreaseDeallocatableBlockGroup& decrease, + DeallocatableBlockGroup* out); - MetaStatusCode Mark(const MarkDeallocatableBlockGroup &mark, - DeallocatableBlockGroup *out); + MetaStatusCode Mark(const MarkDeallocatableBlockGroup& mark, + DeallocatableBlockGroup* out); - storage::Status SetAppliedIndex(storage::StorageTransaction *transaction, + storage::Status SetAppliedIndex(storage::StorageTransaction* transaction, std::int64_t index); - storage::Status DelAppliedIndex(storage::StorageTransaction *transaction); + storage::Status DelAppliedIndex(storage::StorageTransaction* transaction); - bool GetInodeCount(std::size_t *count); + storage::Status GetInodeCount(std::size_t* count); - storage::Status SetInodeCount(storage::StorageTransaction *transaction, + storage::Status SetInodeCount(storage::StorageTransaction* transaction, std::size_t count); - storage::Status DelInodeCount(storage::StorageTransaction *transaction); + storage::Status DelInodeCount(storage::StorageTransaction* transaction); private: // FIXME: please remove this lock, because we has locked each inode diff --git a/curvefs/src/metaserver/metastore.cpp b/curvefs/src/metaserver/metastore.cpp index 4bfdd16ec7..21a4bbe411 100644 --- a/curvefs/src/metaserver/metastore.cpp +++ b/curvefs/src/metaserver/metastore.cpp @@ -114,8 +114,12 @@ bool MetaStoreImpl::Load(const std::string &pathname) { // NOTE: for compatibility, we cann't load pending tx // when construct partition, it must be loaded in here + // `Init()` load dentry and inode metadata also for (auto it = partitionMap_.begin(); it != partitionMap_.end(); it++) { - it->second->LoadPendingTx(); + if (!it->second->Init()) { + LOG(ERROR) << "Init partition " << it->first << " failed"; + return false; + } } for (auto it = partitionMap_.begin(); it != partitionMap_.end(); it++) { @@ -167,46 +171,38 @@ void MetaStoreImpl::SaveBackground(const std::string &path, done->Run(); } -// TODO(NaturalSelect): support asynchronous snapshot -bool MetaStoreImpl::Save(const std::string &dir, - OnSnapshotSaveDoneClosure *done) { - brpc::ClosureGuard doneGuard(done); +bool MetaStoreImpl::SaveMeta(const std::string &dir, + std::vector *files) { + // NOTE: we will keep meta fstream consistent with + // snapshot metadata, so we should hold the locks + // during `fstream.Save()` WriteLockGuard writeLockGuard(rwLock_); - MetaStoreFStream fstream(&partitionMap_, kvStorage_, - copysetNode_->GetPoolId(), - copysetNode_->GetCopysetId()); + copysetNode_->GetPoolId(), + copysetNode_->GetCopysetId()); const std::string metadata = dir + "/" + kMetaDataFilename; - bool succ = fstream.Save(metadata); - if (!succ) { - done->SetError(MetaStatusCode::SAVE_META_FAIL); - return false; + if (fstream.Save(metadata)) { + files->push_back(kMetaDataFilename); + return true; } + return false; +} - // checkpoint storage +bool MetaStoreImpl::SaveData(const std::string &dir, + std::vector *files) { butil::Timer timer; timer.start(); - std::vector files; - succ = kvStorage_->Checkpoint(dir, &files); + std::vector tmp; + bool succ = kvStorage_->Checkpoint(dir, &tmp); if (!succ) { - done->SetError(MetaStatusCode::SAVE_META_FAIL); return false; } - + for (auto & file : tmp) { + files->push_back(std::move(file)); + } timer.stop(); g_storage_checkpoint_latency << timer.u_elapsed(); - - // add files to snapshot writer - // file is a relative path under the given directory - auto *writer = done->GetSnapshotWriter(); - writer->add_file(kMetaDataFilename); - - for (const auto &f : files) { - writer->add_file(f); - } - - done->SetSuccess(); return true; } diff --git a/curvefs/src/metaserver/metastore.h b/curvefs/src/metaserver/metastore.h index 597158c7c7..df164ed3dc 100644 --- a/curvefs/src/metaserver/metastore.h +++ b/curvefs/src/metaserver/metastore.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "curvefs/proto/metaserver.pb.h" #include "curvefs/src/common/rpc_stream.h" @@ -111,8 +112,10 @@ class MetaStore { virtual ~MetaStore() = default; virtual bool Load(const std::string& pathname) = 0; - virtual bool Save(const std::string& dir, - OnSnapshotSaveDoneClosure* done) = 0; + virtual bool SaveMeta(const std::string &dir, + std::vector *files) = 0; + virtual bool SaveData(const std::string &dir, + std::vector *files) = 0; virtual bool Clear() = 0; virtual bool Destroy() = 0; virtual MetaStatusCode CreatePartition( @@ -224,8 +227,10 @@ class MetaStoreImpl : public MetaStore { const storage::StorageOptions& storageOptions); bool Load(const std::string& checkpoint) override; - bool Save(const std::string& dir, - OnSnapshotSaveDoneClosure* done) override; + bool SaveMeta(const std::string &dir, + std::vector *files) override; + bool SaveData(const std::string &dir, + std::vector *files) override; bool Clear() override; bool Destroy() override; diff --git a/curvefs/src/metaserver/metastore_fstream.cpp b/curvefs/src/metaserver/metastore_fstream.cpp index f7c2076a53..3766a0e73c 100644 --- a/curvefs/src/metaserver/metastore_fstream.cpp +++ b/curvefs/src/metaserver/metastore_fstream.cpp @@ -217,18 +217,22 @@ bool MetaStoreFStream::Load(const std::string &pathname, uint8_t *version) { case ENTRY_TYPE::INODE: LOG(ERROR) << "Snapshot is too old, incompatible with current version"; + break; case ENTRY_TYPE::DENTRY: LOG(ERROR) << "Snapshot is too old, incompatible with current version"; + break; case ENTRY_TYPE::PENDING_TX: ++totalPendingTx; return LoadPendingTx(partitionId, key, value); case ENTRY_TYPE::S3_CHUNK_INFO_LIST: LOG(ERROR) << "Snapshot is too old, incompatible with current version"; + break; case ENTRY_TYPE::VOLUME_EXTENT: LOG(ERROR) << "Snapshot is too old, incompatible with current version"; + break; case ENTRY_TYPE::UNKNOWN: break; } diff --git a/curvefs/src/metaserver/partition.cpp b/curvefs/src/metaserver/partition.cpp index d3ea32e927..01d6e9cfe0 100644 --- a/curvefs/src/metaserver/partition.cpp +++ b/curvefs/src/metaserver/partition.cpp @@ -114,7 +114,7 @@ Partition::Partition(PartitionInfo partition, } \ } while (0) -MetaStatusCode Partition::CreateDentry(const Dentry &dentry, +MetaStatusCode Partition::CreateDentry(const Dentry& dentry, std::int64_t logIndex) { PRECHECK(dentry.fsid(), dentry.parentinodeid()); MetaStatusCode ret = dentryManager_->CreateDentry(dentry, logIndex); @@ -148,7 +148,7 @@ MetaStatusCode Partition::CreateDentry(const Dentry &dentry, } } -MetaStatusCode Partition::LoadDentry(const DentryVec &vec, bool merge, +MetaStatusCode Partition::LoadDentry(const DentryVec& vec, bool merge, std::int64_t logIndex) { auto dentry = vec.dentrys(0); @@ -161,7 +161,7 @@ MetaStatusCode Partition::LoadDentry(const DentryVec &vec, bool merge, return rc; } -MetaStatusCode Partition::DeleteDentry(const Dentry &dentry, +MetaStatusCode Partition::DeleteDentry(const Dentry& dentry, std::int64_t logIndex) { PRECHECK(dentry.fsid(), dentry.parentinodeid()); @@ -193,13 +193,13 @@ MetaStatusCode Partition::DeleteDentry(const Dentry &dentry, } } -MetaStatusCode Partition::GetDentry(Dentry *dentry) { +MetaStatusCode Partition::GetDentry(Dentry* dentry) { PRECHECK(dentry->fsid(), dentry->parentinodeid()); return dentryManager_->GetDentry(dentry); } -MetaStatusCode Partition::ListDentry(const Dentry &dentry, - std::vector *dentrys, +MetaStatusCode Partition::ListDentry(const Dentry& dentry, + std::vector* dentrys, uint32_t limit, bool onlyDir) { PRECHECK(dentry.fsid(), dentry.parentinodeid()); return dentryManager_->ListDentry(dentry, dentrys, limit, onlyDir); @@ -207,19 +207,19 @@ MetaStatusCode Partition::ListDentry(const Dentry &dentry, void Partition::ClearDentry() { dentryManager_->ClearDentry(); } -MetaStatusCode Partition::HandleRenameTx(const std::vector &dentrys, +MetaStatusCode Partition::HandleRenameTx(const std::vector& dentrys, std::int64_t logIndex) { - for (const auto &it : dentrys) { + for (const auto& it : dentrys) { PRECHECK(it.fsid(), it.parentinodeid()); } return dentryManager_->HandleRenameTx(dentrys, logIndex); } -bool Partition::InsertPendingTx(const PrepareRenameTxRequest &pendingTx) { +bool Partition::InsertPendingTx(const PrepareRenameTxRequest& pendingTx) { std::vector dentrys{pendingTx.dentrys().begin(), pendingTx.dentrys().end()}; - for (const auto &it : dentrys) { + for (const auto& it : dentrys) { if (!IsInodeBelongs(it.fsid(), it.parentinodeid())) { return false; } @@ -229,18 +229,25 @@ bool Partition::InsertPendingTx(const PrepareRenameTxRequest &pendingTx) { return txManager_->InsertPendingTx(renameTx); } -void Partition::LoadPendingTx() { txManager_->LoadPengingTx(); } +bool Partition::Init() { + // NOTE: invoke `dentryStorage::Init()` + // and `inodeStorage::Init()` is unnecessary + // they will be invoked by `manager` + return dentryManager_->Init() && + inodeManager_->Init() && + txManager_->Init(); +} -void Partition::SerializeRenameTx(const RenameTx &in, - PrepareRenameTxRequest *out) { - auto *dentrys = in.GetConstDentrys(); +void Partition::SerializeRenameTx(const RenameTx& in, + PrepareRenameTxRequest* out) { + auto* dentrys = in.GetConstDentrys(); out->set_poolid(partitionInfo_.poolid()); out->set_copysetid(partitionInfo_.copysetid()); out->set_partitionid(partitionInfo_.partitionid()); *out->mutable_dentrys() = {dentrys->begin(), dentrys->end()}; } -bool Partition::FindPendingTx(PrepareRenameTxRequest *pendingTx) { +bool Partition::FindPendingTx(PrepareRenameTxRequest* pendingTx) { if (GetStatus() == PartitionStatus::DELETING) { return false; } @@ -256,7 +263,7 @@ bool Partition::FindPendingTx(PrepareRenameTxRequest *pendingTx) { } // inode -MetaStatusCode Partition::CreateInode(const InodeParam ¶m, Inode *inode, +MetaStatusCode Partition::CreateInode(const InodeParam& param, Inode* inode, std::int64_t logIndex) { if (GetStatus() == PartitionStatus::READONLY) { return MetaStatusCode::PARTITION_ALLOC_ID_FAIL; @@ -282,7 +289,7 @@ MetaStatusCode Partition::CreateInode(const InodeParam ¶m, Inode *inode, return ret; } -MetaStatusCode Partition::CreateRootInode(const InodeParam ¶m, +MetaStatusCode Partition::CreateRootInode(const InodeParam& param, std::int64_t logIndex) { PRECHECK_FSID(param.fsId); auto ret = inodeManager_->CreateRootInode(param, logIndex); @@ -292,9 +299,9 @@ MetaStatusCode Partition::CreateRootInode(const InodeParam ¶m, return ret; } -MetaStatusCode Partition::CreateManageInode(const InodeParam ¶m, +MetaStatusCode Partition::CreateManageInode(const InodeParam& param, ManageInodeType manageType, - Inode *inode, + Inode* inode, std::int64_t logIndex) { PRECHECK_FSID(param.fsId); auto ret = @@ -306,19 +313,19 @@ MetaStatusCode Partition::CreateManageInode(const InodeParam ¶m, } MetaStatusCode Partition::GetInode(uint32_t fsId, uint64_t inodeId, - Inode *inode) { + Inode* inode) { PRECHECK(fsId, inodeId); return inodeManager_->GetInode(fsId, inodeId, inode); } MetaStatusCode Partition::GetInodeAttr(uint32_t fsId, uint64_t inodeId, - InodeAttr *attr) { + InodeAttr* attr) { PRECHECK(fsId, inodeId); return inodeManager_->GetInodeAttr(fsId, inodeId, attr); } MetaStatusCode Partition::GetXAttr(uint32_t fsId, uint64_t inodeId, - XAttr *xattr) { + XAttr* xattr) { PRECHECK(fsId, inodeId); return inodeManager_->GetXAttr(fsId, inodeId, xattr); } @@ -333,7 +340,7 @@ MetaStatusCode Partition::DeleteInode(uint32_t fsId, uint64_t inodeId, return ret; } -MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest &request, +MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest& request, std::int64_t logIndex) { PRECHECK(request.fsid(), request.inodeid()); auto ret = inodeManager_->UpdateInode(request, logIndex); @@ -344,9 +351,9 @@ MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest &request, } MetaStatusCode Partition::GetOrModifyS3ChunkInfo( - uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap &map2add, - const S3ChunkInfoMap &map2del, bool returnS3ChunkInfoMap, - std::shared_ptr *iterator, std::int64_t logIndex) { + uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add, + const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap, + std::shared_ptr* iterator, std::int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = inodeManager_->GetOrModifyS3ChunkInfo( fsId, inodeId, map2add, map2del, returnS3ChunkInfoMap, iterator, @@ -359,13 +366,13 @@ MetaStatusCode Partition::GetOrModifyS3ChunkInfo( MetaStatusCode Partition::PaddingInodeS3ChunkInfo(int32_t fsId, uint64_t inodeId, - S3ChunkInfoMap *m, + S3ChunkInfoMap* m, uint64_t limit) { PRECHECK(fsId, inodeId); return inodeManager_->PaddingInodeS3ChunkInfo(fsId, inodeId, m, limit); } -MetaStatusCode Partition::InsertInode(const Inode &inode, +MetaStatusCode Partition::InsertInode(const Inode& inode, std::int64_t logIndex) { PRECHECK(inode.fsid(), inode.inodeid()); auto ret = inodeManager_->InsertInode(inode, logIndex); @@ -375,7 +382,7 @@ MetaStatusCode Partition::InsertInode(const Inode &inode, return ret; } -bool Partition::GetInodeIdList(std::list *InodeIdList) { +bool Partition::GetInodeIdList(std::list* InodeIdList) { return inodeManager_->GetInodeIdList(InodeIdList); } @@ -454,7 +461,7 @@ bool Partition::Clear() { } partitionInfo_.set_inodenum(0); partitionInfo_.set_dentrynum(0); - for (auto &it : *partitionInfo_.mutable_filetype2inodenum()) { + for (auto& it : *partitionInfo_.mutable_filetype2inodenum()) { it.second = 0; } @@ -463,6 +470,10 @@ bool Partition::Clear() { return true; } +// NOTE: store nextid to kvstroage is unnecessary +// we will replay the logs filter the log entries that +// already applied, but keep nextid changes in memory +// so it will grow to corrected value after replay uint64_t Partition::GetNewInodeId() { if (partitionInfo_.nextid() > partitionInfo_.end()) { partitionInfo_.set_status(PartitionStatus::READONLY); @@ -481,7 +492,9 @@ uint32_t Partition::GetDentryNum() { return static_cast(dentryStorage_->Size()); } -bool Partition::EmptyInodeStorage() { return inodeStorage_->Empty(); } +bool Partition::EmptyInodeStorage() { + return inodeStorage_->Empty(); +} std::string Partition::GetInodeTablename() { std::ostringstream oss; @@ -497,7 +510,7 @@ std::string Partition::GetDentryTablename() { MetaStatusCode Partition::UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSliceList &extents, + const VolumeExtentSliceList& extents, std::int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = @@ -510,7 +523,7 @@ Partition::UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, MetaStatusCode Partition::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, + const VolumeExtentSlice& slice, std::int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = @@ -522,14 +535,14 @@ Partition::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, } MetaStatusCode Partition::GetVolumeExtent(uint32_t fsId, uint64_t inodeId, - const std::vector &slices, - VolumeExtentSliceList *extents) { + const std::vector& slices, + VolumeExtentSliceList* extents) { PRECHECK(fsId, inodeId); return inodeManager_->GetVolumeExtent(fsId, inodeId, slices, extents); } MetaStatusCode Partition::UpdateDeallocatableBlockGroup( - const UpdateDeallocatableBlockGroupRequest &request, + const UpdateDeallocatableBlockGroupRequest& request, std::int64_t logIndex) { PRECHECK_FSID(request.fsid()); auto ret = inodeManager_->UpdateDeallocatableBlockGroup(request, logIndex); @@ -540,7 +553,7 @@ MetaStatusCode Partition::UpdateDeallocatableBlockGroup( } MetaStatusCode Partition::GetAllBlockGroup( - std::vector *deallocatableBlockGroupVec) { + std::vector* deallocatableBlockGroupVec) { return inodeStorage_->GetAllBlockGroup(deallocatableBlockGroupVec); } diff --git a/curvefs/src/metaserver/partition.h b/curvefs/src/metaserver/partition.h index 209eca5d0a..cca8ce3c18 100644 --- a/curvefs/src/metaserver/partition.h +++ b/curvefs/src/metaserver/partition.h @@ -57,90 +57,90 @@ class Partition { Partition() = default; // dentry - MetaStatusCode CreateDentry(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode CreateDentry(const Dentry& dentry, std::int64_t logIndex); - MetaStatusCode LoadDentry(const DentryVec &vec, bool merge, + MetaStatusCode LoadDentry(const DentryVec& vec, bool merge, std::int64_t logIndex); - MetaStatusCode DeleteDentry(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode DeleteDentry(const Dentry& dentry, std::int64_t logIndex); - MetaStatusCode GetDentry(Dentry *dentry); + MetaStatusCode GetDentry(Dentry* dentry); - MetaStatusCode ListDentry(const Dentry &dentry, - std::vector *dentrys, uint32_t limit, + MetaStatusCode ListDentry(const Dentry& dentry, + std::vector* dentrys, uint32_t limit, bool onlyDir = false); void ClearDentry(); - MetaStatusCode HandleRenameTx(const std::vector &dentrys, + MetaStatusCode HandleRenameTx(const std::vector& dentrys, std::int64_t logIndex); - bool InsertPendingTx(const PrepareRenameTxRequest &pendingTx); + bool InsertPendingTx(const PrepareRenameTxRequest& pendingTx); - bool FindPendingTx(PrepareRenameTxRequest *pendingTx); + bool FindPendingTx(PrepareRenameTxRequest* pendingTx); - void SerializeRenameTx(const RenameTx &in, PrepareRenameTxRequest *out); + void SerializeRenameTx(const RenameTx& in, PrepareRenameTxRequest* out); - void LoadPendingTx(); + bool Init(); // inode - MetaStatusCode CreateInode(const InodeParam ¶m, Inode *inode, + MetaStatusCode CreateInode(const InodeParam& param, Inode* inode, std::int64_t logIndex); - MetaStatusCode CreateRootInode(const InodeParam ¶m, + MetaStatusCode CreateRootInode(const InodeParam& param, std::int64_t logIndex); - MetaStatusCode CreateManageInode(const InodeParam ¶m, - ManageInodeType manageType, Inode *inode, + MetaStatusCode CreateManageInode(const InodeParam& param, + ManageInodeType manageType, Inode* inode, std::int64_t logIndex); - MetaStatusCode GetInode(uint32_t fsId, uint64_t inodeId, Inode *inode); + MetaStatusCode GetInode(uint32_t fsId, uint64_t inodeId, Inode* inode); MetaStatusCode GetInodeAttr(uint32_t fsId, uint64_t inodeId, - InodeAttr *attr); + InodeAttr* attr); - MetaStatusCode GetXAttr(uint32_t fsId, uint64_t inodeId, XAttr *xattr); + MetaStatusCode GetXAttr(uint32_t fsId, uint64_t inodeId, XAttr* xattr); MetaStatusCode DeleteInode(uint32_t fsId, uint64_t inodeId, std::int64_t logIndex); - MetaStatusCode UpdateInode(const UpdateInodeRequest &request, + MetaStatusCode UpdateInode(const UpdateInodeRequest& request, std::int64_t logIndex); MetaStatusCode GetOrModifyS3ChunkInfo(uint32_t fsId, uint64_t inodeId, - const S3ChunkInfoMap &map2add, - const S3ChunkInfoMap &map2del, + const S3ChunkInfoMap& map2add, + const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap, - std::shared_ptr *iterator, + std::shared_ptr* iterator, std::int64_t logIndex); MetaStatusCode PaddingInodeS3ChunkInfo(int32_t fsId, uint64_t inodeId, - S3ChunkInfoMap *m, + S3ChunkInfoMap* m, uint64_t limit = 0); MetaStatusCode UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSliceList &extents, + const VolumeExtentSliceList& extents, std::int64_t logIndex); MetaStatusCode UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, + const VolumeExtentSlice& slice, std::int64_t logIndex); MetaStatusCode GetVolumeExtent(uint32_t fsId, uint64_t inodeId, - const std::vector &slices, - VolumeExtentSliceList *extents); + const std::vector& slices, + VolumeExtentSliceList* extents); MetaStatusCode UpdateDeallocatableBlockGroup( - const UpdateDeallocatableBlockGroupRequest &request, + const UpdateDeallocatableBlockGroupRequest& request, std::int64_t logIndex); virtual MetaStatusCode GetAllBlockGroup( - std::vector *deallocatableBlockGroupVec); + std::vector* deallocatableBlockGroupVec); - MetaStatusCode InsertInode(const Inode &inode, std::int64_t logIndex); + MetaStatusCode InsertInode(const Inode& inode, std::int64_t logIndex); - bool GetInodeIdList(std::list *InodeIdList); + bool GetInodeIdList(std::list* InodeIdList); // if partition has no inode or no dentry, it is deletable bool IsDeletable(); diff --git a/curvefs/src/metaserver/storage/converter.cpp b/curvefs/src/metaserver/storage/converter.cpp index 721a101da8..5732dca9b9 100644 --- a/curvefs/src/metaserver/storage/converter.cpp +++ b/curvefs/src/metaserver/storage/converter.cpp @@ -45,9 +45,9 @@ using ::curve::common::StringToUl; using ::curve::common::StringToUll; using ::curvefs::common::PartitionInfo; -static const char *const kDelimiter = ":"; +static const char* const kDelimiter = ":"; -static bool CompareType(const std::string &str, KEY_TYPE keyType) { +static bool CompareType(const std::string& str, KEY_TYPE keyType) { uint32_t n; return StringToUl(str, &n) && n == keyType; } @@ -119,7 +119,7 @@ size_t NameGenerator::GetFixedLength() { std::string NameGenerator::Format(KEY_TYPE type, uint32_t partitionId) { char buf[sizeof(partitionId)]; - std::memcpy(buf, reinterpret_cast(&partitionId), sizeof(buf)); + std::memcpy(buf, reinterpret_cast(&partitionId), sizeof(buf)); return absl::StrCat(type, kDelimiter, absl::string_view(buf, sizeof(buf))); } @@ -128,10 +128,10 @@ Key4Inode::Key4Inode() : fsId(0), inodeId(0) {} Key4Inode::Key4Inode(uint32_t fsId, uint64_t inodeId) : fsId(fsId), inodeId(inodeId) {} -Key4Inode::Key4Inode(const Inode &inode) +Key4Inode::Key4Inode(const Inode& inode) : fsId(inode.fsid()), inodeId(inode.inodeid()) {} -bool Key4Inode::operator==(const Key4Inode &rhs) { +bool Key4Inode::operator==(const Key4Inode& rhs) { return fsId == rhs.fsId && inodeId == rhs.inodeId; } @@ -139,7 +139,7 @@ std::string Key4Inode::SerializeToString() const { return absl::StrCat(keyType_, ":", fsId, ":", inodeId); } -bool Key4Inode::ParseFromString(const std::string &value) { +bool Key4Inode::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 3 && CompareType(items[0], keyType_) && @@ -150,7 +150,7 @@ std::string Prefix4AllInode::SerializeToString() const { return absl::StrCat(keyType_, ":"); } -bool Prefix4AllInode::ParseFromString(const std::string &value) { +bool Prefix4AllInode::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 1 && CompareType(items[0], keyType_); @@ -167,45 +167,53 @@ Key4S3ChunkInfoList::Key4S3ChunkInfoList(uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, uint64_t firstChunkId, uint64_t lastChunkId, uint64_t size) - : fsId(fsId), inodeId(inodeId), chunkIndex(chunkIndex), - firstChunkId(firstChunkId), lastChunkId(lastChunkId), size(size) {} + : fsId(fsId), + inodeId(inodeId), + chunkIndex(chunkIndex), + firstChunkId(firstChunkId), + lastChunkId(lastChunkId), + size(size) {} std::string Key4S3ChunkInfoList::SerializeToString() const { - return absl::StrCat(keyType_, ":", fsId, ":", inodeId, ":", chunkIndex, ":", - absl::StrFormat("%020" PRIu64 "", firstChunkId), ":", - absl::StrFormat("%020" PRIu64 "", lastChunkId), ":", - size); + return absl::StrCat(keyType_, ":", fsId, ":", inodeId, ":", + chunkIndex, ":", absl::StrFormat("%020" PRIu64"", firstChunkId), ":", + absl::StrFormat("%020" PRIu64"", lastChunkId), ":", size); } -bool Key4S3ChunkInfoList::ParseFromString(const std::string &value) { +bool Key4S3ChunkInfoList::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 7 && CompareType(items[0], keyType_) && - StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId) && - StringToUll(items[3], &chunkIndex) && - StringToUll(items[4], &firstChunkId) && - StringToUll(items[5], &lastChunkId) && StringToUll(items[6], &size); + StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId) && + StringToUll(items[3], &chunkIndex) && + StringToUll(items[4], &firstChunkId) && + StringToUll(items[5], &lastChunkId) && + StringToUll(items[6], &size); } Prefix4ChunkIndexS3ChunkInfoList::Prefix4ChunkIndexS3ChunkInfoList() : fsId(0), inodeId(0), chunkIndex(0) {} Prefix4ChunkIndexS3ChunkInfoList::Prefix4ChunkIndexS3ChunkInfoList( - uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex) + uint32_t fsId, + uint64_t inodeId, + uint64_t chunkIndex) : fsId(fsId), inodeId(inodeId), chunkIndex(chunkIndex) {} std::string Prefix4ChunkIndexS3ChunkInfoList::SerializeToString() const { - return absl::StrCat(keyType_, ":", fsId, ":", inodeId, ":", chunkIndex, - ":"); + return absl::StrCat(keyType_, ":", + fsId, ":", + inodeId, ":", + chunkIndex, ":"); } bool Prefix4ChunkIndexS3ChunkInfoList::ParseFromString( - const std::string &value) { + const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 4 && CompareType(items[0], keyType_) && - StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId) && - StringToUll(items[3], &chunkIndex); + StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId) && + StringToUll(items[3], &chunkIndex); } Prefix4InodeS3ChunkInfoList::Prefix4InodeS3ChunkInfoList() @@ -219,25 +227,26 @@ std::string Prefix4InodeS3ChunkInfoList::SerializeToString() const { return absl::StrCat(keyType_, ":", fsId, ":", inodeId, ":"); } -bool Prefix4InodeS3ChunkInfoList::ParseFromString(const std::string &value) { +bool Prefix4InodeS3ChunkInfoList::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 3 && CompareType(items[0], keyType_) && - StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId); + StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId); } std::string Prefix4AllS3ChunkInfoList::SerializeToString() const { return absl::StrCat(kTypeS3ChunkInfo, ":"); } -bool Prefix4AllS3ChunkInfoList::ParseFromString(const std::string &value) { +bool Prefix4AllS3ChunkInfoList::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 1 && CompareType(items[0], keyType_); } -Key4Dentry::Key4Dentry(uint32_t fsId, uint64_t parentInodeId, - const std::string &name) +Key4Dentry::Key4Dentry(uint32_t fsId, + uint64_t parentInodeId, + const std::string& name) : fsId(fsId), parentInodeId(parentInodeId), name(name) {} std::string Key4Dentry::SerializeToString() const { @@ -245,7 +254,7 @@ std::string Key4Dentry::SerializeToString() const { kDelimiter, name); } -bool Key4Dentry::ParseFromString(const std::string &value) { +bool Key4Dentry::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); if (items.size() < 3 || !CompareType(items[0], keyType_) || @@ -254,7 +263,9 @@ bool Key4Dentry::ParseFromString(const std::string &value) { return false; } - size_t prefixLength = items[0].size() + items[1].size() + items[2].size() + + size_t prefixLength = items[0].size() + + items[1].size() + + items[2].size() + 3 * strlen(kDelimiter); if (value.size() < prefixLength) { return false; @@ -268,11 +279,12 @@ Prefix4SameParentDentry::Prefix4SameParentDentry(uint32_t fsId, : fsId(fsId), parentInodeId(parentInodeId) {} std::string Prefix4SameParentDentry::SerializeToString() const { - return absl::StrCat(keyType_, kDelimiter, fsId, kDelimiter, parentInodeId, + return absl::StrCat(keyType_, kDelimiter, fsId, + kDelimiter, parentInodeId, kDelimiter); } -bool Prefix4SameParentDentry::ParseFromString(const std::string &value) { +bool Prefix4SameParentDentry::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 3 && CompareType(items[0], keyType_) && @@ -283,13 +295,14 @@ std::string Prefix4AllDentry::SerializeToString() const { return absl::StrCat(keyType_, ":"); } -bool Prefix4AllDentry::ParseFromString(const std::string &value) { +bool Prefix4AllDentry::ParseFromString(const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 1 && CompareType(items[0], keyType_); } -Key4VolumeExtentSlice::Key4VolumeExtentSlice(uint32_t fsId, uint64_t inodeId, +Key4VolumeExtentSlice::Key4VolumeExtentSlice(uint32_t fsId, + uint64_t inodeId, uint64_t offset) : fsId_(fsId), inodeId_(inodeId), offset_(offset) {} @@ -298,7 +311,7 @@ std::string Key4VolumeExtentSlice::SerializeToString() const { kDelimiter, offset_); } -bool Key4VolumeExtentSlice::ParseFromString(const std::string &value) { +bool Key4VolumeExtentSlice::ParseFromString(const std::string& value) { // TODO(wuhanqing): reduce unnecessary creation of temporary strings, // but, currently, `absl::from_chars` only support floating // point @@ -318,7 +331,7 @@ std::string Prefix4InodeVolumeExtent::SerializeToString() const { kDelimiter); } -bool Prefix4InodeVolumeExtent::ParseFromString(const std::string &value) { +bool Prefix4InodeVolumeExtent::ParseFromString(const std::string& value) { std::vector items; SplitString(value, kDelimiter, &items); return items.size() == 3 && CompareType(items[0], keyType_) && @@ -329,20 +342,21 @@ std::string Prefix4AllVolumeExtent::SerializeToString() const { return absl::StrCat(keyType_, kDelimiter); } -bool Prefix4AllVolumeExtent::ParseFromString(const std::string &value) { +bool Prefix4AllVolumeExtent::ParseFromString(const std::string& value) { std::vector items; SplitString(value, kDelimiter, &items); return items.size() == 1 && CompareType(items[0], keyType_); } -Key4InodeAuxInfo::Key4InodeAuxInfo(uint32_t fsId, uint64_t inodeId) +Key4InodeAuxInfo::Key4InodeAuxInfo(uint32_t fsId, + uint64_t inodeId) : fsId(fsId), inodeId(inodeId) {} std::string Key4InodeAuxInfo::SerializeToString() const { return absl::StrCat(keyType_, kDelimiter, fsId, kDelimiter, inodeId); } -bool Key4InodeAuxInfo::ParseFromString(const std::string &value) { +bool Key4InodeAuxInfo::ParseFromString(const std::string& value) { std::vector items; SplitString(value, kDelimiter, &items); return items.size() == 3 && CompareType(items[0], keyType_) && @@ -353,7 +367,7 @@ std::string Key4DeallocatableBlockGroup::SerializeToString() const { return absl::StrCat(keyType_, kDelimiter, fsId, kDelimiter, volumeOffset); } -bool Key4DeallocatableBlockGroup::ParseFromString(const std::string &value) { +bool Key4DeallocatableBlockGroup::ParseFromString(const std::string& value) { std::vector items; SplitString(value, kDelimiter, &items); return items.size() == 3 && CompareType(items[0], keyType_) && @@ -365,18 +379,18 @@ std::string Prefix4AllDeallocatableBlockGroup::SerializeToString() const { } bool Prefix4AllDeallocatableBlockGroup::ParseFromString( - const std::string &value) { + const std::string& value) { std::vector items; SplitString(value, ":", &items); return items.size() == 1 && CompareType(items[0], keyType_); } -std::string Converter::SerializeToString(const StorageKey &key) { +std::string Converter::SerializeToString(const StorageKey& key) { return key.SerializeToString(); } -bool Converter::SerializeToString(const google::protobuf::Message &entry, - std::string *value) { +bool Converter::SerializeToString(const google::protobuf::Message& entry, + std::string* value) { if (!entry.IsInitialized()) { LOG(ERROR) << "Message not initialized"; return false; diff --git a/curvefs/src/metaserver/storage/rocksdb_storage.cpp b/curvefs/src/metaserver/storage/rocksdb_storage.cpp index 5121db8f6b..5875ba6817 100644 --- a/curvefs/src/metaserver/storage/rocksdb_storage.cpp +++ b/curvefs/src/metaserver/storage/rocksdb_storage.cpp @@ -438,7 +438,11 @@ bool RocksDBStorage::Checkpoint(const std::string& dir, std::vector* files) { rocksdb::FlushOptions options; options.wait = true; - options.allow_write_stall = true; + // NOTE: for asynchronous snapshot + // we cannot allow write stall + // rocksdb will wait until flush + // can be performed without causing write stall + options.allow_write_stall = false; auto status = db_->Flush(options, handles_); if (!status.ok()) { LOG(ERROR) << "Failed to flush DB, " << status.ToString(); diff --git a/curvefs/src/metaserver/transaction.cpp b/curvefs/src/metaserver/transaction.cpp index fc6ba7e6dc..a32123cd82 100644 --- a/curvefs/src/metaserver/transaction.cpp +++ b/curvefs/src/metaserver/transaction.cpp @@ -29,9 +29,10 @@ namespace metaserver { using curve::common::ReadLockGuard; using curve::common::WriteLockGuard; -RenameTx::RenameTx(const std::vector &dentrys, +RenameTx::RenameTx(const std::vector& dentrys, std::shared_ptr storage) - : txId_(dentrys[0].txid()), txSequence_(dentrys[0].txsequence()), + : txId_(dentrys[0].txid()), + txSequence_(dentrys[0].txsequence()), dentrys_(dentrys), storage_(storage) {} bool RenameTx::Prepare(std::string txPayload, std::int64_t logIndex) { @@ -54,17 +55,17 @@ uint64_t RenameTx::GetTxId() { return txId_; } uint64_t RenameTx::GetTxSequence() { return txSequence_; } -std::vector *RenameTx::GetDentrys() { return &dentrys_; } +std::vector* RenameTx::GetDentrys() { return &dentrys_; } -const std::vector *RenameTx::GetConstDentrys() const { +const std::vector* RenameTx::GetConstDentrys() const { return &dentrys_; } -inline bool RenameTx::operator==(const RenameTx &rhs) { +inline bool RenameTx::operator==(const RenameTx& rhs) { return dentrys_ == rhs.dentrys_; } -std::ostream &operator<<(std::ostream &os, const RenameTx &renameTx) { +std::ostream& operator<<(std::ostream& os, const RenameTx& renameTx) { auto dentrys = renameTx.dentrys_; os << "txId = " << renameTx.txId_; for (size_t i = 0; i < dentrys.size(); i++) { @@ -79,7 +80,7 @@ TxManager::TxManager(std::shared_ptr storage, : storage_(std::move(storage)), conv_(std::make_shared()), partitionInfo_(std::move(partitionInfo)) {} -MetaStatusCode TxManager::PreCheck(const std::vector &dentrys) { +MetaStatusCode TxManager::PreCheck(const std::vector& dentrys) { auto size = dentrys.size(); if (size != 1 && size != 2) { return MetaStatusCode::PARAM_ERROR; @@ -94,18 +95,19 @@ MetaStatusCode TxManager::PreCheck(const std::vector &dentrys) { return MetaStatusCode::OK; } -void TxManager::SerializeRenameTx(const RenameTx &in, - PrepareRenameTxRequest *out) { - auto *dentrys = in.GetConstDentrys(); +void TxManager::SerializeRenameTx(const RenameTx& in, + PrepareRenameTxRequest* out) { + auto* dentrys = in.GetConstDentrys(); out->set_poolid(partitionInfo_.poolid()); out->set_copysetid(partitionInfo_.copysetid()); out->set_partitionid(partitionInfo_.partitionid()); *out->mutable_dentrys() = {dentrys->begin(), dentrys->end()}; } -void TxManager::LoadPengingTx() { +bool TxManager::Init() { metaserver::TransactionRequest request; - if (storage_->GetPendingTx(&request)) { + auto s = storage_->GetPendingTx(&request); + if (s.ok()) { TxType type = static_cast(request.type()); if (type == TxType::None) { // NOTE: if tx type is none @@ -119,10 +121,12 @@ void TxManager::LoadPengingTx() { storage_); pendingTx_ = tx; } + return true; } + return s.IsNotFound(); } -MetaStatusCode TxManager::HandleRenameTx(const std::vector &dentrys, +MetaStatusCode TxManager::HandleRenameTx(const std::vector& dentrys, std::int64_t logIndex) { auto rc = PreCheck(dentrys); if (rc != MetaStatusCode::OK) { @@ -165,7 +169,7 @@ MetaStatusCode TxManager::HandleRenameTx(const std::vector &dentrys, return MetaStatusCode::OK; } -bool TxManager::InsertPendingTx(const RenameTx &tx) { +bool TxManager::InsertPendingTx(const RenameTx& tx) { WriteLockGuard w(rwLock_); if (pendingTx_ == EMPTY_TX) { pendingTx_ = tx; @@ -179,7 +183,7 @@ void TxManager::DeletePendingTx() { pendingTx_ = EMPTY_TX; } -bool TxManager::FindPendingTx(RenameTx *pendingTx) { +bool TxManager::FindPendingTx(RenameTx* pendingTx) { ReadLockGuard r(rwLock_); if (pendingTx_ == EMPTY_TX) { return false; @@ -188,7 +192,7 @@ bool TxManager::FindPendingTx(RenameTx *pendingTx) { return true; } -bool TxManager::HandlePendingTx(uint64_t txId, RenameTx *pendingTx, +bool TxManager::HandlePendingTx(uint64_t txId, RenameTx* pendingTx, std::int64_t logIndex) { if (txId > pendingTx->GetTxId()) { return pendingTx->Commit(logIndex); diff --git a/curvefs/src/metaserver/transaction.h b/curvefs/src/metaserver/transaction.h index 7be0e85d8d..b9b87b3358 100644 --- a/curvefs/src/metaserver/transaction.h +++ b/curvefs/src/metaserver/transaction.h @@ -41,7 +41,7 @@ class RenameTx { public: RenameTx() = default; - RenameTx(const std::vector &dentrys, + RenameTx(const std::vector& dentrys, std::shared_ptr storage); bool Prepare(std::string txStr, std::int64_t logIndex); @@ -54,13 +54,13 @@ class RenameTx { uint64_t GetTxSequence(); - std::vector *GetDentrys(); + std::vector* GetDentrys(); - const std::vector *GetConstDentrys() const; + const std::vector* GetConstDentrys() const; - bool operator==(const RenameTx &rhs); + bool operator==(const RenameTx& rhs); - friend std::ostream &operator<<(std::ostream &os, const RenameTx &renameTx); + friend std::ostream& operator<<(std::ostream& os, const RenameTx& renameTx); private: uint64_t txId_; @@ -78,23 +78,23 @@ class TxManager { explicit TxManager(std::shared_ptr storage, common::PartitionInfo partitionInfo); - MetaStatusCode HandleRenameTx(const std::vector &dentrys, + MetaStatusCode HandleRenameTx(const std::vector& dentrys, std::int64_t logIndex); - MetaStatusCode PreCheck(const std::vector &dentrys); + MetaStatusCode PreCheck(const std::vector& dentrys); - bool InsertPendingTx(const RenameTx &tx); + bool InsertPendingTx(const RenameTx& tx); - bool FindPendingTx(RenameTx *pendingTx); + bool FindPendingTx(RenameTx* pendingTx); void DeletePendingTx(); - bool HandlePendingTx(uint64_t txId, RenameTx *pendingTx, + bool HandlePendingTx(uint64_t txId, RenameTx* pendingTx, std::int64_t logIndex); - void SerializeRenameTx(const RenameTx &in, PrepareRenameTxRequest *out); + void SerializeRenameTx(const RenameTx& in, PrepareRenameTxRequest* out); - void LoadPengingTx(); + bool Init(); private: RWLock rwLock_; diff --git a/curvefs/src/metaserver/trash.cpp b/curvefs/src/metaserver/trash.cpp index 7b19b169e3..db44dea151 100644 --- a/curvefs/src/metaserver/trash.cpp +++ b/curvefs/src/metaserver/trash.cpp @@ -231,7 +231,6 @@ MetaStatusCode TrashImpl::DeleteInodeAndData(const TrashItem &item) { return MetaStatusCode::S3_DELETE_ERR; } } - // TODO(NaturalSelect): store a log index to inode storage is necessary? ret = inodeStorage_->ForceDelete(Key4Inode(item.fsId, item.inodeId)); if (ret != MetaStatusCode::OK && ret != MetaStatusCode::NOT_FOUND) { LOG(ERROR) << "Delete Inode fail, fsId = " << item.fsId diff --git a/curvefs/test/metaserver/copyset/copyset_node_snapshot_test.cpp b/curvefs/test/metaserver/copyset/copyset_node_snapshot_test.cpp index a4e4f83026..b13392865c 100644 --- a/curvefs/test/metaserver/copyset/copyset_node_snapshot_test.cpp +++ b/curvefs/test/metaserver/copyset/copyset_node_snapshot_test.cpp @@ -108,7 +108,7 @@ class CopysetNodeRaftSnapshotTest : public testing::Test { options_.port = kTestPort; options_.dataUri = "local://" + dataPath_; - options_.raftNodeOptions.log_uri = "local://" + dataPath_; + options_.raftNodeOptions.log_uri = "local://" + dataPath_; options_.raftNodeOptions.raft_meta_uri = "local://" + dataPath_; options_.raftNodeOptions.snapshot_uri = "local://" + dataPath_; @@ -185,14 +185,14 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_SaveConfEpochFailed) { MockSnapshotWriter writer; FakeSnapshotSaveClosure done; - EXPECT_CALL(writer, get_path()) - .WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(writer, get_path()).WillRepeatedly(Return(dataPath_)); EXPECT_CALL(*mockfs_, Open(_, _)) .WillOnce(Invoke([](const std::string&, int) { errno = EINVAL; return -1; })); - EXPECT_CALL(*mockMetaStore_, Save(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore_, SaveMeta(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore_, SaveData(_, _)).Times(0); node->on_snapshot_save(&writer, &done); @@ -202,7 +202,7 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_SaveConfEpochFailed) { EXPECT_EQ(EINVAL, done.status().error_code()); } -TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_MetaStoreSaveFailed) { +TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_MetaStoreSaveFailed_Meta) { ASSERT_TRUE(CreateOneCopyset()); auto* node = nodeManager_->GetCopysetNode(poolId_, copysetId_); @@ -214,31 +214,69 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_MetaStoreSaveFailed) { MockSnapshotWriter writer; FakeSnapshotSaveClosure done; - EXPECT_CALL(writer, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(writer, add_file(_)) - .Times(1); - EXPECT_CALL(*mockfs_, Open(_, _)) - .WillOnce(Return(0)); + EXPECT_CALL(writer, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(writer, add_file(_)).Times(1); + EXPECT_CALL(*mockfs_, Open(_, _)).WillOnce(Return(0)); EXPECT_CALL(*mockfs_, Write(_, Matcher(_), _, _)) .WillOnce(Invoke( [](int fd, const char*, uint64_t, int length) { return length; })); - EXPECT_CALL(*mockfs_, Fsync(_)) - .WillOnce(Return(0)); - EXPECT_CALL(*mockfs_, Close(_)) - .Times(1); - EXPECT_CALL(*mockMetaStore, Save(_, _)) - .WillOnce(Invoke([](std::string path, OnSnapshotSaveDoneClosure* done) { - done->SetError(MetaStatusCode::UNKNOWN_ERROR); - done->Run(); - return false; - })); + EXPECT_CALL(*mockfs_, Fsync(_)).WillOnce(Return(0)); + EXPECT_CALL(*mockfs_, Close(_)).Times(1); + EXPECT_CALL(*mockMetaStore, SaveMeta(_, _)) + .WillOnce( + Invoke([](const std::string& dir, std::vector* files) { + (void)dir; + (void)files; + return false; + })); node->on_snapshot_save(&writer, &done); done.WaitRunned(); EXPECT_FALSE(done.status().ok()); - EXPECT_EQ(MetaStatusCode::UNKNOWN_ERROR, done.status().error_code()); + EXPECT_EQ(MetaStatusCode::SAVE_META_FAIL, done.status().error_code()); +} + +TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_MetaStoreSaveFailed_Data) { + ASSERT_TRUE(CreateOneCopyset()); + + auto* node = nodeManager_->GetCopysetNode(poolId_, copysetId_); + ASSERT_NE(nullptr, node); + + auto mockMetaStore = mockMetaStore_.get(); + node->SetMetaStore(mockMetaStore_.release()); + + MockSnapshotWriter writer; + FakeSnapshotSaveClosure done; + + EXPECT_CALL(writer, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(writer, add_file(_)).Times(1); + EXPECT_CALL(*mockfs_, Open(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*mockfs_, Write(_, Matcher(_), _, _)) + .WillOnce(Invoke( + [](int fd, const char*, uint64_t, int length) { return length; })); + EXPECT_CALL(*mockfs_, Fsync(_)).WillOnce(Return(0)); + EXPECT_CALL(*mockfs_, Close(_)).Times(1); + EXPECT_CALL(*mockMetaStore, SaveMeta(_, _)) + .WillOnce( + Invoke([](const std::string& dir, std::vector* files) { + (void)dir; + (void)files; + return true; + })); + EXPECT_CALL(*mockMetaStore, SaveData(_, _)) + .WillOnce( + Invoke([](const std::string& dir, std::vector* files) { + (void)dir; + (void)files; + return false; + })); + + node->on_snapshot_save(&writer, &done); + done.WaitRunned(); + + EXPECT_FALSE(done.status().ok()); + EXPECT_EQ(MetaStatusCode::SAVE_META_FAIL, done.status().error_code()); // TODO(wuhanqing): check metric } @@ -255,25 +293,28 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotSaveTest_Success) { MockSnapshotWriter writer; FakeSnapshotSaveClosure done; - EXPECT_CALL(writer, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(writer, add_file(_)) - .Times(1); - EXPECT_CALL(*mockfs_, Open(_, _)) - .WillOnce(Return(0)); + EXPECT_CALL(writer, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(writer, add_file(_)).Times(1); + EXPECT_CALL(*mockfs_, Open(_, _)).WillOnce(Return(0)); EXPECT_CALL(*mockfs_, Write(_, Matcher(_), _, _)) .WillOnce(Invoke( [](int fd, const char*, uint64_t, int length) { return length; })); - EXPECT_CALL(*mockfs_, Fsync(_)) - .WillOnce(Return(0)); - EXPECT_CALL(*mockfs_, Close(_)) - .Times(1); - EXPECT_CALL(*mockMetaStore, Save(_, _)) - .WillOnce(Invoke([](std::string path, OnSnapshotSaveDoneClosure* done) { - done->SetSuccess(); - done->Run(); - return true; - })); + EXPECT_CALL(*mockfs_, Fsync(_)).WillOnce(Return(0)); + EXPECT_CALL(*mockfs_, Close(_)).Times(1); + EXPECT_CALL(*mockMetaStore, SaveMeta(_, _)) + .WillOnce( + Invoke([](const std::string& dir, std::vector* files) { + (void)dir; + (void)files; + return true; + })); + EXPECT_CALL(*mockMetaStore, SaveData(_, _)) + .WillOnce( + Invoke([](const std::string& dir, std::vector* files) { + (void)dir; + (void)files; + return true; + })); node->on_snapshot_save(&writer, &done); done.WaitRunned(); @@ -291,12 +332,9 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotLoadTest_LoadConfFileFailed) { ASSERT_NE(nullptr, node); MockSnapshotReader reader; - EXPECT_CALL(reader, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(*mockfs_, FileExists(_)) - .WillOnce(Return(true)); - EXPECT_CALL(*mockfs_, Open(_, _)) - .WillOnce(Return(-1)); + EXPECT_CALL(reader, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(*mockfs_, FileExists(_)).WillOnce(Return(true)); + EXPECT_CALL(*mockfs_, Open(_, _)).WillOnce(Return(-1)); ASSERT_FALSE(node->IsLoading()); EXPECT_NE(0, node->on_snapshot_load(&reader)); @@ -314,16 +352,11 @@ TEST_F(CopysetNodeRaftSnapshotTest, node->SetMetaStore(mockMetaStore_.release()); MockSnapshotReader reader; - EXPECT_CALL(reader, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(*mockfs_, FileExists(_)) - .WillOnce(Return(false)); - EXPECT_CALL(*mockfs_, Open(_, _)) - .Times(0); - EXPECT_CALL(*mockMetaStore, Clear()) - .Times(1); - EXPECT_CALL(*mockMetaStore, Load(_)) - .WillOnce(Return(true)); + EXPECT_CALL(reader, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(*mockfs_, FileExists(_)).WillOnce(Return(false)); + EXPECT_CALL(*mockfs_, Open(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore, Clear()).Times(1); + EXPECT_CALL(*mockMetaStore, Load(_)).WillOnce(Return(true)); braft::SnapshotMeta meta; meta.set_last_included_index(100); @@ -367,18 +400,12 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotLoadTest_MetaStoreLoadFailed) { node->SetMetaStore(mockMetaStore_.release()); MockSnapshotReader reader; - EXPECT_CALL(reader, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(*mockfs_, FileExists(_)) - .WillOnce(Return(false)); - EXPECT_CALL(*mockfs_, Open(_, _)) - .Times(0); - EXPECT_CALL(*mockMetaStore, Clear()) - .Times(1); - EXPECT_CALL(*mockMetaStore, Load(_)) - .WillOnce(Return(false)); - EXPECT_CALL(reader, load_meta(_)) - .Times(0); + EXPECT_CALL(reader, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(*mockfs_, FileExists(_)).WillOnce(Return(false)); + EXPECT_CALL(*mockfs_, Open(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore, Clear()).Times(1); + EXPECT_CALL(*mockMetaStore, Load(_)).WillOnce(Return(false)); + EXPECT_CALL(reader, load_meta(_)).Times(0); ASSERT_FALSE(node->IsLoading()); EXPECT_NE(0, node->on_snapshot_load(&reader)); @@ -387,7 +414,7 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotLoadTest_MetaStoreLoadFailed) { node->SetMetaStore(nullptr); } -void RunOnSnapshotLoad(CopysetNode* node, MockSnapshotReader *reader, +void RunOnSnapshotLoad(CopysetNode* node, MockSnapshotReader* reader, uint32_t sleepSec) { sleep(sleepSec); ASSERT_FALSE(node->IsLoading()); @@ -402,7 +429,7 @@ void RunGetPartitionInfoList(CopysetNode* node, uint32_t sleepSec, ASSERT_EQ(node->GetPartitionInfoList(&partitionInfoList), expectedValue); } -void RunGetBlockStatInfo(CopysetNode *node, uint32_t sleepSec, +void RunGetBlockStatInfo(CopysetNode* node, uint32_t sleepSec, bool expectedValue) { sleep(sleepSec); std::map blockStatInfoMap; @@ -421,21 +448,16 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotLoadTest_MetaStoreLoadSuccess1) { node->SetMetaStore(mockMetaStore_.release()); MockSnapshotReader reader; - EXPECT_CALL(reader, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(*mockfs_, FileExists(_)) - .WillOnce(Return(false)); - EXPECT_CALL(*mockfs_, Open(_, _)) - .Times(0); - EXPECT_CALL(*mockMetaStore, Clear()) - .Times(1); + EXPECT_CALL(reader, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(*mockfs_, FileExists(_)).WillOnce(Return(false)); + EXPECT_CALL(*mockfs_, Open(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore, Clear()).Times(1); EXPECT_CALL(*mockMetaStore, Load(_)) - .WillOnce(Invoke([](const std::string& pathname){ + .WillOnce(Invoke([](const std::string& pathname) { sleep(3); return true; })); - EXPECT_CALL(reader, load_meta(_)) - .Times(1); + EXPECT_CALL(reader, load_meta(_)).Times(1); std::thread thread1(RunOnSnapshotLoad, node, &reader, 0); std::thread thread2(RunGetPartitionInfoList, node, 3, false); @@ -458,23 +480,17 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotLoadTest_MetaStoreLoadSuccess2) { node->SetMetaStore(mockMetaStore_.release()); MockSnapshotReader reader; - EXPECT_CALL(reader, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(*mockfs_, FileExists(_)) - .WillOnce(Return(false)); - EXPECT_CALL(*mockfs_, Open(_, _)) - .Times(0); - EXPECT_CALL(*mockMetaStore, Clear()) - .Times(1); + EXPECT_CALL(reader, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(*mockfs_, FileExists(_)).WillOnce(Return(false)); + EXPECT_CALL(*mockfs_, Open(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore, Clear()).Times(1); EXPECT_CALL(*mockMetaStore, Load(_)) - .WillOnce(Invoke([](const std::string& pathname){ + .WillOnce(Invoke([](const std::string& pathname) { sleep(1); return true; })); - EXPECT_CALL(reader, load_meta(_)) - .Times(1); - EXPECT_CALL(*mockMetaStore, GetPartitionInfoList(_)) - .WillOnce(Return(true)); + EXPECT_CALL(reader, load_meta(_)).Times(1); + EXPECT_CALL(*mockMetaStore, GetPartitionInfoList(_)).WillOnce(Return(true)); EXPECT_CALL(*mockMetaStore, GetPartitionSnap(_)) .Times(2) .WillOnce(Return(false)) @@ -502,23 +518,17 @@ TEST_F(CopysetNodeRaftSnapshotTest, SnapshotLoadTest_MetaStoreLoadSuccess3) { node->SetMetaStore(mockMetaStore_.release()); MockSnapshotReader reader; - EXPECT_CALL(reader, get_path()) - .WillRepeatedly(Return(dataPath_)); - EXPECT_CALL(*mockfs_, FileExists(_)) - .WillOnce(Return(false)); - EXPECT_CALL(*mockfs_, Open(_, _)) - .Times(0); - EXPECT_CALL(*mockMetaStore, Clear()) - .Times(1); + EXPECT_CALL(reader, get_path()).WillRepeatedly(Return(dataPath_)); + EXPECT_CALL(*mockfs_, FileExists(_)).WillOnce(Return(false)); + EXPECT_CALL(*mockfs_, Open(_, _)).Times(0); + EXPECT_CALL(*mockMetaStore, Clear()).Times(1); EXPECT_CALL(*mockMetaStore, Load(_)) - .WillOnce(Invoke([](const std::string& pathname){ + .WillOnce(Invoke([](const std::string& pathname) { sleep(1); return true; })); - EXPECT_CALL(reader, load_meta(_)) - .Times(1); - EXPECT_CALL(*mockMetaStore, GetPartitionInfoList(_)) - .WillOnce(Return(true)); + EXPECT_CALL(reader, load_meta(_)).Times(1); + EXPECT_CALL(*mockMetaStore, GetPartitionInfoList(_)).WillOnce(Return(true)); std::thread thread1(RunOnSnapshotLoad, node, &reader, 2); std::thread thread2(RunGetPartitionInfoList, node, 1, true); diff --git a/curvefs/test/metaserver/copyset/meta_operator_test.cpp b/curvefs/test/metaserver/copyset/meta_operator_test.cpp index d1f554ff3f..06f45a0770 100644 --- a/curvefs/test/metaserver/copyset/meta_operator_test.cpp +++ b/curvefs/test/metaserver/copyset/meta_operator_test.cpp @@ -45,7 +45,7 @@ const int kDummyServerPort = 32000; template -MetaStatusCode FakeOnApplyFunc(const RequestT *request, ResponseT *response, +MetaStatusCode FakeOnApplyFunc(const RequestT* request, ResponseT* response, std::int64_t logIndex) { (void)logIndex; response->set_statuscode(code); @@ -73,8 +73,8 @@ class FakeClosure : public google::protobuf::Closure { bool runned_ = false; }; -std::string Exec(const std::string &cmd) { - FILE *pipe = popen(cmd.c_str(), "r"); +std::string Exec(const std::string& cmd) { + FILE* pipe = popen(cmd.c_str(), "r"); if (!pipe) { return "ERROR"; } @@ -104,7 +104,7 @@ class MetaOperatorTest : public testing::Test { ASSERT_EQ(0, brpc::StartDummyServerAt(kDummyServerPort)); } - static bool CheckMetric(const std::string &curlCmd, uint64_t expectValue) { + static bool CheckMetric(const std::string& curlCmd, uint64_t expectValue) { std::string cmdResult = Exec(curlCmd); if (cmdResult.empty() || cmdResult == "ERROR") { return false; @@ -163,7 +163,7 @@ TEST_F(MetaOperatorTest, OnApplyErrorTest) { braft::Configuration conf; CopysetNode node(poolId, copysetId, conf, &mockNodeManager_); - mock::MockMetaStore *mockMetaStore = new mock::MockMetaStore(); + mock::MockMetaStore* mockMetaStore = new mock::MockMetaStore(); node.SetMetaStore(mockMetaStore); ON_CALL(*mockMetaStore, Clear()).WillByDefault(Return(true)); @@ -205,9 +205,9 @@ TEST_F(MetaOperatorTest, OnApplyErrorTest) { // it's only for GetOrModifyS3ChunkInfo() { EXPECT_CALL(*mockMetaStore, GetOrModifyS3ChunkInfo(_, _, _, _)) - .WillOnce(Invoke([&](const GetOrModifyS3ChunkInfoRequest *request, - GetOrModifyS3ChunkInfoResponse *response, - std::shared_ptr *iterator, + .WillOnce(Invoke([&](const GetOrModifyS3ChunkInfoRequest* request, + GetOrModifyS3ChunkInfoResponse* response, + std::shared_ptr* iterator, std::int64_t logIndex) { (void)logIndex; response->set_statuscode(MetaStatusCode::UNKNOWN_ERROR); @@ -291,7 +291,7 @@ TEST_F(MetaOperatorTest, OnApplyFromLogErrorTest) { braft::Configuration conf; CopysetNode node(poolId, copysetId, conf, &mockNodeManager_); - mock::MockMetaStore *mockMetaStore = new mock::MockMetaStore(); + mock::MockMetaStore* mockMetaStore = new mock::MockMetaStore(); node.SetMetaStore(mockMetaStore); ON_CALL(*mockMetaStore, Clear()).WillByDefault(Return(true)); @@ -375,30 +375,26 @@ TEST_F(MetaOperatorTest, OnApplyFromLogErrorTest) { "/vars | grep " "op_apply_from_log_pool_100_copyset_100_delete_inode_total_error", 1)); - EXPECT_TRUE( - CheckMetric("curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + - "/vars | grep " - "op_apply_from_log_pool_100_copyset_100_create_root_" - "inode_total_error", - 1)); - EXPECT_TRUE( - CheckMetric("curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + - "/vars | grep " - "op_apply_from_log_pool_100_copyset_100_create_" - "partition_total_error", - 1)); - EXPECT_TRUE( - CheckMetric("curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + - "/vars | grep " - "op_apply_from_log_pool_100_copyset_100_delete_" - "partition_total_error", - 1)); - EXPECT_TRUE( - CheckMetric("curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + - "/vars | grep " - "op_apply_from_log_pool_100_copyset_100_prepare_rename_" - "tx_total_error", - 1)); + EXPECT_TRUE(CheckMetric( + "curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + + "/vars | grep " + "op_apply_from_log_pool_100_copyset_100_create_root_inode_total_error", + 1)); + EXPECT_TRUE(CheckMetric( + "curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + + "/vars | grep " + "op_apply_from_log_pool_100_copyset_100_create_partition_total_error", + 1)); + EXPECT_TRUE(CheckMetric( + "curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + + "/vars | grep " + "op_apply_from_log_pool_100_copyset_100_delete_partition_total_error", + 1)); + EXPECT_TRUE(CheckMetric( + "curl -s 0.0.0.0:" + std::to_string(kDummyServerPort) + + "/vars | grep " + "op_apply_from_log_pool_100_copyset_100_prepare_rename_tx_total_error", + 1)); } TEST_F(MetaOperatorTest, PropostTest_IsNotLeader) { @@ -436,15 +432,19 @@ TEST_F(MetaOperatorTest, PropostTest_RequestCanBypassProcess) { EXPECT_CALL(localFs, Mkdir(_)).WillOnce(Return(0)); EXPECT_TRUE(node.Init(options)); - auto *mockMetaStore = new mock::MockMetaStore(); + auto* mockMetaStore = new mock::MockMetaStore(); node.SetMetaStore(mockMetaStore); - auto *mockRaftNode = new MockRaftNode(); + auto* mockRaftNode = new MockRaftNode(); node.SetRaftNode(mockRaftNode); - ON_CALL(*mockMetaStore, Clear()).WillByDefault(Return(true)); - EXPECT_CALL(*mockRaftNode, apply(_)).Times(0); - EXPECT_CALL(*mockRaftNode, shutdown(_)).Times(AtLeast(1)); - EXPECT_CALL(*mockRaftNode, join()).Times(AtLeast(1)); + ON_CALL(*mockMetaStore, Clear()) + .WillByDefault(Return(true)); + EXPECT_CALL(*mockRaftNode, apply(_)) + .Times(0); + EXPECT_CALL(*mockRaftNode, shutdown(_)) + .Times(AtLeast(1)); + EXPECT_CALL(*mockRaftNode, join()) + .Times(AtLeast(1)); EXPECT_CALL(*mockMetaStore, GetDentry(_, _, _)) .WillOnce(Return(MetaStatusCode::OK)); @@ -486,10 +486,11 @@ TEST_F(MetaOperatorTest, PropostTest_IsNotLeaseLeader) { options.localFileSystem = &localFs; options.storageOptions.type = "memory"; - EXPECT_CALL(localFs, Mkdir(_)).WillOnce(Return(0)); + EXPECT_CALL(localFs, Mkdir(_)) + .WillOnce(Return(0)); EXPECT_TRUE(node.Init(options)); - auto *mockRaftNode = new MockRaftNode(); + auto* mockRaftNode = new MockRaftNode(); node.SetRaftNode(mockRaftNode); node.on_leader_start(1); @@ -521,10 +522,11 @@ TEST_F(MetaOperatorTest, PropostTest_PropostTaskFailed) { options.localFileSystem = &localFs; options.storageOptions.type = "memory"; - EXPECT_CALL(localFs, Mkdir(_)).WillOnce(Return(0)); + EXPECT_CALL(localFs, Mkdir(_)) + .WillOnce(Return(0)); EXPECT_TRUE(node.Init(options)); - auto *mockRaftNode = new MockRaftNode(); + auto* mockRaftNode = new MockRaftNode(); node.SetRaftNode(mockRaftNode); node.on_leader_start(1); diff --git a/curvefs/test/metaserver/dentry_manager_test.cpp b/curvefs/test/metaserver/dentry_manager_test.cpp index 85dbd0a380..87eea08c51 100644 --- a/curvefs/test/metaserver/dentry_manager_test.cpp +++ b/curvefs/test/metaserver/dentry_manager_test.cpp @@ -65,6 +65,8 @@ class DentryManagerTest : public ::testing::Test { txManager_ = std::make_shared(dentryStorage_, partitionInfo); dentryManager_ = std::make_shared(dentryStorage_, txManager_); + ASSERT_TRUE(dentryManager_->Init()); + ASSERT_TRUE(txManager_->Init()); logIndex_ = 0; } @@ -73,7 +75,7 @@ class DentryManagerTest : public ::testing::Test { ASSERT_EQ(0, localfs->Delete(kBaseTestDir)); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -87,8 +89,12 @@ class DentryManagerTest : public ::testing::Test { return result; } - Dentry GenDentry(uint32_t fsId, uint64_t parentId, const std::string &name, - uint64_t txId, uint64_t inodeId, bool deleteMarkFlag) { + Dentry GenDentry(uint32_t fsId, + uint64_t parentId, + const std::string& name, + uint64_t txId, + uint64_t inodeId, + bool deleteMarkFlag) { Dentry dentry; dentry.set_fsid(fsId); dentry.set_parentinodeid(parentId); diff --git a/curvefs/test/metaserver/dentry_storage_test.cpp b/curvefs/test/metaserver/dentry_storage_test.cpp index 886b17ba22..89c1405826 100644 --- a/curvefs/test/metaserver/dentry_storage_test.cpp +++ b/curvefs/test/metaserver/dentry_storage_test.cpp @@ -63,7 +63,7 @@ class DentryStorageTest : public ::testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -77,7 +77,7 @@ class DentryStorageTest : public ::testing::Test { return result; } - Dentry GenDentry(uint32_t fsId, uint64_t parentId, const std::string &name, + Dentry GenDentry(uint32_t fsId, uint64_t parentId, const std::string& name, uint64_t txId, uint64_t inodeId, bool deleteMarkFlag, FsFileType type = FsFileType::TYPE_FILE) { Dentry dentry; @@ -91,8 +91,8 @@ class DentryStorageTest : public ::testing::Test { return dentry; } - void InsertDentrys(DentryStorage *storage, - const std::vector &&dentrys) { + void InsertDentrys(DentryStorage* storage, + const std::vector&& dentrys) { // NOTE: store real transaction is unnecessary metaserver::TransactionRequest request; request.set_type(0); @@ -103,8 +103,8 @@ class DentryStorageTest : public ::testing::Test { ASSERT_EQ(storage->Size(), dentrys.size()); } - void ASSERT_DENTRYS_EQ(const std::vector &lhs, - const std::vector &&rhs) { + void ASSERT_DENTRYS_EQ(const std::vector& lhs, + const std::vector&& rhs) { ASSERT_EQ(lhs, rhs); } @@ -117,7 +117,7 @@ class DentryStorageTest : public ::testing::Test { TEST_F(DentryStorageTest, Insert) { DentryStorage storage(kvStorage_, nameGenerator_, 0); - + ASSERT_TRUE(storage.Init()); Dentry dentry; dentry.set_fsid(1); dentry.set_parentinodeid(1); @@ -163,6 +163,7 @@ TEST_F(DentryStorageTest, Insert) { TEST_F(DentryStorageTest, Delete) { DentryStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); // NOTE: store real transaction is unnecessary metaserver::TransactionRequest request; @@ -235,6 +236,7 @@ TEST_F(DentryStorageTest, Delete) { TEST_F(DentryStorageTest, Get) { DentryStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); Dentry dentry; // CASE 1: dentry not found @@ -242,8 +244,7 @@ TEST_F(DentryStorageTest, Get) { ASSERT_EQ(storage.Get(&dentry), MetaStatusCode::NOT_FOUND); // CASE 2: get success - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "B", 0, 2, false), @@ -261,8 +262,7 @@ TEST_F(DentryStorageTest, Get) { // CASE 3: get multi-dentrys with different txid storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "A", 1, 2, false), @@ -275,8 +275,7 @@ TEST_F(DentryStorageTest, Get) { // CASE 4: get dentry with DELETE_MARK_FLAG flag storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "A", 1, 1, true), @@ -290,12 +289,12 @@ TEST_F(DentryStorageTest, Get) { TEST_F(DentryStorageTest, List) { DentryStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); std::vector dentrys; Dentry dentry; // CASE 1: basic list - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A1", 0, 1, false), GenDentry(1, 0, "A2", 0, 2, false), @@ -308,12 +307,12 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 5); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 0, "A1", 0, 1, false), - GenDentry(1, 0, "A2", 0, 2, false), - GenDentry(1, 0, "A3", 0, 3, false), - GenDentry(1, 0, "A4", 0, 4, false), - GenDentry(1, 0, "A5", 0, 5, false), - }); + GenDentry(1, 0, "A1", 0, 1, false), + GenDentry(1, 0, "A2", 0, 2, false), + GenDentry(1, 0, "A3", 0, 3, false), + GenDentry(1, 0, "A4", 0, 4, false), + GenDentry(1, 0, "A5", 0, 5, false), + }); // CASE 2: list by specify name dentrys.clear(); @@ -321,14 +320,13 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 2); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 0, "A4", 0, 4, false), - GenDentry(1, 0, "A5", 0, 5, false), - }); + GenDentry(1, 0, "A4", 0, 4, false), + GenDentry(1, 0, "A5", 0, 5, false), + }); // CASE 3: list by lower txid storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A1", 1, 1, false), GenDentry(1, 0, "A2", 2, 2, false), @@ -340,9 +338,9 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 2); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 0, "A1", 1, 1, false), - GenDentry(1, 0, "A2", 2, 2, false), - }); + GenDentry(1, 0, "A1", 1, 1, false), + GenDentry(1, 0, "A2", 2, 2, false), + }); // CASE 4: list by higher txid dentrys.clear(); @@ -350,15 +348,14 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 3); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 0, "A1", 1, 1, false), - GenDentry(1, 0, "A2", 2, 2, false), - GenDentry(1, 0, "A3", 3, 3, false), - }); + GenDentry(1, 0, "A1", 1, 1, false), + GenDentry(1, 0, "A2", 2, 2, false), + GenDentry(1, 0, "A3", 3, 3, false), + }); // CASE 5: list dentrys which has DELETE_MARK_FLAG flag storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A1", 1, 1, false), GenDentry(1, 0, "A2", 2, 2, true), @@ -370,14 +367,13 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 2); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 0, "A1", 1, 1, false), - GenDentry(1, 0, "A3", 3, 3, false), - }); + GenDentry(1, 0, "A1", 1, 1, false), + GenDentry(1, 0, "A3", 3, 3, false), + }); // CASE 6: list same dentrys with different txid storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "A", 1, 1, false), @@ -389,13 +385,12 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 1); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 0, "A", 2, 1, false), - }); + GenDentry(1, 0, "A", 2, 1, false), + }); // CASE 7: list by dentry tree storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "B", 0, 2, false), @@ -411,23 +406,22 @@ TEST_F(DentryStorageTest, List) { ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 3); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 2, "C", 0, 3, false), - GenDentry(1, 2, "D", 0, 4, false), - GenDentry(1, 2, "E", 0, 5, false), - }); + GenDentry(1, 2, "C", 0, 3, false), + GenDentry(1, 2, "D", 0, 4, false), + GenDentry(1, 2, "E", 0, 5, false), + }); dentrys.clear(); dentry = GenDentry(1, 4, "", 0, 0, false); ASSERT_EQ(storage.List(dentry, &dentrys, 0), MetaStatusCode::OK); ASSERT_EQ(dentrys.size(), 1); ASSERT_DENTRYS_EQ(dentrys, std::vector{ - GenDentry(1, 4, "G", 0, 7, false), - }); + GenDentry(1, 4, "G", 0, 7, false), + }); // CASE 8: list empty directory storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "B", 0, 2, false), @@ -469,8 +463,7 @@ TEST_F(DentryStorageTest, List) { // CASE 10: list directory only with limit storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "B", 0, 2, false), @@ -500,6 +493,7 @@ TEST_F(DentryStorageTest, List) { TEST_F(DentryStorageTest, HandleTx) { DentryStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); std::vector dentrys; Dentry dentry; // NOTE: store real transaction is unnecessary @@ -507,8 +501,7 @@ TEST_F(DentryStorageTest, HandleTx) { request.set_type(0); request.set_rawpayload(""); // CASE 1: prepare success - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), }); @@ -537,8 +530,7 @@ TEST_F(DentryStorageTest, HandleTx) { // CASE 3: commit dentry with DELETE_MARK_FLAG flag storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "A", 1, 1, true), @@ -551,8 +543,7 @@ TEST_F(DentryStorageTest, HandleTx) { // CASE 4: Rollback success storage.Clear(); - InsertDentrys(&storage, - std::vector{ + InsertDentrys(&storage, std::vector{ // { fsId, parentId, name, txId, inodeId, deleteMarkFlag } GenDentry(1, 0, "A", 0, 1, false), GenDentry(1, 0, "A", 1, 2, false), diff --git a/curvefs/test/metaserver/inode_manager_test.cpp b/curvefs/test/metaserver/inode_manager_test.cpp index a322d694d8..65797169c5 100644 --- a/curvefs/test/metaserver/inode_manager_test.cpp +++ b/curvefs/test/metaserver/inode_manager_test.cpp @@ -77,6 +77,7 @@ class InodeManagerTest : public ::testing::Test { filetype2InodeNum_ = std::make_shared(); manager = std::make_shared(inodeStorage, trash, filetype2InodeNum_.get()); + ASSERT_TRUE(manager->Init()); param_.fsId = 1; param_.length = 100; @@ -97,7 +98,7 @@ class InodeManagerTest : public ::testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -111,7 +112,7 @@ class InodeManagerTest : public ::testing::Test { return result; } - bool CompareInode(const Inode &first, const Inode &second) { + bool CompareInode(const Inode& first, const Inode& second) { return first.fsid() == second.fsid() && first.atime() == second.atime() && first.inodeid() == second.inodeid() && @@ -124,15 +125,16 @@ class InodeManagerTest : public ::testing::Test { first.nlink() == second.nlink(); } - bool EqualS3ChunkInfo(const S3ChunkInfo &lhs, const S3ChunkInfo &rhs) { + bool EqualS3ChunkInfo(const S3ChunkInfo& lhs, const S3ChunkInfo& rhs) { return lhs.chunkid() == rhs.chunkid() && lhs.compaction() == rhs.compaction() && - lhs.offset() == rhs.offset() && lhs.len() == rhs.len() && + lhs.offset() == rhs.offset() && + lhs.len() == rhs.len() && lhs.size() == rhs.size() && lhs.zero() == rhs.zero(); } - bool EqualS3ChunkInfoList(const S3ChunkInfoList &lhs, - const S3ChunkInfoList &rhs) { + bool EqualS3ChunkInfoList(const S3ChunkInfoList& lhs, + const S3ChunkInfoList& rhs) { size_t size = lhs.s3chunks_size(); if (size != rhs.s3chunks_size()) { return false; @@ -150,7 +152,7 @@ class InodeManagerTest : public ::testing::Test { uint64_t lastChunkId) { S3ChunkInfoList list; for (uint64_t id = firstChunkId; id <= lastChunkId; id++) { - S3ChunkInfo *info = list.add_s3chunks(); + S3ChunkInfo* info = list.add_s3chunks(); info->set_chunkid(id); info->set_compaction(0); info->set_offset(0); @@ -161,10 +163,10 @@ class InodeManagerTest : public ::testing::Test { return list; } - void - CHECK_ITERATOR_S3CHUNKINFOLIST(std::shared_ptr iterator, - const std::vector chunkIndexs, - const std::vector lists) { + void CHECK_ITERATOR_S3CHUNKINFOLIST( + std::shared_ptr iterator, + const std::vector chunkIndexs, + const std::vector lists) { size_t size = 0; Key4S3ChunkInfoList key; S3ChunkInfoList list4get; @@ -295,7 +297,8 @@ TEST_F(InodeManagerTest, GetOrModifyS3ChunkInfo) { fsId, inodeId, map2add, map2del, true, &iterator, logIndex_++); ASSERT_EQ(rc, MetaStatusCode::OK); - CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, std::vector{1, 2, 3}, + CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, + std::vector{1, 2, 3}, std::vector{ GenS3ChunkInfoList(1, 1), GenS3ChunkInfoList(2, 2), @@ -307,7 +310,8 @@ TEST_F(InodeManagerTest, GetOrModifyS3ChunkInfo) { true, &iterator, logIndex_++); ASSERT_EQ(rc, MetaStatusCode::OK); - CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, std::vector{1, 2, 3}, + CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, + std::vector{1, 2, 3}, std::vector{ GenS3ChunkInfoList(1, 1), GenS3ChunkInfoList(2, 2), @@ -330,7 +334,8 @@ TEST_F(InodeManagerTest, GetOrModifyS3ChunkInfo) { fsId, inodeId, map2add, map2del, true, &iterator, logIndex_++); ASSERT_EQ(rc, MetaStatusCode::OK); - CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, std::vector{}, + CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, + std::vector{}, std::vector{}); } @@ -374,15 +379,15 @@ TEST_F(InodeManagerTest, GetOrModifyS3ChunkInfo) { ASSERT_EQ(iterator->Status(), 0); CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, - std::vector{0, 1, 2, 7, 8, 9}, - std::vector{ - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(1, 100), - GenS3ChunkInfoList(1, 100), - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - }); + std::vector{ 0, 1, 2, 7, 8, 9 }, + std::vector{ + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(1, 100), + GenS3ChunkInfoList(1, 100), + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + }); // step3: delete all s3chunkinfo map2add.clear(); @@ -399,15 +404,15 @@ TEST_F(InodeManagerTest, GetOrModifyS3ChunkInfo) { ASSERT_EQ(iterator->Status(), 0); CHECK_ITERATOR_S3CHUNKINFOLIST(iterator, - std::vector{0, 1, 2, 7, 8, 9}, - std::vector{ - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - GenS3ChunkInfoList(100, 100), - }); + std::vector{ 0, 1, 2, 7, 8, 9 }, + std::vector{ + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + GenS3ChunkInfoList(100, 100), + }); } } diff --git a/curvefs/test/metaserver/inode_storage_test.cpp b/curvefs/test/metaserver/inode_storage_test.cpp index 6b51bd0a9c..e75e518786 100644 --- a/curvefs/test/metaserver/inode_storage_test.cpp +++ b/curvefs/test/metaserver/inode_storage_test.cpp @@ -87,7 +87,7 @@ class InodeStorageTest : public ::testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -101,7 +101,7 @@ class InodeStorageTest : public ::testing::Test { return result; } - bool CompareInode(const Inode &first, const Inode &second) { + bool CompareInode(const Inode& first, const Inode& second) { return first.fsid() == second.fsid() && first.atime() == second.atime() && first.inodeid() == second.inodeid(); @@ -130,7 +130,7 @@ class InodeStorageTest : public ::testing::Test { uint64_t lastChunkId) { S3ChunkInfoList list; for (uint64_t id = firstChunkId; id <= lastChunkId; id++) { - S3ChunkInfo *info = list.add_s3chunks(); + S3ChunkInfo* info = list.add_s3chunks(); info->set_chunkid(id); info->set_compaction(0); info->set_offset(0); @@ -141,15 +141,15 @@ class InodeStorageTest : public ::testing::Test { return list; } - bool EqualS3ChunkInfo(const S3ChunkInfo &lhs, const S3ChunkInfo &rhs) { + bool EqualS3ChunkInfo(const S3ChunkInfo& lhs, const S3ChunkInfo& rhs) { return lhs.chunkid() == rhs.chunkid() && lhs.compaction() == rhs.compaction() && lhs.offset() == rhs.offset() && lhs.len() == rhs.len() && lhs.size() == rhs.size() && lhs.zero() == rhs.zero(); } - bool EqualS3ChunkInfoList(const S3ChunkInfoList &lhs, - const S3ChunkInfoList &rhs) { + bool EqualS3ChunkInfoList(const S3ChunkInfoList& lhs, + const S3ChunkInfoList& rhs) { size_t size = lhs.s3chunks_size(); if (size != rhs.s3chunks_size()) { return false; @@ -163,7 +163,7 @@ class InodeStorageTest : public ::testing::Test { return true; } - void CHECK_INODE_S3CHUNKINFOLIST(InodeStorage *storage, uint32_t fsId, + void CHECK_INODE_S3CHUNKINFOLIST(InodeStorage* storage, uint32_t fsId, uint64_t inodeId, const std::vector chunkIndexs, const std::vector lists) { @@ -195,6 +195,7 @@ class InodeStorageTest : public ::testing::Test { TEST_F(InodeStorageTest, test1) { InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); Inode inode1 = GenInode(1, 1); Inode inode2 = GenInode(2, 2); Inode inode3 = GenInode(3, 3); @@ -244,6 +245,7 @@ TEST_F(InodeStorageTest, test1) { TEST_F(InodeStorageTest, testGetAttrNotFound) { InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); Inode inode; inode.set_fsid(1); inode.set_inodeid(1); @@ -268,6 +270,7 @@ TEST_F(InodeStorageTest, testGetAttrNotFound) { TEST_F(InodeStorageTest, testGetAttr) { InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); Inode inode; inode.set_fsid(1); inode.set_inodeid(1); @@ -295,6 +298,7 @@ TEST_F(InodeStorageTest, testGetAttr) { TEST_F(InodeStorageTest, testGetXAttr) { InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); Inode inode; inode.set_fsid(1); inode.set_inodeid(1); @@ -340,6 +344,7 @@ TEST_F(InodeStorageTest, ModifyInodeS3ChunkInfoList) { uint32_t fsId = 1; uint64_t inodeId = 1; InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); // CASE 1: get empty s3chunkinfo { @@ -635,6 +640,7 @@ TEST_F(InodeStorageTest, PaddingInodeS3ChunkInfo) { uint32_t fsId = 1; uint64_t inodeId = 1; InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); S3ChunkInfoList list2del; // step1: insert inode @@ -701,6 +707,7 @@ TEST_F(InodeStorageTest, PaddingInodeS3ChunkInfo) { TEST_F(InodeStorageTest, GetAllS3ChunkInfoList) { InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); uint64_t chunkIndex = 1; S3ChunkInfoList list2add = GenS3ChunkInfoList(1, 10); @@ -743,7 +750,7 @@ TEST_F(InodeStorageTest, TestUpdateVolumeExtentSlice) { {MetaStatusCode::OK, Status::OK()}, {MetaStatusCode::STORAGE_INTERNAL_ERROR, Status::InternalError()}}; - for (const auto &test : cases) { + for (const auto& test : cases) { auto kvStorage = std::make_shared(); InodeStorage storage(kvStorage, nameGenerator_, 0); @@ -780,7 +787,7 @@ TEST_F(InodeStorageTest, TestUpdateVolumeExtentSlice) { } } -static void RandomSetExtent(VolumeExtent *ext) { +static void RandomSetExtent(VolumeExtent* ext) { std::random_device rd; ext->set_fsoffset(rd()); @@ -789,9 +796,9 @@ static void RandomSetExtent(VolumeExtent *ext) { ext->set_isused(rd() & 1); } -static bool PrepareGetAllVolumeExtentTest(InodeStorage *storage, uint32_t fsId, +static bool PrepareGetAllVolumeExtentTest(InodeStorage* storage, uint32_t fsId, uint64_t inodeId, - std::vector *out, + std::vector* out, std::int64_t logIndex) { VolumeExtentSlice slice1; slice1.set_offset(0); @@ -828,22 +835,22 @@ static bool PrepareGetAllVolumeExtentTest(InodeStorage *storage, uint32_t fsId, return true; } -static bool operator==(const VolumeExtentSliceList &list, - const std::vector &slices) { +static bool operator==(const VolumeExtentSliceList& list, + const std::vector& slices) { std::vector clist(list.slices().begin(), list.slices().end()); auto copy = slices; std::sort(copy.begin(), copy.end(), - [](const VolumeExtentSlice &s1, const VolumeExtentSlice &s2) { + [](const VolumeExtentSlice& s1, const VolumeExtentSlice& s2) { return s1.offset() < s2.offset(); }); return true; } -static bool operator==(const VolumeExtentSlice &s1, - const VolumeExtentSlice &s2) { +static bool operator==(const VolumeExtentSlice& s1, + const VolumeExtentSlice& s2) { return google::protobuf::util::MessageDifferencer::Equals(s1, s2); } @@ -855,7 +862,7 @@ TEST_F(InodeStorageTest, TestGetAllVolumeExtent) { std::make_shared(opts); std::shared_ptr kvStore = kvStorage_; - for (auto &store : {memStore, kvStore}) { + for (auto& store : {memStore, kvStore}) { InodeStorage storage(store, nameGenerator_, 0); const uint32_t fsId = 1; const uint64_t inodeId = 2; @@ -899,8 +906,9 @@ TEST_F(InodeStorageTest, TestGetVolumeExtentByOffset) { std::make_shared(opts); std::shared_ptr kvStore = kvStorage_; - for (auto &store : {kvStorage_, memStore}) { + for (auto& store : {kvStorage_, memStore}) { InodeStorage storage(store, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); const uint32_t fsId = 1; const uint64_t inodeId = 2; @@ -946,6 +954,7 @@ TEST_F(InodeStorageTest, Test_UpdateDeallocatableBlockGroup) { uint32_t fsId = 1; auto tableName = nameGenerator_->GetDeallocatableBlockGroupTableName(); InodeStorage storage(kvStorage_, nameGenerator_, 0); + ASSERT_TRUE(storage.Init()); uint64_t increaseSize = 100 * 1024; uint64_t decreaseSize = 4 * 1024; Key4DeallocatableBlockGroup key(fsId, blockGroupOffset); diff --git a/curvefs/test/metaserver/metastore_test.cpp b/curvefs/test/metaserver/metastore_test.cpp index 45ba5c6c06..435546c4e5 100644 --- a/curvefs/test/metaserver/metastore_test.cpp +++ b/curvefs/test/metaserver/metastore_test.cpp @@ -64,12 +64,12 @@ namespace { class MockSnapshotWriter : public braft::SnapshotWriter { public: MOCK_METHOD0(get_path, std::string()); - MOCK_METHOD1(list_files, void(std::vector *)); - MOCK_METHOD1(save_meta, int(const braft::SnapshotMeta &)); - MOCK_METHOD1(add_file, int(const std::string &)); + MOCK_METHOD1(list_files, void(std::vector*)); + MOCK_METHOD1(save_meta, int(const braft::SnapshotMeta&)); + MOCK_METHOD1(add_file, int(const std::string&)); MOCK_METHOD2(add_file, - int(const std::string &, const google::protobuf::Message *)); - MOCK_METHOD1(remove_file, int(const std::string &)); + int(const std::string&, const google::protobuf::Message*)); + MOCK_METHOD1(remove_file, int(const std::string&)); }; curve::common::UUIDGenerator uuid; @@ -110,7 +110,7 @@ class MetastoreTest : public ::testing::Test { ASSERT_TRUE(0 == localfs->Delete(dataDir_) || errno == ENOENT); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -124,12 +124,16 @@ class MetastoreTest : public ::testing::Test { return result; } - bool CompareInode(const Inode &first, const Inode &second) { - uint64_t firstMtime = first.mtime() * 1000000000u + first.mtime_ns(); - uint64_t secondMtime = second.mtime() * 1000000000u + second.mtime_ns(); + bool CompareInode(const Inode& first, const Inode& second) { + uint64_t firstMtime = first.mtime() * 1000000000u + + first.mtime_ns(); + uint64_t secondMtime = second.mtime() * 1000000000u + + second.mtime_ns(); - uint64_t firstCtime = first.ctime() * 1000000000u + first.ctime_ns(); - uint64_t secondCtime = second.ctime() * 1000000000u + second.ctime_ns(); + uint64_t firstCtime = first.ctime() * 1000000000u + + first.ctime_ns(); + uint64_t secondCtime = second.ctime() * 1000000000u + + second.ctime_ns(); return first.fsid() == second.fsid() && first.atime() == second.atime() && @@ -138,19 +142,20 @@ class MetastoreTest : public ::testing::Test { first.length() == second.length() && first.uid() == second.uid() && first.gid() == second.gid() && first.mode() == second.mode() && first.type() == second.type() && - firstMtime >= secondMtime && firstCtime >= secondCtime && + firstMtime >= secondMtime && + firstCtime >= secondCtime && first.symlink() == second.symlink() && first.nlink() >= second.nlink(); } - void PrintDentry(const Dentry &dentry) { + void PrintDentry(const Dentry& dentry) { LOG(INFO) << "dentry: fsid = " << dentry.fsid() << ", inodeid = " << dentry.inodeid() << ", name = " << dentry.name() << ", parentinodeid = " << dentry.parentinodeid(); } - bool CompareDentry(const Dentry &first, const Dentry &second) { + bool CompareDentry(const Dentry& first, const Dentry& second) { bool ret = first.fsid() == second.fsid() && first.inodeid() == second.inodeid() && first.parentinodeid() == second.parentinodeid() && @@ -162,8 +167,8 @@ class MetastoreTest : public ::testing::Test { return ret; } - bool ComparePartition(const PartitionInfo &first, - const PartitionInfo &second) { + bool ComparePartition(const PartitionInfo& first, + const PartitionInfo& second) { bool ret = first.fsid() == second.fsid() && first.poolid() == second.poolid() && first.copysetid() == second.copysetid() && @@ -181,15 +186,15 @@ class MetastoreTest : public ::testing::Test { return ret; } - bool EqualS3ChunkInfo(const S3ChunkInfo &lhs, const S3ChunkInfo &rhs) { + bool EqualS3ChunkInfo(const S3ChunkInfo& lhs, const S3ChunkInfo& rhs) { return lhs.chunkid() == rhs.chunkid() && lhs.compaction() == rhs.compaction() && lhs.offset() == rhs.offset() && lhs.len() == rhs.len() && lhs.size() == rhs.size() && lhs.zero() == rhs.zero(); } - bool EqualS3ChunkInfoList(const S3ChunkInfoList &lhs, - const S3ChunkInfoList &rhs) { + bool EqualS3ChunkInfoList(const S3ChunkInfoList& lhs, + const S3ChunkInfoList& rhs) { size_t size = lhs.s3chunks_size(); if (size != rhs.s3chunks_size()) { return false; @@ -207,7 +212,7 @@ class MetastoreTest : public ::testing::Test { uint64_t lastChunkId) { S3ChunkInfoList list; for (uint64_t id = firstChunkId; id <= lastChunkId; id++) { - S3ChunkInfo *info = list.add_s3chunks(); + S3ChunkInfo* info = list.add_s3chunks(); info->set_chunkid(id); info->set_compaction(0); info->set_offset(0); @@ -218,10 +223,10 @@ class MetastoreTest : public ::testing::Test { return list; } - void - CHECK_ITERATOR_S3CHUNKINFOLIST(std::shared_ptr iterator, - const std::vector chunkIndexs, - const std::vector lists) { + void CHECK_ITERATOR_S3CHUNKINFOLIST( + std::shared_ptr iterator, + const std::vector chunkIndexs, + const std::vector lists) { size_t size = 0; Key4S3ChunkInfoList key; S3ChunkInfoList list4get; @@ -259,7 +264,7 @@ class MetastoreTest : public ::testing::Test { } bool IsSuccess() { return ret_; } - braft::SnapshotWriter *GetSnapshotWriter() const override { + braft::SnapshotWriter* GetSnapshotWriter() const override { static MockSnapshotWriter mockWriter; return &mockWriter; } @@ -415,7 +420,7 @@ TEST_F(MetastoreTest, test_inode) { FsFileType type = FsFileType::TYPE_DIRECTORY; struct timespec now; clock_gettime(CLOCK_REALTIME, &now); - Time *tm = new Time(); + Time* tm = new Time(); tm->set_sec(now.tv_sec); tm->set_nsec(now.tv_nsec); @@ -976,7 +981,25 @@ TEST_F(MetastoreTest, persist_success) { // dump MetaStoreImpl to file OnSnapshotSaveDoneImpl done; LOG(INFO) << "MetastoreTest test Save"; - ASSERT_TRUE(metastore.Save(test_path_, &done)); + do { + brpc::ClosureGuard doneGuard(&done); + auto* writer = done.GetSnapshotWriter(); + std::vector files; + if (!metastore.SaveMeta(test_path_, &files)) { + LOG(ERROR) << "Save metadata failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + if (!metastore.SaveData(test_path_, &files)) { + LOG(ERROR) << "Save data failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + for (const auto& f : files) { + writer->add_file(f); + } + done.SetSuccess(); + } while (false); // wait meta save to file done.Wait(); @@ -1143,8 +1166,25 @@ TEST_F(MetastoreTest, persist_deleting_partition_success) { // dump MetaStoreImpl to file OnSnapshotSaveDoneImpl done; LOG(INFO) << "MetastoreTest test Save"; - ASSERT_TRUE(metastore.Save(test_path_, &done)); - + do { + brpc::ClosureGuard doneGuard(&done); + auto* writer = done.GetSnapshotWriter(); + std::vector files; + if (!metastore.SaveMeta(test_path_, &files)) { + LOG(ERROR) << "Save metadata failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + if (!metastore.SaveData(test_path_, &files)) { + LOG(ERROR) << "Save data failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + for (const auto& f : files) { + writer->add_file(f); + } + done.SetSuccess(); + } while (false); // wait meta save to file done.Wait(); ASSERT_TRUE(done.IsSuccess()); @@ -1197,7 +1237,25 @@ TEST_F(MetastoreTest, persist_partition_fail) { // dump MetaStoreImpl to file OnSnapshotSaveDoneImpl done; LOG(INFO) << "MetastoreTest test Save"; - ASSERT_FALSE(metastore.Save(test_path_, &done)); + do { + brpc::ClosureGuard doneGuard(&done); + auto* writer = done.GetSnapshotWriter(); + std::vector files; + if (!metastore.SaveMeta(test_path_, &files)) { + LOG(ERROR) << "Save metadata failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + if (!metastore.SaveData(test_path_, &files)) { + LOG(ERROR) << "Save data failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + for (const auto& f : files) { + writer->add_file(f); + } + done.SetSuccess(); + } while (false); // wait meta save to file done.Wait(); @@ -1281,7 +1339,25 @@ TEST_F(MetastoreTest, persist_dentry_fail) { // dump MetaStoreImpl to file OnSnapshotSaveDoneImpl done; LOG(INFO) << "MetastoreTest test Save"; - ASSERT_FALSE(metastore.Save(test_path_, &done)); + do { + brpc::ClosureGuard doneGuard(&done); + auto* writer = done.GetSnapshotWriter(); + std::vector files; + if (!metastore.SaveMeta(test_path_, &files)) { + LOG(ERROR) << "Save metadata failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + if (!metastore.SaveData(test_path_, &files)) { + LOG(ERROR) << "Save data failed"; + done.SetError(MetaStatusCode::SAVE_META_FAIL); + break; + } + for (const auto& f : files) { + writer->add_file(f); + } + done.SetSuccess(); + } while (false); // wait meta save to file done.Wait(); @@ -1808,7 +1884,7 @@ TEST_F(MetastoreTest, TestUpdateVolumeExtent_PartitionNotFound) { } // namespace metaserver } // namespace curvefs -int main(int argc, char **argv) { +int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleMock(&argc, argv); ::curvefs::common::Process::InitSetProcTitle(argc, argv); diff --git a/curvefs/test/metaserver/mock/mock_kv_storage.h b/curvefs/test/metaserver/mock/mock_kv_storage.h index e2f66e9eda..2923b42d02 100644 --- a/curvefs/test/metaserver/mock/mock_kv_storage.h +++ b/curvefs/test/metaserver/mock/mock_kv_storage.h @@ -39,35 +39,35 @@ namespace storage { class MockKVStorage : public KVStorage, public StorageTransaction { public: MOCK_METHOD3(HGet, - Status(const std::string &, const std::string &, ValueType *)); + Status(const std::string&, const std::string&, ValueType*)); - MOCK_METHOD3(HSet, Status(const std::string &, const std::string &, - const ValueType &)); + MOCK_METHOD3(HSet, Status(const std::string&, const std::string&, + const ValueType&)); - MOCK_METHOD2(HDel, Status(const std::string &, const std::string &)); + MOCK_METHOD2(HDel, Status(const std::string&, const std::string&)); - MOCK_METHOD1(HGetAll, std::shared_ptr(const std::string &)); + MOCK_METHOD1(HGetAll, std::shared_ptr(const std::string&)); - MOCK_METHOD1(HSize, size_t(const std::string &)); + MOCK_METHOD1(HSize, size_t(const std::string&)); - MOCK_METHOD1(HClear, Status(const std::string &)); + MOCK_METHOD1(HClear, Status(const std::string&)); MOCK_METHOD3(SGet, - Status(const std::string &, const std::string &, ValueType *)); + Status(const std::string&, const std::string&, ValueType*)); - MOCK_METHOD3(SSet, Status(const std::string &, const std::string &, - const ValueType &)); + MOCK_METHOD3(SSet, Status(const std::string&, const std::string&, + const ValueType&)); - MOCK_METHOD2(SDel, Status(const std::string &, const std::string &)); + MOCK_METHOD2(SDel, Status(const std::string&, const std::string&)); - MOCK_METHOD2(SSeek, std::shared_ptr(const std::string &, - const std::string &)); + MOCK_METHOD2(SSeek, std::shared_ptr(const std::string&, + const std::string&)); - MOCK_METHOD1(SGetAll, std::shared_ptr(const std::string &)); + MOCK_METHOD1(SGetAll, std::shared_ptr(const std::string&)); - MOCK_METHOD1(SSize, size_t(const std::string &)); + MOCK_METHOD1(SSize, size_t(const std::string&)); - MOCK_METHOD1(SClear, Status(const std::string &)); + MOCK_METHOD1(SClear, Status(const std::string&)); MOCK_METHOD0(Type, STORAGE_TYPE()); @@ -80,9 +80,9 @@ class MockKVStorage : public KVStorage, public StorageTransaction { MOCK_METHOD0(BeginTransaction, std::shared_ptr()); MOCK_METHOD2(Checkpoint, - bool(const std::string &, std::vector *)); + bool(const std::string&, std::vector*)); - MOCK_METHOD1(Recover, bool(const std::string &)); + MOCK_METHOD1(Recover, bool(const std::string&)); MOCK_METHOD0(Commit, Status()); diff --git a/curvefs/test/metaserver/mock/mock_metastore.h b/curvefs/test/metaserver/mock/mock_metastore.h index 0d0fa0d1a1..cc3b216d34 100644 --- a/curvefs/test/metaserver/mock/mock_metastore.h +++ b/curvefs/test/metaserver/mock/mock_metastore.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "curvefs/src/metaserver/metastore.h" @@ -38,89 +39,92 @@ namespace mock { class MockMetaStore : public curvefs::metaserver::MetaStore { public: - MOCK_METHOD1(Load, bool(const std::string &)); - MOCK_METHOD2(Save, bool(const std::string &, OnSnapshotSaveDoneClosure *)); + MOCK_METHOD1(Load, bool(const std::string&)); + MOCK_METHOD2(SaveMeta, + bool(const std::string&, std::vector* files)); + MOCK_METHOD2(SaveData, + bool(const std::string&, std::vector* files)); MOCK_METHOD0(Clear, bool()); MOCK_METHOD0(Destroy, bool()); - MOCK_METHOD3(CreatePartition, MetaStatusCode(const CreatePartitionRequest *, - CreatePartitionResponse *, + MOCK_METHOD3(CreatePartition, MetaStatusCode(const CreatePartitionRequest*, + CreatePartitionResponse*, std::int64_t logIndex)); - MOCK_METHOD3(DeletePartition, MetaStatusCode(const DeletePartitionRequest *, - DeletePartitionResponse *, + MOCK_METHOD3(DeletePartition, MetaStatusCode(const DeletePartitionRequest*, + DeletePartitionResponse*, std::int64_t logIndex)); - MOCK_METHOD1(GetPartitionInfoList, bool(std::list *)); + MOCK_METHOD1(GetPartitionInfoList, bool(std::list*)); MOCK_METHOD1(GetPartitionSnap, - bool(std::map> *)); + bool(std::map>*)); MOCK_METHOD3(CreateDentry, - MetaStatusCode(const CreateDentryRequest *, - CreateDentryResponse *, std::int64_t logIndex)); + MetaStatusCode(const CreateDentryRequest*, + CreateDentryResponse*, std::int64_t logIndex)); MOCK_METHOD3(DeleteDentry, - MetaStatusCode(const DeleteDentryRequest *, - DeleteDentryResponse *, std::int64_t logIndex)); + MetaStatusCode(const DeleteDentryRequest*, + DeleteDentryResponse*, std::int64_t logIndex)); MOCK_METHOD3(GetDentry, - MetaStatusCode(const GetDentryRequest *, GetDentryResponse *, + MetaStatusCode(const GetDentryRequest*, GetDentryResponse*, std::int64_t logIndex)); MOCK_METHOD3(ListDentry, - MetaStatusCode(const ListDentryRequest *, ListDentryResponse *, + MetaStatusCode(const ListDentryRequest*, ListDentryResponse*, std::int64_t logIndex)); MOCK_METHOD3(CreateInode, - MetaStatusCode(const CreateInodeRequest *, - CreateInodeResponse *, std::int64_t logIndex)); - MOCK_METHOD3(CreateRootInode, MetaStatusCode(const CreateRootInodeRequest *, - CreateRootInodeResponse *, + MetaStatusCode(const CreateInodeRequest*, CreateInodeResponse*, + std::int64_t logIndex)); + MOCK_METHOD3(CreateRootInode, MetaStatusCode(const CreateRootInodeRequest*, + CreateRootInodeResponse*, std::int64_t logIndex)); MOCK_METHOD3(CreateManageInode, - MetaStatusCode(const CreateManageInodeRequest *, - CreateManageInodeResponse *, + MetaStatusCode(const CreateManageInodeRequest*, + CreateManageInodeResponse*, std::int64_t logIndex)); MOCK_METHOD3(GetInode, - MetaStatusCode(const GetInodeRequest *, GetInodeResponse *, + MetaStatusCode(const GetInodeRequest*, GetInodeResponse*, std::int64_t logIndex)); MOCK_METHOD3(BatchGetInodeAttr, - MetaStatusCode(const BatchGetInodeAttrRequest *, - BatchGetInodeAttrResponse *, + MetaStatusCode(const BatchGetInodeAttrRequest*, + BatchGetInodeAttrResponse*, std::int64_t logIndex)); - MOCK_METHOD3(BatchGetXAttr, MetaStatusCode(const BatchGetXAttrRequest *, - BatchGetXAttrResponse *, - std::int64_t logIndex)); + MOCK_METHOD3(BatchGetXAttr, + MetaStatusCode(const BatchGetXAttrRequest*, + BatchGetXAttrResponse*, std::int64_t logIndex)); MOCK_METHOD3(DeleteInode, - MetaStatusCode(const DeleteInodeRequest *, - DeleteInodeResponse *, std::int64_t logIndex)); + MetaStatusCode(const DeleteInodeRequest*, DeleteInodeResponse*, + std::int64_t logIndex)); MOCK_METHOD3(UpdateInode, - MetaStatusCode(const UpdateInodeRequest *, - UpdateInodeResponse *, std::int64_t logIndex)); + MetaStatusCode(const UpdateInodeRequest*, UpdateInodeResponse*, + std::int64_t logIndex)); - MOCK_METHOD3(PrepareRenameTx, MetaStatusCode(const PrepareRenameTxRequest *, - PrepareRenameTxResponse *, + MOCK_METHOD3(PrepareRenameTx, MetaStatusCode(const PrepareRenameTxRequest*, + PrepareRenameTxResponse*, std::int64_t logIndex)); MOCK_METHOD0(GetStreamServer, std::shared_ptr()); MOCK_METHOD4(GetOrModifyS3ChunkInfo, - MetaStatusCode(const GetOrModifyS3ChunkInfoRequest *request, - GetOrModifyS3ChunkInfoResponse *response, - std::shared_ptr *iterator, + MetaStatusCode(const GetOrModifyS3ChunkInfoRequest* request, + GetOrModifyS3ChunkInfoResponse* response, + std::shared_ptr* iterator, std::int64_t logIndex)); MOCK_METHOD2(SendS3ChunkInfoByStream, MetaStatusCode(std::shared_ptr connection, std::shared_ptr iterator)); - MOCK_METHOD3(GetVolumeExtent, MetaStatusCode(const GetVolumeExtentRequest *, - GetVolumeExtentResponse *, + MOCK_METHOD3(GetVolumeExtent, MetaStatusCode(const GetVolumeExtentRequest*, + GetVolumeExtentResponse*, std::int64_t logIndex)); MOCK_METHOD3(UpdateVolumeExtent, - MetaStatusCode(const UpdateVolumeExtentRequest *, - UpdateVolumeExtentResponse *, + MetaStatusCode(const UpdateVolumeExtentRequest*, + UpdateVolumeExtentResponse*, std::int64_t logIndex)); MOCK_METHOD3(UpdateDeallocatableBlockGroup, - MetaStatusCode(const UpdateDeallocatableBlockGroupRequest *, - UpdateDeallocatableBlockGroupResponse *, + MetaStatusCode(const UpdateDeallocatableBlockGroupRequest*, + UpdateDeallocatableBlockGroupResponse*, std::int64_t logIndex)); }; diff --git a/curvefs/test/metaserver/mock/mock_partition.h b/curvefs/test/metaserver/mock/mock_partition.h index 9dd48c0f12..c0fb2f9102 100644 --- a/curvefs/test/metaserver/mock/mock_partition.h +++ b/curvefs/test/metaserver/mock/mock_partition.h @@ -34,7 +34,7 @@ class MockPartition : public curvefs::metaserver::Partition { public: MockPartition() : Partition() {} MOCK_METHOD1(GetAllBlockGroup, - MetaStatusCode(std::vector *)); + MetaStatusCode(std::vector*)); MOCK_CONST_METHOD0(GetPartitionId, uint32_t()); MOCK_CONST_METHOD0(GetFsId, uint32_t()); }; diff --git a/curvefs/test/metaserver/partition_test.cpp b/curvefs/test/metaserver/partition_test.cpp index e5cfd8d0e3..9b02a2527c 100644 --- a/curvefs/test/metaserver/partition_test.cpp +++ b/curvefs/test/metaserver/partition_test.cpp @@ -81,7 +81,7 @@ class PartitionTest : public ::testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -115,6 +115,8 @@ TEST_F(PartitionTest, testInodeIdGen1) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + ASSERT_TRUE(partition1.IsDeletable()); for (int i = 0; i < 100; i++) { ASSERT_EQ(partition1.GetNewInodeId(), partitionInfo1.start() + i); @@ -135,6 +137,8 @@ TEST_F(PartitionTest, testInodeIdGen2) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + ASSERT_TRUE(partition1.IsDeletable()); for (int i = 0; i < 50; i++) { ASSERT_EQ(partition1.GetNewInodeId(), partitionInfo1.nextid() + i); @@ -155,6 +159,8 @@ TEST_F(PartitionTest, testInodeIdGen3) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + ASSERT_EQ(partition1.GetNewInodeId(), UINT64_MAX); ASSERT_EQ(partition1.GetNewInodeId(), UINT64_MAX); } @@ -163,7 +169,7 @@ TEST_F(PartitionTest, testInodeIdGen4_NextId) { std::vector> testsets = { {0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 4}}; - for (auto &t : testsets) { + for (auto& t : testsets) { PartitionInfo partitionInfo1; partitionInfo1.set_fsid(1); partitionInfo1.set_poolid(2); @@ -173,6 +179,9 @@ TEST_F(PartitionTest, testInodeIdGen4_NextId) { partitionInfo1.set_end(199); Partition p(partitionInfo1, kvStorage_); + + ASSERT_TRUE(p.Init()); + EXPECT_EQ(t.second, p.GetNewInodeId()); } } @@ -190,6 +199,8 @@ TEST_F(PartitionTest, testInodeIdGen5_paritionstatus) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + ASSERT_EQ(partition1.GetNewInodeId(), 198); ASSERT_EQ(partition1.GetPartitionInfo().status(), PartitionStatus::READWRITE); @@ -212,6 +223,8 @@ TEST_F(PartitionTest, test1) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + ASSERT_TRUE(partition1.IsDeletable()); ASSERT_TRUE(partition1.IsInodeBelongs(1, 100)); ASSERT_TRUE(partition1.IsInodeBelongs(1, 199)); @@ -234,6 +247,8 @@ TEST_F(PartitionTest, inodenum) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + ASSERT_EQ(partition1.GetInodeNum(), 0); Inode inode; @@ -255,6 +270,9 @@ TEST_F(PartitionTest, dentrynum) { partitionInfo1.set_end(199); Partition partition1(partitionInfo1, kvStorage_); + + ASSERT_TRUE(partition1.Init()); + ASSERT_EQ(partition1.GetDentryNum(), 0); @@ -289,6 +307,8 @@ TEST_F(PartitionTest, PARTITION_ID_MISSMATCH_ERROR) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + Dentry dentry1; dentry1.set_fsid(2); dentry1.set_parentinodeid(100); @@ -396,6 +416,8 @@ TEST_F(PartitionTest, testGetInodeAttr) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + // create parent inode Inode inode; inode.set_inodeid(100); @@ -425,6 +447,8 @@ TEST_F(PartitionTest, testGetXAttr) { Partition partition1(partitionInfo1, kvStorage_); + ASSERT_TRUE(partition1.Init()); + // create parent inode Inode inode; inode.set_inodeid(100); diff --git a/curvefs/test/metaserver/recycle_cleaner_test.cpp b/curvefs/test/metaserver/recycle_cleaner_test.cpp index 0994db6eda..48a1a3e36e 100644 --- a/curvefs/test/metaserver/recycle_cleaner_test.cpp +++ b/curvefs/test/metaserver/recycle_cleaner_test.cpp @@ -117,7 +117,7 @@ class RecycleCleanerTest : public testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const string &cmd) { + std::string execShell(const string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), diff --git a/curvefs/test/metaserver/recycle_manager_test.cpp b/curvefs/test/metaserver/recycle_manager_test.cpp index c30ebfd6be..551306d95d 100644 --- a/curvefs/test/metaserver/recycle_manager_test.cpp +++ b/curvefs/test/metaserver/recycle_manager_test.cpp @@ -75,7 +75,7 @@ class RecycleManangeTest : public testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const string &cmd) { + std::string execShell(const string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -107,7 +107,7 @@ class RecycleManangeTest : public testing::Test { }; TEST_F(RecycleManangeTest, test_empty_recycle) { - RecycleManager *manager = &RecycleManager::GetInstance(); + RecycleManager* manager = &RecycleManager::GetInstance(); RecycleManagerOption opt; opt.mdsClient = mdsclient_; opt.metaClient = std::make_shared(); diff --git a/curvefs/test/metaserver/s3compact/s3compact_test.cpp b/curvefs/test/metaserver/s3compact/s3compact_test.cpp index f6ac7ae8ee..8098cd31d7 100644 --- a/curvefs/test/metaserver/s3compact/s3compact_test.cpp +++ b/curvefs/test/metaserver/s3compact/s3compact_test.cpp @@ -145,7 +145,7 @@ TEST_F(S3CompactTest, test_CopysetNodeWrapper) { } TEST_F(S3CompactTest, test_S3InfoCache) { - auto mock_requests3info = [](uint64_t fsid, S3Info *info) { + auto mock_requests3info = [](uint64_t fsid, S3Info* info) { if (fsid == 0) return S3InfoCache::RequestStatusCode::NOS3INFO; if (fsid == 1) @@ -153,7 +153,7 @@ TEST_F(S3CompactTest, test_S3InfoCache) { return S3InfoCache::RequestStatusCode::SUCCESS; }; EXPECT_CALL(*s3infoCache_, GetS3Info(_, _)) - .WillRepeatedly(testing::Invoke([&](uint64_t fsid, S3Info *info) { + .WillRepeatedly(testing::Invoke([&](uint64_t fsid, S3Info* info) { return s3infoCache_->S3InfoCache::GetS3Info(fsid, info); })); EXPECT_CALL(*s3infoCache_, InvalidateS3Info(_)) @@ -209,7 +209,7 @@ TEST_F(S3CompactTest, test_S3AdapterManager) { // test get&release testS3adapterManager_->Init(); - std::pair p = testS3adapterManager_->GetS3Adapter(); + std::pair p = testS3adapterManager_->GetS3Adapter(); ASSERT_EQ(p.first, 0); ASSERT_NE(p.second, nullptr); p = testS3adapterManager_->GetS3Adapter(); @@ -501,7 +501,7 @@ TEST_F(S3CompactTest, test_ReadFullChunk) { }; EXPECT_CALL(*s3adapter_, DeleteObject(_)).WillRepeatedly(Return(0)); - auto mock_getobj = [&](const Aws::String &key, std::string *data) { + auto mock_getobj = [&](const Aws::String& key, std::string* data) { data->clear(); data->append(ctx.blockSize, '\0'); return 0; @@ -558,7 +558,7 @@ TEST_F(S3CompactTest, test_CompactChunks) { uint64_t chunkSize = 64; Inode tmp; auto mock_updateinode = - [&](CopysetNode *copysetNode, const PartitionInfo &pinfo, + [&](CopysetNode* copysetNode, const PartitionInfo& pinfo, uint64_t inode, ::google::protobuf::Map s3ChunkInfoAdd, ::google::protobuf::Map @@ -570,7 +570,7 @@ TEST_F(S3CompactTest, test_CompactChunks) { .WillRepeatedly(testing::Invoke(mock_updateinode)); EXPECT_CALL(*s3adapter_, PutObject(_, _)).WillRepeatedly(Return(0)); EXPECT_CALL(*s3adapter_, DeleteObject(_)).WillRepeatedly(Return(0)); - auto mock_getobj = [&](const Aws::String &key, std::string *data) { + auto mock_getobj = [&](const Aws::String& key, std::string* data) { data->clear(); data->append(blockSize, '\0'); return 0; @@ -578,7 +578,7 @@ TEST_F(S3CompactTest, test_CompactChunks) { EXPECT_CALL(*s3adapter_, GetObject(_, _)) .WillRepeatedly(testing::Invoke(mock_getobj)); - auto *mockCopysetNodeWrapper = mockCopysetNodeWrapper_.get(); + auto* mockCopysetNodeWrapper = mockCopysetNodeWrapper_.get(); EXPECT_CALL(*mockCopysetNodeWrapper, IsLeaderTerm()) .WillRepeatedly(Return(true)); @@ -593,7 +593,7 @@ TEST_F(S3CompactTest, test_CompactChunks) { EXPECT_CALL(*s3adapterManager_, ReleaseS3Adapter(_)) .WillRepeatedly(Return()); - auto mock_gets3info_success = [&](uint64_t fsid, S3Info *s3info) { + auto mock_gets3info_success = [&](uint64_t fsid, S3Info* s3info) { s3info->set_ak("1"); s3info->set_sk("2"); s3info->set_endpoint("3"); @@ -671,9 +671,9 @@ TEST_F(S3CompactTest, test_CompactChunks) { ASSERT_EQ(inodeStorage_->Update(inode1, logIndex_++), MetaStatusCode::OK); mockImpl_->CompactChunks(t); ASSERT_EQ(tmp.s3chunkinfomap().size(), 1); - const auto &l = tmp.s3chunkinfomap().at(0); + const auto& l = tmp.s3chunkinfomap().at(0); ASSERT_EQ(l.s3chunks_size(), 1); - const auto &s3chunkinfo = l.s3chunks(0); + const auto& s3chunkinfo = l.s3chunks(0); ASSERT_EQ(s3chunkinfo.chunkid(), 21); ASSERT_EQ(s3chunkinfo.compaction(), 1); ASSERT_EQ(s3chunkinfo.offset(), 0); @@ -687,7 +687,7 @@ TEST_F(S3CompactTest, test_CompactChunks) { EXPECT_CALL(*mockImpl_, UpdateInode_rvr(_, _, _, _, _)) .WillRepeatedly(Return(MetaStatusCode::UNKNOWN_ERROR)); mockImpl_->CompactChunks(t); - auto mock_gets3info_fail = [&](uint64_t fsid, S3Info *s3info) { + auto mock_gets3info_fail = [&](uint64_t fsid, S3Info* s3info) { return -1; }; EXPECT_CALL(*s3infoCache_, GetS3Info(_, _)) diff --git a/curvefs/test/metaserver/transaction_test.cpp b/curvefs/test/metaserver/transaction_test.cpp index b0bae89b68..fc8e54a6aa 100644 --- a/curvefs/test/metaserver/transaction_test.cpp +++ b/curvefs/test/metaserver/transaction_test.cpp @@ -63,6 +63,8 @@ class TransactionTest : public ::testing::Test { txManager_ = std::make_shared(dentryStorage_, partitionInfo); dentryManager_ = std::make_shared(dentryStorage_, txManager_); + ASSERT_TRUE(dentryManager_->Init()); + ASSERT_TRUE(txManager_->Init()); logIndex_ = 0; } @@ -72,7 +74,7 @@ class TransactionTest : public ::testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"), @@ -86,8 +88,12 @@ class TransactionTest : public ::testing::Test { return result; } - Dentry GenDentry(uint32_t fsId, uint64_t parentId, const std::string &name, - uint64_t txId, uint64_t inodeId, uint32_t flag) { + Dentry GenDentry(uint32_t fsId, + uint64_t parentId, + const std::string& name, + uint64_t txId, + uint64_t inodeId, + uint32_t flag) { Dentry dentry; dentry.set_fsid(fsId); dentry.set_parentinodeid(parentId); @@ -99,7 +105,7 @@ class TransactionTest : public ::testing::Test { } void InsertDentrys(std::shared_ptr storage, - const std::vector &&dentrys) { + const std::vector&& dentrys) { // NOTE: store real transaction is unnecessary metaserver::TransactionRequest request; request.set_type(0); @@ -109,8 +115,8 @@ class TransactionTest : public ::testing::Test { ASSERT_EQ(storage->Size(), dentrys.size()); } - void ASSERT_DENTRYS_EQ(const std::vector &lhs, - const std::vector &&rhs) { + void ASSERT_DENTRYS_EQ(const std::vector& lhs, + const std::vector&& rhs) { ASSERT_EQ(lhs, rhs); } diff --git a/curvefs/test/metaserver/trash_test.cpp b/curvefs/test/metaserver/trash_test.cpp index 02ce93d257..6ee8617e26 100644 --- a/curvefs/test/metaserver/trash_test.cpp +++ b/curvefs/test/metaserver/trash_test.cpp @@ -78,7 +78,7 @@ class TestTrash : public ::testing::Test { ASSERT_EQ(output.size(), 0); } - std::string execShell(const std::string &cmd) { + std::string execShell(const std::string& cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd.c_str(), "r"),