Coverage Report

Created: 2026-04-16 13:22

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