Skip to content

Commit

Permalink
Allow to create graph space without options (#408)
Browse files Browse the repository at this point in the history
* Allow to create graph space without options

* Addressed @steppenwolfyuetong's comments

* Addressed comments
  • Loading branch information
dutor authored Jun 11, 2019
1 parent c4fedff commit 9df4d23
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 18 deletions.
2 changes: 1 addition & 1 deletion etc/nebula-graphd.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
# HTTP service port
--ws_http_port=13000
# HTTP2 service port
--ws_h2_portt=13002
--ws_h2_port=13002
9 changes: 8 additions & 1 deletion etc/nebula-metad.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
# HTTP service port
--ws_http_port=11000
# HTTP2 service port
--ws_h2_portt=11002
--ws_h2_port=11002

########## storage ##########
# Root data path, multi paths should be split by comma.For rocksdb engine, one path one instance.
--data_path=data/meta


########## Misc #########
# The default number of parts when a space is created
--default_parts_num=1024
# The default replica factor when a space is created
--default_replica_factor=1
12 changes: 6 additions & 6 deletions src/graph/CreateSpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ Status CreateSpaceExecutor::prepare() {
switch (item->getOptType()) {
case SpaceOptItem::PARTITION_NUM:
partNum_ = item->get_partition_num();
if (partNum_ <= 0) {
return Status::Error("Partition_num value should be greater than zero");
}
break;
case SpaceOptItem::REPLICA_FACTOR:
replicaFactor_ = item->get_replica_factor();
if (replicaFactor_ <= 0) {
return Status::Error("Replica_factor value should be greater than zero");
}
break;
}
}
if (partNum_ <= 0) {
return Status::Error("Partition_num value should be greater than zero");
}
if (replicaFactor_ <= 0) {
return Status::Error("Replica_factor value should be greater than zero");
}
return Status::OK();
}

Expand Down
10 changes: 6 additions & 4 deletions src/graph/CreateSpaceExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class CreateSpaceExecutor final : public Executor {
void execute() override;

private:
CreateSpaceSentence *sentence_{nullptr};
const std::string *spaceName_{nullptr};
int32_t partNum_{0};
int32_t replicaFactor_{0};
CreateSpaceSentence *sentence_{nullptr};
const std::string *spaceName_{nullptr};
// TODO Due to the currently design of the createSpace interface,
// it's impossible to express *not specified*, so we use 0 to indicate this.
int32_t partNum_{0};
int32_t replicaFactor_{0};
};

} // namespace graph
Expand Down
22 changes: 22 additions & 0 deletions src/graph/test/SchemaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ TEST_F(SchemaTest, metaCommunication) {
};
ASSERT_TRUE(verifyResult(resp, expected));
}
{
cpp2::ExecutionResponse resp;
std::string query = "CREATE SPACE space_with_default_options";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
{
cpp2::ExecutionResponse resp;
std::string query = "DESCRIBE SPACE space_with_default_options";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
std::vector<std::tuple<int, std::string, int, int>> expected{
{2, "space_with_default_options", 1024, 1},
};
ASSERT_TRUE(verifyResult(resp, expected));
}
{
cpp2::ExecutionResponse resp;
std::string query = "DROP SPACE space_with_default_options";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
{
cpp2::ExecutionResponse resp;
std::string query = "USE default_space";
Expand Down
2 changes: 1 addition & 1 deletion src/meta/MetaServiceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::string MetaServiceUtils::spaceKey(GraphSpaceID spaceId) {
return key;
}

std::string MetaServiceUtils::spaceVal(cpp2::SpaceProperties properties) {
std::string MetaServiceUtils::spaceVal(const cpp2::SpaceProperties &properties) {
std::string val;
apache::thrift::CompactSerializer::serialize(properties, &val);
return val;
Expand Down
2 changes: 1 addition & 1 deletion src/meta/MetaServiceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MetaServiceUtils final {

static std::string spaceKey(GraphSpaceID spaceId);

static std::string spaceVal(cpp2::SpaceProperties properties);
static std::string spaceVal(const cpp2::SpaceProperties &properties);

static cpp2::SpaceProperties parseSpace(folly::StringPiece rawData);

Expand Down
16 changes: 15 additions & 1 deletion src/meta/processors/partsMan/CreateSpaceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "meta/processors/partsMan/CreateSpaceProcessor.h"
#include "meta/ActiveHostsMan.h"

DEFINE_int32(default_parts_num, 1024, "The default number of parts when a space is created");
DEFINE_int32(default_replica_factor, 1, "The default replica factor when a space is created");

namespace nebula {
namespace meta {

void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
folly::SharedMutex::WriteHolder wHolder(LockUtils::spaceLock());
auto& properties = req.get_properties();
auto properties = req.get_properties();
auto spaceRet = getSpaceId(properties.get_space_name());
if (spaceRet.ok()) {
LOG(ERROR) << "Create Space Failed : Space " << properties.get_space_name()
Expand All @@ -35,6 +38,16 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
auto spaceName = properties.get_space_name();
auto partitionNum = properties.get_partition_num();
auto replicaFactor = properties.get_replica_factor();
if (partitionNum == 0) {
partitionNum = FLAGS_default_parts_num;
// Set the default value back to the struct, which will be written to storage
properties.set_partition_num(partitionNum);
}
if (replicaFactor == 0) {
replicaFactor = FLAGS_default_replica_factor;
// Set the default value back to the struct, which will be written to storage
properties.set_replica_factor(replicaFactor);
}
VLOG(3) << "Create space " << spaceName << ", id " << spaceId;
if ((int32_t)hosts.size() < replicaFactor) {
LOG(ERROR) << "Not enough hosts existed for replica "
Expand All @@ -43,6 +56,7 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
onFinished();
return;
}

std::vector<kvstore::KV> data;
data.emplace_back(MetaServiceUtils::indexSpaceKey(spaceName),
std::string(reinterpret_cast<const char*>(&spaceId), sizeof(spaceId)));
Expand Down
12 changes: 10 additions & 2 deletions src/parser/AdminSentences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,16 @@ std::string SpaceOptList::toString() const {


std::string CreateSpaceSentence::toString() const {
return folly::stringPrintf("CREATE SPACE %s(%s) ", spaceName_.get()->c_str(),
spaceOpts_->toString().c_str());
std::string buf;
buf.reserve(256);
buf += "CREATE SPACE ";
buf += *spaceName_;
if (spaceOpts_ != nullptr) {
buf += "(";
buf += spaceOpts_->toString();
buf += ")";
}
return buf;
}

std::string DropSpaceSentence::toString() const {
Expand Down
3 changes: 3 additions & 0 deletions src/parser/AdminSentences.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ class CreateSpaceSentence final : public Sentence {
}

std::vector<SpaceOptItem*> getOpts() {
if (spaceOpts_ == nullptr) {
return {};
}
return spaceOpts_->getOpts();
}

Expand Down
6 changes: 5 additions & 1 deletion src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,11 @@ host_item
port : INTEGER { $$ = $1; }

create_space_sentence
: KW_CREATE KW_SPACE name_label L_PAREN space_opt_list R_PAREN {
: KW_CREATE KW_SPACE name_label {
auto sentence = new CreateSpaceSentence($3);
$$ = sentence;
}
| KW_CREATE KW_SPACE name_label L_PAREN space_opt_list R_PAREN {
auto sentence = new CreateSpaceSentence($3);
sentence->setOpts($5);
$$ = sentence;
Expand Down

0 comments on commit 9df4d23

Please sign in to comment.