Skip to content

Commit

Permalink
Support white list
Browse files Browse the repository at this point in the history
  • Loading branch information
darionyaphet committed Nov 24, 2021
1 parent b7c6901 commit b3735fd
Show file tree
Hide file tree
Showing 63 changed files with 2,804 additions and 2,096 deletions.
63 changes: 48 additions & 15 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3022,17 +3022,48 @@ void MetaClient::loadLeader(const std::vector<cpp2::HostItem>& hostItems,
}
}

folly::Future<StatusOr<bool>> MetaClient::addZone(std::string zoneName,
std::vector<HostAddr> nodes) {
cpp2::AddZoneReq req;
req.set_zone_name(std::move(zoneName));
req.set_nodes(std::move(nodes));
folly::Future<StatusOr<bool>> MetaClient::addHosts(std::vector<HostAddr> hosts) {
cpp2::AddHostsReq req;
req.set_hosts(std::move(hosts));

folly::Promise<StatusOr<bool>> promise;
auto future = promise.getFuture();
getResponse(
std::move(req),
[](auto client, auto request) { return client->future_addHosts(request); },
[](cpp2::ExecResp&& resp) -> bool {
return resp.get_code() == nebula::cpp2::ErrorCode::SUCCEEDED;
},
std::move(promise));
return future;
}

folly::Future<StatusOr<bool>> MetaClient::dropHosts(std::vector<HostAddr> hosts) {
cpp2::DropHostsReq req;
req.set_hosts(std::move(hosts));

folly::Promise<StatusOr<bool>> promise;
auto future = promise.getFuture();
getResponse(
std::move(req),
[](auto client, auto request) { return client->future_dropHosts(request); },
[](cpp2::ExecResp&& resp) -> bool {
return resp.get_code() == nebula::cpp2::ErrorCode::SUCCEEDED;
},
std::move(promise));
return future;
}

folly::Future<StatusOr<bool>> MetaClient::mergeZone(std::vector<std::string> zones,
std::string zoneName) {
cpp2::MergeZoneReq req;
req.set_zone_name(zoneName);
req.set_zones(zones);
folly::Promise<StatusOr<bool>> promise;
auto future = promise.getFuture();
getResponse(
std::move(req),
[](auto client, auto request) { return client->future_addZone(request); },
[](auto client, auto request) { return client->future_mergeZone(request); },
[](cpp2::ExecResp&& resp) -> bool {
return resp.get_code() == nebula::cpp2::ErrorCode::SUCCEEDED;
},
Expand All @@ -3056,33 +3087,35 @@ folly::Future<StatusOr<bool>> MetaClient::dropZone(std::string zoneName) {
return future;
}

folly::Future<StatusOr<bool>> MetaClient::addHostIntoZone(HostAddr node, std::string zoneName) {
cpp2::AddHostIntoZoneReq req;
req.set_node(node);
folly::Future<StatusOr<bool>> MetaClient::splitZone(
std::string zoneName, std::unordered_map<std::string, std::vector<HostAddr>>) {
cpp2::SplitZoneReq req;
req.set_zone_name(zoneName);

folly::Promise<StatusOr<bool>> promise;
auto future = promise.getFuture();
getResponse(
std::move(req),
[](auto client, auto request) { return client->future_addHostIntoZone(request); },
[](auto client, auto request) { return client->future_splitZone(request); },
[](cpp2::ExecResp&& resp) -> bool {
return resp.get_code() == nebula::cpp2::ErrorCode::SUCCEEDED;
},
std::move(promise));
return future;
}

folly::Future<StatusOr<bool>> MetaClient::dropHostFromZone(HostAddr node, std::string zoneName) {
cpp2::DropHostFromZoneReq req;
req.set_node(node);
folly::Future<StatusOr<bool>> MetaClient::addHostsIntoZone(std::vector<HostAddr> hosts,
std::string zoneName,
bool isNew) {
cpp2::AddHostsIntoZoneReq req;
req.set_hosts(hosts);
req.set_zone_name(zoneName);
req.set_is_new(isNew);

folly::Promise<StatusOr<bool>> promise;
auto future = promise.getFuture();
getResponse(
std::move(req),
[](auto client, auto request) { return client->future_dropHostFromZone(request); },
[](auto client, auto request) { return client->future_addHostsIntoZone(request); },
[](cpp2::ExecResp&& resp) -> bool {
return resp.get_code() == nebula::cpp2::ErrorCode::SUCCEEDED;
},
Expand Down
13 changes: 10 additions & 3 deletions src/clients/meta/MetaClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,20 @@ class MetaClient {

StatusOr<LeaderInfo> getLeaderInfo();

folly::Future<StatusOr<bool>> addZone(std::string zoneName, std::vector<HostAddr> nodes);
folly::Future<StatusOr<bool>> addHosts(std::vector<HostAddr> hosts);

folly::Future<StatusOr<bool>> dropHosts(std::vector<HostAddr> hosts);

folly::Future<StatusOr<bool>> mergeZone(std::vector<std::string> zones, std::string zoneName);

folly::Future<StatusOr<bool>> dropZone(std::string zoneName);

folly::Future<StatusOr<bool>> addHostIntoZone(HostAddr node, std::string zoneName);
folly::Future<StatusOr<bool>> splitZone(
std::string zoneName, std::unordered_map<std::string, std::vector<HostAddr>> zones);

folly::Future<StatusOr<bool>> dropHostFromZone(HostAddr node, std::string zoneName);
folly::Future<StatusOr<bool>> addHostsIntoZone(std::vector<HostAddr> hosts,
std::string zoneName,
bool isNew);

folly::Future<StatusOr<std::vector<HostAddr>>> getZone(std::string zoneName);

Expand Down
19 changes: 18 additions & 1 deletion src/common/utils/MetaKeyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace nebula {
static const std::unordered_map<std::string, std::pair<std::string, bool>> systemTableMaps = {
{"users", {"__users__", true}},
{"hosts", {"__hosts__", false}},
{"machines", {"__machines__", false}},
{"snapshots", {"__snapshots__", false}},
{"configs", {"__configs__", true}},
{"groups", {"__groups__", true}},
Expand Down Expand Up @@ -56,7 +57,8 @@ static const std::unordered_map<
// clang-format off
static const std::string kSpacesTable = tableMaps.at("spaces").first; // NOLINT
static const std::string kPartsTable = tableMaps.at("parts").first; // NOLINT
static const std::string kHostsTable = systemTableMaps.at("hosts").first; // NOLINT
static const std::string kHostsTable = systemTableMaps.at("hosts").first; // NOLINT
static const std::string kMachinesTable = systemTableMaps.at("machines").first; // NOLINT
static const std::string kTagsTable = tableMaps.at("tags").first; // NOLINT
static const std::string kEdgesTable = tableMaps.at("edges").first; // NOLINT
static const std::string kIndexesTable = tableMaps.at("indexes").first; // NOLINT
Expand Down Expand Up @@ -235,6 +237,21 @@ std::vector<HostAddr> MetaKeyUtils::parsePartValV2(folly::StringPiece val) {
return ret;
}

std::string MetaKeyUtils::machineKey(std::string addr, Port port) {
std::string key;
HostAddr h(addr, port);
key.append(kMachinesTable.data(), kMachinesTable.size())
.append(MetaKeyUtils::serializeHostAddr(h));
return key;
}

const std::string& MetaKeyUtils::machinePrefix() { return kMachinesTable; }

HostAddr MetaKeyUtils::parseMachineKey(folly::StringPiece key) {
key.advance(kMachinesTable.size());
return MetaKeyUtils::deserializeHostAddr(key);
}

std::string MetaKeyUtils::hostKey(std::string addr, Port port) { return hostKeyV2(addr, port); }

std::string MetaKeyUtils::hostKeyV2(std::string addr, Port port) {
Expand Down
6 changes: 6 additions & 0 deletions src/common/utils/MetaKeyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ class MetaKeyUtils final {

static std::vector<HostAddr> parsePartValV2(folly::StringPiece val);

static std::string machineKey(std::string ip, Port port);

static const std::string& machinePrefix();

static HostAddr parseMachineKey(folly::StringPiece key);

static std::string hostKey(std::string ip, Port port);

static std::string hostKeyV2(std::string addr, Port port);
Expand Down
7 changes: 7 additions & 0 deletions src/common/utils/test/MetaKeyUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ TEST(MetaKeyUtilsTest, storeStrIpCodecTest) {
}
}

{
// kMachinesTable : key
auto key = MetaKeyUtils::machineKey(hostnames[0], ports[0]);
auto host = MetaKeyUtils::parseMachineKey(key);
ASSERT_EQ(host.host, hostnames[0]);
ASSERT_EQ(host.port, ports[0]);
}
{
// kHostsTable : key
auto key = MetaKeyUtils::hostKey(hostnames[0], ports[0]);
Expand Down
2 changes: 2 additions & 0 deletions src/graph/executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ nebula_add_library(
algo/ProduceAllPathsExecutor.cpp
algo/CartesianProductExecutor.cpp
algo/SubgraphExecutor.cpp
admin/AddHostsExecutor.cpp
admin/DropHostsExecutor.cpp
admin/SwitchSpaceExecutor.cpp
admin/CreateUserExecutor.cpp
admin/DropUserExecutor.cpp
Expand Down
22 changes: 15 additions & 7 deletions src/graph/executor/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
#include "graph/context/ExecutionContext.h"
#include "graph/context/QueryContext.h"
#include "graph/executor/ExecutionError.h"
#include "graph/executor/admin/AddHostsExecutor.h"
#include "graph/executor/admin/ChangePasswordExecutor.h"
#include "graph/executor/admin/CharsetExecutor.h"
#include "graph/executor/admin/ConfigExecutor.h"
#include "graph/executor/admin/CreateUserExecutor.h"
#include "graph/executor/admin/DownloadExecutor.h"
#include "graph/executor/admin/DropHostsExecutor.h"
#include "graph/executor/admin/DropUserExecutor.h"
#include "graph/executor/admin/GrantRoleExecutor.h"
#include "graph/executor/admin/GroupExecutor.h"
Expand Down Expand Up @@ -431,6 +433,12 @@ Executor *Executor::makeExecutor(QueryContext *qctx, const PlanNode *node) {
case PlanNode::Kind::kSubgraph: {
return pool->add(new SubgraphExecutor(node, qctx));
}
case PlanNode::Kind::kAddHosts: {
return pool->add(new AddHostsExecutor(node, qctx));
}
case PlanNode::Kind::kDropHosts: {
return pool->add(new DropHostsExecutor(node, qctx));
}
case PlanNode::Kind::kAddGroup: {
return pool->add(new AddGroupExecutor(node, qctx));
}
Expand All @@ -449,20 +457,20 @@ Executor *Executor::makeExecutor(QueryContext *qctx, const PlanNode *node) {
case PlanNode::Kind::kShowGroups: {
return pool->add(new ListGroupsExecutor(node, qctx));
}
case PlanNode::Kind::kAddZone: {
return pool->add(new AddZoneExecutor(node, qctx));
case PlanNode::Kind::kMergeZone: {
return pool->add(new MergeZoneExecutor(node, qctx));
}
case PlanNode::Kind::kDropZone: {
return pool->add(new DropZoneExecutor(node, qctx));
}
case PlanNode::Kind::kSplitZone: {
return pool->add(new SplitZoneExecutor(node, qctx));
}
case PlanNode::Kind::kDescribeZone: {
return pool->add(new DescribeZoneExecutor(node, qctx));
}
case PlanNode::Kind::kAddHostIntoZone: {
return pool->add(new AddHostIntoZoneExecutor(node, qctx));
}
case PlanNode::Kind::kDropHostFromZone: {
return pool->add(new DropHostFromZoneExecutor(node, qctx));
case PlanNode::Kind::kAddHostsIntoZone: {
return pool->add(new AddHostsIntoZoneExecutor(node, qctx));
}
case PlanNode::Kind::kShowZones: {
return pool->add(new ListZonesExecutor(node, qctx));
Expand Down
32 changes: 32 additions & 0 deletions src/graph/executor/admin/AddHostsExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "graph/executor/admin/AddHostsExecutor.h"

#include "graph/planner/plan/Admin.h"

namespace nebula {
namespace graph {

folly::Future<Status> AddHostsExecutor::execute() {
SCOPED_TIMER(&execTime_);
auto *ahNode = asNode<AddHosts>(node());
return qctx()
->getMetaClient()
->addHosts(ahNode->getHosts())
.via(runner())
.thenValue([this](StatusOr<bool> resp) {
SCOPED_TIMER(&execTime_);
NG_RETURN_IF_ERROR(resp);
if (!resp.value()) {
return Status::Error("Add Hosts failed!");
}
return Status::OK();
});
}

} // namespace graph
} // namespace nebula
26 changes: 26 additions & 0 deletions src/graph/executor/admin/AddHostsExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef GRAPH_EXECUTOR_ADMIN_ADD_HOST_EXECUTOR_H_
#define GRAPH_EXECUTOR_ADMIN_ADD_HOST_EXECUTOR_H_

#include "graph/executor/Executor.h"

namespace nebula {
namespace graph {

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

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

} // namespace graph
} // namespace nebula

#endif // GRAPH_EXECUTOR_ADMIN_ADD_HOST_EXECUTOR_H_
31 changes: 31 additions & 0 deletions src/graph/executor/admin/DropHostsExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "graph/executor/admin/DropHostsExecutor.h"

#include "graph/planner/plan/Admin.h"

namespace nebula {
namespace graph {

folly::Future<Status> DropHostsExecutor::execute() {
auto *dhNode = asNode<DropHosts>(node());
return qctx()
->getMetaClient()
->dropHosts(dhNode->getHosts())
.via(runner())
.thenValue([this](StatusOr<bool> resp) {
SCOPED_TIMER(&execTime_);
NG_RETURN_IF_ERROR(resp);
if (!resp.value()) {
return Status::Error("Drop Hosts failed!");
}
return Status::OK();
});
}

} // namespace graph
} // namespace nebula
26 changes: 26 additions & 0 deletions src/graph/executor/admin/DropHostsExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef GRAPH_EXECUTOR_ADMIN_DROP_HOST_EXECUTOR_H_
#define GRAPH_EXECUTOR_ADMIN_DROP_HOST_EXECUTOR_H_

#include "graph/executor/Executor.h"

namespace nebula {
namespace graph {

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

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

} // namespace graph
} // namespace nebula

#endif // GRAPH_EXECUTOR_ADMIN_DROP_HOST_EXECUTOR_H_
2 changes: 1 addition & 1 deletion src/graph/executor/admin/ShowHostsExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class ShowHostsExecutor final : public Executor {
} // namespace graph
} // namespace nebula

#endif
#endif // GRAPH_EXECUTOR_ADMIN_SHOW_HOSTS_EXECUTOR_H_
Loading

0 comments on commit b3735fd

Please sign in to comment.