diff --git a/be/src/http/action/compaction_score_action.cpp b/be/src/http/action/compaction_score_action.cpp new file mode 100644 index 00000000000000..946f344ed4f3f8 --- /dev/null +++ b/be/src/http/action/compaction_score_action.cpp @@ -0,0 +1,103 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +#include "http/action/compaction_score_action.h" + +#include +#include +#include +#include + +#include +#include + +#include "http/http_channel.h" +#include "http/http_headers.h" +#include "http/http_request.h" +#include "http/http_status.h" +#include "olap/olap_common.h" +#include "olap/tablet_fwd.h" +#include "olap/tablet_manager.h" + +namespace doris { + +constexpr std::string TABLET_ID = "tablet_id"; +constexpr std::string BASE_COMPACTION_SCORE = "base_compaction_score"; +constexpr std::string CUMULATIVE_COMPACTION_SCORE = "cumu_compaction_score"; + +CompactionScoreAction::CompactionScoreAction(ExecEnv* exec_env, TPrivilegeHier::type hier, + TPrivilegeType::type type, + StorageEngine& storage_engine) + : HttpHandlerWithAuth(exec_env, hier, type), _storage_engine(storage_engine) {} + +static rapidjson::Document jsonfy_tablet_compaction_score( + const TabletSharedPtr& tablet, rapidjson::MemoryPoolAllocator<>& allocator) { + rapidjson::Document node; + node.SetObject(); + + rapidjson::Value tablet_id_key; + tablet_id_key.SetString(TABLET_ID.c_str(), TABLET_ID.length(), allocator); + rapidjson::Value tablet_id_val; + auto tablet_id_str = std::to_string(tablet->tablet_id()); + tablet_id_val.SetString(tablet_id_str.c_str(), tablet_id_str.length(), allocator); + + rapidjson::Value base_score_key; + base_score_key.SetString(BASE_COMPACTION_SCORE.c_str(), BASE_COMPACTION_SCORE.length(), + allocator); + rapidjson::Value base_score_val; + tablet->calc_compaction_score(CompactionType::BASE_COMPACTION, + tablet->get_cumulative_compaction_policy()); + + rapidjson::Value cumu_score_key; + cumu_score_key.SetString(CUMULATIVE_COMPACTION_SCORE.c_str(), + CUMULATIVE_COMPACTION_SCORE.length(), allocator); + rapidjson::Value cumu_score_val; + tablet->calc_compaction_score(CompactionType::CUMULATIVE_COMPACTION, + tablet->get_cumulative_compaction_policy()); + + node.AddMember(tablet_id_key, tablet_id_val, allocator); + node.AddMember(base_score_key, base_score_val, allocator); + node.AddMember(cumu_score_key, cumu_score_val, allocator); + return node; +} + +void CompactionScoreAction::handle(HttpRequest* req) { + req->add_output_header(HttpHeaders::CONTENT_TYPE, HttpHeaders::JsonType.data()); + auto tablet_id = req->param(TABLET_ID); + auto* tablet_mgr = _storage_engine.tablet_manager(); + rapidjson::Document root; + if (tablet_id.empty()) { + // fetch comapction scores from all tablets + // [{tablet_id: xxx, base_compaction_score: xxx, cumu_compaction_score: xxx}, ...] + auto tablets = tablet_mgr->get_all_tablet(); + root.SetArray(); + auto& allocator = root.GetAllocator(); + for (const auto& tablet : tablets) { + root.PushBack(jsonfy_tablet_compaction_score(tablet, allocator), allocator); + } + } else { + // {tablet_id: xxx, base_compaction_score: xxx, cumu_compaction_score: xxx} + auto tablet = tablet_mgr->get_tablet(std::atoll(tablet_id.c_str())); + root.SetObject(); + root = jsonfy_tablet_compaction_score(tablet, root.GetAllocator()); + } + rapidjson::StringBuffer str_buf; + rapidjson::PrettyWriter writer(str_buf); + root.Accept(writer); + HttpChannel::send_reply(req, HttpStatus::OK, str_buf.GetString()); +} + +} // namespace doris diff --git a/be/src/http/action/compaction_score_action.h b/be/src/http/action/compaction_score_action.h new file mode 100644 index 00000000000000..c3f91e35cd1d60 --- /dev/null +++ b/be/src/http/action/compaction_score_action.h @@ -0,0 +1,35 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include "http/http_handler_with_auth.h" +#include "olap/storage_engine.h" +namespace doris { + +class CompactionScoreAction : public HttpHandlerWithAuth { +public: + CompactionScoreAction(ExecEnv* exec_env, TPrivilegeHier::type hier, TPrivilegeType::type type, + StorageEngine& storage_engine); + + void handle(HttpRequest* req) override; + +private: + StorageEngine& _storage_engine; +}; + +} // namespace doris diff --git a/be/src/service/http_service.cpp b/be/src/service/http_service.cpp index b851302aaa830e..d447e480a51e14 100644 --- a/be/src/service/http_service.cpp +++ b/be/src/service/http_service.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -37,6 +38,7 @@ #include "http/action/checksum_action.h" #include "http/action/clear_cache_action.h" #include "http/action/compaction_action.h" +#include "http/action/compaction_score_action.h" #include "http/action/config_action.h" #include "http/action/debug_point_action.h" #include "http/action/download_action.h" @@ -378,6 +380,11 @@ void HttpService::register_local_handler(StorageEngine& engine) { new ShowNestedIndexFileAction(_env, TPrivilegeHier::GLOBAL, TPrivilegeType::ADMIN)); _ev_http_server->register_handler(HttpMethod::GET, "/api/show_nested_index_file", show_nested_index_file_action); + + CompactionScoreAction* compaction_score_action = _pool.add( + new CompactionScoreAction(_env, TPrivilegeHier::GLOBAL, TPrivilegeType::ADMIN, engine)); + _ev_http_server->register_handler(HttpMethod::GET, "/api/compaction_score", + compaction_score_action); } void HttpService::register_cloud_handler(CloudStorageEngine& engine) {