Coverage Report

Created: 2026-03-15 08:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_base_compaction.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_base_compaction.h"
19
20
#include <gen_cpp/cloud.pb.h>
21
22
#include <boost/container_hash/hash.hpp>
23
24
#include "cloud/cloud_meta_mgr.h"
25
#include "cloud/config.h"
26
#include "common/config.h"
27
#include "core/value/vdatetime_value.h"
28
#include "cpp/sync_point.h"
29
#include "service/backend_options.h"
30
#include "storage/compaction/compaction.h"
31
#include "storage/task/engine_checksum_task.h"
32
#include "util/thread.h"
33
#include "util/uuid_generator.h"
34
35
namespace doris {
36
using namespace ErrorCode;
37
38
bvar::Adder<uint64_t> base_output_size("base_compaction", "output_size");
39
bvar::Adder<uint64_t> base_input_cached_size("base_compaction", "input_cached_size");
40
bvar::Adder<uint64_t> base_input_size("base_compaction", "input_size");
41
bvar::LatencyRecorder g_base_compaction_hold_delete_bitmap_lock_time_ms(
42
        "base_compaction_hold_delete_bitmap_lock_time_ms");
43
44
CloudBaseCompaction::CloudBaseCompaction(CloudStorageEngine& engine, CloudTabletSPtr tablet)
45
2
        : CloudCompactionMixin(engine, tablet,
46
2
                               "BaseCompaction:" + std::to_string(tablet->tablet_id())) {}
47
48
2
CloudBaseCompaction::~CloudBaseCompaction() = default;
49
50
0
Status CloudBaseCompaction::prepare_compact() {
51
0
    Status st;
52
0
    Defer defer_set_st([&] {
53
0
        if (!st.ok()) {
54
0
            cloud_tablet()->set_last_base_compaction_status(st.to_string());
55
0
            cloud_tablet()->set_last_base_compaction_failure_time(UnixMillis());
56
0
        }
57
0
    });
58
0
    if (_tablet->tablet_state() != TABLET_RUNNING) {
59
0
        st = Status::InternalError("invalid tablet state. tablet_id={}", _tablet->tablet_id());
60
0
        return st;
61
0
    }
62
63
0
    bool need_sync_tablet = true;
64
0
    {
65
0
        std::shared_lock rlock(_tablet->get_header_lock());
66
        // If number of rowsets is equal to approximate_num_rowsets, it is very likely that this tablet has been
67
        // synchronized with meta-service.
68
0
        if (_tablet->tablet_meta()->all_rs_metas().size() >=
69
0
                    cloud_tablet()->fetch_add_approximate_num_rowsets(0) &&
70
0
            cloud_tablet()->last_sync_time_s > 0) {
71
0
            need_sync_tablet = false;
72
0
        }
73
0
    }
74
0
    if (need_sync_tablet) {
75
0
        st = cloud_tablet()->sync_rowsets();
76
0
        RETURN_IF_ERROR(st);
77
0
    }
78
79
0
    st = pick_rowsets_to_compact();
80
0
    RETURN_IF_ERROR(st);
81
82
0
    for (auto& rs : _input_rowsets) {
83
0
        _input_row_num += rs->num_rows();
84
0
        _input_segments += rs->num_segments();
85
0
        _input_rowsets_data_size += rs->data_disk_size();
86
0
        _input_rowsets_index_size += rs->index_disk_size();
87
0
        _input_rowsets_total_size += rs->total_disk_size();
88
0
        _input_rowsets_cached_data_size += rs->approximate_cached_data_size();
89
0
        _input_rowsets_cached_index_size += rs->approximate_cache_index_size();
90
0
    }
91
0
    LOG_INFO("start CloudBaseCompaction, tablet_id={}, range=[{}-{}]", _tablet->tablet_id(),
92
0
             _input_rowsets.front()->start_version(), _input_rowsets.back()->end_version())
93
0
            .tag("job_id", _uuid)
94
0
            .tag("input_rowsets", _input_rowsets.size())
95
0
            .tag("input_rows", _input_row_num)
96
0
            .tag("input_segments", _input_segments)
97
0
            .tag("input_rowsets_data_size", _input_rowsets_data_size)
98
0
            .tag("input_rowsets_index_size", _input_rowsets_index_size)
99
0
            .tag("input_rowsets_total_size", _input_rowsets_total_size)
100
0
            .tag("input_rowsets_cached_data_size", _input_rowsets_cached_data_size)
101
0
            .tag("input_rowsets_cached_index_size", _input_rowsets_cached_index_size);
102
0
    base_input_cached_size << (_input_rowsets_cached_data_size + _input_rowsets_cached_index_size);
103
0
    base_input_size << _input_rowsets_total_size;
104
0
    return Status::OK();
105
0
}
106
107
0
Status CloudBaseCompaction::request_global_lock() {
108
    // prepare compaction job
109
0
    cloud::TabletJobInfoPB job;
110
0
    auto idx = job.mutable_idx();
111
0
    idx->set_tablet_id(_tablet->tablet_id());
112
0
    idx->set_table_id(_tablet->table_id());
113
0
    idx->set_index_id(_tablet->index_id());
114
0
    idx->set_partition_id(_tablet->partition_id());
115
0
    auto compaction_job = job.add_compaction();
116
0
    compaction_job->set_id(_uuid);
117
0
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
118
0
                                  std::to_string(config::heartbeat_service_port));
119
0
    compaction_job->set_type(cloud::TabletCompactionJobPB::BASE);
120
0
    compaction_job->set_base_compaction_cnt(_base_compaction_cnt);
121
0
    compaction_job->set_cumulative_compaction_cnt(_cumulative_compaction_cnt);
122
0
    compaction_job->add_input_versions(_input_rowsets.front()->start_version());
123
0
    compaction_job->add_input_versions(_input_rowsets.back()->end_version());
124
0
    using namespace std::chrono;
125
0
    int64_t now = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
126
0
    _expiration = now + config::compaction_timeout_seconds;
127
0
    compaction_job->set_expiration(_expiration);
128
0
    compaction_job->set_lease(now + config::lease_compaction_interval_seconds * 4);
129
0
    cloud::StartTabletJobResponse resp;
130
0
    auto st = _engine.meta_mgr().prepare_tablet_job(job, &resp);
131
0
    cloud_tablet()->set_last_base_compaction_status(st.to_string());
132
0
    if (resp.has_alter_version()) {
133
0
        (static_cast<CloudTablet*>(_tablet.get()))->set_alter_version(resp.alter_version());
134
0
    }
135
0
    if (!st.ok()) {
136
0
        cloud_tablet()->set_last_base_compaction_failure_time(UnixMillis());
137
0
        if (resp.status().code() == cloud::STALE_TABLET_CACHE) {
138
            // set last_sync_time to 0 to force sync tablet next time
139
0
            cloud_tablet()->last_sync_time_s = 0;
140
0
        } else if (resp.status().code() == cloud::TABLET_NOT_FOUND) {
141
            // tablet not found
142
0
            cloud_tablet()->clear_cache();
143
0
        } else if (resp.status().code() == cloud::JOB_CHECK_ALTER_VERSION) {
144
0
            auto* cloud_tablet = (static_cast<CloudTablet*>(_tablet.get()));
145
0
            std::stringstream ss;
146
0
            ss << "failed to prepare cumu compaction. Check compaction input versions "
147
0
                  "failed in schema change. The input version end must "
148
0
                  "less than or equal to alter_version."
149
0
                  "current alter version in BE is not correct."
150
0
                  "input_version_start="
151
0
               << compaction_job->input_versions(0)
152
0
               << " input_version_end=" << compaction_job->input_versions(1)
153
0
               << " current alter_version=" << cloud_tablet->alter_version()
154
0
               << " schema_change_alter_version=" << resp.alter_version();
155
0
            std::string msg = ss.str();
156
0
            LOG(WARNING) << msg;
157
0
            return Status::InternalError(msg);
158
0
        }
159
0
    }
160
0
    return st;
161
0
}
162
163
0
void CloudBaseCompaction::_filter_input_rowset() {
164
    // if dup_key and no delete predicate
165
    // we skip big files to save resources
166
0
    if (_tablet->keys_type() != KeysType::DUP_KEYS) {
167
0
        return;
168
0
    }
169
0
    for (auto& rs : _input_rowsets) {
170
0
        if (rs->rowset_meta()->has_delete_predicate()) {
171
0
            return;
172
0
        }
173
0
    }
174
0
    int64_t max_size = config::base_compaction_dup_key_max_file_size_mbytes * 1024 * 1024;
175
    // first find a proper rowset for start
176
0
    auto rs_iter = _input_rowsets.begin();
177
0
    while (rs_iter != _input_rowsets.end()) {
178
0
        if ((*rs_iter)->rowset_meta()->total_disk_size() >= max_size) {
179
0
            rs_iter = _input_rowsets.erase(rs_iter);
180
0
        } else {
181
0
            break;
182
0
        }
183
0
    }
184
0
}
185
186
0
Status CloudBaseCompaction::pick_rowsets_to_compact() {
187
0
    _input_rowsets.clear();
188
0
    {
189
0
        std::shared_lock rlock(_tablet->get_header_lock());
190
0
        _base_compaction_cnt = cloud_tablet()->base_compaction_cnt();
191
0
        _cumulative_compaction_cnt = cloud_tablet()->cumulative_compaction_cnt();
192
0
        _input_rowsets = cloud_tablet()->pick_candidate_rowsets_to_base_compaction();
193
0
    }
194
0
    if (auto st = check_version_continuity(_input_rowsets); !st.ok()) {
195
0
        DCHECK(false) << st;
196
0
        return st;
197
0
    }
198
0
    _filter_input_rowset();
199
0
    if (_input_rowsets.size() <= 1) {
200
0
        return Status::Error<BE_NO_SUITABLE_VERSION>(
201
0
                "insufficent compaction input rowset, #rowsets={}", _input_rowsets.size());
202
0
    }
203
204
0
    if (_input_rowsets.size() == 2 && _input_rowsets[0]->end_version() == 1) {
205
        // the tablet is with rowset: [0-1], [2-y]
206
        // and [0-1] has no data. in this situation, no need to do base compaction.
207
0
        return Status::Error<BE_NO_SUITABLE_VERSION>("no suitable versions for compaction");
208
0
    }
209
210
0
    int score = 0;
211
0
    int rowset_cnt = 0;
212
0
    int64_t max_compaction_score = _tablet->keys_type() == KeysType::UNIQUE_KEYS &&
213
0
                                                   _tablet->enable_unique_key_merge_on_write()
214
0
                                           ? config::mow_base_compaction_max_compaction_score
215
0
                                           : config::base_compaction_max_compaction_score;
216
0
    while (rowset_cnt < _input_rowsets.size()) {
217
0
        score += _input_rowsets[rowset_cnt++]->rowset_meta()->get_compaction_score();
218
0
        if (score > max_compaction_score) {
219
0
            break;
220
0
        }
221
0
    }
222
0
    _input_rowsets.resize(rowset_cnt);
223
224
    // 1. cumulative rowset must reach base_compaction_min_rowset_num threshold
225
0
    if (_input_rowsets.size() > config::base_compaction_min_rowset_num) {
226
0
        VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
227
0
                    << ", num_cumulative_rowsets=" << _input_rowsets.size() - 1
228
0
                    << ", base_compaction_num_cumulative_rowsets="
229
0
                    << config::base_compaction_min_rowset_num;
230
0
        apply_txn_size_truncation_and_log("CloudBaseCompaction");
231
0
        return Status::OK();
232
0
    }
233
234
    // 2. the ratio between base rowset and all input cumulative rowsets reaches the threshold
235
    // `_input_rowsets` has been sorted by end version, so we consider `_input_rowsets[0]` is the base rowset.
236
0
    int64_t base_size = _input_rowsets.front()->data_disk_size();
237
0
    int64_t cumulative_total_size = 0;
238
0
    for (auto it = _input_rowsets.begin() + 1; it != _input_rowsets.end(); ++it) {
239
0
        cumulative_total_size += (*it)->data_disk_size();
240
0
    }
241
242
0
    double base_cumulative_delta_ratio = config::base_compaction_min_data_ratio;
243
0
    if (base_size == 0) {
244
        // base_size == 0 means this may be a base version [0-1], which has no data.
245
        // set to 1 to void divide by zero
246
0
        base_size = 1;
247
0
    }
248
0
    double cumulative_base_ratio = static_cast<double>(cumulative_total_size) / base_size;
249
250
0
    if (cumulative_base_ratio > base_cumulative_delta_ratio) {
251
0
        VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
252
0
                    << ", cumulative_total_size=" << cumulative_total_size
253
0
                    << ", base_size=" << base_size
254
0
                    << ", cumulative_base_ratio=" << cumulative_base_ratio
255
0
                    << ", policy_ratio=" << base_cumulative_delta_ratio;
256
0
        apply_txn_size_truncation_and_log("CloudBaseCompaction");
257
0
        return Status::OK();
258
0
    }
259
260
    // 3. the interval since last base compaction reaches the threshold
261
0
    int64_t base_creation_time = _input_rowsets[0]->creation_time();
262
0
    int64_t interval_threshold = config::base_compaction_interval_seconds_since_last_operation;
263
0
    int64_t interval_since_last_base_compaction = time(nullptr) - base_creation_time;
264
0
    if (interval_since_last_base_compaction > interval_threshold) {
265
0
        VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
266
0
                    << ", interval_since_last_base_compaction="
267
0
                    << interval_since_last_base_compaction
268
0
                    << ", interval_threshold=" << interval_threshold;
269
0
        apply_txn_size_truncation_and_log("CloudBaseCompaction");
270
0
        return Status::OK();
271
0
    }
272
273
0
    VLOG_NOTICE << "don't satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
274
0
                << ", num_cumulative_rowsets=" << _input_rowsets.size() - 1
275
0
                << ", cumulative_base_ratio=" << cumulative_base_ratio
276
0
                << ", interval_since_last_base_compaction=" << interval_since_last_base_compaction;
277
0
    return Status::Error<BE_NO_SUITABLE_VERSION>("no suitable versions for compaction");
278
0
}
279
280
0
Status CloudBaseCompaction::execute_compact() {
281
0
#ifndef __APPLE__
282
0
    if (config::enable_base_compaction_idle_sched) {
283
0
        Thread::set_idle_sched();
284
0
    }
285
0
#endif
286
287
0
    SCOPED_ATTACH_TASK(_mem_tracker);
288
289
0
    using namespace std::chrono;
290
0
    auto start = steady_clock::now();
291
0
    Status st;
292
0
    Defer defer_set_st([&] {
293
0
        cloud_tablet()->set_last_base_compaction_status(st.to_string());
294
0
        if (!st.ok()) {
295
0
            cloud_tablet()->set_last_base_compaction_failure_time(UnixMillis());
296
0
        } else {
297
0
            cloud_tablet()->set_last_base_compaction_success_time(UnixMillis());
298
0
        }
299
0
    });
300
0
    st = CloudCompactionMixin::execute_compact();
301
0
    if (!st.ok()) {
302
0
        LOG(WARNING) << "fail to do " << compaction_name() << ". res=" << st
303
0
                     << ", tablet=" << _tablet->tablet_id()
304
0
                     << ", output_version=" << _output_version;
305
0
        return st;
306
0
    }
307
0
    LOG_INFO("finish CloudBaseCompaction, tablet_id={}, cost={}ms range=[{}-{}]",
308
0
             _tablet->tablet_id(), duration_cast<milliseconds>(steady_clock::now() - start).count(),
309
0
             _input_rowsets.front()->start_version(), _input_rowsets.back()->end_version())
310
0
            .tag("job_id", _uuid)
311
0
            .tag("input_rowsets", _input_rowsets.size())
312
0
            .tag("input_rows", _input_row_num)
313
0
            .tag("input_segments", _input_segments)
314
0
            .tag("input_rowsets_data_size", _input_rowsets_data_size)
315
0
            .tag("input_rowsets_index_size", _input_rowsets_index_size)
316
0
            .tag("input_rowsets_total", _input_rowsets_total_size)
317
0
            .tag("output_rows", _output_rowset->num_rows())
318
0
            .tag("output_segments", _output_rowset->num_segments())
319
0
            .tag("output_rowset_data_size", _output_rowset->data_disk_size())
320
0
            .tag("output_rowset_index_size", _output_rowset->index_disk_size())
321
0
            .tag("output_rowset_total_size", _output_rowset->total_disk_size())
322
0
            .tag("local_read_time_us", _stats.cloud_local_read_time)
323
0
            .tag("remote_read_time_us", _stats.cloud_remote_read_time)
324
0
            .tag("local_read_bytes", _local_read_bytes_total)
325
0
            .tag("remote_read_bytes", _remote_read_bytes_total);
326
327
    //_compaction_succeed = true;
328
0
    _state = CompactionState::SUCCESS;
329
330
0
    DorisMetrics::instance()->base_compaction_deltas_total->increment(_input_rowsets.size());
331
0
    DorisMetrics::instance()->base_compaction_bytes_total->increment(_input_rowsets_total_size);
332
0
    base_output_size << _output_rowset->total_disk_size();
333
334
0
    st = Status::OK();
335
0
    return st;
336
0
}
337
338
0
Status CloudBaseCompaction::modify_rowsets() {
339
    // commit compaction job
340
0
    cloud::TabletJobInfoPB job;
341
0
    auto idx = job.mutable_idx();
342
0
    idx->set_tablet_id(_tablet->tablet_id());
343
0
    idx->set_table_id(_tablet->table_id());
344
0
    idx->set_index_id(_tablet->index_id());
345
0
    idx->set_partition_id(_tablet->partition_id());
346
0
    auto compaction_job = job.add_compaction();
347
0
    compaction_job->set_id(_uuid);
348
0
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
349
0
                                  std::to_string(config::heartbeat_service_port));
