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 | 354k | const std::shared_ptr<RowsetWriter>& rowset_writer() const { return _rowset_writer; } |
72 | | |
73 | 1.31M | const BaseTabletSPtr& tablet() const { return _tablet; } |
74 | | |
75 | 2 | const RowsetSharedPtr& rowset() const { return _rowset; } |
76 | | |
77 | 173k | const TabletSchemaSPtr& tablet_schema() const { return _tablet_schema; } |
78 | | |
79 | | // For UT |
80 | | const DeleteBitmapPtr& get_delete_bitmap() { return _delete_bitmap; } |
81 | | |
82 | 173k | const std::shared_ptr<PartialUpdateInfo>& get_partial_update_info() const { |
83 | 173k | return _partial_update_info; |
84 | 173k | } |
85 | | |
86 | 403k | 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 _build_current_tablet_schema(int64_t index_id, |
101 | | const OlapTableSchemaParam* table_schema_param, |
102 | | const TabletSchema& ori_tablet_schema); |
103 | | |
104 | | virtual void _init_profile(RuntimeProfile* profile); |
105 | | |
106 | | Status _init_context_common_fields(RowsetWriterContext& context); |
107 | | |
108 | | bool _is_init = false; |
109 | | bool _is_cancelled = false; |
110 | | bool _is_committed = false; |
111 | | WriteRequest _req; |
112 | | BaseTabletSPtr _tablet; |
113 | | RowsetSharedPtr _rowset; |
114 | | // Extra rowsets attached to the same txn (e.g. binlog rowsets). |
115 | | std::vector<RowsetSharedPtr> _attach_rowsets; |
116 | | std::shared_ptr<RowsetWriter> _rowset_writer; |
117 | | PendingRowsetGuard _pending_rs_guard; |
118 | | // Extra rowset ids that share the same PendingRowsetGuard. |
119 | | std::vector<RowsetId> _attach_rowset_ids; |
120 | | TabletSchemaSPtr _tablet_schema; |
121 | | |
122 | | std::mutex _lock; |
123 | | |
124 | | DeleteBitmapPtr _delete_bitmap; |
125 | | std::unique_ptr<CalcDeleteBitmapToken> _calc_delete_bitmap_token; |
126 | | // current rowset_ids, used to do diff in publish_version |
127 | | std::shared_ptr<RowsetIdUnorderedSet> _rowset_ids {std::make_shared<RowsetIdUnorderedSet>()}; |
128 | | int64_t _max_version_in_flush_phase {-1}; |
129 | | |
130 | | std::shared_ptr<PartialUpdateInfo> _partial_update_info; |
131 | | |
132 | | RuntimeProfile* _profile = nullptr; |
133 | | RuntimeProfile::Counter* _build_rowset_timer = nullptr; |
134 | | RuntimeProfile::Counter* _submit_delete_bitmap_timer = nullptr; |
135 | | RuntimeProfile::Counter* _wait_delete_bitmap_timer = nullptr; |
136 | | |
137 | | RowsetId _rowset_id; |
138 | | }; |
139 | | |
140 | | // `StorageEngine` mixin for `BaseRowsetBuilder` |
141 | | class RowsetBuilder : public BaseRowsetBuilder { |
142 | | public: |
143 | | RowsetBuilder(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile); |
144 | | |
145 | | ~RowsetBuilder() override; |
146 | | |
147 | | Status init() override; |
148 | | |
149 | | Status commit_txn() override; |
150 | | |
151 | | // Cast `BaseTablet` to `Tablet` |
152 | | Tablet* tablet(); |
153 | | |
154 | | private: |
155 | | void _init_profile(RuntimeProfile* profile) override; |
156 | | |
157 | | Status check_tablet_version_count(); |
158 | | |
159 | | Status prepare_txn(); |
160 | | |
161 | | void _garbage_collection(bool cancel_txn); |
162 | | |
163 | | TabletSharedPtr tablet_sptr(); |
164 | | |
165 | | StorageEngine& _engine; |
166 | | RuntimeProfile::Counter* _commit_txn_timer = nullptr; |
167 | | }; |
168 | | |
169 | | // Rowset builder dedicated for row_binlog rowset, it shares the same tablet |
170 | | // but uses an independent row_binlog tablet schema. |
171 | | class RowBinlogRowsetBuilder : public RowsetBuilder { |
172 | | public: |
173 | | RowBinlogRowsetBuilder(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile); |
174 | | |
175 | | // just attach rowset to txn_rs_builder in GroupRowsetBuilder, then rely on |
176 | | // txn_rs_builder's clean logic. |
177 | | ~RowBinlogRowsetBuilder() override = default; |
178 | | |
179 | | Status init() override; |
180 | | |
181 | | // before commit, binlog rowset builder is responsible for cleaning rowset. |
182 | | // after commit, rowset will be attached to data(txn) rowset builder, and |
183 | | // the owner of rowset will be changed, so cleaning rowset is handed to the |
184 | | // data(txn) rowset builder. |
185 | 0 | Status commit_txn() override { |
186 | 0 | _is_committed = true; |
187 | 0 | return Status::OK(); |
188 | 0 | } |
189 | | }; |
190 | | |
191 | | // manage one transaction with multiple rowset_builder |
192 | | // eg. normal data rowset + row_binlog rowset |
193 | | // Now only support one tablet |
194 | | class GroupRowsetBuilder : public BaseRowsetBuilder { |
195 | | public: |
196 | | GroupRowsetBuilder(StorageEngine& engine, const WriteRequest& req, |
197 | | const WriteRequest& row_binlog_req, RuntimeProfile* profile); |
198 | | |
199 | | Status init() override; |
200 | | |
201 | | Status build_rowset() override; |
202 | | |
203 | | Status submit_calc_delete_bitmap_task() override; |
204 | | |
205 | | Status wait_calc_delete_bitmap() override; |
206 | | |
207 | | Status commit_txn() override; |
208 | | |
209 | | RowsetBuilder* txn_rowset_builder() { return _txn_rs_builder.get(); } |
210 | | RowsetBuilder* row_binlog_builder() { return _row_binlog_rowset_builder.get(); } |
211 | | |
212 | | private: |
213 | | // txn rowset builder will manage txn; other builders will add their |
214 | | // rowsets into here. |
215 | | std::shared_ptr<RowsetBuilder> _txn_rs_builder; |
216 | | std::shared_ptr<RowsetBuilder> _row_binlog_rowset_builder; |
217 | | }; |
218 | | |
219 | | } // namespace doris |