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

fix meta http replace host handler #3794

Merged
Merged
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
83 changes: 70 additions & 13 deletions src/meta/http/MetaHttpReplaceHostHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@ void MetaHttpReplaceHostHandler::onRequest(std::unique_ptr<HTTPMessage> headers)

if (!headers->hasQueryParam("from")) {
err_ = HttpCode::E_ILLEGAL_ARGUMENT;
errMsg_ = "miss argument [from]";
errMsg_ = "Miss argument [from]";
return;
}

if (!headers->hasQueryParam("to")) {
err_ = HttpCode::E_ILLEGAL_ARGUMENT;
errMsg_ = "miss argument [to]";
errMsg_ = "Miss argument [to]";
return;
}

ipv4From_ = headers->getQueryParam("from");

ipv4To_ = headers->getQueryParam("to");

LOG(INFO) << folly::sformat("change host info from {} to {}", ipv4From_, ipv4To_);
LOG(INFO) << folly::sformat("Change host info from {} to {}", ipv4From_, ipv4To_);
}

void MetaHttpReplaceHostHandler::onBody(std::unique_ptr<folly::IOBuf>) noexcept {
Expand All @@ -76,15 +75,31 @@ void MetaHttpReplaceHostHandler::onEOM() noexcept {
break;
}

if (replaceHost(ipv4From_, ipv4To_)) {
LOG(INFO) << "Replace Host successfully";
if (replaceHostInPart(ipv4From_, ipv4To_)) {
LOG(INFO) << "Replace Host in partition successfully";
ResponseBuilder(downstream_)
.status(WebServiceUtils::to(HttpStatusCode::OK),
WebServiceUtils::toString(HttpStatusCode::OK))
.body("Replace Host successfully")
.body("Replace Host in partition successfully")
.sendWithEOM();
} else {
LOG(INFO) << "Replace Host failed";
LOG(INFO) << "Replace Host in partition failed";
ResponseBuilder(downstream_)
.status(WebServiceUtils::to(HttpStatusCode::FORBIDDEN),
WebServiceUtils::toString(HttpStatusCode::FORBIDDEN))
.body(errMsg_)
.sendWithEOM();
}

if (replaceHostInZone(ipv4From_, ipv4To_)) {
LOG(INFO) << "Replace Host in zone successfully";
ResponseBuilder(downstream_)
.status(WebServiceUtils::to(HttpStatusCode::OK),
WebServiceUtils::toString(HttpStatusCode::OK))
.body("Replace Host in zone successfully")
.sendWithEOM();
} else {
LOG(INFO) << "Replace Host in zone failed";
ResponseBuilder(downstream_)
.status(WebServiceUtils::to(HttpStatusCode::FORBIDDEN),
WebServiceUtils::toString(HttpStatusCode::FORBIDDEN))
Expand All @@ -106,13 +121,13 @@ void MetaHttpReplaceHostHandler::onError(ProxygenError error) noexcept {
<< proxygen::getErrorString(error);
}

bool MetaHttpReplaceHostHandler::replaceHost(std::string ipv4From, std::string ipv4To) {
bool MetaHttpReplaceHostHandler::replaceHostInPart(std::string ipv4From, std::string ipv4To) {
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock());
const auto& spacePrefix = MetaKeyUtils::spacePrefix();
std::unique_ptr<kvstore::KVIterator> iter;
auto kvRet = kvstore_->prefix(kDefaultSpaceId, kDefaultPartId, spacePrefix, &iter);
if (kvRet != nebula::cpp2::ErrorCode::SUCCEEDED) {
errMsg_ = folly::stringPrintf("can't get space prefix=%s", spacePrefix.c_str());
errMsg_ = folly::stringPrintf("Can't get space prefix=%s", spacePrefix.c_str());
LOG(INFO) << errMsg_;
return false;
}
Expand All @@ -123,14 +138,14 @@ bool MetaHttpReplaceHostHandler::replaceHost(std::string ipv4From, std::string i
allSpaceId.emplace_back(spaceId);
iter->next();
}
LOG(INFO) << "allSpaceId.size()=" << allSpaceId.size();
LOG(INFO) << "AllSpaceId.size()=" << allSpaceId.size();

std::vector<nebula::kvstore::KV> data;
for (const auto& spaceId : allSpaceId) {
const auto& partPrefix = MetaKeyUtils::partPrefix(spaceId);
kvRet = kvstore_->prefix(kDefaultSpaceId, kDefaultPartId, partPrefix, &iter);
if (kvRet != nebula::cpp2::ErrorCode::SUCCEEDED) {
errMsg_ = folly::stringPrintf("can't get partPrefix=%s", partPrefix.c_str());
errMsg_ = folly::stringPrintf("Can't get partPrefix=%s", partPrefix.c_str());
LOG(INFO) << errMsg_;
return false;
}
Expand All @@ -156,12 +171,54 @@ bool MetaHttpReplaceHostHandler::replaceHost(std::string ipv4From, std::string i
kvstore_->asyncMultiPut(
kDefaultSpaceId, kDefaultPartId, std::move(data), [&](nebula::cpp2::ErrorCode code) {
updateSucceed = (code == nebula::cpp2::ErrorCode::SUCCEEDED);
errMsg_ = folly::stringPrintf("write to kvstore failed, %s , %d", __func__, __LINE__);
errMsg_ = folly::stringPrintf("Write to kvstore failed, %s , %d", __func__, __LINE__);
baton.post();
});
baton.wait();
return updateSucceed;
}

bool MetaHttpReplaceHostHandler::replaceHostInZone(std::string ipv4From, std::string ipv4To) {
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock());
const auto& zonePrefix = MetaKeyUtils::zonePrefix();
std::unique_ptr<kvstore::KVIterator> iter;
auto kvRet = kvstore_->prefix(kDefaultSpaceId, kDefaultPartId, zonePrefix, &iter);
if (kvRet != nebula::cpp2::ErrorCode::SUCCEEDED) {
errMsg_ = folly::stringPrintf("Can't get zone prefix=%s", zonePrefix.c_str());
LOG(INFO) << errMsg_;
return false;
}

std::vector<nebula::kvstore::KV> data;
while (iter->valid()) {
bool needUpdate = false;
auto zoneName = MetaKeyUtils::parseZoneName(iter->key());
auto hosts = MetaKeyUtils::parseZoneHosts(iter->val());
for (auto& host : hosts) {
if (host.host == ipv4From) {
host.host = ipv4To;
needUpdate = true;
SuperYoko marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (needUpdate) {
data.emplace_back(iter->key(), MetaKeyUtils::zoneVal(hosts));
}
iter->next();
}

bool updateSucceed{false};
folly::Baton<true, std::atomic> baton;
kvstore_->asyncMultiPut(
kDefaultSpaceId, kDefaultPartId, std::move(data), [&](nebula::cpp2::ErrorCode code) {
updateSucceed = (code == nebula::cpp2::ErrorCode::SUCCEEDED);
errMsg_ = folly::stringPrintf("Write to kvstore failed, %s , %d", __func__, __LINE__);
baton.post();
});
baton.wait();

return updateSucceed;
}

} // namespace meta
} // namespace nebula
11 changes: 6 additions & 5 deletions src/meta/http/MetaHttpReplaceHostHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ using nebula::HttpCode;
/**
* @brief It will replace host info in meta partition table from
* backup host to current cluster host.
* It should be called after ingesting meta sst files when
* restore cluster.
* It will replace given host in zone table and partition table. Notice that,
* it only replace the host without port.
* Functions such as onRequest, onBody... and requestComplete are inherited
* from RequestHandler, we will check request parameters in onRequest and
* call main logic in onEOM.
Expand All @@ -33,7 +33,7 @@ class MetaHttpReplaceHostHandler : public proxygen::RequestHandler {
public:
MetaHttpReplaceHostHandler() = default;

void init(nebula::kvstore::KVStore *kvstore);
void init(nebula::kvstore::KVStore* kvstore);

void onRequest(std::unique_ptr<proxygen::HTTPMessage> headers) noexcept override;

Expand All @@ -47,14 +47,15 @@ class MetaHttpReplaceHostHandler : public proxygen::RequestHandler {

void onError(proxygen::ProxygenError error) noexcept override;

bool replaceHost(std::string ipv4From, std::string ipv4To);
bool replaceHostInPart(std::string ipv4From, std::string ipv4To);
bool replaceHostInZone(std::string ipv4From, std::string ipv4To);

private:
HttpCode err_{HttpCode::SUCCEEDED};
std::string errMsg_;
std::string ipv4From_;
std::string ipv4To_;
nebula::kvstore::KVStore *kvstore_;
nebula::kvstore::KVStore* kvstore_;
};

} // namespace meta
Expand Down
1 change: 0 additions & 1 deletion src/meta/processors/admin/RestoreProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ nebula::cpp2::ErrorCode RestoreProcessor::replaceHostInZone(const HostAddr& ipv4
bool needUpdate = false;
auto zoneName = MetaKeyUtils::parseZoneName(iter->key());
auto hosts = MetaKeyUtils::parseZoneHosts(iter->val());
std::vector<HostAddr> DesHosts;
for (auto& host : hosts) {
if (host == ipv4From) {
needUpdate = true;
Expand Down