350
0
    compaction_job->set_type(cloud::TabletCompactionJobPB::BASE);
351
0
    compaction_job->set_input_cumulative_point(cloud_tablet()->cumulative_layer_point());
352
0
    compaction_job->set_output_cumulative_point(cloud_tablet()->cumulative_layer_point());
353
0
    compaction_job->set_num_input_rows(_input_row_num);
354
0
    compaction_job->set_num_output_rows(_output_rowset->num_rows());
355
0
    compaction_job->set_size_input_rowsets(_input_rowsets_total_size);
356
0
    compaction_job->set_size_output_rowsets(_output_rowset->total_disk_size());
357
0
    compaction_job->set_num_input_segments(_input_segments);
358
0
    compaction_job->set_num_output_segments(_output_rowset->num_segments());
359
0
    compaction_job->set_num_input_rowsets(num_input_rowsets());
360
0
    compaction_job->set_num_output_rowsets(1);
361
0
    compaction_job->add_input_versions(_input_rowsets.front()->start_version());
362
0
    compaction_job->add_input_versions(_input_rowsets.back()->end_version());
363
0
    compaction_job->add_output_versions(_output_rowset->end_version());
364
0
    compaction_job->add_txn_id(_output_rowset->txn_id());
365
0
    compaction_job->add_output_rowset_ids(_output_rowset->rowset_id().to_string());
