Coverage Report

Created: 2024-11-20 12:06

/root/doris/be/src/olap/base_compaction.cpp
Line
Count
Source (jump to first uncovered line)
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 "olap/base_compaction.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
22
#include <memory>
23
#include <mutex>
24
#include <ostream>
25
26
#include "common/config.h"
27
#include "common/logging.h"
28
#include "olap/compaction.h"
29
#include "olap/olap_define.h"
30
#include "olap/rowset/rowset_meta.h"
31
#include "olap/tablet.h"
32
#include "runtime/thread_context.h"
33
#include "util/doris_metrics.h"
34
#include "util/thread.h"
35
#include "util/trace.h"
36
37
namespace doris {
38
using namespace ErrorCode;
39
40
BaseCompaction::BaseCompaction(StorageEngine& engine, const TabletSharedPtr& tablet)
41
3
        : CompactionMixin(engine, tablet, "BaseCompaction:" + std::to_string(tablet->tablet_id())) {
42
3
}
43
44
3
BaseCompaction::~BaseCompaction() = default;
45
46
0
Status BaseCompaction::prepare_compact() {
47
0
    if (!tablet()->init_succeeded()) {
48
0
        return Status::Error<INVALID_ARGUMENT, false>("_tablet init failed");
49
0
    }
50
51
0
    std::unique_lock<std::mutex> lock(tablet()->get_base_compaction_lock(), std::try_to_lock);
52
0
    if (!lock.owns_lock()) {
53
0
        return Status::Error<TRY_LOCK_FAILED, false>(
54
0
                "another base compaction is running. tablet={}", _tablet->tablet_id());
55
0
    }
56
57
    // 1. pick rowsets to compact
58
0
    RETURN_IF_ERROR(pick_rowsets_to_compact());
59
0
    COUNTER_UPDATE(_input_rowsets_counter, _input_rowsets.size());
60
61
0
    return Status::OK();
62
0
}
63
64
0
Status BaseCompaction::execute_compact() {
65
0
#ifndef __APPLE__
66
0
    if (config::enable_base_compaction_idle_sched) {
67
0
        Thread::set_idle_sched();
68
0
    }
69
0
#endif
70
0
    std::unique_lock<std::mutex> lock(tablet()->get_base_compaction_lock(), std::try_to_lock);
71
0
    if (!lock.owns_lock()) {
72
0
        return Status::Error<TRY_LOCK_FAILED, false>(
73
0
                "another base compaction is running. tablet={}", _tablet->tablet_id());
74
0
    }
75
76
0
    SCOPED_ATTACH_TASK(_mem_tracker);
77
78
0
    RETURN_IF_ERROR(CompactionMixin::execute_compact());
79
0
    DCHECK_EQ(_state, CompactionState::SUCCESS);
80
81
0
    tablet()->set_last_base_compaction_success_time(UnixMillis());
82
0
    DorisMetrics::instance()->base_compaction_deltas_total->increment(_input_rowsets.size());
83
0
    DorisMetrics::instance()->base_compaction_bytes_total->increment(_input_rowsets_total_size);
84
85
0
    return Status::OK();
86
0
}
87
88
1
void BaseCompaction::_filter_input_rowset() {
89
    // if dup_key and no delete predicate
90
    // we skip big files to save resources
91
1
    if (_tablet->keys_type() != KeysType::DUP_KEYS) {
92
0
        return;
93
0
    }
94
24
    for (auto& rs : _input_rowsets) {
95
24
        if (rs->rowset_meta()->has_delete_predicate()) {
96
0
            return;
97
0
        }
98
24
    }
99
1
    int64_t max_size = config::base_compaction_dup_key_max_file_size_mbytes * 1024 * 1024;
100
    // first find a proper rowset for start
101
1
    auto rs_iter = _input_rowsets.begin();
102
1
    while (rs_iter != _input_rowsets.end()) {
103
1
        if ((*rs_iter)->rowset_meta()->total_disk_size() >= max_size) {
104
0
            rs_iter = _input_rowsets.erase(rs_iter);
105
1
        } else {
106
1
            break;
107
1
        }
108
1
    }
109
1
}
110
111
1
Status BaseCompaction::pick_rowsets_to_compact() {
112
1
    _input_rowsets = tablet()->pick_candidate_rowsets_to_base_compaction();
113
1
    RETURN_IF_ERROR(check_version_continuity(_input_rowsets));
114
1
    _filter_input_rowset();
115
1
    if (_input_rowsets.size() <= 1) {
116
0
        return Status::Error<BE_NO_SUITABLE_VERSION>("_input_rowsets.size() is 1");
117
0
    }
118
119
    // There are two occasions, first is that we set enable_delete_when_cumu_compaction false:
120
    // If there are delete predicate rowsets in tablet, start_version > 0 implies some rowsets before
121
    // delete version cannot apply these delete predicates, which can cause incorrect query result.
122
    // So we must abort this base compaction.
123
    // A typical scenario is that some rowsets before cumulative point are on remote storage.
124
    // For example, consider rowset[0,3] is on remote storage, now we pass [4,4],[5,5],[6,9]
125
    // to do base compaction and rowset[5,5] is delete predicate rowset, if we allow them to do
126
    // such procedure, then we'll get [4,9] while it will lose the delete predicate information in [5,5]
127
    // which rusult in data in [0,3] will not be deleted.
128
    // Another occasion is that we set enable_delete_when_cumu_compaction true:
129
    // Then whatever the _input_rowsets.front()->start_version() > 0 or not, once the output
130
    // rowset's start version is bigger than 2, we'll always remain the delete pred information inside
131
    // the output rowset so the rowsets whose version is less than _input_rowsets.front()->start_version() > 0
132
    // would apply the delete pred in the end.
133
1
    if (!_allow_delete_in_cumu_compaction && _input_rowsets.front()->start_version() > 0) {
134
0
        bool has_delete_predicate = false;
135
0
        for (const auto& rs : _input_rowsets) {
136
0
            if (rs->rowset_meta()->has_delete_predicate()) {
137
0
                has_delete_predicate = true;
138
0
                break;
139
0
            }
140
0
        }
141
0
        if (has_delete_predicate) {
142
0
            return Status::Error<BE_NO_SUITABLE_VERSION>(
143
0
                    "Some rowsets cannot apply delete predicates in base compaction. tablet_id={}",
144
0
                    _tablet->tablet_id());
145
0
        }
146
0
    }
147
148
1
    if (_input_rowsets.size() == 2 && _input_rowsets[0]->end_version() == 1) {
149
0
        return Status::Error<BE_NO_SUITABLE_VERSION>(
150
0
                "the tablet is with rowset: [0-1], [2-y], and [0-1] has no data. in this "
151
0
                "situation, no need to do base compaction.");
152
0
    }
153
154
1
    int score = 0;
155
1
    int rowset_cnt = 0;
156
21
    while (rowset_cnt < _input_rowsets.size()) {
157
21
        score += _input_rowsets[rowset_cnt++]->rowset_meta()->get_compaction_score();
158
21
        if (score > config::base_compaction_max_compaction_score) {
159
1
            break;
160
1
        }
161
21
    }
162
1
    _input_rowsets.resize(rowset_cnt);
163
164
    // 1. cumulative rowset must reach base_compaction_num_cumulative_deltas threshold
165
1
    if (_input_rowsets.size() > config::base_compaction_min_rowset_num) {
166
1
        VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
167
0
                    << ", num_cumulative_rowsets=" << _input_rowsets.size() - 1
168
0
                    << ", base_compaction_num_cumulative_rowsets="
169
0
                    << config::base_compaction_min_rowset_num;
170
1
        return Status::OK();
171
1
    }
172
173
    // 2. the ratio between base rowset and all input cumulative rowsets reaches the threshold
174
    // `_input_rowsets` has been sorted by end version, so we consider `_input_rowsets[0]` is the base rowset.
175
0
    int64_t base_size = _input_rowsets.front()->data_disk_size();
176
0
    int64_t cumulative_total_size = 0;
177
0
    for (auto it = _input_rowsets.begin() + 1; it != _input_rowsets.end(); ++it) {
178
0
        cumulative_total_size += (*it)->data_disk_size();
179
0
    }
180
181
0
    double min_data_ratio = config::base_compaction_min_data_ratio;
182
0
    if (base_size == 0) {
183
        // base_size == 0 means this may be a base version [0-1], which has no data.
184
        // set to 1 to void divide by zero
185
0
        base_size = 1;
186
0
    }
187
0
    double cumulative_base_ratio = static_cast<double>(cumulative_total_size) / base_size;
188
189
0
    if (cumulative_base_ratio > min_data_ratio) {
190
0
        VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
191
0
                    << ", cumulative_total_size=" << cumulative_total_size
192
0
                    << ", base_size=" << base_size
193
0
                    << ", cumulative_base_ratio=" << cumulative_base_ratio
194
0
                    << ", policy_min_data_ratio=" << min_data_ratio;
195
0
        return Status::OK();
196
0
    }
197
198
    // 3. the interval since last base compaction reaches the threshold
199
0
    int64_t base_creation_time = _input_rowsets[0]->creation_time();
200
0
    int64_t interval_threshold = 86400;
201
0
    int64_t interval_since_last_base_compaction = time(nullptr) - base_creation_time;
202
0
    if (interval_since_last_base_compaction > interval_threshold) {
203
0
        VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id()
204
0
                    << ", interval_since_last_base_compaction="
205
0
                    << interval_since_last_base_compaction
206
0
                    << ", interval_threshold=" << interval_threshold;
207
0
        return Status::OK();
208
0
    }
209
210
0
    return Status::Error<BE_NO_SUITABLE_VERSION>(
211
0
            "don't satisfy the base compaction policy. tablet={}, num_cumulative_rowsets={}, "
212
0
            "cumulative_base_ratio={}, interval_since_last_base_compaction={}",
213
0
            _tablet->tablet_id(), _input_rowsets.size() - 1, cumulative_base_ratio,
214
0
            interval_since_last_base_compaction);
215
0
}
216
217
} // namespace doris