From 4ab0bb5cd59f528d311b5bd859da3840f9f453f1 Mon Sep 17 00:00:00 2001 From: SuperYoko <90179377+SuperYoko@users.noreply.github.com> Date: Fri, 20 May 2022 04:17:19 +0000 Subject: [PATCH] Fix restart failed --- src/clients/meta/MetaClient.cpp | 14 +++++++++++++ src/clients/meta/MetaClient.h | 4 ++++ src/storage/StorageServer.cpp | 35 +++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/clients/meta/MetaClient.cpp b/src/clients/meta/MetaClient.cpp index 037fe3028b2..7c9d2159b0c 100644 --- a/src/clients/meta/MetaClient.cpp +++ b/src/clients/meta/MetaClient.cpp @@ -89,6 +89,20 @@ MetaClient::~MetaClient() { VLOG(3) << "~MetaClient"; } +#ifdef BUILD_STANDALONE +StatusOr MetaClient::checkLocalMachineRegistered() { + auto ret = heartbeat().get(); + if (!ret.ok()) { + if (ret.status().toString() == "Machine not existed!") { + return false; + } + LOG(ERROR) << "Check register failed: " << ret.status(); + return Status::Error("Check register failed!"); + } + return true; +} +#endif + bool MetaClient::isMetadReady() { // UNKNOWN is reserved for tools such as upgrader, in that case the ip/port is not set. We do // not send heartbeat to meta to avoid writing error host info (e.g. Host("", 0)) diff --git a/src/clients/meta/MetaClient.h b/src/clients/meta/MetaClient.h index 15c89620eee..6b7135ba3bf 100644 --- a/src/clients/meta/MetaClient.h +++ b/src/clients/meta/MetaClient.h @@ -239,6 +239,10 @@ class MetaClient : public BaseMetaClient { ~MetaClient() override; +#ifdef BUILD_STANDALONE + StatusOr checkLocalMachineRegistered(); +#endif + bool isMetadReady(); bool waitForMetadReady(int count = -1, int retryIntervalSecs = FLAGS_heartbeat_interval_secs); diff --git a/src/storage/StorageServer.cpp b/src/storage/StorageServer.cpp index 2f1401c0215..26197685e0b 100644 --- a/src/storage/StorageServer.cpp +++ b/src/storage/StorageServer.cpp @@ -173,20 +173,29 @@ bool StorageServer::start() { #ifdef BUILD_STANDALONE if (FLAGS_add_local_host) { - std::vector hosts = {{FLAGS_local_ip, FLAGS_storage_port}}; - folly::Baton<> baton; - bool isAdded = false; - metaClient_->addHosts(hosts).thenValue([&isAdded, &baton](StatusOr resp) { - if (!resp.ok() || !resp.value()) { - LOG(ERROR) << "Add hosts for standalone failed."; - } else { - LOG(INFO) << "Add hosts for standalone succeed."; - isAdded = true; + // meta allready ready when standalone. + auto ret = metaClient_->checkLocalMachineRegistered(); + if (ret.ok()) { + if (!ret.value()) { + std::vector hosts = {{FLAGS_local_ip, FLAGS_storage_port}}; + folly::Baton<> baton; + bool isAdded = false; + metaClient_->addHosts(hosts).thenValue([&isAdded, &baton](StatusOr resp) { + if (!resp.ok() || !resp.value()) { + LOG(ERROR) << "Add hosts for standalone failed."; + } else { + LOG(INFO) << "Add hosts for standalone succeed."; + isAdded = true; + } + baton.post(); + }); + + baton.wait(); + if (!isAdded) { + return false; + } } - baton.post(); - }); - baton.wait(); - if (!isAdded) { + } else { return false; } }