|
| 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 |
0 commit comments