Coverage Report

Created: 2026-05-08 14:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
277k
        : _req(req), _tablet_schema(std::make_shared<TabletSchema>()) {
69
277k
    if (profile != nullptr) {
70
13.9k
        _init_profile(profile);
71
13.9k
    }
72
277k
}
73
74
RowsetBuilder::RowsetBuilder(StorageEngine& engine, const WriteRequest& req,
75
                             RuntimeProfile* profile)
76
17.1k
        : 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
13.9k
void BaseRowsetBuilder::_init_profile(RuntimeProfile* profile) {
83
13.9k
    DCHECK(profile != nullptr);
84
13.9k
    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
13.9k
    _profile = profile->create_child(
91
13.9k
            fmt::format(
92
13.9k
                    "RowsetBuilder {} {}", _req.tablet_id,
93
13.9k
                    _req.write_req_type == WriteRequestType::ROW_BINLOG ? "row_binlog" : "data"),
94
13.9k
            true, true);
95
13.9k
    _build_rowset_timer = ADD_TIMER(_profile, "BuildRowsetTime");
96
13.9k
    _submit_delete_bitmap_timer = ADD_TIMER(_profile, "DeleteBitmapSubmitTime");
97
13.9k
    _wait_delete_bitmap_timer = ADD_TIMER(_profile, "DeleteBitmapWaitTime");
98
13.9k
}
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
278k
BaseRowsetBuilder::~BaseRowsetBuilder() {
107
278k
    if (!_is_init) {
108
90.1k
        return;
109
90.1k
    }
110
111
187k
    if (_calc_delete_bitmap_token != nullptr) {
112
187k
        _calc_delete_bitmap_token->cancel();
113
187k
    }
114
187k
}
115
116
17.1k
RowsetBuilder::~RowsetBuilder() {
117
17.1k
    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
17.1k
}
122
123
145k
Tablet* RowsetBuilder::tablet() {
124
145k
    return static_cast<Tablet*>(_tablet.get());
125
145k
}
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
57.1k
Status BaseRowsetBuilder::init_mow_context(std::shared_ptr<MowContext>& mow_context) {
151
57.1k
    DCHECK(is_data_builder());
152
153
57.1k
    std::lock_guard<std::shared_mutex> lck(tablet()->get_header_lock());
154
57.1k
    _max_version_in_flush_phase = tablet()->max_version_unlocked();
155
57.1k
    std::vector<RowsetSharedPtr> rowset_ptrs;
156
    // tablet is under alter process. The delete bitmap will be calculated after conversion.
157
57.1k
    if (tablet()->tablet_state() == TABLET_NOTREADY) {
158
        // Disable 'partial_update' when the tablet is undergoing a 'schema changing process'
159
33
        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
33
        _rowset_ids->clear();
165
57.0k
    } else {
166
57.0k
        RETURN_IF_ERROR(
167
57.0k
                tablet()->get_all_rs_id_unlocked(_max_version_in_flush_phase, _rowset_ids.get()));
168
57.0k
        rowset_ptrs = tablet()->get_rowset_by_ids(_rowset_ids.get());
169
57.0k
    }
170
57.1k
    _delete_bitmap = std::make_shared<DeleteBitmap>(tablet()->tablet_id());
171
57.1k
    mow_context = std::make_shared<MowContext>(_max_version_in_flush_phase, _req.txn_id,
172
57.1k
                                               _rowset_ids, rowset_ptrs, _delete_bitmap);
173
57.1k
    return Status::OK();
174
57.1k
}
175
176
16.1k
Status RowsetBuilder::check_tablet_version_count() {
177
16.1k
    DCHECK(is_data_builder());
178
179
16.1k
    auto max_version_config = _tablet->max_version_config();
180
16.1k
    auto version_count = tablet()->version_count();
181
16.1k
    DBUG_EXECUTE_IF("RowsetBuilder.check_tablet_version_count.too_many_version",
182
16.1k
                    { version_count = INT_MAX; });
183
    // Trigger TOO MANY VERSION error first
184
16.1k
    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
16.1k
    if ((!config::disable_auto_compaction &&
195
16.1k
         !_tablet->tablet_meta()->tablet_schema()->disable_auto_compaction()) &&
196
16.1k
        (version_count > max_version_config - 100) &&
197
16.1k
        !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
16.1k
    return Status::OK();
207
16.1k
}
208
209
16.1k
Status RowsetBuilder::prepare_txn() {
210
16.1k
    DCHECK(is_data_builder());
211
16.1k
    return tablet()->prepare_txn(_req.partition_id, _req.txn_id, _req.load_id, false);
212
16.1k
}
213
214
16.1k
Status RowsetBuilder::init() {
215
16.1k
    RowsetWriterContext context;
216
217
16.1k
    RETURN_IF_ERROR(_init_context_common_fields(context));
218
219
16.1k
    if (tablet()->enable_row_binlog()) {
220
1
        context.write_binlog_opt().mark_primary_writer();
221
1
    }
222
223
16.1k
    std::shared_ptr<MowContext> mow_context;
224
16.1k
    if (_tablet->enable_unique_key_merge_on_write()) {
225
6.01k
        RETURN_IF_ERROR(init_mow_context(mow_context));
226
6.01k
    }
227
228
16.1k
    RETURN_IF_ERROR(check_tablet_version_count());
229
230
16.1k
    auto version_count = tablet()->version_count() + tablet()->stale_version_count();
231
16.1k
    if (tablet()->avg_rs_meta_serialize_size() * version_count >
232
16.1k
        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
16.1k
    RETURN_IF_ERROR(prepare_txn());
242
243
16.1k
    DBUG_EXECUTE_IF("BaseRowsetBuilder::init.check_partial_update_column_num", {
244
16.1k
        if (_req.table_schema_param->partial_update_input_columns().size() !=
245
16.1k
            dp->param<int>("column_num")) {
246
16.1k
            return Status::InternalError("partial update input column num wrong!");
247
16.1k
        };
248
16.1k
    })
249
    // build tablet schema in request level
250
16.1k
    RETURN_IF_ERROR(_build_current_tablet_schema(_req.index_id, _req.table_schema_param.get(),
251
16.1k
                                                 *_tablet->tablet_schema()));
252
253
16.1k
    context.mow_context = mow_context;
254
255
16.1k
    context.partial_update_info = _partial_update_info;
256
16.1k
    _rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false));
257
16.1k
    _rowset_id = context.rowset_id;
258
16.1k
    std::vector<RowsetId> tmp_pending_rowset_ids = {_rowset_id};
259
16.1k
    tmp_pending_rowset_ids.resize(1 + _attach_rowset_ids.size());
260
16.1k
    std::copy(_attach_rowset_ids.begin(), _attach_rowset_ids.end(),
261
16.1k
              tmp_pending_rowset_ids.begin() + 1);
262
16.1k
    _pending_rs_guard = _engine.pending_local_rowsets().add(tmp_pending_rowset_ids);
263
264
16.1k
    _calc_delete_bitmap_token = _engine.calc_delete_bitmap_executor()->create_token();
265
266
16.1k
    _is_init = true;
267
16.1k
    return Status::OK();
268
16.1k
}
269
270
16.1k
Status BaseRowsetBuilder::_init_context_common_fields(RowsetWriterContext& context) {
271
16.1k
    _tablet = DORIS_TRY(ExecEnv::get_tablet(_req.tablet_id));
272
273
16.1k
    context.txn_id = _req.txn_id;
274
16.1k
    context.load_id = _req.load_id;
275
16.1k
    context.rowset_state = PREPARED;
276
16.1k
    context.segments_overlap = OVERLAPPING;
277
16.1k
    context.tablet_schema = _tablet_schema;
278
16.1k
    context.newest_write_timestamp = UnixSeconds();
279
16.1k
    context.tablet_id = _req.tablet_id;
280
16.1k
    context.index_id = _req.index_id;
281
16.1k
    context.tablet = _tablet;
282
16.1k
    context.enable_segcompaction = true;
283
16.1k
    context.write_type = DataWriteType::TYPE_DIRECT;
284
16.1k
    context.write_file_cache = _req.write_file_cache;
285
286
16.1k
    return Status::OK();
287
16.1k
}
288
289
187k
Status BaseRowsetBuilder::build_rowset() {
290
187k
    std::lock_guard<std::mutex> l(_lock);
291
18.4E
    DCHECK(_is_init) << "rowset builder is supposed be to initialized before "
292
18.4E
                        "build_rowset() being called";
293
294
187k
    SCOPED_TIMER(_build_rowset_timer);
295
    // use rowset meta manager to save meta
296
187k
    RETURN_NOT_OK_STATUS_WITH_WARN(_rowset_writer->build(_rowset), "fail to build rowset");
297
187k
    return Status::OK();
298
187k
}
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
187k
Status BaseRowsetBuilder::submit_calc_delete_bitmap_task() {
307
187k
    DCHECK(is_data_builder());
308
187k
    if (!_tablet->enable_unique_key_merge_on_write() || _rowset->num_segments() == 0) {
309
164k
        return Status::OK();
310
164k
    }
311
22.7k
    std::lock_guard<std::mutex> l(_lock);
312
22.7k
    SCOPED_TIMER(_submit_delete_bitmap_timer);
313
22.7k
    if (_partial_update_info && _partial_update_info->is_flexible_partial_update()) {
314
264
        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
264
    }
323
324
22.7k
    auto* beta_rowset = reinterpret_cast<BetaRowset*>(_rowset.get());
325
22.7k
    std::vector<segment_v2::SegmentSharedPtr> segments;
326
22.7k
    RETURN_IF_ERROR(beta_rowset->load_segments(&segments));
327
22.7k
    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
22.7k
    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
1.60k
        LOG(INFO) << fmt::format(
345
1.60k
                "{} calc delete bitmap summary before commit: tablet({}), txn_id({}), "
346
1.60k
                "rowset_ids({}), cur max_version({}), bitmap num({}), bitmap_cardinality({}), num "
347
1.60k
                "rows updated({}), num rows new added({}), num rows deleted({}), total rows({})",
348
1.60k
                _partial_update_info->partial_update_mode_str(), tablet()->tablet_id(), _req.txn_id,
349
1.60k
                _rowset_ids->size(), rowset_writer()->context().mow_context->max_version,
350
1.60k
                _delete_bitmap->get_delete_bitmap_count(), _delete_bitmap->cardinality(),
351
1.60k
                rowset_writer()->num_rows_updated(), rowset_writer()->num_rows_new_added(),
352
1.60k
                rowset_writer()->num_rows_deleted(), rowset_writer()->num_rows());
353
1.60k
        return Status::OK();
354
1.60k
    }
