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