diff --git a/.bazelrc b/.bazelrc index 74efc749e3..bd9e2fd766 100644 --- a/.bazelrc +++ b/.bazelrc @@ -3,7 +3,7 @@ build --verbose_failures build --define=with_glog=true --define=libunwind=true build --copt -DHAVE_ZLIB=1 --copt -DGFLAGS_NS=google --copt -DUSE_BTHREAD_MUTEX build --cxxopt -Wno-error=format-security -build:gcc7-later --cxxopt -faligned-new --cxxopt -Wno-error=implicit-fallthrough +build:gcc7-later --cxxopt -faligned-new build --incompatible_blacklisted_protos_requires_proto_info=false build --copt=-fdiagnostics-color=always diff --git a/curvefs/proto/metaserver.proto b/curvefs/proto/metaserver.proto index 88ef34e5b2..a79f40d752 100644 --- a/curvefs/proto/metaserver.proto +++ b/curvefs/proto/metaserver.proto @@ -145,7 +145,11 @@ message PrepareRenameTxRequest { } message TransactionRequest { - required uint32 type = 1; + enum TransactionType { + None = 0; + Rename = 1; + } + required TransactionType type = 1; required string rawPayload = 2; } diff --git a/curvefs/src/metaserver/copyset/copyset_node.cpp b/curvefs/src/metaserver/copyset/copyset_node.cpp index 45245195b5..42c84a0f3e 100644 --- a/curvefs/src/metaserver/copyset/copyset_node.cpp +++ b/curvefs/src/metaserver/copyset/copyset_node.cpp @@ -92,7 +92,8 @@ CopysetNode::CopysetNode(PoolId poolId, CopysetId copysetId, confChangeMtx_(), ongoingConfChange_(), metric_(absl::make_unique(poolId_, copysetId_)), - isLoading_(false) {} + isLoading_(false), + snapshotTask_() {} CopysetNode::~CopysetNode() { Stop(); @@ -188,6 +189,8 @@ void CopysetNode::Stop() { LOG_IF(ERROR, metaStore_->Destroy() != true) << "Failed to clear metastore, copyset: " << name_; } + // wait for snapshot + WaitSnapshotDone(); } int CopysetNode::LoadConfEpoch(const std::string& file) { @@ -362,6 +365,41 @@ class OnSnapshotSaveDoneClosureImpl : public OnSnapshotSaveDoneClosure { } // namespace +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_ = std::async(std::launch::async,[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(); + }); + } + doneGuard.release(); +} + void CopysetNode::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done) { LOG(INFO) << "Copyset " << name_ << " saving snapshot to '" @@ -390,12 +428,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(); @@ -403,6 +436,13 @@ void CopysetNode::on_snapshot_save(braft::SnapshotWriter* writer, std::move(cleanMetricIfFailed).Cancel(); } +void CopysetNode::WaitSnapshotDone() { + std::lock_guard lock(snapshotLock_); + if (snapshotTask_.valid()) { + snapshotTask_.wait(); + } +} + namespace { class CopysetLoadingGuard { @@ -463,7 +503,7 @@ int CopysetNode::on_snapshot_load(braft::SnapshotReader* reader) { void CopysetNode::on_leader_start(int64_t term) { /* - * Invoke order in on_leader_start: + * Invoke order in on_leader_start: * 1. flush concurrent apply queue. * 2. set term in states machine. * diff --git a/curvefs/src/metaserver/copyset/copyset_node.h b/curvefs/src/metaserver/copyset/copyset_node.h index ef6c09b655..f1ab756fe8 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,13 @@ class CopysetNode : public braft::StateMachine { FRIEND_TEST(CopysetNodeBlockGroupTest, Test_AggregateBlockStatInfo); + private: + // for snapshot + + void WaitSnapshotDone(); + + void DoSnapshot(OnSnapshotSaveDoneClosure* done); + private: const PoolId poolId_; const CopysetId copysetId_; @@ -267,6 +275,19 @@ 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_; + + std::future snapshotTask_; }; 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..552c4a1a09 100644 --- a/curvefs/src/metaserver/dentry_manager.cpp +++ b/curvefs/src/metaserver/dentry_manager.cpp @@ -26,6 +26,17 @@ #include "curvefs/src/metaserver/dentry_manager.h" +#define CHECK_APPLIED() \ + do { \ + if (logIndex <= appliedIndex_) { \ + VLOG(3) << __func__ \ + << "Log entry already be applied, index = " \ + << logIndex \ + << "applied index = " << appliedIndex_; \ + return MetaStatusCode::IDEMPOTENCE_OK; \ + } \ + } while (false) + namespace curvefs { namespace metaserver { @@ -33,20 +44,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 == MetaStatusCode::OK || s == MetaStatusCode::NOT_FOUND; + } + 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,26 +76,18 @@ void DentryManager::Log4Code(const std::string &request, MetaStatusCode rc) { } } -MetaStatusCode DentryManager::CreateDentry(const Dentry &dentry, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } +MetaStatusCode DentryManager::CreateDentry(const Dentry& dentry, + int64_t logIndex) { + CHECK_APPLIED(); Log4Dentry("CreateDentry", dentry); MetaStatusCode rc = dentryStorage_->Insert(dentry, logIndex); Log4Code("CreateDentry", rc); return rc; } -MetaStatusCode DentryManager::CreateDentry(const DentryVec &vec, bool merge, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } +MetaStatusCode DentryManager::CreateDentry(const DentryVec& vec, bool merge, + int64_t logIndex) { + CHECK_APPLIED(); VLOG(9) << "Receive CreateDentryVec request, dentryVec = (" << vec.ShortDebugString() << ")"; MetaStatusCode rc = dentryStorage_->Insert(vec, merge, logIndex); @@ -85,28 +95,24 @@ MetaStatusCode DentryManager::CreateDentry(const DentryVec &vec, bool merge, return rc; } -MetaStatusCode DentryManager::DeleteDentry(const Dentry &dentry, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } +MetaStatusCode DentryManager::DeleteDentry(const Dentry& dentry, + int64_t logIndex) { + CHECK_APPLIED(); Log4Dentry("DeleteDentry", dentry); MetaStatusCode rc = dentryStorage_->Delete(dentry, logIndex); Log4Code("DeleteDentry", rc); 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 +125,9 @@ void DentryManager::ClearDentry() { LOG(INFO) << "ClearDentry ok"; } -MetaStatusCode DentryManager::HandleRenameTx(const std::vector &dentrys, - std::int64_t logIndex) { - for (const auto &dentry : dentrys) { +MetaStatusCode DentryManager::HandleRenameTx(const std::vector& dentrys, + int64_t logIndex) { + 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..ef5ca8305c 100644 --- a/curvefs/src/metaserver/dentry_manager.h +++ b/curvefs/src/metaserver/dentry_manager.h @@ -40,33 +40,35 @@ 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, int64_t logIndex); // only invoked from snapshot loadding - MetaStatusCode CreateDentry(const DentryVec &vec, bool merge, - std::int64_t logIndex); + MetaStatusCode CreateDentry(const DentryVec& vec, bool merge, + int64_t logIndex); - MetaStatusCode DeleteDentry(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode DeleteDentry(const Dentry& dentry, 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, - std::int64_t logIndex); + MetaStatusCode HandleRenameTx(const std::vector& dentrys, + 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_; std::shared_ptr txManager_; - std::int64_t appliedIndex_; + int64_t appliedIndex_; }; } // namespace metaserver diff --git a/curvefs/src/metaserver/dentry_storage.cpp b/curvefs/src/metaserver/dentry_storage.cpp index 501ad32e52..aa4d83d023 100644 --- a/curvefs/src/metaserver/dentry_storage.cpp +++ b/curvefs/src/metaserver/dentry_storage.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -43,30 +44,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 +78,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 +88,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 +114,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 +154,27 @@ 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_; +} + +const char* DentryStorage::kDentryAppliedKey("dentry"); +const char* DentryStorage::kDentryCountKey("count"); +const char* DentryStorage::kHandleTxKey("handleTx"); +const char* DentryStorage::kPendingTxKey("pendingTx"); -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 +188,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, + uint64_t* outCount) { DentryVector vector(vec); std::vector deleted; if (dentrys->size() == 2) { @@ -182,71 +206,41 @@ 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); } + const char* step = "Compress dentry from transaction"; Status s; std::string skey = DentryKey(*dentrys->begin()); - if (vec->dentrys_size() == 0) { // delete directly - s = txn->SDel(table4Dentry_, skey); - } else { - s = txn->SSet(table4Dentry_, skey, *vec); - } - if (!s.ok()) { - LOG(ERROR) << "Compress dentry from transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + do { + if (vec->dentrys_size() == 0) { // delete directly + s = txn->SDel(table4Dentry_, skey); + } else { + s = txn->SSet(table4Dentry_, skey, *vec); } - return false; - } - std::uint64_t countCopy = *outCount; - vector.Confirm(&countCopy); - s = SetDentryCount(txn, countCopy); - if (!s.ok()) { - LOG(ERROR) << "Insert dentry count to transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + if (!s.ok()) { + break; } - return false; - } - *outCount = countCopy; - return true; -} - -bool DentryStorage::CompressDentry(DentryVec *vec, BTree *dentrys) { - DentryVector vector(vec); - std::vector deleted; - if (dentrys->size() == 2) { - deleted.push_back(*dentrys->begin()); - } - if (HasDeleteMarkFlag(*dentrys->rbegin())) { - deleted.push_back(*dentrys->rbegin()); - } - for (const auto &dentry : deleted) { - vector.Delete(dentry); - } - - Status s; - std::string skey = DentryKey(*dentrys->begin()); - if (vec->dentrys_size() == 0) { // delete directly - s = kvStorage_->SDel(table4Dentry_, skey); - } else { - s = kvStorage_->SSet(table4Dentry_, skey, *vec); - } - - if (s.ok()) { - vector.Confirm(&nDentry_); + uint64_t countCopy = *outCount; + vector.Confirm(&countCopy); + s = SetDentryCount(txn, countCopy); + if (!s.ok()) { + step = "Insert dentry count to transaction"; + break; + } + *outCount = countCopy; return true; - } + } while (false); + LOG(ERROR) << step + << " failed, status = " + << s.ToString(); return false; } // 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 +276,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, + uint64_t* compressOutCount) { std::string skey = DentryKey(in); Status s = txn->SGet(table4Dentry_, skey, vec); if (s.IsNotFound()) { @@ -321,346 +315,308 @@ MetaStatusCode DentryStorage::Find(storage::StorageTransaction *txn, return rc; } -bool DentryStorage::GetAppliedIndex(std::int64_t *index) { +MetaStatusCode DentryStorage::GetAppliedIndex(int64_t* index) { common::AppliedIndex val; - auto s = kvStorage_->SGet(table4AppliedIndex_, "dentry", &val); + auto s = kvStorage_->SGet(table4AppliedIndex_, kDentryAppliedKey, &val); if (s.ok()) { *index = val.index(); - return true; + return MetaStatusCode::OK; } - return false; + if (s.IsNotFound()) { + return MetaStatusCode::NOT_FOUND; + } + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } storage::Status -DentryStorage::SetAppliedIndex(storage::StorageTransaction *transaction, - std::int64_t index) { +DentryStorage::SetAppliedIndex(storage::StorageTransaction* transaction, + int64_t index) { common::AppliedIndex val; val.set_index(index); - return transaction->SSet(table4AppliedIndex_, "dentry", val); + return transaction->SSet(table4AppliedIndex_, kDentryAppliedKey, val); } storage::Status -DentryStorage::DelAppliedIndex(storage::StorageTransaction *transaction) { - return transaction->SDel(table4AppliedIndex_, "dentry"); +DentryStorage::DelAppliedIndex(storage::StorageTransaction* transaction) { + return transaction->SDel(table4AppliedIndex_, kDentryAppliedKey); } storage::Status -DentryStorage::SetHandleTxIndex(storage::StorageTransaction *transaction, - std::int64_t index) { +DentryStorage::SetHandleTxIndex(storage::StorageTransaction* transaction, + int64_t index) { common::AppliedIndex val; val.set_index(index); - return transaction->SSet(table4AppliedIndex_, "handleTx", val); + return transaction->SSet(table4AppliedIndex_, kHandleTxKey, val); } storage::Status -DentryStorage::DelHandleTxIndex(storage::StorageTransaction *transaction) { - return transaction->SDel(table4AppliedIndex_, "handleTx"); +DentryStorage::DelHandleTxIndex(storage::StorageTransaction* transaction) { + return transaction->SDel(table4AppliedIndex_, kHandleTxKey); } -bool DentryStorage::GetPendingTx(metaserver::TransactionRequest *request) { - auto s = kvStorage_->SGet(table4Transaction_, "transaction", request); - return s.ok(); +MetaStatusCode +DentryStorage::GetPendingTx(metaserver::TransactionRequest* request) { + auto s = kvStorage_->SGet(table4Transaction_, kPendingTxKey, request); + if (s.ok()) { + return MetaStatusCode::OK; + } + if (s.IsNotFound()) { + return MetaStatusCode::NOT_FOUND; + } + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } storage::Status -DentryStorage::SetPendingTx(storage::StorageTransaction *txn, - const metaserver::TransactionRequest &request) { - return txn->SSet(table4Transaction_, "transaction", request); +DentryStorage::SetPendingTx(storage::StorageTransaction* txn, + const metaserver::TransactionRequest& request) { + return txn->SSet(table4Transaction_, kPendingTxKey, request); } -storage::Status DentryStorage::DelPendingTx(storage::StorageTransaction *txn) { - return txn->SDel(table4Transaction_, "transaction"); +storage::Status DentryStorage::DelPendingTx(storage::StorageTransaction* txn) { + return txn->SDel(table4Transaction_, kPendingTxKey); } storage::Status -DentryStorage::ClearPendingTx(storage::StorageTransaction *txn) { +DentryStorage::ClearPendingTx(storage::StorageTransaction* txn) { metaserver::TransactionRequest request; - request.set_type(static_cast(TxType::None)); + request.set_type(metaserver::TransactionRequest::None); request.set_rawpayload(""); return txn->SSet(table4Transaction_, "transaction", request); } -storage::Status DentryStorage::SetDentryCount(storage::StorageTransaction *txn, - std::uint64_t count) { +storage::Status DentryStorage::SetDentryCount(storage::StorageTransaction* txn, + uint64_t count) { common::ItemCount val; val.set_count(count); - return txn->SSet(table4DentryCount_, "count", val); + return txn->SSet(table4DentryCount_, kDentryCountKey, val); } storage::Status -DentryStorage::DelDentryCount(storage::StorageTransaction *txn) { - return txn->SDel(table4DentryCount_, "count"); +DentryStorage::DelDentryCount(storage::StorageTransaction* txn) { + return txn->SDel(table4DentryCount_, kDentryCountKey); } -bool DentryStorage::GetDentryCount(std::uint64_t *count) { +storage::Status DentryStorage::GetDentryCount(uint64_t* count) { common::ItemCount val; - auto s = kvStorage_->SGet(table4DentryCount_, "count", &val); + auto s = kvStorage_->SGet(table4DentryCount_, kDentryCountKey, &val); if (s.ok()) { *count = val.count(); - return true; } - return false; + return s; } -bool DentryStorage::GetHandleTxIndex(std::int64_t *index) { +storage::Status DentryStorage::GetHandleTxIndex(int64_t* index) { common::AppliedIndex val; - auto s = kvStorage_->SGet(table4AppliedIndex_, "handleTx", &val); + auto s = kvStorage_->SGet(table4AppliedIndex_, kHandleTxKey, &val); if (s.ok()) { *index = val.index(); - return true; } - return false; + return s; } -MetaStatusCode DentryStorage::Insert(const Dentry &dentry, - std::int64_t logIndex) { +MetaStatusCode DentryStorage::Insert(const Dentry& dentry, + int64_t logIndex) { WriteLockGuard lg(rwLock_); Dentry out; DentryVec vec; - std::shared_ptr txn = - kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - std::uint64_t count = nDentry_; - auto s = SetAppliedIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed, status = " - << s.ToString(); + std::shared_ptr txn; + storage::Status s; + const char* step = "Begin transaction"; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - LOG(ERROR) << "Insert applied index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - MetaStatusCode rc = Find(txn.get(), dentry, &out, &vec, &count); - if (rc == MetaStatusCode::OK) { - auto s = txn->Commit(); + uint64_t count = nDentry_; + s = SetAppliedIndex(txn.get(), logIndex); if (!s.ok()) { - LOG(ERROR) << "Commit compress dentry transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + step = "Insert applied index to transaction"; + break; + } + MetaStatusCode rc = Find(txn.get(), dentry, &out, &vec, &count); + if (rc == MetaStatusCode::OK) { + auto s = txn->Commit(); + if (!s.ok()) { + step = "Commit compress dentry transaction"; + break; } - } else { // if compress is success // we use output dentry count to replace old one nDentry_ = count; + if (BelongSomeOne(out, dentry)) { + return MetaStatusCode::IDEMPOTENCE_OK; + } + return MetaStatusCode::DENTRY_EXIST; + } else if (rc != MetaStatusCode::NOT_FOUND) { + step = "Find dentry failed"; + break; } - if (BelongSomeOne(out, dentry)) { - return MetaStatusCode::IDEMPOTENCE_OK; - } - return MetaStatusCode::DENTRY_EXIST; - } else if (rc != MetaStatusCode::NOT_FOUND) { - LOG(ERROR) << "Find dentry failed"; - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed"; - } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - // rc == MetaStatusCode::NOT_FOUND - - // NOTE: `count` maybe already written by `Find()` in here - // so we continue use `count` in follow operations - DentryVector vector(&vec); - vector.Insert(dentry); - std::string skey = DentryKey(dentry); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->SSet(table4Dentry_, skey, vec); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed"; + // rc == MetaStatusCode::NOT_FOUND + + // NOTE: `count` maybe already written by `Find()` in here + // so we continue use `count` in follow operations + DentryVector vector(&vec); + vector.Insert(dentry); + std::string skey = DentryKey(dentry); + s = txn->SSet(table4Dentry_, skey, vec); + if (!s.ok()) { + step = "Insert dentry to transaction"; + break; } - LOG(ERROR) << "Insert dentry to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - vector.Confirm(&count); - s = SetDentryCount(txn.get(), count); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed"; + vector.Confirm(&count); + s = SetDentryCount(txn.get(), count); + if (!s.ok()) { + step = "Insert dentry count to transaction"; + break; } - LOG(ERROR) << "Insert dentry count to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed, status = " - << s.ToString(); + s = txn->Commit(); + if (!s.ok()) { + step = "Insert dentry"; + break; } - LOG(ERROR) << "Insert dentry failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + nDentry_ = count; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback insert dentry transaction failed, status = " + << s.ToString(); } - nDentry_ = count; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode DentryStorage::Insert(const DentryVec &vec, bool merge, - std::int64_t logIndex) { +MetaStatusCode DentryStorage::Insert(const DentryVec& vec, bool merge, + int64_t logIndex) { WriteLockGuard lg(rwLock_); Status s; DentryVec oldVec; std::string skey = DentryKey(vec.dentrys(0)); - std::shared_ptr txn = - kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - if (merge) { // for old version dumpfile (v1) - s = txn->SGet(table4Dentry_, skey, &oldVec); - if (s.IsNotFound()) { - // do nothing - } else if (!s.ok()) { - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + std::shared_ptr txn; + const char* step = "Begin transaction"; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - } - - DentryVector vector(&oldVec); - vector.Merge(vec); - s = txn->SSet(table4Dentry_, skey, oldVec); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed, status = " - << s.ToString(); + if (merge) { // for old version dumpfile (v1) + s = txn->SGet(table4Dentry_, skey, &oldVec); + if (s.IsNotFound()) { + // do nothing + } else if (!s.ok()) { + step = "Find old version from transaction"; + break; + } } - LOG(ERROR) << "Insert dentry vector to tranasction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = SetAppliedIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed, status = " - << s.ToString(); + DentryVector vector(&oldVec); + vector.Merge(vec); + s = txn->SSet(table4Dentry_, skey, oldVec); + if (!s.ok()) { + step = "Insert dentry vector to tranasction"; + break; } - LOG(ERROR) << "Insert applied index to tranasction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - std::uint64_t count = nDentry_; - vector.Confirm(&count); - s = SetDentryCount(txn.get(), count); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed, status = " - << s.ToString(); + s = SetAppliedIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert applied index to tranasction"; + break; } - LOG(ERROR) << "Insert dentry count to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert dentry transaction failed, status = " - << s.ToString(); + uint64_t count = nDentry_; + vector.Confirm(&count); + s = SetDentryCount(txn.get(), count); + if (!s.ok()) { + step = "Insert dentry count to transaction"; + break; } - LOG(ERROR) << "Insert dentry vector failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + s = txn->Commit(); + if (!s.ok()) { + step = "Insert dentry vector"; + break; + } + nDentry_ = count; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback insert dentry transaction failed, status = " + << s.ToString(); } - nDentry_ = count; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode DentryStorage::Delete(const Dentry &dentry, - std::int64_t logIndex) { +MetaStatusCode DentryStorage::Delete(const Dentry& dentry, + int64_t logIndex) { WriteLockGuard lg(rwLock_); Dentry out; DentryVec vec; - std::shared_ptr txn = - kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - std::uint64_t count = nDentry_; - auto s = SetAppliedIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete dentry transaction failed"; + const char* step = "Begin transaction"; + std::shared_ptr txn; + storage::Status s; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - LOG(ERROR) << "Insert applied index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - MetaStatusCode rc = Find(txn.get(), dentry, &out, &vec, &count); - if (rc == MetaStatusCode::NOT_FOUND) { - // NOTE: we should commit transaction - // even if rc is NOT_FOUND - // because Find() maybe write dentry count to rocksdb - s = txn->Commit(); - if (s.ok()) { + uint64_t count = nDentry_; + s = SetAppliedIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert applied index to transaction"; + break; + } + MetaStatusCode rc = Find(txn.get(), dentry, &out, &vec, &count); + if (rc == MetaStatusCode::NOT_FOUND) { + // NOTE: we should commit transaction + // even if rc is NOT_FOUND + // because Find() maybe write dentry count to rocksdb + s = txn->Commit(); + if (!s.ok()) { + step = "Commit transaction"; + break; + } nDentry_ = count; return MetaStatusCode::NOT_FOUND; + } else if (rc != MetaStatusCode::OK) { + step = "Find dentry"; + break; } - LOG(ERROR) << "Commit transaction failed, status = " << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + DentryVector vector(&vec); + vector.Delete(out); + std::string skey = DentryKey(dentry); + if (vec.dentrys_size() == 0) { + s = txn->SDel(table4Dentry_, skey); + } else { + s = txn->SSet(table4Dentry_, skey, vec); } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } else if (rc != MetaStatusCode::OK) { - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - - DentryVector vector(&vec); - vector.Delete(out); - std::string skey = DentryKey(dentry); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - if (vec.dentrys_size() == 0) { - s = txn->SDel(table4Dentry_, skey); - } else { - s = txn->SSet(table4Dentry_, skey, vec); - } - - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete dentry transaction failed"; + if (!s.ok()) { + step = "Delete dentry vector from transaction"; + break; } - LOG(ERROR) << "Delete dentry vector from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - // NOTE: we should use count variable instead of nDentry_ - // (it means that we should not reset count to nDentry_) - // count is newest version of dentry count - vector.Confirm(&count); - s = SetDentryCount(txn.get(), count); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete dentry transaction failed"; + // NOTE: we should use count variable instead of nDentry_ + // (it means that we should not reset count to nDentry_) + // count is newest version of dentry count + vector.Confirm(&count); + s = SetDentryCount(txn.get(), count); + if (!s.ok()) { + step = "Insert applied index to transaction"; + break; } - LOG(ERROR) << "Insert applied index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete dentry transaction failed" - << s.ToString(); + s = txn->Commit(); + if (!s.ok()) { + step = "Delete dentry vector"; + break; } - LOG(ERROR) << "Delete dentry vector failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + nDentry_ = count; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction failed"; } - nDentry_ = count; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode DentryStorage::Get(Dentry *dentry) { +MetaStatusCode DentryStorage::Get(Dentry* dentry) { ReadLockGuard lg(rwLock_); Dentry out; @@ -677,8 +633,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,72 +693,70 @@ MetaStatusCode DentryStorage::List(const Dentry &dentry, } MetaStatusCode -DentryStorage::PrepareTx(const std::vector &dentrys, - const metaserver::TransactionRequest &txRequest, - std::int64_t logIndex) { +DentryStorage::PrepareTx(const std::vector& dentrys, + const metaserver::TransactionRequest& txRequest, + int64_t logIndex) { WriteLockGuard lg(rwLock_); - std::uint64_t count = nDentry_; + uint64_t count = nDentry_; Status s; - auto txn = kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - for (const auto &dentry : dentrys) { - DentryVec vec; - DentryVector vector(&vec); - std::string skey = DentryKey(dentry); - s = txn->SGet(table4Dentry_, skey, &vec); - if (!s.ok() && !s.IsNotFound()) { - LOG(ERROR) << "Get dentry from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + const char* step = "Begin transaction"; + std::shared_ptr txn; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - // OK || NOT_FOUND - vector.Insert(dentry); - s = txn->SSet(table4Dentry_, skey, vec); - if (!s.ok()) { - LOG(ERROR) << "Insert dentry to transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction fail"; + bool quit = false; + for (const auto& dentry : dentrys) { + DentryVec vec; + DentryVector vector(&vec); + std::string skey = DentryKey(dentry); + s = txn->SGet(table4Dentry_, skey, &vec); + if (!s.ok() && !s.IsNotFound()) { + step = "Get dentry from transaction"; + quit = true; + break; + } + // OK || NOT_FOUND + vector.Insert(dentry); + s = txn->SSet(table4Dentry_, skey, vec); + if (!s.ok()) { + step = "Insert dentry to transaction"; + quit = true; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + vector.Confirm(&count); } - vector.Confirm(&count); - } - s = SetAppliedIndex(txn.get(), logIndex); - if (!s.ok()) { - LOG(ERROR) << "Insert applied index to transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction fail"; + if (quit) { + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = SetPendingTx(txn.get(), txRequest); - if (!s.ok()) { - LOG(ERROR) << "Insert tx request to transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction fail"; + s = SetAppliedIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert applied index to transaction"; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - LOG(ERROR) << "Commit transaction failed, status = " << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction fail"; + s = SetPendingTx(txn.get(), txRequest); + if (!s.ok()) { + step = "Insert tx request to transaction"; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + s = txn->Commit(); + if (!s.ok()) { + step = "Commit transaction"; + break; + } + nDentry_ = count; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction fail"; } - nDentry_ = count; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode DentryStorage::CommitTx(const std::vector &dentrys, - std::int64_t logIndex) { +MetaStatusCode DentryStorage::CommitTx(const std::vector& dentrys, + int64_t logIndex) { if (logIndex <= handleTxIndex_) { // NOTE: if we enter here // means that this log entry is "half apply" @@ -817,57 +771,57 @@ MetaStatusCode DentryStorage::CommitTx(const std::vector &dentrys, } WriteLockGuard lg(rwLock_); Status s; - auto txn = kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - std::uint64_t count = nDentry_; - for (const auto &dentry : dentrys) { - Dentry out; - DentryVec vec; - std::string skey = DentryKey(dentry); - MetaStatusCode rc = MetaStatusCode::OK; - rc = Find(txn.get(), dentry, &out, &vec, &count); - if (rc != MetaStatusCode::OK && rc != MetaStatusCode::NOT_FOUND) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + const char* step = "Begin transaction"; + std::shared_ptr txn; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; + } + uint64_t count = nDentry_; + bool quit = false; + for (const auto& dentry : dentrys) { + Dentry out; + DentryVec vec; + std::string skey = DentryKey(dentry); + MetaStatusCode rc = MetaStatusCode::OK; + rc = Find(txn.get(), dentry, &out, &vec, &count); + if (rc != MetaStatusCode::OK && rc != MetaStatusCode::NOT_FOUND) { + step = "Find dentry from transaction"; + quit = true; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - } - s = SetHandleTxIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + if (quit) { + break; } - LOG(ERROR) << "Insert handle tx index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = ClearPendingTx(txn.get()); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = SetHandleTxIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert handle tx index to transaction"; + break; } - LOG(ERROR) << "Delete pending tx from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = ClearPendingTx(txn.get()); + if (!s.ok()) { + step = "Delete pending tx from transaction"; + break; } - LOG(ERROR) << "Commit transaction failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + s = txn->Commit(); + if (!s.ok()) { + step = "Commit transaction"; + break; + } + nDentry_ = count; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction failed"; } - nDentry_ = count; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode DentryStorage::RollbackTx(const std::vector &dentrys, - std::int64_t logIndex) { +MetaStatusCode DentryStorage::RollbackTx(const std::vector& dentrys, + int64_t logIndex) { if (logIndex <= handleTxIndex_) { // NOTE: if we enter here // means that this log entry is "half apply" @@ -882,73 +836,70 @@ MetaStatusCode DentryStorage::RollbackTx(const std::vector &dentrys, } WriteLockGuard lg(rwLock_); Status s; - auto txn = kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - std::uint64_t count = nDentry_; - for (const auto &dentry : dentrys) { - DentryVec vec; - DentryVector vector(&vec); - std::string skey = DentryKey(dentry); - s = txn->SGet(table4Dentry_, skey, &vec); - if (!s.ok() && !s.IsNotFound()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + const char* step = "Begin transaction"; + std::shared_ptr txn; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; + } + uint64_t count = nDentry_; + bool quit = false; + for (const auto& dentry : dentrys) { + DentryVec vec; + DentryVector vector(&vec); + std::string skey = DentryKey(dentry); + s = txn->SGet(table4Dentry_, skey, &vec); + if (!s.ok() && !s.IsNotFound()) { + step = "Find dentry"; + quit = true; + break; + } + // OK || NOT_FOUND + vector.Delete(dentry); + if (vec.dentrys_size() == 0) { // delete directly + s = txn->SDel(table4Dentry_, skey); + } else { + s = txn->SSet(table4Dentry_, skey, vec); + } + if (!s.ok()) { + step = "Delete dentry from transaction"; + quit = true; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + vector.Confirm(&count); } - // OK || NOT_FOUND - vector.Delete(dentry); - if (vec.dentrys_size() == 0) { // delete directly - s = txn->SDel(table4Dentry_, skey); - } else { - s = txn->SSet(table4Dentry_, skey, vec); + if (quit) { + break; } + s = SetDentryCount(txn.get(), count); if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; - } - LOG(ERROR) << "Delete dentry from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + step = "Insert dentry count to transaction"; + break; } - vector.Confirm(&count); - } - s = SetDentryCount(txn.get(), count); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = SetHandleTxIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert handle tx index to transaction"; + break; } - LOG(ERROR) << "Insert dentry count to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = SetHandleTxIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = ClearPendingTx(txn.get()); + if (!s.ok()) { + step = "Delete pending tx from transaction"; + break; } - LOG(ERROR) << "Insert handle tx index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = ClearPendingTx(txn.get()); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = txn->Commit(); + if (!s.ok()) { + step = "Commit transaction"; + break; } - LOG(ERROR) << "Delete pending tx from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + nDentry_ = count; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction failed"; } - nDentry_ = count; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } std::shared_ptr DentryStorage::GetAll() { @@ -992,55 +943,48 @@ MetaStatusCode DentryStorage::Clear() { LOG(ERROR) << "Clear dentry table failed, status = " << s.ToString(); return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - auto txn = kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = DelDentryCount(txn.get()); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + std::shared_ptr txn; + const char* step = "Begin transaction"; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - LOG(ERROR) << "Delete dentry count failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = DelPendingTx(txn.get()); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = DelDentryCount(txn.get()); + if (!s.ok()) { + step = "Delete dentry count"; + break; } - LOG(ERROR) << "Delete pending tx failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = DelAppliedIndex(txn.get()); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = DelPendingTx(txn.get()); + if (!s.ok()) { + step = "Delete pending tx"; + break; } - LOG(ERROR) << "Delete applied index failed, status = " << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = DelHandleTxIndex(txn.get()); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = DelAppliedIndex(txn.get()); + if (!s.ok()) { + step = "Delete applied index"; + break; } - LOG(ERROR) << "Delete handle tx index failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = DelHandleTxIndex(txn.get()); + if (!s.ok()) { + step = "Delete handle tx index"; + break; } - LOG(ERROR) << "Commit clear dentry table transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + s = txn->Commit(); + if (!s.ok()) { + step = "Commit clear dentry table transaction"; + break; + } + nDentry_ = 0; + return MetaStatusCode::OK; + } while (false); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction failed"; } - nDentry_ = 0; - return MetaStatusCode::OK; + LOG(ERROR) << step + << " failed, status = " + << s.ToString(); + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } } // namespace metaserver diff --git a/curvefs/src/metaserver/dentry_storage.h b/curvefs/src/metaserver/dentry_storage.h index 80052e737c..1392ed71ed 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,34 +98,34 @@ 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, int64_t logIndex); // only for loadding from snapshot - MetaStatusCode Insert(const DentryVec &vec, bool merge, - std::int64_t logIndex); + MetaStatusCode Insert(const DentryVec& vec, bool merge, + int64_t logIndex); - MetaStatusCode Delete(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode Delete(const Dentry& dentry, 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, - std::int64_t logIndex); + MetaStatusCode PrepareTx(const std::vector& dentrys, + const metaserver::TransactionRequest& txRequest, + int64_t logIndex); - MetaStatusCode CommitTx(const std::vector &dentrys, - std::int64_t logIndex); + MetaStatusCode CommitTx(const std::vector& dentrys, + int64_t logIndex); - MetaStatusCode RollbackTx(const std::vector &dentrys, - std::int64_t logIndex); + MetaStatusCode RollbackTx(const std::vector& dentrys, + int64_t logIndex); std::shared_ptr GetAll(); @@ -135,47 +135,47 @@ class DentryStorage { MetaStatusCode Clear(); - bool GetPendingTx(metaserver::TransactionRequest *request); + MetaStatusCode GetPendingTx(metaserver::TransactionRequest* request); - private: - std::string DentryKey(const Dentry &entry); + MetaStatusCode GetAppliedIndex(int64_t* index); - bool CompressDentry(DentryVec *vec, BTree *dentrys); + private: + std::string DentryKey(const Dentry& entry); - bool CompressDentry(storage::StorageTransaction *txn, DentryVec *vec, - BTree *dentrys, std::uint64_t *outCount); + bool CompressDentry(storage::StorageTransaction* txn, DentryVec* vec, + BTree* dentrys, 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, + uint64_t* compressOutCount); - storage::Status SetAppliedIndex(storage::StorageTransaction *transaction, - std::int64_t index); + storage::Status SetAppliedIndex(storage::StorageTransaction* transaction, + int64_t index); - storage::Status DelAppliedIndex(storage::StorageTransaction *transaction); + storage::Status DelAppliedIndex(storage::StorageTransaction* transaction); - storage::Status SetHandleTxIndex(storage::StorageTransaction *transaction, - std::int64_t index); + storage::Status SetHandleTxIndex(storage::StorageTransaction* transaction, + 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, - std::uint64_t count); + storage::Status SetDentryCount(storage::StorageTransaction* transaction, + uint64_t count); - storage::Status DelDentryCount(storage::StorageTransaction *transaction); + storage::Status DelDentryCount(storage::StorageTransaction* transaction); - bool GetDentryCount(std::uint64_t *count); + storage::Status GetDentryCount(uint64_t* count); - bool GetHandleTxIndex(std::int64_t *count); + storage::Status GetHandleTxIndex(int64_t* count); private: RWLock rwLock_; @@ -184,9 +184,14 @@ class DentryStorage { std::string table4AppliedIndex_; std::string table4Transaction_; std::string table4DentryCount_; - std::int64_t handleTxIndex_; + int64_t handleTxIndex_; uint64_t nDentry_; Converter conv_; + + static const char* kDentryCountKey; + static const char* kDentryAppliedKey; + static const char* kHandleTxKey; + static const char* kPendingTxKey; }; } // namespace metaserver diff --git a/curvefs/src/metaserver/inode_manager.cpp b/curvefs/src/metaserver/inode_manager.cpp index 5308923301..c72f0e5290 100644 --- a/curvefs/src/metaserver/inode_manager.cpp +++ b/curvefs/src/metaserver/inode_manager.cpp @@ -40,18 +40,33 @@ using ::curve::common::NameLockGuard; using ::curve::common::TimeUtility; using ::google::protobuf::util::MessageDifferencer; +#define CHECK_APPLIED() \ + do { \ + if (logIndex <= appliedIndex_) { \ + VLOG(3) << __func__ \ + << "Log entry already be applied, index = " \ + << logIndex \ + << "applied index = " << appliedIndex_; \ + return MetaStatusCode::IDEMPOTENCE_OK; \ + } \ + } while (false) + namespace curvefs { namespace metaserver { +bool InodeManager::Init() { + if (inodeStorage_->Init()) { + auto s = inodeStorage_->GetAppliedIndex(&appliedIndex_); + return s == MetaStatusCode::OK || s == MetaStatusCode::NOT_FOUND; + } + return false; +} + MetaStatusCode InodeManager::CreateInode(uint64_t inodeId, const InodeParam ¶m, Inode *newInode, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); VLOG(6) << "CreateInode, fsId = " << param.fsId << ", length = " << param.length << ", uid = " << param.uid << ", gid = " << param.gid << ", mode = " << param.mode @@ -84,12 +99,8 @@ MetaStatusCode InodeManager::CreateInode(uint64_t inodeId, } MetaStatusCode InodeManager::CreateRootInode(const InodeParam ¶m, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); LOG(INFO) << "CreateRootInode, fsId = " << param.fsId << ", uid = " << param.uid << ", gid = " << param.gid << ", mode = " << param.mode; @@ -115,12 +126,8 @@ MetaStatusCode InodeManager::CreateRootInode(const InodeParam ¶m, MetaStatusCode InodeManager::CreateManageInode(const InodeParam ¶m, ManageInodeType manageType, Inode *newInode, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); LOG(INFO) << "CreateManageInode, fsId = " << param.fsId << ", uid = " << param.uid << ", gid = " << param.gid << ", mode = " << param.mode; @@ -267,12 +274,8 @@ MetaStatusCode InodeManager::GetXAttr(uint32_t fsId, uint64_t inodeId, } MetaStatusCode InodeManager::DeleteInode(uint32_t fsId, uint64_t inodeId, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); VLOG(6) << "DeleteInode, fsId = " << fsId << ", inodeId = " << inodeId; NameLockGuard lg(inodeLock_, GetInodeLockName(fsId, inodeId)); InodeAttr attr; @@ -303,12 +306,8 @@ MetaStatusCode InodeManager::DeleteInode(uint32_t fsId, uint64_t inodeId, } MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest &request, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); VLOG(9) << "update inode, fsid: " << request.fsid() << ", inodeid: " << request.inodeid(); NameLockGuard lg(inodeLock_, @@ -326,10 +325,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) @@ -447,17 +446,13 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest &request, MetaStatusCode InodeManager::GetOrModifyS3ChunkInfo( uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap &map2add, const S3ChunkInfoMap &map2del, bool returnS3ChunkInfoMap, - std::shared_ptr *iterator4InodeS3Meta, std::int64_t logIndex) { + std::shared_ptr *iterator4InodeS3Meta, int64_t logIndex) { VLOG(6) << "GetOrModifyS3ChunkInfo, fsId: " << fsId << ", inodeId: " << inodeId; NameLockGuard lg(inodeLock_, GetInodeLockName(fsId, inodeId)); - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + CHECK_APPLIED(); const S3ChunkInfoList *list2add; const S3ChunkInfoList *list2del; @@ -543,14 +538,10 @@ MetaStatusCode InodeManager::PaddingInodeS3ChunkInfo(int32_t fsId, MetaStatusCode InodeManager::UpdateInodeWhenCreateOrRemoveSubNode( uint32_t fsId, uint64_t inodeId, FsFileType type, bool isCreate, - std::int64_t logIndex) { + int64_t logIndex) { VLOG(6) << "UpdateInodeWhenCreateOrRemoveSubNode, fsId = " << fsId << ", inodeId = " << inodeId << ", isCreate = " << isCreate; - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + CHECK_APPLIED(); NameLockGuard lg(inodeLock_, GetInodeLockName(fsId, inodeId)); Inode inode; @@ -591,12 +582,8 @@ MetaStatusCode InodeManager::UpdateInodeWhenCreateOrRemoveSubNode( } MetaStatusCode InodeManager::InsertInode(const Inode &inode, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); VLOG(6) << "InsertInode, " << inode.ShortDebugString(); // 2. insert inode @@ -621,14 +608,14 @@ bool InodeManager::GetInodeIdList(std::list *inodeIdList) { MetaStatusCode InodeManager::UpdateVolumeExtentSliceLocked(uint32_t fsId, uint64_t inodeId, const VolumeExtentSlice &slice, - std::int64_t logIndex) { + int64_t logIndex) { return inodeStorage_->UpdateVolumeExtentSlice(fsId, inodeId, slice, logIndex); } MetaStatusCode InodeManager::UpdateVolumeExtentSliceLocked( std::shared_ptr *txn, uint32_t fsId, - uint64_t inodeId, const VolumeExtentSlice &slice, std::int64_t logIndex) { + uint64_t inodeId, const VolumeExtentSlice &slice, int64_t logIndex) { return inodeStorage_->UpdateVolumeExtentSlice(txn, fsId, inodeId, slice, logIndex); } @@ -636,7 +623,7 @@ MetaStatusCode InodeManager::UpdateVolumeExtentSliceLocked( MetaStatusCode InodeManager::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, const VolumeExtentSlice &slice, - std::int64_t logIndex) { + int64_t logIndex) { VLOG(6) << "UpdateInodeExtent, fsId: " << fsId << ", inodeId: " << inodeId << ", slice offset: " << slice.offset(); NameLockGuard guard(inodeLock_, GetInodeLockName(fsId, inodeId)); @@ -646,7 +633,7 @@ InodeManager::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, MetaStatusCode InodeManager::UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, const VolumeExtentSliceList &extents, - std::int64_t logIndex) { + int64_t logIndex) { VLOG(6) << "UpdateInodeExtent, fsId: " << fsId << ", inodeId: " << inodeId; NameLockGuard guard(inodeLock_, GetInodeLockName(fsId, inodeId)); @@ -691,12 +678,8 @@ InodeManager::GetVolumeExtent(uint32_t fsId, uint64_t inodeId, MetaStatusCode InodeManager::UpdateDeallocatableBlockGroup( const UpdateDeallocatableBlockGroupRequest &request, - std::int64_t logIndex) { - if (logIndex <= appliedIndex_) { - LOG(INFO) << "Log entry already be applied, index = " << logIndex - << " applied index = " << appliedIndex_; - return MetaStatusCode::IDEMPOTENCE_OK; - } + int64_t logIndex) { + CHECK_APPLIED(); return inodeStorage_->UpdateDeallocatableBlockGroup( request.fsid(), request.update(), logIndex); } diff --git a/curvefs/src/metaserver/inode_manager.h b/curvefs/src/metaserver/inode_manager.h index da70c73e4f..8a9496e480 100644 --- a/curvefs/src/metaserver/inode_manager.h +++ b/curvefs/src/metaserver/inode_manager.h @@ -68,17 +68,18 @@ 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); + Inode *inode, int64_t logIndex); MetaStatusCode CreateRootInode(const InodeParam ¶m, - std::int64_t logIndex); + int64_t logIndex); MetaStatusCode CreateManageInode(const InodeParam ¶m, ManageInodeType manageType, Inode *inode, - std::int64_t logIndex); + int64_t logIndex); MetaStatusCode GetInode(uint32_t fsId, uint64_t inodeId, Inode *inode, bool paddingS3ChunkInfo = false); @@ -89,15 +90,15 @@ class InodeManager { MetaStatusCode GetXAttr(uint32_t fsId, uint64_t inodeId, XAttr *xattr); MetaStatusCode DeleteInode(uint32_t fsId, uint64_t inodeId, - std::int64_t logIndex); + int64_t logIndex); MetaStatusCode UpdateInode(const UpdateInodeRequest &request, - std::int64_t logIndex); + int64_t logIndex); MetaStatusCode GetOrModifyS3ChunkInfo( uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap &map2add, const S3ChunkInfoMap &map2del, bool returnS3ChunkInfoMap, - std::shared_ptr *iterator4InodeS3Meta, std::int64_t logIndex); + std::shared_ptr *iterator4InodeS3Meta, int64_t logIndex); MetaStatusCode PaddingInodeS3ChunkInfo(int32_t fsId, uint64_t inodeId, S3ChunkInfoMap *m, @@ -107,21 +108,21 @@ class InodeManager { uint64_t inodeId, FsFileType type, bool isCreate, - std::int64_t logIndex); + int64_t logIndex); - MetaStatusCode InsertInode(const Inode &inode, std::int64_t logIndex); + MetaStatusCode InsertInode(const Inode &inode, int64_t logIndex); bool GetInodeIdList(std::list *inodeIdList); // Update one or more volume extent slice MetaStatusCode UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, const VolumeExtentSliceList &extents, - std::int64_t logIndex); + int64_t logIndex); // Update only one volume extent slice MetaStatusCode UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, const VolumeExtentSlice &slice, - std::int64_t logIndex); + int64_t logIndex); MetaStatusCode GetVolumeExtent(uint32_t fsId, uint64_t inodeId, const std::vector &slices, @@ -129,7 +130,7 @@ class InodeManager { MetaStatusCode UpdateDeallocatableBlockGroup( const UpdateDeallocatableBlockGroupRequest &request, - std::int64_t logIndex); + int64_t logIndex); private: void GenerateInodeInternal(uint64_t inodeId, const InodeParam ¶m, @@ -145,18 +146,18 @@ class InodeManager { MetaStatusCode UpdateVolumeExtentSliceLocked(uint32_t fsId, uint64_t inodeId, const VolumeExtentSlice &slice, - std::int64_t logIndex); + int64_t logIndex); MetaStatusCode UpdateVolumeExtentSliceLocked( std::shared_ptr *txn, uint32_t fsId, uint64_t inodeId, const VolumeExtentSlice &slice, - std::int64_t logIndex); + int64_t logIndex); private: std::shared_ptr inodeStorage_; std::shared_ptr trash_; FileType2InodeNumMap *type2InodeNum_; - std::int64_t appliedIndex_; + int64_t appliedIndex_; NameLock inodeLock_; }; diff --git a/curvefs/src/metaserver/inode_storage.cpp b/curvefs/src/metaserver/inode_storage.cpp index 949eb611c2..e834317c27 100644 --- a/curvefs/src/metaserver/inode_storage.cpp +++ b/curvefs/src/metaserver/inode_storage.cpp @@ -57,6 +57,10 @@ using ::curvefs::metaserver::storage::Prefix4InodeS3ChunkInfoList; using ::curvefs::metaserver::storage::Prefix4InodeVolumeExtent; using ::curvefs::metaserver::storage::Status; +const char* InodeStorage::kInodeCountKey("count"); + +const char* InodeStorage::kInodeAppliedKey("inode"); + InodeStorage::InodeStorage(std::shared_ptr kvStorage, std::shared_ptr nameGenerator, uint64_t nInode) @@ -74,60 +78,65 @@ 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); + auto s = kvStorage_->SGet(table4InodeCount_, kInodeCountKey, &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); + return txn->SSet(table4InodeCount_, kInodeCountKey, val); } -storage::Status InodeStorage::DelInodeCount(storage::StorageTransaction *txn) { - return txn->SDel(table4InodeCount_, "count"); +storage::Status InodeStorage::DelInodeCount(storage::StorageTransaction* txn) { + return txn->SDel(table4InodeCount_, kInodeCountKey); } storage::Status -InodeStorage::SetAppliedIndex(storage::StorageTransaction *transaction, - std::int64_t index) { +InodeStorage::SetAppliedIndex(storage::StorageTransaction* transaction, + int64_t index) { common::AppliedIndex val; val.set_index(index); - return transaction->SSet(table4AppliedIndex_, "inode", val); + return transaction->SSet(table4AppliedIndex_, kInodeAppliedKey, val); } storage::Status -InodeStorage::DelAppliedIndex(storage::StorageTransaction *transaction) { - return transaction->SDel(table4AppliedIndex_, "inode"); +InodeStorage::DelAppliedIndex(storage::StorageTransaction* transaction) { + return transaction->SDel(table4AppliedIndex_, kInodeAppliedKey); } -bool InodeStorage::GetAppliedIndex(std::int64_t *index) { +MetaStatusCode InodeStorage::GetAppliedIndex(int64_t* index) { common::AppliedIndex val; - auto s = kvStorage_->SGet(table4AppliedIndex_, "inode", &val); + auto s = kvStorage_->SGet(table4AppliedIndex_, kInodeAppliedKey, &val); if (s.ok()) { *index = val.index(); - return true; + return MetaStatusCode::OK; } - return false; + if (s.IsNotFound()) { + return MetaStatusCode::NOT_FOUND; + } + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode InodeStorage::Insert(const Inode &inode, std::int64_t logIndex) { +MetaStatusCode InodeStorage::Insert(const Inode& inode, int64_t logIndex) { WriteLockGuard lg(rwLock_); Key4Inode key(inode.fsid(), inode.inodeid()); std::string skey = conv_.SerializeToString(key); - // NOTE: HGet() is cheap, because the key not found in most cases, // so the rocksdb storage only should check bloom filter. Inode out; @@ -137,50 +146,45 @@ MetaStatusCode InodeStorage::Insert(const Inode &inode, std::int64_t logIndex) { } else if (!s.IsNotFound()) { return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - // key not found - std::shared_ptr txn = - kvStorage_->BeginTransaction(); - s = txn->HSet(table4Inode_, skey, inode); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert inode transaction failed"; + const char* step = "Begin transaction"; + std::shared_ptr txn; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - LOG(ERROR) << "Insert inode to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = SetAppliedIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert inode transaction failed"; + s = txn->HSet(table4Inode_, skey, inode); + if (!s.ok()) { + step = "Insert inode to transaction"; + break; } - LOG(ERROR) << "Insert applied index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = SetInodeCount(txn.get(), nInode_ + 1); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert inode transaction failed"; + s = SetAppliedIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert applied index to transaction"; + break; + } + s = SetInodeCount(txn.get(), nInode_ + 1); + if (!s.ok()) { + step = "Insert inode count to transaction"; + break; + } + s = txn->Commit(); + if (!s.ok()) { + step = "Insert inode failed"; + break; } - LOG(ERROR) << "Insert inode count to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (s.ok()) { nInode_++; return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction failed"; } - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback insert inode transaction failed"; - } - LOG(ERROR) << "Insert inode failed, status = " << s.ToString(); 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 +200,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 +242,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,117 +259,108 @@ MetaStatusCode InodeStorage::GetXAttr(const Key4Inode &key, XAttr *xattr) { return MetaStatusCode::OK; } -MetaStatusCode InodeStorage::ForceDelete(const Key4Inode &key) { - WriteLockGuard lg(rwLock_); +// NOTE: if transaction success +// we will commit transaction +// it should be the last step of your operations +storage::Status +InodeStorage::DeleteInternal(storage::StorageTransaction *transaction, + const Key4Inode &key) { std::string skey = conv_.SerializeToString(key); - std::shared_ptr txn = - kvStorage_->BeginTransaction(); - Status s = txn->HDel(table4Inode_, skey); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete inode transaction failed, status = " - << s.ToString(); - } - LOG(ERROR) << "Delete inode from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - // NOTE: for rocksdb storage, it will never check whether - // the key exist in delete(), so if the client delete the - // unexist inode in some anbormal cases, it will cause the - // nInode less then the real value. - if (nInode_ > 0) { - s = SetInodeCount(txn.get(), nInode_ - 1); + Status s; + const char* step = "Delete inode from transaction"; + do { + s = transaction->HDel(table4Inode_, skey); if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete inode transaction failed"; + break; + } + // NOTE: for rocksdb storage, it will never check whether + // the key exist in delete(), so if the client delete the + // non-exist inode in some abnormal cases, it will cause the + // nInode less then the real value. + if (nInode_ > 0) { + s = SetInodeCount(transaction, nInode_ - 1); + if (!s.ok()) { + step = "Insert inode count to transaction"; + break; } - LOG(ERROR) << "Insert inode count to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - } - s = txn->Commit(); - if (s.ok()) { + s = transaction->Commit(); + if (!s.ok()) { + step = "Delete inode"; + break; + } // NOTE: for rocksdb storage, it will never check whether // the key exist in delete(), so if the client delete the - // unexist inode in some anbormal cases, it will cause the + // non-exist inode in some abnormal cases, it will cause the // nInode less then the real value. if (nInode_ > 0) { nInode_--; } + return s; + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + return s; +} + +MetaStatusCode InodeStorage::Delete(const Key4Inode& key, + int64_t logIndex) { + WriteLockGuard lg(rwLock_); + std::shared_ptr txn; + Status s; + const char* step = "Begin transaction"; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; + } + s = SetAppliedIndex(txn.get(), logIndex); + if (!s.ok()) { + step = "Insert applied index to transaction"; + break; + } + s = DeleteInternal(txn.get(), key); + if (!s.ok()) { + step = "Delete inode from transaction"; + break; + } return MetaStatusCode::OK; - } - if (!txn->Rollback().ok()) { + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { LOG(ERROR) << "Rollback delete inode transaction failed, status = " << s.ToString(); } - LOG(ERROR) << "Delete inode failed, status = " << s.ToString(); return MetaStatusCode::STORAGE_INTERNAL_ERROR; } -MetaStatusCode InodeStorage::Delete(const Key4Inode &key, - std::int64_t logIndex) { +MetaStatusCode InodeStorage::ForceDelete(const Key4Inode& key) { WriteLockGuard lg(rwLock_); - std::string skey = conv_.SerializeToString(key); - std::shared_ptr txn = - kvStorage_->BeginTransaction(); - Status s = txn->HDel(table4Inode_, skey); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete inode transaction failed, status = " - << s.ToString(); - } - LOG(ERROR) << "Delete inode from transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = SetAppliedIndex(txn.get(), logIndex); - if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete inode transaction failed"; + std::shared_ptr txn = nullptr; + Status s; + const char* step = "Begin transaction"; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - LOG(ERROR) << "Insert applied index to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - // NOTE: for rocksdb storage, it will never check whether - // the key exist in delete(), so if the client delete the - // unexist inode in some anbormal cases, it will cause the - // nInode less then the real value. - if (nInode_ > 0) { - s = SetInodeCount(txn.get(), nInode_ - 1); + s = DeleteInternal(txn.get(), key); if (!s.ok()) { - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback delete inode transaction failed"; - } - LOG(ERROR) << "Insert inode count to transaction failed, status = " - << s.ToString(); - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - } - s = txn->Commit(); - if (s.ok()) { - // NOTE: for rocksdb storage, it will never check whether - // the key exist in delete(), so if the client delete the - // unexist inode in some anbormal cases, it will cause the - // nInode less then the real value. - if (nInode_ > 0) { - nInode_--; + step = "Delete inode from transaction"; + break; } return MetaStatusCode::OK; - } - if (!txn->Rollback().ok()) { + } while (false); + LOG(ERROR) << step << " failed, status = " << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { LOG(ERROR) << "Rollback delete inode transaction failed, status = " << s.ToString(); } - LOG(ERROR) << "Delete inode failed, status = " << s.ToString(); return MetaStatusCode::STORAGE_INTERNAL_ERROR; } MetaStatusCode -InodeStorage::Update(std::shared_ptr *txn, - const Inode &inode, std::int64_t logIndex, +InodeStorage::Update(std::shared_ptr* txn, + const Inode& inode, int64_t logIndex, bool inodeDeallocate) { if (*txn == nullptr) { *txn = kvStorage_->BeginTransaction(); @@ -406,7 +401,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, int64_t logIndex, bool inodeDeallocate) { std::shared_ptr txn; if (Update(&txn, inode, logIndex, inodeDeallocate) == MetaStatusCode::OK) { @@ -418,7 +413,7 @@ MetaStatusCode InodeStorage::Update(const Inode &inode, std::int64_t logIndex, return MetaStatusCode::OK; } } - if (!txn->Rollback().ok()) { + if (txn != nullptr && !txn->Rollback().ok()) { LOG(ERROR) << "Rollback transaction failed"; } LOG(ERROR) << "Update inode failed"; @@ -432,7 +427,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) { @@ -508,38 +503,38 @@ MetaStatusCode InodeStorage::Clear() { << s.ToString(); return MetaStatusCode::STORAGE_INTERNAL_ERROR; } - auto txn = kvStorage_->BeginTransaction(); - if (txn == nullptr) { - LOG(ERROR) << "Begin transaction failed"; - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = DelInodeCount(txn.get()); - if (!s.ok()) { - LOG(ERROR) << "Delete inode count failed, status = " << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + std::shared_ptr txn; + const char* step = "Begin transaction"; + do { + txn = kvStorage_->BeginTransaction(); + if (txn == nullptr) { + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = DelAppliedIndex(txn.get()); - if (!s.ok()) { - LOG(ERROR) << "Delete applied index failed, status = " << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = DelInodeCount(txn.get()); + if (!s.ok()) { + step = "Delete inode count"; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; - } - s = txn->Commit(); - if (!s.ok()) { - LOG(ERROR) << "Commit clear InodeStorage transaction failed, status = " - << s.ToString(); - if (!txn->Rollback().ok()) { - LOG(ERROR) << "Rollback transaction failed"; + s = DelAppliedIndex(txn.get()); + if (!s.ok()) { + step = "Delete applied index"; + break; } - return MetaStatusCode::STORAGE_INTERNAL_ERROR; + s = txn->Commit(); + if (!s.ok()) { + step = "Commit clear InodeStorage transaction"; + break; + } + nInode_ = 0; + return MetaStatusCode::OK; + } while (false); + LOG(ERROR) << step + << " failed, status = " + << s.ToString(); + if (txn != nullptr && !txn->Rollback().ok()) { + LOG(ERROR) << "Rollback transaction failed"; } - nInode_ = 0; - return MetaStatusCode::OK; + return MetaStatusCode::STORAGE_INTERNAL_ERROR; } MetaStatusCode InodeStorage::UpdateInodeS3MetaSize(Transaction txn, @@ -598,9 +593,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 +606,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 +621,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 +659,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 +675,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, + int64_t logIndex) { if (*txn == nullptr) { *txn = kvStorage_->BeginTransaction(); if (*txn == nullptr) { @@ -721,13 +722,13 @@ MetaStatusCode InodeStorage::ModifyInodeS3ChunkInfoList( MetaStatusCode InodeStorage::ModifyInodeS3ChunkInfoList( uint32_t fsId, uint64_t inodeId, uint64_t chunkIndex, - const S3ChunkInfoList *list2add, const S3ChunkInfoList *list2del, - std::int64_t logIndex) { + const S3ChunkInfoList* list2add, const S3ChunkInfoList* list2del, + int64_t logIndex) { std::shared_ptr txn; MetaStatusCode rc = ModifyInodeS3ChunkInfoList( &txn, fsId, inodeId, chunkIndex, list2add, list2del, logIndex); if (rc != MetaStatusCode::OK) { - if (!txn->Rollback().ok()) { + if (txn != nullptr && !txn->Rollback().ok()) { LOG(ERROR) << "Rollback transaction failed"; rc = MetaStatusCode::STORAGE_INTERNAL_ERROR; } @@ -741,7 +742,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 +755,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 +804,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, int64_t logIndex) { WriteLockGuard guard(rwLock_); if (*txn == nullptr) { *txn = kvStorage_->BeginTransaction(); @@ -833,8 +834,8 @@ MetaStatusCode InodeStorage::UpdateVolumeExtentSlice( MetaStatusCode InodeStorage::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, - std::int64_t logIndex) { + const VolumeExtentSlice& slice, + int64_t logIndex) { std::shared_ptr txn; auto rc = UpdateVolumeExtentSlice(&txn, fsId, inodeId, slice, logIndex); if (rc != MetaStatusCode::OK) { @@ -856,13 +857,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 +890,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 +907,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 +933,14 @@ MetaStatusCode InodeStorage::GetAllBlockGroup( } MetaStatusCode InodeStorage::UpdateDeallocatableBlockGroup( - uint32_t fsId, const DeallocatableBlockGroupVec &update, - std::int64_t logIndex) { + uint32_t fsId, const DeallocatableBlockGroupVec& update, + 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 +1002,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 +1016,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 +1041,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 +1079,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..6c282642df 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); + MetaStatusCode GetAppliedIndex(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, 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, 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, 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, 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, - std::int64_t logIndex); + const S3ChunkInfoList* list2add, + const S3ChunkInfoList* list2del, + 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, 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, - std::int64_t logIndex); + const VolumeExtentSlice& slice, + int64_t logIndex); MetaStatusCode - UpdateVolumeExtentSlice(std::shared_ptr *txn, + UpdateVolumeExtentSlice(std::shared_ptr* txn, uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, - std::int64_t logIndex); + const VolumeExtentSlice& slice, + 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, - std::int64_t logIndex); + const DeallocatableBlockGroupVec& update, + int64_t logIndex); MetaStatusCode GetAllBlockGroup( - std::vector *deallocatableBlockGroupVec); + std::vector* deallocatableBlockGroupVec); private: MetaStatusCode UpdateInodeS3MetaSize(Transaction txn, uint32_t fsId, @@ -196,33 +199,39 @@ 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, - std::int64_t index); + storage::Status SetAppliedIndex(storage::StorageTransaction* transaction, + 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); + + // NOTE: if transaction success + // we will commit transaction + // it should be the last step of your operations + storage::Status DeleteInternal(storage::StorageTransaction* transaction, + const Key4Inode& key); private: // FIXME: please remove this lock, because we has locked each inode @@ -241,6 +250,9 @@ class InodeStorage { size_t nInode_; Converter conv_; + + static const char* kInodeCountKey; + static const char* kInodeAppliedKey; }; } // namespace metaserver diff --git a/curvefs/src/metaserver/metastore.cpp b/curvefs/src/metaserver/metastore.cpp index 4bfdd16ec7..f4d63cf08a 100644 --- a/curvefs/src/metaserver/metastore.cpp +++ b/curvefs/src/metaserver/metastore.cpp @@ -95,27 +95,28 @@ bool MetaStoreImpl::Load(const std::string &pathname) { const std::string metadata = pathname + "/" + kMetaDataFilename; - // NOTE: we need rocksdb data to recover - // metadata(applied index,pending tx) - // so it must be first initialized - auto succ = kvStorage_->Recover(pathname); + uint8_t version = 0; + auto succ = fstream.Load(metadata, &version); if (!succ) { - LOG(ERROR) << "Failed to recover storage"; + partitionMap_.clear(); + LOG(ERROR) << "Load metadata failed."; return false; } - uint8_t version = 0; - succ = fstream.Load(metadata, &version); + succ = kvStorage_->Recover(pathname); if (!succ) { - partitionMap_.clear(); - LOG(ERROR) << "Load metadata failed."; + LOG(ERROR) << "Failed to recover storage"; return false; } // 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 +168,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; } @@ -251,7 +244,7 @@ bool MetaStoreImpl::Destroy() { MetaStatusCode MetaStoreImpl::CreatePartition(const CreatePartitionRequest *request, CreatePartitionResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: we doesn't filter in-memory data changes (void)logIndex; WriteLockGuard writeLockGuard(rwLock_); @@ -274,7 +267,7 @@ MetaStoreImpl::CreatePartition(const CreatePartitionRequest *request, MetaStatusCode MetaStoreImpl::DeletePartition(const DeletePartitionRequest *request, DeletePartitionResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: we doesn't filter in-memory data changes (void)logIndex; WriteLockGuard writeLockGuard(rwLock_); @@ -371,7 +364,7 @@ std::shared_ptr MetaStoreImpl::GetStreamServer() { // dentry MetaStatusCode MetaStoreImpl::CreateDentry(const CreateDentryRequest *request, CreateDentryResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { ReadLockGuard readLockGuard(rwLock_); std::shared_ptr partition; GET_PARTITION_OR_RETURN(partition); @@ -384,7 +377,7 @@ MetaStatusCode MetaStoreImpl::CreateDentry(const CreateDentryRequest *request, MetaStatusCode MetaStoreImpl::GetDentry(const GetDentryRequest *request, GetDentryResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: filter read-only request is unnecessary (void)logIndex; uint32_t fsId = request->fsid(); @@ -412,7 +405,7 @@ MetaStatusCode MetaStoreImpl::GetDentry(const GetDentryRequest *request, MetaStatusCode MetaStoreImpl::DeleteDentry(const DeleteDentryRequest *request, DeleteDentryResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { uint32_t fsId = request->fsid(); uint64_t parentInodeId = request->parentinodeid(); std::string name = request->name(); @@ -436,7 +429,7 @@ MetaStatusCode MetaStoreImpl::DeleteDentry(const DeleteDentryRequest *request, MetaStatusCode MetaStoreImpl::ListDentry(const ListDentryRequest *request, ListDentryResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: filter read-only request is unnecessary (void)logIndex; uint32_t fsId = request->fsid(); @@ -473,7 +466,7 @@ MetaStatusCode MetaStoreImpl::ListDentry(const ListDentryRequest *request, MetaStatusCode MetaStoreImpl::PrepareRenameTx(const PrepareRenameTxRequest *request, PrepareRenameTxResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { ReadLockGuard readLockGuard(rwLock_); MetaStatusCode rc; std::shared_ptr partition; @@ -492,7 +485,7 @@ MetaStoreImpl::PrepareRenameTx(const PrepareRenameTxRequest *request, // inode MetaStatusCode MetaStoreImpl::CreateInode(const CreateInodeRequest *request, CreateInodeResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { InodeParam param; param.fsId = request->fsid(); param.length = request->length(); @@ -538,7 +531,7 @@ MetaStatusCode MetaStoreImpl::CreateInode(const CreateInodeRequest *request, MetaStatusCode MetaStoreImpl::CreateRootInode(const CreateRootInodeRequest *request, CreateRootInodeResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { InodeParam param; param.fsId = request->fsid(); param.uid = request->uid(); @@ -572,7 +565,7 @@ MetaStoreImpl::CreateRootInode(const CreateRootInodeRequest *request, MetaStatusCode MetaStoreImpl::CreateManageInode(const CreateManageInodeRequest *request, CreateManageInodeResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { InodeParam param; param.fsId = request->fsid(); param.uid = request->uid(); @@ -620,7 +613,7 @@ MetaStoreImpl::CreateManageInode(const CreateManageInodeRequest *request, MetaStatusCode MetaStoreImpl::GetInode(const GetInodeRequest *request, GetInodeResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: filter read-only request is unnecessary (void)logIndex; uint32_t fsId = request->fsid(); @@ -658,7 +651,7 @@ MetaStatusCode MetaStoreImpl::GetInode(const GetInodeRequest *request, MetaStatusCode MetaStoreImpl::BatchGetInodeAttr(const BatchGetInodeAttrRequest *request, BatchGetInodeAttrResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: filter read-only request is unnecessary (void)logIndex; ReadLockGuard readLockGuard(rwLock_); @@ -682,7 +675,7 @@ MetaStoreImpl::BatchGetInodeAttr(const BatchGetInodeAttrRequest *request, MetaStatusCode MetaStoreImpl::BatchGetXAttr(const BatchGetXAttrRequest *request, BatchGetXAttrResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: filter read-only request is unnecessary (void)logIndex; ReadLockGuard readLockGuard(rwLock_); @@ -706,7 +699,7 @@ MetaStatusCode MetaStoreImpl::BatchGetXAttr(const BatchGetXAttrRequest *request, MetaStatusCode MetaStoreImpl::DeleteInode(const DeleteInodeRequest *request, DeleteInodeResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { uint32_t fsId = request->fsid(); uint64_t inodeId = request->inodeid(); @@ -721,7 +714,7 @@ MetaStatusCode MetaStoreImpl::DeleteInode(const DeleteInodeRequest *request, MetaStatusCode MetaStoreImpl::UpdateInode(const UpdateInodeRequest *request, UpdateInodeResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { ReadLockGuard readLockGuard(rwLock_); VLOG(9) << "UpdateInode inode " << request->inodeid(); std::shared_ptr partition; @@ -735,7 +728,7 @@ MetaStatusCode MetaStoreImpl::UpdateInode(const UpdateInodeRequest *request, MetaStatusCode MetaStoreImpl::GetOrModifyS3ChunkInfo( const GetOrModifyS3ChunkInfoRequest *request, GetOrModifyS3ChunkInfoResponse *response, - std::shared_ptr *iterator, std::int64_t logIndex) { + std::shared_ptr *iterator, int64_t logIndex) { MetaStatusCode rc; ReadLockGuard readLockGuard(rwLock_); std::shared_ptr partition; @@ -805,7 +798,7 @@ std::shared_ptr MetaStoreImpl::GetPartition(uint32_t partitionId) { MetaStatusCode MetaStoreImpl::GetVolumeExtent(const GetVolumeExtentRequest *request, GetVolumeExtentResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { // NOTE: filter read-only request is unnecessary (void)logIndex; ReadLockGuard guard(rwLock_); @@ -827,7 +820,7 @@ MetaStoreImpl::GetVolumeExtent(const GetVolumeExtentRequest *request, MetaStatusCode MetaStoreImpl::UpdateVolumeExtent(const UpdateVolumeExtentRequest *request, UpdateVolumeExtentResponse *response, - std::int64_t logIndex) { + int64_t logIndex) { ReadLockGuard guard(rwLock_); std::shared_ptr partition; GET_PARTITION_OR_RETURN(partition); @@ -842,7 +835,7 @@ MetaStoreImpl::UpdateVolumeExtent(const UpdateVolumeExtentRequest *request, MetaStatusCode MetaStoreImpl::UpdateDeallocatableBlockGroup( const UpdateDeallocatableBlockGroupRequest *request, - UpdateDeallocatableBlockGroupResponse *response, std::int64_t logIndex) { + UpdateDeallocatableBlockGroupResponse *response, int64_t logIndex) { ReadLockGuard guard(rwLock_); std::shared_ptr partition; GET_PARTITION_OR_RETURN(partition); diff --git a/curvefs/src/metaserver/metastore.h b/curvefs/src/metaserver/metastore.h index 597158c7c7..5af0ab63a2 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,19 +112,21 @@ 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( const CreatePartitionRequest* request, CreatePartitionResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode DeletePartition( const DeletePartitionRequest* request, DeletePartitionResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual bool GetPartitionInfoList( std::list *partitionInfoList) = 0; @@ -136,66 +139,66 @@ class MetaStore { // dentry virtual MetaStatusCode CreateDentry(const CreateDentryRequest* request, CreateDentryResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode GetDentry(const GetDentryRequest* request, GetDentryResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode DeleteDentry(const DeleteDentryRequest* request, DeleteDentryResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode ListDentry(const ListDentryRequest* request, ListDentryResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode PrepareRenameTx( const PrepareRenameTxRequest* request, PrepareRenameTxResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; // inode virtual MetaStatusCode CreateInode(const CreateInodeRequest* request, CreateInodeResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode CreateRootInode( const CreateRootInodeRequest* request, CreateRootInodeResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode CreateManageInode( const CreateManageInodeRequest* request, CreateManageInodeResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode GetInode(const GetInodeRequest* request, GetInodeResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode BatchGetInodeAttr( const BatchGetInodeAttrRequest* request, BatchGetInodeAttrResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode BatchGetXAttr(const BatchGetXAttrRequest* request, BatchGetXAttrResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode DeleteInode(const DeleteInodeRequest* request, DeleteInodeResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode UpdateInode(const UpdateInodeRequest* request, UpdateInodeResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode GetOrModifyS3ChunkInfo( const GetOrModifyS3ChunkInfoRequest* request, GetOrModifyS3ChunkInfoResponse* response, std::shared_ptr* iterator, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode SendS3ChunkInfoByStream( std::shared_ptr connection, @@ -204,17 +207,17 @@ class MetaStore { virtual MetaStatusCode GetVolumeExtent( const GetVolumeExtentRequest* request, GetVolumeExtentResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode UpdateVolumeExtent( const UpdateVolumeExtentRequest* request, UpdateVolumeExtentResponse* response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; virtual MetaStatusCode UpdateDeallocatableBlockGroup( const UpdateDeallocatableBlockGroupRequest *request, UpdateDeallocatableBlockGroupResponse *response, - std::int64_t logIndex) = 0; + int64_t logIndex) = 0; }; class MetaStoreImpl : public MetaStore { @@ -224,18 +227,20 @@ 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; MetaStatusCode CreatePartition(const CreatePartitionRequest* request, CreatePartitionResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode DeletePartition(const DeletePartitionRequest* request, DeletePartitionResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; bool GetPartitionInfoList( std::list *partitionInfoList) override; @@ -248,57 +253,57 @@ class MetaStoreImpl : public MetaStore { // dentry MetaStatusCode CreateDentry(const CreateDentryRequest* request, CreateDentryResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode GetDentry(const GetDentryRequest* request, GetDentryResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode DeleteDentry(const DeleteDentryRequest* request, DeleteDentryResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode ListDentry(const ListDentryRequest* request, ListDentryResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode PrepareRenameTx(const PrepareRenameTxRequest* request, PrepareRenameTxResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; // inode MetaStatusCode CreateInode(const CreateInodeRequest* request, CreateInodeResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode CreateRootInode(const CreateRootInodeRequest* request, CreateRootInodeResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode CreateManageInode( const CreateManageInodeRequest* request, CreateManageInodeResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode GetInode(const GetInodeRequest* request, GetInodeResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode BatchGetInodeAttr(const BatchGetInodeAttrRequest* request, BatchGetInodeAttrResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode BatchGetXAttr(const BatchGetXAttrRequest* request, BatchGetXAttrResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode DeleteInode(const DeleteInodeRequest* request, DeleteInodeResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode UpdateInode(const UpdateInodeRequest* request, UpdateInodeResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; std::shared_ptr GetPartition(uint32_t partitionId); @@ -306,7 +311,7 @@ class MetaStoreImpl : public MetaStore { const GetOrModifyS3ChunkInfoRequest* request, GetOrModifyS3ChunkInfoResponse* response, std::shared_ptr* iterator, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode SendS3ChunkInfoByStream( std::shared_ptr connection, @@ -314,18 +319,18 @@ class MetaStoreImpl : public MetaStore { MetaStatusCode GetVolumeExtent(const GetVolumeExtentRequest* request, GetVolumeExtentResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; MetaStatusCode UpdateVolumeExtent( const UpdateVolumeExtentRequest* request, UpdateVolumeExtentResponse* response, - std::int64_t logIndex) override; + int64_t logIndex) override; // block group MetaStatusCode UpdateDeallocatableBlockGroup( const UpdateDeallocatableBlockGroupRequest *request, UpdateDeallocatableBlockGroupResponse *response, - std::int64_t logIndex) override; + int64_t logIndex) override; private: FRIEND_TEST(MetastoreTest, partition); diff --git a/curvefs/src/metaserver/metastore_fstream.cpp b/curvefs/src/metaserver/metastore_fstream.cpp index f7c2076a53..06cdae9e6d 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; } @@ -260,9 +264,6 @@ bool MetaStoreFStream::Save(const std::string &path, DumpFileClosure *done) { ChildrenType children; children.push_back(NewPartitionIterator()); - for (const auto &item : *partitionMap_) { - children.push_back(NewPendingTxIterator(item.second)); - } for (const auto &child : children) { if (nullptr == child) { diff --git a/curvefs/src/metaserver/partition.cpp b/curvefs/src/metaserver/partition.cpp index d3ea32e927..6a30527fa3 100644 --- a/curvefs/src/metaserver/partition.cpp +++ b/curvefs/src/metaserver/partition.cpp @@ -32,8 +32,6 @@ #include "curvefs/proto/metaserver.pb.h" #include "curvefs/src/metaserver/s3compact_manager.h" -#include "curvefs/src/metaserver/storage/common.h" -#include "curvefs/src/metaserver/storage/status.h" #include "curvefs/src/metaserver/trash_manager.h" #include "curvefs/src/metaserver/storage/converter.h" #include "curvefs/src/metaserver/s3compact.h" @@ -91,7 +89,6 @@ Partition::Partition(PartitionInfo partition, StartVolumeDeallocate(); } } - conv_ = std::make_shared(); } #define PRECHECK(fsId, inodeId) \ @@ -114,8 +111,8 @@ Partition::Partition(PartitionInfo partition, } \ } while (0) -MetaStatusCode Partition::CreateDentry(const Dentry &dentry, - std::int64_t logIndex) { +MetaStatusCode Partition::CreateDentry(const Dentry& dentry, + int64_t logIndex) { PRECHECK(dentry.fsid(), dentry.parentinodeid()); MetaStatusCode ret = dentryManager_->CreateDentry(dentry, logIndex); if (MetaStatusCode::OK == ret) { @@ -148,8 +145,8 @@ MetaStatusCode Partition::CreateDentry(const Dentry &dentry, } } -MetaStatusCode Partition::LoadDentry(const DentryVec &vec, bool merge, - std::int64_t logIndex) { +MetaStatusCode Partition::LoadDentry(const DentryVec& vec, bool merge, + int64_t logIndex) { auto dentry = vec.dentrys(0); PRECHECK(dentry.fsid(), dentry.parentinodeid()); @@ -161,8 +158,8 @@ MetaStatusCode Partition::LoadDentry(const DentryVec &vec, bool merge, return rc; } -MetaStatusCode Partition::DeleteDentry(const Dentry &dentry, - std::int64_t logIndex) { +MetaStatusCode Partition::DeleteDentry(const Dentry& dentry, + int64_t logIndex) { PRECHECK(dentry.fsid(), dentry.parentinodeid()); MetaStatusCode ret = dentryManager_->DeleteDentry(dentry, logIndex); @@ -193,13 +190,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 +204,19 @@ MetaStatusCode Partition::ListDentry(const Dentry &dentry, void Partition::ClearDentry() { dentryManager_->ClearDentry(); } -MetaStatusCode Partition::HandleRenameTx(const std::vector &dentrys, - std::int64_t logIndex) { - for (const auto &it : dentrys) { +MetaStatusCode Partition::HandleRenameTx(const std::vector& dentrys, + int64_t logIndex) { + 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 +226,21 @@ 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(); - out->set_poolid(partitionInfo_.poolid()); - out->set_copysetid(partitionInfo_.copysetid()); - out->set_partitionid(partitionInfo_.partitionid()); - *out->mutable_dentrys() = {dentrys->begin(), dentrys->end()}; +void Partition::SerializeRenameTx(const RenameTx& in, + PrepareRenameTxRequest* out) { + txManager_->SerializeRenameTx(in, out); } -bool Partition::FindPendingTx(PrepareRenameTxRequest *pendingTx) { +bool Partition::FindPendingTx(PrepareRenameTxRequest* pendingTx) { if (GetStatus() == PartitionStatus::DELETING) { return false; } @@ -256,8 +256,8 @@ bool Partition::FindPendingTx(PrepareRenameTxRequest *pendingTx) { } // inode -MetaStatusCode Partition::CreateInode(const InodeParam ¶m, Inode *inode, - std::int64_t logIndex) { +MetaStatusCode Partition::CreateInode(const InodeParam& param, Inode* inode, + int64_t logIndex) { if (GetStatus() == PartitionStatus::READONLY) { return MetaStatusCode::PARTITION_ALLOC_ID_FAIL; } @@ -282,8 +282,8 @@ MetaStatusCode Partition::CreateInode(const InodeParam ¶m, Inode *inode, return ret; } -MetaStatusCode Partition::CreateRootInode(const InodeParam ¶m, - std::int64_t logIndex) { +MetaStatusCode Partition::CreateRootInode(const InodeParam& param, + int64_t logIndex) { PRECHECK_FSID(param.fsId); auto ret = inodeManager_->CreateRootInode(param, logIndex); if (ret == MetaStatusCode::IDEMPOTENCE_OK) { @@ -292,10 +292,10 @@ MetaStatusCode Partition::CreateRootInode(const InodeParam ¶m, return ret; } -MetaStatusCode Partition::CreateManageInode(const InodeParam ¶m, +MetaStatusCode Partition::CreateManageInode(const InodeParam& param, ManageInodeType manageType, - Inode *inode, - std::int64_t logIndex) { + Inode* inode, + int64_t logIndex) { PRECHECK_FSID(param.fsId); auto ret = inodeManager_->CreateManageInode(param, manageType, inode, logIndex); @@ -306,25 +306,25 @@ 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); } MetaStatusCode Partition::DeleteInode(uint32_t fsId, uint64_t inodeId, - std::int64_t logIndex) { + int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = inodeManager_->DeleteInode(fsId, inodeId, logIndex); if (ret == MetaStatusCode::IDEMPOTENCE_OK) { @@ -333,8 +333,8 @@ MetaStatusCode Partition::DeleteInode(uint32_t fsId, uint64_t inodeId, return ret; } -MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest &request, - std::int64_t logIndex) { +MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest& request, + int64_t logIndex) { PRECHECK(request.fsid(), request.inodeid()); auto ret = inodeManager_->UpdateInode(request, logIndex); if (ret == MetaStatusCode::IDEMPOTENCE_OK) { @@ -344,9 +344,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, int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = inodeManager_->GetOrModifyS3ChunkInfo( fsId, inodeId, map2add, map2del, returnS3ChunkInfoMap, iterator, @@ -359,14 +359,14 @@ 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, - std::int64_t logIndex) { +MetaStatusCode Partition::InsertInode(const Inode& inode, + int64_t logIndex) { PRECHECK(inode.fsid(), inode.inodeid()); auto ret = inodeManager_->InsertInode(inode, logIndex); if (ret == MetaStatusCode::IDEMPOTENCE_OK) { @@ -375,7 +375,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 +454,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 +463,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 +485,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,8 +503,8 @@ std::string Partition::GetDentryTablename() { MetaStatusCode Partition::UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSliceList &extents, - std::int64_t logIndex) { + const VolumeExtentSliceList& extents, + int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = inodeManager_->UpdateVolumeExtent(fsId, inodeId, extents, logIndex); @@ -510,8 +516,8 @@ Partition::UpdateVolumeExtent(uint32_t fsId, uint64_t inodeId, MetaStatusCode Partition::UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, - std::int64_t logIndex) { + const VolumeExtentSlice& slice, + int64_t logIndex) { PRECHECK(fsId, inodeId); auto ret = inodeManager_->UpdateVolumeExtentSlice(fsId, inodeId, slice, logIndex); @@ -522,15 +528,15 @@ 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, - std::int64_t logIndex) { + const UpdateDeallocatableBlockGroupRequest& request, + int64_t logIndex) { PRECHECK_FSID(request.fsid()); auto ret = inodeManager_->UpdateDeallocatableBlockGroup(request, logIndex); if (ret == MetaStatusCode::IDEMPOTENCE_OK) { @@ -540,7 +546,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..ab019b6646 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, int64_t logIndex); - MetaStatusCode LoadDentry(const DentryVec &vec, bool merge, - std::int64_t logIndex); + MetaStatusCode LoadDentry(const DentryVec& vec, bool merge, + int64_t logIndex); - MetaStatusCode DeleteDentry(const Dentry &dentry, std::int64_t logIndex); + MetaStatusCode DeleteDentry(const Dentry& dentry, 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, - std::int64_t logIndex); + MetaStatusCode HandleRenameTx(const std::vector& dentrys, + 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, - std::int64_t logIndex); + MetaStatusCode CreateInode(const InodeParam& param, Inode* inode, + int64_t logIndex); - MetaStatusCode CreateRootInode(const InodeParam ¶m, - std::int64_t logIndex); + MetaStatusCode CreateRootInode(const InodeParam& param, + int64_t logIndex); - MetaStatusCode CreateManageInode(const InodeParam ¶m, - ManageInodeType manageType, Inode *inode, - std::int64_t logIndex); + MetaStatusCode CreateManageInode(const InodeParam& param, + ManageInodeType manageType, Inode* inode, + 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); + int64_t logIndex); - MetaStatusCode UpdateInode(const UpdateInodeRequest &request, - std::int64_t logIndex); + MetaStatusCode UpdateInode(const UpdateInodeRequest& request, + 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::int64_t logIndex); + std::shared_ptr* iterator, + 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, - std::int64_t logIndex); + const VolumeExtentSliceList& extents, + int64_t logIndex); MetaStatusCode UpdateVolumeExtentSlice(uint32_t fsId, uint64_t inodeId, - const VolumeExtentSlice &slice, - std::int64_t logIndex); + const VolumeExtentSlice& slice, + 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, - std::int64_t logIndex); + const UpdateDeallocatableBlockGroupRequest& request, + 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, 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(); @@ -225,7 +225,6 @@ class Partition { std::shared_ptr txManager_; PartitionInfo partitionInfo_; - std::shared_ptr conv_; }; } // namespace metaserver } // namespace curvefs 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/dumpfile.cpp b/curvefs/src/metaserver/storage/dumpfile.cpp index dd2d03c4f0..9aa1e5f33a 100644 --- a/curvefs/src/metaserver/storage/dumpfile.cpp +++ b/curvefs/src/metaserver/storage/dumpfile.cpp @@ -67,7 +67,7 @@ using ::curve::common::CRC32; const std::string DumpFile::kCurvefs_ = "CURVEFS"; // NOLINT const uint32_t DumpFile::kEOF_ = 0; -const uint8_t DumpFile::kVersion_ = kDumpFileV3; +const uint8_t DumpFile::kVersion_ = kDumpFileV4; const uint32_t DumpFile::kMaxStringLength_ = 1024 * 1024 * 1024; // 1GB diff --git a/curvefs/src/metaserver/storage/dumpfile.h b/curvefs/src/metaserver/storage/dumpfile.h index 3bcc3104a3..f107a6e8a1 100644 --- a/curvefs/src/metaserver/storage/dumpfile.h +++ b/curvefs/src/metaserver/storage/dumpfile.h @@ -87,6 +87,10 @@ enum DumpFileVersion : uint8_t { // kDumpFileV2 (because they're not inserted into rocksdb), other metadata // is saved by rocksdb kDumpFileV3 = 3, + // Version 4 only dumps partitions into file based on + // kDumpFileV3 (because they're not inserted into rocksdb), other metadata + // is saved by rocksdb + kDumpFileV4 = 4, }; std::ostream& operator<<(std::ostream& os, DUMPFILE_ERROR code); diff --git a/curvefs/src/metaserver/storage/memory_storage.cpp b/curvefs/src/metaserver/storage/memory_storage.cpp index 717da7a5ef..feef149e1f 100644 --- a/curvefs/src/metaserver/storage/memory_storage.cpp +++ b/curvefs/src/metaserver/storage/memory_storage.cpp @@ -310,9 +310,7 @@ bool MemoryStorage::Checkpoint(const std::string& dir, bool MemoryStorage::Recover(const std::string& dir) { (void)dir; LOG(WARNING) << "Not supported"; - // NOTE: for compatibility - // we cannot return false in here - return true; + return false; } } // namespace storage 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/storage/storage_fstream.h b/curvefs/src/metaserver/storage/storage_fstream.h index c4f24aa28d..e26dcf463d 100644 --- a/curvefs/src/metaserver/storage/storage_fstream.h +++ b/curvefs/src/metaserver/storage/storage_fstream.h @@ -183,6 +183,11 @@ inline bool LoadFromFile(const std::string &pathname, uint8_t *version, std::string key = ukey.second; std::string value = iter->Value(); uint8_t version = dumpfile.GetVersion(); + if (version < static_cast(DumpFileVersion::kDumpFileV3)) { + LOG(ERROR) << "The dumpfile is too old, " + << "the version of dumpfile should be V3 at least"; + return false; + } switch (entryType) { CASE_TYPE_CALLBACK(INODE); CASE_TYPE_CALLBACK(DENTRY); diff --git a/curvefs/src/metaserver/transaction.cpp b/curvefs/src/metaserver/transaction.cpp index fc6ba7e6dc..38a2889a3d 100644 --- a/curvefs/src/metaserver/transaction.cpp +++ b/curvefs/src/metaserver/transaction.cpp @@ -29,24 +29,25 @@ 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) { +bool RenameTx::Prepare(const std::string &txPayload, int64_t logIndex) { metaserver::TransactionRequest request; - request.set_type(static_cast(TxType::Rename)); + request.set_type(metaserver::TransactionRequest::Rename); request.set_rawpayload(txPayload); return storage_->PrepareTx(dentrys_, request, logIndex) == MetaStatusCode::OK; } -bool RenameTx::Commit(std::int64_t logIndex) { +bool RenameTx::Commit(int64_t logIndex) { return storage_->CommitTx(dentrys_, logIndex) == MetaStatusCode::OK; } -bool RenameTx::Rollback(std::int64_t logIndex) { +bool RenameTx::Rollback(int64_t logIndex) { return storage_->RollbackTx(dentrys_, logIndex) == MetaStatusCode::OK; } @@ -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::GetDentrys() 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++) { @@ -76,10 +77,10 @@ std::ostream &operator<<(std::ostream &os, const RenameTx &renameTx) { TxManager::TxManager(std::shared_ptr storage, common::PartitionInfo partitionInfo) - : storage_(std::move(storage)), conv_(std::make_shared()), + : storage_(std::move(storage)), conv_(), 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,36 +95,39 @@ 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) { + const auto* dentrys = in.GetDentrys(); 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)) { - TxType type = static_cast(request.type()); - if (type == TxType::None) { + auto s = storage_->GetPendingTx(&request); + if (s == MetaStatusCode::OK) { + auto txType = request.type(); + if (txType == metaserver::TransactionRequest::None) { // NOTE: if tx type is none // means that pending tx is empty pendingTx_ = EMPTY_TX; - } else if (type == TxType::Rename) { + } else if (txType == metaserver::TransactionRequest::Rename) { std::string txPayload = request.rawpayload(); PrepareRenameTxRequest request; - conv_->ParseFromString(txPayload, &request); + conv_.ParseFromString(txPayload, &request); RenameTx tx({request.dentrys().begin(), request.dentrys().end()}, storage_); pendingTx_ = tx; } + return true; } + return s == MetaStatusCode::NOT_FOUND; } -MetaStatusCode TxManager::HandleRenameTx(const std::vector &dentrys, - std::int64_t logIndex) { +MetaStatusCode TxManager::HandleRenameTx(const std::vector& dentrys, + int64_t logIndex) { auto rc = PreCheck(dentrys); if (rc != MetaStatusCode::OK) { return rc; @@ -156,7 +160,7 @@ MetaStatusCode TxManager::HandleRenameTx(const std::vector &dentrys, PrepareRenameTxRequest request; SerializeRenameTx(renameTx, &request); std::string txPayload; - conv_->SerializeToString(request, &txPayload); + conv_.SerializeToString(request, &txPayload); if (!renameTx.Prepare(txPayload, logIndex)) { LOG(ERROR) << "Prepare for RenameTx failed, renameTx: " << renameTx; return MetaStatusCode::HANDLE_TX_FAILED; @@ -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,8 +192,8 @@ bool TxManager::FindPendingTx(RenameTx *pendingTx) { return true; } -bool TxManager::HandlePendingTx(uint64_t txId, RenameTx *pendingTx, - std::int64_t logIndex) { +bool TxManager::HandlePendingTx(uint64_t txId, RenameTx* pendingTx, + 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..7a1f9d6d98 100644 --- a/curvefs/src/metaserver/transaction.h +++ b/curvefs/src/metaserver/transaction.h @@ -35,32 +35,30 @@ namespace curvefs { namespace metaserver { -enum class TxType : std::uint32_t { None = 0, Rename = 1 }; - 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); + bool Prepare(const std::string &txPayload, int64_t logIndex); - bool Commit(std::int64_t logIndex); + bool Commit(int64_t logIndex); - bool Rollback(std::int64_t logIndex); + bool Rollback(int64_t logIndex); uint64_t GetTxId(); uint64_t GetTxSequence(); - std::vector *GetDentrys(); + std::vector* GetDentrys(); - const std::vector *GetConstDentrys() const; + const std::vector* GetDentrys() 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 +76,23 @@ class TxManager { explicit TxManager(std::shared_ptr storage, common::PartitionInfo partitionInfo); - MetaStatusCode HandleRenameTx(const std::vector &dentrys, - std::int64_t logIndex); + MetaStatusCode HandleRenameTx(const std::vector& dentrys, + 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, - std::int64_t logIndex); + bool HandlePendingTx(uint64_t txId, RenameTx* pendingTx, + int64_t logIndex); - void SerializeRenameTx(const RenameTx &in, PrepareRenameTxRequest *out); + void SerializeRenameTx(const RenameTx& in, PrepareRenameTxRequest* out); - void LoadPengingTx(); + bool Init(); private: RWLock rwLock_; @@ -103,7 +101,7 @@ class TxManager { RenameTx EMPTY_TX, pendingTx_; - std::shared_ptr conv_; + Converter conv_; common::PartitionInfo partitionInfo_; }; 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..5ff178dd03 100644 --- a/curvefs/test/metaserver/copyset/meta_operator_test.cpp +++ b/curvefs/test/metaserver/copyset/meta_operator_test.cpp @@ -45,8 +45,8 @@ const int kDummyServerPort = 32000; template -MetaStatusCode FakeOnApplyFunc(const RequestT *request, ResponseT *response, - std::int64_t logIndex) { +MetaStatusCode FakeOnApplyFunc(const RequestT* request, ResponseT* response, + int64_t logIndex) { (void)logIndex; response->set_statuscode(code); return 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; @@ -123,7 +123,7 @@ class MetaOperatorTest : public testing::Test { protected: MockCopysetNodeManager mockNodeManager_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(MetaOperatorTest, OperatorTypeTest) { @@ -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,10 +205,10 @@ TEST_F(MetaOperatorTest, OnApplyErrorTest) { // it's only for GetOrModifyS3ChunkInfo() { EXPECT_CALL(*mockMetaStore, GetOrModifyS3ChunkInfo(_, _, _, _)) - .WillOnce(Invoke([&](const GetOrModifyS3ChunkInfoRequest *request, - GetOrModifyS3ChunkInfoResponse *response, - std::shared_ptr *iterator, - std::int64_t logIndex) { + .WillOnce(Invoke([&](const GetOrModifyS3ChunkInfoRequest* request, + GetOrModifyS3ChunkInfoResponse* response, + std::shared_ptr* iterator, + int64_t logIndex) { (void)logIndex; response->set_statuscode(MetaStatusCode::UNKNOWN_ERROR); return 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..1ffef8882b 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); @@ -107,7 +113,7 @@ class DentryManagerTest : public ::testing::Test { std::shared_ptr dentryStorage_; std::shared_ptr dentryManager_; std::shared_ptr txManager_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(DentryManagerTest, CreateDentry) { diff --git a/curvefs/test/metaserver/dentry_storage_test.cpp b/curvefs/test/metaserver/dentry_storage_test.cpp index 886b17ba22..bde4ecd537 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,11 +91,11 @@ 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); + request.set_type(metaserver::TransactionRequest::None); request.set_rawpayload(""); auto rc = storage->PrepareTx(dentrys, request, logIndex_++); @@ -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); } @@ -112,12 +112,12 @@ class DentryStorageTest : public ::testing::Test { std::string dataDir_; std::shared_ptr nameGenerator_; std::shared_ptr kvStorage_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(DentryStorageTest, Insert) { DentryStorage storage(kvStorage_, nameGenerator_, 0); - + ASSERT_TRUE(storage.Init()); Dentry dentry; dentry.set_fsid(1); dentry.set_parentinodeid(1); @@ -149,7 +149,7 @@ TEST_F(DentryStorageTest, Insert) { // CASE 4: direct insert success by handle tx // NOTE: store real transaction is unnecessary metaserver::TransactionRequest request; - request.set_type(0); + request.set_type(metaserver::TransactionRequest::None); request.set_rawpayload(""); auto rc = storage.PrepareTx({dentry}, request, logIndex_++); ASSERT_EQ(rc, MetaStatusCode::OK); @@ -163,10 +163,11 @@ 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; - request.set_type(0); + request.set_type(metaserver::TransactionRequest::None); request.set_rawpayload(""); Dentry dentry; dentry.set_fsid(1); @@ -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,15 +493,15 @@ 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 metaserver::TransactionRequest request; - request.set_type(0); + request.set_type(metaserver::TransactionRequest::None); 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..2b60cde3b4 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; @@ -186,7 +188,7 @@ class InodeManagerTest : public ::testing::Test { std::string dataDir_; std::shared_ptr kvStorage_; std::shared_ptr filetype2InodeNum_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(InodeManagerTest, test1) { @@ -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..47c10a0aed 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) { @@ -190,11 +190,12 @@ class InodeStorageTest : public ::testing::Test { std::shared_ptr nameGenerator_; std::shared_ptr kvStorage_; std::shared_ptr conv_; - std::int64_t logIndex_; + int64_t logIndex_; }; 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,10 +796,10 @@ 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::int64_t logIndex) { + std::vector* out, + 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..1b95839902 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; } @@ -279,7 +284,7 @@ class MetastoreTest : public ::testing::Test { std::shared_ptr copyset_; std::shared_ptr mdsCli_; StorageOptions options_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(MetastoreTest, partition) { @@ -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..e999331bc0 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,90 +39,93 @@ 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 *, - std::int64_t logIndex)); - MOCK_METHOD3(DeletePartition, MetaStatusCode(const DeletePartitionRequest *, - DeletePartitionResponse *, - std::int64_t logIndex)); - MOCK_METHOD1(GetPartitionInfoList, bool(std::list *)); + MOCK_METHOD3(CreatePartition, MetaStatusCode(const CreatePartitionRequest*, + CreatePartitionResponse*, + int64_t logIndex)); + MOCK_METHOD3(DeletePartition, MetaStatusCode(const DeletePartitionRequest*, + DeletePartitionResponse*, + int64_t logIndex)); + 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*, int64_t logIndex)); MOCK_METHOD3(DeleteDentry, - MetaStatusCode(const DeleteDentryRequest *, - DeleteDentryResponse *, std::int64_t logIndex)); + MetaStatusCode(const DeleteDentryRequest*, + DeleteDentryResponse*, int64_t logIndex)); MOCK_METHOD3(GetDentry, - MetaStatusCode(const GetDentryRequest *, GetDentryResponse *, - std::int64_t logIndex)); + MetaStatusCode(const GetDentryRequest*, GetDentryResponse*, + int64_t logIndex)); MOCK_METHOD3(ListDentry, - MetaStatusCode(const ListDentryRequest *, ListDentryResponse *, - std::int64_t logIndex)); + MetaStatusCode(const ListDentryRequest*, ListDentryResponse*, + int64_t logIndex)); MOCK_METHOD3(CreateInode, - MetaStatusCode(const CreateInodeRequest *, - CreateInodeResponse *, std::int64_t logIndex)); - MOCK_METHOD3(CreateRootInode, MetaStatusCode(const CreateRootInodeRequest *, - CreateRootInodeResponse *, - std::int64_t logIndex)); + MetaStatusCode(const CreateInodeRequest*, CreateInodeResponse*, + int64_t logIndex)); + MOCK_METHOD3(CreateRootInode, MetaStatusCode(const CreateRootInodeRequest*, + CreateRootInodeResponse*, + int64_t logIndex)); MOCK_METHOD3(CreateManageInode, - MetaStatusCode(const CreateManageInodeRequest *, - CreateManageInodeResponse *, - std::int64_t logIndex)); + MetaStatusCode(const CreateManageInodeRequest*, + CreateManageInodeResponse*, + int64_t logIndex)); MOCK_METHOD3(GetInode, - MetaStatusCode(const GetInodeRequest *, GetInodeResponse *, - std::int64_t logIndex)); + MetaStatusCode(const GetInodeRequest*, GetInodeResponse*, + int64_t logIndex)); MOCK_METHOD3(BatchGetInodeAttr, - MetaStatusCode(const BatchGetInodeAttrRequest *, - BatchGetInodeAttrResponse *, - std::int64_t logIndex)); - MOCK_METHOD3(BatchGetXAttr, MetaStatusCode(const BatchGetXAttrRequest *, - BatchGetXAttrResponse *, - std::int64_t logIndex)); + MetaStatusCode(const BatchGetInodeAttrRequest*, + BatchGetInodeAttrResponse*, + int64_t logIndex)); + MOCK_METHOD3(BatchGetXAttr, + MetaStatusCode(const BatchGetXAttrRequest*, + BatchGetXAttrResponse*, int64_t logIndex)); MOCK_METHOD3(DeleteInode, - MetaStatusCode(const DeleteInodeRequest *, - DeleteInodeResponse *, std::int64_t logIndex)); + MetaStatusCode(const DeleteInodeRequest*, DeleteInodeResponse*, + int64_t logIndex)); MOCK_METHOD3(UpdateInode, - MetaStatusCode(const UpdateInodeRequest *, - UpdateInodeResponse *, std::int64_t logIndex)); + MetaStatusCode(const UpdateInodeRequest*, UpdateInodeResponse*, + int64_t logIndex)); - MOCK_METHOD3(PrepareRenameTx, MetaStatusCode(const PrepareRenameTxRequest *, - PrepareRenameTxResponse *, - std::int64_t logIndex)); + MOCK_METHOD3(PrepareRenameTx, MetaStatusCode(const PrepareRenameTxRequest*, + PrepareRenameTxResponse*, + int64_t logIndex)); MOCK_METHOD0(GetStreamServer, std::shared_ptr()); MOCK_METHOD4(GetOrModifyS3ChunkInfo, - MetaStatusCode(const GetOrModifyS3ChunkInfoRequest *request, - GetOrModifyS3ChunkInfoResponse *response, - std::shared_ptr *iterator, - std::int64_t logIndex)); + MetaStatusCode(const GetOrModifyS3ChunkInfoRequest* request, + GetOrModifyS3ChunkInfoResponse* response, + std::shared_ptr* iterator, + int64_t logIndex)); MOCK_METHOD2(SendS3ChunkInfoByStream, MetaStatusCode(std::shared_ptr connection, std::shared_ptr iterator)); - MOCK_METHOD3(GetVolumeExtent, MetaStatusCode(const GetVolumeExtentRequest *, - GetVolumeExtentResponse *, - std::int64_t logIndex)); + MOCK_METHOD3(GetVolumeExtent, MetaStatusCode(const GetVolumeExtentRequest*, + GetVolumeExtentResponse*, + int64_t logIndex)); MOCK_METHOD3(UpdateVolumeExtent, - MetaStatusCode(const UpdateVolumeExtentRequest *, - UpdateVolumeExtentResponse *, - std::int64_t logIndex)); + MetaStatusCode(const UpdateVolumeExtentRequest*, + UpdateVolumeExtentResponse*, + int64_t logIndex)); MOCK_METHOD3(UpdateDeallocatableBlockGroup, - MetaStatusCode(const UpdateDeallocatableBlockGroupRequest *, - UpdateDeallocatableBlockGroupResponse *, - std::int64_t logIndex)); + MetaStatusCode(const UpdateDeallocatableBlockGroupRequest*, + UpdateDeallocatableBlockGroupResponse*, + int64_t logIndex)); }; } // namespace mock 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_clean_test.cpp b/curvefs/test/metaserver/partition_clean_test.cpp index bac684acfd..7c3aee1a9e 100644 --- a/curvefs/test/metaserver/partition_clean_test.cpp +++ b/curvefs/test/metaserver/partition_clean_test.cpp @@ -98,7 +98,7 @@ class PartitionCleanManagerTest : public testing::Test { std::shared_ptr mdsCli_; std::shared_ptr s3Adaptor_; MockCopysetNode *copyset_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(PartitionCleanManagerTest, test1) { diff --git a/curvefs/test/metaserver/partition_test.cpp b/curvefs/test/metaserver/partition_test.cpp index e5cfd8d0e3..4448d05f8e 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"), @@ -101,7 +101,7 @@ class PartitionTest : public ::testing::Test { std::string dataDir_; StorageOptions options_; std::shared_ptr kvStorage_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(PartitionTest, testInodeIdGen1) { @@ -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..5663c2c295 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"), @@ -149,7 +149,7 @@ class RecycleCleanerTest : public testing::Test { std::shared_ptr metaClient_; std::shared_ptr partition_; copyset::MockCopysetNode copysetNode_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(RecycleCleanerTest, time_func_test) { diff --git a/curvefs/test/metaserver/recycle_manager_test.cpp b/curvefs/test/metaserver/recycle_manager_test.cpp index c30ebfd6be..7d086b6652 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"), @@ -103,11 +103,11 @@ class RecycleManangeTest : public testing::Test { std::string dataDir_; std::shared_ptr kvStorage_; std::shared_ptr mdsclient_; - std::int64_t logIndex_; + int64_t logIndex_; }; 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..0e348f56fd 100644 --- a/curvefs/test/metaserver/s3compact/s3compact_test.cpp +++ b/curvefs/test/metaserver/s3compact/s3compact_test.cpp @@ -130,7 +130,7 @@ class S3CompactTest : public ::testing::Test { std::string dataDir_; std::shared_ptr kvStorage_; std::shared_ptr filetype2InodeNum_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(S3CompactTest, test_CopysetNodeWrapper) { @@ -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..2c0a81e03f 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,18 +105,18 @@ 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); + request.set_type(metaserver::TransactionRequest::None); request.set_rawpayload(""); auto rc = storage->PrepareTx(dentrys, request, logIndex_++); ASSERT_EQ(rc, MetaStatusCode::OK); 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); } @@ -124,7 +130,7 @@ class TransactionTest : public ::testing::Test { std::shared_ptr dentryStorage_; std::shared_ptr dentryManager_; std::shared_ptr txManager_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(TransactionTest, PreCheck) { diff --git a/curvefs/test/metaserver/trash_test.cpp b/curvefs/test/metaserver/trash_test.cpp index 02ce93d257..56edf05f7a 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"), @@ -138,7 +138,7 @@ class TestTrash : public ::testing::Test { std::shared_ptr kvStorage_; std::shared_ptr inodeStorage_; std::shared_ptr trashManager_; - std::int64_t logIndex_; + int64_t logIndex_; }; TEST_F(TestTrash, testAdd3ItemAndDelete) {