366
0
    compaction_job->set_index_size_input_rowsets(_input_rowsets_index_size);
367
0
    compaction_job->set_segment_size_input_rowsets(_input_rowsets_data_size);
368
0
    compaction_job->set_index_size_output_rowsets(_output_rowset->index_disk_size());
369
0
    compaction_job->set_segment_size_output_rowsets(_output_rowset->data_disk_size());
370
371
0
    DeleteBitmapPtr output_rowset_delete_bitmap = nullptr;
372
0
    int64_t get_delete_bitmap_lock_start_time = 0;
373
0
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
374
0
        _tablet->enable_unique_key_merge_on_write()) {
375
0
        int64_t initiator = this->initiator();
376
0
        RETURN_IF_ERROR(cloud_tablet()->calc_delete_bitmap_for_compaction(
377
0
                _input_rowsets, _output_rowset, *_rowid_conversion, compaction_type(),
378
0
                _stats.merged_rows, _stats.filtered_rows, initiator, output_rowset_delete_bitmap,
379
0
                _allow_delete_in_cumu_compaction, get_delete_bitmap_lock_start_time));
380
0
        LOG_INFO("update delete bitmap in CloudBaseCompaction, tablet_id={}, range=[{}-{}]",
381
0
                 _tablet->tablet_id(), _input_rowsets.front()->start_version(),
382
0
                 _input_rowsets.back()->end_version())
383
0
                .tag("job_id", _uuid)
384
0
                .tag("initiator", initiator)
385
0
                .tag("input_rowsets", _input_rowsets.size())
386
0
                .tag("input_rows", _input_row_num)
387
0
                .tag("input_segments", _input_segments)
388
0
                .tag("num_output_delete_bitmap", output_rowset_delete_bitmap->delete_bitmap.size());
389
0
        compaction_job->set_delete_bitmap_lock_initiator(initiator);
390
0
    }
