Skip to content

Commit

Permalink
Implement create space, drop space, add hosts, show hosts , remove ho…
Browse files Browse the repository at this point in the history
…sts via MetaClient (vesoft-inc#223)

close vesoft-inc#200
  • Loading branch information
ayyt authored and dutor committed Apr 16, 2019
1 parent ef32037 commit a0f43bb
Show file tree
Hide file tree
Showing 30 changed files with 1,079 additions and 110 deletions.
1 change: 1 addition & 0 deletions src/daemons/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_executable(
$<TARGET_OBJECTS:meta_client>
$<TARGET_OBJECTS:meta_thrift_obj>
$<TARGET_OBJECTS:storage_client>
$<TARGET_OBJECTS:meta_thrift_obj>
$<TARGET_OBJECTS:time_obj>
$<TARGET_OBJECTS:fs_obj>
$<TARGET_OBJECTS:stats_obj>
Expand Down
58 changes: 58 additions & 0 deletions src/executor/AddHostsExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved
*
* This source code is licensed under Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory)
*/

#include "graph/AddHostsExecutor.h"

namespace nebula {
namespace graph {

AddHostsExecutor::AddHostsExecutor(Sentence *sentence,
ExecutionContext *ectx) : Executor(ectx) {
sentence_ = static_cast<AddHostsSentence*>(sentence);
}


Status AddHostsExecutor::prepare() {
hosts_ = sentence_->hosts();
if (hosts_.size() == 0) {
return Status::Error("Host address illegal");
}
return Status::OK();
}


void AddHostsExecutor::execute() {
auto future = ectx()->getMetaClient()->addHosts(hosts_);
auto *runner = ectx()->rctx()->runner();

auto cb = [this] (auto &&resp) {
if (!resp.ok()) {
DCHECK(onError_);
onError_(resp.status());
return;
}
auto ret = resp.value();
if (!ret) {
DCHECK(onError_);
onError_(Status::Error("Add hosts failed"));
return;
}
DCHECK(onFinish_);
onFinish_();
};

auto error = [this] (auto &&e) {
LOG(ERROR) << "Exception caught: " << e.what();
DCHECK(onError_);
onError_(Status::Error("Internal error"));
return;
};

std::move(future).via(runner).thenValue(cb).thenError(error);
}

} // namespace graph
} // namespace nebula
36 changes: 36 additions & 0 deletions src/executor/AddHostsExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved
*
* This source code is licensed under Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory)
*/

#ifndef GRAPH_ADDHOSTSEXECUTOR_H_
#define GRAPH_ADDHOSTSEXECUTOR_H_

#include "base/Base.h"
#include "graph/Executor.h"

namespace nebula {
namespace graph {

class AddHostsExecutor final : public Executor {
public:
AddHostsExecutor(Sentence *sentence, ExecutionContext *ectx);

const char* name() const override {
return "AddHostsExecutor";
}

Status MUST_USE_RESULT prepare() override;

void execute() override;

private:
AddHostsSentence *sentence_{nullptr};
std::vector<HostAddr> hosts_;
};

} // namespace graph
} // namespace nebula

#endif // GRAPH_ADDHOSTSEXECUTOR_H_
17 changes: 15 additions & 2 deletions src/executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@ add_library(
InsertVertexExecutor.cpp
InsertEdgeExecutor.cpp
AssignmentExecutor.cpp
ShowExecutor.cpp
InterimResult.cpp
VariableHolder.cpp
AddHostsExecutor.cpp
RemoveHostsExecutor.cpp
CreateSpaceExecutor.cpp
DropSpaceExecutor.cpp
ShowExecutor.cpp
mock/PropertiesSchema.cpp
mock/EdgeSchema.cpp
mock/TagSchema.cpp
mock/SchemaManager.cpp
mock/StorageService.cpp
)
add_dependencies(graph_obj base_obj parser_obj graph_thrift_obj ws_obj)
add_dependencies(
graph_obj
base_obj
parser_obj
graph_thrift_obj
ws_obj
meta_thrift_obj
meta_client
schema_obj
)

add_subdirectory(test)
71 changes: 71 additions & 0 deletions src/executor/CreateSpaceExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved
*
* This source code is licensed under Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory)
*/

#include "graph/CreateSpaceExecutor.h"

namespace nebula {
namespace graph {

CreateSpaceExecutor::CreateSpaceExecutor(Sentence *sentence,
ExecutionContext *ectx) : Executor(ectx) {
sentence_ = static_cast<CreateSpaceSentence*>(sentence);
}


Status CreateSpaceExecutor::prepare() {
spaceName_ = sentence_->spaceName();
for (auto &item : sentence_->getOpts()) {
switch (item->getOptType()) {
case SpaceOptItem::PARTITION_NUM:
partNum_ = item->get_partition_num();
break;
case SpaceOptItem::REPLICA_FACTOR:
replicaFactor_ = item->get_replica_factor();
break;
}
}
if (partNum_ == 0) {
return Status::Error("Partition_num value illegal");
}
if (replicaFactor_ == 0) {
return Status::Error("Replica_factor value illegal");
}
return Status::OK();
}


void CreateSpaceExecutor::execute() {
auto future = ectx()->getMetaClient()->createSpace(*spaceName_, partNum_, replicaFactor_);
auto *runner = ectx()->rctx()->runner();

auto cb = [this] (auto &&resp) {
if (!resp.ok()) {
DCHECK(onError_);
onError_(resp.status());
return;
}
auto spaceId = resp.value();
if (spaceId <= 0) {
DCHECK(onError_);
onError_(Status::Error("Create space failed"));
return;
}
DCHECK(onFinish_);
onFinish_();
};

auto error = [this] (auto &&e) {
LOG(ERROR) << "Exception caught: " << e.what();
DCHECK(onError_);
onError_(Status::Error("Internal error"));
return;
};

std::move(future).via(runner).thenValue(cb).thenError(error);
}

} // namespace graph
} // namespace nebula
38 changes: 38 additions & 0 deletions src/executor/CreateSpaceExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved
*
* This source code is licensed under Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory)
*/

