Coverage Report

Created: 2026-07-14 18:43

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