Coverage Report

Created: 2026-08-07 05:23

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