forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support displaying online hosts in "SHOW HOSTS" command and save host…
…s info in rocskdb (vesoft-inc#450) * Support displaying online hosts in "SHOW HOSTS" command [440] * remove ActivHostsManHolder, store host info in rocksdb * Use call_once to init ActiveHostsMan. Rebased on master.
- Loading branch information
1 parent
5ad2718
commit 6800274
Showing
35 changed files
with
394 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* Copyright (c) 2019 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 "meta/ActiveHostsMan.h" | ||
#include "meta/MetaServiceUtils.h" | ||
#include "meta/processors/BaseProcessor.h" | ||
|
||
namespace nebula { | ||
namespace meta { | ||
|
||
ActiveHostsMan::ActiveHostsMan(int32_t intervalSeconds, int32_t expiredSeconds, | ||
kvstore::KVStore* kv) | ||
: intervalSeconds_(intervalSeconds) | ||
, expirationInSeconds_(expiredSeconds) { | ||
if (kv != nullptr) { | ||
kvstore_ = kv; | ||
} | ||
|
||
CHECK_GT(intervalSeconds, 0) | ||
<< "intervalSeconds " << intervalSeconds << " should > 0!"; | ||
CHECK_GE(expiredSeconds, intervalSeconds) | ||
<< "expiredSeconds " << expiredSeconds | ||
<< " should >= intervalSeconds " << intervalSeconds; | ||
CHECK(checkThread_.start()); | ||
checkThread_.addTimerTask(intervalSeconds * 1000, | ||
intervalSeconds * 1000, | ||
&ActiveHostsMan::cleanExpiredHosts, | ||
this); | ||
} | ||
|
||
void ActiveHostsMan::updateHostInfo(const HostAddr& hostAddr, const HostInfo& info) { | ||
std::vector<kvstore::KV> data; | ||
{ | ||
folly::RWSpinLock::ReadHolder rh(&lock_); | ||
auto it = hostsMap_.find(hostAddr); | ||
if (it == hostsMap_.end()) { | ||
folly::RWSpinLock::UpgradedHolder uh(&lock_); | ||
hostsMap_.emplace(hostAddr, std::move(info)); | ||
data.emplace_back(MetaServiceUtils::hostKey(hostAddr.first, hostAddr.second), | ||
MetaServiceUtils::hostValOnline()); | ||
} else { | ||
it->second.lastHBTimeInSec_ = info.lastHBTimeInSec_; | ||
} | ||
} | ||
if (kvstore_ != nullptr && !data.empty()) { | ||
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock()); | ||
kvstore_->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data), | ||
[] (kvstore::ResultCode code) { | ||
CHECK_EQ(code, kvstore::ResultCode::SUCCEEDED); | ||
}); | ||
} | ||
} | ||
|
||
std::vector<HostAddr> ActiveHostsMan::getActiveHosts() { | ||
std::vector<HostAddr> hosts; | ||
folly::RWSpinLock::ReadHolder rh(&lock_); | ||
hosts.resize(hostsMap_.size()); | ||
std::transform(hostsMap_.begin(), hostsMap_.end(), hosts.begin(), | ||
[](const auto& entry) -> decltype(auto) { | ||
return entry.first; | ||
}); | ||
return hosts; | ||
} | ||
|
||
void ActiveHostsMan::loadHostMap() { | ||
if (kvstore_ == nullptr) { | ||
return; | ||
} | ||
|
||
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; | ||
} | ||
|
||
while (iter->valid()) { | ||
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); | ||
} | ||
iter->next(); | ||
} | ||
} | ||
|
||
void ActiveHostsMan::cleanExpiredHosts() { | ||
int64_t now = time::TimeUtils::nowInSeconds(); | ||
std::vector<kvstore::KV> data; | ||
{ | ||
folly::RWSpinLock::WriteHolder rh(&lock_); | ||
auto it = hostsMap_.begin(); | ||
while (it != hostsMap_.end()) { | ||
if ((now - it->second.lastHBTimeInSec_) > expirationInSeconds_) { | ||
LOG(INFO) << it->first << " expired! last hb time " | ||
<< it->second.lastHBTimeInSec_; | ||
data.emplace_back(MetaServiceUtils::hostKey(it->first.first, it->first.second), | ||
MetaServiceUtils::hostValOffline()); | ||
it = hostsMap_.erase(it); | ||
} else { | ||
it++; | ||
} | ||
} | ||
} | ||
|
||
if (!data.empty() && kvstore_ != nullptr) { | ||
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock()); | ||
LOG(INFO) << "set " << data.size() << " expired hosts to offline in meta rocksdb"; | ||
kvstore_->asyncMultiPut(kDefaultSpaceId, kDefaultPartId, std::move(data), | ||
[] (kvstore::ResultCode code) { | ||
CHECK_EQ(code, kvstore::ResultCode::SUCCEEDED); | ||
}); | ||
} | ||
} | ||
|
||
} // namespace meta | ||
} // namespace nebula |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.