Coverage Report

Created: 2026-03-19 11:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_index_change_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_index_change_compaction.h"
19
20
#include "cloud/cloud_meta_mgr.h"
21
#include "cloud/config.h"
22
#include "common/status.h"
23
#include "cpp/sync_point.h"
24
25
namespace doris {
26
27
921
CloudIndexChangeCompaction::~CloudIndexChangeCompaction() = default;
28
29
CloudIndexChangeCompaction::CloudIndexChangeCompaction(CloudStorageEngine& engine,
30
                                                       CloudTabletSPtr tablet,
31
                                                       int32_t schema_version,
32
                                                       std::vector<TOlapTableIndex>& index_list,
33
                                                       std::vector<TColumn>& columns)
34
919
        : CloudCompactionMixin(engine, tablet,
35
919
                               "CloudIndexChangeCompaction:" + std::to_string(tablet->tablet_id())),
36
919
          _schema_version(schema_version),
37
919
          _index_list(index_list),
38
919
          _columns(columns) {}
39
40
891
Status CloudIndexChangeCompaction::prepare_compact() {
41
891
    TEST_SYNC_POINT_RETURN_WITH_VALUE("CloudIndexChangeCompaction::prepare_compact", Status::OK());
42
43
891
    if (_tablet->tablet_state() != TABLET_RUNNING) {
44
0
        LOG(WARNING) << "[index_change] tablet state is not running. tablet_id="
45
0
                     << _tablet->tablet_id();
46
0
        return Status::InternalError("invalid tablet state. tablet_id={}", _tablet->tablet_id());
47
0
    }
48
49
891
    RETURN_IF_ERROR(cloud_tablet()->sync_rowsets());
50
51
891
    {
52
891
        std::shared_lock rlock(_tablet->get_header_lock());
53
891
        _base_compaction_cnt = cloud_tablet()->base_compaction_cnt();
54
891
        _cumulative_compaction_cnt = cloud_tablet()->cumulative_compaction_cnt();
55
891
    }
56
57
891
    bool is_base_rowset = false;
58
891
    auto input_rowset = DORIS_TRY(
59
891
            cloud_tablet()->pick_a_rowset_for_index_change(_schema_version, is_base_rowset));
60
891
    if (input_rowset == nullptr) {
61
0
        return Status::OK();
62
0
    }
63
64
891
    if (is_base_rowset) {
65
0
        _compact_type = cloud::TabletCompactionJobPB::BASE;
66
891
    } else {
67
891
        _compact_type = cloud::TabletCompactionJobPB::CUMULATIVE;
68
891
    }
69
70
891
    _input_rowsets.push_back(input_rowset);
71
72
    // Apply transaction size truncation (usually only 1 rowset for index change)
73
891
    int64_t kept_size = 0;
74
891
    int64_t truncated_size = 0;
75
891
    cloud::truncate_rowsets_by_txn_size(_input_rowsets, kept_size, truncated_size);
76
77
891
    for (auto& rs : _input_rowsets) {
78
884
        _input_row_num += rs->num_rows();
79
884
        _input_segments += rs->num_segments();
80
884
        _input_rowsets_data_size += rs->data_disk_size();
81
884
        _input_rowsets_index_size += rs->index_disk_size();
82
884
        _input_rowsets_total_size += rs->total_disk_size();
83
884
    }
84
85
891
    _enable_inverted_index_compaction = false;
86
891
    LOG_INFO("[index_change]prepare CloudIndexChangeCompaction, tablet_id={}, range=[{}-{}]",
87
891
             _tablet->tablet_id(), _input_rowsets.front()->start_version(),
88
891
             _input_rowsets.back()->end_version())
89
891
            .tag("job_id", _uuid)
90
891
            .tag("input_rowsets", _input_rowsets.size())
91
891
            .tag("input_rows", _input_row_num)
92
891
            .tag("input_segments", _input_segments)
93
891
            .tag("input_rowsets_data_size", _input_rowsets_data_size)
94
891
            .tag("input_rowsets_index_size", _input_rowsets_index_size)
95
891
            .tag("input_rowsets_total_size", _input_rowsets_total_size)
96
891
            .tag("tablet_max_version", cloud_tablet()->max_version_unlocked())
97
891
            .tag("cumulative_point", cloud_tablet()->cumulative_layer_point())
98
891
            .tag("num_rowsets", cloud_tablet()->fetch_add_approximate_num_rowsets(0))
99
891
            .tag("cumu_num_rowsets", cloud_tablet()->fetch_add_approximate_cumu_num_rowsets(0));
100
101
891
    return Status::OK();
102
891
}
103
104
876
Status CloudIndexChangeCompaction::rebuild_tablet_schema() {
105
876
    DCHECK(_input_rowsets.size() == 1);
106
876
    auto input_schema_ptr = _input_rowsets.back()->tablet_schema();
107
108
876
    _cur_tablet_schema = std::make_shared<TabletSchema>();
109
876
    _cur_tablet_schema->copy_from(*input_schema_ptr);
110
    // rebuild tablet schema
111
876
    _cur_tablet_schema->clear_columns();
112
876
    _cur_tablet_schema->clear_index();
113
114
7.06k
    for (int i = 0; i < _columns.size(); i++) {
115
6.18k
        _cur_tablet_schema->append_column(TabletColumn(_columns[i]));
116
6.18k
    }
117
118
2.00k
    for (int i = 0; i < _index_list.size(); i++) {
119
1.12k
        TabletIndex index;
120
1.12k
        const auto& t_idx = _index_list[i];
121
1.12k
        index.init_from_thrift(t_idx, *_cur_tablet_schema);
122
1.12k
        _cur_tablet_schema->append_index(std::move(index));
123
1.12k
    }
124
125
876
    _cur_tablet_schema->set_schema_version(_schema_version);
126
127
876
    _final_tablet_schema = std::make_shared<TabletSchema>();
128
876
    _final_tablet_schema->copy_from(*_cur_tablet_schema);
129
876
    return Status::OK();
130
876
}
131
132
905
Status CloudIndexChangeCompaction::request_global_lock(bool& should_skip_err) {
133
905
    cloud::TabletJobInfoPB job;
134
905
    auto idx = job.mutable_idx();
135
905
    idx->set_tablet_id(_tablet->tablet_id());
136
905
    idx->set_table_id(_tablet->table_id());
137
905
    idx->set_index_id(_tablet->index_id());
138
905
    idx->set_partition_id(_tablet->partition_id());
139
905
    auto compaction_job = job.add_compaction();
140
905
    compaction_job->set_id(_uuid);
141
905
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
142
905
                                  std::to_string(config::heartbeat_service_port));
143
905
    compaction_job->set_base_compaction_cnt(_base_compaction_cnt);
144
905
    compaction_job->set_cumulative_compaction_cnt(_cumulative_compaction_cnt);
145
905
    using namespace std::chrono;
146
905
    int64_t now = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
147
905
    _expiration = now + config::compaction_timeout_seconds;
148
905
    compaction_job->set_expiration(_expiration);
149
905
    compaction_job->set_lease(now + config::lease_compaction_interval_seconds * 4);
150
151
905
    compaction_job->add_input_versions(_input_rowsets.front()->start_version());
152
905
    compaction_job->add_input_versions(_input_rowsets.back()->end_version());
153
154
905
    if (is_base_compaction()) {
155
0
        compaction_job->set_type(cloud::TabletCompactionJobPB::BASE);
156
905
    } else {
157
905
        compaction_job->set_type(cloud::TabletCompactionJobPB::CUMULATIVE);
158
        // Set input version range to let meta-service check version range conflict
159
905
        compaction_job->set_check_input_versions_range(config::enable_parallel_cumu_compaction);
160
905
    }
161
162
905
    cloud::StartTabletJobResponse resp;
163
905
    Status st = _engine.meta_mgr().prepare_tablet_job(job, &resp);
164
905
    if (!st.ok()) {
165
12
        if (resp.status().code() == cloud::STALE_TABLET_CACHE) {
166
            // set last_sync_time to 0 to force sync tablet next time
167
4
            cloud_tablet()->last_sync_time_s = 0;
168
4
            should_skip_err = true;
169
8
        } else if (resp.status().code() == cloud::TABLET_NOT_FOUND) {
170
            // tablet not found
171
4
#ifndef BE_TEST
172
4
            cloud_tablet()->clear_cache();
173
4
#endif
174
4
        } else if (resp.status().code() == cloud::JOB_TABLET_BUSY) {
175
2
            LOG_WARNING("[index_change]failed to prepare index change compaction")
176
2
                    .tag("job_id", _uuid)
177
2
                    .tag("msg", resp.status().msg());
178
2
            return Status::Error<ErrorCode::CUMULATIVE_NO_SUITABLE_VERSION>(
179
2
                    "index change compaction no suitable versions: job tablet busy");
180
2
        } else if (resp.status().code() == cloud::JOB_CHECK_ALTER_VERSION) {
181
            // NOTE: usually index change job and schema change job won't run run simultaneously.
182
            // just log here in case;
183
2
            (static_cast<CloudTablet*>(_tablet.get()))->set_alter_version(resp.alter_version());
184
2
            std::stringstream ss;
185
2
            ss << "[index_change]failed to prepare index change compaction. Check compaction input "
186
2
                  "versions "
187
2
                  "failed in schema change. "
188
2
                  "input_version_start="
189
2
               << compaction_job->input_versions(0)
190
2
               << " input_version_end=" << compaction_job->input_versions(1)
191
2
               << " schema_change_alter_version=" << resp.alter_version();
192
2
            std::string msg = ss.str();
193
2
            LOG(WARNING) << msg;
194
2
            return Status::InternalError(msg);
195
2
        }
196
8
        return st;
197
12
    }
198
199
893
    return Status::OK();
200
905
}
201
202
888
Status CloudIndexChangeCompaction::execute_compact() {
203
888
    TEST_SYNC_POINT_RETURN_WITH_VALUE("CloudIndexChangeCompaction::execute_compact", Status::OK());
204
888
    SCOPED_ATTACH_TASK(_mem_tracker);
205
206
888
    using namespace std::chrono;
207
888
    auto start = steady_clock::now();
208
888
    Status st;
209
888
    st = CloudCompactionMixin::execute_compact();
210
888
    if (!st.ok()) {
211
0
        LOG(WARNING) << "[index_change]fail to do " << compaction_name() << ". res=" << st
212
0
                     << ", tablet=" << _tablet->tablet_id()
213
0
                     << ", output_version=" << _output_version;
214
0
        return st;
215
0
    }
216
888
    LOG_INFO(
217
888
            "[index_change]finish CloudIndexChangeCompaction, tablet_id={}, cost={}ms, "
218
888
            "range=[{}-{}]",
219
888
            _tablet->tablet_id(), duration_cast<milliseconds>(steady_clock::now() - start).count(),
220
888
            _input_rowsets.front()->start_version(), _input_rowsets.back()->end_version())
221
888
            .tag("job_id", _uuid)
222
888
            .tag("input_rowsets", _input_rowsets.size())
223
888
            .tag("input_rows", _input_row_num)
224
888
            .tag("input_segments", _input_segments)
225
888
            .tag("input_rowsets_data_size", _input_rowsets_data_size)
226
888
            .tag("input_rowsets_index_size", _input_rowsets_index_size)
227
888
            .tag("input_rowsets_total_size", _input_rowsets_total_size)
228
888
            .tag("output_rows", _output_rowset->num_rows())
229
888
            .tag("output_segments", _output_rowset->num_segments())
230
888
            .tag("output_rowset_data_size", _output_rowset->data_disk_size())
231
888
            .tag("output_rowset_index_size", _output_rowset->index_disk_size())
232
888
            .tag("output_rowset_total_size", _output_rowset->total_disk_size())
233
888
            .tag("tablet_max_version", _tablet->max_version_unlocked())
234
888
            .tag("cumulative_point", cloud_tablet()->cumulative_layer_point())
235
888
            .tag("num_rowsets", cloud_tablet()->fetch_add_approximate_num_rowsets(0))
236
888
            .tag("cumu_num_rowsets", cloud_tablet()->fetch_add_approximate_cumu_num_rowsets(0))
237
888
            .tag("local_read_time_us", _stats.cloud_local_read_time)
238
888
            .tag("remote_read_time_us", _stats.cloud_remote_read_time)
239
888
            .tag("local_read_bytes", _local_read_bytes_total)
240
888
            .tag("remote_read_bytes", _remote_read_bytes_total);
241
242
888
    _state = CompactionState::SUCCESS;
243
244
888
    st = Status::OK();
245
888
    return st;
246
888
}
247
248
891
Status CloudIndexChangeCompaction::modify_rowsets() {
249
    // commit compaction job
250
891
    cloud::TabletJobInfoPB job;
251
891
    auto idx = job.mutable_idx();
252
891
    idx->set_tablet_id(_tablet->tablet_id());
253
891
    idx->set_table_id(_tablet->table_id());
254
891
    idx->set_index_id(_tablet->index_id());
255
891
    idx->set_partition_id(_tablet->partition_id());
256
891
    auto compaction_job = job.add_compaction();
257
891
    compaction_job->set_id(_uuid);
258
891
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
259
891
                                  std::to_string(config::heartbeat_service_port));