#ifndef GRAPH_CREATESPACEEXECUTOR_H_
#define GRAPH_CREATESPACEEXECUTOR_H_

#include "base/Base.h"
#include "graph/Executor.h"

namespace nebula {
namespace graph {

class CreateSpaceExecutor final : public Executor {
public:
CreateSpaceExecutor(Sentence *sentence, ExecutionContext *ectx);

const char* name() const override {
return "CreateSpaceExecutor";
}

Status MUST_USE_RESULT prepare() override;

void execute() override;

private:
CreateSpaceSentence *sentence_{nullptr};
std::string *spaceName_{nullptr};
int32_t partNum_{0};
int32_t replicaFactor_{0};
};

} // namespace graph
} // namespace nebula

#endif // GRAPH_CREATESPACEEXECUTOR_H_
55 changes: 55 additions & 0 deletions src/executor/DropSpaceExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved
*
* This source code is licensed under Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory)
*/

#include "graph/DropSpaceExecutor.h"

namespace nebula {
namespace graph {

DropSpaceExecutor::DropSpaceExecutor(Sentence *sentence,
ExecutionContext *ectx) : Executor(ectx) {
sentence_ = static_cast<DropSpaceSentence*>(sentence);
}


Status DropSpaceExecutor::prepare() {
spaceName_ = sentence_->spaceName();
return Status::OK();
}


void DropSpaceExecutor::execute() {
auto future = ectx()->getMetaClient()->dropSpace(*spaceName_);
auto *runner = ectx()->rctx()->runner();

auto cb = [this] (auto &&resp) {
if (!resp.ok()) {
DCHECK(onError_);
onError_(resp.status());
return;
}
auto ret = resp.value();
if (!ret) {
DCHECK(onError_);
onError_(Status::Error("Drop space failed"));
return;
}
DCHECK(onFinish_);
onFinish_();
};

auto error = [this] (auto &&e) {
LOG(ERROR) << "Exception caught: " << e.what();
DCHECK(onError_);
onError_(Status::Error("Internal error"));
return;
};

std::move(future).via(runner).thenValue(cb).thenError(error);
}

} // namespace graph
} // namespace nebula
36 changes: 36 additions & 0 deletions src/executor/DropSpaceExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved
*
* This source code is licensed under Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory)
*/

#ifndef GRAPH_DROPSPACEEXECUTOR_H_
#define GRAPH_DROPSPACEEXECUTOR_H_

#include "base/Base.h"
#include "graph/Executor.h"

namespace nebula {
namespace graph {

class DropSpaceExecutor final : public Executor {
public:
DropSpaceExecutor(Sentence *sentence, ExecutionContext *ectx);

const char* name() const override {
return "DropSpaceExecutor";
}

Status MUST_USE_RESULT prepare() override;

void execute() override;

private:
DropSpaceSentence *sentence_{nullptr};
std::string *spaceName_{nullptr};
};

} // namespace graph
} // namespace nebula

#endif // GRAPH_DROPSPACEEXECUTOR_H_
13 changes: 11 additions & 2 deletions src/executor/ExecutionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "parser/SequentialSentences.h"
#include "meta/SchemaManager.h"
#include "graph/VariableHolder.h"
#include "graph/mock/StorageService.h"
#include "meta/client/MetaClient.h"

/**
* ExecutionContext holds context infos in the execution process, e.g. clients of storage or meta services.
Expand All @@ -29,10 +31,12 @@ class ExecutionContext final : public cpp::NonCopyable, public cpp::NonMovable {
using RequestContextPtr = std::unique_ptr<RequestContext<cpp2::ExecutionResponse>>;
ExecutionContext(RequestContextPtr rctx,
meta::SchemaManager *sm,
storage::StorageClient *storage) {
storage::StorageClient *storage,
meta::MetaClient *metaClient) {
rctx_ = std::move(rctx);
sm_ = sm;
storage_ = storage;
metaClient_ = metaClient;
variableHolder_ = std::make_unique<VariableHolder>();
}

Expand All @@ -54,10 +58,15 @@ class ExecutionContext final : public cpp::NonCopyable, public cpp::NonMovable {
return variableHolder_.get();
}

meta::MetaClient* getMetaClient() const {
return metaClient_;
}

private:
RequestContextPtr rctx_;
meta::SchemaManager *sm_{nullptr};
meta::SchemaManager *sm_{nullptr};
storage::StorageClient *storage_{nullptr};
meta::MetaClient *metaClient_{nullptr};
std::unique_ptr<VariableHolder> variableHolder_;
};

Expand Down
Loading

0 comments on commit a0f43bb

Please sign in to comment.