Skip to content

Commit f9bcc48

Browse files
authored
HTTP APIs : SHOW QUERY, SHOW TRANSACTIONS, SHOW TRANSACTION (#2007)
### What problem does this PR solve? New HTTP APIs of: - SHOW QUERY {QUERY_ID} (not documented) - SHOW TRANSACTIONS - SHOW TRANSACTION Issue link: #1937 ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Documentation Update --------- Signed-off-by: vsian <[email protected]>
1 parent cfbc37b commit f9bcc48

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

docs/references/http_api_reference.mdx

+143
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,150 @@ A `500` HTTP status code indicates an error condition. The response includes a J
26352635
26362636
---
26372637
2638+
## Show transactions
26382639
2640+
**GET** `/instance/transactions`
2641+
2642+
Shows running transactions on database.
2643+
2644+
### Request
2645+
2646+
- Method: GET
2647+
- URL: `/instance/transactions`
2648+
- Headers: `accept: application/json`
2649+
2650+
#### Request example
2651+
2652+
```shell
2653+
curl --request GET \
2654+
--url http://localhost:23820/instance/transactions \
2655+
--header 'accept: application/json'
2656+
```
2657+
2658+
### Response
2659+
2660+
<Tabs
2661+
defaultValue="s200"
2662+
values={[
2663+
{label: 'Status code 200', value: 's200'},
2664+
{label: 'Status code 500', value: 's500'},
2665+
]}>
2666+
<TabItem value="s200">
2667+
2668+
The response includes a JSON object like the following:
2669+
2670+
```shell
2671+
{
2672+
"error_code":0,
2673+
"transactions": [
2674+
{
2675+
"transaction_id":"27275",
2676+
"transaction_text":""
2677+
},
2678+
{
2679+
"transaction_id":"27274",
2680+
"transaction_text":""
2681+
}
2682+
]
2683+
}
2684+
```
2685+
2686+
- `"error_code"`: `integer`
2687+
`0`: The operation succeeds.
2688+
2689+
</TabItem>
2690+
<TabItem value="s500">
2691+
2692+
A `500` HTTP status code indicates an error condition. The response includes a JSON object like the following:
2693+
2694+
```shell
2695+
{
2696+
"error_code": 2005,
2697+
"error_message": "Not support in maintenance mode"
2698+
}
2699+
```
2700+
2701+
- `"error_code"`: `integer`
2702+
A non-zero value indicates a specific error condition.
2703+
- `"error_message"`: `string`
2704+
When `error_code` is non-zero, `"error_message"` provides additional details about the error.
2705+
2706+
</TabItem>
2707+
</Tabs>
2708+
2709+
---
2710+
2711+
## Show transaction
2712+
2713+
**GET** `/instance/transactions/{trasaction_id}`
2714+
2715+
Shows running transactions on database by transaction_id.
2716+
2717+
### Request
2718+
2719+
- Method: GET
2720+
- URL: `/instance/transactions`
2721+
- Headers: `accept: application/json`
2722+
2723+
#### Request example
2724+
2725+
```shell
2726+
curl --request GET \
2727+
--url http://localhost:23820/instance/transactions/{transaction_id} \
2728+
--header 'accept: application/json'
2729+
```
2730+
2731+
#### Request parameters
2732+
2733+
- `transaction_id`: (*Path parameter*)
2734+
The id of the running transaction.
2735+
2736+
### Response
2737+
2738+
<Tabs
2739+
defaultValue="s200"
2740+
values={[
2741+
{label: 'Status code 200', value: 's200'},
2742+
{label: 'Status code 500', value: 's500'},
2743+
]}>
2744+
<TabItem value="s200">
2745+
2746+
The response includes a JSON object like the following:
2747+
2748+
```shell
2749+
{
2750+
"error_code":0,
2751+
"transaction": {
2752+
"transaction_id":"27275",
2753+
"transaction_text":""
2754+
}
2755+
}
2756+
```
2757+
2758+
- `"error_code"`: `integer`
2759+
`0`: The operation succeeds.
2760+
2761+
</TabItem>
2762+
<TabItem value="s500">
2763+
2764+
A `500` HTTP status code indicates an error condition. The response includes a JSON object like the following:
2765+
2766+
```shell
2767+
{
2768+
"error_code": 2005,
2769+
"error_message": "Not support in maintenance mode"
2770+
}
2771+
```
2772+
2773+
- `"error_code"`: `integer`
2774+
A non-zero value indicates a specific error condition.
2775+
- `"error_message"`: `string`
2776+
When `error_code` is non-zero, `"error_message"` provides additional details about the error.
2777+
2778+
</TabItem>
2779+
</Tabs>
2780+
2781+
---
26392782
26402783
## Admin set node role
26412784

src/network/http_server.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,116 @@ class ShowFullCheckpointHandler final : public HttpRequestHandler {
35903590
}
35913591
};
35923592

