Coverage Report

Created: 2026-03-16 01:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_compaction_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 "cloud/cloud_compaction_action.h"
19
20
// IWYU pragma: no_include <bits/chrono.h>
21
#include <chrono> // IWYU pragma: keep
22
#include <exception>
23
#include <future>
24
#include <memory>
25
#include <mutex>
26
#include <sstream>
27
#include <string>
28
#include <thread>
29
#include <utility>
30
31
#include "absl/strings/substitute.h"
32
#include "cloud/cloud_base_compaction.h"
33
#include "cloud/cloud_compaction_action.h"
34
#include "cloud/cloud_cumulative_compaction.h"
35
#include "cloud/cloud_full_compaction.h"
36
#include "cloud/cloud_tablet.h"
37
#include "cloud/cloud_tablet_mgr.h"
38
#include "common/logging.h"
39
#include "common/metrics/doris_metrics.h"
40
#include "common/status.h"
41
#include "service/http/http_channel.h"
42
#include "service/http/http_headers.h"
43
#include "service/http/http_request.h"
44
#include "service/http/http_status.h"
45
#include "storage/compaction/base_compaction.h"
46
#include "storage/compaction/cumulative_compaction.h"
47
#include "storage/compaction/cumulative_compaction_policy.h"
48
#include "storage/compaction/cumulative_compaction_time_series_policy.h"
49
#include "storage/compaction/full_compaction.h"
50
#include "storage/olap_define.h"
51
#include "storage/storage_engine.h"
52
#include "storage/tablet/tablet_manager.h"
53
#include "util/stopwatch.hpp"
54
55
namespace doris {
56
using namespace ErrorCode;
57
58
namespace {}
59
60
const static std::string HEADER_JSON = "application/json";
61
62
CloudCompactionAction::CloudCompactionAction(CompactionActionType ctype, ExecEnv* exec_env,
63
                                             CloudStorageEngine& engine, TPrivilegeHier::type hier,
64
                                             TPrivilegeType::type ptype)
65
0
        : HttpHandlerWithAuth(exec_env, hier, ptype), _engine(engine), _compaction_type(ctype) {}
66
67
/// check param and fetch tablet_id & table_id from req
68
0
static Status _check_param(HttpRequest* req, uint64_t* tablet_id, uint64_t* table_id) {
69
    // req tablet id and table id, we have to set only one of them.
70
0
    std::string req_tablet_id = req->param(TABLET_ID_KEY);
71
0
    std::string req_table_id = req->param(TABLE_ID_KEY);
72
0
    if (req_tablet_id == "") {
73
0
        if (req_table_id == "") {
74
            // both tablet id and table id are empty, return error.
75
0
            return Status::InternalError(
76
0
                    "tablet id and table id can not be empty at the same time!");
77
0
        } else {
78
0
            try {
79
0
                *table_id = std::stoull(req_table_id);
80
0
            } catch (const std::exception& e) {
81
0
                return Status::InternalError("convert table_id failed, {}", e.what());
82
0
            }
83
0
            return Status::OK();
84
0
        }
85
0
    } else {
86
0
        if (req_table_id == "") {
87
0
            try {
88
0
                *tablet_id = std::stoull(req_tablet_id);
89
0
            } catch (const std::exception& e) {
90
0
                return Status::InternalError("convert tablet_id failed, {}", e.what());
91
0
            }
92
0
            return Status::OK();
93
0
        } else {
94
            // both tablet id and table id are not empty, return err.
95
0
            return Status::InternalError("tablet id and table id can not be set at the same time!");
96
0
        }
97
0
    }
98
0
}
99
100
/// retrieve specific id from req
101
0
static Status _check_param(HttpRequest* req, uint64_t* id_param, const std::string param_name) {
102
0
    const auto& req_id_param = req->param(param_name);
103
0
    if (!req_id_param.empty()) {
104
0
        try {
105
0
            *id_param = std::stoull(req_id_param);
106
0
        } catch (const std::exception& e) {
107
0
            return Status::InternalError("convert {} failed, {}", param_name, e.what());
108
0
        }
109
0
    }
110
111
0
    return Status::OK();
112
0
}
113
114
// for viewing the compaction status
115
0
Status CloudCompactionAction::_handle_show_compaction(HttpRequest* req, std::string* json_result) {
116
0
    uint64_t tablet_id = 0;
117
0
    RETURN_NOT_OK_STATUS_WITH_WARN(_check_param(req, &tablet_id, TABLET_ID_KEY),
118
0
                                   "check param failed");
119
0
    if (tablet_id == 0) {
120
0
        return Status::InternalError("check param failed: missing tablet_id");
121
0
    }
122
123
0
    LOG(INFO) << "begin to handle show compaction, tablet id: " << tablet_id;
124
125
    //TabletSharedPtr tablet = _engine.tablet_manager()->get_tablet(tablet_id);
126
0
    CloudTabletSPtr tablet = DORIS_TRY(_engine.tablet_mgr().get_tablet(tablet_id));
127
0
    if (tablet == nullptr) {
128
0
        return Status::NotFound("Tablet not found. tablet_id={}", tablet_id);
129
0
    }
130
131
0
    tablet->get_compaction_status(json_result);
132
0
    LOG(INFO) << "finished to handle show compaction, tablet id: " << tablet_id;
133
0
    return Status::OK();
134
0
}
135
136
0
Status CloudCompactionAction::_handle_run_compaction(HttpRequest* req, std::string* json_result) {
137
    // 1. param check
138
    // check req_tablet_id or req_table_id is not empty and can not be set together.
139
0
    uint64_t tablet_id = 0;
140
0
    uint64_t table_id = 0;
141
0
    RETURN_NOT_OK_STATUS_WITH_WARN(_check_param(req, &tablet_id, &table_id), "check param failed");
142
0
    LOG(INFO) << "begin to handle run compaction, tablet id: " << tablet_id
143
0
              << " table id: " << table_id;
144
145
    // check compaction_type equals 'base' or 'cumulative'
146
0
    auto& compaction_type = req->param(PARAM_COMPACTION_TYPE);
147
0
    if (compaction_type != PARAM_COMPACTION_BASE &&
148
0
        compaction_type != PARAM_COMPACTION_CUMULATIVE &&
149
0
        compaction_type != PARAM_COMPACTION_FULL) {
150
0
        return Status::NotSupported("The compaction type '{}' is not supported", compaction_type);
151
0
    }
152
0
    bool sync_delete_bitmap = compaction_type != PARAM_COMPACTION_FULL;
153
0
    CloudTabletSPtr tablet =
154
0
            DORIS_TRY(_engine.tablet_mgr().get_tablet(tablet_id, false, sync_delete_bitmap));
155
0
    if (tablet == nullptr) {
156
0
        return Status::NotFound("Tablet not found. tablet_id={}", tablet_id);
157
0
    }
158
159
0
    if (compaction_type == PARAM_COMPACTION_BASE) {
160
0
        tablet->set_last_base_compaction_schedule_time(UnixMillis());
161
0
    } else if (compaction_type == PARAM_COMPACTION_CUMULATIVE) {
162
0
        tablet->set_last_cumu_compaction_schedule_time(UnixMillis());
163
0
    } else if (compaction_type == PARAM_COMPACTION_FULL) {
164
0
        tablet->set_last_full_compaction_schedule_time(UnixMillis());
165
0
    }
166
167
0
    LOG(INFO) << "manual submit compaction task, tablet id: " << tablet_id
168
0
              << " table id: " << table_id;
169
    // 3. submit compaction task
170
0
    RETURN_IF_ERROR(_engine.submit_compaction_task(
171
0
            tablet, compaction_type == PARAM_COMPACTION_BASE ? CompactionType::BASE_COMPACTION
172
0
                    : compaction_type == PARAM_COMPACTION_CUMULATIVE
173
0
                            ? CompactionType::CUMULATIVE_COMPACTION
174
0
                            : CompactionType::FULL_COMPACTION));
175
176
0
    LOG(INFO) << "Manual compaction task is successfully triggered, tablet id: " << tablet_id
177
0
              << " table id: " << table_id;
178
0
    *json_result =
179
0
            R"({"status": "Success", "msg": "compaction task is successfully triggered. Table id: )" +
180
0
            std::to_string(table_id) + ". Tablet id: " + std::to_string(tablet_id) + "\"}";
181
0
    return Status::OK();
182
0
}
183
184
Status CloudCompactionAction::_handle_run_status_compaction(HttpRequest* req,
185
0
                                                            std::string* json_result) {
186
0
    uint64_t tablet_id = 0;
187
0
    RETURN_NOT_OK_STATUS_WITH_WARN(_check_param(req, &tablet_id, TABLET_ID_KEY),
188
0
                                   "check param failed");
189
0
    LOG(INFO) << "begin to handle run status compaction, tablet id: " << tablet_id;
190
191
0
    if (tablet_id == 0) {
192
        // overall compaction status
193
0
        RETURN_IF_ERROR(_engine.get_compaction_status_json(json_result));
194
0
    } else {
195
0
        std::string json_template = R"({
196
0
            "status" : "Success",
197
0
            "run_status" : $0,
198
0
            "msg" : "$1",
199
0
            "tablet_id" : $2,
200
0
            "compact_type" : "$3"
201
0
        })";
