Coverage Report

Created: 2026-07-23 15:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_cumulative_compaction_policy.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_cumulative_compaction_policy.h"
19
20
#include <algorithm>
21
#include <iterator>
22
#include <list>
23
#include <ostream>
24
#include <string>
25
26
#include "cloud/config.h"
27
#include "common/config.h"
28
#include "common/logging.h"
29
#include "cpp/sync_point.h"
30
#include "storage/compaction/cumulative_compaction_time_series_policy.h"
31
#include "storage/olap_common.h"
32
#include "storage/tablet/tablet.h"
33
#include "storage/tablet/tablet_meta.h"
34
#include "util/defer_op.h"
35
36
namespace doris {
37
38
int64_t CloudCumulativeCompactionPolicy::calculate_cumulative_point(
39
        CloudTablet* tablet, const std::vector<RowsetSharedPtr>& rowsets,
40
        const RowsetSharedPtr& output_rowset, Version& last_delete_version,
41
17
        int64_t input_cumulative_point) {
42
17
    DORIS_CHECK(std::find(rowsets.begin(), rowsets.end(), output_rowset) != rowsets.end());
43
17
    int64_t cumulative_point = input_cumulative_point;
44
17
    Version no_delete_version {-1, -1};
45
30
    for (const auto& rowset : rowsets) {
46
30
        DORIS_CHECK_EQ(rowset->start_version(), cumulative_point);
47
30
        if (rowset->rowset_meta()->has_delete_predicate()) {
48
2
            cumulative_point = rowset->end_version() + 1;
49
2
            continue;
50
2
        }
51
28
        if (rowset->rowset_meta()->is_segments_overlapping()) {
52
3
            return cumulative_point;
53
3
        }
54
25
        Version& delete_version = rowset == output_rowset ? last_delete_version : no_delete_version;
55
25
        int64_t candidate_cumulative_point =
56
25
                new_cumulative_point(tablet, rowset, delete_version, cumulative_point);
57
25
        if (candidate_cumulative_point != rowset->end_version() + 1) {
58
9
            return cumulative_point;
59
9
        }
60
16
        cumulative_point = candidate_cumulative_point;
61
16
    }
62
5
    return cumulative_point;
63
17
}
64
65
CloudSizeBasedCumulativeCompactionPolicy::CloudSizeBasedCumulativeCompactionPolicy(
66
        int64_t promotion_size, double promotion_ratio, int64_t promotion_min_size,
67
        int64_t compaction_min_size)
68
259
        : _promotion_size(promotion_size),
69
259
          _promotion_ratio(promotion_ratio),
70
259
          _promotion_min_size(promotion_min_size),
71
259
          _compaction_min_size(compaction_min_size) {}
72
73
38
int64_t CloudSizeBasedCumulativeCompactionPolicy::_level_size(const int64_t size) {
74
38
    if (size < 1024) return 0;
75
24
    int64_t max_level = (int64_t)1
76
24
                        << (sizeof(_promotion_size) * 8 - 1 - __builtin_clzl(_promotion_size / 2));
77
24
    if (size >= max_level) return max_level;
78
19
    return (int64_t)1 << (sizeof(size) * 8 - 1 - __builtin_clzl(size));
79
24
}
80
81
void find_longest_consecutive_empty_rowsets(std::vector<RowsetSharedPtr>* result,
82
20
                                            const std::vector<RowsetSharedPtr>& candidate_rowsets) {
83
20
    std::vector<RowsetSharedPtr> current_sequence;
84
20
    std::vector<RowsetSharedPtr> longest_sequence;
85
86
296
    for (size_t i = 0; i < candidate_rowsets.size(); ++i) {
87
276
        auto& rowset = candidate_rowsets[i];
88
89
        // Check if rowset is empty and has no delete predicate
90
276
        if (rowset->num_segments() == 0 && !rowset->rowset_meta()->has_delete_predicate()) {
91
            // Check if this is consecutive with previous rowset
92
191
            if (current_sequence.empty() ||
93
191
                (current_sequence.back()->end_version() == rowset->start_version() - 1)) {
94
191
                current_sequence.push_back(rowset);
95
191
            } else {
96
                // Start new sequence if not consecutive
97
0
                if (current_sequence.size() > longest_sequence.size()) {
98
0
                    longest_sequence = current_sequence;
99
0
                }
100
0
                current_sequence.clear();
101
0
                current_sequence.push_back(rowset);
102
0
            }
103
191
        } else {
104
            // Non-empty rowset, check if we have a sequence to compare
105
85
            if (current_sequence.size() > longest_sequence.size()) {
106
8
                longest_sequence = current_sequence;
107
8
            }
108
85
            current_sequence.clear();
109
85
        }
110
276
    }
111
112
    // Check final sequence
113
20
    if (current_sequence.size() > longest_sequence.size()) {
114
3
        longest_sequence = current_sequence;
115
3
    }
116
117
20
    *result = longest_sequence;
118
20
}
119
120
int64_t CloudSizeBasedCumulativeCompactionPolicy::pick_input_rowsets(
121
        CloudTablet* tablet, const std::vector<RowsetSharedPtr>& candidate_rowsets,
122
        const int64_t max_compaction_score, const int64_t min_compaction_score,
123
        std::vector<RowsetSharedPtr>* input_rowsets, Version* last_delete_version,
124
18
        size_t* compaction_score, bool allow_delete) {
125
18
    DBUG_EXECUTE_IF(
126
18
            "CloudSizeBasedCumulativeCompactionPolicy::pick_input_rowsets.set_input_rowsets", {
127
18
                auto target_tablet_id = dp->param<int64_t>("tablet_id", -1);
128
18
                if (target_tablet_id == tablet->tablet_id()) {
129
18
                    auto start_version = dp->param<int64_t>("start_version", -1);
130
18
                    auto end_version = dp->param<int64_t>("end_version", -1);
131
18
                    for (auto& rowset : candidate_rowsets) {
132
18
                        if (rowset->start_version() >= start_version &&
133
18
                            rowset->end_version() <= end_version) {
134
18
                            input_rowsets->push_back(rowset);
135
18
                        }
136
18
                    }
137
18
                    LOG_INFO(
138
18
                            "[CloudSizeBasedCumulativeCompactionPolicy::pick_input_rowsets.set_"
139
18
                            "input_rowsets] tablet_id={}, start={}, end={}, "
140
18
                            "input_rowsets->size()={}",
141
18
                            target_tablet_id, start_version, end_version, input_rowsets->size());
142
18
                    return input_rowsets->size();
143
18
                }
144
18
            })
145
146
18
    size_t promotion_size = cloud_promotion_size(tablet);
147
18
    auto max_version = tablet->max_version().first;
148
18
    int transient_size = 0;
149
18
    *compaction_score = 0;
150
18
    int64_t total_size = 0;
151
18
    bool skip_trim = false; // Skip trim for Empty Rowset Compaction
152
153
    // DEFER: trim input_rowsets from back if score > max_compaction_score
154
    // This ensures we don't return more rowsets than allowed by max_compaction_score,
155
    // while still collecting enough rowsets to pass min_compaction_score check after level_size removal.
156
    // Must be placed after variable initialization and before collection loop.
157
18
    DEFER({
158
18
        if (skip_trim) {
159
18
            return;
160
18
        }
161
        // Keep at least 1 rowset to avoid removing the only rowset (consistent with fallback branch)
162
18
        while (input_rowsets->size() > 1 &&
163
18
               *compaction_score > static_cast<size_t>(max_compaction_score)) {
164
18
            auto& last_rowset = input_rowsets->back();
165
18
            *compaction_score -= last_rowset->rowset_meta()->get_compaction_score();
166
18
            total_size -= last_rowset->rowset_meta()->total_disk_size();
167
18
            input_rowsets->pop_back();
168
18
        }
169
18
    });
170
171
274
    for (auto& rowset : candidate_rowsets) {
172
        // check whether this rowset is delete version
173
274
        if (!allow_delete && rowset->rowset_meta()->has_delete_predicate()) {
174
0
            *last_delete_version = rowset->version();
175
0
            if (!input_rowsets->empty()) {
176
                // we meet a delete version, and there were other versions before.
177
                // we should compact those version before handling them over to base compaction
178
0
                break;
179
0
            } else {
180
                // we meet a delete version, and no other versions before, skip it and continue
181
0
                input_rowsets->clear();
182
0
                *compaction_score = 0;
183
0
                transient_size = 0;
184
0
                continue;
185
0
            }
186
0
        }
187
274
        if (tablet->tablet_state() == TABLET_NOTREADY) {
188
            // If tablet under alter, keep latest 10 version so that base tablet max version
189
            // not merged in new tablet, and then we can copy data from base tablet
190
0
            if (rowset->version().second < max_version - 10) {
191
0
                continue;
192
0
            }
193
0
        }
194
        // Removed: max_compaction_score check here
195
        // We now collect all candidate rowsets and trim from back at return time via DEFER
196
274
        *compaction_score += rowset->rowset_meta()->get_compaction_score();
197
274
        total_size += rowset->rowset_meta()->total_disk_size();
198
199
274
        transient_size += 1;
200
274
        input_rowsets->push_back(rowset);
201
274
    }
202
203
    // if there is delete version, do compaction directly
204
18
    if (last_delete_version->first != -1) {
205
0
        if (input_rowsets->size() == 1) {
206
0
            auto rs_meta = input_rowsets->front()->rowset_meta();
207
            // if there is only one rowset and not overlapping,
208
            // we do not need to do cumulative compaction
209
0
            if (!rs_meta->is_segments_overlapping()) {
210
0
                input_rowsets->clear();
211
0
                *compaction_score = 0;
212
0
            }
213
0
        }
214
0
        return transient_size;
215
0
    }
216
217
    // Check if empty rowset compaction strategy is enabled
218
18
    if (config::enable_empty_rowset_compaction && !input_rowsets->empty()) {
219
        // Check if input_rowsets contain consecutive empty rowsets that meet criteria
220
17
        std::vector<RowsetSharedPtr> consecutive_empty_rowsets;
221
17
        find_longest_consecutive_empty_rowsets(&consecutive_empty_rowsets, *input_rowsets);
222
223
17
        if (!consecutive_empty_rowsets.empty() &&
224
17
            consecutive_empty_rowsets.size() >= config::empty_rowset_compaction_min_count &&
225
17
            static_cast<double>(consecutive_empty_rowsets.size()) /
226
5
                            static_cast<double>(input_rowsets->size()) >=
227
5
                    config::empty_rowset_compaction_min_ratio) {
228
            // Prioritize consecutive empty rowset compaction
229
            // Skip trim: empty rowset compaction has very low cost and the goal is to reduce rowset count
230
4
            *input_rowsets = consecutive_empty_rowsets;
231
4
            *compaction_score = consecutive_empty_rowsets.size();
232
4
            skip_trim = true;
233
4
            return consecutive_empty_rowsets.size();
234
4
        }
235
17
    }
236
237
14
    auto rs_begin = input_rowsets->begin();
238
14
    size_t new_compaction_score = *compaction_score;
239
14
    const bool can_handle_exhausted_input =
240
14
            (config::prioritize_query_perf_in_compaction && tablet->keys_type() != DUP_KEYS) ||
241
14
            *compaction_score >= static_cast<size_t>(max_compaction_score);
242
20
    while (rs_begin != input_rowsets->end()) {
243
19
        auto& rs_meta = (*rs_begin)->rowset_meta();
244
19
        int64_t current_level = _level_size(rs_meta->total_disk_size());
245
19
        int64_t remain_level = _level_size(total_size - rs_meta->total_disk_size());
246
        // if current level less then remain level, input rowsets contain current rowset
247
        // and process return; otherwise, input rowsets do not contain current rowset.
248
19
        if (current_level <= remain_level) {
249
8
            break;
250
8
        }
251
252
11
        auto next = std::next(rs_begin);
253
        // Keep the last suffix rowset for the singleton checks unless the exhausted-input
254
        // fallback below can select a useful input.
255
11
        if (next == input_rowsets->end() && !can_handle_exhausted_input) {
256
5
            break;
257
5
        }
258
6
        total_size -= rs_meta->total_disk_size();
259
6
        new_compaction_score -= rs_meta->get_compaction_score();
260
6
        rs_begin = next;
261
6
    }
262
14
    if (rs_begin == input_rowsets->end()) { // No suitable level size found in `input_rowsets`
263
1
        if (config::prioritize_query_perf_in_compaction && tablet->keys_type() != DUP_KEYS) {
264
            // While tablet's key type is not `DUP_KEYS`, compacting rowset in such tablets has a significant
265
            // positive impact on queries and reduces space amplification, so we ignore level limitation and
266
            // pick candidate rowsets as input rowsets.
267
1
            return transient_size;
268
1
        } else if (*compaction_score >= max_compaction_score) {
269
            // Score of `input_rowsets` exceed max compaction score, which means `input_rowsets` will never change and
270
            // this tablet will never execute cumulative compaction. MUST execute compaction on these `input_rowsets`
271
            // to reduce compaction score.
272
0
            RowsetSharedPtr rs_with_max_score;
273
0
            uint32_t max_score = 1;
274
0
            for (auto& rs : *input_rowsets) {
275
0
                if (rs->rowset_meta()->get_compaction_score() > max_score) {
276
0
                    max_score = rs->rowset_meta()->get_compaction_score();
277
0
                    rs_with_max_score = rs;
278
0
                }
279
0
            }
280
0
            if (rs_with_max_score) {
281
0
                input_rowsets->clear();
282
0
                input_rowsets->push_back(std::move(rs_with_max_score));
283
0
                *compaction_score = max_score;
284
0
                return transient_size;
285
0
            }
286
            // no rowset is OVERLAPPING, return all input rowsets (DEFER will trim to max_compaction_score)
287
0
            return transient_size;
288
0
        }
289
1
    }
290
13
    input_rowsets->erase(input_rowsets->begin(), rs_begin);
291
13
    *compaction_score = new_compaction_score;
292
293
13
    VLOG_CRITICAL << "cumulative compaction size_based policy, compaction_score = "
294
0
                  << *compaction_score << ", total_size = " << total_size
295
0
                  << ", calc promotion size value = " << promotion_size
296
0
                  << ", tablet = " << tablet->tablet_id() << ", input_rowset size "
297
0
                  << input_rowsets->size();
298
299
    // empty return
300
13
    if (input_rowsets->empty()) {
301
0
        return transient_size;
302
0
    }
303
304
    // if we have a sufficient number of segments, we should process the compaction.
305
    // otherwise, we check number of segments and total_size whether can do compaction.
306
13
    if (total_size < _compaction_min_size && *compaction_score < min_compaction_score) {
307
2
        input_rowsets->clear();
308
2
        *compaction_score = 0;
309
11
    } else if (total_size >= _compaction_min_size && input_rowsets->size() == 1) {
310
3
        auto rs_meta = input_rowsets->front()->rowset_meta();
311
        // if there is only one rowset and not overlapping,
312
        // we do not need to do compaction
313
3
        if (!rs_meta->is_segments_overlapping()) {
314
1
            input_rowsets->clear();
315
1
            *compaction_score = 0;
316
1
        }
317
3
    }
318
13
    return transient_size;
319
13
}
320
321
37
int64_t CloudSizeBasedCumulativeCompactionPolicy::cloud_promotion_size(CloudTablet* t) const {
322
37
    int64_t promotion_size = int64_t(cast_set<double>(t->base_size()) * _promotion_ratio);
323
    // promotion_size is between _size_based_promotion_size and _size_based_promotion_min_size
324
37
    return promotion_size > _promotion_size       ? _promotion_size
325
37
           : promotion_size < _promotion_min_size ? _promotion_min_size
326
37
                                                  : promotion_size;
327
37
}
328
329
int64_t CloudSizeBasedCumulativeCompactionPolicy::new_cumulative_point(
330
        CloudTablet* tablet, const RowsetSharedPtr& output_rowset, Version& last_delete_version,
331
21
        int64_t last_cumulative_point) {
332
21
    TEST_INJECTION_POINT_RETURN_WITH_VALUE("new_cumulative_point", int64_t(0), output_rowset.get(),
333
21
                                           last_cumulative_point);
334
21
    DBUG_EXECUTE_IF("CloudSizeBasedCumulativeCompactionPolicy::new_cumulative_point", {
335
21
        auto target_tablet_id = dp->param<int64_t>("tablet_id", -1);
336
21
        auto cumu_point = dp->param<int64_t>("cumu_point", -1);
337
21
        if (target_tablet_id == tablet->tablet_id() && cumu_point != -1) {
338
21
            LOG_INFO(
339
21
                    "[CloudSizeBasedCumulativeCompactionPolicy::new_cumulative_point] "
340
21
                    "tablet_id={}, cumu_point={}",
341
21
                    target_tablet_id, cumu_point);
342
21
            return cumu_point;
343
21
        }
344
21
    });
345
    // for MoW table, if there's too many versions, the delete bitmap will grow to
346
    // a very big size, which may cause the tablet meta too big and the `save_meta`
347
    // operation too slow.
348
    // if the rowset should not promotion according to it's disk size, we should also
349
    // consider it's version count here.
350
21
    bool satisfy_promotion_version = tablet->enable_unique_key_merge_on_write() &&
351
21
                                     output_rowset->end_version() - output_rowset->start_version() >
352
1
                                             config::compaction_promotion_version_count;
353
    // if rowsets have delete version, move to the last directly.
354
    // if rowsets have no delete version, check output_rowset total disk size satisfies promotion size.
355
21
    return (last_delete_version.first != -1 ||
356
21
            output_rowset->total_disk_size() >= cloud_promotion_size(tablet) ||
357
21
            satisfy_promotion_version)
358
21
                   ? output_rowset->end_version() + 1
359
21
                   : last_cumulative_point;
360
21
}
361
362
int64_t CloudTimeSeriesCumulativeCompactionPolicy::pick_input_rowsets(
363
        CloudTablet* tablet, const std::vector<RowsetSharedPtr>& candidate_rowsets,
364
        const int64_t max_compaction_score, const int64_t min_compaction_score,
365
        std::vector<RowsetSharedPtr>* input_rowsets, Version* last_delete_version,
366
0
        size_t* compaction_score, bool allow_delete) {
367
0
    int64_t last_cumu = tablet->last_cumu_compaction_success_time();
368
0
    return TimeSeriesCumulativeCompactionPolicy::pick_input_rowsets(
369
0
            tablet, last_cumu, candidate_rowsets, max_compaction_score, min_compaction_score,
370
0
            input_rowsets, last_delete_version, compaction_score, allow_delete);
371
0
}
372
373
int64_t CloudTimeSeriesCumulativeCompactionPolicy::get_compaction_level(
374
        CloudTablet* tablet, const std::vector<RowsetSharedPtr>& input_rowsets,
375
0
        RowsetSharedPtr output_rowset) {
376
0
    return TimeSeriesCumulativeCompactionPolicy::get_compaction_level((BaseTablet*)tablet,
377
0
                                                                      input_rowsets, output_rowset);
378
0
}
379
380
int64_t CloudTimeSeriesCumulativeCompactionPolicy::new_cumulative_point(
381
        CloudTablet* tablet, const RowsetSharedPtr& output_rowset, Version& last_delete_version,
382
6
        int64_t last_cumulative_point) {
383
6
    if (tablet->tablet_state() != TABLET_RUNNING || output_rowset->num_segments() == 0) {
384
2
        return last_cumulative_point;
385
2
    }
386
387
4
    if (tablet->tablet_meta()->time_series_compaction_level_threshold() >= 2 &&
388
4
        output_rowset->rowset_meta()->compaction_level() < 2) {
389
1
        return last_cumulative_point;
390
1
    }
391
392
3
    return output_rowset->end_version() + 1;
393
4
}
394
395
} // namespace doris