260
891
    compaction_job->set_input_cumulative_point(cloud_tablet()->cumulative_layer_point());
261
891
    compaction_job->set_output_cumulative_point(cloud_tablet()->cumulative_layer_point());
262
891
    compaction_job->set_num_input_rows(_input_row_num);
263
891
    compaction_job->set_num_output_rows(_output_rowset->num_rows());
264
891
    compaction_job->set_size_input_rowsets(_input_rowsets_total_size);
265
891
    compaction_job->set_size_output_rowsets(_output_rowset->total_disk_size());
266
891
    compaction_job->set_num_input_segments(_input_segments);
267
891
    compaction_job->set_num_output_segments(_output_rowset->num_segments());
268
891
    compaction_job->set_num_input_rowsets(num_input_rowsets());
269
891
    compaction_job->set_num_output_rowsets(1);
270
891
    compaction_job->add_input_versions(_input_rowsets.front()->start_version());
271
891
    compaction_job->add_input_versions(_input_rowsets.back()->end_version());
272
891
    compaction_job->add_output_versions(_output_rowset->end_version());
273
891
    compaction_job->add_txn_id(_output_rowset->txn_id());
274
891
    compaction_job->add_output_rowset_ids(_output_rowset->rowset_id().to_string());
275
891
    compaction_job->set_index_size_input_rowsets(_input_rowsets_index_size);
