Skip to content

Commit 702fb89

Browse files
[feature](compaction) Add a http action for visibility of compaction score on each tablet
1 parent 0b43fa9 commit 702fb89

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
#include "http/action/compaction_score_action.h"
18+
19+
#include <gen_cpp/Types_types.h>
20+
#include <rapidjson/document.h>
21+
#include <rapidjson/prettywriter.h>
22+
#include <rapidjson/stringbuffer.h>
23+
24+
#include <cstdlib>
25+
#include <string>
26+
27+
#include "http/http_channel.h"
28+
#include "http/http_headers.h"
29+
#include "http/http_request.h"
30+
#include "http/http_status.h"
31+
#include "olap/olap_common.h"
32+
#include "olap/tablet_fwd.h"
33+
#include "olap/tablet_manager.h"
34+
35+
namespace doris {
36+
37+
constexpr std::string TABLET_ID = "tablet_id";
38+
constexpr std::string BASE_COMPACTION_SCORE = "base_compaction_score";
39+
constexpr std::string CUMULATIVE_COMPACTION_SCORE = "cumu_compaction_score";
40+
41+
CompactionScoreAction::CompactionScoreAction(ExecEnv* exec_env, TPrivilegeHier::type hier,
42+
TPrivilegeType::type type,
43+
StorageEngine& storage_engine)
44+
: HttpHandlerWithAuth(exec_env, hier, type), _storage_engine(storage_engine) {}
45+
46+
static rapidjson::Document jsonfy_tablet_compaction_score(
47+
const TabletSharedPtr& tablet, rapidjson::MemoryPoolAllocator<>& allocator) {
48+
rapidjson::Document node;
49+
node.SetObject();
50+
51+
rapidjson::Value tablet_id_key;
52+
tablet_id_key.SetString(TABLET_ID.c_str(), TABLET_ID.length(), allocator);
53+
rapidjson::Value tablet_id_val;
54+
auto tablet_id_str = std::to_string(tablet->tablet_id());
55+
tablet_id_val.SetString(tablet_id_str.c_str(), tablet_id_str.length(), allocator);
56+
57+
rapidjson::Value base_score_key;
58+
base_score_key.SetString(BASE_COMPACTION_SCORE.c_str(), BASE_COMPACTION_SCORE.length(),
59+
allocator);
60+
rapidjson::Value base_score_val;
61+
tablet->calc_compaction_score(CompactionType::BASE_COMPACTION,
62+
tablet->get_cumulative_compaction_policy());
63+
64+
rapidjson::Value cumu_score_key;
65+
cumu_score_key.SetString(CUMULATIVE_COMPACTION_SCORE.c_str(),
66+
CUMULATIVE_COMPACTION_SCORE.length(), allocator);
67+
rapidjson::Value cumu_score_val;
68+
tablet->calc_compaction_score(CompactionType::CUMULATIVE_COMPACTION,
69+
tablet->get_cumulative_compaction_policy());
70+
71+
node.AddMember(tablet_id_key, tablet_id_val, allocator);
72+
node.AddMember(base_score_key, base_score_val, allocator);
73+
node.AddMember(cumu_score_key, cumu_score_val, allocator);
74+
return node;
75+
}
76+
77+
void CompactionScoreAction::handle(HttpRequest* req) {
78+
req->add_output_header(HttpHeaders::CONTENT_TYPE, HttpHeaders::JsonType.data());
79+
auto tablet_id = req->param(TABLET_ID);
80+
auto* tablet_mgr = _storage_engine.tablet_manager();
81+
rapidjson::Document root;
82+
if (tablet_id.empty()) {
83+
// fetch comapction scores from all tablets
84+
// [{tablet_id: xxx, base_compaction_score: xxx, cumu_compaction_score: xxx}, ...]
85+
auto tablets = tablet_mgr->get_all_tablet();
86+
root.SetArray();
87+
auto& allocator = root.GetAllocator();
88+
for (const auto& tablet : tablets) {
89+
root.PushBack(jsonfy_tablet_compaction_score(tablet, allocator), allocator);
90+
}
91+
} else {
92+
// {tablet_id: xxx, base_compaction_score: xxx, cumu_compaction_score: xxx}
93+
auto tablet = tablet_mgr->get_tablet(std::atoll(tablet_id.c_str()));
94+
root.SetObject();
95+
root = jsonfy_tablet_compaction_score(tablet, root.GetAllocator());
96+
}
97+
rapidjson::StringBuffer str_buf;
98+
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(str_buf);
99+
root.Accept(writer);
100+
HttpChannel::send_reply(req, HttpStatus::OK, str_buf.GetString());
101+
}
102+
103+
} // namespace doris
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#pragma once
19+
20+
#include "http/http_handler_with_auth.h"
21+
#include "olap/storage_engine.h"
22+
namespace doris {
23+
24+
class CompactionScoreAction : public HttpHandlerWithAuth {
25+
public:
26+
CompactionScoreAction(ExecEnv* exec_env, TPrivilegeHier::type hier, TPrivilegeType::type type,
27+
StorageEngine& storage_engine);
28+
29+
void handle(HttpRequest* req) override;
30+
31+
private:
32+
StorageEngine& _storage_engine;
33+
};
34+
35+
} // namespace doris

be/src/service/http_service.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <event2/bufferevent.h>
2121
#include <event2/http.h>
22+
#include <gen_cpp/FrontendService_types.h>
2223

2324
#include <string>
2425
#include <vector>
@@ -37,6 +38,7 @@
3738
#include "http/action/checksum_action.h"
3839
#include "http/action/clear_cache_action.h"
3940
#include "http/action/compaction_action.h"
41+
#include "http/action/compaction_score_action.h"
4042
#include "http/action/config_action.h"
4143
#include "http/action/debug_point_action.h"
4244
#include "http/action/download_action.h"
@@ -378,6 +380,11 @@ void HttpService::register_local_handler(StorageEngine& engine) {
378380
new ShowNestedIndexFileAction(_env, TPrivilegeHier::GLOBAL, TPrivilegeType::ADMIN));
379381
_ev_http_server->register_handler(HttpMethod::GET, "/api/show_nested_index_file",
380382
show_nested_index_file_action);
383+
384+
CompactionScoreAction* compaction_score_action = _pool.add(
385+
new CompactionScoreAction(_env, TPrivilegeHier::GLOBAL, TPrivilegeType::ADMIN, engine));
386+
_ev_http_server->register_handler(HttpMethod::GET, "/api/compaction_score",
387+
compaction_score_action);
381388
}
382389

383390
void HttpService::register_cloud_handler(CloudStorageEngine& engine) {

0 commit comments

Comments
 (0)