Skip to content

Commit

Permalink
Support more validation when create space on an empty zone (#3065)
Browse files Browse the repository at this point in the history
Co-authored-by: Yee <[email protected]>
Co-authored-by: Doodle <[email protected]>
  • Loading branch information
3 people authored and Sophie-Xie committed Oct 18, 2021
1 parent 3ede2df commit 3418ecb
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions src/meta/processors/parts/CreateSpaceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
std::string(reinterpret_cast<const char*>(&spaceId), sizeof(spaceId)));
data.emplace_back(MetaServiceUtils::spaceKey(spaceId), MetaServiceUtils::spaceVal(properties));

nebula::cpp2::ErrorCode code = nebula::cpp2::ErrorCode::SUCCEEDED;
if (properties.group_name_ref().has_value()) {
auto& groupName = *properties.group_name_ref();
LOG(INFO) << "Create Space on group: " << groupName;
Expand Down Expand Up @@ -154,14 +155,12 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
auto zoneKey = MetaServiceUtils::zoneKey(zone);
auto zoneValueRet = doGet(std::move(zoneKey));
if (!nebula::ok(zoneValueRet)) {
auto retCode = nebula::error(zoneValueRet);
if (retCode == nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND) {
retCode = nebula::cpp2::ErrorCode::E_ZONE_NOT_FOUND;
code = nebula::error(zoneValueRet);
if (code == nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND) {
code = nebula::cpp2::ErrorCode::E_ZONE_NOT_FOUND;
}
LOG(ERROR) << "Get zone " << zone << " failed.";
handleErrorCode(retCode);
onFinished();
return;
break;
}

auto hosts = MetaServiceUtils::parseZoneHosts(std::move(nebula::value(zoneValueRet)));
Expand All @@ -177,30 +176,34 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
zoneHosts[zone] = std::move(hosts);
}

if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "Create space failed";
handleErrorCode(code);
onFinished();
return;
}

for (auto partId = 1; partId <= partitionNum; partId++) {
auto pickedZonesRet = pickLightLoadZones(replicaFactor);
if (!pickedZonesRet.ok()) {
LOG(ERROR) << "Pick zone failed.";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
code = nebula::cpp2::ErrorCode::E_INVALID_PARM;
break;
}

auto pickedZones = std::move(pickedZonesRet).value();
auto partHostsRet = pickHostsWithZone(pickedZones, zoneHosts);
if (!partHostsRet.ok()) {
LOG(ERROR) << "Pick hosts with zone failed.";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
code = nebula::cpp2::ErrorCode::E_INVALID_PARM;
break;
}

auto partHosts = std::move(partHostsRet).value();
if (partHosts.empty()) {
LOG(ERROR) << "Pick hosts is empty.";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
code = nebula::cpp2::ErrorCode::E_INVALID_PARM;
break;
}

std::stringstream ss;
Expand Down Expand Up @@ -245,6 +248,13 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
}
}

if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "Create space failed";
handleErrorCode(code);
onFinished();
return;
}

resp_.set_id(to(spaceId, EntryType::SPACE));
doSyncPutAndUpdate(std::move(data));
LOG(INFO) << "Create space " << spaceName;
Expand Down Expand Up @@ -289,6 +299,16 @@ StatusOr<Hosts> CreateSpaceProcessor::pickHostsWithZone(
Hosts pickedHosts;
nebula::cpp2::ErrorCode code = nebula::cpp2::ErrorCode::SUCCEEDED;
for (auto iter = zoneHosts.begin(); iter != zoneHosts.end(); iter++) {
if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
break;
}

if (iter->second.empty()) {
LOG(ERROR) << "Zone " << iter->first << " is empty";
code = nebula::cpp2::ErrorCode::E_INVALID_PARM;
break;
}

auto zoneIter = std::find(std::begin(zones), std::end(zones), iter->first);
if (zoneIter == std::end(zones)) {
continue;
Expand All @@ -315,8 +335,6 @@ StatusOr<Hosts> CreateSpaceProcessor::pickHostsWithZone(
}

if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
handleErrorCode(code);
onFinished();
return Status::Error("Host not found");
}
return pickedHosts;
Expand Down

0 comments on commit 3418ecb

Please sign in to comment.