391
392
0
    cloud::FinishTabletJobResponse resp;
393
0
    auto st = _engine.meta_mgr().commit_tablet_job(job, &resp);
394
0
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
395
0
        _tablet->enable_unique_key_merge_on_write()) {
396
0
        int64_t hold_delete_bitmap_lock_time_ms =
397
0
                (MonotonicMicros() - get_delete_bitmap_lock_start_time) / 1000;
398
0
        g_base_compaction_hold_delete_bitmap_lock_time_ms << hold_delete_bitmap_lock_time_ms;
399
0
    }
400
0
    if (!st.ok()) {
401
0
        if (resp.status().code() == cloud::TABLET_NOT_FOUND) {
402
0
            cloud_tablet()->clear_cache();
403
0
        } else if (resp.status().code() == cloud::JOB_CHECK_ALTER_VERSION) {
404
0
            auto* cloud_tablet = (static_cast<CloudTablet*>(_tablet.get()));
405
0
            std::stringstream ss;
406
0
            ss << "failed to prepare cumu compaction. Check compaction input versions "
407
0
                  "failed in schema change. The input version end must "
408
0
                  "less than or equal to alter_version."
409
0
                  "current alter version in BE is not correct."
410
0
                  "input_version_start="
411
0
               << compaction_job->input_versions(0)
412
0
               << " input_version_end=" << compaction_job->input_versions(1)
413
0
               << " current alter_version=" << cloud_tablet->alter_version()
414
0
               << " schema_change_alter_version=" << resp.alter_version();
415
0
            std::string msg = ss.str();
416
0
            LOG(WARNING) << msg;
417
0
            cloud_tablet->set_alter_version(resp.alter_version());
418
0
            return Status::InternalError(msg);
419
0
        }
420
0
        return st;
421
0
    }
