be/src/storage/tablet/base_tablet.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "storage/tablet/base_tablet.h" |
19 | | |
20 | | #include <bthread/mutex.h> |
21 | | #include <crc32c/crc32c.h> |
22 | | #include <fmt/format.h> |
23 | | #include <rapidjson/prettywriter.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <cstdint> |
27 | | #include <iterator> |
28 | | #include <random> |
29 | | #include <shared_mutex> |
30 | | |
31 | | #include "cloud/cloud_tablet.h" |
32 | | #include "cloud/config.h" |
33 | | #include "common/cast_set.h" |
34 | | #include "common/logging.h" |
35 | | #include "common/metrics/doris_metrics.h" |
36 | | #include "common/status.h" |
37 | | #include "core/assert_cast.h" |
38 | | #include "core/column/column_vector.h" |
39 | | #include "core/data_type/data_type_factory.hpp" |
40 | | #include "cpp/sync_point.h" |
41 | | #include "load/memtable/memtable.h" |
42 | | #include "service/point_query_executor.h" |
43 | | #include "storage/binlog.h" |
44 | | #include "storage/compaction/cumulative_compaction_time_series_policy.h" |
45 | | #include "storage/delete/calc_delete_bitmap_executor.h" |
46 | | #include "storage/delete/delete_bitmap_calculator.h" |
47 | | #include "storage/index/primary_key_index.h" |
48 | | #include "storage/iterators.h" |
49 | | #include "storage/partial_update_info.h" |
50 | | #include "storage/rowid_conversion.h" |
51 | | #include "storage/rowset/beta_rowset.h" |
52 | | #include "storage/rowset/group_rowset_writer.h" |
53 | | #include "storage/rowset/rowset.h" |
54 | | #include "storage/rowset/rowset_factory.h" |
55 | | #include "storage/rowset/rowset_fwd.h" |
56 | | #include "storage/rowset/rowset_reader.h" |
57 | | #include "storage/rowset/rowset_writer_context.h" |
58 | | #include "storage/segment/column_reader.h" |
59 | | #include "storage/tablet/tablet_fwd.h" |
60 | | #include "storage/txn/txn_manager.h" |
61 | | #include "util/bvar_helper.h" |
62 | | #include "util/debug_points.h" |
63 | | #include "util/jsonb/serialize.h" |
64 | | |
65 | | namespace doris { |
66 | | |
67 | | using namespace ErrorCode; |
68 | | |
69 | | namespace { |
70 | | |
71 | | bvar::LatencyRecorder g_tablet_commit_phase_update_delete_bitmap_latency( |
72 | | "doris_pk", "commit_phase_update_delete_bitmap"); |
73 | | bvar::LatencyRecorder g_tablet_lookup_rowkey_latency("doris_pk", "tablet_lookup_rowkey"); |
74 | | bvar::Adder<uint64_t> g_tablet_pk_not_found("doris_pk", "lookup_not_found"); |
75 | | bvar::PerSecond<bvar::Adder<uint64_t>> g_tablet_pk_not_found_per_second( |
76 | | "doris_pk", "lookup_not_found_per_second", &g_tablet_pk_not_found, 60); |
77 | | bvar::LatencyRecorder g_tablet_update_delete_bitmap_latency("doris_pk", "update_delete_bitmap"); |
78 | | |
79 | | static bvar::Adder<size_t> g_total_tablet_num("doris_total_tablet_num"); |
80 | | |
81 | | Status _get_segment_column_iterator(const BetaRowsetSharedPtr& rowset, uint32_t segid, |
82 | | const TabletColumn& target_column, |
83 | | SegmentCacheHandle* segment_cache_handle, |
84 | | std::unique_ptr<segment_v2::ColumnIterator>* column_iterator, |
85 | | OlapReaderStatistics* stats, |
86 | 8.72k | const io::IOContext* input_io_ctx = nullptr) { |
87 | 8.72k | RETURN_IF_ERROR(SegmentLoader::instance()->load_segments(rowset, segment_cache_handle, true, |
88 | 8.72k | false, stats, input_io_ctx)); |
89 | | // find segment |
90 | 8.72k | auto it = std::find_if( |
91 | 8.72k | segment_cache_handle->get_segments().begin(), |
92 | 8.72k | segment_cache_handle->get_segments().end(), |
93 | 8.75k | [&segid](const segment_v2::SegmentSharedPtr& seg) { return seg->id() == segid; }); |
94 | 8.72k | if (it == segment_cache_handle->get_segments().end()) { |
95 | 0 | return Status::NotFound(fmt::format("rowset {} 's segemnt not found, seg_id {}", |
96 | 0 | rowset->rowset_id().to_string(), segid)); |
97 | 0 | } |
98 | 8.72k | segment_v2::SegmentSharedPtr segment = *it; |
99 | 8.72k | StorageReadOptions opts; |
100 | 8.72k | opts.stats = stats; |
101 | 8.72k | if (input_io_ctx != nullptr) { |
102 | 4.67k | opts.io_ctx = *input_io_ctx; |
103 | 4.67k | } |
104 | 8.72k | RETURN_IF_ERROR(segment->new_column_iterator(target_column, column_iterator, &opts)); |
105 | 8.72k | auto io_ctx = opts.io_ctx; |
106 | 8.72k | io_ctx.reader_type = ReaderType::READER_QUERY; |
107 | 8.72k | io_ctx.file_cache_stats = &stats->file_cache_stats; |
108 | 8.72k | segment_v2::ColumnIteratorOptions opt { |
109 | 8.72k | .use_page_cache = !config::disable_storage_page_cache, |
110 | 8.72k | .file_reader = segment->file_reader().get(), |
111 | 8.72k | .stats = stats, |
112 | 8.72k | .io_ctx = io_ctx, |
113 | 8.72k | }; |
114 | 8.72k | RETURN_IF_ERROR((*column_iterator)->init(opt)); |
115 | 8.72k | return Status::OK(); |
116 | 8.72k | } |
117 | | |
118 | | } // namespace |
119 | | |
120 | | extern MetricPrototype METRIC_query_scan_bytes; |
121 | | extern MetricPrototype METRIC_query_scan_rows; |
122 | | extern MetricPrototype METRIC_query_scan_count; |
123 | | DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(flush_bytes, MetricUnit::BYTES); |
124 | | DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(flush_finish_count, MetricUnit::OPERATIONS); |
125 | | |
126 | 402k | BaseTablet::BaseTablet(TabletMetaSharedPtr tablet_meta) : _tablet_meta(std::move(tablet_meta)) { |
127 | 402k | _metric_entity = DorisMetrics::instance()->metric_registry()->register_entity( |
128 | 402k | fmt::format("Tablet.{}", tablet_id()), {{"tablet_id", std::to_string(tablet_id())}}, |
129 | 402k | MetricEntityType::kTablet); |
130 | 402k | INT_COUNTER_METRIC_REGISTER(_metric_entity, query_scan_bytes); |
131 | 402k | INT_COUNTER_METRIC_REGISTER(_metric_entity, query_scan_rows); |
132 | 402k | INT_COUNTER_METRIC_REGISTER(_metric_entity, query_scan_count); |
133 | 402k | INT_COUNTER_METRIC_REGISTER(_metric_entity, flush_bytes); |
134 | 402k | INT_COUNTER_METRIC_REGISTER(_metric_entity, flush_finish_count); |
135 | | |
136 | | // construct _timestamped_versioned_tracker from rs and stale rs meta |
137 | 402k | _timestamped_version_tracker.construct_versioned_tracker(_tablet_meta->all_rs_metas(), |
138 | 402k | _tablet_meta->all_stale_rs_metas()); |
139 | 402k | _row_binlog_version_tracker.construct_versioned_tracker( |
140 | 402k | _tablet_meta->all_row_binlog_rs_metas()); |
141 | | |
142 | | // if !_tablet_meta->all_rs_metas()[0]->tablet_schema(), |
143 | | // that mean the tablet_meta is still no upgrade to doris 1.2 versions. |
144 | | // Before doris 1.2 version, rowset metas don't have tablet schema. |
145 | | // And when upgrade to doris 1.2 version, |
146 | | // all rowset metas will be set the tablet schmea from tablet meta. |
147 | 402k | if (_tablet_meta->all_rs_metas().empty() || |
148 | 402k | !_tablet_meta->all_rs_metas().begin()->second->tablet_schema()) { |
149 | 119k | _max_version_schema = _tablet_meta->tablet_schema(); |
150 | 282k | } else { |
151 | 282k | std::vector<RowsetMetaSharedPtr> rowset_metas(_tablet_meta->all_rs_metas().size()); |
152 | 282k | std::transform(_tablet_meta->all_rs_metas().begin(), _tablet_meta->all_rs_metas().end(), |
153 | 483k | rowset_metas.begin(), [](const auto& it) { return it.second; }); |
154 | 282k | _max_version_schema = tablet_schema_with_merged_max_schema_version(rowset_metas); |
155 | 282k | } |
156 | 402k | DCHECK(_max_version_schema); |
157 | 402k | g_total_tablet_num << 1; |
158 | 402k | } |
159 | | |
160 | 160k | BaseTablet::~BaseTablet() { |
161 | 160k | DorisMetrics::instance()->metric_registry()->deregister_entity(_metric_entity); |
162 | 160k | g_total_tablet_num << -1; |
163 | 160k | } |
164 | | |
165 | | TabletSchemaSPtr BaseTablet::tablet_schema_with_merged_max_schema_version( |
166 | 289k | const std::vector<RowsetMetaSharedPtr>& rowset_metas) { |
167 | 289k | RowsetMetaSharedPtr max_schema_version_rs = *std::max_element( |
168 | 289k | rowset_metas.begin(), rowset_metas.end(), [](const auto& a, const auto& b) -> bool { |
169 | 248k | return !a->tablet_schema() |
170 | 248k | ? true |
171 | 248k | : (!b->tablet_schema() |
172 | 248k | ? false |
173 | 248k | : a->tablet_schema()->schema_version() < |
174 | 248k | b->tablet_schema()->schema_version()); |
175 | 248k | }); |
176 | 289k | return max_schema_version_rs->tablet_schema(); |
177 | 289k | } |
178 | | |
179 | 11.9k | Status BaseTablet::set_tablet_state(TabletState state) { |
180 | 11.9k | if (_tablet_meta->tablet_state() == TABLET_SHUTDOWN && state != TABLET_SHUTDOWN) { |
181 | 0 | return Status::Error<META_INVALID_ARGUMENT>( |
182 | 0 | "could not change tablet state from shutdown to {}", state); |
183 | 0 | } |
184 | 11.9k | _tablet_meta->set_tablet_state(state); |
185 | 11.9k | return Status::OK(); |
186 | 11.9k | } |
187 | | |
188 | 2.18k | void BaseTablet::update_max_version_schema(const TabletSchemaSPtr& tablet_schema) { |
189 | 2.18k | std::lock_guard wrlock(_meta_lock); |
190 | | // Double Check for concurrent update |
191 | 2.18k | if (!_max_version_schema || |
192 | 2.18k | tablet_schema->schema_version() > _max_version_schema->schema_version()) { |
193 | 1.69k | _max_version_schema = tablet_schema; |
194 | 1.69k | } |
195 | 2.18k | } |
196 | | |
197 | 258k | uint32_t BaseTablet::get_real_compaction_score() const { |
198 | 258k | std::shared_lock l(_meta_lock); |
199 | 258k | return get_real_compaction_score_unlocked(); |
200 | 258k | } |
201 | | |
202 | 538k | uint32_t BaseTablet::get_real_compaction_score_unlocked() const { |
203 | 538k | const auto& rs_metas = _tablet_meta->all_rs_metas(); |
204 | 1.09M | return std::accumulate(rs_metas.begin(), rs_metas.end(), 0, [](uint32_t score, const auto& it) { |
205 | 1.09M | return score + it.second->get_compaction_score(); |
206 | 1.09M | }); |
207 | 538k | } |
208 | | |
209 | | Status BaseTablet::capture_rs_readers_unlocked(const Versions& version_path, |
210 | 0 | std::vector<RowSetSplits>* rs_splits) const { |
211 | 0 | DCHECK(rs_splits != nullptr && rs_splits->empty()); |
212 | 0 | for (auto version : version_path) { |
213 | 0 | auto it = _rs_version_map.find(version); |
214 | 0 | if (it == _rs_version_map.end()) { |
215 | 0 | VLOG_NOTICE << "fail to find Rowset in rs_version for version. tablet=" << tablet_id() |
216 | 0 | << ", version='" << version.first << "-" << version.second; |
217 | |
|
218 | 0 | it = _stale_rs_version_map.find(version); |
219 | 0 | if (it == _stale_rs_version_map.end()) { |
220 | 0 | return Status::Error<CAPTURE_ROWSET_READER_ERROR>( |
221 | 0 | "fail to find Rowset in stale_rs_version for version. tablet={}, " |
222 | 0 | "version={}-{}", |
223 | 0 | tablet_id(), version.first, version.second); |
224 | 0 | } |
225 | 0 | } |
226 | 0 | RowsetReaderSharedPtr rs_reader; |
227 | 0 | auto res = it->second->create_reader(&rs_reader); |
228 | 0 | if (!res.ok()) { |
229 | 0 | return Status::Error<CAPTURE_ROWSET_READER_ERROR>( |
230 | 0 | "failed to create reader for rowset:{}", it->second->rowset_id().to_string()); |
231 | 0 | } |
232 | 0 | rs_splits->emplace_back(std::move(rs_reader)); |
233 | 0 | } |
234 | 0 | return Status::OK(); |
235 | 0 | } |
236 | | |
237 | | // snapshot manager may call this api to check if version exists, so that |
238 | | // the version maybe not exist |
239 | | RowsetSharedPtr BaseTablet::get_rowset_by_version(const Version& version, |
240 | 133k | bool find_in_stale) const { |
241 | 133k | auto iter = _rs_version_map.find(version); |
242 | 133k | if (iter == _rs_version_map.end()) { |
243 | 132k | if (find_in_stale) { |
244 | 0 | return get_stale_rowset_by_version(version); |
245 | 0 | } |
246 | 132k | return nullptr; |
247 | 132k | } |
248 | 764 | return iter->second; |
249 | 133k | } |
250 | | |
251 | 0 | RowsetSharedPtr BaseTablet::get_stale_rowset_by_version(const Version& version) const { |
252 | 0 | auto iter = _stale_rs_version_map.find(version); |
253 | 0 | if (iter == _stale_rs_version_map.end()) { |
254 | 0 | VLOG_NOTICE << "no rowset for version:" << version << ", tablet: " << tablet_id(); |
255 | 0 | return nullptr; |
256 | 0 | } |
257 | 0 | return iter->second; |
258 | 0 | } |
259 | | |
260 | 0 | RowsetSharedPtr BaseTablet::get_row_binlog_rowset_by_version(const Version& version) const { |
261 | 0 | auto iter = _row_binlog_rs_version_map.find(version); |
262 | 0 | if (iter == _row_binlog_rs_version_map.end()) { |
263 | 0 | return nullptr; |
264 | 0 | } |
265 | 0 | return iter->second; |
266 | 0 | } |
267 | | |
268 | | // Already under _meta_lock |
269 | 241k | RowsetSharedPtr BaseTablet::get_rowset_with_max_version() const { |
270 | 241k | Version max_version = _tablet_meta->max_version(); |
271 | 241k | if (max_version.first == -1) { |
272 | 0 | return nullptr; |
273 | 0 | } |
274 | | |
275 | 241k | auto iter = _rs_version_map.find(max_version); |
276 | 241k | if (iter == _rs_version_map.end()) { |
277 | 0 | DCHECK(false) << "invalid version:" << max_version; |
278 | 0 | return nullptr; |
279 | 0 | } |
280 | 241k | return iter->second; |
281 | 241k | } |
282 | | |
283 | 11 | Status BaseTablet::get_all_rs_id(int64_t max_version, RowsetIdUnorderedSet* rowset_ids) const { |
284 | 11 | std::shared_lock rlock(_meta_lock); |
285 | 11 | return get_all_rs_id_unlocked(max_version, rowset_ids); |
286 | 11 | } |
287 | | |
288 | | Status BaseTablet::get_all_rs_id_unlocked(int64_t max_version, |
289 | 120k | RowsetIdUnorderedSet* rowset_ids) const { |
290 | | // Ensure that the obtained versions of rowsets are continuous |
291 | 120k | Version spec_version(0, max_version); |
292 | 120k | Versions version_path; |
293 | 120k | auto st = _timestamped_version_tracker.capture_consistent_versions(spec_version, &version_path); |
294 | 120k | if (!st.ok()) [[unlikely]] { |
295 | 0 | return st; |
296 | 0 | } |
297 | | |
298 | 1.24M | for (auto& ver : version_path) { |
299 | 1.24M | if (ver.second == 1) { |
300 | | // [0-1] rowset is empty for each tablet, skip it |
301 | 120k | continue; |
302 | 120k | } |
303 | 1.12M | auto it = _rs_version_map.find(ver); |
304 | 1.12M | if (it == _rs_version_map.end()) { |
305 | 0 | return Status::Error<CAPTURE_ROWSET_ERROR, false>( |
306 | 0 | "fail to find Rowset for version. tablet={}, version={}", tablet_id(), |
307 | 0 | ver.to_string()); |
308 | 0 | } |
309 | 1.12M | rowset_ids->emplace(it->second->rowset_id()); |
310 | 1.12M | } |
311 | 120k | return Status::OK(); |
312 | 120k | } |
313 | | |
314 | 0 | Versions BaseTablet::get_missed_versions(int64_t spec_version) const { |
315 | 0 | DCHECK(spec_version > 0) << "invalid spec_version: " << spec_version; |
316 | |
|
317 | 0 | Versions existing_versions; |
318 | 0 | { |
319 | 0 | std::shared_lock rdlock(_meta_lock); |
320 | 0 | for (const auto& [ver, _] : _tablet_meta->all_rs_metas()) { |
321 | 0 | existing_versions.emplace_back(ver); |
322 | 0 | } |
323 | 0 | } |
324 | 0 | return calc_missed_versions(spec_version, std::move(existing_versions)); |
325 | 0 | } |
326 | | |
327 | | Versions BaseTablet::get_missed_versions_unlocked(int64_t spec_version, |
328 | 1.01k | bool capture_row_binlog) const { |
329 | 1.01k | DCHECK(spec_version > 0) << "invalid spec_version: " << spec_version; |
330 | | |
331 | 1.01k | Versions existing_versions; |
332 | 1.01k | const auto& rs_metas = capture_row_binlog ? _tablet_meta->all_row_binlog_rs_metas() |
333 | 1.01k | : _tablet_meta->all_rs_metas(); |
334 | 2.58k | for (const auto& [ver, _] : rs_metas) { |
335 | 2.58k | existing_versions.emplace_back(ver); |
336 | 2.58k | } |
337 | 1.01k | return calc_missed_versions(spec_version, std::move(existing_versions)); |
338 | 1.01k | } |
339 | | |
340 | 2 | void BaseTablet::_print_missed_versions(const Versions& missed_versions) const { |
341 | 2 | std::stringstream ss; |
342 | 2 | ss << tablet_id() << " has " << missed_versions.size() << " missed version:"; |
343 | | // print at most 10 version |
344 | 6 | for (int i = 0; i < 10 && i < missed_versions.size(); ++i) { |
345 | 4 | ss << missed_versions[i] << ","; |
346 | 4 | } |
347 | 2 | LOG(WARNING) << ss.str(); |
348 | 2 | } |
349 | | |
350 | 3.37k | bool BaseTablet::_reconstruct_version_tracker_if_necessary() { |
351 | 3.37k | double data_orphan_vertex_ratio = _timestamped_version_tracker.get_orphan_vertex_ratio(); |
352 | 3.37k | double row_binlog_orphan_vertex_ratio = _row_binlog_version_tracker.get_orphan_vertex_ratio(); |
353 | 3.37k | if (data_orphan_vertex_ratio >= config::tablet_version_graph_orphan_vertex_ratio) { |
354 | 3.32k | _timestamped_version_tracker.construct_versioned_tracker( |
355 | 3.32k | _tablet_meta->all_rs_metas(), _tablet_meta->all_stale_rs_metas()); |
356 | 3.32k | return true; |
357 | 3.32k | } else if (row_binlog_orphan_vertex_ratio >= config::tablet_version_graph_orphan_vertex_ratio) { |
358 | 0 | _row_binlog_version_tracker.construct_versioned_tracker( |
359 | 0 | _tablet_meta->all_row_binlog_rs_metas()); |
360 | 0 | return true; |
361 | 0 | } |
362 | 48 | return false; |
363 | 3.37k | } |
364 | | |
365 | | // should use this method to get a copy of current tablet meta |
366 | | // there are some rowset meta in local meta store and in in-memory tablet meta |
367 | | // but not in tablet meta in local meta store |
368 | | void BaseTablet::generate_tablet_meta_copy(TabletMeta& new_tablet_meta, |
369 | 2 | bool cloud_get_rowset_meta) const { |
370 | 2 | std::shared_lock rdlock(_meta_lock); |
371 | 2 | generate_tablet_meta_copy_unlocked(new_tablet_meta, cloud_get_rowset_meta); |
372 | 2 | } |
373 | | |
374 | | // this is a unlocked version of generate_tablet_meta_copy() |
375 | | // some method already hold the _meta_lock before calling this, |
376 | | // such as EngineCloneTask::_finish_clone -> tablet->revise_tablet_meta |
377 | | void BaseTablet::generate_tablet_meta_copy_unlocked(TabletMeta& new_tablet_meta, |
378 | 6 | bool cloud_get_rowset_meta) const { |
379 | 6 | TabletMetaPB tablet_meta_pb; |
380 | 6 | _tablet_meta->to_meta_pb(&tablet_meta_pb, cloud_get_rowset_meta); |
381 | 6 | new_tablet_meta.init_from_pb(tablet_meta_pb); |
382 | 6 | } |
383 | | |
384 | | Status BaseTablet::calc_delete_bitmap_between_segments( |
385 | | TabletSchemaSPtr schema, RowsetId rowset_id, |
386 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, DeleteBitmapPtr delete_bitmap, |
387 | 8 | int64_t queue_time_us) { |
388 | 8 | size_t const num_segments = segments.size(); |
389 | 8 | if (num_segments < 2) { |
390 | 8 | return Status::OK(); |
391 | 8 | } |
392 | | |
393 | 0 | OlapStopWatch watch; |
394 | 0 | size_t seq_col_length = 0; |
395 | 0 | if (schema->has_sequence_col()) { |
396 | 0 | auto seq_col_idx = schema->sequence_col_idx(); |
397 | 0 | seq_col_length = schema->column(seq_col_idx).length() + 1; |
398 | 0 | } |
399 | 0 | size_t rowid_length = 0; |
400 | 0 | if (!schema->cluster_key_uids().empty()) { |
401 | 0 | rowid_length = PrimaryKeyIndexReader::ROW_ID_LENGTH; |
402 | 0 | } |
403 | |
|
404 | 0 | MergeIndexDeleteBitmapCalculator calculator; |
405 | 0 | RETURN_IF_ERROR(calculator.init(rowset_id, segments, seq_col_length, rowid_length)); |
406 | | |
407 | 0 | RETURN_IF_ERROR(calculator.calculate_all(delete_bitmap)); |
408 | | |
409 | 0 | delete_bitmap->add( |
410 | 0 | {rowset_id, DeleteBitmap::INVALID_SEGMENT_ID, DeleteBitmap::TEMP_VERSION_COMMON}, |
411 | 0 | DeleteBitmap::ROWSET_SENTINEL_MARK); |
412 | 0 | LOG(INFO) << fmt::format( |
413 | 0 | "construct delete bitmap between segments, " |
414 | 0 | "tablet: {}, rowset: {}, number of segments: {}, bitmap count: {}, bitmap cardinality: " |
415 | 0 | "{}, queue_time_us: {}, cost {} (us)", |
416 | 0 | tablet_id(), rowset_id.to_string(), num_segments, |
417 | 0 | delete_bitmap->get_delete_bitmap_count(), delete_bitmap->cardinality(), queue_time_us, |
418 | 0 | watch.get_elapse_time_us()); |
419 | 0 | return Status::OK(); |
420 | 0 | } |
421 | | |
422 | | std::vector<RowsetSharedPtr> BaseTablet::get_rowset_by_ids( |
423 | 140k | const RowsetIdUnorderedSet* specified_rowset_ids) { |
424 | 140k | std::vector<RowsetSharedPtr> rowsets; |
425 | 1.46M | for (auto& rs : _rs_version_map) { |
426 | 1.46M | if (!specified_rowset_ids || |
427 | 1.46M | specified_rowset_ids->find(rs.second->rowset_id()) != specified_rowset_ids->end()) { |
428 | 679k | rowsets.push_back(rs.second); |
429 | 679k | } |
430 | 1.46M | } |
431 | | |
432 | 2.62M | std::sort(rowsets.begin(), rowsets.end(), [](RowsetSharedPtr& lhs, RowsetSharedPtr& rhs) { |
433 | 2.62M | return lhs->end_version() > rhs->end_version(); |
434 | 2.62M | }); |
435 | 140k | return rowsets; |
436 | 140k | } |
437 | | |
438 | | Status BaseTablet::lookup_row_data(const Slice& encoded_key, const RowLocation& row_location, |
439 | | RowsetSharedPtr input_rowset, OlapReaderStatistics& stats, |
440 | | std::string& values, bool write_to_cache, |
441 | 4.67k | const io::IOContext* io_ctx) { |
442 | 4.67k | MonotonicStopWatch watch; |
443 | 4.67k | size_t row_size = 1; |
444 | 4.67k | watch.start(); |
445 | 4.67k | Defer _defer([&]() { |
446 | 4.67k | LOG_EVERY_N(INFO, 500) << "get a single_row, cost(us):" << watch.elapsed_time() / 1000 |
447 | 10 | << ", row_size:" << row_size; |
448 | 4.67k | }); |
449 | | |
450 | 4.67k | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(input_rowset); |
451 | 4.67k | CHECK(rowset); |
452 | 4.67k | const TabletSchemaSPtr tablet_schema = rowset->tablet_schema(); |
453 | 4.67k | SegmentCacheHandle segment_cache_handle; |
454 | 4.67k | std::unique_ptr<segment_v2::ColumnIterator> column_iterator; |
455 | 4.67k | const auto& column = *DORIS_TRY(tablet_schema->column(BeConsts::ROW_STORE_COL)); |
456 | 4.67k | RETURN_IF_ERROR(_get_segment_column_iterator(rowset, row_location.segment_id, column, |
457 | 4.67k | &segment_cache_handle, &column_iterator, &stats, |
458 | 4.67k | io_ctx)); |
459 | | // get and parse tuple row |
460 | 4.67k | MutableColumnPtr column_ptr = ColumnString::create(); |
461 | 4.67k | std::vector<segment_v2::rowid_t> rowids {static_cast<segment_v2::rowid_t>(row_location.row_id)}; |
462 | 4.67k | RETURN_IF_ERROR(column_iterator->read_by_rowids(rowids.data(), 1, column_ptr)); |
463 | 4.67k | assert(column_ptr->size() == 1); |
464 | 4.67k | auto* string_column = static_cast<ColumnString*>(column_ptr.get()); |
465 | 4.67k | StringRef value = string_column->get_data_at(0); |
466 | 4.67k | values = value.to_string(); |
467 | 4.67k | if (write_to_cache) { |
468 | 0 | RowCache::instance()->insert({tablet_id(), encoded_key}, Slice {value.data, value.size}); |
469 | 0 | } |
470 | 4.67k | return Status::OK(); |
471 | 4.67k | } |
472 | | |
473 | | // NOLINTNEXTLINE(readability-function-size): Keep the existing rowset lookup flow together. |
474 | | Status BaseTablet::lookup_row_key(const Slice& encoded_key, TabletSchema* latest_schema, |
475 | | bool with_seq_col, |
476 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
477 | | RowLocation* row_location, int64_t version, |
478 | | std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches, |
479 | | RowsetSharedPtr* rowset, bool with_rowid, |
480 | | std::string* encoded_seq_value, OlapReaderStatistics* stats, |
481 | 3.81M | DeleteBitmapPtr delete_bitmap, const io::IOContext* io_ctx) { |
482 | 3.81M | SCOPED_BVAR_LATENCY(g_tablet_lookup_rowkey_latency); |
483 | 3.81M | size_t seq_col_length = 0; |
484 | | // use the latest tablet schema to decide if the tablet has sequence column currently |
485 | 3.81M | const TabletSchema* schema = |
486 | 3.81M | (latest_schema == nullptr ? _tablet_meta->tablet_schema().get() : latest_schema); |
487 | 3.81M | if (schema->has_sequence_col() && with_seq_col) { |
488 | 23.3k | seq_col_length = schema->column(schema->sequence_col_idx()).length() + 1; |
489 | 23.3k | } |
490 | 3.81M | size_t rowid_length = 0; |
491 | 3.82M | if (with_rowid && !schema->cluster_key_uids().empty()) { |
492 | 71.2k | rowid_length = PrimaryKeyIndexReader::ROW_ID_LENGTH; |
493 | 71.2k | } |
494 | 3.81M | Slice key_without_seq = |
495 | 3.81M | Slice(encoded_key.get_data(), encoded_key.get_size() - seq_col_length - rowid_length); |
496 | 3.81M | RowLocation loc; |
497 | | |
498 | 3.81M | auto tablet_delete_bitmap = |
499 | 3.81M | delete_bitmap == nullptr ? _tablet_meta->delete_bitmap_ptr() : delete_bitmap; |
500 | 4.26M | for (size_t i = 0; i < specified_rowsets.size(); i++) { |
501 | 4.26M | const auto& rs = specified_rowsets[i]; |
502 | 4.26M | std::vector<KeyBoundsPB> segments_key_bounds; |
503 | 4.26M | rs->rowset_meta()->get_segments_key_bounds(&segments_key_bounds); |
504 | 4.26M | int num_segments = cast_set<int>(rs->num_segments()); |
505 | | // MOW lookup requires per-segment bounds. Aggregation must be disabled |
506 | | // for MOW writers, but enforce at runtime too — indexing segments_key_bounds[j] |
507 | | // below would be out-of-bounds otherwise. |
508 | 4.26M | if (UNLIKELY(rs->rowset_meta()->is_segments_key_bounds_aggregated() || |
509 | 4.26M | static_cast<int>(segments_key_bounds.size()) != num_segments)) { |
510 | 0 | return Status::InternalError( |
511 | 0 | "MOW lookup got rowset with inconsistent segments_key_bounds, rowset_id={}, " |
512 | 0 | "aggregated={}, bounds_size={}, num_segments={}", |
513 | 0 | rs->rowset_id().to_string(), |
514 | 0 | rs->rowset_meta()->is_segments_key_bounds_aggregated(), |
515 | 0 | segments_key_bounds.size(), num_segments); |
516 | 0 | } |
517 | 4.26M | std::vector<uint32_t> picked_segments; |
518 | 8.31M | for (int j = num_segments - 1; j >= 0; j--) { |
519 | 4.05M | if (_key_is_not_in_segment(key_without_seq, segments_key_bounds[j], |
520 | 4.05M | rs->rowset_meta()->is_segments_key_bounds_truncated())) { |
521 | 166k | continue; |
522 | 166k | } |
523 | 3.88M | picked_segments.emplace_back(j); |
524 | 3.88M | } |
525 | 4.26M | if (picked_segments.empty()) { |
526 | 358k | continue; |
527 | 358k | } |
528 | | |
529 | 3.90M | if (UNLIKELY(segment_caches[i] == nullptr)) { |
530 | 54.3k | segment_caches[i] = std::make_unique<SegmentCacheHandle>(); |
531 | 54.3k | RETURN_IF_ERROR(SegmentLoader::instance()->load_segments( |
532 | 54.3k | std::static_pointer_cast<BetaRowset>(rs), segment_caches[i].get(), true, true, |
533 | 54.3k | stats, io_ctx)); |
534 | 54.3k | } |
535 | 3.90M | auto& segments = segment_caches[i]->get_segments(); |
536 | 3.90M | DCHECK_EQ(segments.size(), num_segments); |
537 | | |
538 | 3.90M | for (auto id : picked_segments) { |
539 | 3.89M | Status s = segments[id]->lookup_row_key(encoded_key, schema, with_seq_col, with_rowid, |
540 | 3.89M | &loc, stats, encoded_seq_value, io_ctx); |
541 | 3.89M | if (s.is<KEY_NOT_FOUND>()) { |
542 | 68.4k | continue; |
543 | 68.4k | } |
544 | 3.83M | if (!s.ok() && !s.is<KEY_ALREADY_EXISTS>()) { |
545 | 0 | return s; |
546 | 0 | } |
547 | 3.83M | if (s.ok() && tablet_delete_bitmap->contains_agg_with_cache_if_eligible( |
548 | 3.82M | {loc.rowset_id, loc.segment_id, version}, loc.row_id)) { |
549 | | // if has sequence col, we continue to compare the sequence_id of |
550 | | // all rowsets, util we find an existing key. |
551 | 321 | if (schema->has_sequence_col()) { |
552 | 189 | continue; |
553 | 189 | } |
554 | | // The key is deleted, we don't need to search for it any more. |
555 | 132 | break; |
556 | 321 | } |
557 | | // `st` is either OK or KEY_ALREADY_EXISTS now. |
558 | | // for partial update, even if the key is already exists, we still need to |
559 | | // read it's original values to keep all columns align. |
560 | 3.83M | *row_location = loc; |
561 | 3.83M | if (rowset) { |
562 | | // return it's rowset |
563 | 3.83M | *rowset = rs; |
564 | 3.83M | } |
565 | 3.83M | TEST_SYNC_POINT_CALLBACK("BaseTablet::lookup_row_key:found", this, rs.get(), |
566 | 3.83M | with_seq_col, s.code()); |
567 | | // find it and return |
568 | 3.83M | return s; |
569 | 3.83M | } |
570 | 3.90M | } |
571 | 18.4E | g_tablet_pk_not_found << 1; |
572 | 18.4E | return Status::Error<ErrorCode::KEY_NOT_FOUND>("can't find key in all rowsets"); |
573 | 3.81M | } |
574 | | |
575 | | // if user pass a token, then all calculation works will submit to a threadpool, |
576 | | // user can get all delete bitmaps from that token. |
577 | | // if `token` is nullptr, the calculation will run in local, and user can get the result |
578 | | // delete bitmap from `delete_bitmap` directly. |
579 | | Status BaseTablet::calc_delete_bitmap(const BaseTabletSPtr& tablet, RowsetSharedPtr rowset, |
580 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, |
581 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
582 | | DeleteBitmapPtr delete_bitmap, int64_t end_version, |
583 | | CalcDeleteBitmapToken* token, RowsetWriter* rowset_writer, |
584 | 87.7k | DeleteBitmapPtr tablet_delete_bitmap) { |
585 | 87.7k | if (specified_rowsets.empty() || segments.empty()) { |
586 | 66.1k | return Status::OK(); |
587 | 66.1k | } |
588 | | |
589 | 21.5k | OlapStopWatch watch; |
590 | 21.6k | for (const auto& segment : segments) { |
591 | 21.6k | const auto& seg = segment; |
592 | 21.6k | if (token != nullptr) { |
593 | 5.90k | RETURN_IF_ERROR(token->submit(tablet, rowset, seg, specified_rowsets, end_version, |
594 | 5.90k | delete_bitmap, rowset_writer, tablet_delete_bitmap)); |
595 | 15.7k | } else { |
596 | 15.7k | RETURN_IF_ERROR(tablet->calc_segment_delete_bitmap( |
597 | 15.7k | rowset, segment, specified_rowsets, delete_bitmap, end_version, rowset_writer, |
598 | 15.7k | tablet_delete_bitmap)); |
599 | 15.7k | } |
600 | 21.6k | } |
601 | | |
602 | 21.5k | return Status::OK(); |
603 | 21.5k | } |
604 | | |
605 | | Status BaseTablet::calc_segment_delete_bitmap(RowsetSharedPtr rowset, |
606 | | const segment_v2::SegmentSharedPtr& seg, |
607 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
608 | | DeleteBitmapPtr delete_bitmap, int64_t end_version, |
609 | | RowsetWriter* rowset_writer, |
610 | | DeleteBitmapPtr tablet_delete_bitmap, |
611 | 21.6k | int64_t queue_time_us) { |
612 | 21.6k | OlapStopWatch watch; |
613 | 21.6k | auto rowset_id = rowset->rowset_id(); |
614 | 21.6k | Version dummy_version(end_version + 1, end_version + 1); |
615 | 21.6k | auto rowset_schema = rowset->tablet_schema(); |
616 | | |
617 | 21.6k | PartialUpdateInfo* partial_update_info = |
618 | 21.6k | rowset_writer != nullptr ? rowset_writer->get_partial_update_info().get() : nullptr; |
619 | 21.6k | bool is_partial_update = partial_update_info && partial_update_info->is_partial_update(); |
620 | 21.6k | bool need_rewrite_conflict = partial_update_info != nullptr; |
621 | | // `have_input_seq_column` is for fixed partial update only. For flexible partial update, we should use |
622 | | // the skip bitmap to determine wheather a row has specified the sequence column |
623 | 21.6k | bool have_input_seq_column = false; |
624 | | // `rids_be_overwritten` is for flexible partial update only, it records row ids that is overwritten by |
625 | | // another row with higher seqeucne value |
626 | 21.6k | std::set<uint32_t> rids_be_overwritten; |
627 | 21.6k | if (is_partial_update) { |
628 | 93 | if (partial_update_info->is_fixed_partial_update() && rowset_schema->has_sequence_col()) { |
629 | 4 | std::vector<uint32_t> including_cids = |
630 | 4 | rowset_writer->get_partial_update_info()->update_cids; |
631 | 4 | have_input_seq_column = |
632 | 4 | rowset_schema->has_sequence_col() && |
633 | 4 | (std::find(including_cids.cbegin(), including_cids.cend(), |
634 | 4 | rowset_schema->sequence_col_idx()) != including_cids.cend()); |
635 | 4 | } |
636 | 93 | } |
637 | | |
638 | 21.6k | if (rowset_schema->num_variant_columns() > 0) { |
639 | | // During partial updates, the extracted columns of a variant should not be included in the rowset schema. |
640 | | // This is because the partial update for a variant needs to ignore the extracted columns. |
641 | | // Otherwise, the schema types in different rowsets might be inconsistent. When performing a partial update, |
642 | | // the complete variant is constructed by reading all the sub-columns of the variant. |
643 | 4.17k | rowset_schema = rowset_schema->copy_without_variant_extracted_columns(); |
644 | 4.17k | } |
645 | | // use for partial update |
646 | 21.6k | FixedReadPlan read_plan_ori; |
647 | 21.6k | FixedReadPlan read_plan_update; |
648 | | // used to read sink-time LSN from input row-binlog rowset |
649 | 21.6k | FixedReadPlan read_plan_lsn; |
650 | 21.6k | RowsetSharedPtr row_binlog_rowset; |
651 | 21.6k | if (rowset_writer != nullptr) { |
652 | 93 | if (auto* group_writer = typeid_cast<GroupRowsetWriter*>(rowset_writer); |
653 | 93 | group_writer != nullptr) { |
654 | 0 | auto& binlog_ctx = |
655 | 0 | const_cast<RowsetWriterContext&>(group_writer->row_binlog_writer()->context()); |
656 | 0 | if (binlog_ctx.write_binlog_opt().enable) { |
657 | 0 | row_binlog_rowset = binlog_ctx.write_binlog_opt() |
658 | 0 | .write_binlog_config() |
659 | 0 | .source.row_binlog_rowset; |
660 | 0 | DCHECK(row_binlog_rowset != nullptr); |
661 | 0 | } |
662 | 0 | } |
663 | 93 | } |
664 | 21.6k | int64_t conflict_rows = 0; |
665 | 21.6k | int64_t new_generated_rows = 0; |
666 | | |
667 | 21.6k | std::map<RowsetId, RowsetSharedPtr> rsid_to_rowset; |
668 | 21.6k | rsid_to_rowset[rowset_id] = rowset; |
669 | 21.6k | Block block = rowset_schema->create_block(); |
670 | 21.6k | Block ordered_block = block.clone_empty(); |
671 | 21.6k | uint32_t pos = 0; |
672 | | |
673 | 21.6k | RETURN_IF_ERROR(seg->load_pk_index_and_bf(nullptr)); // We need index blocks to iterate |
674 | 21.6k | const auto* pk_idx = seg->get_primary_key_index(); |
675 | 21.6k | int64_t total = pk_idx->num_rows(); |
676 | 21.6k | uint32_t row_id = 0; |
677 | 21.6k | int64_t remaining = total; |
678 | 21.6k | bool exact_match = false; |
679 | 21.6k | std::string last_key; |
680 | 21.6k | int batch_size = 1024; |
681 | | // The data for each segment may be lookup multiple times. Creating a SegmentCacheHandle |
682 | | // will update the lru cache, and there will be obvious lock competition in multithreading |
683 | | // scenarios, so using a segment_caches to cache SegmentCacheHandle. |
684 | 21.6k | std::vector<std::unique_ptr<SegmentCacheHandle>> segment_caches(specified_rowsets.size()); |
685 | 46.6k | while (remaining > 0) { |
686 | 25.0k | std::unique_ptr<segment_v2::IndexedColumnIterator> iter; |
687 | 25.0k | RETURN_IF_ERROR(pk_idx->new_iterator(&iter, nullptr)); |
688 | | |
689 | 25.0k | size_t num_to_read = std::min<int64_t>(batch_size, remaining); |
690 | 25.0k | auto index_type = DataTypeFactory::instance().create_data_type(pk_idx->type(), 1, 0); |
691 | 25.0k | auto index_column = index_type->create_column(); |
692 | 25.0k | Slice last_key_slice(last_key); |
693 | 25.0k | RETURN_IF_ERROR(iter->seek_at_or_after(&last_key_slice, &exact_match)); |
694 | 25.0k | auto current_ordinal = iter->get_current_ordinal(); |
695 | 25.0k | DCHECK(total == remaining + current_ordinal) |
696 | 104 | << "total: " << total << ", remaining: " << remaining |
697 | 104 | << ", current_ordinal: " << current_ordinal; |
698 | | |
699 | 25.0k | size_t num_read = num_to_read; |
700 | 25.0k | RETURN_IF_ERROR(iter->next_batch(&num_read, index_column)); |
701 | 18.4E | DCHECK(num_to_read == num_read) |
702 | 18.4E | << "num_to_read: " << num_to_read << ", num_read: " << num_read; |
703 | 25.0k | last_key = index_column->get_data_at(num_read - 1).to_string(); |
704 | | |
705 | | // exclude last_key, last_key will be read in next batch. |
706 | 25.0k | if (num_read == batch_size && num_read != remaining) { |
707 | 3.55k | num_read -= 1; |
708 | 3.55k | } |
709 | 3.85M | for (size_t i = 0; i < num_read; i++, row_id++) { |
710 | 3.82M | Slice key = Slice(index_column->get_data_at(i).data, index_column->get_data_at(i).size); |
711 | 3.82M | RowLocation loc; |
712 | | // calculate row id |
713 | 3.82M | if (!_tablet_meta->tablet_schema()->cluster_key_uids().empty()) { |
714 | 71.2k | size_t seq_col_length = 0; |
715 | 71.2k | if (_tablet_meta->tablet_schema()->has_sequence_col()) { |
716 | 11.9k | seq_col_length = |
717 | 11.9k | _tablet_meta->tablet_schema() |
718 | 11.9k | ->column(_tablet_meta->tablet_schema()->sequence_col_idx()) |
719 | 11.9k | .length() + |
720 | 11.9k | 1; |
721 | 11.9k | } |
722 | 71.2k | size_t rowid_length = PrimaryKeyIndexReader::ROW_ID_LENGTH; |
723 | 71.2k | Slice key_without_seq = |
724 | 71.2k | Slice(key.get_data(), key.get_size() - seq_col_length - rowid_length); |
725 | 71.2k | Slice rowid_slice = |
726 | 71.2k | Slice(key.get_data() + key_without_seq.get_size() + seq_col_length + 1, |
727 | 71.2k | rowid_length - 1); |
728 | 71.2k | const auto* rowid_coder = get_key_coder(FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT); |
729 | 71.2k | RETURN_IF_ERROR(rowid_coder->decode_ascending(&rowid_slice, rowid_length, |
730 | 71.2k | (uint8_t*)&row_id)); |
731 | 71.2k | } |
732 | | // same row in segments should be filtered |
733 | 3.82M | if (delete_bitmap->contains({rowset_id, seg->id(), DeleteBitmap::TEMP_VERSION_COMMON}, |
734 | 3.82M | row_id)) { |
735 | 6 | continue; |
736 | 6 | } |
737 | | |
738 | 3.82M | DBUG_EXECUTE_IF("BaseTablet::calc_segment_delete_bitmap.inject_err", { |
739 | 3.82M | auto p = dp->param("percent", 0.01); |
740 | 3.82M | std::mt19937 gen {std::random_device {}()}; |
741 | 3.82M | std::bernoulli_distribution inject_fault {p}; |
742 | 3.82M | if (inject_fault(gen)) { |
743 | 3.82M | return Status::InternalError( |
744 | 3.82M | "injection error in calc_segment_delete_bitmap, " |
745 | 3.82M | "tablet_id={}, rowset_id={}", |
746 | 3.82M | tablet_id(), rowset_id.to_string()); |
747 | 3.82M | } |
748 | 3.82M | }); |
749 | | |
750 | 3.82M | RowsetSharedPtr rowset_find; |
751 | 3.82M | Status st = Status::OK(); |
752 | 3.82M | if (tablet_delete_bitmap == nullptr) { |
753 | 2.33M | st = lookup_row_key(key, rowset_schema.get(), true, specified_rowsets, &loc, |
754 | 2.33M | dummy_version.first - 1, segment_caches, &rowset_find); |
755 | 2.33M | } else { |
756 | 1.48M | st = lookup_row_key(key, rowset_schema.get(), true, specified_rowsets, &loc, |
757 | 1.48M | dummy_version.first - 1, segment_caches, &rowset_find, true, |
758 | 1.48M | nullptr, nullptr, tablet_delete_bitmap); |
759 | 1.48M | } |
760 | 3.82M | bool expected_st = st.ok() || st.is<KEY_NOT_FOUND>() || st.is<KEY_ALREADY_EXISTS>(); |
761 | | // It's a defensive DCHECK, we need to exclude some common errors to avoid core-dump |
762 | | // while stress test |
763 | 18.4E | DCHECK(expected_st || st.is<MEM_LIMIT_EXCEEDED>()) |
764 | 18.4E | << "unexpected error status while lookup_row_key:" << st; |
765 | 3.82M | if (!expected_st) { |
766 | 0 | return st; |
767 | 0 | } |
768 | 3.82M | if (st.is<KEY_NOT_FOUND>()) { |
769 | 25.1k | continue; |
770 | 25.1k | } |
771 | | |
772 | 3.80M | ++conflict_rows; |
773 | 3.80M | if (st.is<KEY_ALREADY_EXISTS>() && |
774 | 3.80M | (!is_partial_update || |
775 | 296 | (partial_update_info->is_fixed_partial_update() && have_input_seq_column))) { |
776 | | // `st.is<KEY_ALREADY_EXISTS>()` means that there exists a row with the same key and larger value |
777 | | // in seqeunce column. |
778 | | // - If the current load is not a partial update, we just delete current row. |
779 | | // - Otherwise, it means that we are doing the alignment process in publish phase due to conflicts |
780 | | // during concurrent partial updates. And there exists another load which introduces a row with |
781 | | // the same keys and larger sequence column value published successfully after the commit phase |
782 | | // of the current load. |
783 | | // - If the columns we update include sequence column, we should delete the current row becase the |
784 | | // partial update on the current row has been `overwritten` by the previous one with larger sequence |
785 | | // column value. |
786 | | // - Otherwise, we should combine the values of the missing columns in the previous row and the values |
787 | | // of the including columns in the current row into a new row. |
788 | 286 | delete_bitmap->add({rowset_id, seg->id(), DeleteBitmap::TEMP_VERSION_COMMON}, |
789 | 286 | row_id); |
790 | 286 | continue; |
791 | | // NOTE: for partial update which doesn't specify the sequence column, we can't use the sequence column value filled in flush phase |
792 | | // as its final value. Otherwise it may cause inconsistency between replicas. |
793 | 286 | } |
794 | 3.80M | if (need_rewrite_conflict) { |
795 | | // In publish version, record rows to be deleted for concurrent update |
796 | | // For example, if version 5 and 6 update a row, but version 6 only see |
797 | | // version 4 when write, and when publish version, version 5's value will |
798 | | // be marked as deleted and it's update is losed. |
799 | | // So here we should read version 5's columns and build a new row, which is |
800 | | // consists of version 6's update columns and version 5's origin columns |
801 | | // here we build 2 read plan for ori values and update values |
802 | | |
803 | | // - for fixed partial update, we should read update columns from current load's rowset |
804 | | // and read missing columns from previous rowsets to create the final block |
805 | | // - for flexible partial update, we should read all columns from current load's rowset |
806 | | // and read non sort key columns from previous rowsets to create the final block |
807 | | // - for upsert rewrite, we should read all columns from current load's rowset |
808 | | // So we only need to record rows to read for both mode partial update and upsert rewrite |
809 | 83 | if (is_partial_update) { |
810 | 83 | read_plan_ori.prepare_to_read(loc, pos); |
811 | 83 | } |
812 | 83 | read_plan_update.prepare_to_read(RowLocation {rowset_id, seg->id(), row_id}, pos); |
813 | 83 | if (row_binlog_rowset != nullptr) { |
814 | 0 | read_plan_lsn.prepare_to_read( |
815 | 0 | RowLocation {row_binlog_rowset->rowset_id(), seg->id(), row_id}, pos); |
816 | 0 | } |
817 | | |
818 | | // For flexible partial update, we should use skip bitmap to determine wheather |
819 | | // a row has specified the sequence column. But skip bitmap should be read from the segment. |
820 | | // So we record these row ids and process and filter them in `generate_new_block_for_flexible_partial_update()` |
821 | 83 | if (st.is<KEY_ALREADY_EXISTS>() && |
822 | 83 | partial_update_info->is_flexible_partial_update()) { |
823 | 0 | rids_be_overwritten.insert(pos); |
824 | 0 | } |
825 | | |
826 | 83 | rsid_to_rowset[rowset_find->rowset_id()] = rowset_find; |
827 | 83 | ++pos; |
828 | | |
829 | | // delete bitmap will be calculate when memtable flush and |
830 | | // publish. The two stages may see different versions. |
831 | | // When there is sequence column, the currently imported data |
832 | | // of rowset may be marked for deletion at memtablet flush or |
833 | | // publish because the seq column is smaller than the previous |
834 | | // rowset. |
835 | | // just set 0 as a unified temporary version number, and update to |
836 | | // the real version number later. |
837 | 83 | delete_bitmap->add( |
838 | 83 | {loc.rowset_id, loc.segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, |
839 | 83 | loc.row_id); |
840 | 83 | delete_bitmap->add({rowset_id, seg->id(), DeleteBitmap::TEMP_VERSION_COMMON}, |
841 | 83 | row_id); |
842 | 83 | ++new_generated_rows; |
843 | 83 | continue; |
844 | 83 | } |
845 | | // when st = ok |
846 | 3.80M | delete_bitmap->add({loc.rowset_id, loc.segment_id, DeleteBitmap::TEMP_VERSION_COMMON}, |
847 | 3.80M | loc.row_id); |
848 | 3.80M | } |
849 | 25.0k | remaining -= num_read; |
850 | 25.0k | } |
851 | | // DCHECK_EQ(total, row_id) << "segment total rows: " << total << " row_id:" << row_id; |
852 | | |
853 | 21.6k | if (config::enable_merge_on_write_correctness_check) { |
854 | 21.5k | RowsetIdUnorderedSet rowsetids; |
855 | 220k | for (const auto& specified_rowset : specified_rowsets) { |
856 | 220k | rowsetids.emplace(specified_rowset->rowset_id()); |
857 | 18.4E | VLOG_NOTICE << "[tabletID:" << tablet_id() << "]" |
858 | 18.4E | << "[add_sentinel_mark_to_delete_bitmap][end_version:" << end_version << "]" |
859 | 18.4E | << "add:" << specified_rowset->rowset_id(); |
860 | 220k | } |
861 | 21.5k | add_sentinel_mark_to_delete_bitmap(delete_bitmap.get(), rowsetids); |
862 | 21.5k | } |
863 | | |
864 | 21.6k | if (pos > 0) { |
865 | 19 | DCHECK(partial_update_info != nullptr); |
866 | 19 | if (partial_update_info->is_flexible_partial_update()) { |
867 | 0 | RETURN_IF_ERROR(generate_new_block_for_flexible_partial_update( |
868 | 0 | rowset_schema, partial_update_info, rids_be_overwritten, read_plan_ori, |
869 | 0 | read_plan_update, rsid_to_rowset, &block)); |
870 | 19 | } else { |
871 | 19 | RETURN_IF_ERROR(generate_new_block_for_partial_update( |
872 | 19 | rowset_schema, partial_update_info, read_plan_ori, read_plan_update, |
873 | 19 | rsid_to_rowset, &block)); |
874 | 19 | } |
875 | | |
876 | | // read sink-time LSN from input row-binlog rowset, aligned with `block` rows. |
877 | 19 | Block lsn_block; |
878 | 19 | if (row_binlog_rowset != nullptr) { |
879 | 0 | auto row_binlog_schema = row_binlog_rowset->tablet_schema(); |
880 | 0 | std::vector<uint32_t> lsn_cids = { |
881 | 0 | static_cast<uint32_t>(row_binlog_schema->binlog_lsn_col_idx())}; |
882 | 0 | lsn_block = row_binlog_schema->create_block_by_cids(lsn_cids); |
883 | 0 | std::map<RowsetId, RowsetSharedPtr> rsid_to_row_binlog { |
884 | 0 | {row_binlog_rowset->rowset_id(), row_binlog_rowset}}; |
885 | 0 | std::map<uint32_t, uint32_t> read_index; |
886 | 0 | RETURN_IF_ERROR(read_plan_lsn.read_columns_by_plan(*row_binlog_schema, lsn_cids, |
887 | 0 | rsid_to_row_binlog, lsn_block, |
888 | 0 | &read_index, false)); |
889 | 0 | } |
890 | | |
891 | 19 | std::vector<uint32_t> sort_perm; |
892 | 19 | RETURN_IF_ERROR(sort_block(block, ordered_block, &sort_perm)); |
893 | 19 | auto segment_id = rowset_writer->allocate_segment_id(); |
894 | | |
895 | | // Publish-phase partial update may flush transient segments to a GroupRowsetWriter. |
896 | | // For row-binlog writing, RowBinlogSegmentWriter requires `seg_id -> lsn_ids` to be |
897 | | // registered before the segment writer is constructed. |
898 | 19 | if (auto* group_writer = typeid_cast<GroupRowsetWriter*>(rowset_writer); |
899 | 19 | group_writer != nullptr) { |
900 | 0 | auto binlog_writer = group_writer->row_binlog_writer(); |
901 | 0 | auto& binlog_ctx = const_cast<RowsetWriterContext&>(binlog_writer->context()); |
902 | 0 | if (binlog_ctx.write_binlog_opt().enable) { |
903 | 0 | const auto& src = |
904 | 0 | assert_cast<const ColumnInt64&>(*lsn_block.get_by_position(0).column); |
905 | 0 | auto lsn_ids = std::make_shared<std::vector<int64_t>>(); |
906 | 0 | lsn_ids->reserve(sort_perm.size()); |
907 | 0 | for (auto p : sort_perm) { |
908 | 0 | lsn_ids->emplace_back(src.get_data()[p]); |
909 | 0 | } |
910 | 0 | binlog_ctx.write_binlog_opt().write_binlog_config().insert_seg_lsn( |
911 | 0 | segment_id, std::move(lsn_ids)); |
912 | 0 | } |
913 | 0 | } |
914 | 19 | RETURN_IF_ERROR(rowset_writer->flush_single_block(&ordered_block, segment_id)); |
915 | 19 | auto cost_us = watch.get_elapse_time_us(); |
916 | 19 | if (config::enable_mow_verbose_log || cost_us > 10 * 1000 || queue_time_us > 10 * 1000) { |
917 | 12 | LOG(INFO) << "calc segment delete bitmap for " |
918 | 12 | << partial_update_info->partial_update_mode_str() |
919 | 12 | << ", tablet: " << tablet_id() << " rowset: " << rowset_id |
920 | 12 | << " seg_id: " << seg->id() << " dummy_version: " << end_version + 1 |
921 | 12 | << " rows: " << seg->num_rows() << " conflict rows: " << conflict_rows |
922 | 12 | << " new generated rows: " << new_generated_rows |
923 | 12 | << " bitmap num: " << delete_bitmap->get_delete_bitmap_count() |
924 | 12 | << " bitmap cardinality: " << delete_bitmap->cardinality() |
925 | 12 | << " queue_time_us: " << queue_time_us << ", cost: " << cost_us << "(us)"; |
926 | 12 | } |
927 | 19 | return Status::OK(); |
928 | 19 | } |
929 | 21.6k | auto cost_us = watch.get_elapse_time_us(); |
930 | 21.6k | if (config::enable_mow_verbose_log || cost_us > 10 * 1000 || queue_time_us > 10 * 1000) { |
931 | 3.64k | LOG(INFO) << "calc segment delete bitmap, tablet: " << tablet_id() |
932 | 3.64k | << " rowset: " << rowset_id << " seg_id: " << seg->id() |
933 | 3.64k | << " dummy_version: " << end_version + 1 << " rows: " << seg->num_rows() |
934 | 3.64k | << " conflict rows: " << conflict_rows |
935 | 3.64k | << " bitmap num: " << delete_bitmap->get_delete_bitmap_count() |
936 | 3.64k | << " bitmap cardinality: " << delete_bitmap->cardinality() |
937 | 3.64k | << " queue_time_us: " << queue_time_us << ", cost: " << cost_us << "(us)"; |
938 | 3.64k | } |
939 | 21.6k | return Status::OK(); |
940 | 21.6k | } |
941 | | |
942 | | Status BaseTablet::sort_block(Block& in_block, Block& output_block, |
943 | 19 | std::vector<uint32_t>* permutation) { |
944 | 19 | ScopedMutableBlock scoped_input_block(&in_block); |
945 | 19 | auto& mutable_input_block = scoped_input_block.mutable_block(); |
946 | 19 | ScopedMutableBlock scoped_output_block(&output_block); |
947 | 19 | auto& mutable_output_block = scoped_output_block.mutable_block(); |
948 | | |
949 | 19 | std::shared_ptr<RowInBlockComparator> vec_row_comparator = |
950 | 19 | std::make_shared<RowInBlockComparator>(_tablet_meta->tablet_schema()); |
951 | 19 | vec_row_comparator->set_block(&mutable_input_block); |
952 | | |
953 | 19 | std::vector<std::unique_ptr<RowInBlock>> row_in_blocks; |
954 | 19 | const auto input_rows = mutable_input_block.rows(); |
955 | 19 | DCHECK(input_rows <= std::numeric_limits<int>::max()); |
956 | 19 | row_in_blocks.reserve(input_rows); |
957 | 102 | for (size_t i = 0; i < input_rows; ++i) { |
958 | 83 | row_in_blocks.emplace_back(std::make_unique<RowInBlock>(i)); |
959 | 83 | } |
960 | 19 | std::sort(row_in_blocks.begin(), row_in_blocks.end(), |
961 | 19 | [&](const std::unique_ptr<RowInBlock>& l, |
962 | 128 | const std::unique_ptr<RowInBlock>& r) -> bool { |
963 | 128 | auto value = (*vec_row_comparator)(l.get(), r.get()); |
964 | 128 | DCHECK(value != 0) << "value equel when sort block, l_pos: " << l->_row_pos |
965 | 0 | << " r_pos: " << r->_row_pos; |
966 | 128 | return value < 0; |
967 | 128 | }); |
968 | 19 | std::vector<uint32_t> row_pos_vec; |
969 | 19 | row_pos_vec.reserve(input_rows); |
970 | 83 | for (auto& block : row_in_blocks) { |
971 | 83 | row_pos_vec.emplace_back(block->_row_pos); |
972 | 83 | } |
973 | 19 | scoped_input_block.restore(); |
974 | 19 | RETURN_IF_ERROR(mutable_output_block.add_rows(&in_block, row_pos_vec.data(), |
975 | 19 | row_pos_vec.data() + input_rows)); |
976 | 19 | if (permutation != nullptr) { |
977 | 19 | *permutation = std::move(row_pos_vec); |
978 | 19 | } |
979 | 19 | return Status::OK(); |
980 | 19 | } |
981 | | |
982 | | // fetch value by row column |
983 | | Status BaseTablet::fetch_value_through_row_column(RowsetSharedPtr input_rowset, |
984 | | const TabletSchema& tablet_schema, uint32_t segid, |
985 | | const std::vector<uint32_t>& rowids, |
986 | 353 | const std::vector<uint32_t>& cids, Block& block) { |
987 | 353 | MonotonicStopWatch watch; |
988 | 353 | watch.start(); |
989 | 353 | Defer _defer([&]() { |
990 | 353 | LOG_EVERY_N(INFO, 500) << "fetch_value_by_rowids, cost(us):" << watch.elapsed_time() / 1000 |
991 | 2 | << ", row_batch_size:" << rowids.size(); |
992 | 353 | }); |
993 | | |
994 | 353 | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(input_rowset); |
995 | 353 | CHECK(rowset); |
996 | 353 | CHECK(tablet_schema.has_row_store_for_all_columns()); |
997 | 353 | SegmentCacheHandle segment_cache_handle; |
998 | 353 | std::unique_ptr<segment_v2::ColumnIterator> column_iterator; |
999 | 353 | OlapReaderStatistics stats; |
1000 | 353 | const auto& column = *DORIS_TRY(tablet_schema.column(BeConsts::ROW_STORE_COL)); |
1001 | 353 | RETURN_IF_ERROR(_get_segment_column_iterator(rowset, segid, column, &segment_cache_handle, |
1002 | 353 | &column_iterator, &stats)); |
1003 | | // get and parse tuple row |
1004 | 353 | MutableColumnPtr column_ptr = ColumnString::create(); |
1005 | 353 | RETURN_IF_ERROR(column_iterator->read_by_rowids(rowids.data(), rowids.size(), column_ptr)); |
1006 | 353 | assert(column_ptr->size() == rowids.size()); |
1007 | 353 | auto* string_column = static_cast<ColumnString*>(column_ptr.get()); |
1008 | 353 | DataTypeSerDeSPtrs serdes; |
1009 | 353 | serdes.resize(cids.size()); |
1010 | 353 | std::unordered_map<uint32_t, uint32_t> col_uid_to_idx; |
1011 | 353 | std::vector<std::string> default_values; |
1012 | 353 | default_values.resize(cids.size()); |
1013 | 3.82k | for (int i = 0; i < cids.size(); ++i) { |
1014 | 3.47k | const TabletColumn& tablet_column = tablet_schema.column(cids[i]); |
1015 | 3.47k | DataTypePtr type = DataTypeFactory::instance().create_data_type(tablet_column); |
1016 | 3.47k | col_uid_to_idx[tablet_column.unique_id()] = i; |
1017 | 3.47k | default_values[i] = tablet_column.default_value(); |
1018 | 3.47k | serdes[i] = type->get_serde(); |
1019 | 3.47k | } |
1020 | 353 | RETURN_IF_ERROR(JsonbSerializeUtil::jsonb_to_block(serdes, *string_column, col_uid_to_idx, |
1021 | 353 | block, default_values, {})); |
1022 | 353 | return Status::OK(); |
1023 | 353 | } |
1024 | | |
1025 | | Status BaseTablet::fetch_value_by_rowids(RowsetSharedPtr input_rowset, uint32_t segid, |
1026 | | const std::vector<uint32_t>& rowids, |
1027 | 3.69k | const TabletColumn& tablet_column, MutableColumnPtr& dst) { |
1028 | 3.69k | MonotonicStopWatch watch; |
1029 | 3.69k | watch.start(); |
1030 | 3.69k | Defer _defer([&]() { |
1031 | 3.69k | LOG_EVERY_N(INFO, 500) << "fetch_value_by_rowids, cost(us):" << watch.elapsed_time() / 1000 |
1032 | 9 | << ", row_batch_size:" << rowids.size(); |
1033 | 3.69k | }); |
1034 | | |
1035 | | // read row data |
1036 | 3.69k | BetaRowsetSharedPtr rowset = std::static_pointer_cast<BetaRowset>(input_rowset); |
1037 | 3.69k | CHECK(rowset); |
1038 | 3.69k | SegmentCacheHandle segment_cache_handle; |
1039 | 3.69k | std::unique_ptr<segment_v2::ColumnIterator> column_iterator; |
1040 | 3.69k | OlapReaderStatistics stats; |
1041 | 3.69k | RETURN_IF_ERROR(_get_segment_column_iterator(rowset, segid, tablet_column, |
1042 | 3.69k | &segment_cache_handle, &column_iterator, &stats)); |
1043 | 3.69k | RETURN_IF_ERROR(column_iterator->read_by_rowids(rowids.data(), rowids.size(), dst)); |
1044 | 3.69k | return Status::OK(); |
1045 | 3.69k | } |
1046 | | |
1047 | | const signed char* BaseTablet::get_delete_sign_column_data(const Block& block, |
1048 | 3.48k | size_t rows_at_least) { |
1049 | 3.48k | if (int pos = block.get_position_by_name(DELETE_SIGN); pos != -1) { |
1050 | 3.47k | const ColumnWithTypeAndName& delete_sign_column = block.get_by_position(pos); |
1051 | 3.47k | const auto& delete_sign_col = assert_cast<const ColumnInt8&>(*(delete_sign_column.column)); |
1052 | 3.47k | if (delete_sign_col.size() >= rows_at_least) { |
1053 | 2.89k | return delete_sign_col.get_data().data(); |
1054 | 2.89k | } |
1055 | 3.47k | } |
1056 | 592 | return nullptr; |
1057 | 3.48k | }; |
1058 | | |
1059 | | Status BaseTablet::generate_default_value_block(const TabletSchema& schema, |
1060 | | const std::vector<uint32_t>& cids, |
1061 | | const std::vector<std::string>& default_values, |
1062 | | const Block& ref_block, |
1063 | 1.56k | Block& default_value_block) { |
1064 | 1.56k | auto mutable_default_value_columns_guard = default_value_block.mutate_columns_scoped(); |
1065 | 1.56k | auto& mutable_default_value_columns = mutable_default_value_columns_guard.mutable_columns(); |
1066 | 12.3k | for (auto i = 0; i < cids.size(); ++i) { |
1067 | 10.8k | const auto& column = schema.column(cids[i]); |
1068 | 10.8k | if (column.has_default_value()) { |
1069 | 3.84k | const auto& default_value = default_values[i]; |
1070 | 3.84k | StringRef str(default_value); |
1071 | 3.84k | RETURN_IF_ERROR(ref_block.get_by_position(i).type->get_serde()->default_from_string( |
1072 | 3.84k | str, *mutable_default_value_columns[i])); |
1073 | 3.84k | } |
1074 | 10.8k | } |
1075 | 1.56k | return Status::OK(); |
1076 | 1.56k | } |
1077 | | |
1078 | | Status BaseTablet::generate_new_block_for_partial_update( |
1079 | | TabletSchemaSPtr rowset_schema, const PartialUpdateInfo* partial_update_info, |
1080 | | const FixedReadPlan& read_plan_ori, const FixedReadPlan& read_plan_update, |
1081 | 19 | const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset, Block* output_block) { |
1082 | | // do partial update related works |
1083 | | // 1. read columns by read plan |
1084 | | // 2. generate new block |
1085 | | // 3. write a new segment and modify rowset meta |
1086 | | // 4. mark current keys deleted |
1087 | 19 | CHECK(output_block); |
1088 | 19 | auto full_mutable_columns_guard = output_block->mutate_columns_scoped(); |
1089 | 19 | auto& full_mutable_columns = full_mutable_columns_guard.mutable_columns(); |
1090 | 19 | const auto& missing_cids = partial_update_info->missing_cids; |
1091 | 19 | const auto& update_cids = partial_update_info->update_cids; |
1092 | 19 | auto old_block = rowset_schema->create_block_by_cids(missing_cids); |
1093 | 19 | auto update_block = rowset_schema->create_block_by_cids(update_cids); |
1094 | | |
1095 | 19 | bool have_input_seq_column = false; |
1096 | 19 | if (rowset_schema->has_sequence_col()) { |
1097 | 4 | have_input_seq_column = |
1098 | 4 | (std::find(update_cids.cbegin(), update_cids.cend(), |
1099 | 4 | rowset_schema->sequence_col_idx()) != update_cids.cend()); |
1100 | 4 | } |
1101 | | |
1102 | | // rowid in the final block(start from 0, increase continuously) -> rowid to read in update_block |
1103 | 19 | std::map<uint32_t, uint32_t> read_index_update; |
1104 | | |
1105 | | // read current rowset first, if a row in the current rowset has delete sign mark |
1106 | | // we don't need to read values from old block |
1107 | 19 | RETURN_IF_ERROR(read_plan_update.read_columns_by_plan( |
1108 | 19 | *rowset_schema, update_cids, rsid_to_rowset, update_block, &read_index_update, false)); |
1109 | 19 | size_t update_rows = read_index_update.size(); |
1110 | 66 | for (auto i = 0; i < update_cids.size(); ++i) { |
1111 | 258 | for (auto idx = 0; idx < update_rows; ++idx) { |
1112 | 211 | full_mutable_columns[update_cids[i]]->insert_from( |
1113 | 211 | *update_block.get_by_position(i).column, read_index_update[idx]); |
1114 | 211 | } |
1115 | 47 | } |
1116 | | |
1117 | | // if there is sequence column in the table, we need to read the sequence column, |
1118 | | // otherwise it may cause the merge-on-read based compaction policy to produce incorrect results |
1119 | 19 | const auto* __restrict new_block_delete_signs = |
1120 | 19 | rowset_schema->has_sequence_col() |
1121 | 19 | ? nullptr |
1122 | 19 | : get_delete_sign_column_data(update_block, update_rows); |
1123 | | |
1124 | | // 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 |
1125 | 19 | std::map<uint32_t, uint32_t> read_index_old; |
1126 | 19 | RETURN_IF_ERROR(read_plan_ori.read_columns_by_plan(*rowset_schema, missing_cids, rsid_to_rowset, |
1127 | 19 | old_block, &read_index_old, true, |
1128 | 19 | new_block_delete_signs)); |
1129 | 19 | size_t old_rows = read_index_old.size(); |
1130 | 19 | const auto* __restrict old_block_delete_signs = |
1131 | 19 | get_delete_sign_column_data(old_block, old_rows); |
1132 | 19 | DCHECK(old_block_delete_signs != nullptr); |
1133 | | // build default value block |
1134 | 19 | auto default_value_block = old_block.clone_empty(); |
1135 | 19 | RETURN_IF_ERROR(BaseTablet::generate_default_value_block(*rowset_schema, missing_cids, |
1136 | 19 | partial_update_info->default_values, |
1137 | 19 | old_block, default_value_block)); |
1138 | | |
1139 | 19 | CHECK(update_rows >= old_rows); |
1140 | | |
1141 | | // build full block |
1142 | 119 | for (auto i = 0; i < missing_cids.size(); ++i) { |
1143 | 100 | const auto& rs_column = rowset_schema->column(missing_cids[i]); |
1144 | 100 | auto& mutable_column = full_mutable_columns[missing_cids[i]]; |
1145 | 560 | for (auto idx = 0; idx < update_rows; ++idx) { |
1146 | | // There are two cases we don't need to read values from old data: |
1147 | | // 1. if the conflicting new row's delete sign is marked, which means the value columns |
1148 | | // of the row will not be read. So we don't need to read the missing values from the previous rows. |
1149 | | // 2. if the conflicting old row's delete sign is marked, which means that the key is not exist now, |
1150 | | // we should not read old values from the deleted data, and should use default value instead. |
1151 | | // NOTE: since now we are in the publishing phase, all data is commited |
1152 | | // before, even the `strict_mode` is true (which requires partial update |
1153 | | // load job can't insert new keys), this "new" key MUST be written into |
1154 | | // the new generated segment file. |
1155 | 460 | bool new_row_delete_sign = |
1156 | 460 | (new_block_delete_signs != nullptr && new_block_delete_signs[idx]); |
1157 | 460 | if (new_row_delete_sign) { |
1158 | 18 | mutable_column->insert_default(); |
1159 | 442 | } else { |
1160 | 442 | bool use_default = false; |
1161 | 442 | bool old_row_delete_sign = (old_block_delete_signs != nullptr && |
1162 | 442 | old_block_delete_signs[read_index_old.at(idx)] != 0); |
1163 | 442 | if (old_row_delete_sign) { |
1164 | 0 | if (!rowset_schema->has_sequence_col()) { |
1165 | 0 | use_default = true; |
1166 | 0 | } else if (have_input_seq_column || !rs_column.is_seqeunce_col()) { |
1167 | | // to keep the sequence column value not decreasing, we should read values of seq column |
1168 | | // from old rows even if the old row is deleted when the input don't specify the sequence column, otherwise |
1169 | | // it may cause the merge-on-read based compaction to produce incorrect results |
1170 | 0 | use_default = true; |
1171 | 0 | } |
1172 | 0 | } |
1173 | | |
1174 | 442 | if (use_default) { |
1175 | 0 | if (rs_column.has_default_value()) { |
1176 | 0 | mutable_column->insert_from(*default_value_block.get_by_position(i).column, |
1177 | 0 | 0); |
1178 | 0 | } else if (rs_column.is_nullable()) { |
1179 | 0 | assert_cast<ColumnNullable*, TypeCheckOnRelease::DISABLE>( |
1180 | 0 | mutable_column.get()) |
1181 | 0 | ->insert_default(); |
1182 | 0 | } else { |
1183 | 0 | mutable_column->insert_default(); |
1184 | 0 | } |
1185 | 442 | } else { |
1186 | 442 | mutable_column->insert_from(*old_block.get_by_position(i).column, |
1187 | 442 | read_index_old[idx]); |
1188 | 442 | } |
1189 | 442 | } |
1190 | 460 | } |
1191 | 100 | } |
1192 | 19 | full_mutable_columns_guard.restore(); |
1193 | 19 | VLOG_DEBUG << "full block when publish: " << output_block->dump_data(); |
1194 | 19 | return Status::OK(); |
1195 | 19 | } |
1196 | | |
1197 | | static void fill_cell_for_flexible_partial_update( |
1198 | | std::map<uint32_t, uint32_t>& read_index_old, |
1199 | | std::map<uint32_t, uint32_t>& read_index_update, const TabletSchemaSPtr& rowset_schema, |
1200 | | const PartialUpdateInfo* partial_update_info, const TabletColumn& tablet_column, |
1201 | | std::size_t idx, MutableColumnPtr& new_col, const IColumn& default_value_col, |
1202 | | const IColumn& old_value_col, const IColumn& cur_col, bool skipped, |
1203 | 0 | bool row_has_sequence_col, const signed char* delete_sign_column_data) { |
1204 | 0 | if (skipped) { |
1205 | 0 | bool use_default = false; |
1206 | 0 | bool old_row_delete_sign = |
1207 | 0 | (delete_sign_column_data != nullptr && |
1208 | 0 | delete_sign_column_data[read_index_old[cast_set<uint32_t>(idx)]] != 0); |
1209 | 0 | if (old_row_delete_sign) { |
1210 | 0 | if (!rowset_schema->has_sequence_col()) { |
1211 | 0 | use_default = true; |
1212 | 0 | } else if (row_has_sequence_col || (!tablet_column.is_seqeunce_col() && |
1213 | 0 | (tablet_column.unique_id() != |
1214 | 0 | partial_update_info->sequence_map_col_uid()))) { |
1215 | | // to keep the sequence column value not decreasing, we should read values of seq column(and seq map column) |
1216 | | // from old rows even if the old row is deleted when the input don't specify the sequence column, otherwise |
1217 | | // it may cause the merge-on-read based compaction to produce incorrect results |
1218 | 0 | use_default = true; |
1219 | 0 | } |
1220 | 0 | } |
1221 | 0 | if (use_default) { |
1222 | 0 | if (tablet_column.has_default_value()) { |
1223 | 0 | new_col->insert_from(default_value_col, 0); |
1224 | 0 | } else if (tablet_column.is_nullable()) { |
1225 | 0 | assert_cast<ColumnNullable*, TypeCheckOnRelease::DISABLE>(new_col.get()) |
1226 | 0 | ->insert_many_defaults(1); |
1227 | 0 | } else if (tablet_column.is_auto_increment()) { |
1228 | | // For auto-increment column, its default value(generated value) is filled in current block in flush phase |
1229 | | // when the load doesn't specify the auto-increment column |
1230 | | // - if the previous conflicting row is deleted, we should use the value in current block as its final value |
1231 | | // - if the previous conflicting row is an insert, we should use the value in old block as its final value to |
1232 | | // keep consistency between replicas |
1233 | 0 | new_col->insert_from(cur_col, read_index_update[cast_set<uint32_t>(idx)]); |
1234 | 0 | } else { |
1235 | 0 | new_col->insert_default(); |
1236 | 0 | } |
1237 | 0 | } else { |
1238 | 0 | new_col->insert_from(old_value_col, read_index_old[cast_set<uint32_t>(idx)]); |
1239 | 0 | } |
1240 | 0 | } else { |
1241 | 0 | new_col->insert_from(cur_col, read_index_update[cast_set<uint32_t>(idx)]); |
1242 | 0 | } |
1243 | 0 | } |
1244 | | |
1245 | | Status BaseTablet::generate_new_block_for_flexible_partial_update( |
1246 | | TabletSchemaSPtr rowset_schema, const PartialUpdateInfo* partial_update_info, |
1247 | | std::set<uint32_t>& rids_be_overwritten, const FixedReadPlan& read_plan_ori, |
1248 | | const FixedReadPlan& read_plan_update, |
1249 | 0 | const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset, Block* output_block) { |
1250 | 0 | CHECK(output_block); |
1251 | |
|
1252 | 0 | int32_t seq_col_unique_id = -1; |
1253 | 0 | if (rowset_schema->has_sequence_col()) { |
1254 | 0 | seq_col_unique_id = rowset_schema->column(rowset_schema->sequence_col_idx()).unique_id(); |
1255 | 0 | } |
1256 | 0 | const auto& non_sort_key_cids = partial_update_info->missing_cids; |
1257 | 0 | std::vector<uint32_t> all_cids(rowset_schema->num_columns()); |
1258 | 0 | std::iota(all_cids.begin(), all_cids.end(), 0); |
1259 | 0 | auto old_block = rowset_schema->create_block_by_cids(non_sort_key_cids); |
1260 | 0 | auto update_block = rowset_schema->create_block_by_cids(all_cids); |
1261 | | |
1262 | | // rowid in the final block(start from 0, increase continuously) -> rowid to read in update_block |
1263 | 0 | std::map<uint32_t, uint32_t> read_index_update; |
1264 | | |
1265 | | // 1. read the current rowset first, if a row in the current rowset has delete sign mark |
1266 | | // we don't need to read values from old block for that row |
1267 | 0 | RETURN_IF_ERROR(read_plan_update.read_columns_by_plan(*rowset_schema, all_cids, rsid_to_rowset, |
1268 | 0 | update_block, &read_index_update, true)); |
1269 | 0 | size_t update_rows = read_index_update.size(); |
1270 | | |
1271 | | // TODO(bobhan1): add the delete sign optimazation here |
1272 | | // // if there is sequence column in the table, we need to read the sequence column, |
1273 | | // // otherwise it may cause the merge-on-read based compaction policy to produce incorrect results |
1274 | | // const auto* __restrict new_block_delete_signs = |
1275 | | // rowset_schema->has_sequence_col() |
1276 | | // ? nullptr |
1277 | | // : get_delete_sign_column_data(update_block, update_rows); |
1278 | | |
1279 | | // 2. read previous rowsets |
1280 | | // 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 |
1281 | 0 | std::map<uint32_t, uint32_t> read_index_old; |
1282 | 0 | RETURN_IF_ERROR(read_plan_ori.read_columns_by_plan( |
1283 | 0 | *rowset_schema, non_sort_key_cids, rsid_to_rowset, old_block, &read_index_old, true)); |
1284 | 0 | size_t old_rows = read_index_old.size(); |
1285 | 0 | DCHECK(update_rows == old_rows); |
1286 | 0 | const auto* __restrict old_block_delete_signs = |
1287 | 0 | get_delete_sign_column_data(old_block, old_rows); |
1288 | 0 | DCHECK(old_block_delete_signs != nullptr); |
1289 | | |
1290 | | // 3. build default value block |
1291 | 0 | auto default_value_block = old_block.clone_empty(); |
1292 | 0 | RETURN_IF_ERROR(BaseTablet::generate_default_value_block(*rowset_schema, non_sort_key_cids, |
1293 | 0 | partial_update_info->default_values, |
1294 | 0 | old_block, default_value_block)); |
1295 | | |
1296 | | // 4. build the final block |
1297 | 0 | auto full_mutable_columns_guard = output_block->mutate_columns_scoped(); |
1298 | 0 | auto& full_mutable_columns = full_mutable_columns_guard.mutable_columns(); |
1299 | 0 | DCHECK(rowset_schema->has_skip_bitmap_col()); |
1300 | 0 | auto skip_bitmap_col_idx = rowset_schema->skip_bitmap_col_idx(); |
1301 | 0 | const std::vector<BitmapValue>* skip_bitmaps = |
1302 | 0 | &(assert_cast<const ColumnBitmap*, TypeCheckOnRelease::DISABLE>( |
1303 | 0 | update_block.get_by_position(skip_bitmap_col_idx).column->get_ptr().get()) |
1304 | 0 | ->get_data()); |
1305 | |
|
1306 | 0 | if (rowset_schema->has_sequence_col() && !rids_be_overwritten.empty()) { |
1307 | | // If the row specifies the sequence column, we should delete the current row becase the |
1308 | | // flexible partial update on the current row has been `overwritten` by the previous one with larger sequence |
1309 | | // column value. |
1310 | 0 | for (auto it = rids_be_overwritten.begin(); it != rids_be_overwritten.end();) { |
1311 | 0 | auto rid = *it; |
1312 | 0 | if (!skip_bitmaps->at(rid).contains(seq_col_unique_id)) { |
1313 | 0 | ++it; |
1314 | 0 | } else { |
1315 | 0 | it = rids_be_overwritten.erase(it); |
1316 | 0 | } |
1317 | 0 | } |
1318 | 0 | } |
1319 | |
|
1320 | 0 | for (std::size_t cid {0}; cid < rowset_schema->num_columns(); cid++) { |
1321 | 0 | MutableColumnPtr& new_col = full_mutable_columns[cid]; |
1322 | 0 | const IColumn& cur_col = *update_block.get_by_position(cid).column; |
1323 | 0 | const auto& rs_column = rowset_schema->column(cid); |
1324 | 0 | auto col_uid = rs_column.unique_id(); |
1325 | 0 | for (auto idx = 0; idx < update_rows; ++idx) { |
1326 | 0 | if (cid < rowset_schema->num_key_columns()) { |
1327 | 0 | new_col->insert_from(cur_col, read_index_update[idx]); |
1328 | 0 | } else { |
1329 | 0 | const IColumn& default_value_col = |
1330 | 0 | *default_value_block.get_by_position(cid - rowset_schema->num_key_columns()) |
1331 | 0 | .column; |
1332 | 0 | const IColumn& old_value_col = |
1333 | 0 | *old_block.get_by_position(cid - rowset_schema->num_key_columns()).column; |
1334 | 0 | if (rids_be_overwritten.contains(idx)) { |
1335 | 0 | new_col->insert_from(old_value_col, read_index_old[idx]); |
1336 | 0 | } else { |
1337 | 0 | fill_cell_for_flexible_partial_update( |
1338 | 0 | read_index_old, read_index_update, rowset_schema, partial_update_info, |
1339 | 0 | rs_column, idx, new_col, default_value_col, old_value_col, cur_col, |
1340 | 0 | skip_bitmaps->at(idx).contains(col_uid), |
1341 | 0 | rowset_schema->has_sequence_col() |
1342 | 0 | ? !skip_bitmaps->at(idx).contains(seq_col_unique_id) |
1343 | 0 | : false, |
1344 | 0 | old_block_delete_signs); |
1345 | 0 | } |
1346 | 0 | } |
1347 | 0 | } |
1348 | 0 | DCHECK_EQ(full_mutable_columns[cid]->size(), update_rows); |
1349 | 0 | } |
1350 | |
|
1351 | 0 | full_mutable_columns_guard.restore(); |
1352 | 0 | VLOG_DEBUG << "full block when publish: " << output_block->dump_data(); |
1353 | 0 | return Status::OK(); |
1354 | 0 | } |
1355 | | |
1356 | | Status BaseTablet::commit_phase_update_delete_bitmap( |
1357 | | const BaseTabletSPtr& tablet, const RowsetSharedPtr& rowset, |
1358 | | RowsetIdUnorderedSet& pre_rowset_ids, DeleteBitmapPtr delete_bitmap, |
1359 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, int64_t txn_id, |
1360 | 19.4k | CalcDeleteBitmapToken* token, RowsetWriter* rowset_writer) { |
1361 | 19.4k | DBUG_EXECUTE_IF("BaseTablet::commit_phase_update_delete_bitmap.enable_spin_wait", { |
1362 | 19.4k | auto tok = dp->param<std::string>("token", "invalid_token"); |
1363 | 19.4k | while (DebugPoints::instance()->is_enable( |
1364 | 19.4k | "BaseTablet::commit_phase_update_delete_bitmap.block")) { |
1365 | 19.4k | auto block_dp = DebugPoints::instance()->get_debug_point( |
1366 | 19.4k | "BaseTablet::commit_phase_update_delete_bitmap.block"); |
1367 | 19.4k | if (block_dp) { |
1368 | 19.4k | auto pass_token = block_dp->param<std::string>("pass_token", ""); |
1369 | 19.4k | if (pass_token == tok) { |
1370 | 19.4k | break; |
1371 | 19.4k | } |
1372 | 19.4k | } |
1373 | 19.4k | std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
1374 | 19.4k | } |
1375 | 19.4k | }); |
1376 | 19.4k | SCOPED_BVAR_LATENCY(g_tablet_commit_phase_update_delete_bitmap_latency); |
1377 | 19.4k | RowsetIdUnorderedSet cur_rowset_ids; |
1378 | 19.4k | RowsetIdUnorderedSet rowset_ids_to_add; |
1379 | 19.4k | RowsetIdUnorderedSet rowset_ids_to_del; |
1380 | 19.4k | int64_t cur_version; |
1381 | | |
1382 | 19.4k | std::vector<RowsetSharedPtr> specified_rowsets; |
1383 | 19.4k | { |
1384 | | // to prevent seeing intermediate state of a tablet |
1385 | 19.4k | std::unique_lock<bthread::Mutex> sync_lock; |
1386 | 19.4k | if (config::is_cloud_mode()) { |
1387 | 19.3k | sync_lock = std::unique_lock<bthread::Mutex>( |
1388 | 19.3k | std::static_pointer_cast<CloudTablet>(tablet)->get_sync_meta_lock()); |
1389 | 19.3k | } |
1390 | 19.4k | std::shared_lock meta_rlock(tablet->_meta_lock); |
1391 | 19.4k | if (tablet->tablet_state() == TABLET_NOTREADY) { |
1392 | | // tablet is under alter process. The delete bitmap will be calculated after conversion. |
1393 | 18 | LOG(INFO) << "tablet is under alter process, delete bitmap will be calculated later, " |
1394 | 18 | "tablet_id: " |
1395 | 18 | << tablet->tablet_id() << " txn_id: " << txn_id; |
1396 | 18 | return Status::OK(); |
1397 | 18 | } |
1398 | 19.3k | cur_version = tablet->max_version_unlocked(); |
1399 | 19.3k | RETURN_IF_ERROR(tablet->get_all_rs_id_unlocked(cur_version, &cur_rowset_ids)); |
1400 | 19.3k | _rowset_ids_difference(cur_rowset_ids, pre_rowset_ids, &rowset_ids_to_add, |
1401 | 19.3k | &rowset_ids_to_del); |
1402 | 19.3k | specified_rowsets = tablet->get_rowset_by_ids(&rowset_ids_to_add); |
1403 | 19.3k | } |
1404 | 10.1k | for (const auto& to_del : rowset_ids_to_del) { |
1405 | 10.1k | delete_bitmap->remove({to_del, 0, 0}, {to_del, UINT32_MAX, INT64_MAX}); |
1406 | 10.1k | } |
1407 | | |
1408 | 19.3k | RETURN_IF_ERROR(calc_delete_bitmap(tablet, rowset, segments, specified_rowsets, delete_bitmap, |
1409 | 19.3k | cur_version, token, rowset_writer)); |
1410 | 19.3k | size_t total_rows = std::accumulate( |
1411 | 19.3k | segments.begin(), segments.end(), 0, |
1412 | 19.3k | [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); }); |
1413 | 19.3k | LOG(INFO) << "[Before Commit] construct delete bitmap tablet: " << tablet->tablet_id() |
1414 | 19.3k | << ", rowset_ids to add: " << rowset_ids_to_add.size() |
1415 | 19.3k | << ", rowset_ids to del: " << rowset_ids_to_del.size() |
1416 | 19.3k | << ", cur max_version: " << cur_version << ", transaction_id: " << txn_id |
1417 | 19.3k | << ", total rows: " << total_rows; |
1418 | 19.3k | pre_rowset_ids = cur_rowset_ids; |
1419 | 19.3k | return Status::OK(); |
1420 | 19.3k | } |
1421 | | |
1422 | | void BaseTablet::add_sentinel_mark_to_delete_bitmap(DeleteBitmap* delete_bitmap, |
1423 | 22.9k | const RowsetIdUnorderedSet& rowsetids) { |
1424 | 225k | for (const auto& rowsetid : rowsetids) { |
1425 | 225k | delete_bitmap->add( |
1426 | 225k | {rowsetid, DeleteBitmap::INVALID_SEGMENT_ID, DeleteBitmap::TEMP_VERSION_COMMON}, |
1427 | 225k | DeleteBitmap::ROWSET_SENTINEL_MARK); |
1428 | 225k | } |
1429 | 22.9k | } |
1430 | | |
1431 | | void BaseTablet::_rowset_ids_difference(const RowsetIdUnorderedSet& cur, |
1432 | | const RowsetIdUnorderedSet& pre, |
1433 | | RowsetIdUnorderedSet* to_add, |
1434 | 69.2k | RowsetIdUnorderedSet* to_del) { |
1435 | 658k | for (const auto& id : cur) { |
1436 | 658k | if (pre.find(id) == pre.end()) { |
1437 | 27.0k | to_add->insert(id); |
1438 | 27.0k | } |
1439 | 658k | } |
1440 | 661k | for (const auto& id : pre) { |
1441 | 661k | if (cur.find(id) == cur.end()) { |
1442 | 28.3k | to_del->insert(id); |
1443 | 28.3k | } |
1444 | 661k | } |
1445 | 69.2k | } |
1446 | | |
1447 | | Status BaseTablet::check_delete_bitmap_correctness(DeleteBitmapPtr delete_bitmap, |
1448 | | int64_t max_version, int64_t txn_id, |
1449 | | const RowsetIdUnorderedSet& rowset_ids, |
1450 | 41.5k | std::vector<RowsetSharedPtr>* rowsets) { |
1451 | 41.5k | RowsetIdUnorderedSet missing_ids; |
1452 | 434k | for (const auto& rowsetid : rowset_ids) { |
1453 | 434k | if (!delete_bitmap->delete_bitmap.contains({rowsetid, DeleteBitmap::INVALID_SEGMENT_ID, |
1454 | 434k | DeleteBitmap::TEMP_VERSION_COMMON})) { |
1455 | 0 | missing_ids.insert(rowsetid); |
1456 | 0 | } |
1457 | 434k | } |
1458 | | |
1459 | 41.5k | if (!missing_ids.empty()) { |
1460 | 0 | LOG(WARNING) << "[txn_id:" << txn_id << "][tablet_id:" << tablet_id() |
1461 | 0 | << "][max_version: " << max_version |
1462 | 0 | << "] check delete bitmap correctness failed!"; |
1463 | 0 | rapidjson::Document root; |
1464 | 0 | root.SetObject(); |
1465 | 0 | rapidjson::Document required_rowsets_arr; |
1466 | 0 | required_rowsets_arr.SetArray(); |
1467 | 0 | rapidjson::Document missing_rowsets_arr; |
1468 | 0 | missing_rowsets_arr.SetArray(); |
1469 | |
|
1470 | 0 | if (rowsets != nullptr) { |
1471 | 0 | for (const auto& rowset : *rowsets) { |
1472 | 0 | rapidjson::Value value; |
1473 | 0 | std::string version_str = rowset->get_rowset_info_str(); |
1474 | 0 | value.SetString(version_str.c_str(), |
1475 | 0 | cast_set<rapidjson::SizeType>(version_str.length()), |
1476 | 0 | required_rowsets_arr.GetAllocator()); |
1477 | 0 | required_rowsets_arr.PushBack(value, required_rowsets_arr.GetAllocator()); |
1478 | 0 | } |
1479 | 0 | } else { |
1480 | 0 | std::vector<RowsetSharedPtr> tablet_rowsets; |
1481 | 0 | { |
1482 | 0 | std::shared_lock meta_rlock(_meta_lock); |
1483 | 0 | tablet_rowsets = get_rowset_by_ids(&rowset_ids); |
1484 | 0 | } |
1485 | 0 | for (const auto& rowset : tablet_rowsets) { |
1486 | 0 | rapidjson::Value value; |
1487 | 0 | std::string version_str = rowset->get_rowset_info_str(); |
1488 | 0 | value.SetString(version_str.c_str(), |
1489 | 0 | cast_set<rapidjson::SizeType>(version_str.length()), |
1490 | 0 | required_rowsets_arr.GetAllocator()); |
1491 | 0 | required_rowsets_arr.PushBack(value, required_rowsets_arr.GetAllocator()); |
1492 | 0 | } |
1493 | 0 | } |
1494 | 0 | for (const auto& missing_rowset_id : missing_ids) { |
1495 | 0 | rapidjson::Value miss_value; |
1496 | 0 | std::string rowset_id_str = missing_rowset_id.to_string(); |
1497 | 0 | miss_value.SetString(rowset_id_str.c_str(), |
1498 | 0 | cast_set<rapidjson::SizeType>(rowset_id_str.length()), |
1499 | 0 | missing_rowsets_arr.GetAllocator()); |
1500 | 0 | missing_rowsets_arr.PushBack(miss_value, missing_rowsets_arr.GetAllocator()); |
1501 | 0 | } |
1502 | |
|
1503 | 0 | root.AddMember("required_rowsets", required_rowsets_arr, root.GetAllocator()); |
1504 | 0 | root.AddMember("missing_rowsets", missing_rowsets_arr, root.GetAllocator()); |
1505 | 0 | rapidjson::StringBuffer strbuf; |
1506 | 0 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf); |
1507 | 0 | root.Accept(writer); |
1508 | 0 | std::string rowset_status_string = std::string(strbuf.GetString()); |
1509 | 0 | LOG_EVERY_SECOND(WARNING) << rowset_status_string; |
1510 | | // let it crash if correctness check failed in Debug mode |
1511 | 0 | DCHECK(false) << "delete bitmap correctness check failed in publish phase!"; |
1512 | 0 | return Status::InternalError("check delete bitmap failed!"); |
1513 | 0 | } |
1514 | 41.5k | return Status::OK(); |
1515 | 41.5k | } |
1516 | | |
1517 | | Status BaseTablet::update_delete_bitmap(const BaseTabletSPtr& self, TabletTxnInfo* txn_info, |
1518 | | int64_t txn_id, int64_t txn_expiration, |
1519 | 50.7k | DeleteBitmapPtr tablet_delete_bitmap) { |
1520 | 50.7k | SCOPED_BVAR_LATENCY(g_tablet_update_delete_bitmap_latency); |
1521 | 50.7k | RowsetIdUnorderedSet cur_rowset_ids; |
1522 | 50.7k | RowsetIdUnorderedSet rowset_ids_to_add; |
1523 | 50.7k | RowsetIdUnorderedSet rowset_ids_to_del; |
1524 | 50.7k | RowsetSharedPtr rowset = txn_info->rowset; |
1525 | 50.7k | RowsetSharedPtr row_binlog_rowset; |
1526 | 50.7k | bool build_row_binlog = false; |
1527 | 50.7k | int64_t cur_version = rowset->start_version(); |
1528 | 50.7k | std::unique_ptr<RowsetWriter> transient_rs_writer; |
1529 | 50.7k | DeleteBitmapPtr delete_bitmap = txn_info->delete_bitmap; |
1530 | 50.7k | bool is_partial_update = |
1531 | 51.0k | txn_info->partial_update_info && txn_info->partial_update_info->is_partial_update(); |
1532 | 50.7k | for (const auto& rs : txn_info->attach_rowsets) { |
1533 | 0 | if (rs != nullptr && rs->rowset_meta() != nullptr && rs->rowset_meta()->is_row_binlog()) { |
1534 | 0 | row_binlog_rowset = rs; |
1535 | 0 | build_row_binlog = is_partial_update || |
1536 | 0 | self->tablet_meta()->binlog_config().need_historical_value(); |
1537 | 0 | break; |
1538 | 0 | } |
1539 | 0 | } |
1540 | | |
1541 | | // rewrite conflict only when partial update or need before |
1542 | 50.7k | if (is_partial_update || build_row_binlog) { |
1543 | 3.24k | if (txn_info->partial_update_info == nullptr) { |
1544 | 0 | txn_info->partial_update_info = std::make_shared<PartialUpdateInfo>(); |
1545 | 0 | } |
1546 | 3.24k | if (txn_info->partial_update_info->partial_update_mode == UniqueKeyUpdateModePB::UPSERT) { |
1547 | 0 | txn_info->partial_update_info->partial_update_input_columns.clear(); |
1548 | 0 | txn_info->partial_update_info->missing_cids.clear(); |
1549 | 0 | txn_info->partial_update_info->default_values.clear(); |
1550 | 0 | auto& update_cids = txn_info->partial_update_info->update_cids; |
1551 | 0 | update_cids.resize(rowset->tablet_schema()->num_columns()); |
1552 | 0 | std::iota(update_cids.begin(), update_cids.end(), 0); |
1553 | 0 | } |
1554 | | |
1555 | 3.24k | DCHECK(txn_info->partial_update_info != nullptr); |
1556 | | |
1557 | 3.24k | transient_rs_writer = DORIS_TRY(self->create_transient_rowset_writer( |
1558 | 3.24k | *rowset, txn_info->partial_update_info, txn_expiration)); |
1559 | 3.24k | DBUG_EXECUTE_IF("BaseTablet::update_delete_bitmap.after.create_transient_rs_writer", |
1560 | 3.24k | DBUG_BLOCK); |
1561 | | // Partial update or upsert rewrite might generate new segments when there is conflicts while publish, and mark |
1562 | | // the same key in original segments as delete. |
1563 | | // When the new segment flush fails or the rowset build fails, the deletion marker for the |
1564 | | // duplicate key of the original segment should not remain in `txn_info->delete_bitmap`, |
1565 | | // so we need to make a copy of `txn_info->delete_bitmap` and make changes on it. |
1566 | 3.24k | delete_bitmap = std::make_shared<DeleteBitmap>(*(txn_info->delete_bitmap)); |
1567 | 3.24k | } |
1568 | | |
1569 | 50.7k | OlapStopWatch watch; |
1570 | 50.7k | std::vector<segment_v2::SegmentSharedPtr> segments; |
1571 | 50.7k | RETURN_IF_ERROR(std::dynamic_pointer_cast<BetaRowset>(rowset)->load_segments(&segments)); |
1572 | 50.7k | auto t1 = watch.get_elapse_time_us(); |
1573 | | |
1574 | 50.7k | int64_t next_visible_version = txn_info->is_txn_load ? txn_info->next_visible_version |
1575 | 50.7k | : txn_info->rowset->start_version(); |
1576 | 50.7k | { |
1577 | 50.7k | std::shared_lock meta_rlock(self->_meta_lock); |
1578 | | // tablet is under alter process. The delete bitmap will be calculated after conversion. |
1579 | 50.7k | if (self->tablet_state() == TABLET_NOTREADY) { |
1580 | 3 | LOG(INFO) << "tablet is under alter process, update delete bitmap later, tablet_id=" |
1581 | 3 | << self->tablet_id(); |
1582 | 3 | return Status::OK(); |
1583 | 3 | } |
1584 | 50.7k | RETURN_IF_ERROR(self->get_all_rs_id_unlocked(next_visible_version - 1, &cur_rowset_ids)); |
1585 | 50.7k | } |
1586 | 50.7k | auto t2 = watch.get_elapse_time_us(); |
1587 | | |
1588 | 50.7k | _rowset_ids_difference(cur_rowset_ids, txn_info->rowset_ids, &rowset_ids_to_add, |
1589 | 50.7k | &rowset_ids_to_del); |
1590 | 50.7k | for (const auto& to_del : rowset_ids_to_del) { |
1591 | 18.1k | delete_bitmap->remove({to_del, 0, 0}, {to_del, UINT32_MAX, INT64_MAX}); |
1592 | 18.1k | } |
1593 | | |
1594 | 50.7k | std::vector<RowsetSharedPtr> specified_rowsets; |
1595 | 50.7k | { |
1596 | 50.7k | std::shared_lock meta_rlock(self->_meta_lock); |
1597 | 50.7k | specified_rowsets = self->get_rowset_by_ids(&rowset_ids_to_add); |
1598 | 50.7k | } |
1599 | 50.7k | if (txn_info->is_txn_load) { |
1600 | 156 | for (auto invisible_rowset : txn_info->invisible_rowsets) { |
1601 | 38 | specified_rowsets.emplace_back(invisible_rowset); |
1602 | 38 | } |
1603 | 156 | std::sort(specified_rowsets.begin(), specified_rowsets.end(), |
1604 | 454 | [](RowsetSharedPtr& lhs, RowsetSharedPtr& rhs) { |
1605 | 454 | return lhs->end_version() > rhs->end_version(); |
1606 | 454 | }); |
1607 | 156 | } |
1608 | 50.7k | auto t3 = watch.get_elapse_time_us(); |
1609 | | |
1610 | | // If a rowset is produced by compaction before the commit phase of the partial update load |
1611 | | // and is not included in txn_info->rowset_ids, we can skip the alignment process of that rowset |
1612 | | // because data remains the same before and after compaction. But we still need to calculate the |
1613 | | // the delete bitmap for that rowset. |
1614 | 50.7k | std::vector<RowsetSharedPtr> rowsets_skip_alignment; |
1615 | 50.7k | if (is_partial_update) { |
1616 | 3.31k | int64_t max_version_in_flush_phase = |
1617 | 3.31k | txn_info->partial_update_info->max_version_in_flush_phase; |
1618 | 3.31k | DCHECK(max_version_in_flush_phase != -1); |
1619 | 3.31k | std::vector<RowsetSharedPtr> remained_rowsets; |
1620 | 3.31k | for (const auto& specified_rowset : specified_rowsets) { |
1621 | 305 | if (specified_rowset->end_version() <= max_version_in_flush_phase && |
1622 | 305 | specified_rowset->produced_by_compaction()) { |
1623 | 49 | rowsets_skip_alignment.emplace_back(specified_rowset); |
1624 | 256 | } else { |
1625 | 256 | remained_rowsets.emplace_back(specified_rowset); |
1626 | 256 | } |
1627 | 305 | } |
1628 | 3.31k | if (!rowsets_skip_alignment.empty()) { |
1629 | 49 | specified_rowsets = std::move(remained_rowsets); |
1630 | 49 | } |
1631 | 3.31k | } |
1632 | | |
1633 | 50.7k | DBUG_EXECUTE_IF("BaseTablet::update_delete_bitmap.enable_spin_wait", { |
1634 | 50.7k | auto token = dp->param<std::string>("token", "invalid_token"); |
1635 | 50.7k | while (DebugPoints::instance()->is_enable("BaseTablet::update_delete_bitmap.block")) { |
1636 | 50.7k | auto block_dp = DebugPoints::instance()->get_debug_point( |
1637 | 50.7k | "BaseTablet::update_delete_bitmap.block"); |
1638 | 50.7k | if (block_dp) { |
1639 | 50.7k | auto wait_token = block_dp->param<std::string>("wait_token", ""); |
1640 | 50.7k | LOG(INFO) << "BaseTablet::update_delete_bitmap.enable_spin_wait, wait_token: " |
1641 | 50.7k | << wait_token << ", token: " << token; |
1642 | 50.7k | if (wait_token != token) { |
1643 | 50.7k | break; |
1644 | 50.7k | } |
1645 | 50.7k | } |
1646 | 50.7k | std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
1647 | 50.7k | } |
1648 | 50.7k | }); |
1649 | | |
1650 | 50.7k | if (!rowsets_skip_alignment.empty()) { |
1651 | 48 | auto token = self->calc_delete_bitmap_executor()->create_token(); |
1652 | | // set rowset_writer to nullptr to skip the alignment process |
1653 | 48 | RETURN_IF_ERROR(calc_delete_bitmap(self, rowset, segments, rowsets_skip_alignment, |
1654 | 48 | delete_bitmap, cur_version - 1, token.get(), nullptr, |
1655 | 48 | tablet_delete_bitmap)); |
1656 | 48 | RETURN_IF_ERROR(token->wait()); |
1657 | 48 | } |
1658 | | |
1659 | | // Publish-phase partial update or upsert rewrite may generate transient segments. If row binlog is enabled for |
1660 | | // this txn (row binlog rowset attached), we need to build row binlog segments together with |
1661 | | // transient data segments: |
1662 | | // 1) create a transient row binlog writer that appends to the same version row binlog rowset |
1663 | | // 2) pre-allocate per-row LSNs for each transient segment and register them in binlog options |
1664 | | // 3) wrap both writers into a GroupRowsetWriter so calc_delete_bitmap writes to both. |
1665 | 50.7k | if (build_row_binlog) { |
1666 | 0 | DCHECK(transient_rs_writer != nullptr); |
1667 | | |
1668 | | // Create transient row binlog writer for publish-phase segment appending. |
1669 | 0 | auto transient_row_binlog_writer = DORIS_TRY(self->create_transient_rowset_writer( |
1670 | 0 | *row_binlog_rowset, txn_info->partial_update_info, txn_expiration)); |
1671 | | |
1672 | | // Prepare source MOW context for historical row retrieval in binlog writer. |
1673 | 0 | auto& data_ctx = const_cast<RowsetWriterContext&>(transient_rs_writer->context()); |
1674 | 0 | data_ctx.mow_context = std::make_shared<MowContext>( |
1675 | 0 | cur_version - 1, txn_id, std::make_shared<RowsetIdUnorderedSet>(), |
1676 | 0 | specified_rowsets, nullptr); |
1677 | |
|
1678 | 0 | auto& binlog_ctx = const_cast<RowsetWriterContext&>(transient_row_binlog_writer->context()); |
1679 | 0 | auto& cfg = binlog_ctx.write_binlog_opt().write_binlog_config(); |
1680 | 0 | cfg.source.tablet_schema = data_ctx.tablet_schema; |
1681 | 0 | cfg.source.partial_update_info = data_ctx.partial_update_info; |
1682 | 0 | cfg.source.mow_context = data_ctx.mow_context; |
1683 | 0 | cfg.source.is_transient_rowset_writer = data_ctx.is_transient_rowset_writer; |
1684 | 0 | cfg.source.source_write_type = data_ctx.write_type; |
1685 | 0 | cfg.source.row_binlog_rowset = row_binlog_rowset; |
1686 | | |
1687 | | // Wrap two transient writers into a group writer for dual flush/build. |
1688 | 0 | RowsetWriterSharedPtr data_writer_sp(std::move(transient_rs_writer)); |
1689 | 0 | RowsetWriterSharedPtr row_binlog_writer_sp(std::move(transient_row_binlog_writer)); |
1690 | 0 | std::unique_ptr<GroupRowsetWriter> group_writer; |
1691 | 0 | RETURN_IF_ERROR(RowsetFactory::create_empty_group_rowset_writer(&group_writer)); |
1692 | 0 | group_writer->set_data_writer(data_writer_sp); |
1693 | 0 | group_writer->set_row_binlog_writer(row_binlog_writer_sp); |
1694 | 0 | transient_rs_writer = std::move(group_writer); |
1695 | 0 | } |
1696 | | |
1697 | | // When there is only one segment, it will be calculated in the current thread. |
1698 | | // Otherwise, it will be submitted to the thread pool for calculation. |
1699 | 50.7k | if (segments.size() <= 1) { |
1700 | 50.1k | RETURN_IF_ERROR(calc_delete_bitmap(self, rowset, segments, specified_rowsets, delete_bitmap, |
1701 | 50.1k | cur_version - 1, nullptr, transient_rs_writer.get(), |
1702 | 50.1k | tablet_delete_bitmap)); |
1703 | | |
1704 | 50.1k | } else { |
1705 | 621 | auto token = self->calc_delete_bitmap_executor()->create_token(); |
1706 | 621 | RETURN_IF_ERROR(calc_delete_bitmap(self, rowset, segments, specified_rowsets, delete_bitmap, |
1707 | 621 | cur_version - 1, token.get(), transient_rs_writer.get(), |
1708 | 621 | tablet_delete_bitmap)); |
1709 | 621 | RETURN_IF_ERROR(token->wait()); |
1710 | 621 | } |
1711 | | |
1712 | 50.7k | std::stringstream ss; |
1713 | 50.7k | ss << "cost(us): (load segments: " << t1 << ", get all rsid: " << t2 - t1 |
1714 | 50.7k | << ", get rowsets: " << t3 - t2 |
1715 | 50.7k | << ", calc delete bitmap: " << watch.get_elapse_time_us() - t3 << ")"; |
1716 | | |
1717 | 50.7k | if (config::enable_merge_on_write_correctness_check && rowset->num_rows() != 0) { |
1718 | | // only do correctness check if the rowset has at least one row written |
1719 | | // check if all the rowset has ROWSET_SENTINEL_MARK |
1720 | 20.6k | auto st = self->check_delete_bitmap_correctness(delete_bitmap, cur_version - 1, -1, |
1721 | 20.6k | cur_rowset_ids, &specified_rowsets); |
1722 | 20.6k | if (!st.ok()) { |
1723 | 0 | LOG(WARNING) << fmt::format("delete bitmap correctness check failed in publish phase!"); |
1724 | 0 | } |
1725 | 20.6k | } |
1726 | | |
1727 | 50.7k | if (transient_rs_writer) { |
1728 | 3.31k | auto t4 = watch.get_elapse_time_us(); |
1729 | 3.31k | DBUG_EXECUTE_IF("Tablet.update_delete_bitmap.partial_update_write_rowset_fail", { |
1730 | 3.31k | if (rand() % 100 < (100 * dp->param("percent", 0.5))) { |
1731 | 3.31k | LOG_WARNING("Tablet.update_delete_bitmap.partial_update_write_rowset random failed") |
1732 | 3.31k | .tag("txn_id", txn_id); |
1733 | 3.31k | return Status::InternalError( |
1734 | 3.31k | "debug update_delete_bitmap partial update write rowset random failed"); |
1735 | 3.31k | } |
1736 | 3.31k | }); |
1737 | | // build rowset writer and merge transient rowset |
1738 | 3.31k | RETURN_IF_ERROR(transient_rs_writer->flush()); |
1739 | 3.31k | RowsetSharedPtr transient_rowset; |
1740 | 3.31k | RowsetSharedPtr transient_row_binlog; |
1741 | 3.31k | if (build_row_binlog) { |
1742 | 0 | auto* group_rowset_writer = typeid_cast<GroupRowsetWriter*>(transient_rs_writer.get()); |
1743 | 0 | DCHECK(group_rowset_writer != nullptr); |
1744 | 0 | std::vector<RowsetSharedPtr> waited_build_rowsets; |
1745 | 0 | RETURN_IF_ERROR(group_rowset_writer->build_rowsets(waited_build_rowsets)); |
1746 | 0 | transient_rowset = waited_build_rowsets.at(0); |
1747 | 0 | transient_row_binlog = waited_build_rowsets.at(1); |
1748 | 3.31k | } else { |
1749 | 3.31k | RETURN_IF_ERROR(transient_rs_writer->build(transient_rowset)); |
1750 | 3.31k | } |
1751 | 3.31k | auto old_segments = rowset->num_segments(); |
1752 | 3.31k | rowset->merge_rowset_meta(*transient_rowset->rowset_meta()); |
1753 | 3.31k | auto new_segments = rowset->num_segments(); |
1754 | 3.31k | ss << ", " << txn_info->partial_update_info->partial_update_mode_str() |
1755 | 3.31k | << " flush rowset (old segment num: " << old_segments |
1756 | 3.31k | << ", new segment num: " << new_segments << ")" |
1757 | 3.31k | << ", cost:" << watch.get_elapse_time_us() - t4 << "(us)"; |
1758 | | |
1759 | 3.31k | if (build_row_binlog) { |
1760 | 0 | DCHECK(row_binlog_rowset != nullptr); |
1761 | 0 | old_segments = row_binlog_rowset->num_segments(); |
1762 | 0 | row_binlog_rowset->merge_rowset_meta(*transient_row_binlog->rowset_meta()); |
1763 | 0 | new_segments = row_binlog_rowset->num_segments(); |
1764 | 0 | ss << ", " << txn_info->partial_update_info->partial_update_mode_str() |
1765 | 0 | << " flush binlog<row> (old segment num: " << old_segments |
1766 | 0 | << ", new segment num: " << new_segments << ")"; |
1767 | |
|
1768 | 0 | SegmentLoader::instance()->erase_segments(row_binlog_rowset->rowset_id(), |
1769 | 0 | row_binlog_rowset->num_segments()); |
1770 | 0 | } |
1771 | | |
1772 | | // update the shared_ptr to new bitmap, which is consistent with current rowset. |
1773 | 3.31k | txn_info->delete_bitmap = delete_bitmap; |
1774 | | // erase segment cache cause we will add a segment to rowset |
1775 | 3.31k | SegmentLoader::instance()->erase_segments(rowset->rowset_id(), rowset->num_segments()); |
1776 | 3.31k | } |
1777 | | |
1778 | 50.7k | size_t total_rows = std::accumulate( |
1779 | 50.7k | segments.begin(), segments.end(), 0, |
1780 | 50.7k | [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); }); |
1781 | 50.7k | auto t5 = watch.get_elapse_time_us(); |
1782 | 50.7k | int64_t lock_id = txn_info->is_txn_load ? txn_info->lock_id : -1; |
1783 | 50.7k | RETURN_IF_ERROR(self->save_delete_bitmap(txn_info, txn_id, delete_bitmap, |
1784 | 50.7k | transient_rs_writer.get(), cur_rowset_ids, lock_id, |
1785 | 50.7k | next_visible_version)); |
1786 | | |
1787 | | // defensive check, check that the delete bitmap cache we wrote is correct |
1788 | 50.7k | RETURN_IF_ERROR(self->check_delete_bitmap_cache(txn_id, delete_bitmap.get())); |
1789 | | |
1790 | 50.7k | LOG(INFO) << "[Publish] construct delete bitmap tablet: " << self->tablet_id() |
1791 | 50.7k | << ", rowset_ids to add: " |
1792 | 50.7k | << (specified_rowsets.size() + rowsets_skip_alignment.size()) |
1793 | 50.7k | << ", rowset_ids to del: " << rowset_ids_to_del.size() |
1794 | 50.7k | << ", cur version: " << cur_version << ", transaction_id: " << txn_id << "," |
1795 | 50.7k | << ss.str() << " , total rows: " << total_rows |
1796 | 50.7k | << ", update delete_bitmap cost: " << watch.get_elapse_time_us() - t5 << "(us)"; |
1797 | 50.7k | return Status::OK(); |
1798 | 50.7k | } |
1799 | | |
1800 | | void BaseTablet::calc_compaction_output_rowset_delete_bitmap( |
1801 | | const std::vector<RowsetSharedPtr>& input_rowsets, const RowIdConversion& rowid_conversion, |
1802 | | uint64_t start_version, uint64_t end_version, std::set<RowLocation>* missed_rows, |
1803 | | std::map<RowsetSharedPtr, std::list<std::pair<RowLocation, RowLocation>>>* location_map, |
1804 | 6.25k | const DeleteBitmap& input_delete_bitmap, DeleteBitmap* output_rowset_delete_bitmap) { |
1805 | 6.25k | RowLocation src; |
1806 | 6.25k | RowLocation dst; |
1807 | 61.1k | for (auto& rowset : input_rowsets) { |
1808 | 61.1k | src.rowset_id = rowset->rowset_id(); |
1809 | 88.3k | for (uint32_t seg_id = 0; seg_id < rowset->num_segments(); ++seg_id) { |
1810 | 27.2k | src.segment_id = seg_id; |
1811 | 27.2k | DeleteBitmap subset_map(tablet_id()); |
1812 | 27.2k | input_delete_bitmap.subset({rowset->rowset_id(), seg_id, start_version}, |
1813 | 27.2k | {rowset->rowset_id(), seg_id, end_version}, &subset_map); |
1814 | | // traverse all versions and convert rowid |
1815 | 27.2k | for (auto iter = subset_map.delete_bitmap.begin(); |
1816 | 33.3k | iter != subset_map.delete_bitmap.end(); ++iter) { |
1817 | 6.07k | auto cur_version = std::get<2>(iter->first); |
1818 | 2.79M | for (auto index = iter->second.begin(); index != iter->second.end(); ++index) { |
1819 | 2.79M | src.row_id = *index; |
1820 | 2.79M | if (rowid_conversion.get(src, &dst) != 0) { |
1821 | 2.48M | VLOG_CRITICAL << "Can't find rowid, may be deleted by the delete_handler, " |
1822 | 1 | << " src loaction: |" << src.rowset_id << "|" |
1823 | 1 | << src.segment_id << "|" << src.row_id |
1824 | 1 | << " version: " << cur_version; |
1825 | 2.48M | if (missed_rows) { |
1826 | 99 | missed_rows->insert(src); |
1827 | 99 | } |
1828 | 2.48M | continue; |
1829 | 2.48M | } |
1830 | 18.4E | VLOG_DEBUG << "calc_compaction_output_rowset_delete_bitmap dst location: |" |
1831 | 18.4E | << dst.rowset_id << "|" << dst.segment_id << "|" << dst.row_id |
1832 | 18.4E | << " src location: |" << src.rowset_id << "|" << src.segment_id |
1833 | 18.4E | << "|" << src.row_id << " start version: " << start_version |
1834 | 18.4E | << "end version" << end_version; |
1835 | 301k | if (location_map) { |
1836 | 0 | (*location_map)[rowset].emplace_back(src, dst); |
1837 | 0 | } |
1838 | 301k | output_rowset_delete_bitmap->add({dst.rowset_id, dst.segment_id, cur_version}, |
1839 | 301k | dst.row_id); |
1840 | 301k | } |
1841 | 6.07k | } |
1842 | 27.2k | } |
1843 | 61.1k | } |
1844 | 6.25k | } |
1845 | | |
1846 | | Status BaseTablet::check_rowid_conversion( |
1847 | | RowsetSharedPtr dst_rowset, |
1848 | | const std::map<RowsetSharedPtr, std::list<std::pair<RowLocation, RowLocation>>>& |
1849 | 0 | location_map) { |
1850 | 0 | if (location_map.empty()) { |
1851 | 0 | VLOG_DEBUG << "check_rowid_conversion, location_map is empty"; |
1852 | 0 | return Status::OK(); |
1853 | 0 | } |
1854 | 0 | std::vector<segment_v2::SegmentSharedPtr> dst_segments; |
1855 | |
|
1856 | 0 | RETURN_IF_ERROR( |
1857 | 0 | std::dynamic_pointer_cast<BetaRowset>(dst_rowset)->load_segments(&dst_segments)); |
1858 | 0 | std::unordered_map<RowsetId, std::vector<segment_v2::SegmentSharedPtr>> input_rowsets_segment; |
1859 | |
|
1860 | 0 | VLOG_DEBUG << "check_rowid_conversion, dst_segments size: " << dst_segments.size(); |
1861 | 0 | for (auto [src_rowset, locations] : location_map) { |
1862 | 0 | std::vector<segment_v2::SegmentSharedPtr>& segments = |
1863 | 0 | input_rowsets_segment[src_rowset->rowset_id()]; |
1864 | 0 | if (segments.empty()) { |
1865 | 0 | RETURN_IF_ERROR( |
1866 | 0 | std::dynamic_pointer_cast<BetaRowset>(src_rowset)->load_segments(&segments)); |
1867 | 0 | } |
1868 | 0 | for (auto& [src, dst] : locations) { |
1869 | 0 | std::string src_key; |
1870 | 0 | std::string dst_key; |
1871 | 0 | Status s = segments[src.segment_id]->read_key_by_rowid(src.row_id, &src_key); |
1872 | 0 | if (UNLIKELY(s.is<NOT_IMPLEMENTED_ERROR>())) { |
1873 | 0 | LOG(INFO) << "primary key index of old version does not " |
1874 | 0 | "support reading key by rowid"; |
1875 | 0 | break; |
1876 | 0 | } |
1877 | 0 | if (UNLIKELY(!s)) { |
1878 | 0 | LOG(WARNING) << "failed to get src key: |" << src.rowset_id << "|" << src.segment_id |
1879 | 0 | << "|" << src.row_id << " status: " << s; |
1880 | 0 | DCHECK(false); |
1881 | 0 | return s; |
1882 | 0 | } |
1883 | | |
1884 | 0 | s = dst_segments[dst.segment_id]->read_key_by_rowid(dst.row_id, &dst_key); |
1885 | 0 | if (UNLIKELY(!s)) { |
1886 | 0 | LOG(WARNING) << "failed to get dst key: |" << dst.rowset_id << "|" << dst.segment_id |
1887 | 0 | << "|" << dst.row_id << " status: " << s; |
1888 | 0 | DCHECK(false); |
1889 | 0 | return s; |
1890 | 0 | } |
1891 | | |
1892 | 0 | VLOG_DEBUG << "check_rowid_conversion, src: |" << src.rowset_id << "|" << src.segment_id |
1893 | 0 | << "|" << src.row_id << "|" << src_key << " dst: |" << dst.rowset_id << "|" |
1894 | 0 | << dst.segment_id << "|" << dst.row_id << "|" << dst_key; |
1895 | 0 | if (UNLIKELY(src_key.compare(dst_key) != 0)) { |
1896 | 0 | LOG(WARNING) << "failed to check key, src key: |" << src.rowset_id << "|" |
1897 | 0 | << src.segment_id << "|" << src.row_id << "|" << src_key |
1898 | 0 | << " dst key: |" << dst.rowset_id << "|" << dst.segment_id << "|" |
1899 | 0 | << dst.row_id << "|" << dst_key; |
1900 | 0 | DCHECK(false); |
1901 | 0 | return Status::InternalError("failed to check rowid conversion"); |
1902 | 0 | } |
1903 | 0 | } |
1904 | 0 | } |
1905 | 0 | return Status::OK(); |
1906 | 0 | } |
1907 | | |
1908 | | // The caller should hold _rowset_update_lock and _meta_lock lock. |
1909 | | Status BaseTablet::update_delete_bitmap_without_lock( |
1910 | | const BaseTabletSPtr& self, const RowsetSharedPtr& rowset, |
1911 | 8 | const std::vector<RowsetSharedPtr>* specified_base_rowsets) { |
1912 | 8 | DBUG_EXECUTE_IF("BaseTablet.update_delete_bitmap_without_lock.random_failed", { |
1913 | 8 | auto rnd = rand() % 100; |
1914 | 8 | auto percent = dp->param("percent", 0.1); |
1915 | 8 | if (rnd < (100 * percent)) { |
1916 | 8 | LOG(WARNING) << "BaseTablet.update_delete_bitmap_without_lock.random_failed"; |
1917 | 8 | return Status::InternalError( |
1918 | 8 | "debug tablet update delete bitmap without lock random failed"); |
1919 | 8 | } else { |
1920 | 8 | LOG(INFO) << "BaseTablet.update_delete_bitmap_without_lock.random_failed not " |
1921 | 8 | "triggered" |
1922 | 8 | << ", rnd:" << rnd << ", percent: " << percent; |
1923 | 8 | } |
1924 | 8 | }); |
1925 | 8 | int64_t cur_version = rowset->start_version(); |
1926 | 8 | std::vector<segment_v2::SegmentSharedPtr> segments; |
1927 | 8 | RETURN_IF_ERROR(std::dynamic_pointer_cast<BetaRowset>(rowset)->load_segments(&segments)); |
1928 | | |
1929 | | // If this rowset does not have a segment, there is no need for an update. |
1930 | 8 | if (segments.empty()) { |
1931 | 0 | LOG(INFO) << "[Schema Change or Clone] skip to construct delete bitmap tablet: " |
1932 | 0 | << self->tablet_id() << " cur max_version: " << cur_version; |
1933 | 0 | return Status::OK(); |
1934 | 0 | } |
1935 | | |
1936 | | // calculate delete bitmap between segments if necessary. |
1937 | 8 | DeleteBitmapPtr delete_bitmap = std::make_shared<DeleteBitmap>(self->tablet_id()); |
1938 | 8 | RETURN_IF_ERROR(self->calc_delete_bitmap_between_segments( |
1939 | 8 | rowset->tablet_schema(), rowset->rowset_id(), segments, delete_bitmap)); |
1940 | | |
1941 | | // get all base rowsets to calculate on |
1942 | 8 | std::vector<RowsetSharedPtr> specified_rowsets; |
1943 | 8 | RowsetIdUnorderedSet cur_rowset_ids; |
1944 | 8 | if (specified_base_rowsets == nullptr) { |
1945 | 8 | RETURN_IF_ERROR(self->get_all_rs_id_unlocked(cur_version - 1, &cur_rowset_ids)); |
1946 | 8 | specified_rowsets = self->get_rowset_by_ids(&cur_rowset_ids); |
1947 | 8 | } else { |
1948 | 0 | specified_rowsets = *specified_base_rowsets; |
1949 | 0 | } |
1950 | | |
1951 | 8 | OlapStopWatch watch; |
1952 | 8 | auto token = self->calc_delete_bitmap_executor()->create_token(); |
1953 | 8 | RETURN_IF_ERROR(calc_delete_bitmap(self, rowset, segments, specified_rowsets, delete_bitmap, |
1954 | 8 | cur_version - 1, token.get())); |
1955 | 8 | RETURN_IF_ERROR(token->wait()); |
1956 | 8 | size_t total_rows = std::accumulate( |
1957 | 8 | segments.begin(), segments.end(), 0, |
1958 | 8 | [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); }); |
1959 | 8 | LOG(INFO) << "[Schema Change or Clone] construct delete bitmap tablet: " << self->tablet_id() |
1960 | 8 | << ", rowset_ids: " << cur_rowset_ids.size() << ", cur max_version: " << cur_version |
1961 | 8 | << ", transaction_id: " << -1 << ", cost: " << watch.get_elapse_time_us() |
1962 | 8 | << "(us), total rows: " << total_rows; |
1963 | 8 | if (config::enable_merge_on_write_correctness_check) { |
1964 | | // check if all the rowset has ROWSET_SENTINEL_MARK |
1965 | 8 | auto st = self->check_delete_bitmap_correctness(delete_bitmap, cur_version - 1, -1, |
1966 | 8 | cur_rowset_ids, &specified_rowsets); |
1967 | 8 | if (!st.ok()) { |
1968 | 0 | LOG(WARNING) << fmt::format("delete bitmap correctness check failed in publish phase!"); |
1969 | 0 | } |
1970 | 8 | delete_bitmap->remove_sentinel_marks(); |
1971 | 8 | } |
1972 | 8 | for (auto& iter : delete_bitmap->delete_bitmap) { |
1973 | 0 | self->_tablet_meta->delete_bitmap().merge( |
1974 | 0 | {std::get<0>(iter.first), std::get<1>(iter.first), cur_version}, iter.second); |
1975 | 0 | } |
1976 | | |
1977 | 8 | return Status::OK(); |
1978 | 8 | } |
1979 | | |
1980 | | void BaseTablet::agg_delete_bitmap_for_stale_rowsets( |
1981 | 2.95k | Version version, DeleteBitmapKeyRanges& remove_delete_bitmap_key_ranges) { |
1982 | 2.95k | if (!config::enable_agg_and_remove_pre_rowsets_delete_bitmap) { |
1983 | 0 | return; |
1984 | 0 | } |
1985 | 2.95k | if (!(keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write())) { |
1986 | 0 | return; |
1987 | 0 | } |
1988 | 2.95k | int64_t start_version = version.first; |
1989 | 2.95k | int64_t end_version = version.second; |
1990 | 2.95k | if (start_version == end_version) { |
1991 | 0 | return; |
1992 | 0 | } |
1993 | 2.95k | DCHECK(start_version < end_version) |
1994 | 0 | << ". start_version: " << start_version << ", end_version: " << end_version; |
1995 | | // get pre rowsets |
1996 | 2.95k | std::vector<RowsetSharedPtr> pre_rowsets {}; |
1997 | 2.95k | { |
1998 | 2.95k | std::shared_lock rdlock(_meta_lock); |
1999 | 18.6k | for (const auto& it2 : _rs_version_map) { |
2000 | 18.6k | if (it2.first.second < start_version) { |
2001 | 4.37k | pre_rowsets.emplace_back(it2.second); |
2002 | 4.37k | } |
2003 | 18.6k | } |
2004 | 2.95k | } |
2005 | 2.95k | std::sort(pre_rowsets.begin(), pre_rowsets.end(), Rowset::comparator); |
2006 | | // do agg for pre rowsets |
2007 | 2.95k | DeleteBitmapPtr new_delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id()); |
2008 | 4.37k | for (auto& rowset : pre_rowsets) { |
2009 | 5.77k | for (uint32_t seg_id = 0; seg_id < rowset->num_segments(); ++seg_id) { |
2010 | 1.40k | auto d = tablet_meta()->delete_bitmap().get_agg_without_cache( |
2011 | 1.40k | {rowset->rowset_id(), seg_id, end_version}, start_version); |
2012 | 1.40k | if (d->isEmpty()) { |
2013 | 761 | continue; |
2014 | 761 | } |
2015 | 642 | VLOG_DEBUG << "agg delete bitmap for tablet_id=" << tablet_id() |
2016 | 0 | << ", rowset_id=" << rowset->rowset_id() << ", seg_id=" << seg_id |
2017 | 0 | << ", rowset_version=" << rowset->version().to_string() |
2018 | 0 | << ". compaction start_version=" << start_version |
2019 | 0 | << ", end_version=" << end_version << ", delete_bitmap=" << d->cardinality(); |
2020 | 642 | DeleteBitmap::BitmapKey start_key {rowset->rowset_id(), seg_id, start_version}; |
2021 | 642 | DeleteBitmap::BitmapKey end_key {rowset->rowset_id(), seg_id, end_version}; |
2022 | 642 | new_delete_bitmap->set(end_key, *d); |
2023 | 642 | remove_delete_bitmap_key_ranges.emplace_back(start_key, end_key); |
2024 | 642 | } |
2025 | 4.37k | } |
2026 | 2.95k | DBUG_EXECUTE_IF("BaseTablet.agg_delete_bitmap_for_stale_rowsets.merge_delete_bitmap.block", |
2027 | 2.95k | DBUG_BLOCK); |
2028 | 2.95k | tablet_meta()->delete_bitmap().merge(*new_delete_bitmap); |
2029 | 2.95k | } |
2030 | | |
2031 | | void BaseTablet::check_agg_delete_bitmap_for_stale_rowsets(int64_t& useless_rowset_count, |
2032 | 0 | int64_t& useless_rowset_version_count) { |
2033 | 0 | std::map<RowsetId, Version> rowset_ids; |
2034 | 0 | std::set<int64_t> end_versions; |
2035 | 0 | traverse_rowsets( |
2036 | 0 | [&rowset_ids, &end_versions](const RowsetSharedPtr& rs) { |
2037 | 0 | rowset_ids[rs->rowset_id()] = rs->version(); |
2038 | 0 | end_versions.emplace(rs->end_version()); |
2039 | 0 | }, |
2040 | 0 | true); |
2041 | |
|
2042 | 0 | std::set<RowsetId> useless_rowsets; |
2043 | 0 | std::map<RowsetId, std::vector<int64_t>> useless_rowset_versions; |
2044 | 0 | { |
2045 | 0 | _tablet_meta->delete_bitmap().traverse_rowset_and_version( |
2046 | | // 0: rowset and rowset with version exists |
2047 | | // -1: rowset does not exist |
2048 | | // -2: find next <rowset, version> |
2049 | | // rowset exist, rowset with version does not exist |
2050 | | // sequence table |
2051 | 0 | [&](const RowsetId& rowset_id, int64_t version) { |
2052 | 0 | auto rowset_it = rowset_ids.find(rowset_id); |
2053 | 0 | if (rowset_it == rowset_ids.end()) { |
2054 | 0 | useless_rowsets.emplace(rowset_id); |
2055 | 0 | return -1; |
2056 | 0 | } |
2057 | 0 | if (end_versions.find(version) == end_versions.end()) { |
2058 | 0 | if (tablet_schema()->has_sequence_col()) { |
2059 | 0 | auto rowset_version = rowset_it->second; |
2060 | 0 | if (version >= rowset_version.first && |
2061 | 0 | version <= rowset_version.second) { |
2062 | 0 | return -2; |
2063 | 0 | } |
2064 | 0 | } |
2065 | 0 | if (useless_rowset_versions.find(rowset_id) == |
2066 | 0 | useless_rowset_versions.end()) { |
2067 | 0 | useless_rowset_versions[rowset_id] = {}; |
2068 | 0 | } |
2069 | 0 | useless_rowset_versions[rowset_id].emplace_back(version); |
2070 | 0 | return -2; |
2071 | 0 | } |
2072 | 0 | return 0; |
2073 | 0 | }); |
2074 | 0 | } |
2075 | 0 | useless_rowset_count = useless_rowsets.size(); |
2076 | 0 | useless_rowset_version_count = useless_rowset_versions.size(); |
2077 | 0 | if (!useless_rowsets.empty() || !useless_rowset_versions.empty()) { |
2078 | 0 | std::stringstream ss; |
2079 | 0 | if (!useless_rowsets.empty()) { |
2080 | 0 | ss << "useless rowsets: {"; |
2081 | 0 | for (auto it = useless_rowsets.begin(); it != useless_rowsets.end(); ++it) { |
2082 | 0 | if (it != useless_rowsets.begin()) { |
2083 | 0 | ss << ", "; |
2084 | 0 | } |
2085 | 0 | ss << it->to_string(); |
2086 | 0 | } |
2087 | 0 | ss << "}. "; |
2088 | 0 | } |
2089 | 0 | if (!useless_rowset_versions.empty()) { |
2090 | 0 | ss << "useless rowset versions: {"; |
2091 | 0 | for (auto iter = useless_rowset_versions.begin(); iter != useless_rowset_versions.end(); |
2092 | 0 | ++iter) { |
2093 | 0 | if (iter != useless_rowset_versions.begin()) { |
2094 | 0 | ss << ", "; |
2095 | 0 | } |
2096 | 0 | ss << iter->first.to_string() << ": ["; |
2097 | | // some versions are continuous, such as [8, 9, 10, 11, 13, 17, 18] |
2098 | | // print as [8-11, 13, 17-18] |
2099 | 0 | int64_t last_start_version = -1; |
2100 | 0 | int64_t last_end_version = -1; |
2101 | 0 | for (int64_t version : iter->second) { |
2102 | 0 | if (last_start_version == -1) { |
2103 | 0 | last_start_version = version; |
2104 | 0 | last_end_version = version; |
2105 | 0 | continue; |
2106 | 0 | } |
2107 | 0 | if (last_end_version + 1 == version) { |
2108 | 0 | last_end_version = version; |
2109 | 0 | } else { |
2110 | 0 | if (last_start_version == last_end_version) { |
2111 | 0 | ss << last_start_version << ", "; |
2112 | 0 | } else { |
2113 | 0 | ss << last_start_version << "-" << last_end_version << ", "; |
2114 | 0 | } |
2115 | 0 | last_start_version = version; |
2116 | 0 | last_end_version = version; |
2117 | 0 | } |
2118 | 0 | } |
2119 | 0 | if (last_start_version == last_end_version) { |
2120 | 0 | ss << last_start_version; |
2121 | 0 | } else { |
2122 | 0 | ss << last_start_version << "-" << last_end_version; |
2123 | 0 | } |
2124 | |
|
2125 | 0 | ss << "]"; |
2126 | 0 | } |
2127 | 0 | ss << "}."; |
2128 | 0 | } |
2129 | 0 | LOG(WARNING) << "failed check_agg_delete_bitmap_for_stale_rowsets for tablet_id=" |
2130 | 0 | << tablet_id() << ". " << ss.str(); |
2131 | 0 | } else { |
2132 | 0 | LOG(INFO) << "succeed check_agg_delete_bitmap_for_stale_rowsets for tablet_id=" |
2133 | 0 | << tablet_id(); |
2134 | 0 | } |
2135 | 0 | } |
2136 | | |
2137 | 7 | RowsetSharedPtr BaseTablet::get_rowset(const RowsetId& rowset_id) { |
2138 | 7 | std::shared_lock rdlock(_meta_lock); |
2139 | 11 | for (auto& version_rowset : _rs_version_map) { |
2140 | 11 | if (version_rowset.second->rowset_id() == rowset_id) { |
2141 | 7 | return version_rowset.second; |
2142 | 7 | } |
2143 | 11 | } |
2144 | 0 | for (auto& stale_version_rowset : _stale_rs_version_map) { |
2145 | 0 | if (stale_version_rowset.second->rowset_id() == rowset_id) { |
2146 | 0 | return stale_version_rowset.second; |
2147 | 0 | } |
2148 | 0 | } |
2149 | 0 | return nullptr; |
2150 | 0 | } |
2151 | | |
2152 | 1.61k | std::vector<RowsetSharedPtr> BaseTablet::get_snapshot_rowset(bool include_stale_rowset) const { |
2153 | 1.61k | std::shared_lock rdlock(_meta_lock); |
2154 | 1.61k | std::vector<RowsetSharedPtr> rowsets; |
2155 | 1.61k | std::transform(_rs_version_map.cbegin(), _rs_version_map.cend(), std::back_inserter(rowsets), |
2156 | 5.10k | [](auto& kv) { return kv.second; }); |
2157 | 1.61k | if (include_stale_rowset) { |
2158 | 201 | std::transform(_stale_rs_version_map.cbegin(), _stale_rs_version_map.cend(), |
2159 | 362 | std::back_inserter(rowsets), [](auto& kv) { return kv.second; }); |
2160 | 201 | } |
2161 | 1.61k | return rowsets; |
2162 | 1.61k | } |
2163 | | |
2164 | | void BaseTablet::calc_consecutive_empty_rowsets( |
2165 | | std::vector<RowsetSharedPtr>* empty_rowsets, |
2166 | 218 | const std::vector<RowsetSharedPtr>& candidate_rowsets, int64_t limit) { |
2167 | 218 | int len = cast_set<int>(candidate_rowsets.size()); |
2168 | 255 | for (int i = 0; i < len - 1; ++i) { |
2169 | 41 | auto rowset = candidate_rowsets[i]; |
2170 | 41 | auto next_rowset = candidate_rowsets[i + 1]; |
2171 | | |
2172 | | // identify two consecutive rowsets that are empty |
2173 | 41 | if (rowset->num_segments() == 0 && next_rowset->num_segments() == 0 && |
2174 | 41 | !rowset->rowset_meta()->has_delete_predicate() && |
2175 | 41 | !next_rowset->rowset_meta()->has_delete_predicate() && |
2176 | 41 | rowset->end_version() == next_rowset->start_version() - 1) { |
2177 | 4 | empty_rowsets->emplace_back(rowset); |
2178 | 4 | empty_rowsets->emplace_back(next_rowset); |
2179 | 4 | rowset = next_rowset; |
2180 | 4 | int next_index = i + 2; |
2181 | | |
2182 | | // keep searching for consecutive empty rowsets |
2183 | 18 | while (next_index < len && candidate_rowsets[next_index]->num_segments() == 0 && |
2184 | 18 | !candidate_rowsets[next_index]->rowset_meta()->has_delete_predicate() && |
2185 | 18 | rowset->end_version() == candidate_rowsets[next_index]->start_version() - 1) { |
2186 | 14 | empty_rowsets->emplace_back(candidate_rowsets[next_index]); |
2187 | 14 | rowset = candidate_rowsets[next_index++]; |
2188 | 14 | } |
2189 | | // if the number of consecutive empty rowset reach the limit, |
2190 | | // and there are still rowsets following them |
2191 | 4 | if (empty_rowsets->size() >= limit && next_index < len) { |
2192 | 4 | return; |
2193 | 4 | } else { |
2194 | | // current rowset is not empty, start searching from that rowset in the next |
2195 | 0 | i = next_index - 1; |
2196 | 0 | empty_rowsets->clear(); |
2197 | 0 | } |
2198 | 4 | } |
2199 | 41 | } |
2200 | 218 | } |
2201 | | |
2202 | | Status BaseTablet::calc_file_crc(uint32_t* crc_value, int64_t start_version, int64_t end_version, |
2203 | 2 | uint32_t* rowset_count, int64_t* file_count) { |
2204 | 2 | Version v(start_version, end_version); |
2205 | 2 | std::vector<RowsetSharedPtr> rowsets; |
2206 | 10 | traverse_rowsets([&rowsets, &v](const auto& rs) { |
2207 | | // get all rowsets |
2208 | 10 | if (v.contains(rs->version())) { |
2209 | 10 | rowsets.emplace_back(rs); |
2210 | 10 | } |
2211 | 10 | }); |
2212 | 2 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
2213 | 2 | *rowset_count = cast_set<uint32_t>(rowsets.size()); |
2214 | | |
2215 | 2 | *crc_value = 0; |
2216 | 2 | *file_count = 0; |
2217 | 10 | for (const auto& rs : rowsets) { |
2218 | 10 | uint32_t rs_crc_value = 0; |
2219 | 10 | int64_t rs_file_count = 0; |
2220 | 10 | auto rowset = std::static_pointer_cast<BetaRowset>(rs); |
2221 | 10 | auto st = rowset->calc_file_crc(&rs_crc_value, &rs_file_count); |
2222 | 10 | if (!st.ok()) { |
2223 | 0 | return st; |
2224 | 0 | } |
2225 | | // crc_value is calculated based on the crc_value of each rowset. |
2226 | 10 | *crc_value = crc32c::Extend(*crc_value, reinterpret_cast<const uint8_t*>(&rs_crc_value), |
2227 | 10 | sizeof(rs_crc_value)); |
2228 | 10 | *file_count += rs_file_count; |
2229 | 10 | } |
2230 | 2 | return Status::OK(); |
2231 | 2 | } |
2232 | | |
2233 | 48 | Status BaseTablet::show_nested_index_file(std::string* json_meta) { |
2234 | 48 | Version v(0, max_version_unlocked()); |
2235 | 48 | std::vector<RowsetSharedPtr> rowsets; |
2236 | 147 | traverse_rowsets([&rowsets, &v](const auto& rs) { |
2237 | | // get all rowsets |
2238 | 147 | if (v.contains(rs->version())) { |
2239 | 147 | rowsets.emplace_back(rs); |
2240 | 147 | } |
2241 | 147 | }); |
2242 | 48 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
2243 | | |
2244 | 48 | rapidjson::Document doc; |
2245 | 48 | doc.SetObject(); |
2246 | 48 | rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); |
2247 | 48 | rapidjson::Value tabletIdValue(tablet_id()); |
2248 | 48 | doc.AddMember("tablet_id", tabletIdValue, allocator); |
2249 | | |
2250 | 48 | rapidjson::Value rowsets_value(rapidjson::kArrayType); |
2251 | | |
2252 | 147 | for (const auto& rs : rowsets) { |
2253 | 147 | rapidjson::Value rowset_value(rapidjson::kObjectType); |
2254 | | |
2255 | 147 | auto rowset = std::static_pointer_cast<BetaRowset>(rs); |
2256 | 147 | RETURN_IF_ERROR(rowset->show_nested_index_file(&rowset_value, allocator)); |
2257 | 146 | rowsets_value.PushBack(rowset_value, allocator); |
2258 | 146 | } |
2259 | 47 | doc.AddMember("rowsets", rowsets_value, allocator); |
2260 | | |
2261 | 47 | rapidjson::StringBuffer buffer; |
2262 | 47 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); |
2263 | 47 | doc.Accept(writer); |
2264 | 47 | *json_meta = std::string(buffer.GetString()); |
2265 | | |
2266 | 47 | return Status::OK(); |
2267 | 48 | } |
2268 | | |
2269 | | void BaseTablet::get_base_rowset_delete_bitmap_count( |
2270 | | uint64_t* max_base_rowset_delete_bitmap_score, |
2271 | 100 | int64_t* max_base_rowset_delete_bitmap_score_tablet_id) { |
2272 | 100 | std::vector<RowsetSharedPtr> rowsets_; |
2273 | 100 | std::string base_rowset_id_str; |
2274 | 100 | { |
2275 | 100 | std::shared_lock rowset_ldlock(this->get_header_lock()); |
2276 | 3.16k | for (const auto& it : _rs_version_map) { |
2277 | 3.16k | rowsets_.emplace_back(it.second); |
2278 | 3.16k | } |
2279 | 100 | } |
2280 | 100 | std::sort(rowsets_.begin(), rowsets_.end(), Rowset::comparator); |
2281 | 100 | if (!rowsets_.empty()) { |
2282 | 100 | bool base_found = false; |
2283 | 272 | for (auto& rowset : rowsets_) { |
2284 | 272 | if (rowset->start_version() > 2) { |
2285 | 72 | break; |
2286 | 72 | } |
2287 | 200 | base_found = true; |
2288 | 200 | uint64_t base_rowset_delete_bitmap_count = |
2289 | 200 | this->tablet_meta()->delete_bitmap().get_count_with_range( |
2290 | 200 | {rowset->rowset_id(), 0, 0}, |
2291 | 200 | {rowset->rowset_id(), UINT32_MAX, UINT64_MAX}); |
2292 | 200 | if (base_rowset_delete_bitmap_count > *max_base_rowset_delete_bitmap_score) { |
2293 | 14 | *max_base_rowset_delete_bitmap_score = base_rowset_delete_bitmap_count; |
2294 | 14 | *max_base_rowset_delete_bitmap_score_tablet_id = this->tablet_id(); |
2295 | 14 | } |
2296 | 200 | } |
2297 | 100 | if (!base_found) { |
2298 | 0 | LOG(WARNING) << "can not found base rowset for tablet " << tablet_id(); |
2299 | 0 | } |
2300 | 100 | } |
2301 | 100 | } |
2302 | | |
2303 | 1.05M | void TabletReadSource::fill_delete_predicates() { |
2304 | 1.05M | DCHECK_EQ(delete_predicates.size(), 0); |
2305 | 1.05M | auto delete_pred_view = |
2306 | 4.71M | rs_splits | std::views::transform([](auto&& split) { |
2307 | 4.71M | return split.rs_reader->rowset()->rowset_meta(); |
2308 | 4.71M | }) | |
2309 | 4.72M | std::views::filter([](const auto& rs_meta) { return rs_meta->has_delete_predicate(); }); |
2310 | 1.05M | delete_predicates = {delete_pred_view.begin(), delete_pred_view.end()}; |
2311 | 1.05M | } |
2312 | | |
2313 | 320M | int32_t BaseTablet::max_version_config() { |
2314 | 320M | int32_t max_version = tablet_meta()->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY |
2315 | 320M | ? std::max(config::time_series_max_tablet_version_num, |
2316 | 4.04k | config::max_tablet_version_num) |
2317 | 320M | : config::max_tablet_version_num; |
2318 | 320M | return max_version; |
2319 | 320M | } |
2320 | | |
2321 | 18.7k | void BaseTablet::prefill_dbm_agg_cache(const RowsetSharedPtr& rowset, int64_t version) { |
2322 | 28.4k | for (std::size_t i = 0; i < rowset->num_segments(); i++) { |
2323 | 9.75k | tablet_meta()->delete_bitmap().get_agg({rowset->rowset_id(), i, version}); |
2324 | 9.75k | } |
2325 | 18.7k | } |
2326 | | |
2327 | 5.98k | void BaseTablet::prefill_dbm_agg_cache_after_compaction(const RowsetSharedPtr& output_rowset) { |
2328 | 5.98k | if (keys_type() == KeysType::UNIQUE_KEYS && enable_unique_key_merge_on_write() && |
2329 | 5.98k | (config::enable_prefill_output_dbm_agg_cache_after_compaction || |
2330 | 3.05k | config::enable_prefill_all_dbm_agg_cache_after_compaction)) { |
2331 | 3.05k | int64_t cur_max_version {-1}; |
2332 | 3.05k | { |
2333 | 3.05k | std::shared_lock rlock(get_header_lock()); |
2334 | 3.05k | cur_max_version = max_version_unlocked(); |
2335 | 3.05k | } |
2336 | 3.05k | if (config::enable_prefill_all_dbm_agg_cache_after_compaction) { |
2337 | 3.05k | traverse_rowsets( |
2338 | 18.7k | [&](const RowsetSharedPtr& rs) { prefill_dbm_agg_cache(rs, cur_max_version); }, |
2339 | 3.05k | false); |
2340 | 3.05k | } else if (config::enable_prefill_output_dbm_agg_cache_after_compaction) { |
2341 | 0 | prefill_dbm_agg_cache(output_rowset, cur_max_version); |
2342 | 0 | } |
2343 | 3.05k | } |
2344 | 5.98k | } |
2345 | | |
2346 | | bool BaseTablet::_key_is_not_in_segment(Slice key, const KeyBoundsPB& segment_key_bounds, |
2347 | 4.03M | bool is_segments_key_bounds_truncated) { |
2348 | 4.03M | Slice maybe_truncated_min_key {segment_key_bounds.min_key()}; |
2349 | 4.03M | Slice maybe_truncated_max_key {segment_key_bounds.max_key()}; |
2350 | 4.03M | bool res1 = Slice::lhs_is_strictly_less_than_rhs(key, false, maybe_truncated_min_key, |
2351 | 4.03M | is_segments_key_bounds_truncated); |
2352 | 4.03M | bool res2 = Slice::lhs_is_strictly_less_than_rhs(maybe_truncated_max_key, |
2353 | 4.03M | is_segments_key_bounds_truncated, key, false); |
2354 | 4.03M | return res1 || res2; |
2355 | 4.03M | } |
2356 | | |
2357 | | } // namespace doris |