Skip to content

Commit

Permalink
alter space add zone
Browse files Browse the repository at this point in the history
  • Loading branch information
liwenhui-soul committed Dec 15, 2021
1 parent 372cac1 commit 89f99bf
Show file tree
Hide file tree
Showing 23 changed files with 367 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,25 @@ folly::Future<StatusOr<std::vector<cpp2::HostItem>>> MetaClient::listHosts(cpp2:
return future;
}

folly::Future<StatusOr<bool>> MetaClient::alterSpace(const std::string& spaceName,
meta::cpp2::AlterSpaceOp op,
const std::vector<std::string>& paras) {
cpp2::AlterSpaceReq req;
req.set_op(op);
req.set_space_name(spaceName);
req.set_paras(paras);
folly::Promise<StatusOr<bool>> promise;
auto future = promise.getFuture();
getResponse(
std::move(req),
[](auto client, auto request) { return client->future_alterSpace(request); },
[](cpp2::ExecResp&& resp) -> bool {
return resp.get_code() == nebula::cpp2::ErrorCode::SUCCEEDED;
},
std::move(promise));
return future;
}

folly::Future<StatusOr<std::vector<cpp2::PartItem>>> MetaClient::listParts(
GraphSpaceID spaceId, std::vector<PartitionID> partIds) {
cpp2::ListPartsReq req;
Expand Down
4 changes: 4 additions & 0 deletions src/clients/meta/MetaClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ class MetaClient {
folly::Future<StatusOr<std::vector<cpp2::HostItem>>> listHosts(
cpp2::ListHostType type = cpp2::ListHostType::ALLOC);

folly::Future<StatusOr<bool>> alterSpace(const std::string& spaceName,
meta::cpp2::AlterSpaceOp op,
const std::vector<std::string>& paras);

folly::Future<StatusOr<std::vector<cpp2::PartItem>>> listParts(GraphSpaceID spaceId,
std::vector<PartitionID> partIds);

Expand Down
3 changes: 3 additions & 0 deletions src/graph/executor/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ Executor *Executor::makeExecutor(QueryContext *qctx, const PlanNode *node) {
case PlanNode::Kind::kAppendVertices: {
return pool->add(new AppendVerticesExecutor(node, qctx));
}
case PlanNode::Kind::kAlterSpace: {
return pool->add(new AlterSpaceExecutor(node, qctx));
}
case PlanNode::Kind::kUnknown: {
LOG(FATAL) << "Unknown plan node kind " << static_cast<int32_t>(node->kind());
break;
Expand Down
17 changes: 17 additions & 0 deletions src/graph/executor/admin/SpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,22 @@ folly::Future<Status> ShowCreateSpaceExecutor::execute() {
.build());
});
}

folly::Future<Status> AlterSpaceExecutor::execute() {
SCOPED_TIMER(&execTime_);
auto *asnode = asNode<AlterSpace>(node());
return qctx()
->getMetaClient()
->alterSpace(asnode->getSpaceName(), asnode->getAlterSpaceOp(), asnode->getParas())
.via(runner())
.thenValue([this](StatusOr<bool> &&resp) {
SCOPED_TIMER(&execTime_);
if (!resp.ok()) {
LOG(ERROR) << resp.status().toString();
return std::move(resp).status();
}
return Status::OK();
});
}
} // namespace graph
} // namespace nebula
8 changes: 8 additions & 0 deletions src/graph/executor/admin/SpaceExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class ShowCreateSpaceExecutor final : public Executor {

folly::Future<Status> execute() override;
};

class AlterSpaceExecutor final : public Executor {
public:
AlterSpaceExecutor(const PlanNode *node, QueryContext *qctx)
: Executor("AlterSpaceExecutor", node, qctx) {}

folly::Future<Status> execute() override;
};
} // namespace graph
} // namespace nebula

Expand Down
32 changes: 32 additions & 0 deletions src/graph/planner/plan/Admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,38 @@ class DropSpace final : public SingleDependencyNode {
bool ifExists_;
};

