Skip to content

Commit

Permalink
Support "show parts" command (vesoft-inc#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangguoqing authored and dangleptr committed Nov 7, 2019
1 parent 94d4385 commit 9922295
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 5 deletions.
2 changes: 1 addition & 1 deletion resources/completion.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"SHOW" : {
"sub_keywords": [
"HOSTS", "TAGS", "EDGES", "SPACES", "USERS", "ROLES", "USER", "CONFIGS"
"HOSTS", "PARTS", "TAGS", "EDGES", "SPACES", "USERS", "ROLES", "USER", "CONFIGS"
]
},
"DESCRIBE" : {
Expand Down
77 changes: 76 additions & 1 deletion src/executor/ShowExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Status ShowExecutor::prepare() {


void ShowExecutor::execute() {
if (sentence_->showType() == ShowSentence::ShowType::kShowTags ||
if (sentence_->showType() == ShowSentence::ShowType::kShowParts ||
sentence_->showType() == ShowSentence::ShowType::kShowTags ||
sentence_->showType() == ShowSentence::ShowType::kShowEdges ||
sentence_->showType() == ShowSentence::ShowType::kShowCreateTag ||
sentence_->showType() == ShowSentence::ShowType::kShowCreateEdge) {
Expand All @@ -43,6 +44,9 @@ void ShowExecutor::execute() {
case ShowSentence::ShowType::kShowSpaces:
showSpaces();
break;
case ShowSentence::ShowType::kShowParts:
showParts();
break;
case ShowSentence::ShowType::kShowTags:
showTags();
break;
Expand Down Expand Up @@ -202,6 +206,77 @@ void ShowExecutor::showSpaces() {
}


void ShowExecutor::showParts() {
auto spaceId = ectx()->rctx()->session()->space();
auto future = ectx()->getMetaClient()->listParts(spaceId);
auto *runner = ectx()->rctx()->runner();

auto cb = [this] (auto &&resp) {
if (!resp.ok()) {
DCHECK(onError_);
onError_(std::move(resp).status());
return;
}

auto partItems = std::move(resp).value();
std::vector<cpp2::RowValue> rows;
std::vector<std::string> header{"Partition ID", "Leader", "Peers", "Losts"};
resp_ = std::make_unique<cpp2::ExecutionResponse>();
resp_->set_column_names(std::move(header));

std::sort(partItems.begin(), partItems.end(),
[] (const auto& a, const auto& b) {
return a.get_part_id() < b.get_part_id();
});

for (auto& item : partItems) {
std::vector<cpp2::ColumnValue> row;
row.resize(4);
row[0].set_integer(item.get_part_id());

if (item.__isset.leader) {
auto leader = item.get_leader();
std::vector<HostAddr> leaders = {{leader->ip, leader->port}};
std::string leaderStr = NetworkUtils::toHosts(leaders);
row[1].set_str(leaderStr);
} else {
row[1].set_str("");
}

std::vector<HostAddr> peers;
for (auto& peer : item.get_peers()) {
peers.emplace_back(peer.ip, peer.port);
}
std::string peersStr = NetworkUtils::toHosts(peers);
row[2].set_str(peersStr);

std::vector<HostAddr> losts;
for (auto& lost : item.get_losts()) {
losts.emplace_back(lost.ip, lost.port);
}
std::string lostsStr = NetworkUtils::toHosts(losts);
row[3].set_str(lostsStr);

rows.emplace_back();
rows.back().set_columns(std::move(row));
}
resp_->set_rows(std::move(rows));

DCHECK(onFinish_);
onFinish_();
};

auto error = [this] (auto &&e) {
LOG(ERROR) << "Exception caught: " << e.what();
DCHECK(onError_);
onError_(Status::Error(folly::stringPrintf("Internal error : %s",
e.what().c_str())));
return;
};
std::move(future).via(runner).thenValue(cb).thenError(error);
}


void ShowExecutor::showTags() {
auto spaceId = ectx()->rctx()->session()->space();
auto future = ectx()->getMetaClient()->listTagSchemas(spaceId);
Expand Down
1 change: 1 addition & 0 deletions src/executor/ShowExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ShowExecutor final : public Executor {
void execute() override;
void showHosts();
void showSpaces();
void showParts();
void showTags();
void showEdges();
void showCreateSpace();
Expand Down
36 changes: 34 additions & 2 deletions src/executor/test/SchemaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "graph/test/TestEnv.h"
#include "graph/test/TestBase.h"
#include "meta/test/TestUtils.h"
#include "storage/test/TestUtils.h"

DECLARE_int32(load_data_interval_secs);

Expand Down Expand Up @@ -136,6 +137,20 @@ TEST_F(SchemaTest, metaCommunication) {
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
// show parts of default_space
{
cpp2::ExecutionResponse resp;
std::string query = "SHOW PARTS; # before leader election";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
ASSERT_EQ(9, (*(resp.get_rows())).size());
std::string host = "127.0.0.1:" + std::to_string(gEnv->storageServerPort());
std::vector<std::tuple<int, std::string, std::string, std::string>> expected;
for (int32_t partId = 1; partId <= 9; partId++) {
expected.emplace_back(std::make_tuple(partId, "", host, ""));
}
ASSERT_TRUE(verifyResult(resp, expected));
}
// Test same prop name
{
cpp2::ExecutionResponse resp;
Expand Down Expand Up @@ -242,7 +257,6 @@ TEST_F(SchemaTest, metaCommunication) {
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::E_EXECUTION_ERROR, code);
}

// Test unreserved keyword
{
cpp2::ExecutionResponse resp;
Expand Down Expand Up @@ -528,7 +542,6 @@ TEST_F(SchemaTest, metaCommunication) {
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
// Test different tag and edge in different space
{
cpp2::ExecutionResponse resp;
std::string query = "SHOW CREATE EDGE education";
Expand All @@ -544,7 +557,26 @@ TEST_F(SchemaTest, metaCommunication) {
};
EXPECT_TRUE(verifyResult(resp, expected));
}
// show parts of default_space
{
auto kvstore = gEnv->storageServer()->kvStore_.get();
auto spaceResult = gEnv->metaClient()->getSpaceIdByNameFromCache("default_space");
ASSERT_TRUE(spaceResult.ok());
GraphSpaceID spaceId = spaceResult.value();
nebula::storage::TestUtils::waitUntilAllElected(kvstore, spaceId, 9);

cpp2::ExecutionResponse resp;
std::string query = "SHOW PARTS; # after leader election";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
ASSERT_EQ(9, (*(resp.get_rows())).size());
std::string host = "127.0.0.1:" + std::to_string(gEnv->storageServerPort());
std::vector<std::tuple<int, std::string, std::string, std::string>> expected;
for (int32_t partId = 1; partId <= 9; partId++) {
expected.emplace_back(std::make_tuple(partId, host, host, ""));
}
ASSERT_TRUE(verifyResult(resp, expected));
}
// Test different tag and edge in different space
{
cpp2::ExecutionResponse resp;
Expand Down
8 changes: 8 additions & 0 deletions src/executor/test/TestEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,13 @@ meta::ClientBasedGflagsManager* TestEnv::gflagsManager() {
return gflagsManager_.get();
}

test::ServerContext* TestEnv::storageServer() {
return storageServer_.get();
}

meta::MetaClient* TestEnv::metaClient() {
return mClient_.get();
}

} // namespace graph
} // namespace nebula
4 changes: 4 additions & 0 deletions src/executor/test/TestEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class TestEnv : public ::testing::Environment {

meta::ClientBasedGflagsManager* gflagsManager();

test::ServerContext* storageServer();

meta::MetaClient* metaClient();

private:
nebula::fs::TempDir metaRootPath_{"/tmp/MetaTest.XXXXXX"};
nebula::fs::TempDir storageRootPath_{"/tmp/StorageTest.XXXXXX"};
Expand Down
2 changes: 2 additions & 0 deletions src/parser/AdminSentences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ std::string ShowSentence::toString() const {
return std::string("SHOW HOSTS");
case ShowType::kShowSpaces:
return std::string("SHOW SPACES");
case ShowType::kShowParts:
return std::string("SHOW PARTS");
case ShowType::kShowTags:
return std::string("SHOW TAGS");
case ShowType::kShowEdges:
Expand Down
1 change: 1 addition & 0 deletions src/parser/AdminSentences.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ShowSentence final : public Sentence {
kUnknown,
kShowHosts,
kShowSpaces,
kShowParts,
kShowTags,
kShowEdges,
kShowUsers,
Expand Down
5 changes: 4 additions & 1 deletion src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class GraphScanner;
%token KW_MATCH KW_INSERT KW_VALUES KW_YIELD KW_RETURN KW_CREATE KW_VERTEX
%token KW_EDGE KW_EDGES KW_STEPS KW_OVER KW_UPTO KW_REVERSELY KW_SPACE KW_DELETE KW_FIND
%token KW_INT KW_BIGINT KW_DOUBLE KW_STRING KW_BOOL KW_TAG KW_TAGS KW_UNION KW_INTERSECT KW_MINUS
%token KW_NO KW_OVERWRITE KW_IN KW_DESCRIBE KW_DESC KW_SHOW KW_HOSTS KW_TIMESTAMP KW_ADD
%token KW_NO KW_OVERWRITE KW_IN KW_DESCRIBE KW_DESC KW_SHOW KW_HOSTS KW_PARTS KW_TIMESTAMP KW_ADD
%token KW_PARTITION_NUM KW_REPLICA_FACTOR KW_DROP KW_REMOVE KW_SPACES KW_INGEST KW_UUID
%token KW_IF KW_NOT KW_EXISTS KW_WITH KW_FIRSTNAME KW_LASTNAME KW_EMAIL KW_PHONE KW_USER KW_USERS
%token KW_PASSWORD KW_CHANGE KW_ROLE KW_GOD KW_ADMIN KW_GUEST KW_GRANT KW_REVOKE KW_ON
Expand Down Expand Up @@ -1362,6 +1362,9 @@ show_sentence
| KW_SHOW KW_SPACES {
$$ = new ShowSentence(ShowSentence::ShowType::kShowSpaces);
}
| KW_SHOW KW_PARTS {
$$ = new ShowSentence(ShowSentence::ShowType::kShowParts);
}
| KW_SHOW KW_TAGS {
$$ = new ShowSentence(ShowSentence::ShowType::kShowTags);
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/scanner.lex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ FALSE ([Ff][Aa][Ll][Ss][Ee])
SHOW ([Ss][Hh][Oo][Ww])
ADD ([Aa][Dd][Dd])
HOSTS ([Hh][Oo][Ss][Tt][Ss])
PARTS ([Pp][Aa][Rr][Tt][Ss])
TIMESTAMP ([Tt][Ii][Mm][Ee][Ss][Tt][Aa][Mm][Pp])
PARTITION_NUM ([Pp][Aa][Rr][Tt][Ii][Tt][Ii][[Oo][Nn][_][Nn][Uu][Mm])
REPLICA_FACTOR ([Rr][Ee][Pp][Ll][Ii][Cc][Aa][_][Ff][Aa][Cc][Tt][Oo][Rr])
Expand Down Expand Up @@ -186,6 +187,7 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
{SHOW} { return TokenType::KW_SHOW; }
{ADD} { return TokenType::KW_ADD; }
{HOSTS} { return TokenType::KW_HOSTS; }
{PARTS} { return TokenType::KW_PARTS; }
{TIMESTAMP} { return TokenType::KW_TIMESTAMP; }
{CREATE} { return TokenType::KW_CREATE;}
{PARTITION_NUM} { return TokenType::KW_PARTITION_NUM; }
Expand Down
6 changes: 6 additions & 0 deletions src/parser/test/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,12 @@ TEST(Parser, AdminOperation) {
auto result = parser.parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
{
GQLParser parser;
std::string query = "SHOW PARTS";
auto result = parser.parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
{
GQLParser parser;
std::string query = "SHOW TAGS";
Expand Down
2 changes: 2 additions & 0 deletions src/parser/test/ScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ TEST(Scanner, Basic) {
CHECK_SEMANTIC_TYPE("space", TokenType::KW_SPACE),
CHECK_SEMANTIC_TYPE("SPACES", TokenType::KW_SPACES),
CHECK_SEMANTIC_TYPE("spaces", TokenType::KW_SPACES),
CHECK_SEMANTIC_TYPE("PARTS", TokenType::KW_PARTS),
CHECK_SEMANTIC_TYPE("Parts", TokenType::KW_PARTS),
CHECK_SEMANTIC_TYPE("BIGINT", TokenType::KW_BIGINT),
CHECK_SEMANTIC_TYPE("bigint", TokenType::KW_BIGINT),
CHECK_SEMANTIC_TYPE("DOUBLE", TokenType::KW_DOUBLE),
Expand Down

0 comments on commit 9922295

Please sign in to comment.