Coverage Report

Created: 2026-05-17 03:41

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
36
namespace doris {
37
38
class CalcDeleteBitmapToken;
39
class FlushToken;
40
class MemTable;
41
class StorageEngine;
42
class TupleDescriptor;
43
class SlotDescriptor;
44
class OlapTableSchemaParam;
45
class RowsetWriter;
46
47
class Block;
48
49
// Writer for a particular (load, index, tablet).
50
// This class is NOT thread-safe, external synchronization is required.
51
class BaseRowsetBuilder {
52
public:
53
    BaseRowsetBuilder(const WriteRequest& req, RuntimeProfile* profile);
54
55
    virtual ~BaseRowsetBuilder();
56
57
    virtual Status init() = 0;
58
59
    virtual Status build_rowset();
60
61
    virtual Status submit_calc_delete_bitmap_task();
62
63
    virtual Status wait_calc_delete_bitmap();
64
65
0
    virtual Status commit_txn() {
66
0
        return Status::NotSupported("BaseRowsetBuilder::commit_txn not implemented");
67
0
    }
68
69
    Status cancel();
70
71
31
    const std::shared_ptr<RowsetWriter>& rowset_writer() const { return _rowset_writer; }
72
73
111
    const BaseTabletSPtr& tablet() const { return _tablet; }
74
75
2
    const RowsetSharedPtr& rowset() const { return _rowset; }
76
77
15
    const TabletSchemaSPtr& tablet_schema() const { return _tablet_schema; }
78
79
    // For UT
80
1
    const DeleteBitmapPtr& get_delete_bitmap() { return _delete_bitmap; }
81
82
15
    const std::shared_ptr<PartialUpdateInfo>& get_partial_update_info() const {
83
15
        return _partial_update_info;
84
15
    }
85
86
126
    bool is_data_builder() const { return _req.write_req_type == WriteRequestType::DATA; }
87
88
    // Attach an extra rowset (e.g. binlog rowset) to the same txn.
89
    Status attach_rowset_to_txn(const RowsetSharedPtr& rowset);
90
91
    // Attach an extra pending rowset id so that PendingLocalRowsets can be
92
    // cleaned up together with the primary rowset.
93
    Status attach_pending_rs_guard_to_txn(const RowsetId& rowset_id);
94
95
1
    RowsetId rowset_id() const { return _rowset_id; }
96
97
    Status init_mow_context(std::shared_ptr<MowContext>& mow_context);
98
99
protected:
100
    Status _check_flexible_partial_update_single_segment() const;
101
102
    Status _build_current_tablet_schema(int64_t index_id,
103
                                        const OlapTableSchemaParam* table_schema_param,
104
                                        const TabletSchema& ori_tablet_schema);
105
106
    virtual void _init_profile(RuntimeProfile* profile);
107
108
    Status _init_context_common_fields(RowsetWriterContext& context);
109
110
    bool _is_init = false;
111
    bool _is_cancelled = false;
112
    bool _is_committed = false;
113
    WriteRequest _req;
114
    BaseTabletSPtr _tablet;
115
    RowsetSharedPtr _rowset;
116
    // Extra rowsets attached to the same txn (e.g. binlog rowsets).
117
    std::vector<RowsetSharedPtr> _attach_rowsets;
118
    std::shared_ptr<RowsetWriter> _rowset_writer;
119
    PendingRowsetGuard _pending_rs_guard;
120
    // Extra rowset ids that share the same PendingRowsetGuard.
121
    std::vector<RowsetId> _attach_rowset_ids;
122
    TabletSchemaSPtr _tablet_schema;
123
124
    std::mutex _lock;
125
126
    DeleteBitmapPtr _delete_bitmap;
127
    std::unique_ptr<CalcDeleteBitmapToken> _calc_delete_bitmap_token;
128
    // current rowset_ids, used to do diff in publish_version
129
    std::shared_ptr<RowsetIdUnorderedSet> _rowset_ids {std::make_shared<RowsetIdUnorderedSet>()};
130
    int64_t _max_version_in_flush_phase {-1};
131
132
    std::shared_ptr<PartialUpdateInfo> _partial_update_info;
133
134
    RuntimeProfile* _profile = nullptr;
135
    RuntimeProfile::Counter* _build_rowset_timer = nullptr;
136
    RuntimeProfile::Counter* _submit_delete_bitmap_timer = nullptr;
137
    RuntimeProfile::Counter* _wait_delete_bitmap_timer = nullptr;
138
139
    RowsetId _rowset_id;
140
};
141
142
// `StorageEngine` mixin for `BaseRowsetBuilder`
143
class RowsetBuilder : public BaseRowsetBuilder {
144
public:
145
    RowsetBuilder(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile);
146
147
    ~RowsetBuilder() override;
148
149
    Status init() override;
150
151
    Status commit_txn() override;
152
153
    // Cast `BaseTablet` to `Tablet`
154
    Tablet* tablet();
155
156
private:
157
    void _init_profile(RuntimeProfile* profile) override;
158
159
    Status check_tablet_version_count();
160
161
    Status prepare_txn();
162
163
    void _garbage_collection(bool cancel_txn);
164
165
    TabletSharedPtr tablet_sptr();
166
167
    StorageEngine& _engine;
168
    RuntimeProfile::Counter* _commit_txn_timer = nullptr;
169
};
170
171
// Rowset builder dedicated for row_binlog rowset, it shares the same tablet
172
// but uses an independent row_binlog tablet schema.
173
class RowBinlogRowsetBuilder : public RowsetBuilder {
174
public:
175
    RowBinlogRowsetBuilder(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile);
176
177
    // just attach rowset to txn_rs_builder in GroupRowsetBuilder, then rely on
178
    // txn_rs_builder's clean logic.
179
1
    ~RowBinlogRowsetBuilder() override = default;
180
181
    Status init() override;
182
183
    // before commit, binlog rowset builder is responsible for cleaning rowset.
184
    // after commit, rowset will be attached to data(txn) rowset builder, and
185
    // the owner of rowset will be changed, so cleaning rowset is handed to the
186
    // data(txn) rowset builder.
187
0
    Status commit_txn() override {
188
0
        _is_committed = true;
189
0
        return Status::OK();
190
0
    }
191
};
192
193
// manage one transaction with multiple rowset_builder
194
// eg. normal data rowset + row_binlog rowset
195
// Now only support one tablet
196
class GroupRowsetBuilder : public BaseRowsetBuilder {
197
public:
198
    GroupRowsetBuilder(StorageEngine& engine, const WriteRequest& req,
199
                       const WriteRequest& row_binlog_req, 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
1
    RowsetBuilder* txn_rowset_builder() { return _txn_rs_builder.get(); }
212
1
    RowsetBuilder* row_binlog_builder() { return _row_binlog_rowset_builder.get(); }
213
214
private:
215
    // txn rowset builder will manage txn; other builders will add their
216
    // rowsets into here.
217
    std::shared_ptr<RowsetBuilder> _txn_rs_builder;
218
    std::shared_ptr<RowsetBuilder> _row_binlog_rowset_builder;
219
};
220
221
} // namespace doris