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 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
23 changes: 16 additions & 7 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 @@ -93,9 +98,13 @@ void ActiveHostsMan::loadHostMap() {
auto host = MetaServiceUtils::parseHostKey(iter->key());
HostInfo info;
info.lastHBTimeInSec_ = time::TimeUtils::nowInSeconds();
if (iter->val() == MetaServiceUtils::hostValOnline()) {
LOG(INFO) << "load host " << host.ip << ":" << host.port;
updateHostInfo({host.ip, host.port}, info);
if (iter->val() == MetaServiceUtils::hostValKVStoreOnline()) {
folly::RWSpinLock::ReadHolder rh(&lock_);
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 All @@ -112,7 +121,7 @@ void ActiveHostsMan::cleanExpiredHosts() {
LOG(INFO) << it->first << " expired! last hb time "
<< it->second.lastHBTimeInSec_;
data.emplace_back(MetaServiceUtils::hostKey(it->first.first, it->first.second),
MetaServiceUtils::hostValOffline());
MetaServiceUtils::hostValKVStoreOnline());
it = hostsMap_.erase(it);
} else {
it++;
Expand All @@ -122,7 +131,7 @@ void ActiveHostsMan::cleanExpiredHosts() {

if (!data.empty() && kvstore_ != nullptr) {
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock());
LOG(INFO) << "set " << data.size() << " expired hosts to offline in meta rocksdb";
LOG(INFO) << "set " << data.size() << " expired hosts to kvstoreOnline in meta rocksdb";
kvstore_->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data),
[] (kvstore::ResultCode code) {
CHECK_EQ(code, kvstore::ResultCode::SUCCEEDED);
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(ActiveHostsManTest, NormalTest2);
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
5 changes: 5 additions & 0 deletions src/meta/MetaServiceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const std::string kUsersTable = "__users__"; // NOLINT
const std::string kRolesTable = "__roles__"; // NOLINT

const std::string kHostOnline = "Online"; // NOLINT
const std::string kHostKVStoreOnline = "KVStoreOnline"; // NOLINT
const std::string kHostOffline = "Offline"; // NOLINT

std::string MetaServiceUtils::spaceKey(GraphSpaceID spaceId) {
Expand Down Expand Up @@ -112,6 +113,10 @@ std::string MetaServiceUtils::hostValOnline() {
return kHostOnline;
}

std::string MetaServiceUtils::hostValKVStoreOnline() {
return kHostKVStoreOnline;
}

std::string MetaServiceUtils::hostValOffline() {
return kHostOffline;
}
Expand Down
2 changes: 2 additions & 0 deletions src/meta/MetaServiceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class MetaServiceUtils final {

static std::string hostValOnline();

static std::string hostValKVStoreOnline();

static std::string hostValOffline();

static const std::string& hostPrefix();
Expand Down
26 changes: 26 additions & 0 deletions src/meta/test/ActiveHostsManTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ TEST(ActiveHostsManTest, NormalTest) {
}
}

TEST(ActiveHostsManTest, NormalTest2) {
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());
usleep(2200);
ASSERT_EQ(3, ahMan.getActiveHosts().size());
{
std::vector<kvstore::KV> data;
for (auto i = 0; i < 3; i++) {
data.emplace_back(MetaServiceUtils::hostKey(0, i),
MetaServiceUtils::hostValKVStoreOnline());
}
kv->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data),
[] (kvstore::ResultCode code) {
CHECK_EQ(code, kvstore::ResultCode::SUCCEEDED);
});
}
usleep(200);
ASSERT_EQ(3, ahMan.getActiveHosts().size());
}

} // namespace meta
} // namespace nebula

Expand Down