Skip to content

Commit

Permalink
Unifies http admin calls using infinity::admin* functions and update …
Browse files Browse the repository at this point in the history
…doc (#2006)

### What problem does this PR solve?

_unify http admin calls using infinity::admin* functions and update doc_

### Type of change

- [x] Documentation Update
- [x] Refactoring
  • Loading branch information
vsian authored Oct 10, 2024
1 parent cf832fa commit 628a1af
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 76 deletions.
47 changes: 11 additions & 36 deletions docs/references/http_api_reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,7 @@ A `500` HTTP status code indicates an error condition. The response includes a J
**POST** `/admin/node/current`
Set a node role.
Sets role for a node.
### Request
Expand All @@ -2662,8 +2662,7 @@ curl --request POST \
'{
"role" : "follower",
"name" : "following",
"address" : "0.0.0.0",
"port" : 23851
"address" : "0.0.0.0:23851"
}'
```
Expand All @@ -2677,7 +2676,6 @@ curl --request POST \
- Parameter settings for setting a node to `follower` or `learner`:
- "name" Required : Name of the node in the cluster.
- "address" Required : Peer server address of the leader in the cluster.
- "port" Required : Peer server port of the leader in the cluster.
### Response
Expand Down Expand Up @@ -2726,7 +2724,7 @@ A `500` HTTP status code indicates an error condition. The response includes a J
**GET** `/admin/variables`
Show all node variables in admin mode.
Shows all node variables in admin mode.
### Request
Expand All @@ -2748,12 +2746,7 @@ None.
### Response
<Tabs
defaultValue="s200"
values={[
{label: 'Status code 200', value: 's200'},
]}>
<TabItem value="s200">
#### Status code 200
The response includes a JSON object like the following:
Expand All @@ -2767,14 +2760,13 @@ The response includes a JSON object like the following:
- `"error_code"`: `integer`
`0`: The operation succeeds.
</Tabs>
---
## Admin show node configs
**GET** `/admin/configs`
Show all node configs in admin mode.
Shows all node configs in admin mode.
### Request
Expand Down Expand Up @@ -2836,8 +2828,6 @@ The response includes a JSON object like the following:
}
```
</Tabs>
---
## Admin show node variable
Expand Down Expand Up @@ -2913,7 +2903,7 @@ The response includes a JSON object like the following:
**GET** `/admin/node/current`
Get information about the currently connected node.
Gets information about the currently connected node.
### Request
Expand All @@ -2934,12 +2924,8 @@ curl --request GET \
None.
### Response
<Tabs
defaultValue="s200"
values={[
{label: 'Status code 200', value: 's200'},
]}>
<TabItem value="s200">
#### Status code 200
The response includes a JSON object like the following:
Expand All @@ -2955,16 +2941,13 @@ The response includes a JSON object like the following:
- `"node_role": string`
The role of querying node.
</TabItem>
</Tabs>
---
## Admin show node
**GET** `/admin/node/{node_name}`
Get information about a node in the cluster.
Gets information about a node in the cluster.
### Request
Expand Down Expand Up @@ -3033,7 +3016,7 @@ A `500` HTTP status code indicates an error condition. The response includes a J
**GET** `/admin/nodes`
List all nodes in the cluster.
Lists all nodes in the cluster.
### Request
Expand All @@ -3055,12 +3038,7 @@ None.
### Response
<Tabs
defaultValue="s200"
values={[
{label: 'Status code 200', value: 's200'},
]}>
<TabItem value="s200">
#### Status code 200
The response includes a JSON object like the following:
Expand All @@ -3080,9 +3058,6 @@ The response includes a JSON object like the following:
- `"nodes" : array` :
Each element is in `[nodename, noderole]` format.
</TabItem>
</Tabs>
---
94 changes: 54 additions & 40 deletions src/network/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3473,7 +3473,7 @@ class ShowQueriesHandler final : public HttpRequestHandler {
}
};