422
0
    auto& stats = resp.stats();
423
0
    LOG(INFO) << "tablet stats=" << stats.ShortDebugString();
424
425
0
    {
426
0
        std::unique_lock wrlock(_tablet->get_header_lock());
427
        // clang-format off
428
0
        cloud_tablet()->set_last_base_compaction_success_time(std::max(cloud_tablet()->last_base_compaction_success_time(), stats.last_base_compaction_time_ms()));
429
0
        cloud_tablet()->set_last_cumu_compaction_success_time(std::max(cloud_tablet()->last_cumu_compaction_success_time(), stats.last_cumu_compaction_time_ms()));
430
0
        cloud_tablet()->set_last_full_compaction_success_time(std::max(cloud_tablet()->last_full_compaction_success_time(), stats.last_full_compaction_time_ms()));
431
        // clang-format on
432
0
        if (cloud_tablet()->base_compaction_cnt() >= stats.base_compaction_cnt()) {
433
            // This could happen while calling `sync_tablet_rowsets` during `commit_tablet_job`
434
0
            return Status::OK();
435
0
        }
436
        // Try to make output rowset visible immediately in tablet cache, instead of waiting for next synchronization from meta-service.
437
0
        cloud_tablet()->delete_rowsets(_input_rowsets, wrlock);
438
0
        cloud_tablet()->add_rowsets({_output_rowset}, false, wrlock);
439
        // ATTN: MUST NOT update `cumu_compaction_cnt` or `cumu_point` which are used when sync rowsets, otherwise may cause
440
        // the tablet to be unable to synchronize the rowset meta changes generated by cumu compaction.
441
0
        cloud_tablet()->set_base_compaction_cnt(stats.base_compaction_cnt());
442
0
        if (output_rowset_delete_bitmap) {
443
0
            _tablet->tablet_meta()->delete_bitmap().merge(*output_rowset_delete_bitmap);
444
0
        }
445
0
        if (stats.cumulative_compaction_cnt() >= cloud_tablet()->cumulative_compaction_cnt()) {
446
0
            cloud_tablet()->reset_approximate_stats(stats.num_rowsets(), stats.num_segments(),
447
0
                                                    stats.num_rows(), stats.data_size());
448
0
        }
449
0
    }
