Coverage Report

Created: 2026-04-11 00:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/action/delete_bitmap_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/delete_bitmap_action.h"
19
20
#include <rapidjson/document.h>
21
#include <rapidjson/encodings.h>
22
#include <rapidjson/prettywriter.h>
23
#include <rapidjson/rapidjson.h>
24
#include <rapidjson/stringbuffer.h>
25
26
#include <chrono> // IWYU pragma: keep
27
#include <exception>
28
#include <future>
29
#include <memory>
30
#include <mutex>
31
#include <sstream>
32
#include <string>
33
#include <thread>
34
#include <utility>
35
36
#include "cloud/cloud_meta_mgr.h"
37
#include "cloud/cloud_storage_engine.h"
38
#include "cloud/cloud_tablet.h"
39
#include "cloud/cloud_tablet_mgr.h"
40
#include "cloud/config.h"
41
#include "common/logging.h"
42
#include "common/metrics/doris_metrics.h"
43
#include "common/status.h"
44
#include "service/http/http_channel.h"
45
#include "service/http/http_headers.h"
46
#include "service/http/http_request.h"
47
#include "service/http/http_status.h"
48
#include "storage/olap_define.h"
49
#include "storage/tablet/tablet_manager.h"
50
#include "util/stopwatch.hpp"
51
52
namespace doris {
53
using namespace ErrorCode;
54
55
namespace {
56
57
constexpr std::string_view HEADER_JSON = "application/json";
58
59
} // namespace
60
61
DeleteBitmapAction::DeleteBitmapAction(DeleteBitmapActionType ctype, ExecEnv* exec_env,
62
                                       BaseStorageEngine& engine, TPrivilegeHier::type hier,
63
                                       TPrivilegeType::type ptype)
64
0
        : HttpHandlerWithAuth(exec_env, hier, ptype),
65
0
          _engine(engine),
66
0
          _delete_bitmap_action_type(ctype) {}
67
68
0
static Status _check_param(HttpRequest* req, uint64_t* tablet_id, bool* verbose) {
69
0
    const auto& req_tablet_id = req->param(TABLET_ID_KEY);
70
0
    if (req_tablet_id.empty()) {
71
0
        return Status::InternalError<false>("tablet id is empty!");
72
0
    }
73
0
    try {
74
0
        *tablet_id = std::stoull(req_tablet_id);
75
0
    } catch (const std::exception& e) {
76
0
        return Status::InternalError<false>("convert tablet_id failed, {}", e.what());
77
0
    }
78
0
    if (*tablet_id == 0) {
79
0
        return Status::InternalError<false>("check param failed: invalid tablet_id");
80
0
    }
81
0
    *verbose = iequal(req->param("verbose"), "true");
82
0
    return Status::OK();
83
0
}
84
85
0
static void _show_delete_bitmap(DeleteBitmap& dm, bool verbose, std::string* json_result) {
86
0
    auto count = dm.get_delete_bitmap_count();
87
0
    auto cardinality = dm.cardinality();
88
0
    auto size = dm.get_size();
89
0
    rapidjson::Document root;
90
0
    root.SetObject();
91
0
    root.AddMember("delete_bitmap_count", count, root.GetAllocator());
92
0
    root.AddMember("cardinality", cardinality, root.GetAllocator());
93
0
    root.AddMember("size", size, root.GetAllocator());
94
0
    if (verbose) {
95
0
        std::string pre_rowset_id;
96
0
        int64_t pre_segment_id = -1;
97
0
        std::vector<std::string> version_vector;
98
0
        rapidjson::Document dm_arr;
99
0
        dm_arr.SetObject();
100
101
0
        auto add_rowset_delete_bitmap_info = [&]() {
102
0
            std::string key =
103
0
                    "rowset: " + pre_rowset_id + ", segment: " + std::to_string(pre_segment_id);
104
0
            rapidjson::Value key_value;
105
0
            key_value.SetString(key.data(), cast_set<uint32_t>(key.length()), root.GetAllocator());
106
0
            rapidjson::Document version_arr;
107
0
            version_arr.SetArray();
108
0
            for (const auto& str : version_vector) {
109
0
                rapidjson::Value value;
110
0
                value.SetString(str.c_str(), cast_set<uint32_t>(str.length()), root.GetAllocator());
111
0
                version_arr.PushBack(value, root.GetAllocator());
112
0
            }
113
0
            dm_arr.AddMember(key_value, version_arr, root.GetAllocator());
114
0
            version_vector.clear();
115
0
        };
116
117
0
        for (auto& [id, bitmap] : dm.delete_bitmap) {
118
0
            auto& [rowset_id, segment_id, version] = id;
119
0
            if (rowset_id.to_string() != pre_rowset_id || segment_id != pre_segment_id) {
120
                // add previous result
121
0
                if (!pre_rowset_id.empty()) {
122
0
                    add_rowset_delete_bitmap_info();
123
0
                }
124
0
                pre_rowset_id = rowset_id.to_string();
125
0
                pre_segment_id = segment_id;
126
0
            }
127
0
            std::string str = fmt::format("v: {}, c: {}, s: {}", version, bitmap.cardinality(),
128
0
                                          bitmap.getSizeInBytes());
129
0
            version_vector.push_back(str);
130
0
        }
131
        // add last result
132
0
        if (!version_vector.empty()) {
133
0
            add_rowset_delete_bitmap_info();
134
0
        }
135
0
        root.AddMember("delete_bitmap", dm_arr, root.GetAllocator());
136
0
    }
137
138
    // to json string
139
0
    rapidjson::StringBuffer strbuf;
140
0
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
141
0
    root.Accept(writer);
142
0
    *json_result = std::string(strbuf.GetString());
143
0
}
144
145
Status DeleteBitmapAction::_handle_show_local_delete_bitmap_count(HttpRequest* req,
146
0
                                                                  std::string* json_result) {
147
0
    uint64_t tablet_id = 0;
148
0
    bool verbose = false;
149
0
    RETURN_NOT_OK_STATUS_WITH_WARN(_check_param(req, &tablet_id, &verbose), "check param failed");
150
151
0
    BaseTabletSPtr tablet = nullptr;
152
0
    if (config::is_cloud_mode()) {
153
0
        tablet = DORIS_TRY(_engine.to_cloud().tablet_mgr().get_tablet(tablet_id));
154
0
        DBUG_EXECUTE_IF(
155
0
                "DeleteBitmapAction._handle_show_local_delete_bitmap_count.vacuum_stale_rowsets",
156
0
                { _engine.to_cloud().tablet_mgr().vacuum_stale_rowsets(CountDownLatch(1)); });
157
0
    } else {
158
0
        tablet = _engine.to_local().tablet_manager()->get_tablet(tablet_id);
159
0
        DBUG_EXECUTE_IF(
160
0
                "DeleteBitmapAction._handle_show_local_delete_bitmap_count.start_delete_unused_"
161
0
                "rowset",
162
0
                { _engine.to_local().start_delete_unused_rowset(); });
163
0
    }
164
0
    if (tablet == nullptr) {
165
0
        return Status::NotFound("Tablet not found. tablet_id={}", tablet_id);
166
0
    }
167
0
    auto dm = tablet->tablet_meta()->delete_bitmap().snapshot();
168
0
    _show_delete_bitmap(dm, verbose, json_result);
169
0
    return Status::OK();
170
0
}
171
172
Status DeleteBitmapAction::_handle_show_ms_delete_bitmap_count(HttpRequest* req,
173
0
                                                               std::string* json_result) {
174
0
    uint64_t tablet_id = 0;
175
0
    bool verbose = false;
176
0
    RETURN_NOT_OK_STATUS_WITH_WARN(_check_param(req, &tablet_id, &verbose), "check param failed");
177
178
0
    TabletMetaSharedPtr tablet_meta;
179
0
    auto st = _engine.to_cloud().meta_mgr().get_tablet_meta(tablet_id, &tablet_meta);
180
0
    if (!st.ok()) {
181
0
        LOG(WARNING) << "failed to get_tablet_meta for tablet=" << tablet_id
182
0
                     << ", st=" << st.to_string();
183
0
        return st;
184
0
    }
185
0
    auto tablet = std::make_shared<CloudTablet>(_engine.to_cloud(), std::move(tablet_meta));
186
0
    SyncOptions options;
187
0
    options.warmup_delta_data = false;
188
0
    options.sync_delete_bitmap = true;
189
0
    options.full_sync = true;
190
0
    st = _engine.to_cloud().meta_mgr().sync_tablet_rowsets(tablet.get(), options);
191
0
    if (!st.ok()) {
192
0
        LOG(WARNING) << "failed to sync tablet=" << tablet_id << ", st=" << st;
193
0
        return st;
194
0
    }
195
0
    auto dm = tablet->tablet_meta()->delete_bitmap().snapshot();
196
0
    _show_delete_bitmap(dm, verbose, json_result);
197
0
    return Status::OK();
198
0
}
199
200
Status DeleteBitmapAction::_handle_show_agg_cache_delete_bitmap_count(HttpRequest* req,
201
0
                                                                      std::string* json_result) {
202
0
    uint64_t tablet_id = 0;
203
0
    bool verbose = false;
204
0
    RETURN_NOT_OK_STATUS_WITH_WARN(_check_param(req, &tablet_id, &verbose), "check param failed");
205
0
    BaseTabletSPtr tablet = nullptr;
206
0
    if (config::is_cloud_mode()) {
207
0
        tablet = DORIS_TRY(_engine.to_cloud().tablet_mgr().get_tablet(tablet_id));
208
0
        DBUG_EXECUTE_IF(
209
0
                "DeleteBitmapAction._handle_show_local_delete_bitmap_count.vacuum_stale_rowsets",
210
0
                { _engine.to_cloud().tablet_mgr().vacuum_stale_rowsets(CountDownLatch(1)); });
211
0
    } else {
212
0
        tablet = _engine.to_local().tablet_manager()->get_tablet(tablet_id);
213
0
        DBUG_EXECUTE_IF(
214
0
                "DeleteBitmapAction._handle_show_local_delete_bitmap_count.start_delete_unused_"
215
0
                "rowset",
216
0
                { _engine.to_local().start_delete_unused_rowset(); });
217
0
    }
218
0
    if (tablet == nullptr) {
219
0
        return Status::NotFound("Tablet not found. tablet_id={}", tablet_id);
220
0
    }
221
0
    auto dbm = tablet->tablet_meta()->delete_bitmap().agg_cache_snapshot();
222
0
    _show_delete_bitmap(dbm, verbose, json_result);
223
0
    return Status::OK();
224
0
}
225
226
0
void DeleteBitmapAction::handle(HttpRequest* req) {
227
0
    req->add_output_header(HttpHeaders::CONTENT_TYPE, HEADER_JSON.data());
228
0
    if (_delete_bitmap_action_type == DeleteBitmapActionType::COUNT_LOCAL) {
229
0
        std::string json_result;
230
0
        Status st = _handle_show_local_delete_bitmap_count(req, &json_result);
231
0
        if (!st.ok()) {
232
0
            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
233
0
        } else {
234
0
            HttpChannel::send_reply(req, HttpStatus::OK, json_result);
235
0
        }
236
0
    } else if (_delete_bitmap_action_type == DeleteBitmapActionType::COUNT_MS) {
237
0
        std::string json_result;
238
0
        Status st = _handle_show_ms_delete_bitmap_count(req, &json_result);
239
0
        if (!st.ok()) {
240
0
            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
241
0
        } else {
242
0
            HttpChannel::send_reply(req, HttpStatus::OK, json_result);
243
0
        }
244
0
    } else if (_delete_bitmap_action_type == DeleteBitmapActionType::COUNT_AGG_CACHE) {
245
0
        std::string json_result;
246
0
        Status st = _handle_show_agg_cache_delete_bitmap_count(req, &json_result);
247
0
        if (!st.ok()) {
248
0
            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
249
0
        } else {
250
0
            HttpChannel::send_reply(req, HttpStatus::OK, json_result);
251
0
        }
252
0
    }
253
0
}
254
255
} // namespace doris