Coverage Report

Created: 2026-08-07 13:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowset_builder.h
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
#pragma once
19
20
#include <atomic>
21
#include <memory>
22
#include <mutex>
23
#include <shared_mutex>
24
#include <unordered_set>
25
#include <vector>
26
27
#include "common/status.h"
28
#include "load/delta_writer/delta_writer_context.h"
29
#include "runtime/runtime_profile.h"
30
#include "storage/olap_common.h"
31
#include "storage/partial_update_info.h"
32
#include "storage/rowset/pending_rowset_helper.h"
33
#include "storage/rowset/rowset.h"
34
#include "storage/tablet/tablet_fwd.h"
35
#include "storage/txn/txn_manager.h"
36
37
namespace doris {
38
39
class CalcDeleteBitmapToken;
40
class FlushToken;
41
class MemTable;
42
class StorageEngine;
43
class TupleDescriptor;
44
class SlotDescriptor;
45
class OlapTableSchemaParam;
46
class RowsetWriter;
47
48
class Block;
49
50
// Writer for a particular (load, index, tablet).
51
// This class is NOT thread-safe, external synchronization is required.
52
class BaseRowsetBuilder {
53
public:
54
    BaseRowsetBuilder(const WriteRequest& req, RuntimeProfile* profile);
55
56
    virtual ~BaseRowsetBuilder();
57
58
    virtual Status init() = 0;
59
60
    virtual Status build_rowset();
61
62
    virtual Status submit_calc_delete_bitmap_task();
63
64
    virtual Status wait_calc_delete_bitmap();
65
66
0
    virtual Status commit_txn() {
67
0
        return Status::NotSupported("BaseRowsetBuilder::commit_txn not implemented");
68
0
    }
69
70
    Status cancel();
71
72
342k
    const std::shared_ptr<RowsetWriter>& rowset_writer() const { return _rowset_writer; }
73
74
1.20M
    const BaseTabletSPtr& tablet() const { return _tablet; }
75
76
82.6k
    virtual const BaseTabletSPtr& tablet_sptr() const { return _tablet; }
77
78
4
    virtual const RowsetSharedPtr& rowset() const { return _rowset; }
79
80
82.4k
    virtual const TabletSchemaSPtr& tablet_schema() const { return _tablet_schema; }
81
82
    // For UT
83
    const DeleteBitmapPtr& get_delete_bitmap() { return _delete_bitmap; }
84
85
82.5k
    virtual const std::shared_ptr<PartialUpdateInfo>& get_partial_update_info() const {
86
82.5k
        return _partial_update_info;
87
82.5k
    }
88
89
950k
    bool is_data_builder() const { return _req.write_req_type == WriteRequestType::DATA; }
90
91
    // Attach the binlog rowset and its independent binlog tablet to the same txn.
92
    Status attach_row_binlog_to_txn(const RowBinlogTxnInfo& attach_row_binlog);
93
94
    // Attach an extra pending rowset id so that PendingLocalRowsets can be
95
    // cleaned up together with the primary rowset.
96
    Status attach_pending_rs_guard_to_txn(const RowsetId& rowset_id);
97
98
2
    RowsetId rowset_id() const { return _rowset_id; }
99
100
    Status init_mow_context(std::shared_ptr<MowContext>& mow_context);
101
102
protected:
103
    Status _build_current_tablet_schema(int64_t index_id,
104
                                        const OlapTableSchemaParam* table_schema_param,
105
                                        const TabletSchema& ori_tablet_schema);
106
107
    virtual void _init_profile(RuntimeProfile* profile);
108
109
    Status _init_context_common_fields(RowsetWriterContext& context);
110
111
    bool _is_init = false;
112
    bool _is_cancelled = false;
113
    bool _is_committed = false;
114
    WriteRequest _req;
115
    BaseTabletSPtr _tablet;
116
    RowsetSharedPtr _rowset;
117
    // The binlog rowset and its independent binlog tablet attached to the same txn.
118
    RowBinlogTxnInfo _attach_row_binlog;
119
    std::shared_ptr<RowsetWriter> _rowset_writer;
120
    PendingRowsetGuard _pending_rs_guard;
121
    // Extra rowset ids that share the same PendingRowsetGuard.
122
    std::vector<RowsetId> _attach_rowset_ids;
123
    TabletSchemaSPtr _tablet_schema;
124
125
    std::mutex _lock;
126
127
    DeleteBitmapPtr _delete_bitmap;
128
    std::unique_ptr<CalcDeleteBitmapToken> _calc_delete_bitmap_token;
129
    // current rowset_ids, used to do diff in publish_version
130
    std::shared_ptr<RowsetIdUnorderedSet> _rowset_ids {std::make_shared<RowsetIdUnorderedSet>()};
131
    int64_t _max_version_in_flush_phase {-1};
132
133
    std::shared_ptr<PartialUpdateInfo> _partial_update_info;
134
135
    RuntimeProfile* _profile = nullptr;
136
    RuntimeProfile::Counter* _build_rowset_timer = nullptr;
137
    RuntimeProfile::Counter* _submit_delete_bitmap_timer = nullptr;
138
    RuntimeProfile::Counter* _wait_delete_bitmap_timer = nullptr;
139
140
    RowsetId _rowset_id;
141
};
142
143
// `StorageEngine` mixin for `BaseRowsetBuilder`
144
class RowsetBuilder : public BaseRowsetBuilder {
145
public:
146
    RowsetBuilder(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile);
147
148
    ~RowsetBuilder() override;
149
150
    Status init() override;
151
152
    Status commit_txn() override;
153
154
    // Cast `BaseTablet` to `Tablet`
155
    Tablet* tablet();
156
157
private:
158
    void _init_profile(RuntimeProfile* profile) override;
159
160
    Status check_tablet_version_count();
161
162
    Status prepare_txn();
163
164
    void _garbage_collection(bool cancel_txn);
165
166
    StorageEngine& _engine;
167
    RuntimeProfile::Counter* _commit_txn_timer = nullptr;
168
};
169
170
// Rowset builder dedicated for row_binlog rowset. It writes to the independent
171
// row_binlog tablet while being attached to the base tablet txn by GroupRowsetBuilder.
172
class RowBinlogRowsetBuilder : public RowsetBuilder {
173
public:
174
    RowBinlogRowsetBuilder(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile);
175
176
    // just attach rowset to txn_rs_builder in GroupRowsetBuilder, then rely on
177
    // txn_rs_builder's clean logic.
178
    ~RowBinlogRowsetBuilder() override = default;
179
180
    Status init() override;
181
182
    // before commit, binlog rowset builder is responsible for cleaning rowset.
183
    // after commit, rowset will be attached to data(txn) rowset builder, and
184
    // the owner of rowset will be changed, so cleaning rowset is handed to the
185
    // data(txn) rowset builder.
186
0
    Status commit_txn() override {
187
0
        std::lock_guard<std::mutex> l(_lock);
188
0
        _is_committed = true;
189
0
        return Status::OK();
190
0
    }
191
};
192
193
// Manage one transaction with multiple rowset_builders.
194
// eg. normal data rowset + row_binlog rowset.
195
class GroupRowsetBuilder : public BaseRowsetBuilder {
196
public:
197
    GroupRowsetBuilder(StorageEngine& engine, const WriteRequest& group_build_req,
198
                       const WriteRequest& sub_data_req, const WriteRequest& sub_row_binlog_req,
199
                       RuntimeProfile* profile);
200
201
    Status init() override;
202
203
    Status build_rowset() override;
204
205
    Status submit_calc_delete_bitmap_task() override;
206
207
    Status wait_calc_delete_bitmap() override;
208
209
    Status commit_txn() override;
210
211
0
    const BaseTabletSPtr& tablet_sptr() const override { return _txn_rs_builder->tablet_sptr(); }
212
213
0
    const RowsetSharedPtr& rowset() const override { return _txn_rs_builder->rowset(); }
214
215
0
    const TabletSchemaSPtr& tablet_schema() const override {
216
0
        return _txn_rs_builder->tablet_schema();
217
0
    }
218
219
0
    const std::shared_ptr<PartialUpdateInfo>& get_partial_update_info() const override {
220
0
        return _txn_rs_builder->get_partial_update_info();
221
0
    }
222
223
    RowsetBuilder* txn_rowset_builder() { return _txn_rs_builder.get(); }
224
    RowsetBuilder* row_binlog_builder() { return _row_binlog_rowset_builder.get(); }
225
226
private:
227
    // txn rowset builder will manage txn; other builders will add their
228
    // rowsets into here.
229
    std::shared_ptr<RowsetBuilder> _txn_rs_builder;
230
    std::shared_ptr<RowsetBuilder> _row_binlog_rowset_builder;
231
};
232
233
} // namespace doris