be/src/storage/rowset_builder.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/rowset_builder.h" |
19 | | |
20 | | #include <brpc/controller.h> |
21 | | #include <fmt/format.h> |
22 | | |
23 | | #include <filesystem> |
24 | | #include <memory> |
25 | | #include <ostream> |
26 | | #include <string> |
27 | | #include <utility> |
28 | | |
29 | | // IWYU pragma: no_include <opentelemetry/common/threadlocal.h> |
30 | | #include "common/compiler_util.h" // IWYU pragma: keep |
31 | | #include "common/config.h" |
32 | | #include "common/status.h" |
33 | | #include "core/block/block.h" |
34 | | #include "io/fs/file_system.h" |
35 | | #include "io/fs/file_writer.h" // IWYU pragma: keep |
36 | | #include "runtime/memory/global_memory_arbitrator.h" |
37 | | #include "storage/delete/calc_delete_bitmap_executor.h" |
38 | | #include "storage/olap_define.h" |
39 | | #include "storage/partial_update_info.h" |
40 | | #include "storage/rowset/beta_rowset.h" |
41 | | #include "storage/rowset/beta_rowset_writer.h" |
42 | | #include "storage/rowset/group_rowset_writer.h" |
43 | | #include "storage/rowset/pending_rowset_helper.h" |
44 | | #include "storage/rowset/rowset_factory.h" |
45 | | #include "storage/rowset/rowset_meta.h" |
46 | | #include "storage/rowset/rowset_meta_manager.h" |
47 | | #include "storage/rowset/rowset_writer.h" |
48 | | #include "storage/rowset/rowset_writer_context.h" |
49 | | #include "storage/schema_change/schema_change.h" |
50 | | #include "storage/storage_engine.h" |
51 | | #include "storage/tablet/tablet_manager.h" |
52 | | #include "storage/tablet/tablet_meta.h" |
53 | | #include "storage/tablet/tablet_schema.h" |
54 | | #include "storage/tablet_info.h" |
55 | | #include "storage/txn/txn_manager.h" |
56 | | #include "util/brpc_client_cache.h" |
57 | | #include "util/brpc_closure.h" |
58 | | #include "util/debug_points.h" |
59 | | #include "util/mem_info.h" |
60 | | #include "util/stopwatch.hpp" |
61 | | #include "util/time.h" |
62 | | #include "util/trace.h" |
63 | | |
64 | | namespace doris { |
65 | | using namespace ErrorCode; |
66 | | |
67 | | BaseRowsetBuilder::BaseRowsetBuilder(const WriteRequest& req, RuntimeProfile* profile) |
68 | 32 | : _req(req), _tablet_schema(std::make_shared<TabletSchema>()) { |
69 | 32 | if (profile != nullptr) { |
70 | 32 | _init_profile(profile); |
71 | 32 | } |
72 | 32 | } |
73 | | |
74 | | RowsetBuilder::RowsetBuilder(StorageEngine& engine, const WriteRequest& req, |
75 | | RuntimeProfile* profile) |
76 | 31 | : BaseRowsetBuilder(req, profile), _engine(engine) {} |
77 | | |
78 | | RowBinlogRowsetBuilder::RowBinlogRowsetBuilder(StorageEngine& engine, const WriteRequest& req, |
79 | | RuntimeProfile* profile) |
80 | 1 | : RowsetBuilder(engine, req, profile) {} |
81 | | |
82 | 32 | void BaseRowsetBuilder::_init_profile(RuntimeProfile* profile) { |
83 | 32 | DCHECK(profile != nullptr); |
84 | 32 | if (_req.write_req_type == WriteRequestType::GROUP) { |
85 | 1 | _profile = profile->create_child(fmt::format("GroupRowsetBuilder {}", _req.tablet_id), true, |
86 | 1 | true); |
87 | 1 | return; |
88 | 1 | } |
89 | | |
90 | 31 | _profile = profile->create_child( |
91 | 31 | fmt::format( |
92 | 31 | "RowsetBuilder {} {}", _req.tablet_id, |
93 | 31 | _req.write_req_type == WriteRequestType::ROW_BINLOG ? "row_binlog" : "data"), |
94 | 31 | true, true); |
95 | 31 | _build_rowset_timer = ADD_TIMER(_profile, "BuildRowsetTime"); |
96 | 31 | _submit_delete_bitmap_timer = ADD_TIMER(_profile, "DeleteBitmapSubmitTime"); |
97 | 31 | _wait_delete_bitmap_timer = ADD_TIMER(_profile, "DeleteBitmapWaitTime"); |
98 | 31 | } |
99 | | |
100 | 0 | void RowsetBuilder::_init_profile(RuntimeProfile* profile) { |
101 | 0 | DCHECK(profile != nullptr); |
102 | 0 | BaseRowsetBuilder::_init_profile(profile); |
103 | 0 | _commit_txn_timer = ADD_TIMER(_profile, "CommitTxnTime"); |
104 | 0 | } |
105 | | |
106 | 32 | BaseRowsetBuilder::~BaseRowsetBuilder() { |
107 | 32 | if (!_is_init) { |
108 | 1 | return; |
109 | 1 | } |
110 | | |
111 | 31 | if (_calc_delete_bitmap_token != nullptr) { |
112 | 29 | _calc_delete_bitmap_token->cancel(); |
113 | 29 | } |
114 | 31 | } |
115 | | |
116 | 31 | RowsetBuilder::~RowsetBuilder() { |
117 | 31 | if (_is_init && !_is_committed) { |
118 | | // For txn rowset builders, we need to rollback txn when necessary. |
119 | 7 | _garbage_collection(is_data_builder()); |
120 | 7 | } |
121 | 31 | } |
122 | | |
123 | 245 | Tablet* RowsetBuilder::tablet() { |
124 | 245 | return static_cast<Tablet*>(_tablet.get()); |
125 | 245 | } |
126 | | |
127 | 0 | TabletSharedPtr RowsetBuilder::tablet_sptr() { |
128 | 0 | return std::static_pointer_cast<Tablet>(_tablet); |
129 | 0 | } |
130 | | |
131 | 7 | void RowsetBuilder::_garbage_collection(bool cancel_txn) { |
132 | 7 | Status rollback_status; |
133 | 7 | bool need_clean = true; |
134 | 7 | if (tablet() != nullptr && cancel_txn) { |
135 | 6 | TxnManager* txn_mgr = _engine.txn_manager(); |
136 | 6 | rollback_status = txn_mgr->rollback_txn(_req.partition_id, *tablet(), _req.txn_id); |
137 | 6 | need_clean = rollback_status.ok(); |
138 | 6 | } |
139 | | // has to check rollback status, because the rowset maybe committed in this thread and |
140 | | // published in another thread, then rollback will fail. |
141 | | // when rollback failed should not delete rowset |
142 | 7 | if (need_clean) { |
143 | 7 | _engine.add_unused_rowset(_rowset); |
144 | 7 | for (auto& rs : _attach_rowsets) { |
145 | 0 | _engine.add_unused_rowset(rs); |
146 | 0 | } |
147 | 7 | } |
148 | 7 | } |
149 | | |
150 | 3 | Status BaseRowsetBuilder::init_mow_context(std::shared_ptr<MowContext>& mow_context) { |
151 | 3 | DCHECK(is_data_builder()); |
152 | | |
153 | 3 | std::lock_guard<std::shared_mutex> lck(tablet()->get_header_lock()); |
154 | 3 | _max_version_in_flush_phase = tablet()->max_version_unlocked(); |
155 | 3 | std::vector<RowsetSharedPtr> rowset_ptrs; |
156 | | // tablet is under alter process. The delete bitmap will be calculated after conversion. |
157 | 3 | if (tablet()->tablet_state() == TABLET_NOTREADY) { |
158 | | // Disable 'partial_update' when the tablet is undergoing a 'schema changing process' |
159 | 0 | if (_req.table_schema_param->is_partial_update()) { |
160 | 0 | return Status::InternalError( |
161 | 0 | "Unable to do 'partial_update' when " |
162 | 0 | "the tablet is undergoing a 'schema changing process'"); |
163 | 0 | } |
164 | 0 | _rowset_ids->clear(); |
165 | 3 | } else { |
166 | 3 | RETURN_IF_ERROR( |
167 | 3 | tablet()->get_all_rs_id_unlocked(_max_version_in_flush_phase, _rowset_ids.get())); |
168 | 3 | rowset_ptrs = tablet()->get_rowset_by_ids(_rowset_ids.get()); |
169 | 3 | } |
170 | 3 | _delete_bitmap = std::make_shared<DeleteBitmap>(tablet()->tablet_id()); |
171 | 3 | mow_context = std::make_shared<MowContext>(_max_version_in_flush_phase, _req.txn_id, |
172 | 3 | _rowset_ids, rowset_ptrs, _delete_bitmap); |
173 | 3 | return Status::OK(); |
174 | 3 | } |
175 | | |
176 | 29 | Status RowsetBuilder::check_tablet_version_count() { |
177 | 29 | DCHECK(is_data_builder()); |
178 | | |
179 | 29 | auto max_version_config = _tablet->max_version_config(); |
180 | 29 | auto version_count = tablet()->version_count(); |
181 | 29 | DBUG_EXECUTE_IF("RowsetBuilder.check_tablet_version_count.too_many_version", |
182 | 29 | { version_count = INT_MAX; }); |
183 | | // Trigger TOO MANY VERSION error first |
184 | 29 | if (version_count > max_version_config) { |
185 | 0 | return Status::Error<TOO_MANY_VERSION>( |
186 | 0 | "failed to init rowset builder. version count: {}, exceed limit: {}, " |
187 | 0 | "tablet: {}. Please reduce the frequency of loading data or adjust the " |
188 | 0 | "max_tablet_version_num or time_series_max_tablet_version_num in be.conf to a " |
189 | 0 | "larger value.", |
190 | 0 | version_count, max_version_config, _tablet->tablet_id()); |
191 | 0 | } |
192 | | // (TODO Refrain) Maybe we can use a configurable param instead of hardcoded values '100'. |
193 | | // max_version_config must > 100, otherwise silent errors will occur. |
194 | 29 | if ((!config::disable_auto_compaction && |
195 | 29 | !_tablet->tablet_meta()->tablet_schema()->disable_auto_compaction()) && |
196 | 29 | (version_count > max_version_config - 100) && |
197 | 29 | !GlobalMemoryArbitrator::is_exceed_soft_mem_limit(GB_EXCHANGE_BYTE)) { |
198 | | // Trigger compaction |
199 | 0 | auto st = _engine.submit_compaction_task( |
200 | 0 | tablet_sptr(), CompactionType::CUMULATIVE_COMPACTION, true, true, 2); |
201 | 0 | if (!st.ok()) [[unlikely]] { |
202 | 0 | LOG(WARNING) << "failed to trigger compaction, tablet_id=" << _tablet->tablet_id() |
203 | 0 | << " : " << st; |
204 | 0 | } |
205 | 0 | } |
206 | 29 | return Status::OK(); |
207 | 29 | } |
208 | | |
209 | 29 | Status RowsetBuilder::prepare_txn() { |
210 | 29 | DCHECK(is_data_builder()); |
211 | 29 | return tablet()->prepare_txn(_req.partition_id, _req.txn_id, _req.load_id, false); |
212 | 29 | } |
213 | | |
214 | 30 | Status RowsetBuilder::init() { |
215 | 30 | RowsetWriterContext context; |
216 | | |
217 | 30 | RETURN_IF_ERROR(_init_context_common_fields(context)); |
218 | | |
219 | 29 | if (tablet()->enable_row_binlog()) { |
220 | 1 | context.write_binlog_opt().mark_primary_writer(); |
221 | 1 | } |
222 | | |
223 | 29 | std::shared_ptr<MowContext> mow_context; |
224 | 29 | if (_tablet->enable_unique_key_merge_on_write()) { |
225 | 3 | RETURN_IF_ERROR(init_mow_context(mow_context)); |
226 | 3 | } |
227 | | |
228 | 29 | RETURN_IF_ERROR(check_tablet_version_count()); |
229 | | |
230 | 29 | auto version_count = tablet()->version_count() + tablet()->stale_version_count(); |
231 | 29 | if (tablet()->avg_rs_meta_serialize_size() * version_count > |
232 | 29 | config::tablet_meta_serialize_size_limit) { |
233 | 0 | return Status::Error<TOO_MANY_VERSION>( |
234 | 0 | "failed to init rowset builder. meta serialize size : {}, exceed limit: {}, " |
235 | 0 | "tablet: {}. Please reduce the frequency of loading data or adjust the " |
236 | 0 | "max_tablet_version_num in be.conf to a larger value.", |
237 | 0 | tablet()->avg_rs_meta_serialize_size() * version_count, |
238 | 0 | config::tablet_meta_serialize_size_limit, _tablet->tablet_id()); |
239 | 0 | } |
240 | | |
241 | 29 | RETURN_IF_ERROR(prepare_txn()); |
242 | | |
243 | 29 | DBUG_EXECUTE_IF("BaseRowsetBuilder::init.check_partial_update_column_num", { |
244 | 29 | if (_req.table_schema_param->partial_update_input_columns().size() != |
245 | 29 | dp->param<int>("column_num")) { |
246 | 29 | return Status::InternalError("partial update input column num wrong!"); |
247 | 29 | }; |
248 | 29 | }) |
249 | | // build tablet schema in request level |
250 | 29 | RETURN_IF_ERROR(_build_current_tablet_schema(_req.index_id, _req.table_schema_param.get(), |
251 | 29 | *_tablet->tablet_schema())); |
252 | | |
253 | 29 | context.mow_context = mow_context; |
254 | | |
255 | 29 | context.partial_update_info = _partial_update_info; |
256 | 29 | _rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false)); |
257 | 29 | _rowset_id = context.rowset_id; |
258 | 29 | std::vector<RowsetId> tmp_pending_rowset_ids = {_rowset_id}; |
259 | 29 | tmp_pending_rowset_ids.resize(1 + _attach_rowset_ids.size()); |
260 | 29 | std::copy(_attach_rowset_ids.begin(), _attach_rowset_ids.end(), |
261 | 29 | tmp_pending_rowset_ids.begin() + 1); |
262 | 29 | _pending_rs_guard = _engine.pending_local_rowsets().add(tmp_pending_rowset_ids); |
263 | | |
264 | 29 | _calc_delete_bitmap_token = _engine.calc_delete_bitmap_executor()->create_token(); |
265 | | |
266 | 29 | _is_init = true; |
267 | 29 | return Status::OK(); |
268 | 29 | } |
269 | | |
270 | 31 | Status BaseRowsetBuilder::_init_context_common_fields(RowsetWriterContext& context) { |
271 | 31 | _tablet = DORIS_TRY(ExecEnv::get_tablet(_req.tablet_id)); |
272 | | |
273 | 30 | context.txn_id = _req.txn_id; |
274 | 30 | context.load_id = _req.load_id; |
275 | 30 | context.rowset_state = PREPARED; |
276 | 30 | context.segments_overlap = OVERLAPPING; |
277 | 30 | context.tablet_schema = _tablet_schema; |
278 | 30 | context.newest_write_timestamp = UnixSeconds(); |
279 | 30 | context.tablet_id = _req.tablet_id; |
280 | 30 | context.index_id = _req.index_id; |
281 | 30 | context.tablet = _tablet; |
282 | 30 | context.enable_segcompaction = true; |
283 | 30 | context.write_type = DataWriteType::TYPE_DIRECT; |
284 | 30 | context.write_file_cache = _req.write_file_cache; |
285 | | |
286 | 30 | return Status::OK(); |
287 | 31 | } |
288 | | |
289 | 25 | Status BaseRowsetBuilder::build_rowset() { |
290 | 25 | std::lock_guard<std::mutex> l(_lock); |
291 | 25 | DCHECK(_is_init) << "rowset builder is supposed be to initialized before " |
292 | 0 | "build_rowset() being called"; |
293 | | |
294 | 25 | SCOPED_TIMER(_build_rowset_timer); |
295 | | // use rowset meta manager to save meta |
296 | 25 | RETURN_NOT_OK_STATUS_WITH_WARN(_rowset_writer->build(_rowset), "fail to build rowset"); |
297 | 25 | return Status::OK(); |
298 | 25 | } |
299 | | |
300 | 1 | Status GroupRowsetBuilder::build_rowset() { |
301 | | // build binlog rowset first, then data rowset |
302 | 1 | RETURN_IF_ERROR(_row_binlog_rowset_builder->build_rowset()); |
303 | 1 | return _txn_rs_builder->build_rowset(); |
304 | 1 | } |
305 | | |
306 | 17 | Status BaseRowsetBuilder::submit_calc_delete_bitmap_task() { |
307 | 17 | DCHECK(is_data_builder()); |
308 | 17 | if (!_tablet->enable_unique_key_merge_on_write() || _rowset->num_segments() == 0) { |
309 | 14 | return Status::OK(); |
310 | 14 | } |
311 | 3 | std::lock_guard<std::mutex> l(_lock); |
312 | 3 | SCOPED_TIMER(_submit_delete_bitmap_timer); |
313 | 3 | if (_partial_update_info && _partial_update_info->is_flexible_partial_update()) { |
314 | 0 | if (_rowset->num_segments() > 1) { |
315 | | // in flexible partial update, when there are more one segment in one load, |
316 | | // we need to do alignment process for same keys between segments, we haven't |
317 | | // implemented it yet and just report an error when encouter this situation |
318 | 0 | return Status::NotSupported( |
319 | 0 | "too large input data in flexible partial update, Please " |
320 | 0 | "reduce the amount of data imported in a single load."); |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | 3 | auto* beta_rowset = reinterpret_cast<BetaRowset*>(_rowset.get()); |
325 | 3 | std::vector<segment_v2::SegmentSharedPtr> segments; |
326 | 3 | RETURN_IF_ERROR(beta_rowset->load_segments(&segments)); |
327 | 3 | if (segments.size() > 1) { |
328 | | // calculate delete bitmap between segments |
329 | 0 | if (config::enable_calc_delete_bitmap_between_segments_concurrently) { |
330 | 0 | RETURN_IF_ERROR(_calc_delete_bitmap_token->submit( |
331 | 0 | _tablet, _tablet_schema, _rowset->rowset_id(), segments, _delete_bitmap)); |
332 | 0 | } else { |
333 | 0 | RETURN_IF_ERROR(_tablet->calc_delete_bitmap_between_segments( |
334 | 0 | _tablet_schema, _rowset->rowset_id(), segments, _delete_bitmap)); |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | | // For partial update, we need to fill in the entire row of data, during the calculation |
339 | | // of the delete bitmap. This operation is resource-intensive, and we need to minimize |
340 | | // the number of times it occurs. Therefore, we skip this operation here. |
341 | 3 | if (_partial_update_info->is_partial_update()) { |
342 | | // for partial update, the delete bitmap calculation is done while append_block() |
343 | | // we print it's summarize logs here before commit. |
344 | 0 | LOG(INFO) << fmt::format( |
345 | 0 | "{} calc delete bitmap summary before commit: tablet({}), txn_id({}), " |
346 | 0 | "rowset_ids({}), cur max_version({}), bitmap num({}), bitmap_cardinality({}), num " |
347 | 0 | "rows updated({}), num rows new added({}), num rows deleted({}), total rows({})", |
348 | 0 | _partial_update_info->partial_update_mode_str(), tablet()->tablet_id(), _req.txn_id, |
349 | 0 | _rowset_ids->size(), rowset_writer()->context().mow_context->max_version, |
350 | 0 | _delete_bitmap->get_delete_bitmap_count(), _delete_bitmap->cardinality(), |
351 | 0 | rowset_writer()->num_rows_updated(), rowset_writer()->num_rows_new_added(), |
352 | 0 | rowset_writer()->num_rows_deleted(), rowset_writer()->num_rows()); |
353 | 0 | return Status::OK(); |
354 | 0 | } |
355 | | |
356 | 3 | LOG(INFO) << "submit calc delete bitmap task to executor, tablet_id: " << tablet()->tablet_id() |
357 | 3 | << ", txn_id: " << _req.txn_id; |
358 | 3 | return BaseTablet::commit_phase_update_delete_bitmap(_tablet, _rowset, *_rowset_ids, |
359 | 3 | _delete_bitmap, segments, _req.txn_id, |
360 | 3 | _calc_delete_bitmap_token.get(), nullptr); |
361 | 3 | } |
362 | | |
363 | 17 | Status BaseRowsetBuilder::wait_calc_delete_bitmap() { |
364 | 17 | DCHECK(is_data_builder()); |
365 | 17 | if (!_tablet->enable_unique_key_merge_on_write() || _partial_update_info->is_partial_update()) { |
366 | 14 | return Status::OK(); |
367 | 14 | } |
368 | 3 | std::lock_guard<std::mutex> l(_lock); |
369 | 3 | SCOPED_TIMER(_wait_delete_bitmap_timer); |
370 | 3 | RETURN_IF_ERROR(_calc_delete_bitmap_token->wait()); |
371 | 3 | return Status::OK(); |
372 | 3 | } |
373 | | |
374 | 23 | Status RowsetBuilder::commit_txn() { |
375 | 23 | DCHECK(is_data_builder()); |
376 | 23 | if (tablet()->enable_unique_key_merge_on_write() && |
377 | 23 | config::enable_merge_on_write_correctness_check && _rowset->num_rows() != 0 && |
378 | 23 | tablet()->tablet_state() != TABLET_NOTREADY) { |
379 | 3 | auto st = tablet()->check_delete_bitmap_correctness( |
380 | 3 | _delete_bitmap, _rowset->end_version() - 1, _req.txn_id, *_rowset_ids); |
381 | 3 | if (!st.ok()) { |
382 | 0 | LOG(WARNING) << fmt::format( |
383 | 0 | "[tablet_id:{}][txn_id:{}][load_id:{}][partition_id:{}] " |
384 | 0 | "delete bitmap correctness check failed in commit phase!", |
385 | 0 | _req.tablet_id, _req.txn_id, UniqueId(_req.load_id).to_string(), |
386 | 0 | _req.partition_id); |
387 | 0 | return st; |
388 | 0 | } |
389 | 3 | } |
390 | 23 | std::lock_guard<std::mutex> l(_lock); |
391 | 23 | SCOPED_TIMER(_commit_txn_timer); |
392 | | |
393 | | // Transfer ownership of `PendingRowsetGuard` to `TxnManager` |
394 | 23 | Status res = _engine.txn_manager()->commit_txn( |
395 | 23 | _req.partition_id, *tablet(), _req.txn_id, _req.load_id, _rowset, |
396 | 23 | std::move(_pending_rs_guard), false, _partial_update_info); |
397 | | |
398 | 23 | if (!res && !res.is<PUSH_TRANSACTION_ALREADY_EXIST>()) { |
399 | 0 | LOG(WARNING) << "Failed to commit txn: " << _req.txn_id |
400 | 0 | << " for rowset: " << _rowset->rowset_id(); |
401 | 0 | return res; |
402 | 0 | } |
403 | 23 | if (_tablet->enable_unique_key_merge_on_write()) { |
404 | 3 | _engine.txn_manager()->set_txn_related_delete_bitmap( |
405 | 3 | _req.partition_id, _req.txn_id, tablet()->tablet_id(), tablet()->tablet_uid(), true, |
406 | 3 | _delete_bitmap, *_rowset_ids, _partial_update_info); |
407 | 3 | } |
408 | | |
409 | 23 | _is_committed = true; |
410 | 23 | return Status::OK(); |
411 | 23 | } |
412 | | |
413 | 0 | Status BaseRowsetBuilder::cancel() { |
414 | 0 | std::lock_guard<std::mutex> l(_lock); |
415 | 0 | if (_is_cancelled) { |
416 | 0 | return Status::OK(); |
417 | 0 | } |
418 | 0 | if (_calc_delete_bitmap_token != nullptr) { |
419 | 0 | _calc_delete_bitmap_token->cancel(); |
420 | 0 | } |
421 | 0 | _is_cancelled = true; |
422 | 0 | return Status::OK(); |
423 | 0 | } |
424 | | |
425 | | Status BaseRowsetBuilder::_build_current_tablet_schema( |
426 | | int64_t index_id, const OlapTableSchemaParam* table_schema_param, |
427 | 30 | const TabletSchema& ori_tablet_schema) { |
428 | | // find the right index id |
429 | 30 | int i = 0; |
430 | 30 | auto indexes = table_schema_param->indexes(); |
431 | 30 | for (; i < indexes.size(); i++) { |
432 | 15 | if (indexes[i]->index_id == index_id) { |
433 | 15 | break; |
434 | 15 | } |
435 | 15 | } |
436 | 30 | if (!indexes.empty() && !indexes[i]->columns.empty() && |
437 | 30 | indexes[i]->columns[0]->unique_id() >= 0) { |
438 | 0 | _tablet_schema->shawdow_copy_without_columns(ori_tablet_schema); |
439 | 0 | _tablet_schema->build_current_tablet_schema( |
440 | 0 | index_id, cast_set<int32_t>(table_schema_param->version()), indexes[i], |
441 | 0 | ori_tablet_schema); |
442 | 30 | } else { |
443 | 30 | _tablet_schema->copy_from(ori_tablet_schema); |
444 | 30 | } |
445 | 30 | if (_tablet_schema->schema_version() > ori_tablet_schema.schema_version()) { |
446 | | // After schema change, should include extracted column |
447 | | // For example: a table has two columns, k and v |
448 | | // After adding a column v2, the schema version increases, max_version_schema needs to be updated. |
449 | | // _tablet_schema includes k, v, and v2 |
450 | | // if v is a variant, need to add the columns decomposed from the v to the _tablet_schema. |
451 | 0 | if (is_data_builder()) { |
452 | 0 | if (_tablet_schema->num_variant_columns() > 0) { |
453 | 0 | TabletSchemaSPtr max_version_schema = std::make_shared<TabletSchema>(); |
454 | 0 | max_version_schema->copy_from(*_tablet_schema); |
455 | 0 | max_version_schema->copy_extracted_columns(ori_tablet_schema); |
456 | 0 | _tablet->update_max_version_schema(max_version_schema); |
457 | 0 | } else { |
458 | 0 | _tablet->update_max_version_schema(_tablet_schema); |
459 | 0 | } |
460 | 0 | } |
461 | 0 | } |
462 | | |
463 | 30 | _tablet_schema->set_table_id(table_schema_param->table_id()); |
464 | 30 | _tablet_schema->set_db_id(table_schema_param->db_id()); |
465 | 30 | if (table_schema_param->is_partial_update()) { |
466 | 0 | _tablet_schema->set_auto_increment_column(table_schema_param->auto_increment_coulumn()); |
467 | 0 | } |
468 | | // set partial update columns info |
469 | 30 | _partial_update_info = std::make_shared<PartialUpdateInfo>(); |
470 | 30 | RETURN_IF_ERROR(_partial_update_info->init( |
471 | 30 | tablet()->tablet_id(), _req.txn_id, *_tablet_schema, |
472 | 30 | table_schema_param->unique_key_update_mode(), |
473 | 30 | table_schema_param->partial_update_new_key_policy(), |
474 | 30 | table_schema_param->partial_update_input_columns(), |
475 | 30 | table_schema_param->is_strict_mode(), table_schema_param->timestamp_ms(), |
476 | 30 | table_schema_param->nano_seconds(), table_schema_param->timezone(), |
477 | 30 | table_schema_param->auto_increment_coulumn(), |
478 | 30 | table_schema_param->sequence_map_col_uid(), _max_version_in_flush_phase)); |
479 | 30 | return Status::OK(); |
480 | 30 | } |
481 | | |
482 | | GroupRowsetBuilder::GroupRowsetBuilder(StorageEngine& engine, const WriteRequest& req, |
483 | | const WriteRequest& row_binlog_req, RuntimeProfile* profile) |
484 | 1 | : BaseRowsetBuilder( |
485 | 1 | [](int64_t tablet_id) { |
486 | 1 | WriteRequest group_req; |
487 | 1 | group_req.tablet_id = tablet_id; |
488 | 1 | group_req.write_req_type = WriteRequestType::GROUP; |
489 | 1 | return group_req; |
490 | 1 | }(req.tablet_id), |
491 | 1 | profile) { |
492 | 1 | _row_binlog_rowset_builder = |
493 | 1 | std::make_shared<RowBinlogRowsetBuilder>(engine, row_binlog_req, profile); |
494 | 1 | _txn_rs_builder = std::make_shared<RowsetBuilder>(engine, req, profile); |
495 | 1 | } |
496 | | |
497 | 1 | Status GroupRowsetBuilder::init() { |
498 | | // init binlog builder first so that its rowset id can be added into |
499 | | // PendingLocalRowsets before txn builder init. |
500 | 1 | RETURN_IF_ERROR(_row_binlog_rowset_builder->init()); |
501 | | // before init txn, need to add all rowset_ids into PendingLocalRowsets. |
502 | | // see https://github.com/apache/doris/pull/25921 |
503 | 1 | RETURN_IF_ERROR(_txn_rs_builder->attach_pending_rs_guard_to_txn( |
504 | 1 | _row_binlog_rowset_builder->rowset_id())); |
505 | 1 | RETURN_IF_ERROR(_txn_rs_builder->init()); |
506 | | |
507 | | // Create a GroupRowsetWriter that forwards flush to both underlying |
508 | | // RowsetWriters. |
509 | 1 | std::unique_ptr<doris::GroupRowsetWriter> group_writer; |
510 | 1 | RETURN_IF_ERROR(RowsetFactory::create_empty_group_rowset_writer(&group_writer)); |
511 | 1 | group_writer->set_data_writer(_txn_rs_builder->rowset_writer()); |
512 | 1 | group_writer->set_row_binlog_writer(_row_binlog_rowset_builder->rowset_writer()); |
513 | | |
514 | 1 | _rowset_writer = std::move(group_writer); |
515 | 1 | _is_init = true; |
516 | 1 | return Status::OK(); |
517 | 1 | } |
518 | | |
519 | 0 | Status GroupRowsetBuilder::submit_calc_delete_bitmap_task() { |
520 | 0 | return _txn_rs_builder->submit_calc_delete_bitmap_task(); |
521 | 0 | } |
522 | | |
523 | 0 | Status GroupRowsetBuilder::wait_calc_delete_bitmap() { |
524 | 0 | return _txn_rs_builder->wait_calc_delete_bitmap(); |
525 | 0 | } |
526 | | |
527 | 0 | Status GroupRowsetBuilder::commit_txn() { |
528 | | // Attach binlog rowset to txn rowset, so that commit/rollback and |
529 | | // clean-up are all handled by txn rowset builder. |
530 | 0 | RETURN_IF_ERROR(_txn_rs_builder->attach_rowset_to_txn(_row_binlog_rowset_builder->rowset())); |
531 | 0 | return _txn_rs_builder->commit_txn(); |
532 | 0 | } |
533 | | |
534 | 1 | Status RowBinlogRowsetBuilder::init() { |
535 | 1 | RowsetWriterContext context; |
536 | | |
537 | 1 | RETURN_IF_ERROR(_init_context_common_fields(context)); |
538 | | |
539 | | // build tablet schema in request level |
540 | 1 | RETURN_IF_ERROR(_build_current_tablet_schema( |
541 | 1 | _req.index_id, _req.table_schema_param.get(), |
542 | 1 | *std::dynamic_pointer_cast<Tablet>(_tablet)->row_binlog_tablet_schema())); |
543 | 1 | context.write_binlog_opt().mark_binlog_writer(); |
544 | | |
545 | 1 | _rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false)); |
546 | | // need to attach PendingRowsetGuard after txn_rs_builder init |
547 | 1 | _rowset_id = context.rowset_id; |
548 | | |
549 | 1 | _is_init = true; |
550 | 1 | return Status::OK(); |
551 | 1 | } |
552 | | |
553 | 0 | Status BaseRowsetBuilder::attach_rowset_to_txn(const RowsetSharedPtr& rowset) { |
554 | 0 | if (!is_data_builder()) { |
555 | 0 | return Status::RuntimeError("the rowset isn't allowed to manage txn"); |
556 | 0 | } |
557 | 0 | _attach_rowsets.push_back(rowset); |
558 | 0 | return Status::OK(); |
559 | 0 | } |
560 | | |
561 | 1 | Status BaseRowsetBuilder::attach_pending_rs_guard_to_txn(const RowsetId& rowset_id) { |
562 | 1 | if (!is_data_builder()) { |
563 | 0 | return Status::RuntimeError("the rowset isn't allowed to manage txn"); |
564 | 0 | } |
565 | 1 | _attach_rowset_ids.push_back(rowset_id); |
566 | 1 | return Status::OK(); |
567 | 1 | } |
568 | | |
569 | | } // namespace doris |