From ab243eb955ac8d345e108c2f145f8b5d4c9a6d75 Mon Sep 17 00:00:00 2001 From: andychow01 <8199095+andychow01@users.noreply.github.com> Date: Wed, 26 Jun 2019 19:37:06 +0800 Subject: [PATCH 1/4] merge host info from kvstore --- src/meta/ActiveHostsMan.cpp | 20 +++++++++++ src/meta/ActiveHostsMan.h | 1 + src/meta/test/ActiveHostsManTest.cpp | 50 ++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/meta/ActiveHostsMan.cpp b/src/meta/ActiveHostsMan.cpp index 7b772946cf3..77b41042b96 100644 --- a/src/meta/ActiveHostsMan.cpp +++ b/src/meta/ActiveHostsMan.cpp @@ -120,6 +120,26 @@ void ActiveHostsMan::cleanExpiredHosts() { } } + // merge host info from kvstore + if (kvstore_ != nullptr) { + const auto& prefix = MetaServiceUtils::hostPrefix(); + std::unique_ptr 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()) { + bool found = hostsMap_.find({host.ip, host.port}) == hostsMap_.end(); + if (found) { + 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"; diff --git a/src/meta/ActiveHostsMan.h b/src/meta/ActiveHostsMan.h index f9aae295ab3..71a5275e0cd 100644 --- a/src/meta/ActiveHostsMan.h +++ b/src/meta/ActiveHostsMan.h @@ -37,6 +37,7 @@ struct HostInfo { class ActiveHostsMan final { FRIEND_TEST(ActiveHostsManTest, NormalTest); + FRIEND_TEST(ActiveHostsManTest, NormalTest2); FRIEND_TEST(ProcessorTest, ListHostsTest); public: diff --git a/src/meta/test/ActiveHostsManTest.cpp b/src/meta/test/ActiveHostsManTest.cpp index 4970569b76d..a926d16ef0c 100644 --- a/src/meta/test/ActiveHostsManTest.cpp +++ b/src/meta/test/ActiveHostsManTest.cpp @@ -37,6 +37,56 @@ TEST(ActiveHostsManTest, NormalTest) { } } +TEST(ActiveHostsManTest, NormalTest2) { + fs::TempDir rootPath("/tmp/ActiveHostsMergeTest.XXXXXX"); + std::unique_ptr kv(TestUtils::initKV(rootPath.path())); + { + std::vector 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 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(3); + 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 From 9fbf00ba9208241adc1bd8390949133eae71f1bb Mon Sep 17 00:00:00 2001 From: andychow01 <8199095+andychow01@users.noreply.github.com> Date: Wed, 26 Jun 2019 19:48:41 +0800 Subject: [PATCH 2/4] add read lock --- src/meta/ActiveHostsMan.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/meta/ActiveHostsMan.cpp b/src/meta/ActiveHostsMan.cpp index 77b41042b96..d1e11f57204 100644 --- a/src/meta/ActiveHostsMan.cpp +++ b/src/meta/ActiveHostsMan.cpp @@ -129,6 +129,7 @@ void ActiveHostsMan::cleanExpiredHosts() { while (iter->valid()) { auto host = MetaServiceUtils::parseHostKey(iter->key()); if (iter->val() == MetaServiceUtils::hostValOnline()) { + folly::RWSpinLock::ReadHolder rh(&lock_); bool found = hostsMap_.find({host.ip, host.port}) == hostsMap_.end(); if (found) { data.emplace_back(MetaServiceUtils::hostKey(host.ip, host.port), From a10649b0e086fc1cd66f876734a45e2ceb7a4ca0 Mon Sep 17 00:00:00 2001 From: andychow01 <8199095+andychow01@users.noreply.github.com> Date: Wed, 26 Jun 2019 19:55:23 +0800 Subject: [PATCH 3/4] found to notFound --- src/meta/ActiveHostsMan.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/meta/ActiveHostsMan.cpp b/src/meta/ActiveHostsMan.cpp index d1e11f57204..0a63022c331 100644 --- a/src/meta/ActiveHostsMan.cpp +++ b/src/meta/ActiveHostsMan.cpp @@ -130,8 +130,8 @@ void ActiveHostsMan::cleanExpiredHosts() { auto host = MetaServiceUtils::parseHostKey(iter->key()); if (iter->val() == MetaServiceUtils::hostValOnline()) { folly::RWSpinLock::ReadHolder rh(&lock_); - bool found = hostsMap_.find({host.ip, host.port}) == hostsMap_.end(); - if (found) { + bool notFound = hostsMap_.find({host.ip, host.port}) == hostsMap_.end(); + if (notFound) { data.emplace_back(MetaServiceUtils::hostKey(host.ip, host.port), MetaServiceUtils::hostValOffline()); } From 23c004f325e176dc0fe30d81932b12d6b666ae64 Mon Sep 17 00:00:00 2001 From: andychow01 <8199095+andychow01@users.noreply.github.com> Date: Tue, 2 Jul 2019 10:12:14 +0800 Subject: [PATCH 4/4] align fix --- src/meta/ActiveHostsMan.cpp | 2 +- src/meta/ActiveHostsMan.h | 2 +- src/meta/test/ActiveHostsManTest.cpp | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/meta/ActiveHostsMan.cpp b/src/meta/ActiveHostsMan.cpp index 0a63022c331..1c5e09c2494 100644 --- a/src/meta/ActiveHostsMan.cpp +++ b/src/meta/ActiveHostsMan.cpp @@ -133,7 +133,7 @@ void ActiveHostsMan::cleanExpiredHosts() { bool notFound = hostsMap_.find({host.ip, host.port}) == hostsMap_.end(); if (notFound) { data.emplace_back(MetaServiceUtils::hostKey(host.ip, host.port), - MetaServiceUtils::hostValOffline()); + MetaServiceUtils::hostValOffline()); } } iter->next(); diff --git a/src/meta/ActiveHostsMan.h b/src/meta/ActiveHostsMan.h index 71a5275e0cd..04ad1dab5ae 100644 --- a/src/meta/ActiveHostsMan.h +++ b/src/meta/ActiveHostsMan.h @@ -37,7 +37,7 @@ struct HostInfo { class ActiveHostsMan final { FRIEND_TEST(ActiveHostsManTest, NormalTest); - FRIEND_TEST(ActiveHostsManTest, NormalTest2); + FRIEND_TEST(ActiveHostsManTest, MergeHostInfo); FRIEND_TEST(ProcessorTest, ListHostsTest); public: diff --git a/src/meta/test/ActiveHostsManTest.cpp b/src/meta/test/ActiveHostsManTest.cpp index a926d16ef0c..c2fc70441a7 100644 --- a/src/meta/test/ActiveHostsManTest.cpp +++ b/src/meta/test/ActiveHostsManTest.cpp @@ -37,14 +37,14 @@ TEST(ActiveHostsManTest, NormalTest) { } } -TEST(ActiveHostsManTest, NormalTest2) { +TEST(ActiveHostsManTest, MergeHostInfo) { fs::TempDir rootPath("/tmp/ActiveHostsMergeTest.XXXXXX"); std::unique_ptr kv(TestUtils::initKV(rootPath.path())); { std::vector data; for (auto i = 0; i < 3; i++) { data.emplace_back(MetaServiceUtils::hostKey(0, i), - MetaServiceUtils::hostValOnline()); + MetaServiceUtils::hostValOnline()); } kv->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data), [] (kvstore::ResultCode code) { @@ -69,7 +69,7 @@ TEST(ActiveHostsManTest, NormalTest2) { ASSERT_EQ(0, offlineNum); ActiveHostsMan ahMan(1, 1, kv.get()); - sleep(3); + sleep(ahMan.intervalSeconds_ + 1); onlineNum = 0; offlineNum = 0; kv->prefix(kDefaultSpaceId, kDefaultPartId, prefix, &iter);