355
356
22.7k
    LOG(INFO) << "submit calc delete bitmap task to executor, tablet_id: " << tablet()->tablet_id()
357
21.1k
              << ", txn_id: " << _req.txn_id;
358
21.1k
    return BaseTablet::commit_phase_update_delete_bitmap(_tablet, _rowset, *_rowset_ids,
359
21.1k
                                                         _delete_bitmap, segments, _req.txn_id,
360
21.1k
                                                         _calc_delete_bitmap_token.get(), nullptr);
361
22.7k
}
362
363
187k
Status BaseRowsetBuilder::wait_calc_delete_bitmap() {
364
187k
    DCHECK(is_data_builder());
365
187k
    if (!_tablet->enable_unique_key_merge_on_write() || _partial_update_info->is_partial_update()) {
366
134k
        return Status::OK();
367
134k
    }
368
53.6k
    std::lock_guard<std::mutex> l(_lock);
369
53.6k
    SCOPED_TIMER(_wait_delete_bitmap_timer);
370
53.6k
    RETURN_IF_ERROR(_calc_delete_bitmap_token->wait());
371
53.6k
    return Status::OK();
372
53.6k
}
373
374
16.1k
Status RowsetBuilder::commit_txn() {
375
16.1k
    DCHECK(is_data_builder());
376
16.1k
    if (tablet()->enable_unique_key_merge_on_write() &&
377
16.1k
        config::enable_merge_on_write_correctness_check && _rowset->num_rows() != 0 &&
378
16.1k
        tablet()->tablet_state() != TABLET_NOTREADY) {
379
2.08k
        auto st = tablet()->check_delete_bitmap_correctness(
380
2.08k
                _delete_bitmap, _rowset->end_version() - 1, _req.txn_id, *_rowset_ids);
381
2.08k
        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
2.08k
    }
390
16.1k
    std::lock_guard<std::mutex> l(_lock);
391
16.1k
    SCOPED_TIMER(_commit_txn_timer);
392
393
    // Transfer ownership of `PendingRowsetGuard` to `TxnManager`
394
16.1k
    Status res = _engine.txn_manager()->commit_txn(
395
16.1k
            _req.partition_id, *tablet(), _req.txn_id, _req.load_id, _rowset,
396
16.1k
            std::move(_pending_rs_guard), false, _partial_update_info);
397
398
16.1k
    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
16.1k
    if (_tablet->enable_unique_key_merge_on_write()) {
404
6.01k
        _engine.txn_manager()->set_txn_related_delete_bitmap(
405
6.01k
                _req.partition_id, _req.txn_id, tablet()->tablet_id(), tablet()->tablet_uid(), true,
406
6.01k
                _delete_bitmap, *_rowset_ids, _partial_update_info);
407
6.01k
    }
408
409
16.1k
    _is_committed = true;
410
16.1k
    return Status::OK();
411
16.1k
}
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
186k
        const TabletSchema& ori_tablet_schema) {
428
    // find the right index id
429
186k
    int i = 0;
430
186k
    auto indexes = table_schema_param->indexes();
431
203k
    for (; i < indexes.size(); i++) {
432
203k
        if (indexes[i]->index_id == index_id) {
433
186k
            break;
434
186k
        }
435
203k
    }
436
186k
    if (!indexes.empty() && !indexes[i]->columns.empty() &&
437
186k
        indexes[i]->columns[0]->unique_id() >= 0) {
438
186k
        _tablet_schema->shawdow_copy_without_columns(ori_tablet_schema);
439
186k
        _tablet_schema->build_current_tablet_schema(
440
186k
                index_id, cast_set<int32_t>(table_schema_param->version()), indexes[i],
441
186k
                ori_tablet_schema);
442
186k
    } else {
443
193
        _tablet_schema->copy_from(ori_tablet_schema);
444
193
    }
445
186k
    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
1.50k
        if (is_data_builder()) {
452
1.50k
            if (_tablet_schema->num_variant_columns() > 0) {
453
109
                TabletSchemaSPtr max_version_schema = std::make_shared<TabletSchema>();
454
109
                max_version_schema->copy_from(*_tablet_schema);
455
109
                max_version_schema->copy_extracted_columns(ori_tablet_schema);
456
109
                _tablet->update_max_version_schema(max_version_schema);
457
1.40k
            } else {
458
1.40k
                _tablet->update_max_version_schema(_tablet_schema);
459
1.40k
            }
460
1.50k
        }
461
1.50k
    }
462
463
186k
    _tablet_schema->set_table_id(table_schema_param->table_id());
464
186k
    _tablet_schema->set_db_id(table_schema_param->db_id());
465
186k
    if (table_schema_param->is_partial_update()) {
466
3.77k
        _tablet_schema->set_auto_increment_column(table_schema_param->auto_increment_coulumn());
467
3.77k
    }
468
    // set partial update columns info
469
186k
    _partial_update_info = std::make_shared<PartialUpdateInfo>();
470
186k
    RETURN_IF_ERROR(_partial_update_info->init(
471
186k
            tablet()->tablet_id(), _req.txn_id, *_tablet_schema,
472
186k
            table_schema_param->unique_key_update_mode(),
473
186k
            table_schema_param->partial_update_new_key_policy(),
474
186k
            table_schema_param->partial_update_input_columns(),
475
186k
            table_schema_param->is_strict_mode(), table_schema_param->timestamp_ms(),
476
186k
            table_schema_param->nano_seconds(), table_schema_param->timezone(),
477
186k
            table_schema_param->auto_increment_coulumn(),
478
186k
            table_schema_param->sequence_map_col_uid(), _max_version_in_flush_phase));
479
186k
    return Status::OK();
480
186k
}
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