be/src/storage/txn/txn_manager.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 <butil/macros.h> |
21 | | #include <gen_cpp/Types_types.h> |
22 | | #include <gen_cpp/types.pb.h> |
23 | | #include <stddef.h> |
24 | | #include <stdint.h> |
25 | | |
26 | | #include <boost/container/detail/std_fwd.hpp> |
27 | | #include <map> |
28 | | #include <memory> |
29 | | #include <mutex> |
30 | | #include <set> |
31 | | #include <shared_mutex> |
32 | | #include <unordered_map> |
33 | | #include <unordered_set> |
34 | | #include <utility> |
35 | | #include <vector> |
36 | | |
37 | | #include "common/status.h" |
38 | | #include "core/block/block.h" |
39 | | #include "runtime/memory/lru_cache_policy.h" |
40 | | #include "storage/olap_common.h" |
41 | | #include "storage/rowset/pending_rowset_helper.h" |
42 | | #include "storage/rowset/rowset.h" |
43 | | #include "storage/rowset/rowset_meta.h" |
44 | | #include "storage/segment/segment.h" |
45 | | #include "storage/segment/vertical_segment_writer.h" |
46 | | #include "storage/tablet/tablet.h" |
47 | | #include "storage/tablet/tablet_meta.h" |
48 | | #include "util/time.h" |
49 | | |
50 | | namespace doris { |
51 | | class OlapMeta; |
52 | | class WorkloadGroup; |
53 | | struct TabletPublishStatistics; |
54 | | struct PartialUpdateInfo; |
55 | | |
56 | | enum class TxnState { |
57 | | NOT_FOUND = 0, |
58 | | PREPARED = 1, |
59 | | COMMITTED = 2, |
60 | | ROLLEDBACK = 3, |
61 | | ABORTED = 4, |
62 | | DELETED = 5, |
63 | | }; |
64 | | enum class PublishStatus { INIT = 0, PREPARE = 1, SUCCEED = 2 }; |
65 | | |
66 | | // The row binlog rowset and its independent binlog tablet, carried through commit and publish. |
67 | | struct RowBinlogTxnInfo { |
68 | | RowsetSharedPtr rowset; |
69 | | BaseTabletSPtr tablet; |
70 | | // Delete bitmap deltas that should be applied to the independent binlog tablet. |
71 | | DeleteBitmapPtr delete_bitmap; |
72 | | }; |
73 | | |
74 | | struct TxnPublishInfo { |
75 | | int64_t publish_version {-1}; |
76 | | int64_t base_compaction_cnt {-1}; |
77 | | int64_t cumulative_compaction_cnt {-1}; |
78 | | int64_t cumulative_point {-1}; |
79 | | }; |
80 | | |
81 | | struct TabletTxnInfo { |
82 | | PUniqueId load_id; |
83 | | // Runtime-only write-stage owner, retained through local publish/retries. |
84 | | std::shared_ptr<WorkloadGroup> workload_group; |
85 | | RowsetSharedPtr rowset; |
86 | | // The row-binlog tablet is attached while PREPARED; its rowset is filled at commit. |
87 | | RowBinlogTxnInfo attach_row_binlog; |
88 | | PendingRowsetGuard pending_rs_guard; |
89 | | bool unique_key_merge_on_write {false}; |
90 | | DeleteBitmapPtr delete_bitmap; |
91 | | // records rowsets calc in commit txn |
92 | | RowsetIdUnorderedSet rowset_ids; |
93 | | int64_t creation_time; |
94 | | bool ingest {false}; |
95 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
96 | | |
97 | | // for cloud only, used to determine if a retry CloudTabletCalcDeleteBitmapTask |
98 | | // needs to re-calculate the delete bitmap |
99 | | std::shared_ptr<PublishStatus> publish_status; |
100 | | TxnPublishInfo publish_info; |
101 | | |
102 | | // for cloud only, used to calculate delete bitmap for txn load |
103 | | bool is_txn_load = false; |
104 | | std::vector<RowsetSharedPtr> invisible_rowsets; |
105 | | int64_t lock_id; |
106 | | int64_t next_visible_version; |
107 | | |
108 | | TxnState state {TxnState::PREPARED}; |
109 | 0 | TabletTxnInfo() = default; |
110 | | |
111 | | TabletTxnInfo(PUniqueId load_id, RowsetSharedPtr rowset) |
112 | 69 | : load_id(std::move(load_id)), |
113 | 69 | rowset(std::move(rowset)), |
114 | 69 | creation_time(UnixSeconds()) {} |
115 | | |
116 | | TabletTxnInfo(PUniqueId load_id, RowsetSharedPtr rowset, bool ingest_arg) |
117 | 65 | : load_id(std::move(load_id)), |
118 | 65 | rowset(std::move(rowset)), |
119 | 65 | creation_time(UnixSeconds()), |
120 | 65 | ingest(ingest_arg) {} |
121 | | |
122 | | TabletTxnInfo(PUniqueId load_id, RowsetSharedPtr rowset, bool merge_on_write, |
123 | | DeleteBitmapPtr delete_bitmap, RowsetIdUnorderedSet ids) |
124 | | : load_id(std::move(load_id)), |
125 | | rowset(std::move(rowset)), |
126 | | unique_key_merge_on_write(merge_on_write), |
127 | | delete_bitmap(std::move(delete_bitmap)), |
128 | | rowset_ids(std::move(ids)), |
129 | 0 | creation_time(UnixSeconds()) {} |
130 | | |
131 | 65 | void prepare() { state = TxnState::PREPARED; } |
132 | 69 | void commit() { state = TxnState::COMMITTED; } |
133 | 0 | void rollback() { state = TxnState::ROLLEDBACK; } |
134 | 0 | void abort() { |
135 | 0 | if (state == TxnState::PREPARED) { |
136 | 0 | state = TxnState::ABORTED; |
137 | 0 | } |
138 | 0 | } |
139 | | }; |
140 | | |
141 | | struct CommitTabletTxnInfo { |
142 | | TTransactionId transaction_id {0}; |
143 | | TPartitionId partition_id {0}; |
144 | | DeleteBitmapPtr delete_bitmap; |
145 | | RowsetIdUnorderedSet rowset_ids; |
146 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
147 | | }; |
148 | | |
149 | | using CommitTabletTxnInfoVec = std::vector<CommitTabletTxnInfo>; |
150 | | |
151 | | // txn manager is used to manage mapping between tablet and txns |
152 | | class TxnManager { |
153 | | public: |
154 | | TxnManager(StorageEngine& engine, int32_t txn_map_shard_size, int32_t txn_shard_size); |
155 | | |
156 | 700 | ~TxnManager() { |
157 | 700 | delete[] _txn_tablet_maps; |
158 | 700 | delete[] _txn_partition_maps; |
159 | 700 | delete[] _txn_map_locks; |
160 | 700 | delete[] _txn_mutex; |
161 | 700 | } |
162 | | |
163 | | class CacheValue : public LRUCacheValueBase { |
164 | | public: |
165 | | int64_t value; |
166 | | }; |
167 | | |
168 | | // add a txn to manager |
169 | | // partition id is useful in publish version stage because version is associated with partition |
170 | | Status prepare_txn(TPartitionId partition_id, const Tablet& tablet, |
171 | | TTransactionId transaction_id, const PUniqueId& load_id, |
172 | | bool is_ingest = false); |
173 | | // most used for ut |
174 | | Status prepare_txn(TPartitionId partition_id, TTransactionId transaction_id, |
175 | | TTabletId tablet_id, TabletUid tablet_uid, const PUniqueId& load_id, |
176 | | bool is_ingest = false); |
177 | | |
178 | | // Internal: attach the independent row-binlog tablet to an existing base-tablet transaction |
179 | | // before commit, so tablet lifecycle operations can drain the whole group transaction. |
180 | | Status attach_row_binlog_tablet_to_txn(TPartitionId partition_id, TTransactionId transaction_id, |
181 | | const TabletInfo& base_tablet_info, |
182 | | const BaseTabletSPtr& row_binlog_tablet); |
183 | | |
184 | | Status commit_txn(TPartitionId partition_id, const Tablet& tablet, |
185 | | TTransactionId transaction_id, const PUniqueId& load_id, |
186 | | const RowsetSharedPtr& rowset_ptr, PendingRowsetGuard guard, bool is_recovery, |
187 | | std::shared_ptr<PartialUpdateInfo> partial_update_info = nullptr, |
188 | | const RowBinlogTxnInfo& attach_row_binlog = {}); |
189 | | |
190 | | Status publish_txn(TPartitionId partition_id, const TabletSharedPtr& tablet, |
191 | | TTransactionId transaction_id, const Version& version, |
192 | | TabletPublishStatistics* stats, |
193 | | std::shared_ptr<TabletTxnInfo>& extend_tablet_txn_info, |
194 | | const int64_t commit_tso = -1); |
195 | | |
196 | | // delete the txn from manager if it is not committed(not have a valid rowset) |
197 | | Status rollback_txn(TPartitionId partition_id, const Tablet& tablet, |
198 | | TTransactionId transaction_id); |
199 | | |
200 | | Status delete_txn(TPartitionId partition_id, const TabletSharedPtr& tablet, |
201 | | TTransactionId transaction_id); |
202 | | |
203 | | Status commit_txn(OlapMeta* meta, TPartitionId partition_id, TTransactionId transaction_id, |
204 | | TTabletId tablet_id, TabletUid tablet_uid, const PUniqueId& load_id, |
205 | | const RowsetSharedPtr& rowset_ptr, PendingRowsetGuard guard, bool is_recovery, |
206 | | std::shared_ptr<PartialUpdateInfo> partial_update_info = nullptr, |
207 | | const RowBinlogTxnInfo& attach_row_binlog = {}); |
208 | | |
209 | | // remove a txn from txn manager |
210 | | // not persist rowset meta because |
211 | | Status publish_txn(OlapMeta* meta, TPartitionId partition_id, TTransactionId transaction_id, |
212 | | TTabletId tablet_id, TabletUid tablet_uid, const Version& version, |
213 | | TabletPublishStatistics* stats, |
214 | | std::shared_ptr<TabletTxnInfo>& extend_tablet_txn_info, |
215 | | const int64_t commit_tso = -1); |
216 | | |
217 | | // only abort not committed txn |
218 | | void abort_txn(TPartitionId partition_id, TTransactionId transaction_id, TTabletId tablet_id, |
219 | | TabletUid tablet_uid); |
220 | | |
221 | | // delete the txn from manager if it is not committed(not have a valid rowset) |
222 | | Status rollback_txn(TPartitionId partition_id, TTransactionId transaction_id, |
223 | | TTabletId tablet_id, TabletUid tablet_uid); |
224 | | |
225 | | // remove the txn from txn manager |
226 | | // delete the related rowset if it is not null |
227 | | // delete rowset related data if it is not null |
228 | | Status delete_txn(OlapMeta* meta, TPartitionId partition_id, TTransactionId transaction_id, |
229 | | TTabletId tablet_id, TabletUid tablet_uid); |
230 | | |
231 | | // Includes transactions keyed by this tablet and base-tablet transactions that attach this |
232 | | // tablet as their independent row-binlog tablet. |
233 | | void get_tablet_related_txns(TTabletId tablet_id, TabletUid tablet_uid, int64_t* partition_id, |
234 | | std::set<int64_t>* transaction_ids); |
235 | | |
236 | | void get_txn_related_tablets( |
237 | | const TTransactionId transaction_id, TPartitionId partition_ids, |
238 | | std::map<TabletInfo, RowsetSharedPtr>* tablet_infos, |
239 | | std::map<TabletInfo, std::shared_ptr<TabletTxnInfo>>* tablet_txn_infos = nullptr); |
240 | | |
241 | | void get_all_related_tablets(std::set<TabletInfo>* tablet_infos); |
242 | | |
243 | | // Get all expired txns and save them in expire_txn_map. |
244 | | // This is currently called before reporting all tablet info, to avoid iterating txn map for every tablets. |
245 | | void build_expire_txn_map(std::map<TabletInfo, std::vector<int64_t>>* expire_txn_map); |
246 | | |
247 | | void force_rollback_tablet_related_txns(OlapMeta* meta, TTabletId tablet_id, |
248 | | TabletUid tablet_uid); |
249 | | |
250 | | void get_partition_ids(const TTransactionId transaction_id, |
251 | | std::vector<TPartitionId>* partition_ids); |
252 | | |
253 | | void set_txn_related_delete_bitmap(TPartitionId partition_id, TTransactionId transaction_id, |
254 | | TTabletId tablet_id, TabletUid tablet_uid, |
255 | | bool unique_key_merge_on_write, |
256 | | DeleteBitmapPtr delete_bitmap, |
257 | | const RowsetIdUnorderedSet& rowset_ids, |
258 | | std::shared_ptr<PartialUpdateInfo> partial_update_info); |
259 | | void get_all_commit_tablet_txn_info_by_tablet( |
260 | | const Tablet& tablet, CommitTabletTxnInfoVec* commit_tablet_txn_info_vec); |
261 | | |
262 | | int64_t get_txn_by_tablet_version(int64_t tablet_id, int64_t version); |
263 | | void update_tablet_version_txn(int64_t tablet_id, int64_t version, int64_t txn_id); |
264 | | |
265 | | TxnState get_txn_state(TPartitionId partition_id, TTransactionId transaction_id, |
266 | | TTabletId tablet_id, TabletUid tablet_uid); |
267 | | |
268 | | void remove_txn_tablet_info(TPartitionId partition_id, TTransactionId transaction_id, |
269 | | TTabletId tablet_id, TabletUid tablet_uid); |
270 | | |
271 | | private: |
272 | | using TxnKey = std::pair<int64_t, int64_t>; // partition_id, transaction_id; |
273 | | |
274 | | // Implement TxnKey hash function to support TxnKey as a key for `unordered_map`. |
275 | | struct TxnKeyHash { |
276 | | template <typename T, typename U> |
277 | 546 | size_t operator()(const std::pair<T, U>& e) const { |
278 | 546 | return std::hash<T>()(e.first) ^ std::hash<U>()(e.second); |
279 | 546 | } |
280 | | }; |
281 | | |
282 | | // Implement TxnKey equal function to support TxnKey as a key for `unordered_map`. |
283 | | struct TxnKeyEqual { |
284 | | template <class T, typename U> |
285 | 325 | bool operator()(const std::pair<T, U>& l, const std::pair<T, U>& r) const { |
286 | 325 | return l.first == r.first && l.second == r.second; |
287 | 325 | } |
288 | | }; |
289 | | |
290 | | using txn_tablet_map_t = |
291 | | std::unordered_map<TxnKey, std::map<TabletInfo, std::shared_ptr<TabletTxnInfo>>, |
292 | | TxnKeyHash, TxnKeyEqual>; |
293 | | using txn_partition_map_t = std::unordered_map<int64_t, std::unordered_set<int64_t>>; |
294 | | std::shared_mutex& _get_txn_map_lock(TTransactionId transactionId); |
295 | | |
296 | | txn_tablet_map_t& _get_txn_tablet_map(TTransactionId transactionId); |
297 | | |
298 | | txn_partition_map_t& _get_txn_partition_map(TTransactionId transactionId); |
299 | | |
300 | | inline std::shared_mutex& _get_txn_lock(TTransactionId transactionId); |
301 | | |
302 | | // Insert or remove (transaction_id, partition_id) from _txn_partition_map |
303 | | // get _txn_map_lock before calling. |
304 | | void _insert_txn_partition_map_unlocked(int64_t transaction_id, int64_t partition_id); |
305 | | void _clear_txn_partition_map_unlocked(int64_t transaction_id, int64_t partition_id); |
306 | | |
307 | | void _remove_txn_tablet_info_unlocked(TPartitionId partition_id, TTransactionId transaction_id, |
308 | | TTabletId tablet_id, TabletUid tablet_uid, |
309 | | std::lock_guard<std::shared_mutex>& txn_lock, |
310 | | std::lock_guard<std::shared_mutex>& wrlock); |
311 | | |
312 | | class TabletVersionCache : public LRUCachePolicy { |
313 | | public: |
314 | | TabletVersionCache(size_t capacity) |
315 | 700 | : LRUCachePolicy(CachePolicy::CacheType::TABLET_VERSION_CACHE, capacity, |
316 | 700 | LRUCacheType::NUMBER, /*sweeptime*/ -1, |
317 | 700 | /*num_shards*/ 32, |
318 | 700 | /*element_count_capacity*/ 0, /*enable_prune*/ false, |
319 | 700 | /*is_lru_k*/ false) {} |
320 | | }; |
321 | | |
322 | | private: |
323 | | StorageEngine& _engine; |
324 | | |
325 | | const int32_t _txn_map_shard_size; |
326 | | |
327 | | const int32_t _txn_shard_size; |
328 | | |
329 | | // _txn_map_locks[i] protect _txn_tablet_maps[i], i=0,1,2...,and i < _txn_map_shard_size |
330 | | txn_tablet_map_t* _txn_tablet_maps = nullptr; |
331 | | // transaction_id -> corresponding partition ids |
332 | | // This is mainly for the clear txn task received from FE, which may only has transaction id, |
333 | | // so we need this map to find out which partitions are corresponding to a transaction id. |
334 | | // The _txn_partition_maps[i] should be constructed/deconstructed/modified alongside with '_txn_tablet_maps[i]' |
335 | | txn_partition_map_t* _txn_partition_maps = nullptr; |
336 | | |
337 | | std::shared_mutex* _txn_map_locks = nullptr; |
338 | | |
339 | | std::shared_mutex* _txn_mutex = nullptr; |
340 | | |
341 | | std::unique_ptr<TabletVersionCache> _tablet_version_cache; |
342 | | DISALLOW_COPY_AND_ASSIGN(TxnManager); |
343 | | }; // TxnManager |
344 | | |
345 | 418 | inline std::shared_mutex& TxnManager::_get_txn_map_lock(TTransactionId transactionId) { |
346 | 418 | return _txn_map_locks[transactionId & (_txn_map_shard_size - 1)]; |
347 | 418 | } |
348 | | |
349 | 416 | inline TxnManager::txn_tablet_map_t& TxnManager::_get_txn_tablet_map(TTransactionId transactionId) { |
350 | 416 | return _txn_tablet_maps[transactionId & (_txn_map_shard_size - 1)]; |
351 | 416 | } |
352 | | |
353 | | inline TxnManager::txn_partition_map_t& TxnManager::_get_txn_partition_map( |
354 | 256 | TTransactionId transactionId) { |
355 | 256 | return _txn_partition_maps[transactionId & (_txn_map_shard_size - 1)]; |
356 | 256 | } |
357 | | |
358 | 195 | inline std::shared_mutex& TxnManager::_get_txn_lock(TTransactionId transactionId) { |
359 | 195 | return _txn_mutex[transactionId & (_txn_shard_size - 1)]; |
360 | 195 | } |
361 | | |
362 | | } // namespace doris |