3593+
3594+
class ShowQueryHandler final : public HttpRequestHandler {
3595+
public:
3596+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
3597+
auto infinity = Infinity::RemoteConnect();
3598+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
3599+
3600+
nlohmann::json json_response;
3601+
nlohmann::json json_table;
3602+
HTTPStatus http_status;
3603+
String query_id = request->getPathVariable("query_id");
3604+
QueryResult result = infinity->Query(fmt::format("show query {}", query_id));
3605+
3606+
if (result.IsOk()) {
3607+
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get();
3608+
auto column_cnt = result.result_table_->ColumnCount();
3609+
for (SizeT col = 0; col < column_cnt; ++col) {
3610+
const String &column_name = result.result_table_->GetColumnNameById(col);
3611+
Value value = data_block->GetValue(col, 0);
3612+
const String &column_value = value.ToString();
3613+
json_table[column_name] = column_value;
3614+
}
3615+
json_response["query"] = json_table;
3616+
json_response["error_code"] = 0;
3617+
http_status = HTTPStatus::CODE_200;
3618+
} else {
3619+
json_response["error_code"] = result.ErrorCode();
3620+
json_response["error_message"] = result.ErrorMsg();
3621+
http_status = HTTPStatus::CODE_500;
3622+
}
3623+
3624+
return ResponseFactory::createResponse(http_status, json_response.dump());
3625+
}
3626+
};
3627+
3628+
class ShowTransactionsHandler final : public HttpRequestHandler {
3629+
public:
3630+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
3631+
auto infinity = Infinity::RemoteConnect();
3632+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
3633+
3634+
nlohmann::json json_response;
3635+
HTTPStatus http_status;
3636+
QueryResult result = infinity->Query("show transactions");
3637+
3638+
if (result.IsOk()) {
3639+
SizeT block_rows = result.result_table_->DataBlockCount();
3640+
for (SizeT block_id = 0; block_id < block_rows; ++block_id) {
3641+
DataBlock *data_block = result.result_table_->GetDataBlockById(block_id).get();
3642+
auto row_count = data_block->row_count();
3643+
auto column_cnt = result.result_table_->ColumnCount();
3644+
for (int row = 0; row < row_count; ++row) {
3645+
nlohmann::json json_table;
3646+
for (SizeT col = 0; col < column_cnt; ++col) {
3647+
const String &column_name = result.result_table_->GetColumnNameById(col);
3648+
Value value = data_block->GetValue(col, row);
3649+
const String &column_value = value.ToString();
3650+
json_table[column_name] = column_value;
3651+
}
3652+
json_response["transactions"].push_back(json_table);
3653+
}
3654+
}
3655+
json_response["error_code"] = 0;
3656+
http_status = HTTPStatus::CODE_200;
3657+
} else {
3658+
json_response["error_code"] = result.ErrorCode();
3659+
json_response["error_message"] = result.ErrorMsg();
3660+
http_status = HTTPStatus::CODE_500;
3661+
}
3662+
3663+
return ResponseFactory::createResponse(http_status, json_response.dump());
3664+
}
3665+
};
3666+
3667+
class ShowTransactionHandler final : public HttpRequestHandler {
3668+
public:
3669+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
3670+
auto infinity = Infinity::RemoteConnect();
3671+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
3672+
3673+
nlohmann::json json_response;
3674+
nlohmann::json json_table;
3675+
HTTPStatus http_status;
3676+
String transaction_id = request->getPathVariable("transaction_id");
3677+
QueryResult result = infinity->Query(fmt::format("show transaction {}", transaction_id));
3678+
3679+
if (result.IsOk()) {
3680+
DataBlock *data_block = result.result_table_->GetDataBlockById(0).get();
3681+
auto column_cnt = result.result_table_->ColumnCount();
3682+
for (SizeT col = 0; col < column_cnt; ++col) {
3683+
const String &column_name = result.result_table_->GetColumnNameById(col);
3684+
Value value = data_block->GetValue(col, 0);
3685+
const String &column_value = value.ToString();
3686+
json_table[column_name] = column_value;
3687+
}
3688+
json_response["error_code"] = 0;
3689+
json_response["transaction"]= json_table;
3690+
http_status = HTTPStatus::CODE_200;
3691+
} else {
3692+
json_response["error_code"] = result.ErrorCode();
3693+
json_response["error_message"] = result.ErrorMsg();
3694+
http_status = HTTPStatus::CODE_500;
3695+
}
3696+
3697+
return ResponseFactory::createResponse(http_status, json_response.dump());
3698+
}
3699+
};
3700+
3701+
3702+
35933703
class AdminShowCurrentNodeHandler final : public HttpRequestHandler {
35943704
public:
35953705
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
@@ -3926,6 +4036,9 @@ void HTTPServer::Start(const String &ip_address, u16 port) {
39264036
router->route("GET", "/instance/logs", MakeShared<ShowLogsHandler>());
39274037
router->route("GET", "/instance/delta_checkpoint", MakeShared<ShowDeltaCheckpointHandler>());
39284038
router->route("GET", "/instance/global_checkpoint", MakeShared<ShowFullCheckpointHandler>());
4039+
router->route("GET", "/instance/queries/{query_id}", MakeShared<ShowQueryHandler>());
4040+
router->route("GET", "/instance/transactions", MakeShared<ShowTransactionsHandler>());
4041+
router->route("GET", "/instance/transactions/{transaction_id}", MakeShared<ShowTransactionHandler>());
39294042

39304043
// variable
39314044
router->route("GET", "/variables/global", MakeShared<ShowGlobalVariablesHandler>());

0 commit comments

Comments
 (0)