Skip to content

Commit

Permalink
fix meta update (#5326)
Browse files Browse the repository at this point in the history
* fix meta update

* fix bug

---------

Co-authored-by: Sophie <[email protected]>
  • Loading branch information
cangfengzhs and Sophie-Xie committed Feb 9, 2023
1 parent f733d81 commit 33828b7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 52 deletions.
34 changes: 18 additions & 16 deletions src/daemons/MetaDaemonInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,30 @@ std::unique_ptr<nebula::kvstore::KVStore> initKV(std::vector<nebula::HostAddr> p
}

auto version = nebula::meta::MetaVersionMan::getMetaVersionFromKV(kvstore.get());
LOG(INFO) << "Get meta version is " << static_cast<int32_t>(version);
if (version == nebula::meta::MetaVersion::UNKNOWN) {
LOG(ERROR) << "Meta version is invalid";
return nullptr;
} else if (version == nebula::meta::MetaVersion::V1) {
LOG(ERROR) << "Can't upgrade meta from V1 to V3_4";
return nullptr;
} else if (version == nebula::meta::MetaVersion::V2) {
auto ret = nebula::meta::MetaVersionMan::updateMetaV2ToV3_4(engine);
if (!ret.ok()) {
LOG(ERROR) << "Update meta from V2 to V3_4 failed " << ret;
return nullptr;
}
} else if (version == nebula::meta::MetaVersion::V3) {
if (!nebula::ok(version)) {
auto ret = nebula::meta::MetaVersionMan::updateMetaV3ToV3_4(engine);
if (!ret.ok()) {
LOG(ERROR) << "Update meta from V3 to V3_4 failed " << ret;
return nullptr;
}
nebula::meta::MetaVersionMan::setMetaVersionToKV(engine, nebula::meta::MetaVersion::V3_4);
} else {
auto v = nebula::value(version);
LOG(INFO) << "Get meta version is " << static_cast<int32_t>(v);
if (v == nebula::meta::MetaVersion::UNKNOWN) {
LOG(ERROR) << "Meta version is invalid";
return nullptr;
} else if (v == nebula::meta::MetaVersion::V1) {
LOG(ERROR) << "Can't upgrade meta from V1 to V3_4";
return nullptr;
} else if (v == nebula::meta::MetaVersion::V2) {
auto ret = nebula::meta::MetaVersionMan::updateMetaV2ToV3_4(engine);
if (!ret.ok()) {
LOG(ERROR) << "Update meta from V2 to V3_4 failed " << ret;
return nullptr;
}
}
}

nebula::meta::MetaVersionMan::setMetaVersionToKV(engine, nebula::meta::MetaVersion::V3_4);
LOG(INFO) << "Nebula store init succeeded, clusterId " << gClusterId;
return kvstore;
}
Expand Down
21 changes: 3 additions & 18 deletions src/meta/MetaVersionMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,19 @@ namespace meta {
static const std::string kMetaVersionKey = "__meta_version__"; // NOLINT

// static
MetaVersion MetaVersionMan::getMetaVersionFromKV(kvstore::KVStore* kv) {
ErrorOr<nebula::cpp2::ErrorCode, MetaVersion> MetaVersionMan::getMetaVersionFromKV(
kvstore::KVStore* kv) {
CHECK_NOTNULL(kv);
std::string value;
auto code = kv->get(kDefaultSpaceId, kDefaultPartId, kMetaVersionKey, &value, true);
if (code == nebula::cpp2::ErrorCode::SUCCEEDED) {
auto version = *reinterpret_cast<const MetaVersion*>(value.data());
return version;
} else {
return getVersionByHost(kv);
return code;
}
}

// static
MetaVersion MetaVersionMan::getVersionByHost(kvstore::KVStore* kv) {
const auto& hostPrefix = nebula::MetaKeyUtils::hostPrefix();
std::unique_ptr<nebula::kvstore::KVIterator> iter;
auto code = kv->prefix(kDefaultSpaceId, kDefaultPartId, hostPrefix, &iter, true);
if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
return MetaVersion::UNKNOWN;
}
if (iter->valid()) {
auto v1KeySize = hostPrefix.size() + sizeof(int64_t);
return (iter->key().size() == v1KeySize) ? MetaVersion::V1 : MetaVersion::V3_4;
}
// No hosts exists, regard as version 3
return MetaVersion::V3_4;
}

// static
bool MetaVersionMan::setMetaVersionToKV(kvstore::KVEngine* engine, MetaVersion version) {
CHECK_NOTNULL(engine);
Expand Down
5 changes: 2 additions & 3 deletions src/meta/MetaVersionMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define META_METAVERSIONMAN_H_

#include "common/base/Base.h"
#include "common/base/ErrorOr.h"
#include "common/utils/MetaKeyUtils.h"
#include "kvstore/KVEngine.h"
#include "kvstore/KVStore.h"
Expand Down Expand Up @@ -35,7 +36,7 @@ class MetaVersionMan final {
* @param kv
* @return
*/
static MetaVersion getMetaVersionFromKV(kvstore::KVStore* kv);
static ErrorOr<nebula::cpp2::ErrorCode, MetaVersion> getMetaVersionFromKV(kvstore::KVStore* kv);

static bool setMetaVersionToKV(kvstore::KVEngine* engine, MetaVersion version);

Expand All @@ -44,8 +45,6 @@ class MetaVersionMan final {
static Status updateMetaV3ToV3_4(kvstore::KVEngine* engine);

private:
static MetaVersion getVersionByHost(kvstore::KVStore* kv);

static Status doUpgradeV3ToV3_4(kvstore::KVEngine* engine);

static Status doUpgradeV2ToV3(kvstore::KVEngine* engine);
Expand Down
7 changes: 6 additions & 1 deletion src/meta/processors/admin/HBProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ void HBProcessor::process(const cpp2::HBReq& req) {

auto version = metaVersion_.load();
if (version == -1) {
metaVersion_.store(static_cast<int64_t>(MetaVersionMan::getMetaVersionFromKV(kvstore_)));
auto v = MetaVersionMan::getMetaVersionFromKV(kvstore_);
if (nebula::ok(v)) {
metaVersion_.store(static_cast<int64_t>(nebula::value(v)));
} else {
metaVersion_.store(static_cast<int64_t>(MetaVersion::V3_4));
}
}

resp_.meta_version_ref() = metaVersion_.load();
Expand Down
15 changes: 1 addition & 14 deletions src/tools/meta-dump/MetaDumpTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,7 @@ class MetaDumper {
}

if (!found) {
prefix = MetaKeyUtils::hostPrefix();
iter->Seek(rocksdb::Slice(prefix));
while (iter->Valid() && iter->key().starts_with(prefix)) {
found = true;
auto v1KeySize = prefix.size() + sizeof(int64_t);
auto version = (iter->key().size() == v1KeySize) ? MetaVersion::V1 : MetaVersion::V3_4;
LOG(INFO) << "Meta version=" << static_cast<int>(version);
iter->Next();
break;
}

if (!found) {
LOG(INFO) << "Meta version= Unknown";
}
LOG(INFO) << "Meta version= Unknown";
}
}
{
Expand Down

0 comments on commit 33828b7

Please sign in to comment.