Coverage Report

Created: 2026-03-15 01:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/action/tablets_distribution_action.cpp
Line
Count
Source
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
#include "service/http/action/tablets_distribution_action.h"
19
20
#include <glog/logging.h>
21
22
#include <exception>
23
#include <map>
24
#include <ostream>
25
#include <string>
26
#include <utility>
27
#include <vector>
28
29
#include "absl/strings/substitute.h"
30
#include "common/status.h"
31
#include "service/backend_options.h"
32
#include "service/http/http_channel.h"
33
#include "service/http/http_headers.h"
34
#include "service/http/http_request.h"
35
#include "service/http/http_status.h"
36
#include "storage/data_dir.h"
37
#include "storage/olap_common.h"
38
#include "storage/storage_engine.h"
39
#include "storage/tablet/tablet_manager.h"
40
41
namespace doris {
42
43
const static std::string HEADER_JSON = "application/json";
44
45
TabletsDistributionAction::TabletsDistributionAction(ExecEnv* exec_env, StorageEngine& engine,
46
                                                     TPrivilegeHier::type hier,
47
                                                     TPrivilegeType::type type)
48
0
        : HttpHandlerWithAuth(exec_env, hier, type), _engine(engine) {
49
0
    _host = BackendOptions::get_localhost();
50
0
}
51
52
0
void TabletsDistributionAction::handle(HttpRequest* req) {
53
0
    req->add_output_header(HttpHeaders::CONTENT_TYPE, HEADER_JSON.c_str());
54
55
0
    std::string req_group_method = req->param("group_by");
56
0
    if (req_group_method == "partition") {
57
0
        std::string req_partition_id = req->param("partition_id");
58
0
        uint64_t partition_id = 0;
59
0
        if (!req_partition_id.empty()) {
60
0
            try {
61
0
                partition_id = std::stoull(req_partition_id);
62
0
            } catch (const std::exception& e) {
63
0
                Status status = Status::InternalError("invalid argument: {}, reason:{}",
64
0
                                                      req_partition_id, e.what());
65
0
                std::string status_result = status.to_json();
66
0
                HttpChannel::send_reply(req, HttpStatus::INTERNAL_SERVER_ERROR, status_result);
67
0
                return;
68
0
            }
69
0
        }
70
0
        HttpChannel::send_reply(
71
0
                req, HttpStatus::OK,
72
0
                get_tablets_distribution_group_by_partition(partition_id).ToString());
73
0
        return;
74
0
    }
75
0
    LOG(WARNING) << "invalid argument. group_by:" << req_group_method;
76
0
    Status status = Status::InternalError(absl::Substitute("invalid argument: group_by"));
77
0
    std::string status_result = status.to_json();
78
0
    HttpChannel::send_reply(req, HttpStatus::INTERNAL_SERVER_ERROR, status_result);
79
0
}
80
81
EasyJson TabletsDistributionAction::get_tablets_distribution_group_by_partition(
82
0
        uint64_t partition_id) {
83
0
    std::map<int64_t, std::map<DataDir*, int64_t>> tablets_num_on_disk;
84
0
    std::map<int64_t, std::map<DataDir*, std::vector<TabletSize>>> tablets_info_on_disk;
85
0
    TabletManager* tablet_manager = _engine.tablet_manager();
86
0
    tablet_manager->get_tablets_distribution_on_different_disks(tablets_num_on_disk,
87
0
                                                                tablets_info_on_disk);
88
89
0
    EasyJson tablets_distribution_ej;
90
0
    tablets_distribution_ej["msg"] = "OK";
91
0
    tablets_distribution_ej["code"] = 0;
92
0
    EasyJson data = tablets_distribution_ej.Set("data", EasyJson::kObject);
93
0
    data["host"] = _host;
94
0
    EasyJson tablets_distribution = data.Set("tablets_distribution", EasyJson::kArray);
95
0
    int64_t tablet_total_number = 0;
96
0
    for (auto& [part_id, disk_tablets_num] : tablets_num_on_disk) {
97
0
        if (partition_id != 0 && partition_id != part_id) {
98
0
            continue;
99
0
        }
100
0
        EasyJson partition = tablets_distribution.PushBack(EasyJson::kObject);
101
0
        partition["partition_id"] = part_id;
102
0
        EasyJson disks = partition.Set("disks", EasyJson::kArray);
103
0
        for (auto& [data_dir, tablets_num] : disk_tablets_num) {
104
0
            EasyJson disk = disks.PushBack(EasyJson::kObject);
105
0
            disk["disk_path"] = data_dir->path();
106
0
            disk["tablets_num"] = tablets_num;
107
0
            tablet_total_number += tablets_num;
108
0
            if (partition_id != 0) {
109
0
                EasyJson tablets = disk.Set("tablets", EasyJson::kArray);
110
0
                for (auto& i : tablets_info_on_disk[part_id][data_dir]) {
111
0
                    EasyJson tablet = tablets.PushBack(EasyJson::kObject);
112
0
                    tablet["tablet_id"] = i.tablet_id;
113
0
                    tablet["tablet_size"] = i.tablet_size;
114
0
                }
115
0
            }
116
0
        }
117
0
    }
118
0
    tablets_distribution_ej["count"] = tablet_total_number;
119
0
    return tablets_distribution_ej;
120
0
}
121
122
} // namespace doris