202
203
0
        std::string msg = "compaction task for this tablet is not running";
204
0
        std::string compaction_type;
205
0
        bool run_status = false;
206
207
0
        if (_engine.has_cumu_compaction(tablet_id)) {
208
0
            msg = "compaction task for this tablet is running";
209
0
            compaction_type = "cumulative";
210
0
            run_status = true;
211
0
            *json_result =
212
0
                    absl::Substitute(json_template, run_status, msg, tablet_id, compaction_type);
213
0
            return Status::OK();
214
0
        }
215
216
0
        if (_engine.has_base_compaction(tablet_id)) {
217
0
            msg = "compaction task for this tablet is running";
218
0
            compaction_type = "base";
219
0
            run_status = true;
220
0
            *json_result =
221
0
                    absl::Substitute(json_template, run_status, msg, tablet_id, compaction_type);
222
0
            return Status::OK();
223
0
        }
224
225
0
        if (_engine.has_full_compaction(tablet_id)) {
226
0
            msg = "compaction task for this tablet is running";
227
0
            compaction_type = "full";
228
0
            run_status = true;
229
0
            *json_result =
230
0
                    absl::Substitute(json_template, run_status, msg, tablet_id, compaction_type);
231
0
            return Status::OK();
232
0
        }
233
        // not running any compaction