276
891
    compaction_job->set_segment_size_input_rowsets(_input_rowsets_data_size);
277
891
    compaction_job->set_index_size_output_rowsets(_output_rowset->index_disk_size());
278
891
    compaction_job->set_segment_size_output_rowsets(_output_rowset->data_disk_size());
279
891
    compaction_job->set_type(_compact_type);
280
281
891
    DeleteBitmapPtr output_rowset_delete_bitmap = nullptr;
282
891
    int64_t initiator = this->initiator();
283
891
    int64_t get_delete_bitmap_lock_start_time = 0;
284
891
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
285
891
        _tablet->enable_unique_key_merge_on_write()) {
286
43
        RETURN_IF_ERROR(cloud_tablet()->calc_delete_bitmap_for_compaction(
287
43
                _input_rowsets, _output_rowset, *_rowid_conversion, compaction_type(),
288
43
                _stats.merged_rows, _stats.filtered_rows, initiator, output_rowset_delete_bitmap,
289
43
                _allow_delete_in_cumu_compaction, get_delete_bitmap_lock_start_time));
290
43
        LOG_INFO(
291
43
                "[index_change]update delete bitmap in CloudIndexChangeCompaction, tablet_id={}, "
292
43
                "range=[{}-{}]",
293
43
                _tablet->tablet_id(), _input_rowsets.front()->start_version(),
294
43
                _input_rowsets.back()->end_version())
295
43
                .tag("job_id", _uuid)
296
43
                .tag("initiator", initiator)
297
43
                .tag("input_rowsets", _input_rowsets.size())
298
43
                .tag("input_rows", _input_row_num)
299
43
                .tag("input_segments", _input_segments)
300
43
                .tag("number_output_delete_bitmap",
301
43
                     output_rowset_delete_bitmap->delete_bitmap.size());
302
43
        compaction_job->set_delete_bitmap_lock_initiator(initiator);
303
43
    }
