Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add retry for god user check when leader changed #4643

Merged
merged 4 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions src/daemons/MetaDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <thrift/lib/cpp2/server/ThriftServer.h>

#include "MetaDaemonInit.h"
#include "clients/meta/MetaClient.h"
#include "common/base/Base.h"
#include "common/base/SignalHandler.h"
#include "common/fs/FileUtils.h"
Expand All @@ -25,7 +26,6 @@
#include "meta/KVBasedClusterIdMan.h"
#include "meta/MetaServiceHandler.h"
#include "meta/MetaVersionMan.h"
#include "meta/RootUserMan.h"
#include "meta/http/MetaHttpReplaceHostHandler.h"
#include "meta/processors/job/JobManager.h"
#include "meta/stats/MetaStats.h"
Expand Down Expand Up @@ -167,29 +167,10 @@ int main(int argc, char* argv[]) {
}
}

{
/**
* Only leader part needed.
*/
auto ret = gKVStore->partLeader(nebula::kDefaultSpaceId, nebula::kDefaultPartId);
if (!nebula::ok(ret)) {
LOG(ERROR) << "Part leader get failed";
return EXIT_FAILURE;
}
if (nebula::value(ret) == localhost) {
LOG(INFO) << "Check and init root user";
auto checkRet = nebula::meta::RootUserMan::isGodExists(gKVStore.get());
if (!nebula::ok(checkRet)) {
auto retCode = nebula::error(checkRet);
LOG(ERROR) << "Parser God Role error:" << apache::thrift::util::enumNameSafe(retCode);
return EXIT_FAILURE;
}
auto existGod = nebula::value(checkRet);
if (!existGod && !nebula::meta::RootUserMan::initRootUser(gKVStore.get())) {
LOG(ERROR) << "Init root user failed";
return EXIT_FAILURE;
}
}
auto godInit = initGodUser(gKVStore.get(), localhost);
if (godInit != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "Init god user failed";
return EXIT_FAILURE;
}

auto metaServer = std::make_unique<apache::thrift::ThriftServer>();
Expand Down
56 changes: 56 additions & 0 deletions src/daemons/MetaDaemonInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@

#include "common/base/Base.h"
#include "common/base/SignalHandler.h"
#include "common/datatypes/HostAddr.h"
#include "common/fs/FileUtils.h"
#include "common/hdfs/HdfsCommandHelper.h"
#include "common/hdfs/HdfsHelper.h"
#include "common/network/NetworkUtils.h"
#include "common/ssl/SSLConfig.h"
#include "common/thread/GenericThreadPool.h"
#include "common/utils/MetaKeyUtils.h"
#include "kvstore/KVStore.h"
#include "kvstore/NebulaStore.h"
#include "kvstore/PartManager.h"
#include "meta/ActiveHostsMan.h"
#include "meta/KVBasedClusterIdMan.h"
#include "meta/MetaServiceHandler.h"
#include "meta/MetaVersionMan.h"
#include "meta/RootUserMan.h"
#include "meta/http/MetaHttpReplaceHostHandler.h"
#include "meta/processors/job/JobManager.h"
#include "meta/stats/MetaStats.h"
Expand All @@ -46,6 +49,8 @@ DECLARE_string(meta_server_addrs); // use define from grap flags.
DEFINE_int32(ws_meta_http_port, 11000, "Port to listen on Meta with HTTP protocol");
#endif

DECLARE_uint32(raft_heartbeat_interval_secs);

using nebula::web::PathParams;

namespace nebula::meta {
Expand Down Expand Up @@ -158,6 +163,57 @@ std::unique_ptr<nebula::kvstore::KVStore> initKV(std::vector<nebula::HostAddr> p
return kvstore;
}

nebula::cpp2::ErrorCode initGodUser(nebula::kvstore::KVStore* kvstore,
const nebula::HostAddr& localhost) {
const int kMaxRetryTime = FLAGS_raft_heartbeat_interval_secs * 3;
constexpr int kRetryInterval = 1;
int retryTime = 0;
// Both leader & follower need to wait all reading ok.
// leader init need to retry writing if leader changed.
while (true) {
retryTime += kRetryInterval;
if (retryTime > kMaxRetryTime) {
LOG(ERROR) << "Retry too many times";
return nebula::cpp2::ErrorCode::E_RETRY_EXHAUSTED;
}
auto ret = kvstore->partLeader(nebula::kDefaultSpaceId, nebula::kDefaultPartId);
if (!nebula::ok(ret)) {
LOG(ERROR) << "Part leader get failed";
return nebula::error(ret);
}
LOG(INFO) << "Check root user"; // follower need to wait reading all ok, too.
auto checkRet = nebula::meta::RootUserMan::isGodExists(kvstore);
if (!nebula::ok(checkRet)) {
auto retCode = nebula::error(checkRet);
if (retCode == nebula::cpp2::ErrorCode::E_LEADER_CHANGED) {
LOG(INFO) << "Leader changed, retry";
sleep(kRetryInterval);
continue;
}
LOG(ERROR) << "Parser God Role error:" << apache::thrift::util::enumNameSafe(retCode);
return nebula::error(checkRet);
}
if (nebula::value(ret) == localhost) {
auto existGod = nebula::value(checkRet);
if (!existGod) {
auto initGod = nebula::meta::RootUserMan::initRootUser(kvstore);
if (initGod != nebula::cpp2::ErrorCode::SUCCEEDED) {
if (initGod != nebula::cpp2::ErrorCode::E_LEADER_CHANGED) {
LOG(ERROR) << "Init God Role error:" << apache::thrift::util::enumNameSafe(initGod);
return initGod;
} else {
LOG(INFO) << "Leader changed, retry";
sleep(kRetryInterval);
continue;
}
}
}
}
break;
}
return nebula::cpp2::ErrorCode::SUCCEEDED;
}

nebula::Status initWebService(nebula::WebService* svc, nebula::kvstore::KVStore* kvstore) {
LOG(INFO) << "Starting Meta HTTP Service";
auto& router = svc->router();
Expand Down
4 changes: 4 additions & 0 deletions src/daemons/MetaDaemonInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "common/base/Status.h"
#include "common/hdfs/HdfsCommandHelper.h"
#include "interface/gen-cpp2/common_types.h"
#include "kvstore/KVStore.h"
#include "webservice/WebService.h"

Expand All @@ -18,4 +19,7 @@ std::unique_ptr<nebula::kvstore::KVStore> initKV(std::vector<nebula::HostAddr> p
nebula::HostAddr localhost);

nebula::Status initWebService(nebula::WebService* svc, nebula::kvstore::KVStore* kvstore);

nebula::cpp2::ErrorCode initGodUser(nebula::kvstore::KVStore* kvstore,
const nebula::HostAddr& localhost);
#endif
27 changes: 4 additions & 23 deletions src/daemons/StandAloneDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,29 +223,10 @@ int main(int argc, char *argv[]) {
}
}

{
/**
* Only leader part needed.
*/
auto ret = gMetaKVStore->partLeader(nebula::kDefaultSpaceId, nebula::kDefaultPartId);
if (!nebula::ok(ret)) {
LOG(ERROR) << "Part leader get failed";
return;
}
if (nebula::value(ret) == metaLocalhost) {
LOG(INFO) << "Check and init root user";
auto checkRet = nebula::meta::RootUserMan::isGodExists(gMetaKVStore.get());
if (!nebula::ok(checkRet)) {
auto retCode = nebula::error(checkRet);
LOG(ERROR) << "Parser God Role error:" << apache::thrift::util::enumNameSafe(retCode);
return;
}
auto existGod = nebula::value(checkRet);
if (!existGod && !nebula::meta::RootUserMan::initRootUser(gMetaKVStore.get())) {
LOG(ERROR) << "Init root user failed";
return;
}
}
auto godInit = initGodUser(gMetaKVStore.get(), metaLocalhost);
if (godInit != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "Init god user failed";
return;
}

auto handler =
Expand Down
12 changes: 5 additions & 7 deletions src/meta/RootUserMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "common/base/Base.h"
#include "common/utils/MetaKeyUtils.h"
#include "interface/gen-cpp2/common_types.h"
#include "kvstore/KVStore.h"

namespace nebula {
Expand Down Expand Up @@ -53,7 +54,7 @@ class RootUserMan {
}
}

static bool initRootUser(kvstore::KVStore* kv) {
static nebula::cpp2::ErrorCode initRootUser(kvstore::KVStore* kv) {
LOG(INFO) << "Init root user";
auto encodedPwd = proxygen::md5Encode(folly::StringPiece("nebula"));
auto userKey = MetaKeyUtils::userKey("root");
Expand All @@ -64,18 +65,15 @@ class RootUserMan {
std::vector<kvstore::KV> data;
data.emplace_back(std::move(userKey), std::move(userVal));
data.emplace_back(std::move(roleKey), std::move(roleVal));
bool ret = true;
nebula::cpp2::ErrorCode ec;
folly::Baton<true, std::atomic> baton;
kv->asyncMultiPut(
kDefaultSpaceId, kDefaultPartId, std::move(data), [&](nebula::cpp2::ErrorCode code) {
if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(INFO) << "Put failed, error " << static_cast<int32_t>(code);
ret = false;
}
ec = code;
baton.post();
});
baton.wait();
return ret;
return ec;
}
};

Expand Down