diff --git a/src/interface/meta.thrift b/src/interface/meta.thrift index 69fcc30f0a0..cc419883dc7 100644 --- a/src/interface/meta.thrift +++ b/src/interface/meta.thrift @@ -89,9 +89,15 @@ struct EdgeItem { 4: common.Schema schema, } +enum HostStatus { + ONLINE = 0x00, + OFFLINE = 0x01, + UNKNOWN = 0x02, +} (cpp.enum_strict) + struct HostItem { 1: common.HostAddr hostAddr, - 2: string status, + 2: HostStatus status, } struct ExecResp { diff --git a/src/meta/client/MetaClient.cpp b/src/meta/client/MetaClient.cpp index 3081d549c27..0a496612d92 100644 --- a/src/meta/client/MetaClient.cpp +++ b/src/meta/client/MetaClient.cpp @@ -256,7 +256,14 @@ std::vector MetaClient::toHostStatus(const std::vector hosts; hosts.resize(tHosts.size()); std::transform(tHosts.begin(), tHosts.end(), hosts.begin(), [](const auto& h) { - return HostStatus(HostAddr(h.hostAddr.get_ip(), h.hostAddr.get_port()), h.get_status()); + switch (h.get_status()) { + case cpp2::HostStatus::ONLINE: + return HostStatus(HostAddr(h.hostAddr.get_ip(), h.hostAddr.get_port()), "online"); + case cpp2::HostStatus::OFFLINE: + return HostStatus(HostAddr(h.hostAddr.get_ip(), h.hostAddr.get_port()), "offline"); + default: + return HostStatus(HostAddr(h.hostAddr.get_ip(), h.hostAddr.get_port()), "unknown"); + } }); return hosts; } diff --git a/src/meta/processors/BaseProcessor.h b/src/meta/processors/BaseProcessor.h index cbc02a7823e..e54383ac21e 100644 --- a/src/meta/processors/BaseProcessor.h +++ b/src/meta/processors/BaseProcessor.h @@ -170,11 +170,6 @@ class BaseProcessor { * */ StatusOr> allHosts(); - /** - * Get all hosts with online/offline status. - * */ - StatusOr> allHostsWithStatus(); - /** * Get one auto-increment Id. * */ diff --git a/src/meta/processors/BaseProcessor.inl b/src/meta/processors/BaseProcessor.inl index 8f587699658..3e52da8987e 100644 --- a/src/meta/processors/BaseProcessor.inl +++ b/src/meta/processors/BaseProcessor.inl @@ -129,54 +129,6 @@ StatusOr> BaseProcessor::allHosts() { return hosts; } -template -StatusOr> BaseProcessor::allHostsWithStatus() { - std::vector hostItems; - auto hosts = ActiveHostsManHolder::hostsMan()->getActiveHosts(); - std::vector activeHosts; - activeHosts.resize(hosts.size()); - std::transform(hosts.begin(), hosts.end(), activeHosts.begin(), [](const auto& h) { - nebula::cpp2::HostAddr th; - th.set_ip(h.first); - th.set_port(h.second); - return th; - }); - - if (!FLAGS_hosts_whitelist_enabled) { - std::for_each(activeHosts.begin(), activeHosts.end(), [&](auto& h) { - cpp2::HostItem item; - item.set_hostAddr(h); - item.set_status("online"); - hostItems.emplace_back(item); - }); - return hostItems; - } - - const auto& prefix = MetaServiceUtils::hostPrefix(); - std::unique_ptr iter; - auto ret = kvstore_->prefix(kDefaultSpaceId_, kDefaultPartId_, prefix, &iter); - if (ret != kvstore::ResultCode::SUCCEEDED) { - return Status::Error("Can't find any hosts"); - } - - while (iter->valid()) { - cpp2::HostItem item; - nebula::cpp2::HostAddr host; - auto hostAddrPiece = iter->key().subpiece(prefix.size()); - memcpy(&host, hostAddrPiece.data(), hostAddrPiece.size()); - item.set_hostAddr(host); - if (std::find(activeHosts.begin(), activeHosts.end(), host) != activeHosts.end()) { - item.set_status("online"); - } else { - item.set_status("offline"); - } - hostItems.emplace_back(item); - iter->next(); - } - - return hostItems; -} - template int32_t BaseProcessor::autoIncrementId() { folly::SharedMutex::WriteHolder holder(LockUtils::idLock()); diff --git a/src/meta/processors/ListHostsProcessor.cpp b/src/meta/processors/ListHostsProcessor.cpp index b1bc1a2e12e..eb734511008 100644 --- a/src/meta/processors/ListHostsProcessor.cpp +++ b/src/meta/processors/ListHostsProcessor.cpp @@ -23,6 +23,54 @@ void ListHostsProcessor::process(const cpp2::ListHostsReq& req) { onFinished(); } +StatusOr> ListHostsProcessor::allHostsWithStatus() { + std::vector hostItems; + auto hosts = ActiveHostsManHolder::hostsMan()->getActiveHosts(); + std::vector activeHosts; + activeHosts.resize(hosts.size()); + std::transform(hosts.begin(), hosts.end(), activeHosts.begin(), [](const auto& h) { + nebula::cpp2::HostAddr th; + th.set_ip(h.first); + th.set_port(h.second); + return th; + }); + + if (!FLAGS_hosts_whitelist_enabled) { + std::for_each(activeHosts.begin(), activeHosts.end(), [&](auto& h) { + cpp2::HostItem item; + item.set_hostAddr(h); + item.set_status(cpp2::HostStatus::ONLINE); + hostItems.emplace_back(item); + }); + return hostItems; + } + + const auto& prefix = MetaServiceUtils::hostPrefix(); + std::unique_ptr iter; + auto ret = kvstore_->prefix(kDefaultSpaceId_, kDefaultPartId_, prefix, &iter); + if (ret != kvstore::ResultCode::SUCCEEDED) { + return Status::Error("Can't find any hosts"); + } + + while (iter->valid()) { + cpp2::HostItem item; + nebula::cpp2::HostAddr host; + auto hostAddrPiece = iter->key().subpiece(prefix.size()); + memcpy(&host, hostAddrPiece.data(), hostAddrPiece.size()); + item.set_hostAddr(host); + if (std::find(activeHosts.begin(), activeHosts.end(), host) != activeHosts.end()) { + item.set_status(cpp2::HostStatus::ONLINE); + } else { + item.set_status(cpp2::HostStatus::OFFLINE); + } + hostItems.emplace_back(item); + iter->next(); + } + + return hostItems; +} + + } // namespace meta } // namespace nebula diff --git a/src/meta/processors/ListHostsProcessor.h b/src/meta/processors/ListHostsProcessor.h index 87ebe2376db..0ed6bf9ecbb 100644 --- a/src/meta/processors/ListHostsProcessor.h +++ b/src/meta/processors/ListHostsProcessor.h @@ -23,6 +23,11 @@ class ListHostsProcessor : public BaseProcessor { private: explicit ListHostsProcessor(kvstore::KVStore* kvstore) : BaseProcessor(kvstore) {} + + /** + * Get all hosts with online/offline status. + * */ + StatusOr> allHostsWithStatus(); }; } // namespace meta diff --git a/src/meta/test/ProcessorTest.cpp b/src/meta/test/ProcessorTest.cpp index e53f36705bd..126e0bb65ea 100644 --- a/src/meta/test/ProcessorTest.cpp +++ b/src/meta/test/ProcessorTest.cpp @@ -149,7 +149,7 @@ TEST(ProcessorTest, ListHostsTest) { for (auto i = 0; i < 10; i++) { ASSERT_EQ(i, resp.hosts[i].hostAddr.ip); ASSERT_EQ(i, resp.hosts[i].hostAddr.port); - ASSERT_EQ("online", resp.hosts[i].status); + ASSERT_EQ(cpp2::HostStatus::ONLINE, resp.hosts[i].status); } } { @@ -163,7 +163,7 @@ TEST(ProcessorTest, ListHostsTest) { for (auto i = 0; i < 10; i++) { ASSERT_EQ(i, resp.hosts[i].hostAddr.ip); ASSERT_EQ(i, resp.hosts[i].hostAddr.port); - ASSERT_EQ("offline", resp.hosts[i].status); + ASSERT_EQ(cpp2::HostStatus::OFFLINE, resp.hosts[i].status); } } }