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

merge host info from kvstore #550

Merged
merged 5 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions src/meta/ActiveHostsMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ void ActiveHostsMan::cleanExpiredHosts() {
}
}

// merge host info from kvstore
if (kvstore_ != nullptr) {
const auto& prefix = MetaServiceUtils::hostPrefix();
std::unique_ptr<kvstore::KVIterator> iter;
auto ret = kvstore_->prefix(kDefaultSpaceId, kDefaultPartId, prefix, &iter);
if (ret == kvstore::ResultCode::SUCCEEDED) {
while (iter->valid()) {
auto host = MetaServiceUtils::parseHostKey(iter->key());
if (iter->val() == MetaServiceUtils::hostValOnline()) {
folly::RWSpinLock::ReadHolder rh(&lock_);
bool notFound = hostsMap_.find({host.ip, host.port}) == hostsMap_.end();
if (notFound) {
data.emplace_back(MetaServiceUtils::hostKey(host.ip, host.port),
MetaServiceUtils::hostValOffline());
}
}
iter->next();
}
}
}

if (!data.empty() && kvstore_ != nullptr) {
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock());
LOG(INFO) << "set " << data.size() << " expired hosts to offline in meta rocksdb";
Expand Down
1 change: 1 addition & 0 deletions src/meta/ActiveHostsMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct HostInfo {

class ActiveHostsMan final {
FRIEND_TEST(ActiveHostsManTest, NormalTest);
FRIEND_TEST(ActiveHostsManTest, MergeHostInfo);
FRIEND_TEST(ProcessorTest, ListHostsTest);

public:
Expand Down
50 changes: 50 additions & 0 deletions src/meta/test/ActiveHostsManTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,56 @@ TEST(ActiveHostsManTest, NormalTest) {
}
}

TEST(ActiveHostsManTest, MergeHostInfo) {
fs::TempDir rootPath("/tmp/ActiveHostsMergeTest.XXXXXX");
std::unique_ptr<kvstore::KVStore> kv(TestUtils::initKV(rootPath.path()));
{
std::vector<kvstore::KV> data;
for (auto i = 0; i < 3; i++) {
data.emplace_back(MetaServiceUtils::hostKey(0, i),
MetaServiceUtils::hostValOnline());
}
kv->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data),
[] (kvstore::ResultCode code) {
CHECK_EQ(code, kvstore::ResultCode::SUCCEEDED);
});
}

int onlineNum = 0;
int offlineNum = 0;
const auto& prefix = MetaServiceUtils::hostPrefix();
std::unique_ptr<kvstore::KVIterator> iter;
kv->prefix(kDefaultSpaceId, kDefaultPartId, prefix, &iter);
while (iter->valid()) {
if (iter->val() == MetaServiceUtils::hostValOnline()) {
++onlineNum;
} else {
++offlineNum;
}
iter->next();
}
ASSERT_EQ(3, onlineNum);
ASSERT_EQ(0, offlineNum);

ActiveHostsMan ahMan(1, 1, kv.get());
sleep(ahMan.intervalSeconds_ + 1);
onlineNum = 0;
offlineNum = 0;
kv->prefix(kDefaultSpaceId, kDefaultPartId, prefix, &iter);
while (iter->valid()) {
auto host = MetaServiceUtils::parseHostKey(iter->key());
LOG(INFO) << "ip: " << host.ip << ", port: " << host.port << ", status: " << iter->val();
if (iter->val() == MetaServiceUtils::hostValOnline()) {
++onlineNum;
} else {
++offlineNum;
}
iter->next();
}
ASSERT_EQ(0, onlineNum);
ASSERT_EQ(3, offlineNum);
}

} // namespace meta
} // namespace nebula

Expand Down