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("RowsetBuilder {} {}", _req.tablet_id, |
92 | 31 | _req.write_req_type == WriteRequestType::BINLOG_IN_GROUP ? "binlog<row>" |
93 | 31 | : "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 | 216 | Tablet* RowsetBuilder::tablet() { |
124 | 216 | return static_cast<Tablet*>(_tablet.get()); |
125 | 216 | } |
126 | | |
127 | 7 | void RowsetBuilder::_garbage_collection(bool cancel_txn) { |
128 | 7 | Status rollback_status; |
129 | 7 | bool need_clean = true; |
130 | 7 | if (tablet() != nullptr && cancel_txn) { |
131 | 6 | TxnManager* txn_mgr = _engine.txn_manager(); |
132 | 6 | rollback_status = txn_mgr->rollback_txn(_req.partition_id, *tablet(), _req.txn_id); |
133 | 6 | need_clean = rollback_status.ok(); |
134 | 6 | } |
135 | | // has to check rollback status, because the rowset maybe committed in this thread and |
136 | | // published in another thread, then rollback will fail. |
137 | | // when rollback failed should not delete rowset |
138 | 7 | if (need_clean) { |
139 | 7 | _engine.add_unused_rowset(_rowset); |
140 | 7 | for (auto& rs : _attach_rowsets) { |
141 | 0 | _engine.add_unused_rowset(rs); |
142 | 0 | } |
143 | 7 | } |
144 | 7 | } |
145 | | |
146 | 4 | Status BaseRowsetBuilder::init_mow_context(std::shared_ptr<MowContext>& mow_context) { |
147 | 4 | DCHECK(is_data_builder()); |
148 | | |
149 | 4 | std::lock_guard<std::shared_mutex> lck(tablet()->get_header_lock()); |
150 | 4 | _max_version_in_flush_phase = tablet()->max_version_unlocked(); |
151 | 4 | std::vector<RowsetSharedPtr> rowset_ptrs; |
152 | | // tablet is under alter process. The delete bitmap will be calculated after conversion. |
153 | 4 | if (tablet()->tablet_state() == TABLET_NOTREADY) { |
154 | | // Disable 'partial_update' when the tablet is undergoing a 'schema changing process' |
155 | 0 | if (_req.table_schema_param->is_partial_update()) { |
156 | 0 | return Status::InternalError( |
157 | 0 | "Unable to do 'partial_update' when " |
158 | 0 | "the tablet is undergoing a 'schema changing process'"); |
159 | 0 | } |
160 | 0 | _rowset_ids->clear(); |
161 | 4 | } else { |
162 | 4 | RETURN_IF_ERROR( |
163 | 4 | tablet()->get_all_rs_id_unlocked(_max_version_in_flush_phase, _rowset_ids.get())); |
164 | 4 | rowset_ptrs = tablet()->get_rowset_by_ids(_rowset_ids.get()); |
165 | 4 | } |
166 | 4 | _delete_bitmap = std::make_shared<DeleteBitmap>(tablet()->tablet_id()); |
167 | 4 | mow_context = std::make_shared<MowContext>(_max_version_in_flush_phase, _req.txn_id, |
168 | 4 | _rowset_ids, rowset_ptrs, _delete_bitmap); |
169 | 4 | return Status::OK(); |
170 | 4 | } |
171 | | |
172 | 29 | Status RowsetBuilder::check_tablet_version_count() { |
173 | 29 | DCHECK(is_data_builder()); |
174 | | |
175 | 29 | auto max_version_config = _tablet->max_version_config(); |
176 | 29 | auto version_count = tablet()->version_count(); |
177 | 29 | DBUG_EXECUTE_IF("RowsetBuilder.check_tablet_version_count.too_many_version", |
178 | 29 | { version_count = INT_MAX; }); |
179 | | // Trigger TOO MANY VERSION error first |
180 | 29 | if (version_count > max_version_config) { |
181 | 0 | return Status::Error<TOO_MANY_VERSION>( |
182 | 0 | "failed to init rowset builder. version count: {}, exceed limit: {}, " |
183 | 0 | "tablet: {}. Please reduce the frequency of loading data or adjust the " |
184 | 0 | "max_tablet_version_num or time_series_max_tablet_version_num in be.conf to a " |
185 | 0 | "larger value.", |
186 | 0 | version_count, max_version_config, _tablet->tablet_id()); |
187 | 0 | } |
188 | | // (TODO Refrain) Maybe we can use a configurable param instead of hardcoded values '100'. |
189 | | // max_version_config must > 100, otherwise silent errors will occur. |
190 | 29 | if ((!config::disable_auto_compaction && |
191 | 29 | !_tablet->tablet_meta()->tablet_schema()->disable_auto_compaction()) && |
192 | 29 | (version_count > max_version_config - 100) && |
193 | 29 | !GlobalMemoryArbitrator::is_exceed_soft_mem_limit(GB_EXCHANGE_BYTE)) { |
194 | | // Trigger compaction |
195 | 0 | auto st = _engine.submit_compaction_task(std::static_pointer_cast<Tablet>(tablet_sptr()), |
196 | 0 | CompactionType::CUMULATIVE_COMPACTION, true, true, |
197 | 0 | 2); |
198 | 0 | if (!st.ok()) [[unlikely]] { |
199 | 0 | LOG(WARNING) << "failed to trigger compaction, tablet_id=" << _tablet->tablet_id() |
200 | 0 | << " : " << st; |
201 | 0 | } |
202 | 0 | } |
203 | 29 | return Status::OK(); |
204 | 29 | } |
205 | | |
206 | 29 | Status RowsetBuilder::prepare_txn() { |
207 | 29 | DCHECK(is_data_builder()); |
208 | 29 | return tablet()->prepare_txn(_req.partition_id, _req.txn_id, _req.load_id, false); |
209 | 29 | } |
210 | | |
211 | 30 | Status RowsetBuilder::init() { |
212 | 30 | RowsetWriterContext context; |
213 | | |
214 | 30 | RETURN_IF_ERROR(_init_context_common_fields(context)); |
215 | | |
216 | | // For group write, the DATA writer needs to mark itself as primary so that |
217 | | // SegmentFlusher can create RowBinlogSegmentWriter and the flush path can |
218 | | // pre-allocate LSNs for row-binlog. |
219 | 29 | if (_req.write_req_type == WriteRequestType::DATA_IN_GROUP) { |
220 | 0 | context.write_binlog_opt().mark_primary_writer(); |
221 | 0 | } |
222 | | |
223 | 29 | std::shared_ptr<MowContext> mow_context; |
224 | 29 | if (_tablet->enable_unique_key_merge_on_write()) { |
225 | 4 | RETURN_IF_ERROR(init_mow_context(mow_context)); |
226 | 4 | } |
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.db_id = _req.table_schema_param->db_id(); |
276 | 30 | context.table_id = _req.table_schema_param->table_id(); |
277 | 30 | context.rowset_state = PREPARED; |
278 | 30 | context.segments_overlap = OVERLAPPING; |
279 | 30 | context.tablet_schema = _tablet_schema; |
280 | 30 | context.newest_write_timestamp = UnixSeconds(); |
281 | 30 | context.tablet_id = _req.tablet_id; |
282 | 30 | context.index_id = _req.index_id; |
283 | 30 | context.tablet = _tablet; |
284 | 30 | context.enable_segcompaction = !_req.table_schema_param->is_partial_update(); |
285 | 30 | if (_req.write_req_type == WriteRequestType::BINLOG_IN_GROUP || |
286 | 30 | _req.write_req_type == WriteRequestType::DATA_IN_GROUP) { |
287 | 1 | context.enable_segcompaction = false; |
288 | 1 | } |
289 | 30 | context.write_type = DataWriteType::TYPE_DIRECT; |
290 | 30 | context.write_file_cache = _req.write_file_cache; |
291 | | |
292 | 30 | return Status::OK(); |
293 | 31 | } |
294 | | |
295 | 25 | Status BaseRowsetBuilder::build_rowset() { |
296 | 25 | std::lock_guard<std::mutex> l(_lock); |
297 | 25 | DCHECK(_is_init) << "rowset builder is supposed be to initialized before " |
298 | 0 | "build_rowset() being called"; |
299 | | |
300 | 25 | SCOPED_TIMER(_build_rowset_timer); |
301 | | // use rowset meta manager to save meta |
302 | 25 | RETURN_NOT_OK_STATUS_WITH_WARN(_rowset_writer->build(_rowset), "fail to build rowset"); |
303 | 25 | return Status::OK(); |
304 | 25 | } |
305 | | |
306 | 1 | Status GroupRowsetBuilder::build_rowset() { |
307 | | // build binlog rowset first, then data rowset |
308 | | // If data rowset build fails after binlog has built some segments, we rely on the |
309 | | // RowsetBuilder/StorageEngine garbage-collection paths to clean up built-but-uncommitted files. |
310 | 1 | RETURN_IF_ERROR(_row_binlog_rowset_builder->build_rowset()); |
311 | 1 | return _txn_rs_builder->build_rowset(); |
312 | 1 | } |
313 | | |
314 | 17 | Status BaseRowsetBuilder::submit_calc_delete_bitmap_task() { |
315 | 17 | DCHECK(is_data_builder()); |
316 | 17 | if (!_tablet->enable_unique_key_merge_on_write() || _rowset->num_segments() == 0) { |
317 | 14 | return Status::OK(); |
318 | 14 | } |
319 | 3 | std::lock_guard<std::mutex> l(_lock); |
320 | 3 | SCOPED_TIMER(_submit_delete_bitmap_timer); |
321 | 3 | if (_partial_update_info && _partial_update_info->is_flexible_partial_update()) { |
322 | 0 | if (_rowset->num_segments() > 1) { |
323 | | // in flexible partial update, when there are more one segment in one load, |
324 | | // we need to do alignment process for same keys between segments, we haven't |
325 | | // implemented it yet and just report an error when encouter this situation |
326 | 0 | return Status::NotSupported( |
327 | 0 | "too large input data in flexible partial update, Please " |
328 | 0 | "reduce the amount of data imported in a single load."); |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | 3 | auto* beta_rowset = reinterpret_cast<BetaRowset*>(_rowset.get()); |
333 | 3 | std::vector<segment_v2::SegmentSharedPtr> segments; |
334 | 3 | RETURN_IF_ERROR(beta_rowset->load_segments(&segments)); |
335 | 3 | if (segments.size() > 1) { |
336 | | // calculate delete bitmap between segments |
337 | 0 | if (config::enable_calc_delete_bitmap_between_segments_concurrently) { |
338 | 0 | RETURN_IF_ERROR(_calc_delete_bitmap_token->submit( |
339 | 0 | _tablet, _tablet_schema, _rowset->rowset_id(), segments, _delete_bitmap)); |
340 | 0 | } else { |
341 | 0 | RETURN_IF_ERROR(_tablet->calc_delete_bitmap_between_segments( |
342 | 0 | _tablet_schema, _rowset->rowset_id(), segments, _delete_bitmap)); |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | // For partial update, we need to fill in the entire row of data, during the calculation |
347 | | // of the delete bitmap. This operation is resource-intensive, and we need to minimize |
348 | | // the number of times it occurs. Therefore, we skip this operation here. |
349 | 3 | if (_partial_update_info->is_partial_update()) { |
350 | | // for partial update, the delete bitmap calculation is done while append_block() |
351 | | // we print it's summarize logs here before commit. |
352 | 0 | LOG(INFO) << fmt::format( |
353 | 0 | "{} calc delete bitmap summary before commit: tablet({}), txn_id({}), " |
354 | 0 | "rowset_ids({}), cur max_version({}), bitmap num({}), bitmap_cardinality({}), num " |
355 | 0 | "rows updated({}), num rows new added({}), num rows deleted({}), total rows({})", |
356 | 0 | _partial_update_info->partial_update_mode_str(), tablet()->tablet_id(), _req.txn_id, |
357 | 0 | _rowset_ids->size(), rowset_writer()->context().mow_context->max_version, |
358 | 0 | _delete_bitmap->get_delete_bitmap_count(), _delete_bitmap->cardinality(), |
359 | 0 | rowset_writer()->num_rows_updated(), rowset_writer()->num_rows_new_added(), |
360 | 0 | rowset_writer()->num_rows_deleted(), rowset_writer()->num_rows()); |
361 | 0 | return Status::OK(); |
362 | 0 | } |
363 | | |
364 | 3 | LOG(INFO) << "submit calc delete bitmap task to executor, tablet_id: " << tablet()->tablet_id() |
365 | 3 | << ", txn_id: " << _req.txn_id; |
366 | 3 | return BaseTablet::commit_phase_update_delete_bitmap(_tablet, _rowset, *_rowset_ids, |
367 | 3 | _delete_bitmap, segments, _req.txn_id, |
368 | 3 | _calc_delete_bitmap_token.get(), nullptr); |
369 | 3 | } |
370 | | |
371 | 17 | Status BaseRowsetBuilder::wait_calc_delete_bitmap() { |
372 | 17 | DCHECK(is_data_builder()); |
373 | 17 | if (!_tablet->enable_unique_key_merge_on_write() || _partial_update_info->is_partial_update()) { |
374 | 14 | return Status::OK(); |
375 | 14 | } |
376 | 3 | std::lock_guard<std::mutex> l(_lock); |
377 | 3 | SCOPED_TIMER(_wait_delete_bitmap_timer); |
378 | 3 | RETURN_IF_ERROR(_calc_delete_bitmap_token->wait()); |
379 | 3 | return Status::OK(); |
380 | 3 | } |
381 | | |
382 | 23 | Status RowsetBuilder::commit_txn() { |
383 | 23 | DCHECK(is_data_builder()); |
384 | 23 | if (tablet()->enable_unique_key_merge_on_write() && |
385 | 23 | config::enable_merge_on_write_correctness_check && _rowset->num_rows() != 0 && |
386 | 23 | tablet()->tablet_state() != TABLET_NOTREADY) { |
387 | 3 | auto st = tablet()->check_delete_bitmap_correctness( |
388 | 3 | _delete_bitmap, _rowset->end_version() - 1, _req.txn_id, *_rowset_ids); |
389 | 3 | if (!st.ok()) { |
390 | 0 | LOG(WARNING) << fmt::format( |
391 | 0 | "[tablet_id:{}][txn_id:{}][load_id:{}][partition_id:{}] " |
392 | 0 | "delete bitmap correctness check failed in commit phase!", |
393 | 0 | _req.tablet_id, _req.txn_id, UniqueId(_req.load_id).to_string(), |
394 | 0 | _req.partition_id); |
395 | 0 | return st; |
396 | 0 | } |
397 | 3 | } |
398 | 23 | std::lock_guard<std::mutex> l(_lock); |
399 | 23 | SCOPED_TIMER(_commit_txn_timer); |
400 | | |
401 | | // Transfer ownership of `PendingRowsetGuard` to `TxnManager` |
402 | 23 | Status res = _engine.txn_manager()->commit_txn( |
403 | 23 | _req.partition_id, *tablet(), _req.txn_id, _req.load_id, _rowset, |
404 | 23 | std::move(_pending_rs_guard), false, _partial_update_info, &_attach_rowsets); |
405 | | |
406 | 23 | if (!res && !res.is<PUSH_TRANSACTION_ALREADY_EXIST>()) { |
407 | 0 | LOG(WARNING) << "Failed to commit txn: " << _req.txn_id |
408 | 0 | << " for rowset: " << _rowset->rowset_id(); |
409 | 0 | return res; |
410 | 0 | } |
411 | 23 | if (_tablet->enable_unique_key_merge_on_write()) { |
412 | | // no need to update binlog_delvec, it'll be updated in publish phase |
413 | 3 | _engine.txn_manager()->set_txn_related_delete_bitmap( |
414 | 3 | _req.partition_id, _req.txn_id, tablet()->tablet_id(), tablet()->tablet_uid(), true, |
415 | 3 | _delete_bitmap, *_rowset_ids, _partial_update_info); |
416 | 3 | } |
417 | | |
418 | 23 | _is_committed = true; |
419 | 23 | return Status::OK(); |
420 | 23 | } |
421 | | |
422 | 0 | Status BaseRowsetBuilder::cancel() { |
423 | 0 | std::lock_guard<std::mutex> l(_lock); |
424 | 0 | if (_is_cancelled) { |
425 | 0 | return Status::OK(); |
426 | 0 | } |
427 | 0 | if (_calc_delete_bitmap_token != nullptr) { |
428 | 0 | _calc_delete_bitmap_token->cancel(); |
429 | 0 | } |
430 | 0 | _is_cancelled = true; |
431 | 0 | return Status::OK(); |
432 | 0 | } |
433 | | |
434 | | Status BaseRowsetBuilder::_build_current_tablet_schema( |
435 | | int64_t index_id, const OlapTableSchemaParam* table_schema_param, |
436 | 30 | const TabletSchema& ori_tablet_schema) { |
437 | 30 | const OlapTableIndexSchema* index_schema = nullptr; |
438 | 30 | if (_req.write_req_type == WriteRequestType::BINLOG_IN_GROUP) { |
439 | 1 | const auto* row_binlog_index_schema = table_schema_param->row_binlog_index_schema(); |
440 | 1 | DCHECK(row_binlog_index_schema != nullptr); |
441 | 1 | DCHECK_EQ(row_binlog_index_schema->index_id, index_id); |
442 | 1 | index_schema = row_binlog_index_schema; |
443 | 29 | } else { |
444 | 29 | for (const auto* schema : table_schema_param->indexes()) { |
445 | 14 | if (schema->index_id == index_id) { |
446 | 14 | index_schema = schema; |
447 | 14 | break; |
448 | 14 | } |
449 | 14 | } |
450 | 29 | } |
451 | | |
452 | 30 | if (index_schema != nullptr && !index_schema->columns.empty() && |
453 | 30 | index_schema->columns[0]->unique_id() >= 0) { |
454 | 0 | _tablet_schema->shawdow_copy_without_columns(ori_tablet_schema); |
455 | 0 | _tablet_schema->build_current_tablet_schema( |
456 | 0 | index_id, cast_set<int32_t>(table_schema_param->version()), index_schema, |
457 | 0 | ori_tablet_schema); |
458 | 30 | } else { |
459 | 30 | _tablet_schema->copy_from(ori_tablet_schema); |
460 | 30 | } |
461 | 30 | if (_tablet_schema->schema_version() > ori_tablet_schema.schema_version()) { |
462 | | // After schema change, should include extracted column |
463 | | // For example: a table has two columns, k and v |
464 | | // After adding a column v2, the schema version increases, max_version_schema needs to be updated. |
465 | | // _tablet_schema includes k, v, and v2 |
466 | | // if v is a variant, need to add the columns decomposed from the v to the _tablet_schema. |
467 | 0 | if (is_data_builder()) { |
468 | 0 | if (_tablet_schema->num_variant_columns() > 0) { |
469 | 0 | TabletSchemaSPtr max_version_schema = std::make_shared<TabletSchema>(); |
470 | 0 | max_version_schema->copy_from(*_tablet_schema); |
471 | 0 | max_version_schema->copy_extracted_columns(ori_tablet_schema); |
472 | 0 | _tablet->update_max_version_schema(max_version_schema); |
473 | 0 | } else { |
474 | 0 | _tablet->update_max_version_schema(_tablet_schema); |
475 | 0 | } |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | 30 | _tablet_schema->set_table_id(table_schema_param->table_id()); |
480 | 30 | _tablet_schema->set_db_id(table_schema_param->db_id()); |
481 | 30 | if (table_schema_param->is_partial_update()) { |
482 | 0 | _tablet_schema->set_auto_increment_column(table_schema_param->auto_increment_coulumn()); |
483 | 0 | } |
484 | | // set partial update columns info |
485 | 30 | if (is_data_builder()) { |
486 | 29 | _partial_update_info = std::make_shared<PartialUpdateInfo>(); |
487 | 29 | RETURN_IF_ERROR(_partial_update_info->init( |
488 | 29 | tablet()->tablet_id(), _req.txn_id, *_tablet_schema, |
489 | 29 | table_schema_param->unique_key_update_mode(), |
490 | 29 | table_schema_param->partial_update_new_key_policy(), |
491 | 29 | table_schema_param->partial_update_input_columns(), |
492 | 29 | table_schema_param->is_strict_mode(), table_schema_param->timestamp_ms(), |
493 | 29 | table_schema_param->nano_seconds(), table_schema_param->timezone(), |
494 | 29 | table_schema_param->auto_increment_coulumn(), |
495 | 29 | table_schema_param->sequence_map_col_uid(), _max_version_in_flush_phase)); |
496 | 29 | } |
497 | 30 | return Status::OK(); |
498 | 30 | } |
499 | | |
500 | | GroupRowsetBuilder::GroupRowsetBuilder(StorageEngine& engine, const WriteRequest& req, |
501 | | const WriteRequest& row_binlog_req, RuntimeProfile* profile) |
502 | 1 | : BaseRowsetBuilder( |
503 | 1 | [](int64_t tablet_id) { |
504 | 1 | WriteRequest group_req; |
505 | 1 | group_req.tablet_id = tablet_id; |
506 | 1 | group_req.write_req_type = WriteRequestType::GROUP; |
507 | 1 | return group_req; |
508 | 1 | }(req.tablet_id), |
509 | 1 | profile) { |
510 | 1 | _row_binlog_rowset_builder = |
511 | 1 | std::make_shared<RowBinlogRowsetBuilder>(engine, row_binlog_req, profile); |
512 | 1 | _txn_rs_builder = std::make_shared<RowsetBuilder>(engine, req, profile); |
513 | 1 | } |
514 | | |
515 | 1 | Status GroupRowsetBuilder::init() { |
516 | | // init binlog builder first so that its rowset id can be added into |
517 | | // PendingLocalRowsets before txn builder init. |
518 | 1 | RETURN_IF_ERROR(_row_binlog_rowset_builder->init()); |
519 | | // before init txn, need to add all rowset_ids into PendingLocalRowsets. |
520 | | // see https://github.com/apache/doris/pull/25921 |
521 | 1 | RETURN_IF_ERROR(_txn_rs_builder->attach_pending_rs_guard_to_txn( |
522 | 1 | _row_binlog_rowset_builder->rowset_id())); |
523 | 1 | RETURN_IF_ERROR(_txn_rs_builder->init()); |
524 | | |
525 | | // Create a GroupRowsetWriter that forwards flush to both underlying |
526 | | // RowsetWriters. |
527 | 1 | std::unique_ptr<doris::GroupRowsetWriter> group_writer; |
528 | 1 | RETURN_IF_ERROR(RowsetFactory::create_empty_group_rowset_writer(&group_writer)); |
529 | 1 | group_writer->set_data_writer(_txn_rs_builder->rowset_writer()); |
530 | 1 | group_writer->set_row_binlog_writer(_row_binlog_rowset_builder->rowset_writer()); |
531 | | |
532 | 1 | { |
533 | 1 | const auto& data_ctx = _txn_rs_builder->rowset_writer()->context(); |
534 | 1 | auto& binlog_ctx = const_cast<RowsetWriterContext&>( |
535 | 1 | _row_binlog_rowset_builder->rowset_writer()->context()); |
536 | 1 | auto& cfg = binlog_ctx.write_binlog_opt().write_binlog_config(); |
537 | 1 | cfg.source.tablet_schema = data_ctx.tablet_schema; |
538 | 1 | cfg.source.partial_update_info = data_ctx.partial_update_info; |
539 | 1 | cfg.source.mow_context = data_ctx.mow_context; |
540 | 1 | cfg.source.is_transient_rowset_writer = data_ctx.is_transient_rowset_writer; |
541 | 1 | cfg.source.source_write_type = data_ctx.write_type; |
542 | 1 | } |
543 | | |
544 | 1 | _rowset_writer = std::move(group_writer); |
545 | 1 | _is_init = true; |
546 | 1 | return Status::OK(); |
547 | 1 | } |
548 | | |
549 | 0 | Status GroupRowsetBuilder::submit_calc_delete_bitmap_task() { |
550 | 0 | return _txn_rs_builder->submit_calc_delete_bitmap_task(); |
551 | 0 | } |
552 | | |
553 | 0 | Status GroupRowsetBuilder::wait_calc_delete_bitmap() { |
554 | 0 | return _txn_rs_builder->wait_calc_delete_bitmap(); |
555 | 0 | } |
556 | | |
557 | 0 | Status GroupRowsetBuilder::commit_txn() { |
558 | | // Attach binlog rowset to txn rowset, so that commit/rollback and |
559 | | // clean-up are all handled by txn rowset builder. |
560 | 0 | RETURN_IF_ERROR(_txn_rs_builder->attach_rowset_to_txn(_row_binlog_rowset_builder->rowset())); |
561 | 0 | auto st = _txn_rs_builder->commit_txn(); |
562 | 0 | if (st.ok()) { |
563 | | // Avoid RowBinlogRowsetBuilder being cleaned in its base dtor. |
564 | 0 | RETURN_IF_ERROR(_row_binlog_rowset_builder->commit_txn()); |
565 | 0 | } |
566 | 0 | return st; |
567 | 0 | } |
568 | | |
569 | 1 | Status RowBinlogRowsetBuilder::init() { |
570 | 1 | RowsetWriterContext context; |
571 | | |
572 | 1 | RETURN_IF_ERROR(_init_context_common_fields(context)); |
573 | | |
574 | | // build tablet schema in request level |
575 | 1 | RETURN_IF_ERROR(_build_current_tablet_schema( |
576 | 1 | _req.index_id, _req.table_schema_param.get(), |
577 | 1 | *std::dynamic_pointer_cast<Tablet>(_tablet)->row_binlog_tablet_schema())); |
578 | 1 | context.write_binlog_opt().mark_binlog_writer(); |
579 | 1 | DCHECK(context.write_binlog_opt().is_binlog_writer()); |
580 | | |
581 | 1 | _rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false)); |
582 | | // need to attach PendingRowsetGuard after txn_rs_builder init |
583 | 1 | _rowset_id = context.rowset_id; |
584 | | |
585 | 1 | _is_init = true; |
586 | 1 | return Status::OK(); |
587 | 1 | } |
588 | | |
589 | 0 | Status BaseRowsetBuilder::attach_rowset_to_txn(const RowsetSharedPtr& rowset) { |
590 | 0 | if (!is_data_builder()) { |
591 | 0 | return Status::RuntimeError("the rowset isn't allowed to manage txn"); |
592 | 0 | } |
593 | 0 | _attach_rowsets.push_back(rowset); |
594 | 0 | return Status::OK(); |
595 | 0 | } |
596 | | |
597 | 1 | Status BaseRowsetBuilder::attach_pending_rs_guard_to_txn(const RowsetId& rowset_id) { |
598 | 1 | if (!is_data_builder()) { |
599 | 0 | return Status::RuntimeError("the rowset isn't allowed to manage txn"); |
600 | 0 | } |
601 | 1 | _attach_rowset_ids.push_back(rowset_id); |
602 | 1 | return Status::OK(); |
603 | 1 | } |
604 | | |
605 | | } // namespace doris |