Skip to content

Commit

Permalink
change HostStatus to enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
critical27 committed May 30, 2019
1 parent af72b43 commit 582f60c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 57 deletions.
8 changes: 7 additions & 1 deletion src/interface/meta.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/meta/client/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,14 @@ std::vector<HostStatus> MetaClient::toHostStatus(const std::vector<cpp2::HostIte
std::vector<HostStatus> 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;
}
Expand Down
5 changes: 0 additions & 5 deletions src/meta/processors/BaseProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ class BaseProcessor {
* */
StatusOr<std::vector<nebula::cpp2::HostAddr>> allHosts();

/**
* Get all hosts with online/offline status.
* */
StatusOr<std::vector<cpp2::HostItem>> allHostsWithStatus();

/**
* Get one auto-increment Id.
* */
Expand Down
48 changes: 0 additions & 48 deletions src/meta/processors/BaseProcessor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -129,54 +129,6 @@ StatusOr<std::vector<nebula::cpp2::HostAddr>> BaseProcessor<RESP>::allHosts() {
return hosts;
}

template<typename RESP>
StatusOr<std::vector<cpp2::HostItem>> BaseProcessor<RESP>::allHostsWithStatus() {
std::vector<cpp2::HostItem> hostItems;
auto hosts = ActiveHostsManHolder::hostsMan()->getActiveHosts();
std::vector<nebula::cpp2::HostAddr> 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<kvstore::KVIterator> 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<typename RESP>
int32_t BaseProcessor<RESP>::autoIncrementId() {
folly::SharedMutex::WriteHolder holder(LockUtils::idLock());
Expand Down
48 changes: 48 additions & 0 deletions src/meta/processors/ListHostsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,54 @@ void ListHostsProcessor::process(const cpp2::ListHostsReq& req) {
onFinished();
}

StatusOr<std::vector<cpp2::HostItem>> ListHostsProcessor::allHostsWithStatus() {
std::vector<cpp2::HostItem> hostItems;
auto hosts = ActiveHostsManHolder::hostsMan()->getActiveHosts();
std::vector<nebula::cpp2::HostAddr> 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<kvstore::KVIterator> 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

5 changes: 5 additions & 0 deletions src/meta/processors/ListHostsProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class ListHostsProcessor : public BaseProcessor<cpp2::ListHostsResp> {
private:
explicit ListHostsProcessor(kvstore::KVStore* kvstore)
: BaseProcessor<cpp2::ListHostsResp>(kvstore) {}

/**
* Get all hosts with online/offline status.
* */
StatusOr<std::vector<cpp2::HostItem>> allHostsWithStatus();
};

} // namespace meta
Expand Down
4 changes: 2 additions & 2 deletions src/meta/test/ProcessorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
{
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 582f60c

Please sign in to comment.