class AlterSpace final : public SingleDependencyNode {
public:
static AlterSpace* make(QueryContext* qctx,
PlanNode* input,
const std::string& spaceName,
meta::cpp2::AlterSpaceOp op,
const std::vector<std::string>& paras) {
return qctx->objPool()->add(new AlterSpace(qctx, input, spaceName, op, paras));
}
const std::string& getSpaceName() const { return spaceName_; }

meta::cpp2::AlterSpaceOp getAlterSpaceOp() const { return op_; }

const std::vector<std::string>& getParas() const { return paras_; }

private:
AlterSpace(QueryContext* qctx,
PlanNode* input,
const std::string& spaceName,
meta::cpp2::AlterSpaceOp op,
const std::vector<std::string>& paras)
: SingleDependencyNode(qctx, Kind::kAlterSpace, input),
spaceName_(spaceName),
op_(op),
paras_(paras) {}

private:
std::string spaceName_;
meta::cpp2::AlterSpaceOp op_;
std::vector<std::string> paras_;
};

class DescSpace final : public SingleDependencyNode {
public:
static DescSpace* make(QueryContext* qctx, PlanNode* input, std::string spaceName) {
Expand Down
2 changes: 2 additions & 0 deletions src/graph/planner/plan/PlanNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ const char* PlanNode::toString(PlanNode::Kind kind) {
return "DropEdge";
case Kind::kShowSpaces:
return "ShowSpaces";
case Kind::kAlterSpace:
return "AlterSpaces";
case Kind::kShowTags:
return "ShowTags";
case Kind::kShowEdges:
Expand Down
1 change: 1 addition & 0 deletions src/graph/planner/plan/PlanNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class PlanNode {
kDropSpace,
kDropTag,
kDropEdge,
kAlterSpace,

// index related
kCreateTagIndex,
Expand Down
1 change: 1 addition & 0 deletions src/graph/service/PermissionCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Status PermissionCheck::permissionCheck(ClientSession *session,
return Status::OK();
}
case Sentence::Kind::kCreateSpace:
case Sentence::Kind::kAlterSpace:
case Sentence::Kind::kCreateSpaceAs:
case Sentence::Kind::kDropSpace:
case Sentence::Kind::kCreateSnapshot:
Expand Down
11 changes: 11 additions & 0 deletions src/graph/validator/AdminValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ Status CreateSpaceAsValidator::toPlan() {
return Status::OK();
}

Status AlterSpaceValidator::validateImpl() { return Status::OK(); }

Status AlterSpaceValidator::toPlan() {
auto sentence = static_cast<AlterSpaceSentence *>(sentence_);
auto *doNode = AlterSpace::make(
qctx_, nullptr, sentence->spaceName(), sentence->alterSpaceOp(), sentence->paras());
root_ = doNode;
tail_ = root_;
return Status::OK();
}

Status DescSpaceValidator::validateImpl() { return Status::OK(); }

Status DescSpaceValidator::toPlan() {
Expand Down
12 changes: 12 additions & 0 deletions src/graph/validator/AdminValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ class CreateSpaceAsValidator final : public Validator {
std::string newSpaceName_;
};

class AlterSpaceValidator final : public Validator {
public:
AlterSpaceValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {
noSpaceRequired_ = true;
}

private:
Status validateImpl() override;

Status toPlan() override;
};

class DescSpaceValidator final : public Validator {
public:
DescSpaceValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {
Expand Down
2 changes: 2 additions & 0 deletions src/graph/validator/Validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ std::unique_ptr<Validator> Validator::makeValidator(Sentence* sentence, QueryCon
return std::make_unique<ShowQueriesValidator>(sentence, context);
case Sentence::Kind::kKillQuery:
return std::make_unique<KillQueryValidator>(sentence, context);
case Sentence::Kind::kAlterSpace:
return std::make_unique<AlterSpaceValidator>(sentence, context);
case Sentence::Kind::kUnknown:
case Sentence::Kind::kReturn: {
// nothing
Expand Down
11 changes: 11 additions & 0 deletions src/interface/meta.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ struct ExecResp {
3: common.HostAddr leader,
}

enum AlterSpaceOp {
ADD_ZONE = 0x01,
} (cpp.enum_strict)

struct AlterSpaceReq {
1: binary space_name,
2: AlterSpaceOp op,
3: list<binary> paras,
}

// Job related data structures
enum AdminJobOp {
ADD = 0x01,
Expand Down Expand Up @@ -1133,6 +1143,7 @@ service MetaService {
ExecResp dropSpace(1: DropSpaceReq req);
GetSpaceResp getSpace(1: GetSpaceReq req);
ListSpacesResp listSpaces(1: ListSpacesReq req);
ExecResp alterSpace(1: AlterSpaceReq req);

ExecResp createSpaceAs(1: CreateSpaceAsReq req);

Expand Down
1 change: 1 addition & 0 deletions src/meta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ nebula_add_library(
processors/parts/ListSpacesProcessor.cpp
processors/parts/DropSpaceProcessor.cpp
processors/parts/GetPartsAllocProcessor.cpp
processors/parts/AlterSpaceProcessor.cpp
processors/schema/CreateTagProcessor.cpp
processors/schema/AlterTagProcessor.cpp
processors/schema/GetTagProcessor.cpp
Expand Down
7 changes: 7 additions & 0 deletions src/meta/MetaServiceHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "meta/processors/kv/RemoveRangeProcessor.h"
#include "meta/processors/kv/ScanProcessor.h"
#include "meta/processors/listener/ListenerProcessor.h"
#include "meta/processors/parts/AlterSpaceProcessor.h"
#include "meta/processors/parts/CreateSpaceAsProcessor.h"
#include "meta/processors/parts/CreateSpaceProcessor.h"
#include "meta/processors/parts/DropSpaceProcessor.h"
Expand Down Expand Up @@ -85,6 +86,12 @@ folly::Future<cpp2::ExecResp> MetaServiceHandler::future_createSpace(
RETURN_FUTURE(processor);
}

folly::Future<cpp2::ExecResp> MetaServiceHandler::future_alterSpace(
const cpp2::AlterSpaceReq& req) {
auto* processor = AlterSpaceProcessor::instance(kvstore_);
RETURN_FUTURE(processor);
}

folly::Future<cpp2::ExecResp> MetaServiceHandler::future_createSpaceAs(
const cpp2::CreateSpaceAsReq& req) {
auto* processor = CreateSpaceAsProcessor::instance(kvstore_);
Expand Down
2 changes: 2 additions & 0 deletions src/meta/MetaServiceHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MetaServiceHandler final : public cpp2::MetaServiceSvIf {
* */
folly::Future<cpp2::ExecResp> future_createSpace(const cpp2::CreateSpaceReq& req) override;

folly::Future<cpp2::ExecResp> future_alterSpace(const cpp2::AlterSpaceReq& req) override;

folly::Future<cpp2::ExecResp> future_createSpaceAs(const cpp2::CreateSpaceAsReq& req) override;

folly::Future<cpp2::ExecResp> future_dropSpace(const cpp2::DropSpaceReq& req) override;
Expand Down
92 changes: 92 additions & 0 deletions src/meta/processors/parts/AlterSpaceProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "meta/processors/parts/AlterSpaceProcessor.h"

namespace nebula {
namespace meta {
void AlterSpaceProcessor::process(const cpp2::AlterSpaceReq& req) {
const std::vector<std::string>& zones = req.get_paras();
const std::string& spaceName = req.get_space_name();
cpp2::AlterSpaceOp op = req.get_op();
switch (op) {
case cpp2::AlterSpaceOp::ADD_ZONE: {
nebula::cpp2::ErrorCode ret = addZones(spaceName, zones);
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
handleErrorCode(ret);
onFinished();
return;
}
break;
}
default:
break;
}
handleErrorCode(nebula::cpp2::ErrorCode::SUCCEEDED);
onFinished();
}

nebula::cpp2::ErrorCode AlterSpaceProcessor::addZones(const std::string& spaceName,
const std::vector<std::string>& zones) {
auto spaceRet = getSpaceId(spaceName);
if (!nebula::ok(spaceRet)) {
auto retCode = nebula::error(spaceRet);
return retCode;
}
auto spaceId = nebula::value(spaceRet);
std::string spaceKey = MetaKeyUtils::spaceKey(spaceId);
std::string spaceVal;
nebula::cpp2::ErrorCode retCode =
kvstore_->get(kDefaultSpaceId, kDefaultPartId, spaceKey, &spaceVal);
if (retCode != nebula::cpp2::ErrorCode::SUCCEEDED) {
return retCode;
}
meta::cpp2::SpaceDesc properties = MetaKeyUtils::parseSpace(spaceVal);
const std::vector<std::string>& curZones = properties.get_zone_names();
std::set<std::string> zm;
for (const std::string& z : curZones) {
zm.insert(z);
}
std::vector<std::string> newZones = curZones;
newZones.reserve(curZones.size() + zones.size());
for (const std::string& z : zones) {
std::string zoneKey = MetaKeyUtils::zoneKey(z);
std::string zoneVal;
nebula::cpp2::ErrorCode zoneRet =
kvstore_->get(kDefaultSpaceId, kDefaultPartId, zoneKey, &zoneVal);
if (zoneRet != nebula::cpp2::ErrorCode::SUCCEEDED) {
return zoneRet == nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND
? nebula::cpp2::ErrorCode::E_ZONE_NOT_FOUND
: zoneRet;
}
if (zm.count(z)) {
return nebula::cpp2::ErrorCode::E_CONFLICT;
}
newZones.emplace_back(z);
}
properties.set_zone_names(newZones);
std::vector<kvstore::KV> data;
data.emplace_back(MetaKeyUtils::spaceKey(spaceId), MetaKeyUtils::spaceVal(properties));
folly::Baton<true, std::atomic> baton;
auto ret = nebula::cpp2::ErrorCode::SUCCEEDED;
kvstore_->asyncMultiPut(kDefaultSpaceId,
kDefaultPartId,
std::move(data),
[&ret, &baton](nebula::cpp2::ErrorCode code) {
if (nebula::cpp2::ErrorCode::SUCCEEDED != code) {
ret = code;
LOG(INFO) << "Put data error on meta server";
}
baton.post();
});
baton.wait();
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
return ret;
}
return nebula::cpp2::ErrorCode::SUCCEEDED;
}

} // namespace meta
} // namespace nebula
33 changes: 33 additions & 0 deletions src/meta/processors/parts/AlterSpaceProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#ifndef NEBULA_GRAPH_ALTERSPACEPROCESSOR_H
#define NEBULA_GRAPH_ALTERSPACEPROCESSOR_H

#include "meta/processors/BaseProcessor.h"

namespace nebula {
namespace meta {
class AlterSpaceProcessor : public BaseProcessor<cpp2::ExecResp> {
public:
static AlterSpaceProcessor* instance(kvstore::KVStore* kvstore) {
return new AlterSpaceProcessor(kvstore);
}

void process(const cpp2::AlterSpaceReq& req);

private:
nebula::cpp2::ErrorCode addZones(const std::string& spaceName,
const std::vector<std::string>& zones);

private:
explicit AlterSpaceProcessor(kvstore::KVStore* kvstore)
: BaseProcessor<cpp2::ExecResp>(kvstore) {}
};

} // namespace meta
} // namespace nebula

#endif // NEBULA_GRAPH_ALTERSPACEPROCESSOR_H
Loading

0 comments on commit 89f99bf

Please sign in to comment.