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 aeb5584 commit 409f054
Show file tree
Hide file tree
Showing 48 changed files with 1,440 additions and 161 deletions.
5 changes: 4 additions & 1 deletion src/common/base/Status.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ class Status final {

// TODO(dangleptr) we could use ErrorOr to replace SpaceNotFound here.
STATUS_GENERATOR(SpaceNotFound);
STATUS_GENERATOR(HostNotFound);

#undef STATUS_GENERATOR

std::string toString() const;

friend std::ostream& operator<<(std::ostream &os, const Status &status);

private:
// If some kind of error really needs to be distinguished with others using a specific
// code, other than a general code and specific msg, you could add a new code below,
// e.g. kSomeError, and add the cooresponding STATUS_GENERATOR(SomeError)
Expand All @@ -123,6 +123,7 @@ class Status final {
// ...
// 4xx, for meta service errors
kSpaceNotFound = 404,
kHostNotFound = 405,
};

Code code() const {
Expand All @@ -131,6 +132,8 @@ class Status final {
}
return reinterpret_cast<const Header*>(state_.get())->code_;
}

private:
// REQUIRES: stat_ != nullptr
uint16_t size() const {
return reinterpret_cast<const Header*>(state_.get())->size_;
Expand Down
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/graph/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/graph/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/graph/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/graph/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/graph/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/graph/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/graph/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_
Loading

0 comments on commit 409f054

Please sign in to comment.