234
0
        *json_result = absl::Substitute(json_template, run_status, msg, tablet_id, compaction_type);
235
0
    }
236
0
    LOG(INFO) << "finished to handle run status compaction, tablet id: " << tablet_id;
237
0
    return Status::OK();
238
0
}
239
240
0
void CloudCompactionAction::handle(HttpRequest* req) {
241
0
    req->add_output_header(HttpHeaders::CONTENT_TYPE, HEADER_JSON.c_str());
242
243
0
    if (_compaction_type == CompactionActionType::SHOW_INFO) {
244
0
        std::string json_result;
245
0
        Status st = _handle_show_compaction(req, &json_result);
246
0
        if (!st.ok()) {
247
0
            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
248
0
        } else {
249
0
            HttpChannel::send_reply(req, HttpStatus::OK, json_result);
250
0
        }
251
0
    } else if (_compaction_type == CompactionActionType::RUN_COMPACTION) {
252
0
        std::string json_result;
253
0
        Status st = _handle_run_compaction(req, &json_result);
254
0
        if (!st.ok()) {
255
0
            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
256
0
        } else {
257
0
            HttpChannel::send_reply(req, HttpStatus::OK, json_result);
258
0
        }
259
0
    } else {
260
0
        std::string json_result;
261
0
        Status st = _handle_run_status_compaction(req, &json_result);
262
0
        if (!st.ok()) {
263
0
            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
264
0
        } else {
265
0
            HttpChannel::send_reply(req, HttpStatus::OK, json_result);
266
0
        }
267
0
    }
268
0
}
269
270
} // end namespace doris