/root/doris/be/src/olap/tablet.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/tablet.h" |
19 | | |
20 | | #include <butil/logging.h> |
21 | | #include <bvar/reducer.h> |
22 | | #include <bvar/window.h> |
23 | | #include <fmt/format.h> |
24 | | #include <gen_cpp/FrontendService_types.h> |
25 | | #include <gen_cpp/MasterService_types.h> |
26 | | #include <gen_cpp/Metrics_types.h> |
27 | | #include <gen_cpp/olap_file.pb.h> |
28 | | #include <gen_cpp/types.pb.h> |
29 | | #include <rapidjson/document.h> |
30 | | #include <rapidjson/encodings.h> |
31 | | #include <rapidjson/prettywriter.h> |
32 | | #include <rapidjson/rapidjson.h> |
33 | | #include <rapidjson/stringbuffer.h> |
34 | | |
35 | | #include <algorithm> |
36 | | #include <atomic> |
37 | | #include <boost/container/detail/std_fwd.hpp> |
38 | | #include <roaring/roaring.hh> |
39 | | |
40 | | #include "common/compiler_util.h" // IWYU pragma: keep |
41 | | // IWYU pragma: no_include <bits/chrono.h> |
42 | | #include <chrono> // IWYU pragma: keep |
43 | | #include <filesystem> |
44 | | #include <iterator> |
45 | | #include <limits> |
46 | | #include <map> |
47 | | #include <memory> |
48 | | #include <mutex> |
49 | | #include <set> |
50 | | #include <shared_mutex> |
51 | | #include <string> |
52 | | #include <tuple> |
53 | | #include <type_traits> |
54 | | #include <unordered_map> |
55 | | #include <unordered_set> |
56 | | |
57 | | #include "agent/utils.h" |
58 | | #include "common/config.h" |
59 | | #include "common/consts.h" |
60 | | #include "common/exception.h" |
61 | | #include "common/logging.h" |
62 | | #include "common/signal_handler.h" |
63 | | #include "common/status.h" |
64 | | #include "exprs/runtime_filter.h" |
65 | | #include "gutil/ref_counted.h" |
66 | | #include "gutil/strings/stringpiece.h" |
67 | | #include "gutil/strings/substitute.h" |
68 | | #include "io/fs/file_reader.h" |
69 | | #include "io/fs/file_reader_writer_fwd.h" |
70 | | #include "io/fs/file_system.h" |
71 | | #include "io/fs/file_writer.h" |
72 | | #include "io/fs/path.h" |
73 | | #include "io/fs/remote_file_system.h" |
74 | | #include "io/io_common.h" |
75 | | #include "olap/base_compaction.h" |
76 | | #include "olap/base_tablet.h" |
77 | | #include "olap/binlog.h" |
78 | | #include "olap/cumulative_compaction.h" |
79 | | #include "olap/cumulative_compaction_policy.h" |
80 | | #include "olap/cumulative_compaction_time_series_policy.h" |
81 | | #include "olap/delete_bitmap_calculator.h" |
82 | | #include "olap/full_compaction.h" |
83 | | #include "olap/memtable.h" |
84 | | #include "olap/olap_common.h" |
85 | | #include "olap/olap_define.h" |
86 | | #include "olap/olap_meta.h" |
87 | | #include "olap/primary_key_index.h" |
88 | | #include "olap/rowid_conversion.h" |
89 | | #include "olap/rowset/beta_rowset.h" |
90 | | #include "olap/rowset/rowset.h" |
91 | | #include "olap/rowset/rowset_factory.h" |
92 | | #include "olap/rowset/rowset_meta.h" |
93 | | #include "olap/rowset/rowset_meta_manager.h" |
94 | | #include "olap/rowset/rowset_writer.h" |
95 | | #include "olap/rowset/rowset_writer_context.h" |
96 | | #include "olap/rowset/segment_v2/column_reader.h" |
97 | | #include "olap/rowset/segment_v2/common.h" |
98 | | #include "olap/rowset/segment_v2/indexed_column_reader.h" |
99 | | #include "olap/rowset/vertical_beta_rowset_writer.h" |
100 | | #include "olap/schema_change.h" |
101 | | #include "olap/single_replica_compaction.h" |
102 | | #include "olap/storage_engine.h" |
103 | | #include "olap/storage_policy.h" |
104 | | #include "olap/tablet_manager.h" |
105 | | #include "olap/tablet_meta.h" |
106 | | #include "olap/tablet_meta_manager.h" |
107 | | #include "olap/tablet_schema.h" |
108 | | #include "olap/txn_manager.h" |
109 | | #include "olap/types.h" |
110 | | #include "olap/utils.h" |
111 | | #include "segment_loader.h" |
112 | | #include "service/point_query_executor.h" |
113 | | #include "tablet.h" |
114 | | #include "util/bvar_helper.h" |
115 | | #include "util/crc32c.h" |
116 | | #include "util/debug_points.h" |
117 | | #include "util/defer_op.h" |
118 | | #include "util/doris_metrics.h" |
119 | | #include "util/pretty_printer.h" |
120 | | #include "util/scoped_cleanup.h" |
121 | | #include "util/stopwatch.hpp" |
122 | | #include "util/threadpool.h" |
123 | | #include "util/time.h" |
124 | | #include "util/trace.h" |
125 | | #include "util/uid_util.h" |
126 | | #include "util/work_thread_pool.hpp" |
127 | | #include "vec/columns/column.h" |
128 | | #include "vec/columns/column_string.h" |
129 | | #include "vec/common/schema_util.h" |
130 | | #include "vec/common/string_ref.h" |
131 | | #include "vec/data_types/data_type.h" |
132 | | #include "vec/data_types/data_type_factory.hpp" |
133 | | #include "vec/data_types/serde/data_type_serde.h" |
134 | | #include "vec/jsonb/serialize.h" |
135 | | |
136 | | namespace doris { |
137 | | class TupleDescriptor; |
138 | | |
139 | | namespace vectorized { |
140 | | class Block; |
141 | | } // namespace vectorized |
142 | | |
143 | | using namespace ErrorCode; |
144 | | using namespace std::chrono_literals; |
145 | | |
146 | | using std::pair; |
147 | | using std::string; |
148 | | using std::vector; |
149 | | using io::FileSystemSPtr; |
150 | | |
151 | | namespace { |
152 | | |
153 | | bvar::LatencyRecorder g_tablet_lookup_rowkey_latency("doris_pk", "tablet_lookup_rowkey"); |
154 | | bvar::LatencyRecorder g_tablet_commit_phase_update_delete_bitmap_latency( |
155 | | "doris_pk", "commit_phase_update_delete_bitmap"); |
156 | | bvar::LatencyRecorder g_tablet_update_delete_bitmap_latency("doris_pk", "update_delete_bitmap"); |
157 | | bvar::Adder<uint64_t> g_tablet_pk_not_found("doris_pk", "lookup_not_found"); |
158 | | bvar::PerSecond<bvar::Adder<uint64_t>> g_tablet_pk_not_found_per_second( |
159 | | "doris_pk", "lookup_not_found_per_second", &g_tablet_pk_not_found, 60); |
160 | | |
161 | | bvar::Adder<uint64_t> exceed_version_limit_counter; |
162 | | bvar::Window<bvar::Adder<uint64_t>> exceed_version_limit_counter_minute( |
163 | | &exceed_version_limit_counter, 60); |
164 | | |
165 | 0 | void set_last_failure_time(Tablet* tablet, const Compaction& compaction, int64_t ms) { |
166 | 0 | switch (compaction.compaction_type()) { |
167 | 0 | case ReaderType::READER_CUMULATIVE_COMPACTION: |
168 | 0 | tablet->set_last_cumu_compaction_failure_time(ms); |
169 | 0 | return; |
170 | 0 | case ReaderType::READER_BASE_COMPACTION: |
171 | 0 | tablet->set_last_base_compaction_failure_time(ms); |
172 | 0 | return; |
173 | 0 | case ReaderType::READER_FULL_COMPACTION: |
174 | 0 | tablet->set_last_full_compaction_failure_time(ms); |
175 | 0 | return; |
176 | 0 | default: |
177 | 0 | LOG(FATAL) << "invalid compaction type " << compaction.compaction_name() |
178 | 0 | << " tablet_id: " << tablet->tablet_id(); |
179 | 0 | } |
180 | 0 | }; |
181 | | |
182 | | } // namespace |
183 | | |
184 | | WriteCooldownMetaExecutors::WriteCooldownMetaExecutors(size_t executor_nums) |
185 | 1 | : _executor_nums(executor_nums) { |
186 | 6 | for (size_t i = 0; i < _executor_nums; i++) { |
187 | 5 | std::unique_ptr<PriorityThreadPool> pool; |
188 | 5 | static_cast<void>(ThreadPoolBuilder("WriteCooldownMetaExecutor") |
189 | 5 | .set_min_threads(1) |
190 | 5 | .set_max_threads(1) |
191 | 5 | .set_max_queue_size(std::numeric_limits<int>::max()) |
192 | 5 | .build(&pool)); |
193 | 5 | _executors.emplace_back(std::move(pool)); |
194 | 5 | } |
195 | 1 | } |
196 | | |
197 | 0 | void WriteCooldownMetaExecutors::stop() { |
198 | 0 | for (auto& pool_ptr : _executors) { |
199 | 0 | if (pool_ptr) { |
200 | 0 | pool_ptr->shutdown(); |
201 | 0 | } |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | 5 | void WriteCooldownMetaExecutors::WriteCooldownMetaExecutors::submit(TabletSharedPtr tablet) { |
206 | 5 | auto tablet_id = tablet->tablet_id(); |
207 | | |
208 | 5 | { |
209 | 5 | std::shared_lock rdlock(tablet->get_header_lock()); |
210 | 5 | if (!tablet->tablet_meta()->cooldown_meta_id().initialized()) { |
211 | 0 | VLOG_NOTICE << "tablet " << tablet_id << " is not cooldown replica"; |
212 | 0 | return; |
213 | 0 | } |
214 | 5 | if (tablet->tablet_state() == TABLET_SHUTDOWN) [[unlikely]] { |
215 | 0 | LOG_INFO("tablet {} has been dropped, don't do cooldown", tablet_id); |
216 | 0 | return; |
217 | 0 | } |
218 | 5 | } |
219 | 5 | { |
220 | | // one tablet could at most have one cooldown task to be done |
221 | 5 | std::unique_lock<std::mutex> lck {_latch}; |
222 | 5 | if (_pending_tablets.count(tablet_id) > 0) { |
223 | 0 | return; |
224 | 0 | } |
225 | 5 | _pending_tablets.insert(tablet_id); |
226 | 5 | } |
227 | | |
228 | 5 | auto async_write_task = [this, t = std::move(tablet)]() { |
229 | 5 | { |
230 | 5 | std::unique_lock<std::mutex> lck {_latch}; |
231 | 5 | _pending_tablets.erase(t->tablet_id()); |
232 | 5 | } |
233 | 5 | auto s = t->write_cooldown_meta(); |
234 | 5 | if (s.ok()) { |
235 | 5 | return; |
236 | 5 | } |
237 | 0 | if (!s.is<ABORTED>()) { |
238 | 0 | LOG_EVERY_SECOND(WARNING) |
239 | 0 | << "write tablet " << t->tablet_id() << " cooldown meta failed because: " << s; |
240 | 0 | submit(t); |
241 | 0 | return; |
242 | 0 | } |
243 | 0 | VLOG_DEBUG << "tablet " << t->tablet_id() << " is not cooldown replica"; |
244 | 0 | }; |
245 | | |
246 | 5 | _executors[_get_executor_pos(tablet_id)]->offer( |
247 | 5 | [task = std::move(async_write_task)]() { task(); }); |
248 | 5 | } |
249 | | |
250 | | Tablet::Tablet(StorageEngine& engine, TabletMetaSharedPtr tablet_meta, DataDir* data_dir, |
251 | | const std::string_view& cumulative_compaction_type) |
252 | | : BaseTablet(std::move(tablet_meta)), |
253 | | _engine(engine), |
254 | | _data_dir(data_dir), |
255 | | _is_bad(false), |
256 | | _last_cumu_compaction_failure_millis(0), |
257 | | _last_base_compaction_failure_millis(0), |
258 | | _last_full_compaction_failure_millis(0), |
259 | | _last_cumu_compaction_success_millis(0), |
260 | | _last_base_compaction_success_millis(0), |
261 | | _last_full_compaction_success_millis(0), |
262 | | _cumulative_point(K_INVALID_CUMULATIVE_POINT), |
263 | | _newly_created_rowset_num(0), |
264 | | _last_checkpoint_time(0), |
265 | | _cumulative_compaction_type(cumulative_compaction_type), |
266 | | _is_tablet_path_exists(true), |
267 | | _last_missed_version(-1), |
268 | 251 | _last_missed_time_s(0) { |
269 | 251 | if (_data_dir != nullptr) { |
270 | 114 | _tablet_path = fmt::format("{}/{}/{}/{}/{}", _data_dir->path(), DATA_PREFIX, |
271 | 114 | _tablet_meta->shard_id(), tablet_id(), schema_hash()); |
272 | 114 | } |
273 | | // construct _timestamped_versioned_tracker from rs and stale rs meta |
274 | 251 | _timestamped_version_tracker.construct_versioned_tracker(_tablet_meta->all_rs_metas(), |
275 | 251 | _tablet_meta->all_stale_rs_metas()); |
276 | | // if !_tablet_meta->all_rs_metas()[0]->tablet_schema(), |
277 | | // that mean the tablet_meta is still no upgrade to doris 1.2 versions. |
278 | | // Before doris 1.2 version, rowset metas don't have tablet schema. |
279 | | // And when upgrade to doris 1.2 version, |
280 | | // all rowset metas will be set the tablet schmea from tablet meta. |
281 | 251 | if (_tablet_meta->all_rs_metas().empty() || !_tablet_meta->all_rs_metas()[0]->tablet_schema()) { |
282 | 214 | _max_version_schema = _tablet_meta->tablet_schema(); |
283 | 214 | } else { |
284 | 37 | _max_version_schema = |
285 | 37 | tablet_schema_with_merged_max_schema_version(_tablet_meta->all_rs_metas()); |
286 | 37 | } |
287 | 251 | DCHECK(_max_version_schema); |
288 | 251 | } |
289 | | |
290 | 0 | bool Tablet::set_tablet_schema_into_rowset_meta() { |
291 | 0 | bool flag = false; |
292 | 0 | for (auto&& rowset_meta : _tablet_meta->all_mutable_rs_metas()) { |
293 | 0 | if (!rowset_meta->tablet_schema()) { |
294 | 0 | rowset_meta->set_tablet_schema(_tablet_meta->tablet_schema()); |
295 | 0 | flag = true; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | return flag; |
299 | 0 | } |
300 | | |
301 | 216 | Status Tablet::_init_once_action() { |
302 | 216 | Status res = Status::OK(); |
303 | 216 | VLOG_NOTICE << "begin to load tablet. tablet=" << tablet_id() |
304 | 0 | << ", version_size=" << _tablet_meta->version_count(); |
305 | | |
306 | 216 | #ifdef BE_TEST |
307 | | // init cumulative compaction policy by type |
308 | 216 | _cumulative_compaction_policy = |
309 | 216 | CumulativeCompactionPolicyFactory::create_cumulative_compaction_policy( |
310 | 216 | _tablet_meta->compaction_policy()); |
311 | 216 | #endif |
312 | | |
313 | 216 | for (const auto& rs_meta : _tablet_meta->all_rs_metas()) { |
314 | 205 | Version version = rs_meta->version(); |
315 | 205 | RowsetSharedPtr rowset; |
316 | 205 | res = create_rowset(rs_meta, &rowset); |
317 | 205 | if (!res.ok()) { |
318 | 0 | LOG(WARNING) << "fail to init rowset. tablet_id=" << tablet_id() |
319 | 0 | << ", schema_hash=" << schema_hash() << ", version=" << version |
320 | 0 | << ", res=" << res; |
321 | 0 | return res; |
322 | 0 | } |
323 | 205 | _rs_version_map[version] = std::move(rowset); |
324 | 205 | } |
325 | | |
326 | | // init stale rowset |
327 | 216 | for (const auto& stale_rs_meta : _tablet_meta->all_stale_rs_metas()) { |
328 | 0 | Version version = stale_rs_meta->version(); |
329 | 0 | RowsetSharedPtr rowset; |
330 | 0 | res = create_rowset(stale_rs_meta, &rowset); |
331 | 0 | if (!res.ok()) { |
332 | 0 | LOG(WARNING) << "fail to init stale rowset. tablet_id:" << tablet_id() |
333 | 0 | << ", schema_hash:" << schema_hash() << ", version=" << version |
334 | 0 | << ", res:" << res; |
335 | 0 | return res; |
336 | 0 | } |
337 | 0 | _stale_rs_version_map[version] = std::move(rowset); |
338 | 0 | } |
339 | | |
340 | 216 | return res; |
341 | 216 | } |
342 | | |
343 | 216 | Status Tablet::init() { |
344 | 216 | return _init_once.call([this] { return _init_once_action(); }); |
345 | 216 | } |
346 | | |
347 | | // should save tablet meta to remote meta store |
348 | | // if it's a primary replica |
349 | 102 | void Tablet::save_meta() { |
350 | 102 | auto res = _tablet_meta->save_meta(_data_dir); |
351 | 102 | CHECK_EQ(res, Status::OK()) << "fail to save tablet_meta. res=" << res |
352 | 0 | << ", root=" << _data_dir->path(); |
353 | 102 | } |
354 | | |
355 | | // Caller should hold _meta_lock. |
356 | | Status Tablet::revise_tablet_meta(const std::vector<RowsetSharedPtr>& to_add, |
357 | | const std::vector<RowsetSharedPtr>& to_delete, |
358 | 0 | bool is_incremental_clone) { |
359 | 0 | LOG(INFO) << "begin to revise tablet. tablet_id=" << tablet_id(); |
360 | | // 1. for incremental clone, we have to add the rowsets first to make it easy to compute |
361 | | // all the delete bitmaps, and it's easy to delete them if we end up with a failure |
362 | | // 2. for full clone, we can calculate delete bitmaps on the cloned rowsets directly. |
363 | 0 | if (is_incremental_clone) { |
364 | 0 | CHECK(to_delete.empty()); // don't need to delete rowsets |
365 | 0 | add_rowsets(to_add); |
366 | | // reconstruct from tablet meta |
367 | 0 | _timestamped_version_tracker.construct_versioned_tracker(_tablet_meta->all_rs_metas()); |
368 | 0 | } |
369 | |
|
370 | 0 | Status calc_bm_status; |
371 | 0 | std::vector<RowsetSharedPtr> base_rowsets_for_full_clone = to_add; // copy vector |
372 | 0 | while (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write()) { |
373 | 0 | std::vector<RowsetSharedPtr> calc_delete_bitmap_rowsets; |
374 | 0 | int64_t to_add_min_version = INT64_MAX; |
375 | 0 | int64_t to_add_max_version = INT64_MIN; |
376 | 0 | for (auto& rs : to_add) { |
377 | 0 | if (to_add_min_version > rs->start_version()) { |
378 | 0 | to_add_min_version = rs->start_version(); |
379 | 0 | } |
380 | 0 | if (to_add_max_version < rs->end_version()) { |
381 | 0 | to_add_max_version = rs->end_version(); |
382 | 0 | } |
383 | 0 | } |
384 | 0 | Version calc_delete_bitmap_ver; |
385 | 0 | if (is_incremental_clone) { |
386 | | // From the rowset of to_add with smallest version, all other rowsets |
387 | | // need to recalculate the delete bitmap |
388 | | // For example: |
389 | | // local tablet: [0-1] [2-5] [6-6] [9-10] |
390 | | // clone tablet: [7-7] [8-8] |
391 | | // new tablet: [0-1] [2-5] [6-6] [7-7] [8-8] [9-10] |
392 | | // [7-7] [8-8] [9-10] need to recalculate delete bitmap |
393 | 0 | calc_delete_bitmap_ver = Version(to_add_min_version, max_version_unlocked().second); |
394 | 0 | } else { |
395 | | // the delete bitmap of to_add's rowsets has clone from remote when full clone. |
396 | | // only other rowsets in local need to recalculate the delete bitmap. |
397 | | // For example: |
398 | | // local tablet: [0-1]x [2-5]x [6-6]x [7-7]x [9-10] |
399 | | // clone tablet: [0-1] [2-4] [5-6] [7-8] |
400 | | // new tablet: [0-1] [2-4] [5-6] [7-8] [9-10] |
401 | | // only [9-10] need to recalculate delete bitmap |
402 | 0 | CHECK_EQ(to_add_min_version, 0) << "to_add_min_version is: " << to_add_min_version; |
403 | 0 | calc_delete_bitmap_ver = Version(to_add_max_version + 1, max_version_unlocked().second); |
404 | 0 | } |
405 | |
|
406 | 0 | if (calc_delete_bitmap_ver.first <= calc_delete_bitmap_ver.second) { |
407 | 0 | calc_bm_status = |
408 | 0 | capture_consistent_rowsets(calc_delete_bitmap_ver, &calc_delete_bitmap_rowsets); |
409 | 0 | if (!calc_bm_status.ok()) { |
410 | 0 | LOG(WARNING) << "fail to capture_consistent_rowsets, res: " << calc_bm_status; |
411 | 0 | break; |
412 | 0 | } |
413 | 0 | for (auto rs : calc_delete_bitmap_rowsets) { |
414 | 0 | if (is_incremental_clone) { |
415 | 0 | calc_bm_status = update_delete_bitmap_without_lock(rs); |
416 | 0 | } else { |
417 | 0 | calc_bm_status = |
418 | 0 | update_delete_bitmap_without_lock(rs, &base_rowsets_for_full_clone); |
419 | 0 | base_rowsets_for_full_clone.push_back(rs); |
420 | 0 | } |
421 | 0 | if (!calc_bm_status.ok()) { |
422 | 0 | LOG(WARNING) << "fail to update_delete_bitmap_without_lock, res: " |
423 | 0 | << calc_bm_status; |
424 | 0 | break; |
425 | 0 | } |
426 | 0 | } |
427 | 0 | } |
428 | 0 | break; // while (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write()) |
429 | 0 | } |
430 | |
|
431 | 0 | DBUG_EXECUTE_IF("Tablet.revise_tablet_meta_fail", { |
432 | 0 | auto ptablet_id = dp->param("tablet_id", 0); |
433 | 0 | if (tablet_id() == ptablet_id) { |
434 | 0 | LOG(INFO) << "injected revies_tablet_meta failure for tabelt: " << ptablet_id; |
435 | 0 | calc_bm_status = Status::InternalError("fault injection error"); |
436 | 0 | } |
437 | 0 | }); |
438 | | |
439 | | // error handling |
440 | 0 | if (!calc_bm_status.ok()) { |
441 | 0 | if (is_incremental_clone) { |
442 | 0 | RETURN_IF_ERROR(delete_rowsets(to_add, false)); |
443 | 0 | LOG(WARNING) << "incremental clone on tablet: " << tablet_id() << " failed due to " |
444 | 0 | << calc_bm_status.msg() << ", revert " << to_add.size() |
445 | 0 | << " rowsets added before."; |
446 | 0 | } else { |
447 | 0 | LOG(WARNING) << "full clone on tablet: " << tablet_id() << " failed due to " |
448 | 0 | << calc_bm_status.msg() << ", will not update tablet meta."; |
449 | 0 | } |
450 | 0 | return calc_bm_status; |
451 | 0 | } |
452 | | |
453 | | // full clone, calculate delete bitmap succeeded, update rowset |
454 | 0 | if (!is_incremental_clone) { |
455 | 0 | RETURN_IF_ERROR(delete_rowsets(to_delete, false)); |
456 | 0 | add_rowsets(to_add); |
457 | | // reconstruct from tablet meta |
458 | 0 | _timestamped_version_tracker.construct_versioned_tracker(_tablet_meta->all_rs_metas()); |
459 | | |
460 | | // check the rowsets used for delete bitmap calculation is equal to the rowsets |
461 | | // that we can capture by version |
462 | 0 | if (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write()) { |
463 | 0 | Version full_version = Version(0, max_version_unlocked().second); |
464 | 0 | std::vector<RowsetSharedPtr> expected_rowsets; |
465 | 0 | auto st = capture_consistent_rowsets(full_version, &expected_rowsets); |
466 | 0 | DCHECK(st.ok()) << st; |
467 | 0 | DCHECK_EQ(base_rowsets_for_full_clone.size(), expected_rowsets.size()); |
468 | 0 | if (st.ok() && base_rowsets_for_full_clone.size() != expected_rowsets.size()) |
469 | 0 | [[unlikely]] { |
470 | 0 | LOG(WARNING) << "full clone succeeded, but the count(" |
471 | 0 | << base_rowsets_for_full_clone.size() |
472 | 0 | << ") of base rowsets used for delete bitmap calculation is not match " |
473 | 0 | "expect count(" |
474 | 0 | << expected_rowsets.size() << ") we capture from tablet meta"; |
475 | 0 | } |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | | // clear stale rowset |
480 | 0 | for (auto& [v, rs] : _stale_rs_version_map) { |
481 | 0 | _engine.add_unused_rowset(rs); |
482 | 0 | } |
483 | 0 | _stale_rs_version_map.clear(); |
484 | 0 | _tablet_meta->clear_stale_rowset(); |
485 | 0 | save_meta(); |
486 | |
|
487 | 0 | LOG(INFO) << "finish to revise tablet. tablet_id=" << tablet_id(); |
488 | 0 | return Status::OK(); |
489 | 0 | } |
490 | | |
491 | 0 | RowsetSharedPtr Tablet::get_rowset(const RowsetId& rowset_id) { |
492 | 0 | std::shared_lock rdlock(_meta_lock); |
493 | 0 | for (auto& version_rowset : _rs_version_map) { |
494 | 0 | if (version_rowset.second->rowset_id() == rowset_id) { |
495 | 0 | return version_rowset.second; |
496 | 0 | } |
497 | 0 | } |
498 | 0 | for (auto& stale_version_rowset : _stale_rs_version_map) { |
499 | 0 | if (stale_version_rowset.second->rowset_id() == rowset_id) { |
500 | 0 | return stale_version_rowset.second; |
501 | 0 | } |
502 | 0 | } |
503 | 0 | return nullptr; |
504 | 0 | } |
505 | | |
506 | 85 | Status Tablet::add_rowset(RowsetSharedPtr rowset) { |
507 | 85 | DCHECK(rowset != nullptr); |
508 | 85 | std::lock_guard<std::shared_mutex> wrlock(_meta_lock); |
509 | 85 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
510 | | // If the rowset already exist, just return directly. The rowset_id is an unique-id, |
511 | | // we can use it to check this situation. |
512 | 85 | if (_contains_rowset(rowset->rowset_id())) { |
513 | 0 | return Status::OK(); |
514 | 0 | } |
515 | | // Otherwise, the version should be not contained in any existing rowset. |
516 | 85 | RETURN_IF_ERROR(_contains_version(rowset->version())); |
517 | | |
518 | 85 | RETURN_IF_ERROR(_tablet_meta->add_rs_meta(rowset->rowset_meta())); |
519 | 85 | _rs_version_map[rowset->version()] = rowset; |
520 | 85 | _timestamped_version_tracker.add_version(rowset->version()); |
521 | | |
522 | 85 | std::vector<RowsetSharedPtr> rowsets_to_delete; |
523 | | // yiguolei: temp code, should remove the rowset contains by this rowset |
524 | | // but it should be removed in multi path version |
525 | 110 | for (auto& it : _rs_version_map) { |
526 | 110 | if (rowset->version().contains(it.first) && rowset->version() != it.first) { |
527 | 0 | CHECK(it.second != nullptr) |
528 | 0 | << "there exist a version=" << it.first |
529 | 0 | << " contains the input rs with version=" << rowset->version() |
530 | 0 | << ", but the related rs is null"; |
531 | 0 | rowsets_to_delete.push_back(it.second); |
532 | 0 | } |
533 | 110 | } |
534 | 85 | std::vector<RowsetSharedPtr> empty_vec; |
535 | 85 | RETURN_IF_ERROR(modify_rowsets(empty_vec, rowsets_to_delete)); |
536 | 85 | ++_newly_created_rowset_num; |
537 | 85 | return Status::OK(); |
538 | 85 | } |
539 | | |
540 | | Status Tablet::modify_rowsets(std::vector<RowsetSharedPtr>& to_add, |
541 | 86 | std::vector<RowsetSharedPtr>& to_delete, bool check_delete) { |
542 | | // the compaction process allow to compact the single version, eg: version[4-4]. |
543 | | // this kind of "single version compaction" has same "input version" and "output version". |
544 | | // which means "to_add->version()" equals to "to_delete->version()". |
545 | | // So we should delete the "to_delete" before adding the "to_add", |
546 | | // otherwise, the "to_add" will be deleted from _rs_version_map, eventually. |
547 | | // |
548 | | // And if the version of "to_add" and "to_delete" are exactly same. eg: |
549 | | // to_add: [7-7] |
550 | | // to_delete: [7-7] |
551 | | // In this case, we no longer need to add the rowset in "to_delete" to |
552 | | // _stale_rs_version_map, but can delete it directly. |
553 | | |
554 | 86 | if (to_add.empty() && to_delete.empty()) { |
555 | 85 | return Status::OK(); |
556 | 85 | } |
557 | | |
558 | 1 | if (check_delete) { |
559 | 0 | for (auto&& rs : to_delete) { |
560 | 0 | if (auto it = _rs_version_map.find(rs->version()); it == _rs_version_map.end()) { |
561 | 0 | return Status::Error<DELETE_VERSION_ERROR>( |
562 | 0 | "try to delete not exist version {} from {}", rs->version().to_string(), |
563 | 0 | tablet_id()); |
564 | 0 | } else if (rs->rowset_id() != it->second->rowset_id()) { |
565 | 0 | return Status::Error<DELETE_VERSION_ERROR>( |
566 | 0 | "try to delete version {} from {}, but rowset id changed, delete rowset id " |
567 | 0 | "is {}, exists rowsetid is {}", |
568 | 0 | rs->version().to_string(), tablet_id(), rs->rowset_id().to_string(), |
569 | 0 | it->second->rowset_id().to_string()); |
570 | 0 | } |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | 1 | bool same_version = true; |
575 | 1 | std::sort(to_add.begin(), to_add.end(), Rowset::comparator); |
576 | 1 | std::sort(to_delete.begin(), to_delete.end(), Rowset::comparator); |
577 | 1 | if (to_add.size() == to_delete.size()) { |
578 | 0 | for (int i = 0; i < to_add.size(); ++i) { |
579 | 0 | if (to_add[i]->version() != to_delete[i]->version()) { |
580 | 0 | same_version = false; |
581 | 0 | break; |
582 | 0 | } |
583 | 0 | } |
584 | 1 | } else { |
585 | 1 | same_version = false; |
586 | 1 | } |
587 | | |
588 | 1 | std::vector<RowsetMetaSharedPtr> rs_metas_to_delete; |
589 | 1 | for (auto& rs : to_delete) { |
590 | 0 | rs_metas_to_delete.push_back(rs->rowset_meta()); |
591 | 0 | _rs_version_map.erase(rs->version()); |
592 | |
|
593 | 0 | if (!same_version) { |
594 | | // put compaction rowsets in _stale_rs_version_map. |
595 | 0 | _stale_rs_version_map[rs->version()] = rs; |
596 | 0 | } |
597 | 0 | } |
598 | | |
599 | 1 | std::vector<RowsetMetaSharedPtr> rs_metas_to_add; |
600 | 1 | for (auto& rs : to_add) { |
601 | 1 | rs_metas_to_add.push_back(rs->rowset_meta()); |
602 | 1 | _rs_version_map[rs->version()] = rs; |
603 | | |
604 | 1 | if (!same_version) { |
605 | | // If version are same, then _timestamped_version_tracker |
606 | | // already has this version, no need to add again. |
607 | 1 | _timestamped_version_tracker.add_version(rs->version()); |
608 | 1 | } |
609 | 1 | ++_newly_created_rowset_num; |
610 | 1 | } |
611 | | |
612 | 1 | _tablet_meta->modify_rs_metas(rs_metas_to_add, rs_metas_to_delete, same_version); |
613 | | |
614 | 1 | if (!same_version) { |
615 | | // add rs_metas_to_delete to tracker |
616 | 1 | _timestamped_version_tracker.add_stale_path_version(rs_metas_to_delete); |
617 | 1 | } else { |
618 | | // delete rowset in "to_delete" directly |
619 | 0 | for (auto& rs : to_delete) { |
620 | 0 | LOG(INFO) << "add unused rowset " << rs->rowset_id() << " because of same version"; |
621 | 0 | _engine.add_unused_rowset(rs); |
622 | 0 | } |
623 | 0 | } |
624 | 1 | return Status::OK(); |
625 | 1 | } |
626 | | |
627 | 5 | void Tablet::add_rowsets(const std::vector<RowsetSharedPtr>& to_add) { |
628 | 5 | if (to_add.empty()) { |
629 | 0 | return; |
630 | 0 | } |
631 | 5 | std::vector<RowsetMetaSharedPtr> rs_metas; |
632 | 5 | rs_metas.reserve(to_add.size()); |
633 | 5 | for (auto& rs : to_add) { |
634 | 5 | _rs_version_map.emplace(rs->version(), rs); |
635 | 5 | _timestamped_version_tracker.add_version(rs->version()); |
636 | 5 | rs_metas.push_back(rs->rowset_meta()); |
637 | 5 | } |
638 | 5 | _tablet_meta->modify_rs_metas(rs_metas, {}); |
639 | 5 | } |
640 | | |
641 | 5 | Status Tablet::delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete, bool move_to_stale) { |
642 | 5 | if (to_delete.empty()) { |
643 | 0 | return Status::OK(); |
644 | 0 | } |
645 | 5 | std::vector<RowsetMetaSharedPtr> rs_metas; |
646 | 5 | rs_metas.reserve(to_delete.size()); |
647 | 5 | for (const auto& rs : to_delete) { |
648 | 5 | rs_metas.push_back(rs->rowset_meta()); |
649 | 5 | _rs_version_map.erase(rs->version()); |
650 | 5 | } |
651 | 5 | _tablet_meta->modify_rs_metas({}, rs_metas, !move_to_stale); |
652 | 5 | if (move_to_stale) { |
653 | 0 | for (const auto& rs : to_delete) { |
654 | 0 | _stale_rs_version_map[rs->version()] = rs; |
655 | 0 | } |
656 | 0 | _timestamped_version_tracker.add_stale_path_version(rs_metas); |
657 | 5 | } else { |
658 | 5 | for (const auto& rs : to_delete) { |
659 | 5 | _timestamped_version_tracker.delete_version(rs->version()); |
660 | 5 | _engine.add_unused_rowset(rs); |
661 | 5 | RETURN_IF_ERROR(RowsetMetaManager::remove(_data_dir->get_meta(), tablet_uid(), |
662 | 5 | rs->rowset_meta()->rowset_id())); |
663 | 5 | } |
664 | 5 | } |
665 | 5 | return Status::OK(); |
666 | 5 | } |
667 | | |
668 | | // snapshot manager may call this api to check if version exists, so that |
669 | | // the version maybe not exist |
670 | | const RowsetSharedPtr Tablet::get_rowset_by_version(const Version& version, |
671 | 11 | bool find_in_stale) const { |
672 | 11 | auto iter = _rs_version_map.find(version); |
673 | 11 | if (iter == _rs_version_map.end()) { |
674 | 0 | if (find_in_stale) { |
675 | 0 | return get_stale_rowset_by_version(version); |
676 | 0 | } |
677 | 0 | return nullptr; |
678 | 0 | } |
679 | 11 | return iter->second; |
680 | 11 | } |
681 | | |
682 | 0 | const RowsetSharedPtr Tablet::get_stale_rowset_by_version(const Version& version) const { |
683 | 0 | auto iter = _stale_rs_version_map.find(version); |
684 | 0 | if (iter == _stale_rs_version_map.end()) { |
685 | 0 | VLOG_NOTICE << "no rowset for version:" << version << ", tablet: " << tablet_id(); |
686 | 0 | return nullptr; |
687 | 0 | } |
688 | 0 | return iter->second; |
689 | 0 | } |
690 | | |
691 | | // Already under _meta_lock |
692 | 28 | const RowsetSharedPtr Tablet::rowset_with_max_version() const { |
693 | 28 | Version max_version = _tablet_meta->max_version(); |
694 | 28 | if (max_version.first == -1) { |
695 | 0 | return nullptr; |
696 | 0 | } |
697 | | |
698 | 28 | auto iter = _rs_version_map.find(max_version); |
699 | 28 | if (iter == _rs_version_map.end()) { |
700 | 0 | DCHECK(false) << "invalid version:" << max_version; |
701 | 0 | return nullptr; |
702 | 0 | } |
703 | 28 | return iter->second; |
704 | 28 | } |
705 | | |
706 | | TabletSchemaSPtr Tablet::tablet_schema_with_merged_max_schema_version( |
707 | 40 | const std::vector<RowsetMetaSharedPtr>& rowset_metas) { |
708 | 40 | RowsetMetaSharedPtr max_schema_version_rs = *std::max_element( |
709 | 40 | rowset_metas.begin(), rowset_metas.end(), |
710 | 173 | [](const RowsetMetaSharedPtr& a, const RowsetMetaSharedPtr& b) { |
711 | 173 | return !a->tablet_schema() |
712 | 173 | ? true |
713 | 173 | : (!b->tablet_schema() |
714 | 173 | ? false |
715 | 173 | : a->tablet_schema()->schema_version() < |
716 | 173 | b->tablet_schema()->schema_version()); |
717 | 173 | }); |
718 | 40 | TabletSchemaSPtr target_schema = max_schema_version_rs->tablet_schema(); |
719 | 40 | if (target_schema->num_variant_columns() > 0) { |
720 | | // For variant columns tablet schema need to be the merged wide tablet schema |
721 | 0 | std::vector<TabletSchemaSPtr> schemas; |
722 | 0 | std::transform(rowset_metas.begin(), rowset_metas.end(), std::back_inserter(schemas), |
723 | 0 | [](const RowsetMetaSharedPtr& rs_meta) { return rs_meta->tablet_schema(); }); |
724 | 0 | static_cast<void>( |
725 | 0 | vectorized::schema_util::get_least_common_schema(schemas, nullptr, target_schema)); |
726 | 0 | VLOG_DEBUG << "dump schema: " << target_schema->dump_structure(); |
727 | 0 | } |
728 | 40 | return target_schema; |
729 | 40 | } |
730 | | |
731 | 0 | RowsetSharedPtr Tablet::_rowset_with_largest_size() { |
732 | 0 | RowsetSharedPtr largest_rowset = nullptr; |
733 | 0 | for (auto& it : _rs_version_map) { |
734 | 0 | if (it.second->empty() || it.second->zero_num_rows()) { |
735 | 0 | continue; |
736 | 0 | } |
737 | 0 | if (largest_rowset == nullptr || it.second->rowset_meta()->index_disk_size() > |
738 | 0 | largest_rowset->rowset_meta()->index_disk_size()) { |
739 | 0 | largest_rowset = it.second; |
740 | 0 | } |
741 | 0 | } |
742 | |
|
743 | 0 | return largest_rowset; |
744 | 0 | } |
745 | | |
746 | | // add inc rowset should not persist tablet meta, because it will be persisted when publish txn. |
747 | 72 | Status Tablet::add_inc_rowset(const RowsetSharedPtr& rowset) { |
748 | 72 | DCHECK(rowset != nullptr); |
749 | 72 | std::lock_guard<std::shared_mutex> wrlock(_meta_lock); |
750 | 72 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
751 | 72 | if (_contains_rowset(rowset->rowset_id())) { |
752 | 0 | return Status::OK(); |
753 | 0 | } |
754 | 72 | RETURN_IF_ERROR(_contains_version(rowset->version())); |
755 | | |
756 | 72 | RETURN_IF_ERROR(_tablet_meta->add_rs_meta(rowset->rowset_meta())); |
757 | 72 | _rs_version_map[rowset->version()] = rowset; |
758 | | |
759 | 72 | _timestamped_version_tracker.add_version(rowset->version()); |
760 | | |
761 | 72 | ++_newly_created_rowset_num; |
762 | 72 | return Status::OK(); |
763 | 72 | } |
764 | | |
765 | 8 | void Tablet::_delete_stale_rowset_by_version(const Version& version) { |
766 | 8 | RowsetMetaSharedPtr rowset_meta = _tablet_meta->acquire_stale_rs_meta_by_version(version); |
767 | 8 | if (rowset_meta == nullptr) { |
768 | 8 | return; |
769 | 8 | } |
770 | 0 | _tablet_meta->delete_stale_rs_meta_by_version(version); |
771 | 0 | VLOG_NOTICE << "delete stale rowset. tablet=" << tablet_id() << ", version=" << version; |
772 | 0 | } |
773 | | |
774 | 1 | void Tablet::delete_expired_stale_rowset() { |
775 | 1 | int64_t now = UnixSeconds(); |
776 | | // hold write lock while processing stable rowset |
777 | 1 | { |
778 | 1 | std::lock_guard<std::shared_mutex> wrlock(_meta_lock); |
779 | 1 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
780 | | // Compute the end time to delete rowsets, when a expired rowset createtime less then this time, it will be deleted. |
781 | 1 | double expired_stale_sweep_endtime = |
782 | 1 | ::difftime(now, config::tablet_rowset_stale_sweep_time_sec); |
783 | 1 | if (config::tablet_rowset_stale_sweep_by_size) { |
784 | 0 | expired_stale_sweep_endtime = now; |
785 | 0 | } |
786 | | |
787 | 1 | std::vector<int64_t> path_id_vec; |
788 | | // capture the path version to delete |
789 | 1 | _timestamped_version_tracker.capture_expired_paths( |
790 | 1 | static_cast<int64_t>(expired_stale_sweep_endtime), &path_id_vec); |
791 | | |
792 | 1 | if (path_id_vec.empty()) { |
793 | 0 | return; |
794 | 0 | } |
795 | | |
796 | 1 | const RowsetSharedPtr lastest_delta = rowset_with_max_version(); |
797 | 1 | if (lastest_delta == nullptr) { |
798 | 0 | LOG(WARNING) << "lastest_delta is null " << tablet_id(); |
799 | 0 | return; |
800 | 0 | } |
801 | | |
802 | | // fetch missing version before delete |
803 | 1 | std::vector<Version> missed_versions; |
804 | 1 | calc_missed_versions_unlocked(lastest_delta->end_version(), &missed_versions); |
805 | | |
806 | 1 | if (!missed_versions.empty()) { |
807 | 0 | LOG(WARNING) << "tablet:" << tablet_id() |
808 | 0 | << ", missed version for version:" << lastest_delta->end_version(); |
809 | 0 | _print_missed_versions(missed_versions); |
810 | 0 | return; |
811 | 0 | } |
812 | | |
813 | | // do check consistent operation |
814 | 1 | auto path_id_iter = path_id_vec.begin(); |
815 | | |
816 | 1 | std::map<int64_t, PathVersionListSharedPtr> stale_version_path_map; |
817 | 5 | while (path_id_iter != path_id_vec.end()) { |
818 | 4 | PathVersionListSharedPtr version_path = |
819 | 4 | _timestamped_version_tracker.fetch_and_delete_path_by_id(*path_id_iter); |
820 | | |
821 | 4 | Version test_version = Version(0, lastest_delta->end_version()); |
822 | 4 | stale_version_path_map[*path_id_iter] = version_path; |
823 | | |
824 | 4 | Status status = capture_consistent_versions(test_version, nullptr, false, false); |
825 | | // 1. When there is no consistent versions, we must reconstruct the tracker. |
826 | 4 | if (!status.ok()) { |
827 | | // 2. fetch missing version after delete |
828 | 0 | std::vector<Version> after_missed_versions; |
829 | 0 | calc_missed_versions_unlocked(lastest_delta->end_version(), &after_missed_versions); |
830 | | |
831 | | // 2.1 check whether missed_versions and after_missed_versions are the same. |
832 | | // when they are the same, it means we can delete the path securely. |
833 | 0 | bool is_missing = missed_versions.size() != after_missed_versions.size(); |
834 | |
|
835 | 0 | if (!is_missing) { |
836 | 0 | for (int ver_index = 0; ver_index < missed_versions.size(); ver_index++) { |
837 | 0 | if (missed_versions[ver_index] != after_missed_versions[ver_index]) { |
838 | 0 | is_missing = true; |
839 | 0 | break; |
840 | 0 | } |
841 | 0 | } |
842 | 0 | } |
843 | |
|
844 | 0 | if (is_missing) { |
845 | 0 | LOG(WARNING) << "The consistent version check fails, there are bugs. " |
846 | 0 | << "Reconstruct the tracker to recover versions in tablet=" |
847 | 0 | << tablet_id(); |
848 | | |
849 | | // 3. try to recover |
850 | 0 | _timestamped_version_tracker.recover_versioned_tracker(stale_version_path_map); |
851 | | |
852 | | // 4. double check the consistent versions |
853 | | // fetch missing version after recover |
854 | 0 | std::vector<Version> recover_missed_versions; |
855 | 0 | calc_missed_versions_unlocked(lastest_delta->end_version(), |
856 | 0 | &recover_missed_versions); |
857 | | |
858 | | // 4.1 check whether missed_versions and recover_missed_versions are the same. |
859 | | // when they are the same, it means we recover successfully. |
860 | 0 | bool is_recover_missing = |
861 | 0 | missed_versions.size() != recover_missed_versions.size(); |
862 | |
|
863 | 0 | if (!is_recover_missing) { |
864 | 0 | for (int ver_index = 0; ver_index < missed_versions.size(); ver_index++) { |
865 | 0 | if (missed_versions[ver_index] != recover_missed_versions[ver_index]) { |
866 | 0 | is_recover_missing = true; |
867 | 0 | break; |
868 | 0 | } |
869 | 0 | } |
870 | 0 | } |
871 | | |
872 | | // 5. check recover fail, version is mission |
873 | 0 | if (is_recover_missing) { |
874 | 0 | if (!config::ignore_rowset_stale_unconsistent_delete) { |
875 | 0 | LOG(FATAL) |
876 | 0 | << "rowset stale unconsistent delete. tablet= " << tablet_id(); |
877 | 0 | } else { |
878 | 0 | LOG(WARNING) |
879 | 0 | << "rowset stale unconsistent delete. tablet= " << tablet_id(); |
880 | 0 | } |
881 | 0 | } |
882 | 0 | } |
883 | 0 | return; |
884 | 0 | } |
885 | 4 | path_id_iter++; |
886 | 4 | } |
887 | | |
888 | 1 | auto old_size = _stale_rs_version_map.size(); |
889 | 1 | auto old_meta_size = _tablet_meta->all_stale_rs_metas().size(); |
890 | | |
891 | | // do delete operation |
892 | 1 | auto to_delete_iter = stale_version_path_map.begin(); |
893 | 5 | while (to_delete_iter != stale_version_path_map.end()) { |
894 | 4 | std::vector<TimestampedVersionSharedPtr>& to_delete_version = |
895 | 4 | to_delete_iter->second->timestamped_versions(); |
896 | 8 | for (auto& timestampedVersion : to_delete_version) { |
897 | 8 | auto it = _stale_rs_version_map.find(timestampedVersion->version()); |
898 | 8 | if (it != _stale_rs_version_map.end()) { |
899 | | // delete rowset |
900 | 0 | it->second->clear_cache(); |
901 | 0 | _engine.add_unused_rowset(it->second); |
902 | 0 | _stale_rs_version_map.erase(it); |
903 | 0 | VLOG_NOTICE << "delete stale rowset tablet=" << tablet_id() << " version[" |
904 | 0 | << timestampedVersion->version().first << "," |
905 | 0 | << timestampedVersion->version().second |
906 | 0 | << "] move to unused_rowset success " << std::fixed |
907 | 0 | << expired_stale_sweep_endtime; |
908 | 8 | } else { |
909 | 8 | LOG(WARNING) << "delete stale rowset tablet=" << tablet_id() << " version[" |
910 | 8 | << timestampedVersion->version().first << "," |
911 | 8 | << timestampedVersion->version().second |
912 | 8 | << "] not find in stale rs version map"; |
913 | 8 | } |
914 | 8 | _delete_stale_rowset_by_version(timestampedVersion->version()); |
915 | 8 | } |
916 | 4 | to_delete_iter++; |
917 | 4 | } |
918 | | |
919 | 1 | bool reconstructed = _reconstruct_version_tracker_if_necessary(); |
920 | | |
921 | 1 | VLOG_NOTICE << "delete stale rowset _stale_rs_version_map tablet=" << tablet_id() |
922 | 0 | << " current_size=" << _stale_rs_version_map.size() << " old_size=" << old_size |
923 | 0 | << " current_meta_size=" << _tablet_meta->all_stale_rs_metas().size() |
924 | 0 | << " old_meta_size=" << old_meta_size << " sweep endtime " << std::fixed |
925 | 0 | << expired_stale_sweep_endtime << ", reconstructed=" << reconstructed; |
926 | 1 | } |
927 | | #ifndef BE_TEST |
928 | | { |
929 | | std::shared_lock<std::shared_mutex> rlock(_meta_lock); |
930 | | save_meta(); |
931 | | } |
932 | | #endif |
933 | 1 | } |
934 | | |
935 | 1 | bool Tablet::_reconstruct_version_tracker_if_necessary() { |
936 | 1 | double orphan_vertex_ratio = _timestamped_version_tracker.get_orphan_vertex_ratio(); |
937 | 1 | if (orphan_vertex_ratio >= config::tablet_version_graph_orphan_vertex_ratio) { |
938 | 1 | _timestamped_version_tracker.construct_versioned_tracker( |
939 | 1 | _tablet_meta->all_rs_metas(), _tablet_meta->all_stale_rs_metas()); |
940 | 1 | return true; |
941 | 1 | } |
942 | 0 | return false; |
943 | 1 | } |
944 | | |
945 | | Status Tablet::capture_consistent_versions(const Version& spec_version, |
946 | | std::vector<Version>* version_path, |
947 | 14 | bool skip_missing_version, bool quiet) const { |
948 | 14 | Status status = |
949 | 14 | _timestamped_version_tracker.capture_consistent_versions(spec_version, version_path); |
950 | 14 | if (!status.ok() && !quiet) { |
951 | 1 | std::vector<Version> missed_versions; |
952 | 1 | calc_missed_versions_unlocked(spec_version.second, &missed_versions); |
953 | 1 | if (missed_versions.empty()) { |
954 | | // if version_path is null, it may be a compaction check logic. |
955 | | // so to avoid print too many logs. |
956 | 0 | if (version_path != nullptr) { |
957 | 0 | LOG(WARNING) << "tablet:" << tablet_id() |
958 | 0 | << ", version already has been merged. spec_version: " << spec_version |
959 | 0 | << ", max_version: " << max_version_unlocked(); |
960 | 0 | } |
961 | 0 | status = Status::Error<VERSION_ALREADY_MERGED>( |
962 | 0 | "missed_versions is empty, spec_version " |
963 | 0 | "{}, max_version {}, tablet_id {}", |
964 | 0 | spec_version.second, max_version_unlocked().second, tablet_id()); |
965 | 1 | } else { |
966 | 1 | if (version_path != nullptr) { |
967 | 1 | LOG(WARNING) << "status:" << status << ", tablet:" << tablet_id() |
968 | 1 | << ", missed version for version:" << spec_version; |
969 | 1 | _print_missed_versions(missed_versions); |
970 | 1 | if (skip_missing_version) { |
971 | 0 | LOG(WARNING) << "force skipping missing version for tablet:" << tablet_id(); |
972 | 0 | return Status::OK(); |
973 | 0 | } |
974 | 1 | } |
975 | 1 | } |
976 | 1 | } |
977 | | |
978 | 14 | DBUG_EXECUTE_IF("TTablet::capture_consistent_versions.inject_failure", { |
979 | 14 | auto tablet_id = dp->param<int64>("tablet_id", -1); |
980 | 14 | if (tablet_id != -1 && tablet_id == _tablet_meta->tablet_id()) { |
981 | 14 | status = Status::Error<VERSION_ALREADY_MERGED>("version already merged"); |
982 | 14 | } |
983 | 14 | }); |
984 | | |
985 | 14 | return status; |
986 | 14 | } |
987 | | |
988 | 0 | Status Tablet::check_version_integrity(const Version& version, bool quiet) { |
989 | 0 | std::shared_lock rdlock(_meta_lock); |
990 | 0 | return capture_consistent_versions(version, nullptr, false, quiet); |
991 | 0 | } |
992 | | |
993 | 24 | bool Tablet::exceed_version_limit(int32_t limit) const { |
994 | 24 | if (_tablet_meta->version_count() > limit) { |
995 | 0 | exceed_version_limit_counter << 1; |
996 | 0 | return true; |
997 | 0 | } |
998 | 24 | return false; |
999 | 24 | } |
1000 | | |
1001 | | // If any rowset contains the specific version, it means the version already exist |
1002 | 1 | bool Tablet::check_version_exist(const Version& version) const { |
1003 | 1 | std::shared_lock rdlock(_meta_lock); |
1004 | 3 | for (auto& it : _rs_version_map) { |
1005 | 3 | if (it.first.contains(version)) { |
1006 | 0 | return true; |
1007 | 0 | } |
1008 | 3 | } |
1009 | 1 | return false; |
1010 | 1 | } |
1011 | | |
1012 | | // The meta read lock should be held before calling |
1013 | | void Tablet::acquire_version_and_rowsets( |
1014 | 0 | std::vector<std::pair<Version, RowsetSharedPtr>>* version_rowsets) const { |
1015 | 0 | for (const auto& it : _rs_version_map) { |
1016 | 0 | version_rowsets->emplace_back(it.first, it.second); |
1017 | 0 | } |
1018 | 0 | } |
1019 | | |
1020 | | Status Tablet::capture_consistent_rowsets(const Version& spec_version, |
1021 | 2 | std::vector<RowsetSharedPtr>* rowsets) const { |
1022 | 2 | std::vector<Version> version_path; |
1023 | 2 | RETURN_IF_ERROR(capture_consistent_versions(spec_version, &version_path, false, false)); |
1024 | 2 | RETURN_IF_ERROR(_capture_consistent_rowsets_unlocked(version_path, rowsets)); |
1025 | 2 | return Status::OK(); |
1026 | 2 | } |
1027 | | |
1028 | | Status Tablet::_capture_consistent_rowsets_unlocked(const std::vector<Version>& version_path, |
1029 | 2 | std::vector<RowsetSharedPtr>* rowsets) const { |
1030 | 2 | DCHECK(rowsets != nullptr); |
1031 | 2 | rowsets->reserve(version_path.size()); |
1032 | 4 | for (auto& version : version_path) { |
1033 | 4 | bool is_find = false; |
1034 | 4 | do { |
1035 | 4 | auto it = _rs_version_map.find(version); |
1036 | 4 | if (it != _rs_version_map.end()) { |
1037 | 4 | is_find = true; |
1038 | 4 | rowsets->push_back(it->second); |
1039 | 4 | break; |
1040 | 4 | } |
1041 | | |
1042 | 0 | auto it_expired = _stale_rs_version_map.find(version); |
1043 | 0 | if (it_expired != _stale_rs_version_map.end()) { |
1044 | 0 | is_find = true; |
1045 | 0 | rowsets->push_back(it_expired->second); |
1046 | 0 | break; |
1047 | 0 | } |
1048 | 0 | } while (false); |
1049 | | |
1050 | 4 | if (!is_find) { |
1051 | 0 | return Status::Error<CAPTURE_ROWSET_ERROR>( |
1052 | 0 | "fail to find Rowset for version. tablet={}, version={}", tablet_id(), |
1053 | 0 | version.to_string()); |
1054 | 0 | } |
1055 | 4 | } |
1056 | 2 | return Status::OK(); |
1057 | 2 | } |
1058 | | |
1059 | | Status Tablet::capture_rs_readers(const Version& spec_version, std::vector<RowSetSplits>* rs_splits, |
1060 | 2 | bool skip_missing_version) const { |
1061 | 2 | std::vector<Version> version_path; |
1062 | 2 | RETURN_IF_ERROR( |
1063 | 2 | capture_consistent_versions(spec_version, &version_path, skip_missing_version, false)); |
1064 | 1 | RETURN_IF_ERROR(capture_rs_readers(version_path, rs_splits)); |
1065 | 1 | return Status::OK(); |
1066 | 1 | } |
1067 | | |
1068 | | Status Tablet::capture_rs_readers(const std::vector<Version>& version_path, |
1069 | 1 | std::vector<RowSetSplits>* rs_splits) const { |
1070 | 1 | DCHECK(rs_splits != nullptr && rs_splits->empty()); |
1071 | 1 | for (auto version : version_path) { |
1072 | 1 | auto it = _rs_version_map.find(version); |
1073 | 1 | if (it == _rs_version_map.end()) { |
1074 | 0 | VLOG_NOTICE << "fail to find Rowset in rs_version for version. tablet=" << tablet_id() |
1075 | 0 | << ", version='" << version.first << "-" << version.second; |
1076 | |
|
1077 | 0 | it = _stale_rs_version_map.find(version); |
1078 | 0 | if (it == _stale_rs_version_map.end()) { |
1079 | 0 | return Status::Error<CAPTURE_ROWSET_READER_ERROR>( |
1080 | 0 | "fail to find Rowset in stale_rs_version for version. tablet={}, " |
1081 | 0 | "version={}-{}", |
1082 | 0 | tablet_id(), version.first, version.second); |
1083 | 0 | } |
1084 | 0 | } |
1085 | 1 | RowsetReaderSharedPtr rs_reader; |
1086 | 1 | auto res = it->second->create_reader(&rs_reader); |
1087 | 1 | if (!res.ok()) { |
1088 | 0 | return Status::Error<CAPTURE_ROWSET_READER_ERROR>( |
1089 | 0 | "failed to create reader for rowset:{}", it->second->rowset_id().to_string()); |
1090 | 0 | } |
1091 | 1 | rs_splits->push_back(RowSetSplits(std::move(rs_reader))); |
1092 | 1 | } |
1093 | 1 | return Status::OK(); |
1094 | 1 | } |
1095 | | |
1096 | 4 | bool Tablet::can_do_compaction(size_t path_hash, CompactionType compaction_type) { |
1097 | 4 | if (compaction_type == CompactionType::BASE_COMPACTION && tablet_state() != TABLET_RUNNING) { |
1098 | | // base compaction can only be done for tablet in TABLET_RUNNING state. |
1099 | | // but cumulative compaction can be done for TABLET_NOTREADY, such as tablet under alter process. |
1100 | 0 | return false; |
1101 | 0 | } |
1102 | | |
1103 | 4 | if (data_dir()->path_hash() != path_hash || !is_used() || !init_succeeded()) { |
1104 | 1 | return false; |
1105 | 1 | } |
1106 | | |
1107 | | // In TABLET_NOTREADY, we keep last 10 versions in new tablet so base tablet max_version |
1108 | | // not merged in new tablet and then we can do compaction |
1109 | 3 | return tablet_state() == TABLET_RUNNING || tablet_state() == TABLET_NOTREADY; |
1110 | 4 | } |
1111 | | |
1112 | | uint32_t Tablet::calc_compaction_score( |
1113 | | CompactionType compaction_type, |
1114 | 5 | std::shared_ptr<CumulativeCompactionPolicy> cumulative_compaction_policy) { |
1115 | | // Need meta lock, because it will iterator "all_rs_metas" of tablet meta. |
1116 | 5 | std::shared_lock rdlock(_meta_lock); |
1117 | 5 | if (compaction_type == CompactionType::CUMULATIVE_COMPACTION) { |
1118 | 5 | return _calc_cumulative_compaction_score(cumulative_compaction_policy); |
1119 | 5 | } else { |
1120 | 0 | DCHECK_EQ(compaction_type, CompactionType::BASE_COMPACTION); |
1121 | 0 | return _calc_base_compaction_score(); |
1122 | 0 | } |
1123 | 5 | } |
1124 | | |
1125 | 0 | uint32_t Tablet::calc_cold_data_compaction_score() const { |
1126 | 0 | uint32_t score = 0; |
1127 | 0 | std::vector<RowsetMetaSharedPtr> cooldowned_rowsets; |
1128 | 0 | int64_t max_delete_version = 0; |
1129 | 0 | { |
1130 | 0 | std::shared_lock rlock(_meta_lock); |
1131 | 0 | for (auto& rs_meta : _tablet_meta->all_rs_metas()) { |
1132 | 0 | if (!rs_meta->is_local()) { |
1133 | 0 | cooldowned_rowsets.push_back(rs_meta); |
1134 | 0 | if (rs_meta->has_delete_predicate() && |
1135 | 0 | rs_meta->end_version() > max_delete_version) { |
1136 | 0 | max_delete_version = rs_meta->end_version(); |
1137 | 0 | } |
1138 | 0 | } |
1139 | 0 | } |
1140 | 0 | } |
1141 | 0 | for (auto& rs_meta : cooldowned_rowsets) { |
1142 | 0 | if (rs_meta->end_version() < max_delete_version) { |
1143 | 0 | score += rs_meta->num_segments(); |
1144 | 0 | } else { |
1145 | 0 | score += rs_meta->get_compaction_score(); |
1146 | 0 | } |
1147 | 0 | } |
1148 | 0 | return (keys_type() != KeysType::DUP_KEYS) ? score * 2 : score; |
1149 | 0 | } |
1150 | | |
1151 | | uint32_t Tablet::_calc_cumulative_compaction_score( |
1152 | 5 | std::shared_ptr<CumulativeCompactionPolicy> cumulative_compaction_policy) { |
1153 | 5 | if (cumulative_compaction_policy == nullptr) [[unlikely]] { |
1154 | 0 | return 0; |
1155 | 0 | } |
1156 | | #ifndef BE_TEST |
1157 | | if (_cumulative_compaction_policy == nullptr || |
1158 | | _cumulative_compaction_policy->name() != cumulative_compaction_policy->name()) { |
1159 | | _cumulative_compaction_policy = cumulative_compaction_policy; |
1160 | | } |
1161 | | #endif |
1162 | 5 | return _cumulative_compaction_policy->calc_cumulative_compaction_score(this); |
1163 | 5 | } |
1164 | | |
1165 | 0 | uint32_t Tablet::_calc_base_compaction_score() const { |
1166 | 0 | uint32_t score = 0; |
1167 | 0 | const int64_t point = cumulative_layer_point(); |
1168 | 0 | bool base_rowset_exist = false; |
1169 | 0 | bool has_delete = false; |
1170 | 0 | for (auto& rs_meta : _tablet_meta->all_rs_metas()) { |
1171 | 0 | if (rs_meta->start_version() == 0) { |
1172 | 0 | base_rowset_exist = true; |
1173 | 0 | } |
1174 | 0 | if (rs_meta->start_version() >= point || !rs_meta->is_local()) { |
1175 | | // all_rs_metas() is not sorted, so we use _continue_ other than _break_ here. |
1176 | 0 | continue; |
1177 | 0 | } |
1178 | 0 | if (rs_meta->has_delete_predicate()) { |
1179 | 0 | has_delete = true; |
1180 | 0 | } |
1181 | 0 | score += rs_meta->get_compaction_score(); |
1182 | 0 | } |
1183 | | |
1184 | | // In the time series compaction policy, we want the base compaction to be triggered |
1185 | | // when there are delete versions present. |
1186 | 0 | if (_tablet_meta->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY) { |
1187 | 0 | return (base_rowset_exist && has_delete) ? score : 0; |
1188 | 0 | } |
1189 | | |
1190 | | // base不存在可能是tablet正在做alter table,先不选它,设score=0 |
1191 | 0 | return base_rowset_exist ? score : 0; |
1192 | 0 | } |
1193 | | |
1194 | 0 | void Tablet::calc_missed_versions(int64_t spec_version, std::vector<Version>* missed_versions) { |
1195 | 0 | std::shared_lock rdlock(_meta_lock); |
1196 | 0 | calc_missed_versions_unlocked(spec_version, missed_versions); |
1197 | 0 | } |
1198 | | |
1199 | | // for example: |
1200 | | // [0-4][5-5][8-8][9-9] |
1201 | | // if spec_version = 6, we still return {7} other than {6, 7} |
1202 | | void Tablet::calc_missed_versions_unlocked(int64_t spec_version, |
1203 | 2 | std::vector<Version>* missed_versions) const { |
1204 | 2 | DCHECK(spec_version > 0) << "invalid spec_version: " << spec_version; |
1205 | 2 | std::list<Version> existing_versions; |
1206 | 8 | for (auto& rs : _tablet_meta->all_rs_metas()) { |
1207 | 8 | existing_versions.emplace_back(rs->version()); |
1208 | 8 | } |
1209 | | |
1210 | | // sort the existing versions in ascending order |
1211 | 11 | existing_versions.sort([](const Version& a, const Version& b) { |
1212 | | // simple because 2 versions are certainly not overlapping |
1213 | 11 | return a.first < b.first; |
1214 | 11 | }); |
1215 | | |
1216 | | // From the first version(=0), find the missing version until spec_version |
1217 | 2 | int64_t last_version = -1; |
1218 | 8 | for (const Version& version : existing_versions) { |
1219 | 8 | if (version.first > last_version + 1) { |
1220 | 4 | for (int64_t i = last_version + 1; i < version.first && i <= spec_version; ++i) { |
1221 | 2 | missed_versions->emplace_back(Version(i, i)); |
1222 | 2 | } |
1223 | 2 | } |
1224 | 8 | last_version = version.second; |
1225 | 8 | if (last_version >= spec_version) { |
1226 | 2 | break; |
1227 | 2 | } |
1228 | 8 | } |
1229 | 2 | for (int64_t i = last_version + 1; i <= spec_version; ++i) { |
1230 | 0 | missed_versions->emplace_back(Version(i, i)); |
1231 | 0 | } |
1232 | 2 | } |
1233 | | |
1234 | 0 | void Tablet::max_continuous_version_from_beginning(Version* version, Version* max_version) { |
1235 | 0 | bool has_version_cross; |
1236 | 0 | std::shared_lock rdlock(_meta_lock); |
1237 | 0 | _max_continuous_version_from_beginning_unlocked(version, max_version, &has_version_cross); |
1238 | 0 | } |
1239 | | |
1240 | | void Tablet::_max_continuous_version_from_beginning_unlocked(Version* version, Version* max_version, |
1241 | 0 | bool* has_version_cross) const { |
1242 | 0 | std::vector<Version> existing_versions; |
1243 | 0 | *has_version_cross = false; |
1244 | 0 | for (auto& rs : _tablet_meta->all_rs_metas()) { |
1245 | 0 | existing_versions.emplace_back(rs->version()); |
1246 | 0 | } |
1247 | | |
1248 | | // sort the existing versions in ascending order |
1249 | 0 | std::sort(existing_versions.begin(), existing_versions.end(), |
1250 | 0 | [](const Version& left, const Version& right) { |
1251 | | // simple because 2 versions are certainly not overlapping |
1252 | 0 | return left.first < right.first; |
1253 | 0 | }); |
1254 | |
|
1255 | 0 | Version max_continuous_version = {-1, -1}; |
1256 | 0 | for (int i = 0; i < existing_versions.size(); ++i) { |
1257 | 0 | if (existing_versions[i].first > max_continuous_version.second + 1) { |
1258 | 0 | break; |
1259 | 0 | } else if (existing_versions[i].first <= max_continuous_version.second) { |
1260 | 0 | *has_version_cross = true; |
1261 | 0 | } |
1262 | 0 | max_continuous_version = existing_versions[i]; |
1263 | 0 | } |
1264 | 0 | *version = max_continuous_version; |
1265 | | // tablet may not has rowset, eg, tablet has just been clear for restore. |
1266 | 0 | if (max_version != nullptr && !existing_versions.empty()) { |
1267 | 0 | *max_version = existing_versions.back(); |
1268 | 0 | } |
1269 | 0 | } |
1270 | | |
1271 | 39 | void Tablet::calculate_cumulative_point() { |
1272 | 39 | std::lock_guard<std::shared_mutex> wrlock(_meta_lock); |
1273 | 39 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1274 | 39 | int64_t ret_cumulative_point; |
1275 | 39 | _cumulative_compaction_policy->calculate_cumulative_point( |
1276 | 39 | this, _tablet_meta->all_rs_metas(), _cumulative_point, &ret_cumulative_point); |
1277 | | |
1278 | 39 | if (ret_cumulative_point == K_INVALID_CUMULATIVE_POINT) { |
1279 | 10 | return; |
1280 | 10 | } |
1281 | 29 | set_cumulative_layer_point(ret_cumulative_point); |
1282 | 29 | } |
1283 | | |
1284 | | // NOTE: only used when create_table, so it is sure that there is no concurrent reader and writer. |
1285 | 0 | void Tablet::delete_all_files() { |
1286 | | // Release resources like memory and disk space. |
1287 | 0 | std::shared_lock rdlock(_meta_lock); |
1288 | 0 | for (auto it : _rs_version_map) { |
1289 | 0 | static_cast<void>(it.second->remove()); |
1290 | 0 | } |
1291 | 0 | _rs_version_map.clear(); |
1292 | |
|
1293 | 0 | for (auto it : _stale_rs_version_map) { |
1294 | 0 | static_cast<void>(it.second->remove()); |
1295 | 0 | } |
1296 | 0 | _stale_rs_version_map.clear(); |
1297 | 0 | } |
1298 | | |
1299 | 0 | void Tablet::check_tablet_path_exists() { |
1300 | 0 | if (!tablet_path().empty()) { |
1301 | 0 | std::error_code ec; |
1302 | 0 | if (std::filesystem::is_directory(tablet_path(), ec)) { |
1303 | 0 | _is_tablet_path_exists.store(true, std::memory_order_relaxed); |
1304 | 0 | } else if (ec.value() == ENOENT || ec.value() == 0) { |
1305 | 0 | _is_tablet_path_exists.store(false, std::memory_order_relaxed); |
1306 | 0 | } |
1307 | 0 | } |
1308 | 0 | } |
1309 | | |
1310 | 0 | bool Tablet::check_path(const std::string& path_to_check) const { |
1311 | 0 | std::shared_lock rdlock(_meta_lock); |
1312 | 0 | if (path_to_check == _tablet_path) { |
1313 | 0 | return true; |
1314 | 0 | } |
1315 | 0 | auto tablet_id_dir = io::Path(_tablet_path).parent_path(); |
1316 | 0 | if (path_to_check == tablet_id_dir) { |
1317 | 0 | return true; |
1318 | 0 | } |
1319 | 0 | for (auto& version_rowset : _rs_version_map) { |
1320 | 0 | bool ret = version_rowset.second->check_path(path_to_check); |
1321 | 0 | if (ret) { |
1322 | 0 | return true; |
1323 | 0 | } |
1324 | 0 | } |
1325 | 0 | for (auto& stale_version_rowset : _stale_rs_version_map) { |
1326 | 0 | bool ret = stale_version_rowset.second->check_path(path_to_check); |
1327 | 0 | if (ret) { |
1328 | 0 | return true; |
1329 | 0 | } |
1330 | 0 | } |
1331 | 0 | return false; |
1332 | 0 | } |
1333 | | |
1334 | 1 | void Tablet::_print_missed_versions(const std::vector<Version>& missed_versions) const { |
1335 | 1 | std::stringstream ss; |
1336 | 1 | ss << tablet_id() << " has " << missed_versions.size() << " missed version:"; |
1337 | | // print at most 10 version |
1338 | 3 | for (int i = 0; i < 10 && i < missed_versions.size(); ++i) { |
1339 | 2 | ss << missed_versions[i] << ","; |
1340 | 2 | } |
1341 | 1 | LOG(WARNING) << ss.str(); |
1342 | 1 | } |
1343 | | |
1344 | 157 | Status Tablet::_contains_version(const Version& version) { |
1345 | | // check if there exist a rowset contains the added rowset |
1346 | 569 | for (auto& it : _rs_version_map) { |
1347 | 569 | if (it.first.contains(version)) { |
1348 | | // TODO(lingbin): Is this check unnecessary? |
1349 | | // because the value type is std::shared_ptr, when will it be nullptr? |
1350 | | // In addition, in this class, there are many places that do not make this judgment |
1351 | | // when access _rs_version_map's value. |
1352 | 0 | CHECK(it.second != nullptr) << "there exist a version=" << it.first |
1353 | 0 | << " contains the input rs with version=" << version |
1354 | 0 | << ", but the related rs is null"; |
1355 | 0 | return Status::Error<PUSH_VERSION_ALREADY_EXIST>("Tablet push duplicate version {}", |
1356 | 0 | version.to_string()); |
1357 | 0 | } |
1358 | 569 | } |
1359 | | |
1360 | 157 | return Status::OK(); |
1361 | 157 | } |
1362 | | |
1363 | 98 | TabletInfo Tablet::get_tablet_info() const { |
1364 | 98 | return TabletInfo(tablet_id(), tablet_uid()); |
1365 | 98 | } |
1366 | | |
1367 | 32 | std::vector<RowsetSharedPtr> Tablet::pick_candidate_rowsets_to_cumulative_compaction() { |
1368 | 32 | std::vector<RowsetSharedPtr> candidate_rowsets; |
1369 | 32 | if (_cumulative_point == K_INVALID_CUMULATIVE_POINT) { |
1370 | 0 | return candidate_rowsets; |
1371 | 0 | } |
1372 | 32 | return _pick_visible_rowsets_to_compaction(_cumulative_point, |
1373 | 32 | std::numeric_limits<int64_t>::max()); |
1374 | 32 | } |
1375 | | |
1376 | 1 | std::vector<RowsetSharedPtr> Tablet::pick_candidate_rowsets_to_base_compaction() { |
1377 | 1 | return _pick_visible_rowsets_to_compaction(std::numeric_limits<int64_t>::min(), |
1378 | 1 | _cumulative_point - 1); |
1379 | 1 | } |
1380 | | |
1381 | | std::vector<RowsetSharedPtr> Tablet::_pick_visible_rowsets_to_compaction( |
1382 | 33 | int64_t min_start_version, int64_t max_start_version) { |
1383 | 33 | auto [visible_version, update_ts] = get_visible_version_and_time(); |
1384 | 33 | bool update_time_long = MonotonicMillis() - update_ts > |
1385 | 33 | config::compaction_keep_invisible_version_timeout_sec * 1000L; |
1386 | 33 | int32_t keep_invisible_version_limit = |
1387 | 33 | update_time_long ? config::compaction_keep_invisible_version_min_count |
1388 | 33 | : config::compaction_keep_invisible_version_max_count; |
1389 | | |
1390 | 33 | std::vector<RowsetSharedPtr> candidate_rowsets; |
1391 | 33 | { |
1392 | 33 | std::shared_lock rlock(_meta_lock); |
1393 | 424 | for (const auto& [version, rs] : _rs_version_map) { |
1394 | 424 | int64_t version_start = version.first; |
1395 | | // rowset is remote or rowset is not in given range |
1396 | 424 | if (!rs->is_local() || version_start < min_start_version || |
1397 | 424 | version_start > max_start_version) { |
1398 | 38 | continue; |
1399 | 38 | } |
1400 | | |
1401 | | // can compact, met one of the conditions: |
1402 | | // 1. had been visible; |
1403 | | // 2. exceeds the limit of keep invisible versions. |
1404 | 386 | int64_t version_end = version.second; |
1405 | 386 | if (version_end <= visible_version || |
1406 | 386 | version_end > visible_version + keep_invisible_version_limit) { |
1407 | 385 | candidate_rowsets.push_back(rs); |
1408 | 385 | } |
1409 | 386 | } |
1410 | 33 | } |
1411 | 33 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
1412 | 33 | return candidate_rowsets; |
1413 | 33 | } |
1414 | | |
1415 | 0 | std::vector<RowsetSharedPtr> Tablet::pick_candidate_rowsets_to_full_compaction() { |
1416 | 0 | std::vector<RowsetSharedPtr> candidate_rowsets; |
1417 | 0 | traverse_rowsets([&candidate_rowsets](const auto& rs) { |
1418 | | // Do full compaction on all local rowsets. |
1419 | 0 | if (rs->is_local()) { |
1420 | 0 | candidate_rowsets.emplace_back(rs); |
1421 | 0 | } |
1422 | 0 | }); |
1423 | 0 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
1424 | 0 | return candidate_rowsets; |
1425 | 0 | } |
1426 | | |
1427 | 3 | std::vector<RowsetSharedPtr> Tablet::pick_first_consecutive_empty_rowsets(int limit) { |
1428 | 3 | std::vector<RowsetSharedPtr> consecutive_empty_rowsets; |
1429 | 3 | std::vector<RowsetSharedPtr> candidate_rowsets = |
1430 | 3 | pick_candidate_rowsets_to_cumulative_compaction(); |
1431 | 3 | int len = candidate_rowsets.size(); |
1432 | 7 | for (int i = 0; i < len - 1; ++i) { |
1433 | 5 | auto rowset = candidate_rowsets[i]; |
1434 | 5 | auto next_rowset = candidate_rowsets[i + 1]; |
1435 | | |
1436 | | // identify two consecutive rowsets that are empty |
1437 | 5 | if (rowset->num_segments() == 0 && next_rowset->num_segments() == 0 && |
1438 | 5 | !rowset->rowset_meta()->has_delete_predicate() && |
1439 | 5 | !next_rowset->rowset_meta()->has_delete_predicate() && |
1440 | 5 | rowset->end_version() == next_rowset->start_version() - 1) { |
1441 | 1 | consecutive_empty_rowsets.emplace_back(rowset); |
1442 | 1 | consecutive_empty_rowsets.emplace_back(next_rowset); |
1443 | 1 | rowset = next_rowset; |
1444 | 1 | int next_index = i + 2; |
1445 | | |
1446 | | // keep searching for consecutive empty rowsets |
1447 | 6 | while (next_index < len && candidate_rowsets[next_index]->num_segments() == 0 && |
1448 | 6 | !candidate_rowsets[next_index]->rowset_meta()->has_delete_predicate() && |
1449 | 6 | rowset->end_version() == candidate_rowsets[next_index]->start_version() - 1) { |
1450 | 5 | consecutive_empty_rowsets.emplace_back(candidate_rowsets[next_index]); |
1451 | 5 | rowset = candidate_rowsets[next_index++]; |
1452 | 5 | } |
1453 | | // if the number of consecutive empty rowset reach the limit, |
1454 | | // and there are still rowsets following them |
1455 | 1 | if (consecutive_empty_rowsets.size() >= limit && next_index < len) { |
1456 | 1 | return consecutive_empty_rowsets; |
1457 | 1 | } else { |
1458 | | // current rowset is not empty, start searching from that rowset in the next |
1459 | 0 | i = next_index - 1; |
1460 | 0 | consecutive_empty_rowsets.clear(); |
1461 | 0 | } |
1462 | 1 | } |
1463 | 5 | } |
1464 | | |
1465 | 2 | return consecutive_empty_rowsets; |
1466 | 3 | } |
1467 | | |
1468 | | std::vector<RowsetSharedPtr> Tablet::pick_candidate_rowsets_to_build_inverted_index( |
1469 | 0 | const std::set<int64_t>& alter_index_uids, bool is_drop_op) { |
1470 | 0 | std::vector<RowsetSharedPtr> candidate_rowsets; |
1471 | 0 | { |
1472 | 0 | std::shared_lock rlock(_meta_lock); |
1473 | 0 | auto has_alter_inverted_index = [&](RowsetSharedPtr rowset) -> bool { |
1474 | 0 | for (const auto& index_id : alter_index_uids) { |
1475 | 0 | if (rowset->tablet_schema()->has_inverted_index_with_index_id(index_id, "")) { |
1476 | 0 | return true; |
1477 | 0 | } |
1478 | 0 | } |
1479 | 0 | return false; |
1480 | 0 | }; |
1481 | |
|
1482 | 0 | for (const auto& [version, rs] : _rs_version_map) { |
1483 | 0 | if (!has_alter_inverted_index(rs) && is_drop_op) { |
1484 | 0 | continue; |
1485 | 0 | } |
1486 | 0 | if (has_alter_inverted_index(rs) && !is_drop_op) { |
1487 | 0 | continue; |
1488 | 0 | } |
1489 | | |
1490 | 0 | if (rs->is_local()) { |
1491 | 0 | candidate_rowsets.push_back(rs); |
1492 | 0 | } |
1493 | 0 | } |
1494 | 0 | } |
1495 | 0 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
1496 | 0 | return candidate_rowsets; |
1497 | 0 | } |
1498 | | |
1499 | 0 | std::string Tablet::_get_rowset_info_str(RowsetSharedPtr rowset, bool delete_flag) { |
1500 | 0 | const Version& ver = rowset->version(); |
1501 | 0 | std::string disk_size = PrettyPrinter::print( |
1502 | 0 | static_cast<uint64_t>(rowset->rowset_meta()->total_disk_size()), TUnit::BYTES); |
1503 | 0 | return strings::Substitute("[$0-$1] $2 $3 $4 $5 $6", ver.first, ver.second, |
1504 | 0 | rowset->num_segments(), (delete_flag ? "DELETE" : "DATA"), |
1505 | 0 | SegmentsOverlapPB_Name(rowset->rowset_meta()->segments_overlap()), |
1506 | 0 | rowset->rowset_id().to_string(), disk_size); |
1507 | 0 | } |
1508 | | |
1509 | 33 | std::tuple<int64_t, int64_t> Tablet::get_visible_version_and_time() const { |
1510 | | // some old tablet has bug, its partition_id is 0, fe couldn't update its visible version. |
1511 | | // so let this tablet's visible version become int64 max. |
1512 | 33 | auto version_info = std::atomic_load_explicit(&_visible_version, std::memory_order_relaxed); |
1513 | 33 | if (version_info != nullptr && partition_id() != 0) { |
1514 | 1 | return std::make_tuple(version_info->version.load(std::memory_order_relaxed), |
1515 | 1 | version_info->update_ts); |
1516 | 32 | } else { |
1517 | 32 | return std::make_tuple(std::numeric_limits<int64_t>::max(), |
1518 | 32 | std::numeric_limits<int64_t>::max()); |
1519 | 32 | } |
1520 | 33 | } |
1521 | | |
1522 | | // For http compaction action |
1523 | 0 | void Tablet::get_compaction_status(std::string* json_result) { |
1524 | 0 | rapidjson::Document root; |
1525 | 0 | root.SetObject(); |
1526 | |
|
1527 | 0 | rapidjson::Document path_arr; |
1528 | 0 | path_arr.SetArray(); |
1529 | |
|
1530 | 0 | std::vector<RowsetSharedPtr> rowsets; |
1531 | 0 | std::vector<RowsetSharedPtr> stale_rowsets; |
1532 | 0 | std::vector<bool> delete_flags; |
1533 | 0 | { |
1534 | 0 | std::shared_lock rdlock(_meta_lock); |
1535 | 0 | rowsets.reserve(_rs_version_map.size()); |
1536 | 0 | for (auto& it : _rs_version_map) { |
1537 | 0 | rowsets.push_back(it.second); |
1538 | 0 | } |
1539 | 0 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
1540 | |
|
1541 | 0 | stale_rowsets.reserve(_stale_rs_version_map.size()); |
1542 | 0 | for (auto& it : _stale_rs_version_map) { |
1543 | 0 | stale_rowsets.push_back(it.second); |
1544 | 0 | } |
1545 | 0 | std::sort(stale_rowsets.begin(), stale_rowsets.end(), Rowset::comparator); |
1546 | |
|
1547 | 0 | delete_flags.reserve(rowsets.size()); |
1548 | 0 | for (auto& rs : rowsets) { |
1549 | 0 | delete_flags.push_back(rs->rowset_meta()->has_delete_predicate()); |
1550 | 0 | } |
1551 | | // get snapshot version path json_doc |
1552 | 0 | _timestamped_version_tracker.get_stale_version_path_json_doc(path_arr); |
1553 | 0 | } |
1554 | 0 | rapidjson::Value cumulative_policy_type; |
1555 | 0 | std::string policy_type_str = "cumulative compaction policy not initializied"; |
1556 | 0 | if (_cumulative_compaction_policy != nullptr) { |
1557 | 0 | policy_type_str = _cumulative_compaction_policy->name(); |
1558 | 0 | } |
1559 | 0 | cumulative_policy_type.SetString(policy_type_str.c_str(), policy_type_str.length(), |
1560 | 0 | root.GetAllocator()); |
1561 | 0 | root.AddMember("cumulative policy type", cumulative_policy_type, root.GetAllocator()); |
1562 | 0 | root.AddMember("cumulative point", _cumulative_point.load(), root.GetAllocator()); |
1563 | 0 | rapidjson::Value cumu_value; |
1564 | 0 | std::string format_str = ToStringFromUnixMillis(_last_cumu_compaction_failure_millis.load()); |
1565 | 0 | cumu_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1566 | 0 | root.AddMember("last cumulative failure time", cumu_value, root.GetAllocator()); |
1567 | 0 | rapidjson::Value base_value; |
1568 | 0 | format_str = ToStringFromUnixMillis(_last_base_compaction_failure_millis.load()); |
1569 | 0 | base_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1570 | 0 | root.AddMember("last base failure time", base_value, root.GetAllocator()); |
1571 | 0 | rapidjson::Value full_value; |
1572 | 0 | format_str = ToStringFromUnixMillis(_last_full_compaction_failure_millis.load()); |
1573 | 0 | full_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1574 | 0 | root.AddMember("last full failure time", full_value, root.GetAllocator()); |
1575 | 0 | rapidjson::Value cumu_success_value; |
1576 | 0 | format_str = ToStringFromUnixMillis(_last_cumu_compaction_success_millis.load()); |
1577 | 0 | cumu_success_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1578 | 0 | root.AddMember("last cumulative success time", cumu_success_value, root.GetAllocator()); |
1579 | 0 | rapidjson::Value base_success_value; |
1580 | 0 | format_str = ToStringFromUnixMillis(_last_base_compaction_success_millis.load()); |
1581 | 0 | base_success_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1582 | 0 | root.AddMember("last base success time", base_success_value, root.GetAllocator()); |
1583 | 0 | rapidjson::Value full_success_value; |
1584 | 0 | format_str = ToStringFromUnixMillis(_last_full_compaction_success_millis.load()); |
1585 | 0 | full_success_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1586 | 0 | root.AddMember("last full success time", full_success_value, root.GetAllocator()); |
1587 | 0 | rapidjson::Value base_schedule_value; |
1588 | 0 | format_str = ToStringFromUnixMillis(_last_base_compaction_schedule_millis.load()); |
1589 | 0 | base_schedule_value.SetString(format_str.c_str(), format_str.length(), root.GetAllocator()); |
1590 | 0 | root.AddMember("last base schedule time", base_schedule_value, root.GetAllocator()); |
1591 | 0 | rapidjson::Value base_compaction_status_value; |
1592 | 0 | base_compaction_status_value.SetString(_last_base_compaction_status.c_str(), |
1593 | 0 | _last_base_compaction_status.length(), |
1594 | 0 | root.GetAllocator()); |
1595 | 0 | root.AddMember("last base status", base_compaction_status_value, root.GetAllocator()); |
1596 | | |
1597 | | // last single replica compaction status |
1598 | | // "single replica compaction status": { |
1599 | | // "remote peer": "172.100.1.0:10875", |
1600 | | // "last failure status": "", |
1601 | | // "last fetched rowset": "[8-10]" |
1602 | | // } |
1603 | 0 | rapidjson::Document status; |
1604 | 0 | status.SetObject(); |
1605 | 0 | TReplicaInfo replica_info; |
1606 | 0 | std::string dummp_token; |
1607 | 0 | if (tablet_meta()->tablet_schema()->enable_single_replica_compaction() && |
1608 | 0 | StorageEngine::instance()->get_peer_replica_info(tablet_id(), &replica_info, |
1609 | 0 | &dummp_token)) { |
1610 | | // remote peer |
1611 | 0 | rapidjson::Value peer_addr; |
1612 | 0 | std::string addr = replica_info.host + ":" + std::to_string(replica_info.brpc_port); |
1613 | 0 | peer_addr.SetString(addr.c_str(), addr.length(), status.GetAllocator()); |
1614 | 0 | status.AddMember("remote peer", peer_addr, status.GetAllocator()); |
1615 | | // last failure status |
1616 | 0 | rapidjson::Value compaction_status; |
1617 | 0 | compaction_status.SetString(_last_single_compaction_failure_status.c_str(), |
1618 | 0 | _last_single_compaction_failure_status.length(), |
1619 | 0 | status.GetAllocator()); |
1620 | 0 | status.AddMember("last failure status", compaction_status, status.GetAllocator()); |
1621 | | // last fetched rowset |
1622 | 0 | rapidjson::Value version; |
1623 | 0 | std::string fetched_version = _last_fetched_version.to_string(); |
1624 | 0 | version.SetString(fetched_version.c_str(), fetched_version.length(), status.GetAllocator()); |
1625 | 0 | status.AddMember("last fetched rowset", version, status.GetAllocator()); |
1626 | 0 | root.AddMember("single replica compaction status", status, root.GetAllocator()); |
1627 | 0 | } |
1628 | | |
1629 | | // print all rowsets' version as an array |
1630 | 0 | rapidjson::Document versions_arr; |
1631 | 0 | rapidjson::Document missing_versions_arr; |
1632 | 0 | versions_arr.SetArray(); |
1633 | 0 | missing_versions_arr.SetArray(); |
1634 | 0 | int64_t last_version = -1; |
1635 | 0 | for (int i = 0; i < rowsets.size(); ++i) { |
1636 | 0 | const Version& ver = rowsets[i]->version(); |
1637 | 0 | if (ver.first != last_version + 1) { |
1638 | 0 | rapidjson::Value miss_value; |
1639 | 0 | miss_value.SetString( |
1640 | 0 | strings::Substitute("[$0-$1]", last_version + 1, ver.first - 1).c_str(), |
1641 | 0 | missing_versions_arr.GetAllocator()); |
1642 | 0 | missing_versions_arr.PushBack(miss_value, missing_versions_arr.GetAllocator()); |
1643 | 0 | } |
1644 | 0 | rapidjson::Value value; |
1645 | 0 | std::string version_str = _get_rowset_info_str(rowsets[i], delete_flags[i]); |
1646 | 0 | value.SetString(version_str.c_str(), version_str.length(), versions_arr.GetAllocator()); |
1647 | 0 | versions_arr.PushBack(value, versions_arr.GetAllocator()); |
1648 | 0 | last_version = ver.second; |
1649 | 0 | } |
1650 | 0 | root.AddMember("rowsets", versions_arr, root.GetAllocator()); |
1651 | 0 | root.AddMember("missing_rowsets", missing_versions_arr, root.GetAllocator()); |
1652 | | |
1653 | | // print all stale rowsets' version as an array |
1654 | 0 | rapidjson::Document stale_versions_arr; |
1655 | 0 | stale_versions_arr.SetArray(); |
1656 | 0 | for (int i = 0; i < stale_rowsets.size(); ++i) { |
1657 | 0 | const Version& ver = stale_rowsets[i]->version(); |
1658 | 0 | rapidjson::Value value; |
1659 | 0 | std::string disk_size = PrettyPrinter::print( |
1660 | 0 | static_cast<uint64_t>(stale_rowsets[i]->rowset_meta()->total_disk_size()), |
1661 | 0 | TUnit::BYTES); |
1662 | 0 | std::string version_str = strings::Substitute( |
1663 | 0 | "[$0-$1] $2 $3 $4", ver.first, ver.second, stale_rowsets[i]->num_segments(), |
1664 | 0 | stale_rowsets[i]->rowset_id().to_string(), disk_size); |
1665 | 0 | value.SetString(version_str.c_str(), version_str.length(), |
1666 | 0 | stale_versions_arr.GetAllocator()); |
1667 | 0 | stale_versions_arr.PushBack(value, stale_versions_arr.GetAllocator()); |
1668 | 0 | } |
1669 | 0 | root.AddMember("stale_rowsets", stale_versions_arr, root.GetAllocator()); |
1670 | | |
1671 | | // add stale version rowsets |
1672 | 0 | root.AddMember("stale version path", path_arr, root.GetAllocator()); |
1673 | | |
1674 | | // to json string |
1675 | 0 | rapidjson::StringBuffer strbuf; |
1676 | 0 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf); |
1677 | 0 | root.Accept(writer); |
1678 | 0 | *json_result = std::string(strbuf.GetString()); |
1679 | 0 | } |
1680 | | |
1681 | 0 | bool Tablet::do_tablet_meta_checkpoint() { |
1682 | 0 | std::lock_guard<std::shared_mutex> store_lock(_meta_store_lock); |
1683 | 0 | if (_newly_created_rowset_num == 0) { |
1684 | 0 | return false; |
1685 | 0 | } |
1686 | 0 | if (UnixMillis() - _last_checkpoint_time < |
1687 | 0 | config::tablet_meta_checkpoint_min_interval_secs * 1000 && |
1688 | 0 | _newly_created_rowset_num < config::tablet_meta_checkpoint_min_new_rowsets_num) { |
1689 | 0 | return false; |
1690 | 0 | } |
1691 | | |
1692 | | // hold read-lock other than write-lock, because it will not modify meta structure |
1693 | 0 | std::shared_lock rdlock(_meta_lock); |
1694 | 0 | if (tablet_state() != TABLET_RUNNING) { |
1695 | 0 | LOG(INFO) << "tablet is under state=" << tablet_state() |
1696 | 0 | << ", not running, skip do checkpoint" |
1697 | 0 | << ", tablet=" << tablet_id(); |
1698 | 0 | return false; |
1699 | 0 | } |
1700 | 0 | VLOG_NOTICE << "start to do tablet meta checkpoint, tablet=" << tablet_id(); |
1701 | 0 | save_meta(); |
1702 | | // if save meta successfully, then should remove the rowset meta existing in tablet |
1703 | | // meta from rowset meta store |
1704 | 0 | for (auto& rs_meta : _tablet_meta->all_rs_metas()) { |
1705 | | // If we delete it from rowset manager's meta explicitly in previous checkpoint, just skip. |
1706 | 0 | if (rs_meta->is_remove_from_rowset_meta()) { |
1707 | 0 | continue; |
1708 | 0 | } |
1709 | 0 | if (RowsetMetaManager::check_rowset_meta(_data_dir->get_meta(), tablet_uid(), |
1710 | 0 | rs_meta->rowset_id())) { |
1711 | 0 | RETURN_FALSE_IF_ERROR(RowsetMetaManager::remove(_data_dir->get_meta(), tablet_uid(), |
1712 | 0 | rs_meta->rowset_id())); |
1713 | 0 | VLOG_NOTICE << "remove rowset id from meta store because it is already persistent with " |
1714 | 0 | << "tablet meta, rowset_id=" << rs_meta->rowset_id(); |
1715 | 0 | } |
1716 | 0 | rs_meta->set_remove_from_rowset_meta(); |
1717 | 0 | } |
1718 | | |
1719 | | // check _stale_rs_version_map to remove meta from rowset meta store |
1720 | 0 | for (auto& rs_meta : _tablet_meta->all_stale_rs_metas()) { |
1721 | | // If we delete it from rowset manager's meta explicitly in previous checkpoint, just skip. |
1722 | 0 | if (rs_meta->is_remove_from_rowset_meta()) { |
1723 | 0 | continue; |
1724 | 0 | } |
1725 | 0 | if (RowsetMetaManager::check_rowset_meta(_data_dir->get_meta(), tablet_uid(), |
1726 | 0 | rs_meta->rowset_id())) { |
1727 | 0 | RETURN_FALSE_IF_ERROR(RowsetMetaManager::remove(_data_dir->get_meta(), tablet_uid(), |
1728 | 0 | rs_meta->rowset_id())); |
1729 | 0 | VLOG_NOTICE << "remove rowset id from meta store because it is already persistent with " |
1730 | 0 | << "tablet meta, rowset_id=" << rs_meta->rowset_id(); |
1731 | 0 | } |
1732 | 0 | rs_meta->set_remove_from_rowset_meta(); |
1733 | 0 | } |
1734 | | |
1735 | 0 | if (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write()) { |
1736 | 0 | static_cast<void>(TabletMetaManager::remove_old_version_delete_bitmap( |
1737 | 0 | _data_dir, tablet_id(), max_version_unlocked().second)); |
1738 | 0 | } |
1739 | |
|
1740 | 0 | _newly_created_rowset_num = 0; |
1741 | 0 | _last_checkpoint_time = UnixMillis(); |
1742 | 0 | return true; |
1743 | 0 | } |
1744 | | |
1745 | 0 | bool Tablet::rowset_meta_is_useful(RowsetMetaSharedPtr rowset_meta) { |
1746 | 0 | std::shared_lock rdlock(_meta_lock); |
1747 | 0 | bool find_version = false; |
1748 | 0 | for (auto& version_rowset : _rs_version_map) { |
1749 | 0 | if (version_rowset.second->rowset_id() == rowset_meta->rowset_id()) { |
1750 | 0 | return true; |
1751 | 0 | } |
1752 | 0 | if (version_rowset.second->contains_version(rowset_meta->version())) { |
1753 | 0 | find_version = true; |
1754 | 0 | } |
1755 | 0 | } |
1756 | 0 | for (auto& stale_version_rowset : _stale_rs_version_map) { |
1757 | 0 | if (stale_version_rowset.second->rowset_id() == rowset_meta->rowset_id()) { |
1758 | 0 | return true; |
1759 | 0 | } |
1760 | 0 | if (stale_version_rowset.second->contains_version(rowset_meta->version())) { |
1761 | 0 | find_version = true; |
1762 | 0 | } |
1763 | 0 | } |
1764 | 0 | return !find_version; |
1765 | 0 | } |
1766 | | |
1767 | 157 | bool Tablet::_contains_rowset(const RowsetId rowset_id) { |
1768 | 569 | for (auto& version_rowset : _rs_version_map) { |
1769 | 569 | if (version_rowset.second->rowset_id() == rowset_id) { |
1770 | 0 | return true; |
1771 | 0 | } |
1772 | 569 | } |
1773 | 157 | for (auto& stale_version_rowset : _stale_rs_version_map) { |
1774 | 0 | if (stale_version_rowset.second->rowset_id() == rowset_id) { |
1775 | 0 | return true; |
1776 | 0 | } |
1777 | 0 | } |
1778 | 157 | return false; |
1779 | 157 | } |
1780 | | |
1781 | | // need check if consecutive version missing in full report |
1782 | | // alter tablet will ignore this check |
1783 | | void Tablet::build_tablet_report_info(TTabletInfo* tablet_info, |
1784 | | bool enable_consecutive_missing_check, |
1785 | 0 | bool enable_path_check) { |
1786 | 0 | std::shared_lock rdlock(_meta_lock); |
1787 | 0 | tablet_info->__set_tablet_id(_tablet_meta->tablet_id()); |
1788 | 0 | tablet_info->__set_schema_hash(_tablet_meta->schema_hash()); |
1789 | 0 | tablet_info->__set_row_count(_tablet_meta->num_rows()); |
1790 | 0 | tablet_info->__set_data_size(_tablet_meta->tablet_local_size()); |
1791 | | |
1792 | | // Here we need to report to FE if there are any missing versions of tablet. |
1793 | | // We start from the initial version and traverse backwards until we meet a discontinuous version. |
1794 | 0 | Version cversion; |
1795 | 0 | Version max_version; |
1796 | 0 | bool has_version_cross; |
1797 | 0 | _max_continuous_version_from_beginning_unlocked(&cversion, &max_version, &has_version_cross); |
1798 | | // cause publish version task runs concurrently, version may be flying |
1799 | | // so we add a consecutive miss check to solve this problem: |
1800 | | // if publish version 5 arrives but version 4 flying, we may judge replica miss version |
1801 | | // and set version miss in tablet_info, which makes fe treat this replica as unhealth |
1802 | | // and lead to other problems |
1803 | 0 | if (enable_consecutive_missing_check) { |
1804 | 0 | if (cversion.second < max_version.second) { |
1805 | 0 | if (_last_missed_version == cversion.second + 1) { |
1806 | 0 | if (MonotonicSeconds() - _last_missed_time_s >= 60) { |
1807 | | // version missed for over 60 seconds |
1808 | 0 | tablet_info->__set_version_miss(true); |
1809 | 0 | _last_missed_version = -1; |
1810 | 0 | _last_missed_time_s = 0; |
1811 | 0 | } |
1812 | 0 | } else { |
1813 | 0 | _last_missed_version = cversion.second + 1; |
1814 | 0 | _last_missed_time_s = MonotonicSeconds(); |
1815 | 0 | } |
1816 | 0 | } |
1817 | 0 | } else { |
1818 | 0 | tablet_info->__set_version_miss(cversion.second < max_version.second); |
1819 | 0 | } |
1820 | |
|
1821 | 0 | DBUG_EXECUTE_IF("Tablet.build_tablet_report_info.version_miss", { |
1822 | 0 | auto tablet_id = dp->param<int64>("tablet_id", -1); |
1823 | 0 | if (tablet_id != -1 && tablet_id == _tablet_meta->tablet_id()) { |
1824 | 0 | auto miss = dp->param<bool>("version_miss", true); |
1825 | 0 | tablet_info->__set_version_miss(miss); |
1826 | 0 | } |
1827 | 0 | }); |
1828 | | |
1829 | | // find rowset with max version |
1830 | 0 | auto iter = _rs_version_map.find(max_version); |
1831 | 0 | if (iter == _rs_version_map.end()) { |
1832 | | // If the tablet is in running state, it must not be doing schema-change. so if we can not |
1833 | | // access its rowsets, it means that the tablet is bad and needs to be reported to the FE |
1834 | | // for subsequent repairs (through the cloning task) |
1835 | 0 | if (tablet_state() == TABLET_RUNNING) { |
1836 | 0 | tablet_info->__set_used(false); |
1837 | 0 | } |
1838 | | // For other states, FE knows that the tablet is in a certain change process, so here |
1839 | | // still sets the state to normal when reporting. Note that every task has an timeout, |
1840 | | // so if the task corresponding to this change hangs, when the task timeout, FE will know |
1841 | | // and perform state modification operations. |
1842 | 0 | } |
1843 | |
|
1844 | 0 | if (tablet_state() == TABLET_RUNNING) { |
1845 | 0 | if (has_version_cross || is_io_error_too_times() || !data_dir()->is_used()) { |
1846 | 0 | LOG(INFO) << "report " << tablet_id() << " as bad, version_cross=" << has_version_cross |
1847 | 0 | << ", ioe times=" << get_io_error_times() << ", data_dir used " |
1848 | 0 | << data_dir()->is_used(); |
1849 | 0 | tablet_info->__set_used(false); |
1850 | 0 | } |
1851 | |
|
1852 | 0 | if (enable_path_check) { |
1853 | 0 | if (!_is_tablet_path_exists.exchange(true, std::memory_order_relaxed)) { |
1854 | 0 | LOG(INFO) << "report " << tablet_id() << " as bad, tablet directory not found"; |
1855 | 0 | tablet_info->__set_used(false); |
1856 | 0 | } |
1857 | 0 | } |
1858 | 0 | } |
1859 | | |
1860 | | // There are two cases when tablet state is TABLET_NOTREADY |
1861 | | // case 1: tablet is doing schema change. Fe knows it's state, doing nothing. |
1862 | | // case 2: tablet has finished schema change, but failed. Fe will perform recovery. |
1863 | 0 | if (tablet_state() == TABLET_NOTREADY && is_alter_failed()) { |
1864 | 0 | tablet_info->__set_used(false); |
1865 | 0 | } |
1866 | |
|
1867 | 0 | if (tablet_state() == TABLET_SHUTDOWN) { |
1868 | 0 | tablet_info->__set_used(false); |
1869 | 0 | } |
1870 | |
|
1871 | 0 | DBUG_EXECUTE_IF("Tablet.build_tablet_report_info.used", { |
1872 | 0 | auto tablet_id = dp->param<int64>("tablet_id", -1); |
1873 | 0 | if (tablet_id != -1 && tablet_id == _tablet_meta->tablet_id()) { |
1874 | 0 | auto used = dp->param<bool>("used", true); |
1875 | 0 | LOG_WARNING("Tablet.build_tablet_report_info.used") |
1876 | 0 | .tag("tablet id", tablet_id) |
1877 | 0 | .tag("used", used); |
1878 | 0 | tablet_info->__set_used(used); |
1879 | 0 | } else { |
1880 | 0 | LOG_WARNING("Tablet.build_tablet_report_info.used").tag("tablet id", tablet_id); |
1881 | 0 | } |
1882 | 0 | }); |
1883 | |
|
1884 | 0 | int64_t total_version_count = _tablet_meta->version_count(); |
1885 | | |
1886 | | // For compatibility. |
1887 | | // For old fe, it wouldn't send visible version request to be, then be's visible version is always 0. |
1888 | | // Let visible_version_count set to total_version_count in be's report. |
1889 | 0 | int64_t visible_version_count = total_version_count; |
1890 | 0 | if (auto [visible_version, _] = get_visible_version_and_time(); visible_version > 0) { |
1891 | 0 | visible_version_count = _tablet_meta->version_count_cross_with_range({0, visible_version}); |
1892 | 0 | } |
1893 | | // the report version is the largest continuous version, same logic as in FE side |
1894 | 0 | tablet_info->__set_version(cversion.second); |
1895 | | // Useless but it is a required filed in TTabletInfo |
1896 | 0 | tablet_info->__set_version_hash(0); |
1897 | 0 | tablet_info->__set_partition_id(_tablet_meta->partition_id()); |
1898 | 0 | tablet_info->__set_storage_medium(_data_dir->storage_medium()); |
1899 | 0 | tablet_info->__set_total_version_count(total_version_count); |
1900 | 0 | tablet_info->__set_visible_version_count(visible_version_count); |
1901 | 0 | tablet_info->__set_path_hash(_data_dir->path_hash()); |
1902 | 0 | tablet_info->__set_is_in_memory(_tablet_meta->tablet_schema()->is_in_memory()); |
1903 | 0 | tablet_info->__set_replica_id(replica_id()); |
1904 | 0 | tablet_info->__set_remote_data_size(_tablet_meta->tablet_remote_size()); |
1905 | 0 | if (_tablet_meta->cooldown_meta_id().initialized()) { // has cooldowned data |
1906 | 0 | tablet_info->__set_cooldown_term(_cooldown_conf.term); |
1907 | 0 | tablet_info->__set_cooldown_meta_id(_tablet_meta->cooldown_meta_id().to_thrift()); |
1908 | 0 | } |
1909 | 0 | if (tablet_state() == TABLET_RUNNING && _tablet_meta->storage_policy_id() > 0) { |
1910 | | // tablet may not have cooldowned data, but the storage policy is set |
1911 | 0 | tablet_info->__set_cooldown_term(_cooldown_conf.term); |
1912 | 0 | } |
1913 | 0 | } |
1914 | | |
1915 | | // should use this method to get a copy of current tablet meta |
1916 | | // there are some rowset meta in local meta store and in in-memory tablet meta |
1917 | | // but not in tablet meta in local meta store |
1918 | 0 | void Tablet::generate_tablet_meta_copy(TabletMetaSharedPtr new_tablet_meta) const { |
1919 | 0 | std::shared_lock rdlock(_meta_lock); |
1920 | 0 | generate_tablet_meta_copy_unlocked(new_tablet_meta); |
1921 | 0 | } |
1922 | | |
1923 | | // this is a unlocked version of generate_tablet_meta_copy() |
1924 | | // some method already hold the _meta_lock before calling this, |
1925 | | // such as EngineCloneTask::_finish_clone -> tablet->revise_tablet_meta |
1926 | 2 | void Tablet::generate_tablet_meta_copy_unlocked(TabletMetaSharedPtr new_tablet_meta) const { |
1927 | 2 | TabletMetaPB tablet_meta_pb; |
1928 | 2 | _tablet_meta->to_meta_pb(&tablet_meta_pb); |
1929 | 2 | new_tablet_meta->init_from_pb(tablet_meta_pb); |
1930 | 2 | } |
1931 | | |
1932 | | Status Tablet::prepare_compaction_and_calculate_permits(CompactionType compaction_type, |
1933 | | const TabletSharedPtr& tablet, |
1934 | | std::shared_ptr<Compaction>& compaction, |
1935 | 11 | int64_t& permits) { |
1936 | 11 | if (compaction_type == CompactionType::CUMULATIVE_COMPACTION) { |
1937 | 11 | MonotonicStopWatch watch; |
1938 | 11 | watch.start(); |
1939 | | |
1940 | 11 | compaction = std::make_shared<CumulativeCompaction>(tablet); |
1941 | 11 | DorisMetrics::instance()->cumulative_compaction_request_total->increment(1); |
1942 | 11 | Status res = compaction->prepare_compact(); |
1943 | 11 | if (!config::disable_compaction_trace_log && |
1944 | 11 | watch.elapsed_time() / 1e9 > config::cumulative_compaction_trace_threshold) { |
1945 | 0 | std::stringstream ss; |
1946 | 0 | compaction->runtime_profile()->pretty_print(&ss); |
1947 | 0 | LOG(WARNING) << "prepare cumulative compaction cost " << watch.elapsed_time() / 1e9 |
1948 | 0 | << std::endl |
1949 | 0 | << ss.str(); |
1950 | 0 | } |
1951 | | |
1952 | 11 | if (!res.ok()) { |
1953 | 1 | tablet->set_last_cumu_compaction_failure_time(UnixMillis()); |
1954 | 1 | permits = 0; |
1955 | 1 | if (!res.is<CUMULATIVE_NO_SUITABLE_VERSION>()) { |
1956 | 0 | DorisMetrics::instance()->cumulative_compaction_request_failed->increment(1); |
1957 | 0 | return Status::InternalError("prepare cumulative compaction with err: {}", res); |
1958 | 0 | } |
1959 | | // return OK if OLAP_ERR_CUMULATIVE_NO_SUITABLE_VERSION, so that we don't need to |
1960 | | // print too much useless logs. |
1961 | | // And because we set permits to 0, so even if we return OK here, nothing will be done. |
1962 | 1 | return Status::OK(); |
1963 | 1 | } |
1964 | 11 | } else if (compaction_type == CompactionType::BASE_COMPACTION) { |
1965 | 0 | MonotonicStopWatch watch; |
1966 | 0 | watch.start(); |
1967 | |
|
1968 | 0 | compaction = std::make_shared<BaseCompaction>(tablet); |
1969 | 0 | DorisMetrics::instance()->base_compaction_request_total->increment(1); |
1970 | 0 | Status res = compaction->prepare_compact(); |
1971 | 0 | if (!config::disable_compaction_trace_log && |
1972 | 0 | watch.elapsed_time() / 1e9 > config::base_compaction_trace_threshold) { |
1973 | 0 | std::stringstream ss; |
1974 | 0 | compaction->runtime_profile()->pretty_print(&ss); |
1975 | 0 | LOG(WARNING) << "prepare base compaction cost " << watch.elapsed_time() / 1e9 |
1976 | 0 | << std::endl |
1977 | 0 | << ss.str(); |
1978 | 0 | } |
1979 | |
|
1980 | 0 | tablet->set_last_base_compaction_status(res.to_string()); |
1981 | 0 | if (!res.ok()) { |
1982 | 0 | tablet->set_last_base_compaction_failure_time(UnixMillis()); |
1983 | 0 | permits = 0; |
1984 | 0 | if (!res.is<BE_NO_SUITABLE_VERSION>()) { |
1985 | 0 | DorisMetrics::instance()->base_compaction_request_failed->increment(1); |
1986 | 0 | return Status::InternalError("prepare base compaction with err: {}", res); |
1987 | 0 | } |
1988 | | // return OK if OLAP_ERR_BE_NO_SUITABLE_VERSION, so that we don't need to |
1989 | | // print too much useless logs. |
1990 | | // And because we set permits to 0, so even if we return OK here, nothing will be done. |
1991 | 0 | return Status::OK(); |
1992 | 0 | } |
1993 | 0 | } else { |
1994 | 0 | DCHECK_EQ(compaction_type, CompactionType::FULL_COMPACTION); |
1995 | |
|
1996 | 0 | compaction = std::make_shared<FullCompaction>(tablet); |
1997 | 0 | Status res = compaction->prepare_compact(); |
1998 | 0 | if (!res.ok()) { |
1999 | 0 | tablet->set_last_full_compaction_failure_time(UnixMillis()); |
2000 | 0 | permits = 0; |
2001 | 0 | if (!res.is<FULL_NO_SUITABLE_VERSION>()) { |
2002 | 0 | return Status::InternalError("prepare full compaction with err: {}", res); |
2003 | 0 | } |
2004 | | // return OK if OLAP_ERR_BE_NO_SUITABLE_VERSION, so that we don't need to |
2005 | | // print too much useless logs. |
2006 | | // And because we set permits to 0, so even if we return OK here, nothing will be done. |
2007 | 0 | return Status::OK(); |
2008 | 0 | } |
2009 | 0 | } |
2010 | | |
2011 | 10 | permits = 0; |
2012 | | // Time series policy does not rely on permits, it uses goal size to control memory |
2013 | 10 | if (tablet->tablet_meta()->compaction_policy() != CUMULATIVE_TIME_SERIES_POLICY) { |
2014 | 280 | for (auto&& rowset : compaction->input_rowsets()) { |
2015 | 280 | permits += rowset->rowset_meta()->get_compaction_score(); |
2016 | 280 | } |
2017 | 10 | } else { |
2018 | | // permits = 0 means that prepare_compaction failed |
2019 | 0 | permits = 1; |
2020 | 0 | } |
2021 | 10 | return Status::OK(); |
2022 | 11 | } |
2023 | | |
2024 | 0 | void Tablet::execute_single_replica_compaction(SingleReplicaCompaction& compaction) { |
2025 | 0 | Status res = compaction.execute_compact(); |
2026 | 0 | if (!res.ok()) { |
2027 | 0 | set_last_failure_time(this, compaction, UnixMillis()); |
2028 | 0 | set_last_single_compaction_failure_status(res.to_string()); |
2029 | 0 | if (res.is<CANCELLED>()) { |
2030 | 0 | DorisMetrics::instance()->single_compaction_request_cancelled->increment(1); |
2031 | 0 | VLOG_CRITICAL << "Cannel fetching from the remote peer. res=" << res |
2032 | 0 | << ", tablet=" << tablet_id(); |
2033 | 0 | } else { |
2034 | 0 | DorisMetrics::instance()->single_compaction_request_failed->increment(1); |
2035 | 0 | LOG(WARNING) << "failed to do single replica compaction. res=" << res |
2036 | 0 | << ", tablet=" << tablet_id(); |
2037 | 0 | } |
2038 | 0 | return; |
2039 | 0 | } |
2040 | 0 | set_last_failure_time(this, compaction, 0); |
2041 | 0 | } |
2042 | | |
2043 | 0 | bool Tablet::should_fetch_from_peer() { |
2044 | 0 | return tablet_meta()->tablet_schema()->enable_single_replica_compaction() && |
2045 | 0 | StorageEngine::instance()->should_fetch_from_peer(tablet_id()); |
2046 | 0 | } |
2047 | | |
2048 | 3 | std::vector<Version> Tablet::get_all_local_versions() { |
2049 | 3 | std::vector<Version> local_versions; |
2050 | 3 | { |
2051 | 3 | std::shared_lock rlock(_meta_lock); |
2052 | 63 | for (const auto& [version, rs] : _rs_version_map) { |
2053 | 63 | if (rs->is_local()) { |
2054 | 53 | local_versions.emplace_back(version); |
2055 | 53 | } |
2056 | 63 | } |
2057 | 3 | } |
2058 | 3 | std::sort(local_versions.begin(), local_versions.end(), |
2059 | 318 | [](const Version& left, const Version& right) { return left.first < right.first; }); |
2060 | 3 | return local_versions; |
2061 | 3 | } |
2062 | | |
2063 | 0 | void Tablet::execute_compaction(Compaction& compaction) { |
2064 | 0 | signal::tablet_id = tablet_id(); |
2065 | |
|
2066 | 0 | MonotonicStopWatch watch; |
2067 | 0 | watch.start(); |
2068 | |
|
2069 | 0 | Status res = [&]() { RETURN_IF_CATCH_EXCEPTION({ return compaction.execute_compact(); }); }(); |
2070 | 0 | if (!res.ok()) [[unlikely]] { |
2071 | 0 | set_last_failure_time(this, compaction, UnixMillis()); |
2072 | 0 | LOG(WARNING) << "failed to do " << compaction.compaction_name() |
2073 | 0 | << ", tablet=" << tablet_id() << " : " << res; |
2074 | 0 | } else { |
2075 | 0 | set_last_failure_time(this, compaction, 0); |
2076 | 0 | } |
2077 | |
|
2078 | 0 | if (!config::disable_compaction_trace_log) { |
2079 | 0 | auto need_trace = [&compaction, &watch] { |
2080 | 0 | return compaction.compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION |
2081 | 0 | ? watch.elapsed_time() / 1e9 > |
2082 | 0 | config::cumulative_compaction_trace_threshold |
2083 | 0 | : compaction.compaction_type() == ReaderType::READER_BASE_COMPACTION |
2084 | 0 | ? watch.elapsed_time() / 1e9 > config::base_compaction_trace_threshold |
2085 | 0 | : false; |
2086 | 0 | }; |
2087 | 0 | if (need_trace()) { |
2088 | 0 | std::stringstream ss; |
2089 | 0 | compaction.runtime_profile()->pretty_print(&ss); |
2090 | 0 | LOG(WARNING) << "execute " << compaction.compaction_name() << " cost " |
2091 | 0 | << watch.elapsed_time() / 1e9 << std::endl |
2092 | 0 | << ss.str(); |
2093 | 0 | } |
2094 | 0 | } |
2095 | 0 | } |
2096 | | |
2097 | 69 | Status Tablet::create_initial_rowset(const int64_t req_version) { |
2098 | 69 | if (req_version < 1) { |
2099 | 0 | return Status::Error<CE_CMD_PARAMS_ERROR>( |
2100 | 0 | "init version of tablet should at least 1. req.ver={}", req_version); |
2101 | 0 | } |
2102 | 69 | Version version(0, req_version); |
2103 | 69 | RowsetSharedPtr new_rowset; |
2104 | | // there is no data in init rowset, so overlapping info is unknown. |
2105 | 69 | RowsetWriterContext context; |
2106 | 69 | context.version = version; |
2107 | 69 | context.rowset_state = VISIBLE; |
2108 | 69 | context.segments_overlap = OVERLAP_UNKNOWN; |
2109 | 69 | context.tablet_schema = tablet_schema(); |
2110 | 69 | context.newest_write_timestamp = UnixSeconds(); |
2111 | 69 | auto rs_writer = DORIS_TRY(create_rowset_writer(context, false)); |
2112 | 69 | RETURN_IF_ERROR(rs_writer->flush()); |
2113 | 69 | RETURN_IF_ERROR(rs_writer->build(new_rowset)); |
2114 | 69 | RETURN_IF_ERROR(add_rowset(std::move(new_rowset))); |
2115 | 69 | set_cumulative_layer_point(req_version + 1); |
2116 | 69 | return Status::OK(); |
2117 | 69 | } |
2118 | | |
2119 | | Result<std::unique_ptr<RowsetWriter>> Tablet::create_rowset_writer(RowsetWriterContext& context, |
2120 | 97 | bool vertical) { |
2121 | 97 | context.rowset_id = _engine.next_rowset_id(); |
2122 | 97 | _init_context_common_fields(context); |
2123 | 97 | std::unique_ptr<RowsetWriter> rowset_writer; |
2124 | 97 | if (auto st = RowsetFactory::create_rowset_writer(context, vertical, &rowset_writer); !st.ok()) |
2125 | 0 | [[unlikely]] { |
2126 | 0 | return unexpected(std::move(st)); |
2127 | 0 | } |
2128 | 97 | return rowset_writer; |
2129 | 97 | } |
2130 | | |
2131 | | // create a rowset writer with rowset_id and seg_id |
2132 | | // after writer, merge this transient rowset with original rowset |
2133 | | Status Tablet::create_transient_rowset_writer( |
2134 | | RowsetSharedPtr rowset_ptr, std::unique_ptr<RowsetWriter>* rowset_writer, |
2135 | 2 | std::shared_ptr<PartialUpdateInfo> partial_update_info) { |
2136 | 2 | RowsetWriterContext context; |
2137 | 2 | context.rowset_state = PREPARED; |
2138 | 2 | context.segments_overlap = OVERLAPPING; |
2139 | 2 | context.tablet_schema = std::make_shared<TabletSchema>(); |
2140 | | // During a partial update, the extracted columns of a variant should not be included in the tablet schema. |
2141 | | // This is because the partial update for a variant needs to ignore the extracted columns. |
2142 | | // Otherwise, the schema types in different rowsets might be inconsistent. When performing a partial update, |
2143 | | // the complete variant is constructed by reading all the sub-columns of the variant. |
2144 | 2 | context.tablet_schema = rowset_ptr->tablet_schema()->copy_without_variant_extracted_columns(); |
2145 | 2 | context.newest_write_timestamp = UnixSeconds(); |
2146 | 2 | context.tablet_id = table_id(); |
2147 | 2 | context.enable_segcompaction = false; |
2148 | | // ATTN: context.tablet is a shared_ptr, can't simply set it's value to `this`. We should |
2149 | | // get the shared_ptr from tablet_manager. |
2150 | 2 | auto tablet = _engine.tablet_manager()->get_tablet(tablet_id()); |
2151 | 2 | if (!tablet) { |
2152 | 0 | LOG(WARNING) << "cant find tablet by tablet_id=" << tablet_id(); |
2153 | 0 | return Status::NotFound(fmt::format("cant find tablet by tablet_id= {}", tablet_id())); |
2154 | 0 | } |
2155 | 2 | context.tablet = tablet; |
2156 | 2 | context.write_type = DataWriteType::TYPE_DIRECT; |
2157 | 2 | context.partial_update_info = partial_update_info; |
2158 | 2 | context.is_transient_rowset_writer = true; |
2159 | 2 | RETURN_IF_ERROR( |
2160 | 2 | create_transient_rowset_writer(context, rowset_ptr->rowset_id(), rowset_writer)); |
2161 | 2 | (*rowset_writer)->set_segment_start_id(rowset_ptr->num_segments()); |
2162 | 2 | return Status::OK(); |
2163 | 2 | } |
2164 | | |
2165 | | Status Tablet::create_transient_rowset_writer(RowsetWriterContext& context, |
2166 | | const RowsetId& rowset_id, |
2167 | 2 | std::unique_ptr<RowsetWriter>* rowset_writer) { |
2168 | 2 | context.rowset_id = rowset_id; |
2169 | 2 | _init_context_common_fields(context); |
2170 | 2 | return RowsetFactory::create_rowset_writer(context, false, rowset_writer); |
2171 | 2 | } |
2172 | | |
2173 | 99 | void Tablet::_init_context_common_fields(RowsetWriterContext& context) { |
2174 | 99 | context.tablet_uid = tablet_uid(); |
2175 | | |
2176 | 99 | context.tablet_id = tablet_id(); |
2177 | 99 | context.partition_id = partition_id(); |
2178 | 99 | context.tablet_schema_hash = schema_hash(); |
2179 | 99 | context.rowset_type = tablet_meta()->preferred_rowset_type(); |
2180 | | // Alpha Rowset will be removed in the future, so that if the tablet's default rowset type is |
2181 | | // alpah rowset, then set the newly created rowset to storage engine's default rowset. |
2182 | 99 | if (context.rowset_type == ALPHA_ROWSET) { |
2183 | 0 | context.rowset_type = _engine.default_rowset_type(); |
2184 | 0 | } |
2185 | 99 | if (context.fs != nullptr && context.fs->type() != io::FileSystemType::LOCAL) { |
2186 | 0 | context.rowset_dir = remote_tablet_path(tablet_id()); |
2187 | 99 | } else { |
2188 | 99 | context.rowset_dir = tablet_path(); |
2189 | 99 | } |
2190 | 99 | context.data_dir = data_dir(); |
2191 | 99 | context.enable_unique_key_merge_on_write = enable_unique_key_merge_on_write(); |
2192 | 99 | } |
2193 | | |
2194 | 205 | Status Tablet::create_rowset(const RowsetMetaSharedPtr& rowset_meta, RowsetSharedPtr* rowset) { |
2195 | 205 | return RowsetFactory::create_rowset( |
2196 | 205 | _tablet_meta->tablet_schema(), |
2197 | 205 | rowset_meta->is_local() ? _tablet_path : remote_tablet_path(tablet_id()), rowset_meta, |
2198 | 205 | rowset); |
2199 | 205 | } |
2200 | | |
2201 | 7 | Status Tablet::cooldown(RowsetSharedPtr rowset) { |
2202 | 7 | std::unique_lock schema_change_lock(_schema_change_lock, std::try_to_lock); |
2203 | 7 | if (!schema_change_lock.owns_lock()) { |
2204 | 0 | return Status::Error<TRY_LOCK_FAILED>("try schema_change_lock failed"); |
2205 | 0 | } |
2206 | | // Check executing serially with compaction task. |
2207 | 7 | std::unique_lock base_compaction_lock(_base_compaction_lock, std::try_to_lock); |
2208 | 7 | if (!base_compaction_lock.owns_lock()) { |
2209 | 0 | return Status::Error<TRY_LOCK_FAILED>("try base_compaction_lock failed"); |
2210 | 0 | } |
2211 | 7 | std::unique_lock cumu_compaction_lock(_cumulative_compaction_lock, std::try_to_lock); |
2212 | 7 | if (!cumu_compaction_lock.owns_lock()) { |
2213 | 0 | return Status::Error<TRY_LOCK_FAILED>("try cumu_compaction_lock failed"); |
2214 | 0 | } |
2215 | 7 | std::shared_lock cooldown_conf_rlock(_cooldown_conf_lock); |
2216 | 7 | if (_cooldown_conf.cooldown_replica_id <= 0) { // wait for FE to push cooldown conf |
2217 | 2 | return Status::InternalError("invalid cooldown_replica_id"); |
2218 | 2 | } |
2219 | | |
2220 | 5 | if (_cooldown_conf.cooldown_replica_id == replica_id()) { |
2221 | | // this replica is cooldown replica |
2222 | 5 | RETURN_IF_ERROR(_cooldown_data(std::move(rowset))); |
2223 | 5 | } else { |
2224 | 0 | Status st = _follow_cooldowned_data(); |
2225 | 0 | if (UNLIKELY(!st.ok())) { |
2226 | 0 | _last_failed_follow_cooldown_time = time(nullptr); |
2227 | 0 | return st; |
2228 | 0 | } |
2229 | 0 | _last_failed_follow_cooldown_time = 0; |
2230 | 0 | } |
2231 | 5 | return Status::OK(); |
2232 | 5 | } |
2233 | | |
2234 | | // hold SHARED `cooldown_conf_lock` |
2235 | 5 | Status Tablet::_cooldown_data(RowsetSharedPtr rowset) { |
2236 | 5 | DCHECK(_cooldown_conf.cooldown_replica_id == replica_id()); |
2237 | | |
2238 | 5 | std::shared_ptr<io::RemoteFileSystem> dest_fs; |
2239 | 5 | RETURN_IF_ERROR(get_remote_file_system(storage_policy_id(), &dest_fs)); |
2240 | 5 | RowsetSharedPtr old_rowset = nullptr; |
2241 | | |
2242 | 5 | if (rowset) { |
2243 | 0 | const auto& rowset_id = rowset->rowset_id(); |
2244 | 0 | const auto& rowset_version = rowset->version(); |
2245 | 0 | std::shared_lock meta_rlock(_meta_lock); |
2246 | 0 | auto iter = _rs_version_map.find(rowset_version); |
2247 | 0 | if (iter != _rs_version_map.end() && iter->second->rowset_id() == rowset_id) { |
2248 | 0 | old_rowset = rowset; |
2249 | 0 | } |
2250 | 0 | } |
2251 | | |
2252 | 5 | if (!old_rowset) { |
2253 | 5 | old_rowset = pick_cooldown_rowset(); |
2254 | 5 | } |
2255 | | |
2256 | 5 | if (!old_rowset) { |
2257 | 0 | LOG(INFO) << "cannot pick cooldown rowset in tablet " << tablet_id(); |
2258 | 0 | return Status::OK(); |
2259 | 0 | } |
2260 | | |
2261 | 5 | RowsetId new_rowset_id = _engine.next_rowset_id(); |
2262 | 5 | auto pending_rs_guard = _engine.pending_remote_rowsets().add(new_rowset_id); |
2263 | 5 | Status st; |
2264 | 5 | Defer defer {[&] { |
2265 | 5 | if (!st.ok()) { |
2266 | | // reclaim the incomplete rowset data in remote storage |
2267 | 0 | record_unused_remote_rowset(new_rowset_id, dest_fs->id(), old_rowset->num_segments()); |
2268 | 0 | } |
2269 | 5 | }}; |
2270 | 5 | auto start = std::chrono::steady_clock::now(); |
2271 | 5 | if (st = old_rowset->upload_to(dest_fs.get(), new_rowset_id); !st.ok()) { |
2272 | 0 | return st; |
2273 | 0 | } |
2274 | | |
2275 | 5 | auto duration = std::chrono::duration<float>(std::chrono::steady_clock::now() - start); |
2276 | 5 | LOG(INFO) << "Upload rowset " << old_rowset->version() << " " << new_rowset_id.to_string() |
2277 | 5 | << " to " << dest_fs->root_path().native() << ", tablet_id=" << tablet_id() |
2278 | 5 | << ", duration=" << duration.count() << ", capacity=" << old_rowset->data_disk_size() |
2279 | 5 | << ", tp=" << old_rowset->data_disk_size() / duration.count() |
2280 | 5 | << ", old rowset_id=" << old_rowset->rowset_id().to_string(); |
2281 | | |
2282 | | // gen a new rowset |
2283 | 5 | auto new_rowset_meta = std::make_shared<RowsetMeta>(); |
2284 | 5 | new_rowset_meta->init(old_rowset->rowset_meta().get()); |
2285 | 5 | new_rowset_meta->set_rowset_id(new_rowset_id); |
2286 | 5 | new_rowset_meta->set_fs(dest_fs); |
2287 | 5 | new_rowset_meta->set_creation_time(time(nullptr)); |
2288 | 5 | UniqueId cooldown_meta_id = UniqueId::gen_uid(); |
2289 | 5 | RowsetSharedPtr new_rowset; |
2290 | 5 | RETURN_IF_ERROR(RowsetFactory::create_rowset(_tablet_meta->tablet_schema(), |
2291 | 5 | remote_tablet_path(tablet_id()), new_rowset_meta, |
2292 | 5 | &new_rowset)); |
2293 | | |
2294 | 5 | { |
2295 | 5 | std::unique_lock meta_wlock(_meta_lock); |
2296 | 5 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
2297 | 5 | if (tablet_state() == TABLET_RUNNING) { |
2298 | 5 | RETURN_IF_ERROR(delete_rowsets({std::move(old_rowset)}, false)); |
2299 | 5 | add_rowsets({std::move(new_rowset)}); |
2300 | | // TODO(plat1ko): process primary key |
2301 | 5 | _tablet_meta->set_cooldown_meta_id(cooldown_meta_id); |
2302 | 5 | } |
2303 | 5 | } |
2304 | 5 | { |
2305 | 5 | std::shared_lock meta_rlock(_meta_lock); |
2306 | 5 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
2307 | 5 | save_meta(); |
2308 | 5 | } |
2309 | | // Upload cooldowned rowset meta to remote fs |
2310 | | // ATTN: Even if it is an empty rowset, in order for the followers to synchronize, the coolown meta must be |
2311 | | // uploaded, otherwise followers may never completely cooldown. |
2312 | 5 | if (auto t = _engine.tablet_manager()->get_tablet(tablet_id()); |
2313 | 5 | t != nullptr) { // `t` can be nullptr if it has been dropped |
2314 | 5 | async_write_cooldown_meta(std::move(t)); |
2315 | 5 | } |
2316 | 5 | return Status::OK(); |
2317 | 5 | } |
2318 | | |
2319 | | // hold SHARED `cooldown_conf_lock` |
2320 | | Status Tablet::_read_cooldown_meta(const std::shared_ptr<io::RemoteFileSystem>& fs, |
2321 | 0 | TabletMetaPB* tablet_meta_pb) { |
2322 | 0 | std::string remote_meta_path = remote_tablet_meta_path( |
2323 | 0 | tablet_id(), _cooldown_conf.cooldown_replica_id, _cooldown_conf.term); |
2324 | 0 | io::FileReaderSPtr tablet_meta_reader; |
2325 | 0 | RETURN_IF_ERROR(fs->open_file(remote_meta_path, &tablet_meta_reader)); |
2326 | 0 | auto file_size = tablet_meta_reader->size(); |
2327 | 0 | size_t bytes_read; |
2328 | 0 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[file_size]); |
2329 | 0 | RETURN_IF_ERROR(tablet_meta_reader->read_at(0, {buf.get(), file_size}, &bytes_read)); |
2330 | 0 | RETURN_IF_ERROR(tablet_meta_reader->close()); |
2331 | 0 | if (!tablet_meta_pb->ParseFromArray(buf.get(), file_size)) { |
2332 | 0 | return Status::InternalError("malformed tablet meta, path={}/{}", fs->root_path().native(), |
2333 | 0 | remote_meta_path); |
2334 | 0 | } |
2335 | 0 | return Status::OK(); |
2336 | 0 | } |
2337 | | |
2338 | | // `rs_metas` MUST already be sorted by `RowsetMeta::comparator` |
2339 | 5 | Status check_version_continuity(const std::vector<RowsetMetaSharedPtr>& rs_metas) { |
2340 | 5 | if (rs_metas.size() < 2) { |
2341 | 2 | return Status::OK(); |
2342 | 2 | } |
2343 | 3 | auto prev = rs_metas.begin(); |
2344 | 7 | for (auto it = rs_metas.begin() + 1; it != rs_metas.end(); ++it) { |
2345 | 4 | if ((*prev)->end_version() + 1 != (*it)->start_version()) { |
2346 | 0 | return Status::InternalError("versions are not continuity: prev={} cur={}", |
2347 | 0 | (*prev)->version().to_string(), |
2348 | 0 | (*it)->version().to_string()); |
2349 | 0 | } |
2350 | 4 | prev = it; |
2351 | 4 | } |
2352 | 3 | return Status::OK(); |
2353 | 3 | } |
2354 | | |
2355 | | // It's guaranteed the write cooldown meta task would be invoked at the end unless BE crashes |
2356 | | // one tablet would at most have one async task to be done |
2357 | 5 | void Tablet::async_write_cooldown_meta(TabletSharedPtr tablet) { |
2358 | 5 | ExecEnv::GetInstance()->write_cooldown_meta_executors()->submit(std::move(tablet)); |
2359 | 5 | } |
2360 | | |
2361 | 2 | bool Tablet::update_cooldown_conf(int64_t cooldown_term, int64_t cooldown_replica_id) { |
2362 | 2 | std::unique_lock wlock(_cooldown_conf_lock, std::try_to_lock); |
2363 | 2 | if (!wlock.owns_lock()) { |
2364 | 0 | LOG(INFO) << "try cooldown_conf_lock failed, tablet_id=" << tablet_id(); |
2365 | 0 | return false; |
2366 | 0 | } |
2367 | 2 | if (cooldown_term <= _cooldown_conf.term) { |
2368 | 0 | return false; |
2369 | 0 | } |
2370 | 2 | LOG(INFO) << "update cooldown conf. tablet_id=" << tablet_id() |
2371 | 2 | << " cooldown_replica_id: " << _cooldown_conf.cooldown_replica_id << " -> " |
2372 | 2 | << cooldown_replica_id << ", cooldown_term: " << _cooldown_conf.term << " -> " |
2373 | 2 | << cooldown_term; |
2374 | 2 | _cooldown_conf.cooldown_replica_id = cooldown_replica_id; |
2375 | 2 | _cooldown_conf.term = cooldown_term; |
2376 | 2 | return true; |
2377 | 2 | } |
2378 | | |
2379 | 5 | Status Tablet::write_cooldown_meta() { |
2380 | 5 | std::shared_lock rlock(_cooldown_conf_lock); |
2381 | 5 | if (_cooldown_conf.cooldown_replica_id != _tablet_meta->replica_id()) { |
2382 | 0 | return Status::Aborted<false>("not cooldown replica({} vs {}) tablet_id={}", |
2383 | 0 | _tablet_meta->replica_id(), |
2384 | 0 | _cooldown_conf.cooldown_replica_id, tablet_id()); |
2385 | 0 | } |
2386 | | |
2387 | 5 | std::shared_ptr<io::RemoteFileSystem> fs; |
2388 | 5 | RETURN_IF_ERROR(get_remote_file_system(storage_policy_id(), &fs)); |
2389 | | |
2390 | 5 | std::vector<RowsetMetaSharedPtr> cooldowned_rs_metas; |
2391 | 5 | UniqueId cooldown_meta_id; |
2392 | 5 | { |
2393 | 5 | std::shared_lock meta_rlock(_meta_lock); |
2394 | 11 | for (auto& rs_meta : _tablet_meta->all_rs_metas()) { |
2395 | 11 | if (!rs_meta->is_local()) { |
2396 | 9 | cooldowned_rs_metas.push_back(rs_meta); |
2397 | 9 | } |
2398 | 11 | } |
2399 | 5 | cooldown_meta_id = _tablet_meta->cooldown_meta_id(); |
2400 | 5 | } |
2401 | 5 | if (cooldowned_rs_metas.empty()) { |
2402 | 0 | LOG(INFO) << "no cooldown meta to write, tablet_id=" << tablet_id(); |
2403 | 0 | return Status::OK(); |
2404 | 0 | } |
2405 | 5 | std::sort(cooldowned_rs_metas.begin(), cooldowned_rs_metas.end(), RowsetMeta::comparator); |
2406 | 5 | DCHECK(cooldowned_rs_metas.front()->start_version() == 0); |
2407 | | // If version not continuous, it must be a bug |
2408 | 5 | if (auto st = check_version_continuity(cooldowned_rs_metas); !st.ok()) { |
2409 | 0 | DCHECK(st.ok()) << st << " tablet_id=" << tablet_id(); |
2410 | 0 | st.set_code(ABORTED); |
2411 | 0 | return st; |
2412 | 0 | } |
2413 | | |
2414 | 5 | TabletMetaPB tablet_meta_pb; |
2415 | 5 | auto rs_metas = tablet_meta_pb.mutable_rs_metas(); |
2416 | 5 | rs_metas->Reserve(cooldowned_rs_metas.size()); |
2417 | 9 | for (auto& rs_meta : cooldowned_rs_metas) { |
2418 | 9 | rs_metas->Add(rs_meta->get_rowset_pb()); |
2419 | 9 | } |
2420 | 5 | tablet_meta_pb.mutable_cooldown_meta_id()->set_hi(cooldown_meta_id.hi); |
2421 | 5 | tablet_meta_pb.mutable_cooldown_meta_id()->set_lo(cooldown_meta_id.lo); |
2422 | | |
2423 | 5 | std::string remote_meta_path = remote_tablet_meta_path( |
2424 | 5 | tablet_id(), _cooldown_conf.cooldown_replica_id, _cooldown_conf.term); |
2425 | 5 | io::FileWriterPtr tablet_meta_writer; |
2426 | | // FIXME(plat1ko): What if object store permanently unavailable? |
2427 | 5 | RETURN_IF_ERROR(fs->create_file(remote_meta_path, &tablet_meta_writer)); |
2428 | 5 | auto val = tablet_meta_pb.SerializeAsString(); |
2429 | 5 | RETURN_IF_ERROR(tablet_meta_writer->append({val.data(), val.size()})); |
2430 | 5 | return tablet_meta_writer->close(); |
2431 | 5 | } |
2432 | | |
2433 | | // hold SHARED `cooldown_conf_lock` |
2434 | 0 | Status Tablet::_follow_cooldowned_data() { |
2435 | 0 | DCHECK(_cooldown_conf.cooldown_replica_id != replica_id()); |
2436 | 0 | LOG(INFO) << "try to follow cooldowned data. tablet_id=" << tablet_id() |
2437 | 0 | << " cooldown_replica_id=" << _cooldown_conf.cooldown_replica_id |
2438 | 0 | << " local replica=" << replica_id(); |
2439 | |
|
2440 | 0 | std::shared_ptr<io::RemoteFileSystem> fs; |
2441 | 0 | RETURN_IF_ERROR(get_remote_file_system(storage_policy_id(), &fs)); |
2442 | | // MUST executing serially with cold data compaction, because compaction input rowsets may be deleted by this function |
2443 | 0 | std::unique_lock cold_compaction_lock(_cold_compaction_lock, std::try_to_lock); |
2444 | 0 | if (!cold_compaction_lock.owns_lock()) { |
2445 | 0 | return Status::Error<TRY_LOCK_FAILED>("try cold_compaction_lock failed"); |
2446 | 0 | } |
2447 | | |
2448 | 0 | TabletMetaPB cooldown_meta_pb; |
2449 | 0 | auto st = _read_cooldown_meta(fs, &cooldown_meta_pb); |
2450 | 0 | if (!st.ok()) { |
2451 | 0 | LOG(INFO) << "cannot read cooldown meta: " << st; |
2452 | 0 | return Status::InternalError<false>("cannot read cooldown meta"); |
2453 | 0 | } |
2454 | 0 | DCHECK(cooldown_meta_pb.rs_metas_size() > 0); |
2455 | 0 | if (_tablet_meta->cooldown_meta_id() == cooldown_meta_pb.cooldown_meta_id()) { |
2456 | | // cooldowned rowsets are same, no need to follow |
2457 | 0 | return Status::OK(); |
2458 | 0 | } |
2459 | | |
2460 | 0 | int64_t cooldowned_version = cooldown_meta_pb.rs_metas().rbegin()->end_version(); |
2461 | |
|
2462 | 0 | std::vector<RowsetSharedPtr> overlap_rowsets; |
2463 | 0 | bool version_aligned = false; |
2464 | |
|
2465 | 0 | { |
2466 | 0 | std::lock_guard wlock(_meta_lock); |
2467 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
2468 | 0 | if (tablet_state() != TABLET_RUNNING) { |
2469 | 0 | return Status::InternalError<false>("tablet not running"); |
2470 | 0 | } |
2471 | | |
2472 | 0 | for (auto& [v, rs] : _rs_version_map) { |
2473 | 0 | if (v.second <= cooldowned_version) { |
2474 | 0 | overlap_rowsets.push_back(rs); |
2475 | 0 | if (!version_aligned && v.second == cooldowned_version) { |
2476 | 0 | version_aligned = true; |
2477 | 0 | } |
2478 | 0 | } else if (!rs->is_local()) { |
2479 | 0 | return Status::InternalError<false>( |
2480 | 0 | "cooldowned version larger than that to follow with cooldown version {}", |
2481 | 0 | cooldowned_version); |
2482 | 0 | } |
2483 | 0 | } |
2484 | | |
2485 | 0 | if (!version_aligned) { |
2486 | 0 | return Status::InternalError<false>("cooldowned version is not aligned with version {}", |
2487 | 0 | cooldowned_version); |
2488 | 0 | } |
2489 | | |
2490 | 0 | std::sort(overlap_rowsets.begin(), overlap_rowsets.end(), Rowset::comparator); |
2491 | 0 | auto rs_pb_it = cooldown_meta_pb.rs_metas().begin(); |
2492 | 0 | auto rs_it = overlap_rowsets.begin(); |
2493 | 0 | for (; rs_pb_it != cooldown_meta_pb.rs_metas().end() && rs_it != overlap_rowsets.end(); |
2494 | 0 | ++rs_pb_it, ++rs_it) { |
2495 | | // skip cooldowned rowset with same version in BE |
2496 | 0 | if ((*rs_it)->is_local() || rs_pb_it->end_version() != (*rs_it)->end_version()) { |
2497 | 0 | break; |
2498 | 0 | } |
2499 | 0 | } |
2500 | 0 | std::vector<RowsetSharedPtr> to_delete(rs_it, overlap_rowsets.end()); |
2501 | 0 | std::vector<RowsetSharedPtr> to_add; |
2502 | 0 | to_add.reserve(cooldown_meta_pb.rs_metas().end() - rs_pb_it); |
2503 | 0 | for (; rs_pb_it != cooldown_meta_pb.rs_metas().end(); ++rs_pb_it) { |
2504 | 0 | auto rs_meta = std::make_shared<RowsetMeta>(); |
2505 | 0 | rs_meta->init_from_pb(*rs_pb_it); |
2506 | 0 | RowsetSharedPtr rs; |
2507 | 0 | RETURN_IF_ERROR(RowsetFactory::create_rowset( |
2508 | 0 | _tablet_meta->tablet_schema(), remote_tablet_path(tablet_id()), rs_meta, &rs)); |
2509 | 0 | to_add.push_back(std::move(rs)); |
2510 | 0 | } |
2511 | | // Note: We CANNOT call `modify_rowsets` here because `modify_rowsets` cannot process version graph correctly. |
2512 | 0 | RETURN_IF_ERROR(delete_rowsets(to_delete, false)); |
2513 | 0 | add_rowsets(to_add); |
2514 | | // TODO(plat1ko): process primary key |
2515 | 0 | _tablet_meta->set_cooldown_meta_id(cooldown_meta_pb.cooldown_meta_id()); |
2516 | 0 | } |
2517 | 0 | { |
2518 | 0 | std::lock_guard rlock(_meta_lock); |
2519 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
2520 | 0 | save_meta(); |
2521 | 0 | } |
2522 | |
|
2523 | 0 | return Status::OK(); |
2524 | 0 | } |
2525 | | |
2526 | 10 | bool Tablet::_has_data_to_cooldown() { |
2527 | 10 | int64_t min_local_version = std::numeric_limits<int64_t>::max(); |
2528 | 10 | RowsetSharedPtr rowset; |
2529 | 10 | std::shared_lock meta_rlock(_meta_lock); |
2530 | | // Ususally once the tablet has done cooldown successfully then the first |
2531 | | // rowset would always be remote rowset |
2532 | 10 | bool has_cooldowned = false; |
2533 | 29 | for (const auto& [_, rs] : _rs_version_map) { |
2534 | 29 | if (!rs->is_local()) { |
2535 | 3 | has_cooldowned = true; |
2536 | 3 | break; |
2537 | 3 | } |
2538 | 29 | } |
2539 | 32 | for (auto& [v, rs] : _rs_version_map) { |
2540 | 32 | auto predicate = rs->is_local() && v.first < min_local_version; |
2541 | 32 | if (!has_cooldowned) { |
2542 | 25 | predicate = predicate && (rs->data_disk_size() > 0); |
2543 | 25 | } |
2544 | 32 | if (predicate) { |
2545 | | // this is a local rowset and has data |
2546 | 25 | min_local_version = v.first; |
2547 | 25 | rowset = rs; |
2548 | 25 | } |
2549 | 32 | } |
2550 | | |
2551 | 10 | int64_t newest_cooldown_time = 0; |
2552 | 10 | if (rowset != nullptr) { |
2553 | 9 | newest_cooldown_time = _get_newest_cooldown_time(rowset); |
2554 | 9 | } |
2555 | | |
2556 | 10 | return (newest_cooldown_time != 0) && (newest_cooldown_time < UnixSeconds()); |
2557 | 10 | } |
2558 | | |
2559 | 9 | RowsetSharedPtr Tablet::pick_cooldown_rowset() { |
2560 | 9 | RowsetSharedPtr rowset; |
2561 | | |
2562 | 9 | if (!_has_data_to_cooldown()) { |
2563 | 1 | return nullptr; |
2564 | 1 | } |
2565 | | |
2566 | | // TODO(plat1ko): should we maintain `cooldowned_version` in `Tablet`? |
2567 | 8 | int64_t cooldowned_version = -1; |
2568 | | // We pick the rowset with smallest start version in local. |
2569 | 8 | int64_t min_local_version = std::numeric_limits<int64_t>::max(); |
2570 | 8 | { |
2571 | 8 | std::shared_lock meta_rlock(_meta_lock); |
2572 | 26 | for (auto& [v, rs] : _rs_version_map) { |
2573 | 26 | if (!rs->is_local()) { |
2574 | 4 | cooldowned_version = std::max(cooldowned_version, v.second); |
2575 | 22 | } else if (v.first < min_local_version) { // this is a local rowset |
2576 | 22 | min_local_version = v.first; |
2577 | 22 | rowset = rs; |
2578 | 22 | } |
2579 | 26 | } |
2580 | 8 | } |
2581 | 8 | if (!rowset) { |
2582 | 0 | return nullptr; |
2583 | 0 | } |
2584 | 8 | if (tablet_footprint() == 0) { |
2585 | 0 | VLOG_DEBUG << "skip cooldown due to empty tablet_id = " << tablet_id(); |
2586 | 0 | return nullptr; |
2587 | 0 | } |
2588 | 8 | if (min_local_version != cooldowned_version + 1) { // ensure version continuity |
2589 | 0 | if (UNLIKELY(cooldowned_version != -1)) { |
2590 | 0 | LOG(WARNING) << "version not continuous. tablet_id=" << tablet_id() |
2591 | 0 | << " cooldowned_version=" << cooldowned_version |
2592 | 0 | << " min_local_version=" << min_local_version; |
2593 | 0 | } |
2594 | 0 | return nullptr; |
2595 | 0 | } |
2596 | 8 | return rowset; |
2597 | 8 | } |
2598 | | |
2599 | 12 | int64_t Tablet::_get_newest_cooldown_time(const RowsetSharedPtr& rowset) { |
2600 | 12 | int64_t id = storage_policy_id(); |
2601 | 12 | if (id <= 0) { |
2602 | 0 | VLOG_DEBUG << "tablet does not need cooldown, tablet id: " << tablet_id(); |
2603 | 0 | return 0; |
2604 | 0 | } |
2605 | 12 | auto storage_policy = get_storage_policy(id); |
2606 | 12 | if (!storage_policy) { |
2607 | 0 | LOG(WARNING) << "Cannot get storage policy: " << id; |
2608 | 0 | return 0; |
2609 | 0 | } |
2610 | 12 | auto cooldown_ttl_sec = storage_policy->cooldown_ttl; |
2611 | 12 | auto cooldown_datetime = storage_policy->cooldown_datetime; |
2612 | 12 | int64_t newest_cooldown_time = std::numeric_limits<int64_t>::max(); |
2613 | | |
2614 | 12 | if (cooldown_ttl_sec >= 0) { |
2615 | 9 | newest_cooldown_time = rowset->newest_write_timestamp() + cooldown_ttl_sec; |
2616 | 9 | } |
2617 | 12 | if (cooldown_datetime > 0) { |
2618 | 10 | newest_cooldown_time = std::min(newest_cooldown_time, cooldown_datetime); |
2619 | 10 | } |
2620 | | |
2621 | 12 | return newest_cooldown_time; |
2622 | 12 | } |
2623 | | |
2624 | 4 | RowsetSharedPtr Tablet::need_cooldown(int64_t* cooldown_timestamp, size_t* file_size) { |
2625 | 4 | RowsetSharedPtr rowset = pick_cooldown_rowset(); |
2626 | 4 | if (!rowset) { |
2627 | 1 | VLOG_DEBUG << "pick cooldown rowset, get null, tablet id: " << tablet_id(); |
2628 | 1 | return nullptr; |
2629 | 1 | } |
2630 | | |
2631 | 3 | auto newest_cooldown_time = _get_newest_cooldown_time(rowset); |
2632 | | |
2633 | | // the rowset should do cooldown job only if it's cooldown ttl plus newest write time is less than |
2634 | | // current time or it's datatime is less than current time |
2635 | 3 | if (newest_cooldown_time != 0 && newest_cooldown_time < UnixSeconds()) { |
2636 | 3 | *cooldown_timestamp = newest_cooldown_time; |
2637 | 3 | *file_size = rowset->data_disk_size(); |
2638 | 3 | VLOG_DEBUG << "tablet need cooldown, tablet id: " << tablet_id() |
2639 | 0 | << " file_size: " << *file_size; |
2640 | 3 | return rowset; |
2641 | 3 | } |
2642 | | |
2643 | 0 | VLOG_DEBUG << "tablet does not need cooldown, tablet id: " << tablet_id() |
2644 | 0 | << " newest write time: " << rowset->newest_write_timestamp(); |
2645 | 0 | return nullptr; |
2646 | 3 | } |
2647 | | |
2648 | | void Tablet::record_unused_remote_rowset(const RowsetId& rowset_id, const std::string& resource, |
2649 | 0 | int64_t num_segments) { |
2650 | 0 | auto gc_key = REMOTE_ROWSET_GC_PREFIX + rowset_id.to_string(); |
2651 | 0 | RemoteRowsetGcPB gc_pb; |
2652 | 0 | gc_pb.set_resource_id(resource); |
2653 | 0 | gc_pb.set_tablet_id(tablet_id()); |
2654 | 0 | gc_pb.set_num_segments(num_segments); |
2655 | 0 | auto st = |
2656 | 0 | _data_dir->get_meta()->put(META_COLUMN_FAMILY_INDEX, gc_key, gc_pb.SerializeAsString()); |
2657 | 0 | if (!st.ok()) { |
2658 | 0 | LOG(WARNING) << "failed to record unused remote rowset. tablet_id=" << tablet_id() |
2659 | 0 | << " rowset_id=" << rowset_id << " resource_id=" << resource; |
2660 | 0 | } |
2661 | 0 | } |
2662 | | |
2663 | 0 | Status Tablet::remove_all_remote_rowsets() { |
2664 | 0 | DCHECK(tablet_state() == TABLET_SHUTDOWN); |
2665 | 0 | std::set<std::string> resource_ids; |
2666 | 0 | for (auto& rs_meta : _tablet_meta->all_rs_metas()) { |
2667 | 0 | if (!rs_meta->is_local()) { |
2668 | 0 | resource_ids.insert(rs_meta->resource_id()); |
2669 | 0 | } |
2670 | 0 | } |
2671 | 0 | if (resource_ids.empty()) { |
2672 | 0 | return Status::OK(); |
2673 | 0 | } |
2674 | 0 | auto tablet_gc_key = REMOTE_TABLET_GC_PREFIX + std::to_string(tablet_id()); |
2675 | 0 | RemoteTabletGcPB gc_pb; |
2676 | 0 | for (auto& resource_id : resource_ids) { |
2677 | 0 | gc_pb.add_resource_ids(resource_id); |
2678 | 0 | } |
2679 | 0 | return _data_dir->get_meta()->put(META_COLUMN_FAMILY_INDEX, tablet_gc_key, |
2680 | 0 | gc_pb.SerializeAsString()); |
2681 | 0 | } |
2682 | | |
2683 | 0 | void Tablet::update_max_version_schema(const TabletSchemaSPtr& tablet_schema) { |
2684 | 0 | std::lock_guard wrlock(_meta_lock); |
2685 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
2686 | | // Double Check for concurrent update |
2687 | 0 | if (!_max_version_schema || |
2688 | 0 | tablet_schema->schema_version() > _max_version_schema->schema_version()) { |
2689 | 0 | _max_version_schema = tablet_schema; |
2690 | 0 | } |
2691 | 0 | } |
2692 | | |
2693 | | Status Tablet::_get_segment_column_iterator( |
2694 | | const BetaRowsetSharedPtr& rowset, uint32_t segid, const TabletColumn& target_column, |
2695 | | SegmentCacheHandle* segment_cache_handle, |
2696 | 0 | std::unique_ptr<segment_v2::ColumnIterator>* column_iterator, OlapReaderStatistics* stats) { |
2697 | 0 | RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(rowset, segment_cache_handle, true)); |
2698 | | // find segment |
2699 | 0 | auto it = std::find_if( |
2700 | 0 | segment_cache_handle->get_segments().begin(), |
2701 | 0 | segment_cache_handle->get_segments().end(), |
2702 | 0 | [&segid](const segment_v2::SegmentSharedPtr& seg) { return seg->id() == segid; }); |
2703 | 0 | if (it == segment_cache_handle->get_segments().end()) { |
2704 | 0 | return Status::NotFound(fmt::format("rowset {} 's segemnt not found, seg_id {}", |
2705 | 0 | rowset->rowset_id().to_string(), segid)); |
2706 | 0 | } |
2707 | 0 | segment_v2::SegmentSharedPtr segment = *it; |
2708 | 0 | RETURN_IF_ERROR(segment->new_column_iterator(target_column, column_iterator, nullptr)); |
2709 | 0 | segment_v2::ColumnIteratorOptions opt { |
2710 | 0 | .use_page_cache = !config::disable_storage_page_cache, |
2711 | 0 | .file_reader = segment->file_reader().get(), |
2712 | 0 | .stats = stats, |
2713 | 0 | .io_ctx = io::IOContext {.reader_type = ReaderType::READER_QUERY}, |
2714 | 0 | }; |
2715 | 0 | RETURN_IF_ERROR((*column_iterator)->init(opt)); |
2716 | 0 | return Status::OK(); |
2717 | 0 | } |
2718 | | |
2719 | | // fetch value by row column |
2720 | | Status Tablet::fetch_value_through_row_column(RowsetSharedPtr input_rowset, |
2721 | | const TabletSchema& tablet_schema, uint32_t segid, |
2722 | | const std::vector<uint32_t>& rowids, |
2723 | | const std::vector<uint32_t>& cids, |
2724 | 0 | vectorized::Block& block) { |
2725 | 0 | MonotonicStopWatch watch; |
2726 | 0 | watch.start(); |
2727 | 0 | Defer _defer([&]() { |
2728 | 0 | LOG_EVERY_N(INFO, 500) << "fetch_value_by_rowids, cost(us):" << watch.elapsed_time() / 1000 |
2729 | 0 | << ", row_batch_size:" << rowids.size(); |
2730 | 0 | }); |
2731 | |
|
2732 | 0 | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(input_rowset); |
2733 | 0 | CHECK(rowset); |
2734 | 0 | CHECK(tablet_schema.store_row_column()); |
2735 | 0 | SegmentCacheHandle segment_cache_handle; |
2736 | 0 | std::unique_ptr<segment_v2::ColumnIterator> column_iterator; |
2737 | 0 | OlapReaderStatistics stats; |
2738 | 0 | RETURN_IF_ERROR(_get_segment_column_iterator(rowset, segid, |
2739 | 0 | tablet_schema.column(BeConsts::ROW_STORE_COL), |
2740 | 0 | &segment_cache_handle, &column_iterator, &stats)); |
2741 | | // get and parse tuple row |
2742 | 0 | vectorized::MutableColumnPtr column_ptr = vectorized::ColumnString::create(); |
2743 | 0 | RETURN_IF_ERROR(column_iterator->read_by_rowids(rowids.data(), rowids.size(), column_ptr)); |
2744 | 0 | assert(column_ptr->size() == rowids.size()); |
2745 | 0 | auto string_column = static_cast<vectorized::ColumnString*>(column_ptr.get()); |
2746 | 0 | vectorized::DataTypeSerDeSPtrs serdes; |
2747 | 0 | serdes.resize(cids.size()); |
2748 | 0 | std::unordered_map<uint32_t, uint32_t> col_uid_to_idx; |
2749 | 0 | std::vector<std::string> default_values; |
2750 | 0 | default_values.resize(cids.size()); |
2751 | 0 | for (int i = 0; i < cids.size(); ++i) { |
2752 | 0 | const TabletColumn& column = tablet_schema.column(cids[i]); |
2753 | 0 | vectorized::DataTypePtr type = |
2754 | 0 | vectorized::DataTypeFactory::instance().create_data_type(column); |
2755 | 0 | col_uid_to_idx[column.unique_id()] = i; |
2756 | 0 | default_values[i] = column.default_value(); |
2757 | 0 | serdes[i] = type->get_serde(); |
2758 | 0 | } |
2759 | 0 | vectorized::JsonbSerializeUtil::jsonb_to_block(serdes, *string_column, col_uid_to_idx, block, |
2760 | 0 | default_values); |
2761 | 0 | return Status::OK(); |
2762 | 0 | } |
2763 | | |
2764 | | Status Tablet::fetch_value_by_rowids(RowsetSharedPtr input_rowset, uint32_t segid, |
2765 | | const std::vector<uint32_t>& rowids, |
2766 | | const TabletColumn& tablet_column, |
2767 | 0 | vectorized::MutableColumnPtr& dst) { |
2768 | 0 | MonotonicStopWatch watch; |
2769 | 0 | watch.start(); |
2770 | 0 | Defer _defer([&]() { |
2771 | 0 | LOG_EVERY_N(INFO, 500) << "fetch_value_by_rowids, cost(us):" << watch.elapsed_time() / 1000 |
2772 | 0 | << ", row_batch_size:" << rowids.size(); |
2773 | 0 | }); |
2774 | | |
2775 | | // read row data |
2776 | 0 | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(input_rowset); |
2777 | 0 | CHECK(rowset); |
2778 | 0 | SegmentCacheHandle segment_cache_handle; |
2779 | 0 | std::unique_ptr<segment_v2::ColumnIterator> column_iterator; |
2780 | 0 | OlapReaderStatistics stats; |
2781 | 0 | RETURN_IF_ERROR(_get_segment_column_iterator(rowset, segid, tablet_column, |
2782 | 0 | &segment_cache_handle, &column_iterator, &stats)); |
2783 | 0 | RETURN_IF_ERROR(column_iterator->read_by_rowids(rowids.data(), rowids.size(), dst)); |
2784 | 0 | return Status::OK(); |
2785 | 0 | } |
2786 | | |
2787 | | Status Tablet::lookup_row_data(const Slice& encoded_key, const RowLocation& row_location, |
2788 | | RowsetSharedPtr input_rowset, const TupleDescriptor* desc, |
2789 | | OlapReaderStatistics& stats, std::string& values, |
2790 | 0 | bool write_to_cache) { |
2791 | 0 | MonotonicStopWatch watch; |
2792 | 0 | size_t row_size = 1; |
2793 | 0 | watch.start(); |
2794 | 0 | Defer _defer([&]() { |
2795 | 0 | LOG_EVERY_N(INFO, 500) << "get a single_row, cost(us):" << watch.elapsed_time() / 1000 |
2796 | 0 | << ", row_size:" << row_size; |
2797 | 0 | }); |
2798 | |
|
2799 | 0 | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(input_rowset); |
2800 | 0 | CHECK(rowset); |
2801 | 0 | const TabletSchemaSPtr tablet_schema = rowset->tablet_schema(); |
2802 | 0 | CHECK(tablet_schema->store_row_column()); |
2803 | 0 | SegmentCacheHandle segment_cache_handle; |
2804 | 0 | std::unique_ptr<segment_v2::ColumnIterator> column_iterator; |
2805 | 0 | RETURN_IF_ERROR(_get_segment_column_iterator(rowset, row_location.segment_id, |
2806 | 0 | tablet_schema->column(BeConsts::ROW_STORE_COL), |
2807 | 0 | &segment_cache_handle, &column_iterator, &stats)); |
2808 | | // get and parse tuple row |
2809 | 0 | vectorized::MutableColumnPtr column_ptr = vectorized::ColumnString::create(); |
2810 | 0 | std::vector<segment_v2::rowid_t> rowids {static_cast<segment_v2::rowid_t>(row_location.row_id)}; |
2811 | 0 | RETURN_IF_ERROR(column_iterator->read_by_rowids(rowids.data(), 1, column_ptr)); |
2812 | 0 | assert(column_ptr->size() == 1); |
2813 | 0 | auto string_column = static_cast<vectorized::ColumnString*>(column_ptr.get()); |
2814 | 0 | StringRef value = string_column->get_data_at(0); |
2815 | 0 | values = value.to_string(); |
2816 | 0 | if (write_to_cache) { |
2817 | 0 | StringRef value = string_column->get_data_at(0); |
2818 | 0 | RowCache::instance()->insert({tablet_id(), encoded_key}, Slice {value.data, value.size}); |
2819 | 0 | } |
2820 | 0 | return Status::OK(); |
2821 | 0 | } |
2822 | | |
2823 | | Status Tablet::lookup_row_key(const Slice& encoded_key, TabletSchema* latest_schema, |
2824 | | bool with_seq_col, |
2825 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
2826 | | RowLocation* row_location, uint32_t version, |
2827 | | std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches, |
2828 | 2 | RowsetSharedPtr* rowset, bool with_rowid, bool is_partial_update) { |
2829 | 2 | SCOPED_BVAR_LATENCY(g_tablet_lookup_rowkey_latency); |
2830 | 2 | size_t seq_col_length = 0; |
2831 | | // use the latest tablet schema to decide if the tablet has sequence column currently |
2832 | 2 | const TabletSchema* schema = |
2833 | 2 | (latest_schema == nullptr ? _tablet_meta->tablet_schema().get() : latest_schema); |
2834 | 2 | if (schema->has_sequence_col() && with_seq_col) { |
2835 | 2 | seq_col_length = schema->column(schema->sequence_col_idx()).length() + 1; |
2836 | 2 | } |
2837 | 2 | size_t rowid_length = 0; |
2838 | 2 | if (with_rowid && !schema->cluster_key_idxes().empty()) { |
2839 | 0 | rowid_length = PrimaryKeyIndexReader::ROW_ID_LENGTH; |
2840 | 0 | } |
2841 | 2 | Slice key_without_seq = |
2842 | 2 | Slice(encoded_key.get_data(), encoded_key.get_size() - seq_col_length - rowid_length); |
2843 | 2 | RowLocation loc; |
2844 | | |
2845 | 2 | bool need_to_check_delete_bitmap = is_partial_update || with_seq_col; |
2846 | | |
2847 | 2 | for (size_t i = 0; i < specified_rowsets.size(); i++) { |
2848 | 2 | auto& rs = specified_rowsets[i]; |
2849 | 2 | auto& segments_key_bounds = rs->rowset_meta()->get_segments_key_bounds(); |
2850 | 2 | int num_segments = rs->num_segments(); |
2851 | 2 | DCHECK_EQ(segments_key_bounds.size(), num_segments); |
2852 | 2 | std::vector<uint32_t> picked_segments; |
2853 | 4 | for (int i = num_segments - 1; i >= 0; i--) { |
2854 | | // If mow table has cluster keys, the key bounds is short keys, not primary keys |
2855 | | // use PrimaryKeyIndexMetaPB in primary key index? |
2856 | 2 | if (schema->cluster_key_idxes().empty()) { |
2857 | 2 | if (key_without_seq.compare(segments_key_bounds[i].max_key()) > 0 || |
2858 | 2 | key_without_seq.compare(segments_key_bounds[i].min_key()) < 0) { |
2859 | 0 | continue; |
2860 | 0 | } |
2861 | 2 | } |
2862 | 2 | picked_segments.emplace_back(i); |
2863 | 2 | } |
2864 | 2 | if (picked_segments.empty()) { |
2865 | 0 | continue; |
2866 | 0 | } |
2867 | | |
2868 | 2 | if (UNLIKELY(segment_caches[i] == nullptr)) { |
2869 | 1 | segment_caches[i] = std::make_unique<SegmentCacheHandle>(); |
2870 | 1 | RETURN_IF_ERROR(SegmentLoader::instance()->load_segments( |
2871 | 1 | std::static_pointer_cast<BetaRowset>(rs), segment_caches[i].get(), true)); |
2872 | 1 | } |
2873 | 2 | auto& segments = segment_caches[i]->get_segments(); |
2874 | 2 | DCHECK_EQ(segments.size(), num_segments); |
2875 | | |
2876 | 2 | for (auto id : picked_segments) { |
2877 | 2 | Status s = segments[id]->lookup_row_key(encoded_key, schema, with_seq_col, with_rowid, |
2878 | 2 | &loc); |
2879 | 2 | if (s.is<KEY_NOT_FOUND>()) { |
2880 | 0 | continue; |
2881 | 0 | } |
2882 | 2 | if (!s.ok() && !s.is<KEY_ALREADY_EXISTS>()) { |
2883 | 0 | return s; |
2884 | 0 | } |
2885 | 2 | if (s.ok() && need_to_check_delete_bitmap) { |
2886 | | // check if the key is already mark deleted |
2887 | 1 | if (_tablet_meta->delete_bitmap().contains_agg_without_cache( |
2888 | 1 | {loc.rowset_id, loc.segment_id, version}, loc.row_id)) { |
2889 | | // if has sequence col, we continue to compare the sequence_id of |
2890 | | // all rowsets, util we find an existing key. |
2891 | 0 | if (with_seq_col) { |
2892 | 0 | continue; |
2893 | 0 | } |
2894 | | // The key is deleted, we need to break the loop and return |
2895 | | // KEY_NOT_FOUND. |
2896 | 0 | break; |
2897 | 0 | } |
2898 | 1 | } |
2899 | | // `st` is either OK or KEY_ALREADY_EXISTS now. |
2900 | | // for partial update, even if the key is already exists, we still need to |
2901 | | // read it's original values to keep all columns align. |
2902 | 2 | *row_location = loc; |
2903 | 2 | if (rowset) { |
2904 | | // return it's rowset |
2905 | 2 | *rowset = rs; |
2906 | 2 | } |
2907 | | // find it and return |
2908 | 2 | return s; |
2909 | 2 | } |
2910 | 2 | } |
2911 | 0 | g_tablet_pk_not_found << 1; |
2912 | 0 | return Status::Error<ErrorCode::KEY_NOT_FOUND>("can't find key in all rowsets"); |
2913 | 2 | } |
2914 | | |
2915 | | // load segment may do io so it should out lock |
2916 | | Status Tablet::_load_rowset_segments(const RowsetSharedPtr& rowset, |
2917 | 2 | std::vector<segment_v2::SegmentSharedPtr>* segments) { |
2918 | 2 | auto beta_rowset = reinterpret_cast<BetaRowset*>(rowset.get()); |
2919 | 2 | RETURN_IF_ERROR(beta_rowset->load_segments(segments)); |
2920 | 2 | return Status::OK(); |
2921 | 2 | } |
2922 | | |
2923 | 0 | Status Tablet::sort_block(vectorized::Block& in_block, vectorized::Block& output_block) { |
2924 | 0 | vectorized::MutableBlock mutable_input_block = |
2925 | 0 | vectorized::MutableBlock::build_mutable_block(&in_block); |
2926 | 0 | vectorized::MutableBlock mutable_output_block = |
2927 | 0 | vectorized::MutableBlock::build_mutable_block(&output_block); |
2928 | |
|
2929 | 0 | std::shared_ptr<RowInBlockComparator> vec_row_comparator = |
2930 | 0 | std::make_shared<RowInBlockComparator>(_tablet_meta->tablet_schema()); |
2931 | 0 | vec_row_comparator->set_block(&mutable_input_block); |
2932 | |
|
2933 | 0 | std::vector<std::unique_ptr<RowInBlock>> row_in_blocks; |
2934 | 0 | DCHECK(in_block.rows() <= std::numeric_limits<int>::max()); |
2935 | 0 | row_in_blocks.reserve(in_block.rows()); |
2936 | 0 | for (size_t i = 0; i < in_block.rows(); ++i) { |
2937 | 0 | row_in_blocks.emplace_back(std::make_unique<RowInBlock>(i)); |
2938 | 0 | } |
2939 | 0 | std::sort(row_in_blocks.begin(), row_in_blocks.end(), |
2940 | 0 | [&](const std::unique_ptr<RowInBlock>& l, |
2941 | 0 | const std::unique_ptr<RowInBlock>& r) -> bool { |
2942 | 0 | auto value = (*vec_row_comparator)(l.get(), r.get()); |
2943 | 0 | DCHECK(value != 0) << "value equel when sort block, l_pos: " << l->_row_pos |
2944 | 0 | << " r_pos: " << r->_row_pos; |
2945 | 0 | return value < 0; |
2946 | 0 | }); |
2947 | 0 | std::vector<uint32_t> row_pos_vec; |
2948 | 0 | row_pos_vec.reserve(in_block.rows()); |
2949 | 0 | for (int i = 0; i < row_in_blocks.size(); i++) { |
2950 | 0 | row_pos_vec.emplace_back(row_in_blocks[i]->_row_pos); |
2951 | 0 | } |
2952 | 0 | return mutable_output_block.add_rows(&in_block, row_pos_vec.data(), |
2953 | 0 | row_pos_vec.data() + in_block.rows()); |
2954 | 0 | } |
2955 | | |
2956 | | Status Tablet::calc_segment_delete_bitmap(RowsetSharedPtr rowset, |
2957 | | const segment_v2::SegmentSharedPtr& seg, |
2958 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
2959 | | DeleteBitmapPtr delete_bitmap, int64_t end_version, |
2960 | 1 | RowsetWriter* rowset_writer) { |
2961 | 1 | OlapStopWatch watch; |
2962 | 1 | auto rowset_id = rowset->rowset_id(); |
2963 | 1 | Version dummy_version(end_version + 1, end_version + 1); |
2964 | 1 | auto rowset_schema = rowset->tablet_schema(); |
2965 | 1 | bool is_partial_update = rowset_writer && rowset_writer->is_partial_update(); |
2966 | 1 | bool have_input_seq_column = false; |
2967 | 1 | if (is_partial_update && rowset_schema->has_sequence_col()) { |
2968 | 0 | std::vector<uint32_t> including_cids = |
2969 | 0 | rowset_writer->get_partial_update_info()->update_cids; |
2970 | 0 | have_input_seq_column = |
2971 | 0 | rowset_schema->has_sequence_col() && |
2972 | 0 | (std::find(including_cids.cbegin(), including_cids.cend(), |
2973 | 0 | rowset_schema->sequence_col_idx()) != including_cids.cend()); |
2974 | 0 | } |
2975 | 1 | if (rowset_schema->num_variant_columns() > 0) { |
2976 | | // During partial updates, the extracted columns of a variant should not be included in the rowset schema. |
2977 | | // This is because the partial update for a variant needs to ignore the extracted columns. |
2978 | | // Otherwise, the schema types in different rowsets might be inconsistent. When performing a partial update, |
2979 | | // the complete variant is constructed by reading all the sub-columns of the variant. |
2980 | 0 | rowset_schema = rowset_schema->copy_without_variant_extracted_columns(); |
2981 | 0 | } |
2982 | | // use for partial update |
2983 | 1 | PartialUpdateReadPlan read_plan_ori; |
2984 | 1 | PartialUpdateReadPlan read_plan_update; |
2985 | 1 | int64_t conflict_rows = 0; |
2986 | 1 | int64_t new_generated_rows = 0; |
2987 | | |
2988 | 1 | std::map<RowsetId, RowsetSharedPtr> rsid_to_rowset; |
2989 | 1 | rsid_to_rowset[rowset_id] = rowset; |
2990 | 1 | vectorized::Block block = rowset_schema->create_block(); |
2991 | 1 | vectorized::Block ordered_block = block.clone_empty(); |
2992 | 1 | uint32_t pos = 0; |
2993 | | |
2994 | 1 | RETURN_IF_ERROR(seg->load_pk_index_and_bf()); // We need index blocks to iterate |
2995 | 1 | auto pk_idx = seg->get_primary_key_index(); |
2996 | 1 | int total = pk_idx->num_rows(); |
2997 | 1 | uint32_t row_id = 0; |
2998 | 1 | int32_t remaining = total; |
2999 | 1 | bool exact_match = false; |
3000 | 1 | std::string last_key; |
3001 | 1 | int batch_size = 1024; |
3002 | | // The data for each segment may be lookup multiple times. Creating a SegmentCacheHandle |
3003 | | // will update the lru cache, and there will be obvious lock competition in multithreading |
3004 | | // scenarios, so using a segment_caches to cache SegmentCacheHandle. |
3005 | 1 | std::vector<std::unique_ptr<SegmentCacheHandle>> segment_caches(specified_rowsets.size()); |
3006 | 2 | while (remaining > 0) { |
3007 | 1 | std::unique_ptr<segment_v2::IndexedColumnIterator> iter; |
3008 | 1 | RETURN_IF_ERROR(pk_idx->new_iterator(&iter)); |
3009 | | |
3010 | 1 | size_t num_to_read = std::min(batch_size, remaining); |
3011 | 1 | auto index_type = vectorized::DataTypeFactory::instance().create_data_type( |
3012 | 1 | pk_idx->type_info()->type(), 1, 0); |
3013 | 1 | auto index_column = index_type->create_column(); |
3014 | 1 | Slice last_key_slice(last_key); |
3015 | 1 | RETURN_IF_ERROR(iter->seek_at_or_after(&last_key_slice, &exact_match)); |
3016 | 1 | auto current_ordinal = iter->get_current_ordinal(); |
3017 | 1 | DCHECK(total == remaining + current_ordinal) |
3018 | 0 | << "total: " << total << ", remaining: " << remaining |
3019 | 0 | << ", current_ordinal: " << current_ordinal; |
3020 | | |
3021 | 1 | size_t num_read = num_to_read; |
3022 | 1 | RETURN_IF_ERROR(iter->next_batch(&num_read, index_column)); |
3023 | 1 | DCHECK(num_to_read == num_read) |
3024 | 0 | << "num_to_read: " << num_to_read << ", num_read: " << num_read; |
3025 | 1 | last_key = index_column->get_data_at(num_read - 1).to_string(); |
3026 | | |
3027 | | // exclude last_key, last_key will be read in next batch. |
3028 | 1 | if (num_read == batch_size && num_read != remaining) { |
3029 | 0 | num_read -= 1; |
3030 | 0 | } |
3031 | 3 | for (size_t i = 0; i < num_read; i++, row_id++) { |
3032 | 2 | Slice key = Slice(index_column->get_data_at(i).data, index_column->get_data_at(i).size); |
3033 | 2 | RowLocation loc; |
3034 | | // calculate row id |
3035 | 2 | if (!_tablet_meta->tablet_schema()->cluster_key_idxes().empty()) { |
3036 | 0 | size_t seq_col_length = 0; |
3037 | 0 | if (_tablet_meta->tablet_schema()->has_sequence_col()) { |
3038 | 0 | seq_col_length = |
3039 | 0 | _tablet_meta->tablet_schema() |
3040 | 0 | ->column(_tablet_meta->tablet_schema()->sequence_col_idx()) |
3041 | 0 | .length() + |
3042 | 0 | 1; |
3043 | 0 | } |
3044 | 0 | size_t rowid_length = PrimaryKeyIndexReader::ROW_ID_LENGTH; |
3045 | 0 | Slice key_without_seq = |
3046 | 0 | Slice(key.get_data(), key.get_size() - seq_col_length - rowid_length); |
3047 | 0 | Slice rowid_slice = |
3048 | 0 | Slice(key.get_data() + key_without_seq.get_size() + seq_col_length + 1, |
3049 | 0 | rowid_length - 1); |
3050 | 0 | const auto* type_info = |
3051 | 0 | get_scalar_type_info<FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT>(); |
3052 | 0 | auto rowid_coder = get_key_coder(type_info->type()); |
3053 | 0 | RETURN_IF_ERROR(rowid_coder->decode_ascending(&rowid_slice, rowid_length, |
3054 | 0 | (uint8_t*)&row_id)); |
3055 | 0 | } |
3056 | | // same row in segments should be filtered |
3057 | 2 | if (delete_bitmap->contains({rowset_id, seg->id(), DeleteBitmap::TEMP_VERSION_COMMON}, |
3058 | 2 | row_id)) { |
3059 | 0 | continue; |
3060 | 0 | } |
3061 | | |
3062 | 2 | RowsetSharedPtr rowset_find; |
3063 | 2 | auto st = lookup_row_key(key, rowset_schema.get(), true, specified_rowsets, &loc, |
3064 | 2 | dummy_version.first - 1, segment_caches, &rowset_find, false, |
3065 | 2 | is_partial_update); |
3066 | 2 | bool expected_st = st.ok() || st.is<KEY_NOT_FOUND>() || st.is<KEY_ALREADY_EXISTS>(); |
3067 | | // It's a defensive DCHECK, we need to exclude some common errors to avoid core-dump |
3068 | | // while stress test |
3069 | 2 | DCHECK(expected_st || st.is<MEM_LIMIT_EXCEEDED>()) |
3070 | 0 | << "unexpected error status while lookup_row_key:" << st; |
3071 | 2 | if (!expected_st) { |
3072 | 0 | return st; |
3073 | 0 | } |
3074 | 2 | if (st.is<KEY_NOT_FOUND>()) { |
3075 | 0 | continue; |
3076 | 0 | } |
3077 | | |
3078 | 2 | if (st.is<KEY_ALREADY_EXISTS>() && (!is_partial_update || have_input_seq_column)) { |
3079 | | // `st.is<KEY_ALREADY_EXISTS>()` means that there exists a row with the same key and larger value |
3080 | | // in seqeunce column. |
3081 | | // - If the current load is not a partial update, we just delete current row. |
3082 | | // - Otherwise, it means that we are doing the alignment process in publish phase due to conflicts |
3083 | | // during concurrent partial updates. And there exists another load which introduces a row with |
3084 | | // the same keys and larger sequence column value published successfully after the commit phase |
3085 | | // of the current load. |
3086 | | // - If the columns we update include sequence column, we should delete the current row becase the |
3087 | | // partial update on the current row has been `overwritten` by the previous one with larger sequence |
3088 | | // column value. |
3089 | | // - Otherwise, we should combine the values of the missing columns in the previous row and the values |
3090 | | // of the including columns in the current row into a new row. |
3091 | 1 | delete_bitmap->add({rowset_id, seg->id(), DeleteBitmap::TEMP_VERSION_COMMON}, |
3092 | 1 | row_id); |
3093 | 1 | ++conflict_rows; |
3094 | 1 | continue; |
3095 | 1 | } |
3096 | 1 | if (is_partial_update && rowset_writer != nullptr) { |
3097 | | // In publish version, record rows to be deleted for concurrent update |
3098 | | // For example, if version 5 and 6 update a row, but version 6 only see |
3099 | | // version 4 when write, and when publish version, version 5's value will |
3100 | | // be marked as deleted and it's update is losed. |
3101 | | // So here we should read version 5's columns and build a new row, which is |
3102 | | // consists of version 6's update columns and version 5's origin columns |
3103 | | // here we build 2 read plan for ori values and update values |
3104 | 0 | prepare_to_read(loc, pos, &read_plan_ori); |
3105 | 0 | prepare_to_read(RowLocation {rowset_id, seg->id(), row_id}, pos, &read_plan_update); |
3106 | 0 | rsid_to_rowset[rowset_find->rowset_id()] = rowset_find; |
3107 | 0 | ++pos; |
3108 | | // delete bitmap will be calculate when memtable flush and |
3109 | | // publish. The two stages may see different versions. |
3110 | | // When there is sequence column, the currently imported data |
3111 | | // of rowset may be marked for deletion at memtablet flush or |
3112 | | // publish because the seq column is smaller than the previous |
3113 | | // rowset. |
3114 | | // just set 0 as a unified temporary version number, and update to |
3115 | | // the real version number later. |
3116 | 0 | delete_bitmap->add( |
3117 | 0 | {loc.rowset_id, loc.segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, |
3118 | 0 | loc.row_id); |
3119 | 0 | delete_bitmap->add({rowset_id, seg->id(), DeleteBitmap::TEMP_VERSION_COMMON}, |
3120 | 0 | row_id); |
3121 | 0 | ++conflict_rows; |
3122 | 0 | ++new_generated_rows; |
3123 | 0 | continue; |
3124 | 0 | } |
3125 | | // when st = ok |
3126 | 1 | delete_bitmap->add({loc.rowset_id, loc.segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, |
3127 | 1 | loc.row_id); |
3128 | 1 | ++conflict_rows; |
3129 | 1 | } |
3130 | 1 | remaining -= num_read; |
3131 | 1 | } |
3132 | | // DCHECK_EQ(total, row_id) << "segment total rows: " << total << " row_id:" << row_id; |
3133 | | |
3134 | 1 | if (config::enable_merge_on_write_correctness_check) { |
3135 | 1 | RowsetIdUnorderedSet rowsetids; |
3136 | 1 | for (const auto& rowset : specified_rowsets) { |
3137 | 1 | rowsetids.emplace(rowset->rowset_id()); |
3138 | 1 | VLOG_NOTICE << "[tabletID:" << tablet_id() << "]" |
3139 | 0 | << "[add_sentinel_mark_to_delete_bitmap][end_version:" << end_version << "]" |
3140 | 0 | << "add:" << rowset->rowset_id(); |
3141 | 1 | } |
3142 | 1 | add_sentinel_mark_to_delete_bitmap(delete_bitmap.get(), rowsetids); |
3143 | 1 | } |
3144 | | |
3145 | 1 | if (pos > 0) { |
3146 | 0 | auto partial_update_info = rowset_writer->get_partial_update_info(); |
3147 | 0 | DCHECK(partial_update_info); |
3148 | 0 | RETURN_IF_ERROR(generate_new_block_for_partial_update( |
3149 | 0 | rowset_schema, partial_update_info.get(), read_plan_ori, read_plan_update, |
3150 | 0 | rsid_to_rowset, &block)); |
3151 | 0 | RETURN_IF_ERROR(sort_block(block, ordered_block)); |
3152 | 0 | RETURN_IF_ERROR(rowset_writer->flush_single_block(&ordered_block)); |
3153 | 0 | if (new_generated_rows != rowset_writer->num_rows()) { |
3154 | 0 | LOG(WARNING) << "partial update correctness warning: conflict new generated rows (" |
3155 | 0 | << new_generated_rows << ") not equal to the new flushed rows (" |
3156 | 0 | << rowset_writer->num_rows() << "), tablet: " << tablet_id(); |
3157 | 0 | } |
3158 | 0 | LOG(INFO) << "calc segment delete bitmap for partial update, tablet: " << tablet_id() |
3159 | 0 | << " rowset: " << rowset_id << " seg_id: " << seg->id() |
3160 | 0 | << " dummy_version: " << end_version + 1 << " rows: " << seg->num_rows() |
3161 | 0 | << " conflict rows: " << conflict_rows |
3162 | 0 | << " new generated rows: " << new_generated_rows |
3163 | 0 | << " bimap num: " << delete_bitmap->delete_bitmap.size() |
3164 | 0 | << " cost: " << watch.get_elapse_time_us() << "(us)"; |
3165 | 0 | return Status::OK(); |
3166 | 0 | } |
3167 | 1 | LOG(INFO) << "calc segment delete bitmap, tablet: " << tablet_id() << " rowset: " << rowset_id |
3168 | 1 | << " seg_id: " << seg->id() << " dummy_version: " << end_version + 1 |
3169 | 1 | << " rows: " << seg->num_rows() << " conflict rows: " << conflict_rows |
3170 | 1 | << " bimap num: " << delete_bitmap->delete_bitmap.size() |
3171 | 1 | << " cost: " << watch.get_elapse_time_us() << "(us)"; |
3172 | 1 | return Status::OK(); |
3173 | 1 | } |
3174 | | |
3175 | | // if user pass a token, then all calculation works will submit to a threadpool, |
3176 | | // user can get all delete bitmaps from that token. |
3177 | | // if `token` is nullptr, the calculation will run in local, and user can get the result |
3178 | | // delete bitmap from `delete_bitmap` directly. |
3179 | | Status Tablet::calc_delete_bitmap(RowsetSharedPtr rowset, |
3180 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, |
3181 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
3182 | | DeleteBitmapPtr delete_bitmap, int64_t end_version, |
3183 | 66 | CalcDeleteBitmapToken* token, RowsetWriter* rowset_writer) { |
3184 | 66 | auto rowset_id = rowset->rowset_id(); |
3185 | 66 | if (specified_rowsets.empty() || segments.empty()) { |
3186 | 65 | LOG(INFO) << "skip to construct delete bitmap tablet: " << tablet_id() |
3187 | 65 | << " rowset: " << rowset_id; |
3188 | 65 | return Status::OK(); |
3189 | 65 | } |
3190 | | |
3191 | 1 | OlapStopWatch watch; |
3192 | 1 | doris::TabletSharedPtr tablet_ptr = _engine.tablet_manager()->get_tablet(tablet_id()); |
3193 | 1 | if (tablet_ptr == nullptr) { |
3194 | 0 | return Status::InternalError("Can't find tablet id: {}, maybe already dropped.", |
3195 | 0 | tablet_id()); |
3196 | 0 | } |
3197 | 1 | for (const auto& segment : segments) { |
3198 | 1 | auto& seg = segment; |
3199 | 1 | if (token != nullptr) { |
3200 | 1 | RETURN_IF_ERROR(token->submit(tablet_ptr, rowset, seg, specified_rowsets, end_version, |
3201 | 1 | delete_bitmap, rowset_writer)); |
3202 | 1 | } else { |
3203 | 0 | RETURN_IF_ERROR(calc_segment_delete_bitmap(rowset, segment, specified_rowsets, |
3204 | 0 | delete_bitmap, end_version, rowset_writer)); |
3205 | 0 | } |
3206 | 1 | } |
3207 | | |
3208 | 1 | return Status::OK(); |
3209 | 1 | } |
3210 | | |
3211 | | std::vector<RowsetSharedPtr> Tablet::get_rowset_by_ids( |
3212 | 68 | const RowsetIdUnorderedSet* specified_rowset_ids, bool include_stale) { |
3213 | 68 | std::vector<RowsetSharedPtr> rowsets; |
3214 | 68 | for (auto& rs : _rs_version_map) { |
3215 | 11 | if (!specified_rowset_ids || |
3216 | 11 | specified_rowset_ids->find(rs.second->rowset_id()) != specified_rowset_ids->end()) { |
3217 | 1 | rowsets.push_back(rs.second); |
3218 | 1 | } |
3219 | 11 | } |
3220 | | |
3221 | 68 | if (include_stale && specified_rowset_ids != nullptr && |
3222 | 68 | rowsets.size() != specified_rowset_ids->size()) { |
3223 | 0 | for (auto& rs : _stale_rs_version_map) { |
3224 | 0 | if (specified_rowset_ids->find(rs.second->rowset_id()) != specified_rowset_ids->end()) { |
3225 | 0 | rowsets.push_back(rs.second); |
3226 | 0 | } |
3227 | 0 | } |
3228 | 0 | } |
3229 | | |
3230 | 68 | std::sort(rowsets.begin(), rowsets.end(), [](RowsetSharedPtr& lhs, RowsetSharedPtr& rhs) { |
3231 | 0 | return lhs->end_version() > rhs->end_version(); |
3232 | 0 | }); |
3233 | 68 | return rowsets; |
3234 | 68 | } |
3235 | | |
3236 | | Status Tablet::generate_new_block_for_partial_update( |
3237 | | TabletSchemaSPtr rowset_schema, const PartialUpdateInfo* partial_update_info, |
3238 | | const PartialUpdateReadPlan& read_plan_ori, const PartialUpdateReadPlan& read_plan_update, |
3239 | | const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset, |
3240 | 0 | vectorized::Block* output_block) { |
3241 | | // do partial update related works |
3242 | | // 1. read columns by read plan |
3243 | | // 2. generate new block |
3244 | | // 3. write a new segment and modify rowset meta |
3245 | | // 4. mark current keys deleted |
3246 | 0 | CHECK(output_block); |
3247 | 0 | auto full_mutable_columns = output_block->mutate_columns(); |
3248 | 0 | const auto& missing_cids = partial_update_info->missing_cids; |
3249 | 0 | const auto& update_cids = partial_update_info->update_cids; |
3250 | 0 | auto old_block = rowset_schema->create_block_by_cids(missing_cids); |
3251 | 0 | auto update_block = rowset_schema->create_block_by_cids(update_cids); |
3252 | |
|
3253 | 0 | auto get_delete_sign_column_data = [](vectorized::Block& block, |
3254 | 0 | size_t rows) -> const signed char* { |
3255 | 0 | if (const vectorized::ColumnWithTypeAndName* delete_sign_column = |
3256 | 0 | block.try_get_by_name(DELETE_SIGN); |
3257 | 0 | delete_sign_column != nullptr) { |
3258 | 0 | const auto& delete_sign_col = |
3259 | 0 | reinterpret_cast<const vectorized::ColumnInt8&>(*(delete_sign_column->column)); |
3260 | 0 | if (delete_sign_col.size() >= rows) { |
3261 | 0 | return delete_sign_col.get_data().data(); |
3262 | 0 | } |
3263 | 0 | } |
3264 | 0 | return nullptr; |
3265 | 0 | }; |
3266 | | |
3267 | | // rowid in the final block(start from 0, increase continuously) -> rowid to read in update_block |
3268 | 0 | std::map<uint32_t, uint32_t> read_index_update; |
3269 | | |
3270 | | // read current rowset first, if a row in the current rowset has delete sign mark |
3271 | | // we don't need to read values from old block |
3272 | 0 | RETURN_IF_ERROR(read_columns_by_plan(rowset_schema, update_cids, read_plan_update, |
3273 | 0 | rsid_to_rowset, update_block, &read_index_update)); |
3274 | | |
3275 | 0 | size_t update_rows = read_index_update.size(); |
3276 | 0 | for (auto i = 0; i < update_cids.size(); ++i) { |
3277 | 0 | for (auto idx = 0; idx < update_rows; ++idx) { |
3278 | 0 | full_mutable_columns[update_cids[i]]->insert_from( |
3279 | 0 | *update_block.get_columns_with_type_and_name()[i].column.get(), |
3280 | 0 | read_index_update[idx]); |
3281 | 0 | } |
3282 | 0 | } |
3283 | | |
3284 | | // if there is sequence column in the table, we need to read the sequence column, |
3285 | | // otherwise it may cause the merge-on-read based compaction policy to produce incorrect results |
3286 | 0 | const auto* __restrict new_block_delete_signs = |
3287 | 0 | rowset_schema->has_sequence_col() |
3288 | 0 | ? nullptr |
3289 | 0 | : get_delete_sign_column_data(update_block, update_rows); |
3290 | | |
3291 | | // rowid in the final block(start from 0, increase, may not continuous becasue we skip to read some rows) -> rowid to read in old_block |
3292 | 0 | std::map<uint32_t, uint32_t> read_index_old; |
3293 | 0 | RETURN_IF_ERROR(read_columns_by_plan(rowset_schema, missing_cids, read_plan_ori, rsid_to_rowset, |
3294 | 0 | old_block, &read_index_old, new_block_delete_signs)); |
3295 | 0 | size_t old_rows = read_index_old.size(); |
3296 | 0 | const auto* __restrict old_block_delete_signs = |
3297 | 0 | get_delete_sign_column_data(old_block, old_rows); |
3298 | | |
3299 | | // build default value block |
3300 | 0 | auto default_value_block = old_block.clone_empty(); |
3301 | 0 | auto mutable_default_value_columns = default_value_block.mutate_columns(); |
3302 | 0 | if (old_block_delete_signs != nullptr || new_block_delete_signs != nullptr) { |
3303 | 0 | for (auto i = 0; i < missing_cids.size(); ++i) { |
3304 | 0 | const auto& column = rowset_schema->column(missing_cids[i]); |
3305 | 0 | if (column.has_default_value()) { |
3306 | 0 | const auto& default_value = partial_update_info->default_values[i]; |
3307 | 0 | vectorized::ReadBuffer rb(const_cast<char*>(default_value.c_str()), |
3308 | 0 | default_value.size()); |
3309 | 0 | RETURN_IF_ERROR(old_block.get_by_position(i).type->from_string( |
3310 | 0 | rb, mutable_default_value_columns[i].get())); |
3311 | 0 | } |
3312 | 0 | } |
3313 | 0 | } |
3314 | | |
3315 | 0 | CHECK(update_rows >= old_rows); |
3316 | | |
3317 | | // build full block |
3318 | 0 | for (auto i = 0; i < missing_cids.size(); ++i) { |
3319 | 0 | const auto& rs_column = rowset_schema->column(missing_cids[i]); |
3320 | 0 | auto& mutable_column = full_mutable_columns[missing_cids[i]]; |
3321 | 0 | for (auto idx = 0; idx < update_rows; ++idx) { |
3322 | | // There are two cases we don't need to read values from old data: |
3323 | | // 1. if the conflicting new row's delete sign is marked, which means the value columns |
3324 | | // of the row will not be read. So we don't need to read the missing values from the previous rows. |
3325 | | // 2. if the conflicting old row's delete sign is marked, which means that the key is not exist now, |
3326 | | // we should not read old values from the deleted data, and should use default value instead. |
3327 | | // NOTE: since now we are in the publishing phase, all data is commited |
3328 | | // before, even the `strict_mode` is true (which requires partial update |
3329 | | // load job can't insert new keys), this "new" key MUST be written into |
3330 | | // the new generated segment file. |
3331 | 0 | if (new_block_delete_signs != nullptr && new_block_delete_signs[idx]) { |
3332 | 0 | mutable_column->insert_default(); |
3333 | 0 | } else if (old_block_delete_signs != nullptr && |
3334 | 0 | old_block_delete_signs[read_index_old[idx]] != 0) { |
3335 | 0 | if (rs_column.has_default_value()) { |
3336 | 0 | mutable_column->insert_from(*mutable_default_value_columns[i].get(), 0); |
3337 | 0 | } else if (rs_column.is_nullable()) { |
3338 | 0 | assert_cast<vectorized::ColumnNullable*>(mutable_column.get()) |
3339 | 0 | ->insert_null_elements(1); |
3340 | 0 | } else { |
3341 | 0 | mutable_column->insert_default(); |
3342 | 0 | } |
3343 | 0 | } else { |
3344 | 0 | mutable_column->insert_from( |
3345 | 0 | *old_block.get_columns_with_type_and_name()[i].column.get(), |
3346 | 0 | read_index_old[idx]); |
3347 | 0 | } |
3348 | 0 | } |
3349 | 0 | } |
3350 | 0 | output_block->set_columns(std::move(full_mutable_columns)); |
3351 | 0 | VLOG_DEBUG << "full block when publish: " << output_block->dump_data(); |
3352 | 0 | return Status::OK(); |
3353 | 0 | } |
3354 | | |
3355 | | // read columns by read plan |
3356 | | // read_index: ori_pos-> block_idx |
3357 | | Status Tablet::read_columns_by_plan(TabletSchemaSPtr tablet_schema, |
3358 | | const std::vector<uint32_t> cids_to_read, |
3359 | | const PartialUpdateReadPlan& read_plan, |
3360 | | const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset, |
3361 | | vectorized::Block& block, |
3362 | | std::map<uint32_t, uint32_t>* read_index, |
3363 | 0 | const signed char* __restrict skip_map) { |
3364 | 0 | bool has_row_column = tablet_schema->store_row_column(); |
3365 | 0 | auto mutable_columns = block.mutate_columns(); |
3366 | 0 | size_t read_idx = 0; |
3367 | 0 | for (auto rs_it : read_plan) { |
3368 | 0 | for (auto seg_it : rs_it.second) { |
3369 | 0 | auto rowset_iter = rsid_to_rowset.find(rs_it.first); |
3370 | 0 | CHECK(rowset_iter != rsid_to_rowset.end()); |
3371 | 0 | std::vector<uint32_t> rids; |
3372 | 0 | for (auto [rid, pos] : seg_it.second) { |
3373 | 0 | if (skip_map && skip_map[pos]) { |
3374 | 0 | continue; |
3375 | 0 | } |
3376 | 0 | rids.emplace_back(rid); |
3377 | 0 | (*read_index)[pos] = read_idx++; |
3378 | 0 | } |
3379 | 0 | if (has_row_column) { |
3380 | 0 | auto st = fetch_value_through_row_column(rowset_iter->second, *tablet_schema, |
3381 | 0 | seg_it.first, rids, cids_to_read, block); |
3382 | 0 | if (!st.ok()) { |
3383 | 0 | LOG(WARNING) << "failed to fetch value through row column"; |
3384 | 0 | return st; |
3385 | 0 | } |
3386 | 0 | continue; |
3387 | 0 | } |
3388 | 0 | for (size_t cid = 0; cid < mutable_columns.size(); ++cid) { |
3389 | 0 | TabletColumn tablet_column = tablet_schema->column(cids_to_read[cid]); |
3390 | 0 | auto st = fetch_value_by_rowids(rowset_iter->second, seg_it.first, rids, |
3391 | 0 | tablet_column, mutable_columns[cid]); |
3392 | | // set read value to output block |
3393 | 0 | if (!st.ok()) { |
3394 | 0 | LOG(WARNING) << "failed to fetch value"; |
3395 | 0 | return st; |
3396 | 0 | } |
3397 | 0 | } |
3398 | 0 | } |
3399 | 0 | } |
3400 | 0 | block.set_columns(std::move(mutable_columns)); |
3401 | 0 | return Status::OK(); |
3402 | 0 | } |
3403 | | |
3404 | | void Tablet::prepare_to_read(const RowLocation& row_location, size_t pos, |
3405 | 0 | PartialUpdateReadPlan* read_plan) { |
3406 | 0 | auto rs_it = read_plan->find(row_location.rowset_id); |
3407 | 0 | if (rs_it == read_plan->end()) { |
3408 | 0 | std::map<uint32_t, std::vector<RidAndPos>> segid_to_rid; |
3409 | 0 | std::vector<RidAndPos> rid_pos; |
3410 | 0 | rid_pos.emplace_back(RidAndPos {row_location.row_id, pos}); |
3411 | 0 | segid_to_rid.emplace(row_location.segment_id, rid_pos); |
3412 | 0 | read_plan->emplace(row_location.rowset_id, segid_to_rid); |
3413 | 0 | return; |
3414 | 0 | } |
3415 | 0 | auto seg_it = rs_it->second.find(row_location.segment_id); |
3416 | 0 | if (seg_it == rs_it->second.end()) { |
3417 | 0 | std::vector<RidAndPos> rid_pos; |
3418 | 0 | rid_pos.emplace_back(RidAndPos {row_location.row_id, pos}); |
3419 | 0 | rs_it->second.emplace(row_location.segment_id, rid_pos); |
3420 | 0 | return; |
3421 | 0 | } |
3422 | 0 | seg_it->second.emplace_back(RidAndPos {row_location.row_id, pos}); |
3423 | 0 | } |
3424 | | |
3425 | | void Tablet::_rowset_ids_difference(const RowsetIdUnorderedSet& cur, |
3426 | | const RowsetIdUnorderedSet& pre, RowsetIdUnorderedSet* to_add, |
3427 | 4 | RowsetIdUnorderedSet* to_del) { |
3428 | 4 | for (const auto& id : cur) { |
3429 | 2 | if (pre.find(id) == pre.end()) { |
3430 | 1 | to_add->insert(id); |
3431 | 1 | } |
3432 | 2 | } |
3433 | 4 | for (const auto& id : pre) { |
3434 | 1 | if (cur.find(id) == cur.end()) { |
3435 | 0 | to_del->insert(id); |
3436 | 0 | } |
3437 | 1 | } |
3438 | 4 | } |
3439 | | |
3440 | | // The caller should hold _rowset_update_lock and _meta_lock lock. |
3441 | | Status Tablet::update_delete_bitmap_without_lock( |
3442 | 0 | const RowsetSharedPtr& rowset, const std::vector<RowsetSharedPtr>* specified_base_rowsets) { |
3443 | 0 | DBUG_EXECUTE_IF("Tablet.update_delete_bitmap_without_lock.random_failed", { |
3444 | 0 | auto rnd = rand() % 100; |
3445 | 0 | auto percent = dp->param("percent", 0.1); |
3446 | 0 | if (rnd < (100 * percent)) { |
3447 | 0 | LOG(WARNING) << "BaseTablet.update_delete_bitmap_without_lock.random_failed"; |
3448 | 0 | return Status::InternalError( |
3449 | 0 | "debug tablet update delete bitmap without lock random failed"); |
3450 | 0 | } else { |
3451 | 0 | LOG(INFO) << "BaseTablet.update_delete_bitmap_without_lock.random_failed not triggered" |
3452 | 0 | << ", rnd:" << rnd << ", percent: " << percent; |
3453 | 0 | } |
3454 | 0 | }); |
3455 | 0 | int64_t cur_version = rowset->end_version(); |
3456 | 0 | std::vector<segment_v2::SegmentSharedPtr> segments; |
3457 | 0 | RETURN_IF_ERROR(_load_rowset_segments(rowset, &segments)); |
3458 | | |
3459 | | // If this rowset does not have a segment, there is no need for an update. |
3460 | 0 | if (segments.empty()) { |
3461 | 0 | LOG(INFO) << "[Schema Change or Clone] skip to construct delete bitmap tablet: " |
3462 | 0 | << tablet_id() << " cur max_version: " << cur_version; |
3463 | 0 | return Status::OK(); |
3464 | 0 | } |
3465 | | |
3466 | | // calculate delete bitmap between segments if necessary. |
3467 | 0 | DeleteBitmapPtr delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id()); |
3468 | 0 | RETURN_IF_ERROR(calc_delete_bitmap_between_segments(rowset, segments, delete_bitmap)); |
3469 | | |
3470 | | // get all base rowsets to calculate on |
3471 | 0 | std::vector<RowsetSharedPtr> specified_rowsets; |
3472 | 0 | RowsetIdUnorderedSet cur_rowset_ids; |
3473 | 0 | if (specified_base_rowsets == nullptr) { |
3474 | 0 | RETURN_IF_ERROR(all_rs_id(cur_version - 1, &cur_rowset_ids)); |
3475 | 0 | specified_rowsets = get_rowset_by_ids(&cur_rowset_ids); |
3476 | 0 | } else { |
3477 | 0 | specified_rowsets = *specified_base_rowsets; |
3478 | 0 | } |
3479 | | |
3480 | 0 | OlapStopWatch watch; |
3481 | 0 | auto token = _engine.calc_delete_bitmap_executor()->create_token(); |
3482 | 0 | RETURN_IF_ERROR(calc_delete_bitmap(rowset, segments, specified_rowsets, delete_bitmap, |
3483 | 0 | cur_version - 1, token.get())); |
3484 | 0 | RETURN_IF_ERROR(token->wait()); |
3485 | 0 | size_t total_rows = std::accumulate( |
3486 | 0 | segments.begin(), segments.end(), 0, |
3487 | 0 | [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); }); |
3488 | 0 | LOG(INFO) << "[Schema Change or Clone] construct delete bitmap tablet: " << tablet_id() |
3489 | 0 | << ", rowset_ids: " << cur_rowset_ids.size() << ", cur max_version: " << cur_version |
3490 | 0 | << ", transaction_id: " << -1 << ", cost: " << watch.get_elapse_time_us() |
3491 | 0 | << "(us), total rows: " << total_rows; |
3492 | 0 | if (config::enable_merge_on_write_correctness_check) { |
3493 | | // check if all the rowset has ROWSET_SENTINEL_MARK |
3494 | 0 | auto st = check_delete_bitmap_correctness(delete_bitmap, cur_version - 1, -1, |
3495 | 0 | cur_rowset_ids, &specified_rowsets); |
3496 | 0 | if (!st.ok()) { |
3497 | 0 | LOG(WARNING) << fmt::format("delete bitmap correctness check failed in publish phase!"); |
3498 | 0 | } |
3499 | 0 | _remove_sentinel_mark_from_delete_bitmap(delete_bitmap); |
3500 | 0 | } |
3501 | 0 | for (auto iter = delete_bitmap->delete_bitmap.begin(); |
3502 | 0 | iter != delete_bitmap->delete_bitmap.end(); ++iter) { |
3503 | 0 | _tablet_meta->delete_bitmap().merge( |
3504 | 0 | {std::get<0>(iter->first), std::get<1>(iter->first), cur_version}, iter->second); |
3505 | 0 | } |
3506 | |
|
3507 | 0 | return Status::OK(); |
3508 | 0 | } |
3509 | | |
3510 | | Status Tablet::commit_phase_update_delete_bitmap( |
3511 | | const RowsetSharedPtr& rowset, RowsetIdUnorderedSet& pre_rowset_ids, |
3512 | | DeleteBitmapPtr delete_bitmap, const std::vector<segment_v2::SegmentSharedPtr>& segments, |
3513 | 2 | int64_t txn_id, CalcDeleteBitmapToken* token, RowsetWriter* rowset_writer) { |
3514 | 2 | SCOPED_BVAR_LATENCY(g_tablet_commit_phase_update_delete_bitmap_latency); |
3515 | 2 | RowsetIdUnorderedSet cur_rowset_ids; |
3516 | 2 | RowsetIdUnorderedSet rowset_ids_to_add; |
3517 | 2 | RowsetIdUnorderedSet rowset_ids_to_del; |
3518 | 2 | int64_t cur_version; |
3519 | | |
3520 | 2 | std::vector<RowsetSharedPtr> specified_rowsets; |
3521 | 2 | { |
3522 | 2 | std::shared_lock meta_rlock(_meta_lock); |
3523 | 2 | cur_version = max_version_unlocked().second; |
3524 | 2 | RETURN_IF_ERROR(all_rs_id(cur_version, &cur_rowset_ids)); |
3525 | 2 | _rowset_ids_difference(cur_rowset_ids, pre_rowset_ids, &rowset_ids_to_add, |
3526 | 2 | &rowset_ids_to_del); |
3527 | 2 | specified_rowsets = get_rowset_by_ids(&rowset_ids_to_add); |
3528 | 2 | } |
3529 | 0 | for (const auto& to_del : rowset_ids_to_del) { |
3530 | 0 | delete_bitmap->remove({to_del, 0, 0}, {to_del, UINT32_MAX, INT64_MAX}); |
3531 | 0 | } |
3532 | | |
3533 | 2 | RETURN_IF_ERROR(calc_delete_bitmap(rowset, segments, specified_rowsets, delete_bitmap, |
3534 | 2 | cur_version, token, rowset_writer)); |
3535 | 2 | size_t total_rows = std::accumulate( |
3536 | 2 | segments.begin(), segments.end(), 0, |
3537 | 2 | [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); }); |
3538 | 2 | LOG(INFO) << "[Before Commit] construct delete bitmap tablet: " << tablet_id() |
3539 | 2 | << ", rowset_ids to add: " << rowset_ids_to_add.size() |
3540 | 2 | << ", rowset_ids to del: " << rowset_ids_to_del.size() |
3541 | 2 | << ", cur max_version: " << cur_version << ", transaction_id: " << txn_id |
3542 | 2 | << ", total rows: " << total_rows; |
3543 | 2 | pre_rowset_ids = cur_rowset_ids; |
3544 | 2 | return Status::OK(); |
3545 | 2 | } |
3546 | | |
3547 | 2 | Status Tablet::update_delete_bitmap(TabletTxnInfo* txn_info, int64_t txn_id) { |
3548 | 2 | SCOPED_BVAR_LATENCY(g_tablet_update_delete_bitmap_latency); |
3549 | 2 | RowsetIdUnorderedSet cur_rowset_ids; |
3550 | 2 | RowsetIdUnorderedSet rowset_ids_to_add; |
3551 | 2 | RowsetIdUnorderedSet rowset_ids_to_del; |
3552 | 2 | RowsetSharedPtr rowset = txn_info->rowset; |
3553 | 2 | int64_t cur_version = rowset->start_version(); |
3554 | | |
3555 | 2 | std::unique_ptr<RowsetWriter> rowset_writer; |
3556 | 2 | RETURN_IF_ERROR( |
3557 | 2 | create_transient_rowset_writer(rowset, &rowset_writer, txn_info->partial_update_info)); |
3558 | | |
3559 | 2 | DeleteBitmapPtr delete_bitmap = txn_info->delete_bitmap; |
3560 | | // Partial update might generate new segments when there is conflicts while publish, and mark |
3561 | | // the same key in original segments as delete. |
3562 | | // When the new segment flush fails or the rowset build fails, the deletion marker for the |
3563 | | // duplicate key of the original segment should not remain in `txn_info->delete_bitmap`, |
3564 | | // so we need to make a copy of `txn_info->delete_bitmap` and make changes on it. |
3565 | 2 | bool is_partial_update = |
3566 | 2 | txn_info->partial_update_info && txn_info->partial_update_info->is_partial_update; |
3567 | 2 | if (is_partial_update) { |
3568 | 0 | delete_bitmap = std::make_shared<DeleteBitmap>(*(txn_info->delete_bitmap)); |
3569 | 0 | } |
3570 | | |
3571 | 2 | OlapStopWatch watch; |
3572 | 2 | std::vector<segment_v2::SegmentSharedPtr> segments; |
3573 | 2 | RETURN_IF_ERROR(_load_rowset_segments(rowset, &segments)); |
3574 | 2 | auto t1 = watch.get_elapse_time_us(); |
3575 | | |
3576 | 2 | { |
3577 | 2 | std::shared_lock meta_rlock(_meta_lock); |
3578 | | // tablet is under alter process. The delete bitmap will be calculated after conversion. |
3579 | 2 | if (tablet_state() == TABLET_NOTREADY) { |
3580 | 0 | LOG(INFO) << "tablet is under alter process, update delete bitmap later, tablet_id=" |
3581 | 0 | << tablet_id(); |
3582 | 0 | return Status::OK(); |
3583 | 0 | } |
3584 | 2 | RETURN_IF_ERROR(all_rs_id(cur_version - 1, &cur_rowset_ids)); |
3585 | 2 | } |
3586 | 2 | auto t2 = watch.get_elapse_time_us(); |
3587 | | |
3588 | 2 | _rowset_ids_difference(cur_rowset_ids, txn_info->rowset_ids, &rowset_ids_to_add, |
3589 | 2 | &rowset_ids_to_del); |
3590 | 2 | for (const auto& to_del : rowset_ids_to_del) { |
3591 | 0 | delete_bitmap->remove({to_del, 0, 0}, {to_del, UINT32_MAX, INT64_MAX}); |
3592 | 0 | } |
3593 | | |
3594 | 2 | std::vector<RowsetSharedPtr> specified_rowsets; |
3595 | 2 | { |
3596 | 2 | std::shared_lock meta_rlock(_meta_lock); |
3597 | 2 | specified_rowsets = get_rowset_by_ids(&rowset_ids_to_add); |
3598 | 2 | } |
3599 | 2 | auto t3 = watch.get_elapse_time_us(); |
3600 | | |
3601 | | // If a rowset is produced by compaction before the commit phase of the partial update load |
3602 | | // and is not included in txn_info->rowset_ids, we can skip the alignment process of that rowset |
3603 | | // because data remains the same before and after compaction. But we still need to calculate the |
3604 | | // the delete bitmap for that rowset. |
3605 | 2 | std::vector<RowsetSharedPtr> rowsets_skip_alignment; |
3606 | 2 | if (is_partial_update) { |
3607 | 0 | int64_t max_version_in_flush_phase = |
3608 | 0 | txn_info->partial_update_info->max_version_in_flush_phase; |
3609 | 0 | DCHECK(max_version_in_flush_phase != -1); |
3610 | 0 | std::vector<RowsetSharedPtr> remained_rowsets; |
3611 | 0 | for (const auto& rowset : specified_rowsets) { |
3612 | 0 | if (rowset->end_version() <= max_version_in_flush_phase && |
3613 | 0 | rowset->produced_by_compaction()) { |
3614 | 0 | rowsets_skip_alignment.emplace_back(rowset); |
3615 | 0 | } else { |
3616 | 0 | remained_rowsets.emplace_back(rowset); |
3617 | 0 | } |
3618 | 0 | } |
3619 | 0 | if (!rowsets_skip_alignment.empty()) { |
3620 | 0 | specified_rowsets = std::move(remained_rowsets); |
3621 | 0 | } |
3622 | 0 | } |
3623 | | |
3624 | 2 | if (!rowsets_skip_alignment.empty()) { |
3625 | 0 | auto token = _engine.calc_delete_bitmap_executor()->create_token(); |
3626 | | // set rowset_writer to nullptr to skip the alignment process |
3627 | 0 | RETURN_IF_ERROR(calc_delete_bitmap(rowset, segments, rowsets_skip_alignment, delete_bitmap, |
3628 | 0 | cur_version - 1, token.get(), nullptr)); |
3629 | 0 | RETURN_IF_ERROR(token->wait()); |
3630 | 0 | } |
3631 | | |
3632 | 2 | auto token = _engine.calc_delete_bitmap_executor()->create_token(); |
3633 | 2 | RETURN_IF_ERROR(calc_delete_bitmap(rowset, segments, specified_rowsets, delete_bitmap, |
3634 | 2 | cur_version - 1, token.get(), rowset_writer.get())); |
3635 | 2 | RETURN_IF_ERROR(token->wait()); |
3636 | | |
3637 | 2 | std::stringstream ss; |
3638 | 2 | if (watch.get_elapse_time_us() < 1 * 1000 * 1000) { |
3639 | 2 | ss << "cost: " << watch.get_elapse_time_us() - t3 << "(us)"; |
3640 | 2 | } else { |
3641 | 0 | ss << "cost(us): (load segments: " << t1 << ", get all rsid: " << t2 - t1 |
3642 | 0 | << ", get rowsets: " << t3 - t2 |
3643 | 0 | << ", calc delete bitmap: " << watch.get_elapse_time_us() - t3 << ")"; |
3644 | 0 | } |
3645 | | |
3646 | 2 | if (config::enable_merge_on_write_correctness_check && rowset->num_rows() != 0) { |
3647 | | // only do correctness check if the rowset has at least one row written |
3648 | | // check if all the rowset has ROWSET_SENTINEL_MARK |
3649 | 2 | auto st = check_delete_bitmap_correctness(delete_bitmap, cur_version - 1, -1, |
3650 | 2 | cur_rowset_ids, &specified_rowsets); |
3651 | 2 | if (!st.ok()) { |
3652 | 0 | LOG(WARNING) << fmt::format("delete bitmap correctness check failed in publish phase!"); |
3653 | 0 | } |
3654 | 2 | _remove_sentinel_mark_from_delete_bitmap(delete_bitmap); |
3655 | 2 | } |
3656 | | |
3657 | 2 | if (txn_info->partial_update_info && txn_info->partial_update_info->is_partial_update) { |
3658 | 0 | DBUG_EXECUTE_IF("Tablet.update_delete_bitmap.partial_update_write_rowset_fail", { |
3659 | 0 | if (rand() % 100 < (100 * dp->param("percent", 0.5))) { |
3660 | 0 | LOG_WARNING("Tablet.update_delete_bitmap.partial_update_write_rowset random failed") |
3661 | 0 | .tag("txn_id", txn_id); |
3662 | 0 | return Status::InternalError( |
3663 | 0 | "debug update_delete_bitmap partial update write rowset random failed"); |
3664 | 0 | } |
3665 | 0 | }); |
3666 | | // build rowset writer and merge transient rowset |
3667 | 0 | RETURN_IF_ERROR(rowset_writer->flush()); |
3668 | 0 | RowsetSharedPtr transient_rowset; |
3669 | 0 | RETURN_IF_ERROR(rowset_writer->build(transient_rowset)); |
3670 | 0 | auto old_segments = rowset->num_segments(); |
3671 | 0 | rowset->merge_rowset_meta(transient_rowset->rowset_meta()); |
3672 | 0 | auto new_segments = rowset->num_segments(); |
3673 | 0 | ss << ", partial update flush rowset (old segment num: " << old_segments |
3674 | 0 | << ", new segment num: " << new_segments << ")"; |
3675 | | |
3676 | | // update the shared_ptr to new delete bitmap |
3677 | 0 | txn_info->delete_bitmap = delete_bitmap; |
3678 | | |
3679 | | // erase segment cache cause we will add a segment to rowset |
3680 | 0 | SegmentLoader::instance()->erase_segments(rowset->rowset_id(), rowset->num_segments()); |
3681 | 0 | } |
3682 | | |
3683 | 2 | size_t total_rows = std::accumulate( |
3684 | 2 | segments.begin(), segments.end(), 0, |
3685 | 2 | [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); }); |
3686 | 2 | LOG(INFO) << "[Publish] construct delete bitmap tablet: " << tablet_id() |
3687 | 2 | << ", rowset_ids to add: " << rowset_ids_to_add.size() |
3688 | 2 | << ", rowset_ids to del: " << rowset_ids_to_del.size() |
3689 | 2 | << ", cur version: " << cur_version << ", transaction_id: " << txn_id << "," |
3690 | 2 | << ss.str() << " , total rows: " << total_rows; |
3691 | | |
3692 | | // update version without write lock, compaction and publish_txn |
3693 | | // will update delete bitmap, handle compaction with _rowset_update_lock |
3694 | | // and publish_txn runs sequential so no need to lock here |
3695 | 2 | for (auto& [key, bitmap] : delete_bitmap->delete_bitmap) { |
3696 | 2 | _tablet_meta->delete_bitmap().merge({std::get<0>(key), std::get<1>(key), cur_version}, |
3697 | 2 | bitmap); |
3698 | 2 | } |
3699 | | |
3700 | 2 | return Status::OK(); |
3701 | 2 | } |
3702 | | |
3703 | | void Tablet::calc_compaction_output_rowset_delete_bitmap( |
3704 | | const std::vector<RowsetSharedPtr>& input_rowsets, const RowIdConversion& rowid_conversion, |
3705 | | uint64_t start_version, uint64_t end_version, std::set<RowLocation>* missed_rows, |
3706 | | std::map<RowsetSharedPtr, std::list<std::pair<RowLocation, RowLocation>>>* location_map, |
3707 | 0 | const DeleteBitmap& input_delete_bitmap, DeleteBitmap* output_rowset_delete_bitmap) { |
3708 | 0 | RowLocation src; |
3709 | 0 | RowLocation dst; |
3710 | 0 | for (auto& rowset : input_rowsets) { |
3711 | 0 | src.rowset_id = rowset->rowset_id(); |
3712 | 0 | for (uint32_t seg_id = 0; seg_id < rowset->num_segments(); ++seg_id) { |
3713 | 0 | src.segment_id = seg_id; |
3714 | 0 | DeleteBitmap subset_map(tablet_id()); |
3715 | 0 | input_delete_bitmap.subset({rowset->rowset_id(), seg_id, start_version}, |
3716 | 0 | {rowset->rowset_id(), seg_id, end_version}, &subset_map); |
3717 | | // traverse all versions and convert rowid |
3718 | 0 | for (auto iter = subset_map.delete_bitmap.begin(); |
3719 | 0 | iter != subset_map.delete_bitmap.end(); ++iter) { |
3720 | 0 | auto cur_version = std::get<2>(iter->first); |
3721 | 0 | for (auto index = iter->second.begin(); index != iter->second.end(); ++index) { |
3722 | 0 | src.row_id = *index; |
3723 | 0 | if (rowid_conversion.get(src, &dst) != 0) { |
3724 | 0 | VLOG_CRITICAL << "Can't find rowid, may be deleted by the delete_handler, " |
3725 | 0 | << " src loaction: |" << src.rowset_id << "|" |
3726 | 0 | << src.segment_id << "|" << src.row_id |
3727 | 0 | << " version: " << cur_version; |
3728 | 0 | if (missed_rows) { |
3729 | 0 | missed_rows->insert(src); |
3730 | 0 | } |
3731 | 0 | continue; |
3732 | 0 | } |
3733 | 0 | VLOG_DEBUG << "calc_compaction_output_rowset_delete_bitmap dst location: |" |
3734 | 0 | << dst.rowset_id << "|" << dst.segment_id << "|" << dst.row_id |
3735 | 0 | << " src location: |" << src.rowset_id << "|" << src.segment_id |
3736 | 0 | << "|" << src.row_id << " start version: " << start_version |
3737 | 0 | << "end version" << end_version; |
3738 | 0 | if (location_map) { |
3739 | 0 | (*location_map)[rowset].emplace_back(src, dst); |
3740 | 0 | } |
3741 | 0 | output_rowset_delete_bitmap->add({dst.rowset_id, dst.segment_id, cur_version}, |
3742 | 0 | dst.row_id); |
3743 | 0 | } |
3744 | 0 | } |
3745 | 0 | } |
3746 | 0 | } |
3747 | 0 | } |
3748 | | |
3749 | 0 | void Tablet::merge_delete_bitmap(const DeleteBitmap& delete_bitmap) { |
3750 | 0 | _tablet_meta->delete_bitmap().merge(delete_bitmap); |
3751 | 0 | } |
3752 | | |
3753 | | Status Tablet::check_rowid_conversion( |
3754 | | RowsetSharedPtr dst_rowset, |
3755 | | const std::map<RowsetSharedPtr, std::list<std::pair<RowLocation, RowLocation>>>& |
3756 | 0 | location_map) { |
3757 | 0 | if (location_map.empty()) { |
3758 | 0 | VLOG_DEBUG << "check_rowid_conversion, location_map is empty"; |
3759 | 0 | return Status::OK(); |
3760 | 0 | } |
3761 | 0 | std::vector<segment_v2::SegmentSharedPtr> dst_segments; |
3762 | 0 | RETURN_IF_ERROR(_load_rowset_segments(dst_rowset, &dst_segments)); |
3763 | 0 | std::unordered_map<RowsetId, std::vector<segment_v2::SegmentSharedPtr>> input_rowsets_segment; |
3764 | |
|
3765 | 0 | VLOG_DEBUG << "check_rowid_conversion, dst_segments size: " << dst_segments.size(); |
3766 | 0 | for (auto [src_rowset, locations] : location_map) { |
3767 | 0 | std::vector<segment_v2::SegmentSharedPtr>& segments = |
3768 | 0 | input_rowsets_segment[src_rowset->rowset_id()]; |
3769 | 0 | if (segments.empty()) { |
3770 | 0 | RETURN_IF_ERROR(_load_rowset_segments(src_rowset, &segments)); |
3771 | 0 | } |
3772 | 0 | for (auto& [src, dst] : locations) { |
3773 | 0 | std::string src_key; |
3774 | 0 | std::string dst_key; |
3775 | 0 | Status s = segments[src.segment_id]->read_key_by_rowid(src.row_id, &src_key); |
3776 | 0 | if (UNLIKELY(s.is<NOT_IMPLEMENTED_ERROR>())) { |
3777 | 0 | LOG(INFO) << "primary key index of old version does not " |
3778 | 0 | "support reading key by rowid"; |
3779 | 0 | break; |
3780 | 0 | } |
3781 | 0 | if (UNLIKELY(!s)) { |
3782 | 0 | LOG(WARNING) << "failed to get src key: |" << src.rowset_id << "|" << src.segment_id |
3783 | 0 | << "|" << src.row_id << " status: " << s; |
3784 | 0 | DCHECK(false); |
3785 | 0 | return s; |
3786 | 0 | } |
3787 | | |
3788 | 0 | s = dst_segments[dst.segment_id]->read_key_by_rowid(dst.row_id, &dst_key); |
3789 | 0 | if (UNLIKELY(!s)) { |
3790 | 0 | LOG(WARNING) << "failed to get dst key: |" << dst.rowset_id << "|" << dst.segment_id |
3791 | 0 | << "|" << dst.row_id << " status: " << s; |
3792 | 0 | DCHECK(false); |
3793 | 0 | return s; |
3794 | 0 | } |
3795 | | |
3796 | 0 | VLOG_DEBUG << "check_rowid_conversion, src: |" << src.rowset_id << "|" << src.segment_id |
3797 | 0 | << "|" << src.row_id << "|" << src_key << " dst: |" << dst.rowset_id << "|" |
3798 | 0 | << dst.segment_id << "|" << dst.row_id << "|" << dst_key; |
3799 | 0 | if (UNLIKELY(src_key.compare(dst_key) != 0)) { |
3800 | 0 | LOG(WARNING) << "failed to check key, src key: |" << src.rowset_id << "|" |
3801 | 0 | << src.segment_id << "|" << src.row_id << "|" << src_key |
3802 | 0 | << " dst key: |" << dst.rowset_id << "|" << dst.segment_id << "|" |
3803 | 0 | << dst.row_id << "|" << dst_key; |
3804 | 0 | DCHECK(false); |
3805 | 0 | return Status::InternalError("failed to check rowid conversion"); |
3806 | 0 | } |
3807 | 0 | } |
3808 | 0 | } |
3809 | 0 | return Status::OK(); |
3810 | 0 | } |
3811 | | |
3812 | 6 | Status Tablet::all_rs_id(int64_t max_version, RowsetIdUnorderedSet* rowset_ids) const { |
3813 | | // Ensure that the obtained versions of rowsets are continuous |
3814 | 6 | std::vector<Version> version_path; |
3815 | 6 | RETURN_IF_ERROR( |
3816 | 6 | capture_consistent_versions(Version(0, max_version), &version_path, false, false)); |
3817 | 8 | for (auto& ver : version_path) { |
3818 | 8 | if (ver.second == 1) { |
3819 | | // [0-1] rowset is empty for each tablet, skip it |
3820 | 6 | continue; |
3821 | 6 | } |
3822 | 2 | auto it = _rs_version_map.find(ver); |
3823 | 2 | if (it == _rs_version_map.end()) { |
3824 | 0 | return Status::Error<CAPTURE_ROWSET_ERROR, false>( |
3825 | 0 | "fail to find Rowset for version. tablet={}, version={}", tablet_id(), |
3826 | 0 | ver.to_string()); |
3827 | 0 | } |
3828 | 2 | rowset_ids->emplace(it->second->rowset_id()); |
3829 | 2 | } |
3830 | 6 | return Status::OK(); |
3831 | 6 | } |
3832 | | |
3833 | 0 | bool Tablet::check_all_rowset_segment() { |
3834 | 0 | std::shared_lock rdlock(_meta_lock); |
3835 | 0 | for (auto& version_rowset : _rs_version_map) { |
3836 | 0 | RowsetSharedPtr rowset = version_rowset.second; |
3837 | 0 | if (!rowset->check_rowset_segment()) { |
3838 | 0 | LOG(WARNING) << "Tablet Segment Check. find a bad tablet, tablet_id=" << tablet_id(); |
3839 | 0 | return false; |
3840 | 0 | } |
3841 | 0 | } |
3842 | 0 | return true; |
3843 | 0 | } |
3844 | | |
3845 | 1 | void Tablet::set_skip_compaction(bool skip, CompactionType compaction_type, int64_t start) { |
3846 | 1 | if (!skip) { |
3847 | 0 | _skip_cumu_compaction = false; |
3848 | 0 | _skip_base_compaction = false; |
3849 | 0 | return; |
3850 | 0 | } |
3851 | 1 | if (compaction_type == CompactionType::CUMULATIVE_COMPACTION) { |
3852 | 1 | _skip_cumu_compaction = true; |
3853 | 1 | _skip_cumu_compaction_ts = start; |
3854 | 1 | } else { |
3855 | 0 | DCHECK(compaction_type == CompactionType::BASE_COMPACTION); |
3856 | 0 | _skip_base_compaction = true; |
3857 | 0 | _skip_base_compaction_ts = start; |
3858 | 0 | } |
3859 | 1 | } |
3860 | | |
3861 | 4 | bool Tablet::should_skip_compaction(CompactionType compaction_type, int64_t now) { |
3862 | 4 | if (compaction_type == CompactionType::CUMULATIVE_COMPACTION && _skip_cumu_compaction && |
3863 | 4 | now < _skip_cumu_compaction_ts + 120) { |
3864 | 2 | return true; |
3865 | 2 | } else if (compaction_type == CompactionType::BASE_COMPACTION && _skip_base_compaction && |
3866 | 2 | now < _skip_base_compaction_ts + 120) { |
3867 | 0 | return true; |
3868 | 0 | } |
3869 | 2 | return false; |
3870 | 4 | } |
3871 | | |
3872 | 0 | std::pair<std::string, int64_t> Tablet::get_binlog_info(std::string_view binlog_version) const { |
3873 | 0 | return RowsetMetaManager::get_binlog_info(_data_dir->get_meta(), tablet_uid(), binlog_version); |
3874 | 0 | } |
3875 | | |
3876 | | std::string Tablet::get_rowset_binlog_meta(std::string_view binlog_version, |
3877 | 0 | std::string_view rowset_id) const { |
3878 | 0 | return RowsetMetaManager::get_rowset_binlog_meta(_data_dir->get_meta(), tablet_uid(), |
3879 | 0 | binlog_version, rowset_id); |
3880 | 0 | } |
3881 | | |
3882 | | Status Tablet::get_rowset_binlog_metas(const std::vector<int64_t>& binlog_versions, |
3883 | 0 | RowsetBinlogMetasPB* metas_pb) { |
3884 | 0 | return RowsetMetaManager::get_rowset_binlog_metas(_data_dir->get_meta(), tablet_uid(), |
3885 | 0 | binlog_versions, metas_pb); |
3886 | 0 | } |
3887 | | |
3888 | 4 | Status Tablet::get_rowset_binlog_metas(Version binlog_versions, RowsetBinlogMetasPB* metas_pb) { |
3889 | 4 | return RowsetMetaManager::get_rowset_binlog_metas(_data_dir->get_meta(), tablet_uid(), |
3890 | 4 | binlog_versions, metas_pb); |
3891 | 4 | } |
3892 | | |
3893 | | std::string Tablet::get_segment_filepath(std::string_view rowset_id, |
3894 | 0 | std::string_view segment_index) const { |
3895 | 0 | return fmt::format("{}/_binlog/{}_{}.dat", _tablet_path, rowset_id, segment_index); |
3896 | 0 | } |
3897 | | |
3898 | 0 | std::string Tablet::get_segment_filepath(std::string_view rowset_id, int64_t segment_index) const { |
3899 | 0 | return fmt::format("{}/_binlog/{}_{}.dat", _tablet_path, rowset_id, segment_index); |
3900 | 0 | } |
3901 | | |
3902 | | std::string Tablet::get_segment_index_filepath(std::string_view rowset_id, |
3903 | | std::string_view segment_index, |
3904 | 0 | std::string_view index_id) const { |
3905 | 0 | auto format = _tablet_meta->tablet_schema()->get_inverted_index_storage_format(); |
3906 | 0 | if (format == doris::InvertedIndexStorageFormatPB::V1) { |
3907 | 0 | return fmt::format("{}/_binlog/{}_{}_{}.idx", _tablet_path, rowset_id, segment_index, |
3908 | 0 | index_id); |
3909 | 0 | } else { |
3910 | 0 | return fmt::format("{}/_binlog/{}_{}.idx", _tablet_path, rowset_id, segment_index); |
3911 | 0 | } |
3912 | 0 | } |
3913 | | |
3914 | | std::string Tablet::get_segment_index_filepath(std::string_view rowset_id, int64_t segment_index, |
3915 | 0 | int64_t index_id) const { |
3916 | 0 | auto format = _tablet_meta->tablet_schema()->get_inverted_index_storage_format(); |
3917 | 0 | if (format == doris::InvertedIndexStorageFormatPB::V1) { |
3918 | 0 | return fmt::format("{}/_binlog/{}_{}_{}.idx", _tablet_path, rowset_id, segment_index, |
3919 | 0 | index_id); |
3920 | 0 | } else { |
3921 | 0 | DCHECK(index_id == -1); |
3922 | 0 | return fmt::format("{}/_binlog/{}_{}.idx", _tablet_path, rowset_id, segment_index); |
3923 | 0 | } |
3924 | 0 | } |
3925 | | |
3926 | 0 | std::vector<std::string> Tablet::get_binlog_filepath(std::string_view binlog_version) const { |
3927 | 0 | const auto& [rowset_id, num_segments] = get_binlog_info(binlog_version); |
3928 | 0 | std::vector<std::string> binlog_filepath; |
3929 | 0 | for (int i = 0; i < num_segments; ++i) { |
3930 | | // TODO(Drogon): rewrite by filesystem path |
3931 | 0 | auto segment_file = fmt::format("{}_{}.dat", rowset_id, i); |
3932 | 0 | binlog_filepath.emplace_back(fmt::format("{}/_binlog/{}", _tablet_path, segment_file)); |
3933 | 0 | } |
3934 | 0 | return binlog_filepath; |
3935 | 0 | } |
3936 | | |
3937 | 0 | bool Tablet::can_add_binlog(uint64_t total_binlog_size) const { |
3938 | 0 | return !_data_dir->reach_capacity_limit(total_binlog_size); |
3939 | 0 | } |
3940 | | |
3941 | 10 | bool Tablet::is_enable_binlog() { |
3942 | 10 | return config::enable_feature_binlog && tablet_meta()->binlog_config().is_enable(); |
3943 | 10 | } |
3944 | | |
3945 | 0 | void Tablet::set_binlog_config(BinlogConfig binlog_config) { |
3946 | 0 | tablet_meta()->set_binlog_config(binlog_config); |
3947 | 0 | } |
3948 | | |
3949 | 0 | void Tablet::gc_binlogs(int64_t version) { |
3950 | 0 | auto meta = _data_dir->get_meta(); |
3951 | 0 | DCHECK(meta != nullptr); |
3952 | |
|
3953 | 0 | const auto& tablet_uid = this->tablet_uid(); |
3954 | 0 | const auto tablet_id = this->tablet_id(); |
3955 | 0 | std::string begin_key = make_binlog_meta_key_prefix(tablet_uid); |
3956 | 0 | std::string end_key = make_binlog_meta_key_prefix(tablet_uid, version + 1); |
3957 | 0 | LOG(INFO) << fmt::format("gc binlog meta, tablet_id:{}, begin_key:{}, end_key:{}", tablet_id, |
3958 | 0 | begin_key, end_key); |
3959 | |
|
3960 | 0 | std::vector<std::string> wait_for_deleted_binlog_keys; |
3961 | 0 | std::vector<std::string> wait_for_deleted_binlog_files; |
3962 | 0 | auto add_to_wait_for_deleted = [&](std::string_view key, std::string_view rowset_id, |
3963 | 0 | int64_t num_segments) { |
3964 | | // add binlog meta key and binlog data key |
3965 | 0 | wait_for_deleted_binlog_keys.emplace_back(key); |
3966 | 0 | wait_for_deleted_binlog_keys.push_back(get_binlog_data_key_from_meta_key(key)); |
3967 | | |
3968 | | // add binlog segment files and index files |
3969 | 0 | for (int64_t i = 0; i < num_segments; ++i) { |
3970 | 0 | wait_for_deleted_binlog_files.emplace_back(get_segment_filepath(rowset_id, i)); |
3971 | 0 | for (const auto& index : this->tablet_schema()->indexes()) { |
3972 | 0 | if (index.index_type() != IndexType::INVERTED) { |
3973 | 0 | continue; |
3974 | 0 | } |
3975 | 0 | wait_for_deleted_binlog_files.emplace_back( |
3976 | 0 | get_segment_index_filepath(rowset_id, i, index.index_id())); |
3977 | 0 | } |
3978 | 0 | } |
3979 | 0 | }; |
3980 | |
|
3981 | 0 | auto check_binlog_ttl = [&](const std::string& key, const std::string& value) mutable -> bool { |
3982 | 0 | if (key >= end_key) { |
3983 | 0 | return false; |
3984 | 0 | } |
3985 | | |
3986 | 0 | BinlogMetaEntryPB binlog_meta_entry_pb; |
3987 | 0 | if (!binlog_meta_entry_pb.ParseFromString(value)) { |
3988 | 0 | LOG(WARNING) << "failed to parse binlog meta entry, key:" << key; |
3989 | 0 | return true; |
3990 | 0 | } |
3991 | | |
3992 | 0 | auto num_segments = binlog_meta_entry_pb.num_segments(); |
3993 | 0 | std::string rowset_id; |
3994 | 0 | if (binlog_meta_entry_pb.has_rowset_id_v2()) { |
3995 | 0 | rowset_id = binlog_meta_entry_pb.rowset_id_v2(); |
3996 | 0 | } else { |
3997 | | // key is 'binlog_meta_6943f1585fe834b5-e542c2b83a21d0b7_00000000000000000069_020000000000000135449d7cd7eadfe672aa0f928fa99593', extract last part '020000000000000135449d7cd7eadfe672aa0f928fa99593' |
3998 | 0 | auto pos = key.rfind('_'); |
3999 | 0 | if (pos == std::string::npos) { |
4000 | 0 | LOG(WARNING) << fmt::format("invalid binlog meta key:{}", key); |
4001 | 0 | return false; |
4002 | 0 | } |
4003 | 0 | rowset_id = key.substr(pos + 1); |
4004 | 0 | } |
4005 | 0 | add_to_wait_for_deleted(key, rowset_id, num_segments); |
4006 | |
|
4007 | 0 | return true; |
4008 | 0 | }; |
4009 | |
|
4010 | 0 | auto status = meta->iterate(META_COLUMN_FAMILY_INDEX, begin_key, check_binlog_ttl); |
4011 | 0 | if (!status.ok()) { |
4012 | 0 | LOG(WARNING) << "failed to iterate binlog meta, status:" << status; |
4013 | 0 | return; |
4014 | 0 | } |
4015 | | |
4016 | | // first remove binlog files, if failed, just break, then retry next time |
4017 | | // this keep binlog meta in meta store, so that binlog can be removed next time |
4018 | 0 | bool remove_binlog_files_failed = false; |
4019 | 0 | for (auto& file : wait_for_deleted_binlog_files) { |
4020 | 0 | if (unlink(file.c_str()) != 0) { |
4021 | | // file not exist, continue |
4022 | 0 | if (errno == ENOENT) { |
4023 | 0 | continue; |
4024 | 0 | } |
4025 | | |
4026 | 0 | remove_binlog_files_failed = true; |
4027 | 0 | LOG(WARNING) << "failed to remove binlog file:" << file << ", errno:" << errno; |
4028 | 0 | break; |
4029 | 0 | } |
4030 | 0 | } |
4031 | 0 | if (!remove_binlog_files_failed) { |
4032 | 0 | static_cast<void>(meta->remove(META_COLUMN_FAMILY_INDEX, wait_for_deleted_binlog_keys)); |
4033 | 0 | } |
4034 | 0 | } |
4035 | | |
4036 | 0 | Status Tablet::ingest_binlog_metas(RowsetBinlogMetasPB* metas_pb) { |
4037 | 0 | return RowsetMetaManager::ingest_binlog_metas(_data_dir->get_meta(), tablet_uid(), metas_pb); |
4038 | 0 | } |
4039 | | |
4040 | 28 | void Tablet::clear_cache() { |
4041 | 28 | std::shared_lock rlock(get_header_lock()); |
4042 | 28 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
4043 | 56 | static auto recycle_segment_cache = [](const auto& rowset_map) { |
4044 | 56 | for (auto& [_, rowset] : rowset_map) { |
4045 | 48 | rowset->clear_cache(); |
4046 | 48 | } |
4047 | 56 | }; |
4048 | 28 | recycle_segment_cache(rowset_map()); |
4049 | 28 | recycle_segment_cache(stale_rowset_map()); |
4050 | 28 | } |
4051 | | |
4052 | | Status Tablet::calc_delete_bitmap_between_segments( |
4053 | | RowsetSharedPtr rowset, const std::vector<segment_v2::SegmentSharedPtr>& segments, |
4054 | 0 | DeleteBitmapPtr delete_bitmap) { |
4055 | 0 | size_t const num_segments = segments.size(); |
4056 | 0 | if (num_segments < 2) { |
4057 | 0 | return Status::OK(); |
4058 | 0 | } |
4059 | | |
4060 | 0 | OlapStopWatch watch; |
4061 | 0 | auto const rowset_id = rowset->rowset_id(); |
4062 | 0 | size_t seq_col_length = 0; |
4063 | 0 | if (_tablet_meta->tablet_schema()->has_sequence_col()) { |
4064 | 0 | auto seq_col_idx = _tablet_meta->tablet_schema()->sequence_col_idx(); |
4065 | 0 | seq_col_length = _tablet_meta->tablet_schema()->column(seq_col_idx).length() + 1; |
4066 | 0 | } |
4067 | 0 | size_t rowid_length = 0; |
4068 | 0 | if (!_tablet_meta->tablet_schema()->cluster_key_idxes().empty()) { |
4069 | 0 | rowid_length = PrimaryKeyIndexReader::ROW_ID_LENGTH; |
4070 | 0 | } |
4071 | |
|
4072 | 0 | MergeIndexDeleteBitmapCalculator calculator; |
4073 | 0 | RETURN_IF_ERROR(calculator.init(rowset_id, segments, seq_col_length, rowid_length)); |
4074 | | |
4075 | 0 | RETURN_IF_ERROR(calculator.calculate_all(delete_bitmap)); |
4076 | | |
4077 | 0 | LOG(INFO) << fmt::format( |
4078 | 0 | "construct delete bitmap between segments, " |
4079 | 0 | "tablet: {}, rowset: {}, number of segments: {}, bitmap size: {}, cost {} (us)", |
4080 | 0 | tablet_id(), rowset_id.to_string(), num_segments, delete_bitmap->delete_bitmap.size(), |
4081 | 0 | watch.get_elapse_time_us()); |
4082 | 0 | return Status::OK(); |
4083 | 0 | } |
4084 | | |
4085 | | void Tablet::add_sentinel_mark_to_delete_bitmap(DeleteBitmap* delete_bitmap, |
4086 | 5 | const RowsetIdUnorderedSet& rowsetids) { |
4087 | 5 | for (const auto& rowsetid : rowsetids) { |
4088 | 5 | delete_bitmap->add( |
4089 | 5 | {rowsetid, DeleteBitmap::INVALID_SEGMENT_ID, DeleteBitmap::TEMP_VERSION_COMMON}, |
4090 | 5 | DeleteBitmap::ROWSET_SENTINEL_MARK); |
4091 | 5 | } |
4092 | 5 | } |
4093 | | |
4094 | 2 | void Tablet::_remove_sentinel_mark_from_delete_bitmap(DeleteBitmapPtr delete_bitmap) { |
4095 | 2 | for (auto it = delete_bitmap->delete_bitmap.begin(), end = delete_bitmap->delete_bitmap.end(); |
4096 | 5 | it != end;) { |
4097 | 3 | if (std::get<1>(it->first) == DeleteBitmap::INVALID_SEGMENT_ID) { |
4098 | 1 | it = delete_bitmap->delete_bitmap.erase(it); |
4099 | 2 | } else { |
4100 | 2 | ++it; |
4101 | 2 | } |
4102 | 3 | } |
4103 | 2 | } |
4104 | | |
4105 | | Status Tablet::check_delete_bitmap_correctness(DeleteBitmapPtr delete_bitmap, int64_t max_version, |
4106 | | int64_t txn_id, |
4107 | | const RowsetIdUnorderedSet& rowset_ids, |
4108 | 4 | std::vector<RowsetSharedPtr>* rowsets) { |
4109 | 4 | RowsetIdUnorderedSet missing_ids; |
4110 | 4 | for (const auto& rowsetid : rowset_ids) { |
4111 | 2 | if (!delete_bitmap->delete_bitmap.contains({rowsetid, DeleteBitmap::INVALID_SEGMENT_ID, |
4112 | 2 | DeleteBitmap::TEMP_VERSION_COMMON})) { |
4113 | 0 | missing_ids.insert(rowsetid); |
4114 | 0 | } |
4115 | 2 | } |
4116 | | |
4117 | 4 | if (!missing_ids.empty()) { |
4118 | 0 | LOG(WARNING) << "[txn_id:" << txn_id << "][tablet_id:" << tablet_id() |
4119 | 0 | << "][max_version: " << max_version |
4120 | 0 | << "] check delete bitmap correctness failed!"; |
4121 | 0 | rapidjson::Document root; |
4122 | 0 | root.SetObject(); |
4123 | 0 | rapidjson::Document required_rowsets_arr; |
4124 | 0 | required_rowsets_arr.SetArray(); |
4125 | 0 | rapidjson::Document missing_rowsets_arr; |
4126 | 0 | missing_rowsets_arr.SetArray(); |
4127 | |
|
4128 | 0 | if (rowsets != nullptr) { |
4129 | 0 | for (const auto& rowset : *rowsets) { |
4130 | 0 | rapidjson::Value value; |
4131 | 0 | std::string version_str = |
4132 | 0 | _get_rowset_info_str(rowset, rowset->rowset_meta()->has_delete_predicate()); |
4133 | 0 | value.SetString(version_str.c_str(), version_str.length(), |
4134 | 0 | required_rowsets_arr.GetAllocator()); |
4135 | 0 | required_rowsets_arr.PushBack(value, required_rowsets_arr.GetAllocator()); |
4136 | 0 | } |
4137 | 0 | } else { |
4138 | 0 | std::vector<RowsetSharedPtr> rowsets; |
4139 | 0 | { |
4140 | 0 | std::shared_lock meta_rlock(_meta_lock); |
4141 | 0 | rowsets = get_rowset_by_ids(&rowset_ids); |
4142 | 0 | } |
4143 | 0 | for (const auto& rowset : rowsets) { |
4144 | 0 | rapidjson::Value value; |
4145 | 0 | std::string version_str = |
4146 | 0 | _get_rowset_info_str(rowset, rowset->rowset_meta()->has_delete_predicate()); |
4147 | 0 | value.SetString(version_str.c_str(), version_str.length(), |
4148 | 0 | required_rowsets_arr.GetAllocator()); |
4149 | 0 | required_rowsets_arr.PushBack(value, required_rowsets_arr.GetAllocator()); |
4150 | 0 | } |
4151 | 0 | } |
4152 | 0 | for (const auto& missing_rowset_id : missing_ids) { |
4153 | 0 | rapidjson::Value miss_value; |
4154 | 0 | std::string rowset_id_str = missing_rowset_id.to_string(); |
4155 | 0 | miss_value.SetString(rowset_id_str.c_str(), rowset_id_str.length(), |
4156 | 0 | missing_rowsets_arr.GetAllocator()); |
4157 | 0 | missing_rowsets_arr.PushBack(miss_value, missing_rowsets_arr.GetAllocator()); |
4158 | 0 | } |
4159 | |
|
4160 | 0 | root.AddMember("required_rowsets", required_rowsets_arr, root.GetAllocator()); |
4161 | 0 | root.AddMember("missing_rowsets", missing_rowsets_arr, root.GetAllocator()); |
4162 | 0 | rapidjson::StringBuffer strbuf; |
4163 | 0 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf); |
4164 | 0 | root.Accept(writer); |
4165 | 0 | std::string rowset_status_string = std::string(strbuf.GetString()); |
4166 | 0 | LOG_EVERY_SECOND(WARNING) << rowset_status_string; |
4167 | | // let it crash if correctness check failed in Debug mode |
4168 | 0 | DCHECK(false) << "delete bitmap correctness check failed in publish phase!"; |
4169 | 0 | return Status::InternalError("check delete bitmap failed!"); |
4170 | 0 | } |
4171 | 4 | return Status::OK(); |
4172 | 4 | } |
4173 | | Status Tablet::calc_local_file_crc(uint32_t* crc_value, int64_t start_version, int64_t end_version, |
4174 | 0 | int32_t* rowset_count, int64_t* file_count) { |
4175 | 0 | Version v(start_version, end_version); |
4176 | 0 | std::vector<RowsetSharedPtr> rowsets; |
4177 | 0 | traverse_rowsets([&rowsets, &v](const auto& rs) { |
4178 | | // get local rowsets |
4179 | 0 | if (rs->is_local() && v.contains(rs->version())) { |
4180 | 0 | rowsets.emplace_back(rs); |
4181 | 0 | } |
4182 | 0 | }); |
4183 | 0 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
4184 | 0 | *rowset_count = rowsets.size(); |
4185 | |
|
4186 | 0 | *crc_value = 0; |
4187 | 0 | *file_count = 0; |
4188 | 0 | for (const auto& rs : rowsets) { |
4189 | 0 | uint32_t rs_crc_value; |
4190 | 0 | int64_t rs_file_count = 0; |
4191 | 0 | auto rowset = std::static_pointer_cast<BetaRowset>(rs); |
4192 | 0 | auto st = rowset->calc_local_file_crc(&rs_crc_value, &rs_file_count); |
4193 | 0 | if (!st.ok()) { |
4194 | 0 | return st; |
4195 | 0 | } |
4196 | | // crc_value is calculated based on the crc_value of each rowset. |
4197 | 0 | *crc_value = crc32c::Extend(*crc_value, reinterpret_cast<const char*>(&rs_crc_value), |
4198 | 0 | sizeof(rs_crc_value)); |
4199 | 0 | *file_count += rs_file_count; |
4200 | 0 | } |
4201 | 0 | return Status::OK(); |
4202 | 0 | } |
4203 | | |
4204 | 0 | Status Tablet::show_nested_index_file(std::string* json_meta) { |
4205 | 0 | Version v(0, max_version_unlocked().second); |
4206 | 0 | std::vector<RowsetSharedPtr> rowsets; |
4207 | 0 | traverse_rowsets([&rowsets, &v](const auto& rs) { |
4208 | | // get all rowsets |
4209 | 0 | if (v.contains(rs->version())) { |
4210 | 0 | rowsets.emplace_back(rs); |
4211 | 0 | } |
4212 | 0 | }); |
4213 | 0 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
4214 | |
|
4215 | 0 | rapidjson::Document doc; |
4216 | 0 | doc.SetObject(); |
4217 | 0 | rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); |
4218 | 0 | rapidjson::Value tabletIdValue(tablet_id()); |
4219 | 0 | doc.AddMember("tablet_id", tabletIdValue, allocator); |
4220 | |
|
4221 | 0 | rapidjson::Value rowsets_value(rapidjson::kArrayType); |
4222 | |
|
4223 | 0 | for (const auto& rs : rowsets) { |
4224 | 0 | rapidjson::Value rowset_value(rapidjson::kObjectType); |
4225 | |
|
4226 | 0 | auto rowset = std::static_pointer_cast<BetaRowset>(rs); |
4227 | 0 | RETURN_IF_ERROR(rowset->show_nested_index_file(&rowset_value, allocator)); |
4228 | 0 | rowsets_value.PushBack(rowset_value, allocator); |
4229 | 0 | } |
4230 | 0 | doc.AddMember("rowsets", rowsets_value, allocator); |
4231 | |
|
4232 | 0 | rapidjson::StringBuffer buffer; |
4233 | 0 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); |
4234 | 0 | doc.Accept(writer); |
4235 | 0 | *json_meta = std::string(buffer.GetString()); |
4236 | |
|
4237 | 0 | return Status::OK(); |
4238 | 0 | } |
4239 | | |
4240 | | } // namespace doris |