Coverage Report

Created: 2026-03-15 22:14

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