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 #544

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions src/meta/ActiveHostsMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ActiveHostsMan::ActiveHostsMan(int32_t intervalSeconds, int32_t expiredSeconds,
CHECK(checkThread_.start());
checkThread_.addTimerTask(intervalSeconds * 1000,
intervalSeconds * 1000,
&ActiveHostsMan::cleanExpiredHosts,
&ActiveHostsMan::handleHosts,
this);
}

Expand Down Expand Up @@ -77,7 +77,12 @@ std::vector<HostAddr> ActiveHostsMan::getActiveHosts() {
return hosts;
}

void ActiveHostsMan::loadHostMap() {
void ActiveHostsMan::handleHosts() {
cleanExpiredHosts();
mergeHosts();
}

void ActiveHostsMan::mergeHosts() {
if (kvstore_ == nullptr) {
return;
}
Expand All @@ -94,8 +99,11 @@ void ActiveHostsMan::loadHostMap() {
HostInfo info;
info.lastHBTimeInSec_ = time::TimeUtils::nowInSeconds();
if (iter->val() == MetaServiceUtils::hostValOnline()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to set status of hosts "which is online in kvstore but not in memory" to offline? Because the info in memory is more updated. After leader really receive heartbeat, it will set info to online.

LOG(INFO) << "load host " << host.ip << ":" << host.port;
updateHostInfo({host.ip, host.port}, info);
bool found = hostsMap_.find({host.ip, host.port}) != hostsMap_.end();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you need to add a lock here?

if (!found) {
LOG(INFO) << "add active host " << host.ip << ":" << host.port;
updateHostInfo({host.ip, host.port}, info);
}
}
iter->next();
}
Expand Down
6 changes: 3 additions & 3 deletions src/meta/ActiveHostsMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ struct HostInfo {

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

public:
static ActiveHostsMan* instance(kvstore::KVStore* kv = nullptr) {
static auto activeHostsMan = std::unique_ptr<ActiveHostsMan>(
new ActiveHostsMan(FLAGS_expired_hosts_check_interval_sec,
FLAGS_expired_threshold_sec, kv));
static std::once_flag initFlag;
std::call_once(initFlag, &ActiveHostsMan::loadHostMap, activeHostsMan.get());
return activeHostsMan.get();
}

Expand All @@ -70,7 +69,8 @@ class ActiveHostsMan final {
private:
ActiveHostsMan(int32_t intervalSeconds, int32_t expiredSeconds, kvstore::KVStore* kv = nullptr);

void loadHostMap();
void handleHosts();
void mergeHosts();

void stopClean() {
checkThread_.stop();
Expand Down
54 changes: 54 additions & 0 deletions src/meta/test/ActiveHostsMergeTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just put the ut in ActiveHostsManTest?

/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include "base/Base.h"
#include <gtest/gtest.h>
#include "meta/ActiveHostsMan.h"
#include "fs/TempDir.h"
#include "meta/test/TestUtils.h"

namespace nebula {
namespace meta {

TEST(ActiveHostsMergeTest, NormalTest) {
fs::TempDir rootPath("/tmp/ActiveHostsMergeTest.XXXXXX");
std::unique_ptr<kvstore::KVStore> kv(TestUtils::initKV(rootPath.path()));
auto now = time::TimeUtils::nowInSeconds();
ActiveHostsMan ahMan(1, 2, kv.get());
ahMan.updateHostInfo(HostAddr(0, 0), HostInfo(now));
ahMan.updateHostInfo(HostAddr(0, 1), HostInfo(now));
ahMan.updateHostInfo(HostAddr(0, 2), HostInfo(now));
ASSERT_EQ(3, ahMan.getActiveHosts().size());
sleep(4);
ASSERT_EQ(0, ahMan.getActiveHosts().size());
{
std::vector<kvstore::KV> data;
for (auto i = 0; i < 3; i++) {
data.emplace_back(MetaServiceUtils::hostKey(i, i),
MetaServiceUtils::hostValOnline());
}
kv->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data),
[] (kvstore::ResultCode code) {
CHECK_EQ(code, kvstore::ResultCode::SUCCEEDED);
});
}
sleep(2);
ASSERT_EQ(3, ahMan.getActiveHosts().size());
}

} // namespace meta
} // namespace nebula


int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
folly::init(&argc, &argv, true);
google::SetStderrLogging(google::INFO);
return RUN_ALL_TESTS();
}



30 changes: 30 additions & 0 deletions src/meta/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ nebula_link_libraries(
nebula_add_test(active_hosts_man_test)


add_executable(
active_hosts_merge_test
ActiveHostsMergeTest.cpp
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:thread_obj>
$<TARGET_OBJECTS:time_obj>
$<TARGET_OBJECTS:fs_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:meta_service_handler>
$<TARGET_OBJECTS:meta_thrift_obj>
$<TARGET_OBJECTS:common_thrift_obj>
$<TARGET_OBJECTS:kvstore_obj>
$<TARGET_OBJECTS:meta_client>
$<TARGET_OBJECTS:thrift_obj>
$<TARGET_OBJECTS:time_obj>
$<TARGET_OBJECTS:schema_obj>
$<TARGET_OBJECTS:raftex_obj>
$<TARGET_OBJECTS:raftex_thrift_obj>
$<TARGET_OBJECTS:wal_obj>
)
nebula_link_libraries(
active_hosts_merge_test
${ROCKSDB_LIBRARIES}
${THRIFT_LIBRARIES}
wangle
gtest
)
nebula_add_test(active_hosts_merge_test)


add_executable(
meta_http_test
MetaHttpHandlerTest.cpp
Expand Down