304
305
891
    cloud::FinishTabletJobResponse resp;
306
891
    auto st = _engine.meta_mgr().commit_tablet_job(job, &resp);
307
    //TODO: add metric for record hold delete bitmap's lock.
308
309
891
    if (!st.ok()) {
310
4
        if (resp.status().code() == cloud::TABLET_NOT_FOUND) {
311
2
#ifndef BE_TEST
312
2
            cloud_tablet()->clear_cache();
313
2
#endif
314
2
        } else if (resp.status().code() == cloud::JOB_CHECK_ALTER_VERSION) {
315
2
            std::stringstream ss;
316
2
            ss << "[index_change]failed to prepare index change compaction. Check compaction input "
317
2
                  "versions "
318
2
                  "failed in schema change. "
319
2
                  "input_version_start="
320
2
               << compaction_job->input_versions(0)
321
2
               << " input_version_end=" << compaction_job->input_versions(1)
322
2
               << " schema_change_alter_version=" << resp.alter_version();
323
2
            std::string msg = ss.str();
324
2
            LOG(WARNING) << msg;
325
2
            cloud_tablet()->set_alter_version(resp.alter_version());
326
2
            return Status::InternalError(msg);
327
2
        }
328
2
        return st;
329
4
    }
330
331
887
    cloud_tablet()->update_max_version_schema(_final_tablet_schema);