class ShowCurrentNodeHandler final : public HttpRequestHandler {
class AdminShowCurrentNodeHandler final : public HttpRequestHandler {
public:
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {

Expand All @@ -3488,7 +3488,7 @@ class ShowCurrentNodeHandler final : public HttpRequestHandler {
if (result.IsOk()) {
json_response["error_code"] = 0;
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get();
Value value = data_block->GetValue(1, 0);
Value value = data_block->GetValue(1, 1);
const String &variable_value = value.ToString();
json_response["role"] = variable_value;

Expand All @@ -3502,7 +3502,7 @@ class ShowCurrentNodeHandler final : public HttpRequestHandler {
}
};

class ShowNodeByNameHandler final : public HttpRequestHandler {
class AdminShowNodeByNameHandler final : public HttpRequestHandler {
public:
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
auto infinity = Infinity::RemoteConnect();
Expand All @@ -3512,24 +3512,27 @@ class ShowNodeByNameHandler final : public HttpRequestHandler {
HTTPStatus http_status;

String node_name = request->getPathVariable("node_name");
infinity::Status status;
SharedPtr<NodeInfo> nodeinfo;
std::tie(status, nodeinfo) = InfinityContext::instance().cluster_manager()->GetNodeInfoPtrByName(node_name);
if (status.ok()) {
http_status = HTTPStatus::CODE_200;
QueryResult result = infinity->AdminShowNode(node_name);

if (result.IsOk()) {
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get();
Value value = data_block->GetValue(1, 1);
const String &node_role = value.ToString();

json_response["error_code"] = ErrorCode::kOk;
json_response["node_role"] = ToString(nodeinfo->node_role_);
json_response["role"] = node_role;
http_status = HTTPStatus::CODE_200;
} else {
json_response["error_code"] = result.ErrorCode();
json_response["error_msg"] = result.ErrorMsg();
http_status = HTTPStatus::CODE_500;
json_response["error_code"] = status.code();
json_response["error_msg"] = status.message();
}

return ResponseFactory::createResponse(http_status, json_response.dump());
}
};

class ListAllNodesHandler final : public HttpRequestHandler {
class AdminListAllNodesHandler final : public HttpRequestHandler {
public:
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
auto infinity = Infinity::RemoteConnect();
Expand All @@ -3539,15 +3542,26 @@ class ListAllNodesHandler final : public HttpRequestHandler {
nlohmann::json json_response;
nlohmann::json nodes_json;

Vector<SharedPtr<NodeInfo>> nodes = InfinityContext::instance().cluster_manager()->ListNodes();
for (const auto &ptr : nodes) {
nodes_json.push_back({ptr->node_name_, ToString(ptr->node_role_)});
QueryResult result = infinity->AdminShowNodes();
if (result.IsOk()) {
SizeT block_rows = result.result_table_->DataBlockCount();
for (SizeT block_id = 0; block_id < block_rows; ++block_id) {
DataBlock *data_block = result.result_table_->GetDataBlockById(block_id).get();
auto row_count = data_block->row_count();
for (int row = 0; row < row_count; ++row) {
String node_name = data_block->GetValue(0, row).ToString();
String node_role = data_block->GetValue(1, row).ToString();
nodes_json.push_back({node_name, node_role});
}
}
http_status = HTTPStatus::CODE_200;
json_response["error_code"] = ErrorCode::kOk;
json_response["nodes"] = nodes_json;
} else {
json_response["error_code"] = result.ErrorCode();
json_response["error_message"] = result.ErrorMsg();
http_status = HTTPStatus::CODE_500;
}

http_status = HTTPStatus::CODE_200;
json_response["error_code"] = ErrorCode::kOk;
json_response["nodes"] = nodes_json;

return ResponseFactory::createResponse(http_status, json_response.dump());
}
};
Expand All @@ -3566,8 +3580,7 @@ class AdminSetNodeRoleHandler final : public HttpRequestHandler {
nlohmann::json http_body_json;
try {
http_body_json = nlohmann::json::parse(data_body);
}
catch (nlohmann::json::exception &e) {
} catch (nlohmann::json::exception &e) {
http_status = HTTPStatus::CODE_500;
json_response["error_code"] = ErrorCode::kInvalidJsonFormat;
json_response["error_message"] = e.what();
Expand All @@ -3582,32 +3595,32 @@ class AdminSetNodeRoleHandler final : public HttpRequestHandler {
}

String role = http_body_json["role"];
QueryResult result;
ToLower(role);
if (role == "uninitialized") {
status = InfinityContext::instance().ChangeRole(NodeRole::kUnInitialized);
} else if (role == "admin") {
status = InfinityContext::instance().ChangeRole(NodeRole::kAdmin);
if (role == "admin") {
result = infinity->AdminSetAdmin();
} else if (role == "standalone") {
status = InfinityContext::instance().ChangeRole(NodeRole::kStandalone);
result = infinity->AdminSetStandalone();
} else if (role == "leader") {
status = InfinityContext::instance().ChangeRole(NodeRole::kLeader, http_body_json["name"]);
result = infinity->AdminSetLeader(http_body_json["name"]);
} else if (role == "follower") {
String node_name = http_body_json["name"];
status = InfinityContext::instance().ChangeRole(NodeRole::kFollower, http_body_json["name"], http_body_json["address"], http_body_json["port"]);
result = infinity->AdminSetFollower(http_body_json["name"], http_body_json["address"]);
} else if (role == "learner") {
String node_name = http_body_json["name"];
status = InfinityContext::instance().ChangeRole(NodeRole::kLearner, http_body_json["name"], http_body_json["address"], http_body_json["port"]);
result = infinity->AdminSetLearner(http_body_json["name"], http_body_json["address"]);
} else {
status = infinity::Status::InvalidNodeRole("invalid node role");
http_status = HTTPStatus::CODE_500;
json_response["error_code"] = ErrorCode::kInvalidNodeRole;
json_response["error_code"] = "invalid node role";
return ResponseFactory::createResponse(http_status, json_response.dump());
}

if (status.ok()) {
if (result.IsOk()) {
http_status = HTTPStatus::CODE_200;
json_response["error_code"] = ErrorCode::kOk;
} else {
http_status = HTTPStatus::CODE_500;
json_response["error_code"] = status.code();
json_response["error_code"] = status.message();
json_response["error_code"] = result.ErrorCode();
json_response["error_code"] = result.ErrorMsg();
}

return ResponseFactory::createResponse(http_status, json_response.dump());
Expand All @@ -3626,7 +3639,8 @@ class AdminShowNodeVariablesHandler final : public HttpRequestHandler {
auto result = infinity->AdminShowVariables();
if (result.IsOk()) {
json_response["error_code"] = 0;
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get(); // Assume the variables output data only included in one data block
DataBlock *data_block =
result.result_table_->GetDataBlockById(0).get(); // Assume the variables output data only included in one data block
auto row_count = data_block->row_count();
for (int row = 0; row < row_count; ++row) {
// variable name
Expand Down Expand Up @@ -3806,9 +3820,9 @@ void HTTPServer::Start(const String &ip_address, u16 port) {
router->route("GET", "/admin/variables", MakeShared<AdminShowNodeVariablesHandler>());
router->route("GET", "/admin/configs", MakeShared<AdminShowNodeConfigsHandler>());
router->route("GET", "/admin/variables/{variable_name}", MakeShared<AdminShowNodeVariableHandler>());
router->route("GET", "/admin/node/current", MakeShared<ShowCurrentNodeHandler>());
router->route("GET", "/admin/node/{node_name}", MakeShared<ShowNodeByNameHandler>());
router->route("GET", "/admin/nodes", MakeShared<ListAllNodesHandler>());
router->route("GET", "/admin/node/current", MakeShared<AdminShowCurrentNodeHandler>());
router->route("GET", "/admin/node/{node_name}", MakeShared<AdminShowNodeByNameHandler>());
router->route("GET", "/admin/nodes", MakeShared<AdminListAllNodesHandler>());

SharedPtr<HttpConnectionProvider> connection_provider = HttpConnectionProvider::createShared({ip_address, port, WebAddress::IP_4});
SharedPtr<HttpConnectionHandler> connection_handler = HttpConnectionHandler::createShared(router);
Expand Down

0 comments on commit 628a1af

Please sign in to comment.