450
0
    _tablet->prefill_dbm_agg_cache_after_compaction(_output_rowset);
451
0
    return Status::OK();
452
0
}
453
454
0
Status CloudBaseCompaction::garbage_collection() {
455
0
    RETURN_IF_ERROR(CloudCompactionMixin::garbage_collection());
456
0
    cloud::TabletJobInfoPB job;
457
0
    auto idx = job.mutable_idx();
458
0
    idx->set_tablet_id(_tablet->tablet_id());
459
0
    idx->set_table_id(_tablet->table_id());
460
0
    idx->set_index_id(_tablet->index_id());
461
0
    idx->set_partition_id(_tablet->partition_id());
462
0
    auto compaction_job = job.add_compaction();
463
0
    compaction_job->set_id(_uuid);
464
0
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
465
0
                                  std::to_string(config::heartbeat_service_port));
466
0
    compaction_job->set_type(cloud::TabletCompactionJobPB::BASE);
467
0
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
468
0
        _tablet->enable_unique_key_merge_on_write()) {
469
0
        compaction_job->set_delete_bitmap_lock_initiator(this->initiator());
470
0
    }
471
0
    auto st = _engine.meta_mgr().abort_tablet_job(job);
472
0
    if (!st.ok()) {
473
0
        LOG_WARNING("failed to abort compaction job")
474
0
                .tag("job_id", _uuid)
475
0
                .tag("tablet_id", _tablet->tablet_id())
476
0
                .error(st);
477
0
    }