332
333
887
    {
334
887
        if (is_base_compaction()) {
335
0
            _update_tablet_for_base_compaction(resp, output_rowset_delete_bitmap);
336
887
        } else {
337
887
            _update_tablet_for_cumu_compaction(resp, output_rowset_delete_bitmap);
338
887
        }
339
887
    }
340
887
    return Status::OK();
341
891
}
342
343
void CloudIndexChangeCompaction::_update_tablet_for_base_compaction(
344
0
        cloud::FinishTabletJobResponse resp, DeleteBitmapPtr output_rowset_delete_bitmap) {
345
0
    auto& stats = resp.stats();
346
0
    LOG(INFO) << "[index_change] tablet stats=" << stats.ShortDebugString();
347
348
0
    {
349
0
        std::unique_lock wrlock(_tablet->get_header_lock());
350
        // clang-format off
351
0
        cloud_tablet()->set_last_base_compaction_success_time(std::max(cloud_tablet()->last_base_compaction_success_time(), stats.last_base_compaction_time_ms()));
352
0
        cloud_tablet()->set_last_cumu_compaction_success_time(std::max(cloud_tablet()->last_cumu_compaction_success_time(), stats.last_cumu_compaction_time_ms()));
353
0
        cloud_tablet()->set_last_full_compaction_success_time(std::max(cloud_tablet()->last_full_compaction_success_time(), stats.last_full_compaction_time_ms()));
354
        // clang-format on
355
0
        if (cloud_tablet()->base_compaction_cnt() >= stats.base_compaction_cnt()) {
356
            // This could happen while calling `sync_tablet_rowsets` during `commit_tablet_job`
357
0
            return;
358
0
        }
359
        // Try to make output rowset visible immediately in tablet cache, instead of waiting for next synchronization from meta-service.
360
0
        if (_input_rowsets.size() == 1) {
361
0
            DCHECK_EQ(_output_rowset->version(), _input_rowsets[0]->version());
362
            // MUST NOT move input rowset to stale path.
363
0
            cloud_tablet()->add_rowsets({_output_rowset}, true, wrlock);
364
0
        } else {
365
0
            cloud_tablet()->delete_rowsets(_input_rowsets, wrlock);
366
0
            cloud_tablet()->add_rowsets({_output_rowset}, false, wrlock);
367
0
        }
368
        // ATTN: MUST NOT update `cumu_compaction_cnt` or `cumu_point` which are used when sync rowsets, otherwise may cause
369
        // the tablet to be unable to synchronize the rowset meta changes generated by cumu compaction.
370
0
        cloud_tablet()->set_base_compaction_cnt(stats.base_compaction_cnt());
371
0
        if (output_rowset_delete_bitmap) {
372
0
            _tablet->tablet_meta()->delete_bitmap().merge(*output_rowset_delete_bitmap);
373
0
        }
374
0
        if (stats.cumulative_compaction_cnt() >= cloud_tablet()->cumulative_compaction_cnt()) {
375
0
            cloud_tablet()->reset_approximate_stats(stats.num_rowsets(), stats.num_segments(),
376
0
                                                    stats.num_rows(), stats.data_size());
377
0
        }
378
0
    }
379
0
}
380
381
void CloudIndexChangeCompaction::_update_tablet_for_cumu_compaction(
382
889
        cloud::FinishTabletJobResponse resp, DeleteBitmapPtr output_rowset_delete_bitmap) {
383
889
    auto& stats = resp.stats();
384
889
    LOG(INFO) << "[index_change]tablet stats=" << stats.ShortDebugString();
385
889
    {
386
889
        std::unique_lock wrlock(_tablet->get_header_lock());
387
        // clang-format off
388
889
        cloud_tablet()->set_last_base_compaction_success_time(std::max(cloud_tablet()->last_base_compaction_success_time(), stats.last_base_compaction_time_ms()));
389
889
        cloud_tablet()->set_last_cumu_compaction_success_time(std::max(cloud_tablet()->last_cumu_compaction_success_time(), stats.last_cumu_compaction_time_ms()));
390
889
        cloud_tablet()->set_last_full_compaction_success_time(std::max(cloud_tablet()->last_full_compaction_success_time(), stats.last_full_compaction_time_ms()));
391
        // clang-format on
392
889
        if (cloud_tablet()->cumulative_compaction_cnt() >= stats.cumulative_compaction_cnt()) {
393
            // This could happen while calling `sync_tablet_rowsets` during `commit_tablet_job`, or parallel cumu compactions which are
394
            // committed later increase tablet.cumulative_compaction_cnt (see CloudCompactionTest.parallel_cumu_compaction)
395
0
            return;
396
0
        }
397
        // Try to make output rowset visible immediately in tablet cache, instead of waiting for next synchronization from meta-service.
398
889
        if (stats.cumulative_point() > cloud_tablet()->cumulative_layer_point() &&
399
889
            stats.cumulative_compaction_cnt() != cloud_tablet()->cumulative_compaction_cnt() + 1) {
400
            // This could happen when there are multiple parallel cumu compaction committed, tablet cache lags several
401
            // cumu compactions behind meta-service (stats.cumulative_compaction_cnt > tablet.cumulative_compaction_cnt + 1).
402
            // If `cumu_point` of the tablet cache also falls behind, MUST ONLY synchronize tablet cache from meta-service,
403
            // otherwise may cause the tablet to be unable to synchronize the rowset meta changes generated by other cumu compaction.
404
0
            return;
405
0
        }
406
889
        if (_input_rowsets.size() == 1) {
407
889
            DCHECK_EQ(_output_rowset->version(), _input_rowsets[0]->version());
408
            // MUST NOT move input rowset to stale path.
409
889
            cloud_tablet()->add_rowsets({_output_rowset}, true, wrlock);
410
889
        } else {
411
0
            cloud_tablet()->delete_rowsets(_input_rowsets, wrlock);
412
0
            cloud_tablet()->add_rowsets({_output_rowset}, false, wrlock);
413
0
        }
414
        // ATTN: MUST NOT update `base_compaction_cnt` which are used when sync rowsets, otherwise may cause
415
        // the tablet to be unable to synchronize the rowset meta changes generated by base compaction.
416
889
        cloud_tablet()->set_cumulative_compaction_cnt(stats.cumulative_compaction_cnt());
417
889
        cloud_tablet()->set_cumulative_layer_point(stats.cumulative_point());
418
889
        if (output_rowset_delete_bitmap) {
419
43
            _tablet->tablet_meta()->delete_bitmap().merge(*output_rowset_delete_bitmap);
420
43
        }
421
889
        if (stats.base_compaction_cnt() >= cloud_tablet()->base_compaction_cnt()) {
422
885
            cloud_tablet()->reset_approximate_stats(stats.num_rowsets(), stats.num_segments(),
423
885
                                                    stats.num_rows(), stats.data_size());
424
885
        }
425
889
    }
426
889
}
427
428
4
void CloudIndexChangeCompaction::do_lease() {
429
4
    cloud::TabletJobInfoPB job;
430
4
    if (_state == CompactionState::SUCCESS) {
431
0
        return;
432
0
    }
433
4
    auto idx = job.mutable_idx();
434
4
    idx->set_tablet_id(_tablet->tablet_id());
435
4
    idx->set_table_id(_tablet->table_id());
436
4
    idx->set_index_id(_tablet->index_id());
437
4
    idx->set_partition_id(_tablet->partition_id());
438
4
    auto compaction_job = job.add_compaction();
439
4
    compaction_job->set_id(_uuid);
440
4
    using namespace std::chrono;
441
4
    int64_t lease_time = duration_cast<seconds>(system_clock::now().time_since_epoch()).count() +
442
4
                         config::lease_compaction_interval_seconds * 4;
443
4
    compaction_job->set_lease(lease_time);
444
4
    auto st = _engine.meta_mgr().lease_tablet_job(job);
445
4
    if (!st.ok()) {
446
2
        LOG_WARNING("[index_change]failed to lease compaction job")
447
2
                .tag("job_id", _uuid)
448
2
                .tag("tablet_id", _tablet->tablet_id())
449
2
                .error(st);
450
2
    }
451
4
}
452
453
0
Status CloudIndexChangeCompaction::garbage_collection() {
454
0
    RETURN_IF_ERROR(CloudCompactionMixin::garbage_collection());
455
0
    cloud::TabletJobInfoPB job;
456
0
    auto idx = job.mutable_idx();
457
0
    idx->set_tablet_id(_tablet->tablet_id());
458
0
    idx->set_table_id(_tablet->table_id());
459
0
    idx->set_index_id(_tablet->index_id());
460
0
    idx->set_partition_id(_tablet->partition_id());
461
0
    auto compaction_job = job.add_compaction();
462
0
    compaction_job->set_id(_uuid);
463
0
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
464
0
                                  std::to_string(config::heartbeat_service_port));
465
0
    compaction_job->set_type(_compact_type);
466
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("[index_change]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
} // namespace doris