478
0
    return st;
479
0
}
480
481
0
void CloudBaseCompaction::do_lease() {
482
0
    cloud::TabletJobInfoPB job;
483
0
    if (_state == CompactionState::SUCCESS) {
484
0
        return;
485
0
    }
486
0
    auto idx = job.mutable_idx();
487
0
    idx->set_tablet_id(_tablet->tablet_id());
488
0
    idx->set_table_id(_tablet->table_id());
489
0
    idx->set_index_id(_tablet->index_id());
490
0
    idx->set_partition_id(_tablet->partition_id());
491
0
    auto compaction_job = job.add_compaction();
492
0
    compaction_job->set_id(_uuid);
493
0
    using namespace std::chrono;
494
0
    int64_t lease_time = duration_cast<seconds>(system_clock::now().time_since_epoch()).count() +
495
0
                         config::lease_compaction_interval_seconds * 4;
496
0
    compaction_job->set_lease(lease_time);
497
0
    auto st = _engine.meta_mgr().lease_tablet_job(job);
498
0
    if (!st.ok()) {
499
0
        LOG_WARNING("failed to lease compaction job")
500
0
                .tag("job_id", _uuid)
501
0
                .tag("tablet_id", _tablet->tablet_id())
502
0
                .error(st);
503
0
    }
504
0
}
505
506
} // namespace doris