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/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 DeltaWriter; |
52 | | class OlapMeta; |
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 | | RowsetSharedPtr rowset; |
84 | | // the row binlog committed along with this txn. |
85 | | RowBinlogTxnInfo attach_row_binlog; |
86 | | PendingRowsetGuard pending_rs_guard; |
87 | | bool unique_key_merge_on_write {false}; |
88 | | DeleteBitmapPtr delete_bitmap; |
89 | | // records rowsets calc in commit txn |
90 | | RowsetIdUnorderedSet rowset_ids; |
91 | | int64_t creation_time; |
92 | | bool ingest {false}; |
93 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
94 | | |
95 | | // for cloud only, used to determine if a retry CloudTabletCalcDeleteBitmapTask |
96 | | // needs to re-calculate the delete bitmap |
97 | | std::shared_ptr<PublishStatus> publish_status; |
98 | | TxnPublishInfo publish_info; |
99 | | |
100 | | // for cloud only, used to calculate delete bitmap for txn load |
101 | | bool is_txn_load = false; |
102 | | std::vector<RowsetSharedPtr> invisible_rowsets; |
103 | | int64_t lock_id; |
104 | | int64_t next_visible_version; |
105 | | |
106 | | TxnState state {TxnState::PREPARED}; |
107 | 0 | TabletTxnInfo() = default; |
108 | | |
109 | | TabletTxnInfo(PUniqueId load_id, RowsetSharedPtr rowset) |
110 | 36 | : load_id(std::move(load_id)), |
111 | 36 | rowset(std::move(rowset)), |
112 | 36 | creation_time(UnixSeconds()) {} |
113 | | |
114 | | TabletTxnInfo(PUniqueId load_id, RowsetSharedPtr rowset, bool ingest_arg) |
115 | 36 | : load_id(std::move(load_id)), |
116 | 36 | rowset(std::move(rowset)), |
117 | 36 | creation_time(UnixSeconds()), |
118 | 36 | ingest(ingest_arg) {} |
119 | | |
120 | | TabletTxnInfo(PUniqueId load_id, RowsetSharedPtr rowset, bool merge_on_write, |
121 | | DeleteBitmapPtr delete_bitmap, RowsetIdUnorderedSet ids) |
122 | | : load_id(std::move(load_id)), |
123 | | rowset(std::move(rowset)), |
124 | | unique_key_merge_on_write(merge_on_write), |
125 | | delete_bitmap(std::move(delete_bitmap)), |
126 | | rowset_ids(std::move(ids)), |
127 | 0 | creation_time(UnixSeconds()) {} |
128 | | |
129 | 36 | void prepare() { state = TxnState::PREPARED; } |
130 | 36 | void commit() { state = TxnState::COMMITTED; } |
131 | 0 | void rollback() { state = TxnState::ROLLEDBACK; } |
132 | 0 | void abort() { |
133 | 0 | if (state == TxnState::PREPARED) { |
134 | 0 | state = TxnState::ABORTED; |
135 | 0 | } |
136 | 0 | } |
137 | | }; |
138 | | |
139 | | struct CommitTabletTxnInfo { |
140 | | TTransactionId transaction_id {0}; |
141 | | TPartitionId partition_id {0}; |
142 | | DeleteBitmapPtr delete_bitmap; |
143 | | RowsetIdUnorderedSet rowset_ids; |
144 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
145 | | }; |
146 | | |
147 | | using CommitTabletTxnInfoVec = std::vector<CommitTabletTxnInfo>; |
148 | | |
149 | | // txn manager is used to manage mapping between tablet and txns |
150 | | class TxnManager { |
151 | | public: |
152 | | TxnManager(StorageEngine& engine, int32_t txn_map_shard_size, int32_t txn_shard_size); |
153 | | |
154 | 509 | ~TxnManager() { |
155 | 509 | delete[] _txn_tablet_maps; |
156 | 509 | delete[] _txn_partition_maps; |
157 | 509 | delete[] _txn_map_locks; |
158 | 509 | delete[] _txn_mutex; |
159 | 509 | delete[] _txn_tablet_delta_writer_map; |
160 | 509 | delete[] _txn_tablet_delta_writer_map_locks; |
161 | 509 | } |
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 | | Status commit_txn(TPartitionId partition_id, const Tablet& tablet, |
179 | | TTransactionId transaction_id, const PUniqueId& load_id, |
180 | | const RowsetSharedPtr& rowset_ptr, PendingRowsetGuard guard, bool is_recovery, |
181 | | std::shared_ptr<PartialUpdateInfo> partial_update_info = nullptr, |
182 | | const RowBinlogTxnInfo& attach_row_binlog = {}); |
183 | | |
184 | | Status publish_txn(TPartitionId partition_id, const TabletSharedPtr& tablet, |
185 | | TTransactionId transaction_id, const Version& version, |
186 | | TabletPublishStatistics* stats, |
187 | | std::shared_ptr<TabletTxnInfo>& extend_tablet_txn_info, |
188 | | const int64_t commit_tso = -1); |
189 | | |
190 | | // delete the txn from manager if it is not committed(not have a valid rowset) |
191 | | Status rollback_txn(TPartitionId partition_id, const Tablet& tablet, |
192 | | TTransactionId transaction_id); |
193 | | |
194 | | Status delete_txn(TPartitionId partition_id, const TabletSharedPtr& tablet, |
195 | | TTransactionId transaction_id); |
196 | | |
197 | | Status commit_txn(OlapMeta* meta, TPartitionId partition_id, TTransactionId transaction_id, |
198 | | TTabletId tablet_id, TabletUid tablet_uid, const PUniqueId& load_id, |
199 | | const RowsetSharedPtr& rowset_ptr, PendingRowsetGuard guard, bool is_recovery, |
200 | | std::shared_ptr<PartialUpdateInfo> partial_update_info = nullptr, |
201 | | const RowBinlogTxnInfo& attach_row_binlog = {}); |
202 | | |
203 | | // remove a txn from txn manager |
204 | | // not persist rowset meta because |
205 | | Status publish_txn(OlapMeta* meta, TPartitionId partition_id, TTransactionId transaction_id, |
206 | | TTabletId tablet_id, TabletUid tablet_uid, const Version& version, |
207 | | TabletPublishStatistics* stats, |
208 | | std::shared_ptr<TabletTxnInfo>& extend_tablet_txn_info, |
209 | | const int64_t commit_tso = -1); |
210 | | |
211 | | // only abort not committed txn |
212 | | void abort_txn(TPartitionId partition_id, TTransactionId transaction_id, TTabletId tablet_id, |
213 | | TabletUid tablet_uid); |
214 | | |
215 | | // delete the txn from manager if it is not committed(not have a valid rowset) |
216 | | Status rollback_txn(TPartitionId partition_id, TTransactionId transaction_id, |
217 | | TTabletId tablet_id, TabletUid tablet_uid); |
218 | | |
219 | | // remove the txn from txn manager |
220 | | // delete the related rowset if it is not null |
221 | | // delete rowset related data if it is not null |
222 | | Status delete_txn(OlapMeta* meta, TPartitionId partition_id, TTransactionId transaction_id, |
223 | | TTabletId tablet_id, TabletUid tablet_uid); |
224 | | |
225 | | void get_tablet_related_txns(TTabletId tablet_id, TabletUid tablet_uid, int64_t* partition_id, |
226 | | std::set<int64_t>* transaction_ids); |
227 | | |
228 | | void get_txn_related_tablets( |
229 | | const TTransactionId transaction_id, TPartitionId partition_ids, |
230 | | std::map<TabletInfo, RowsetSharedPtr>* tablet_infos, |
231 | | std::map<TabletInfo, std::shared_ptr<TabletTxnInfo>>* tablet_txn_infos = nullptr); |
232 | | |
233 | | void get_all_related_tablets(std::set<TabletInfo>* tablet_infos); |
234 | | |
235 | | // Get all expired txns and save them in expire_txn_map. |
236 | | // This is currently called before reporting all tablet info, to avoid iterating txn map for every tablets. |
237 | | void build_expire_txn_map(std::map<TabletInfo, std::vector<int64_t>>* expire_txn_map); |
238 | | |
239 | | void force_rollback_tablet_related_txns(OlapMeta* meta, TTabletId tablet_id, |
240 | | TabletUid tablet_uid); |
241 | | |
242 | | void get_partition_ids(const TTransactionId transaction_id, |
243 | | std::vector<TPartitionId>* partition_ids); |
244 | | |
245 | | void add_txn_tablet_delta_writer(int64_t transaction_id, int64_t tablet_id, |
246 | | DeltaWriter* delta_writer); |
247 | | void clear_txn_tablet_delta_writer(int64_t transaction_id); |
248 | | void finish_slave_tablet_pull_rowset(int64_t transaction_id, int64_t tablet_id, int64_t node_id, |
249 | | bool is_succeed); |
250 | | |
251 | | void set_txn_related_delete_bitmap(TPartitionId partition_id, TTransactionId transaction_id, |
252 | | TTabletId tablet_id, TabletUid tablet_uid, |
253 | | bool unique_key_merge_on_write, |
254 | | DeleteBitmapPtr delete_bitmap, |
255 | | const RowsetIdUnorderedSet& rowset_ids, |
256 | | std::shared_ptr<PartialUpdateInfo> partial_update_info); |
257 | | void get_all_commit_tablet_txn_info_by_tablet( |
258 | | const Tablet& tablet, CommitTabletTxnInfoVec* commit_tablet_txn_info_vec); |
259 | | |
260 | | int64_t get_txn_by_tablet_version(int64_t tablet_id, int64_t version); |
261 | | void update_tablet_version_txn(int64_t tablet_id, int64_t version, int64_t txn_id); |
262 | | |
263 | | TxnState get_txn_state(TPartitionId partition_id, TTransactionId transaction_id, |
264 | | TTabletId tablet_id, TabletUid tablet_uid); |
265 | | |
266 | | void remove_txn_tablet_info(TPartitionId partition_id, TTransactionId transaction_id, |
267 | | TTabletId tablet_id, TabletUid tablet_uid); |
268 | | |
269 | | private: |
270 | | using TxnKey = std::pair<int64_t, int64_t>; // partition_id, transaction_id; |
271 | | |
272 | | // Implement TxnKey hash function to support TxnKey as a key for `unordered_map`. |
273 | | struct TxnKeyHash { |
274 | | template <typename T, typename U> |
275 | 270 | size_t operator()(const std::pair<T, U>& e) const { |
276 | 270 | return std::hash<T>()(e.first) ^ std::hash<U>()(e.second); |
277 | 270 | } |
278 | | }; |
279 | | |
280 | | // Implement TxnKey equal function to support TxnKey as a key for `unordered_map`. |
281 | | struct TxnKeyEqual { |
282 | | template <class T, typename U> |
283 | 142 | bool operator()(const std::pair<T, U>& l, const std::pair<T, U>& r) const { |
284 | 142 | return l.first == r.first && l.second == r.second; |
285 | 142 | } |
286 | | }; |
287 | | |
288 | | using txn_tablet_map_t = |
289 | | std::unordered_map<TxnKey, std::map<TabletInfo, std::shared_ptr<TabletTxnInfo>>, |
290 | | TxnKeyHash, TxnKeyEqual>; |
291 | | using txn_partition_map_t = std::unordered_map<int64_t, std::unordered_set<int64_t>>; |
292 | | using txn_tablet_delta_writer_map_t = |
293 | | std::unordered_map<int64_t, std::map<int64_t, DeltaWriter*>>; |
294 | | |
295 | | std::shared_mutex& _get_txn_map_lock(TTransactionId transactionId); |
296 | | |
297 | | txn_tablet_map_t& _get_txn_tablet_map(TTransactionId transactionId); |
298 | | |
299 | | txn_partition_map_t& _get_txn_partition_map(TTransactionId transactionId); |
300 | | |
301 | | inline std::shared_mutex& _get_txn_lock(TTransactionId transactionId); |
302 | | |
303 | | std::shared_mutex& _get_txn_tablet_delta_writer_map_lock(TTransactionId transactionId); |
304 | | |
305 | | txn_tablet_delta_writer_map_t& _get_txn_tablet_delta_writer_map(TTransactionId transactionId); |
306 | | |
307 | | // Insert or remove (transaction_id, partition_id) from _txn_partition_map |
308 | | // get _txn_map_lock before calling. |
309 | | void _insert_txn_partition_map_unlocked(int64_t transaction_id, int64_t partition_id); |
310 | | void _clear_txn_partition_map_unlocked(int64_t transaction_id, int64_t partition_id); |
311 | | |
312 | | void _remove_txn_tablet_info_unlocked(TPartitionId partition_id, TTransactionId transaction_id, |
313 | | TTabletId tablet_id, TabletUid tablet_uid, |
314 | | std::lock_guard<std::shared_mutex>& txn_lock, |
315 | | std::lock_guard<std::shared_mutex>& wrlock); |
316 | | |
317 | | class TabletVersionCache : public LRUCachePolicy { |
318 | | public: |
319 | | TabletVersionCache(size_t capacity) |
320 | 509 | : LRUCachePolicy(CachePolicy::CacheType::TABLET_VERSION_CACHE, capacity, |
321 | 509 | LRUCacheType::NUMBER, /*sweeptime*/ -1, |
322 | 509 | /*num_shards*/ 32, |
323 | 509 | /*element_count_capacity*/ 0, /*enable_prune*/ false, |
324 | 509 | /*is_lru_k*/ false) {} |
325 | | }; |
326 | | |
327 | | private: |
328 | | StorageEngine& _engine; |
329 | | |
330 | | const int32_t _txn_map_shard_size; |
331 | | |
332 | | const int32_t _txn_shard_size; |
333 | | |
334 | | // _txn_map_locks[i] protect _txn_tablet_maps[i], i=0,1,2...,and i < _txn_map_shard_size |
335 | | txn_tablet_map_t* _txn_tablet_maps = nullptr; |
336 | | // transaction_id -> corresponding partition ids |
337 | | // This is mainly for the clear txn task received from FE, which may only has transaction id, |
338 | | // so we need this map to find out which partitions are corresponding to a transaction id. |
339 | | // The _txn_partition_maps[i] should be constructed/deconstructed/modified alongside with '_txn_tablet_maps[i]' |
340 | | txn_partition_map_t* _txn_partition_maps = nullptr; |
341 | | |
342 | | std::shared_mutex* _txn_map_locks = nullptr; |
343 | | |
344 | | std::shared_mutex* _txn_mutex = nullptr; |
345 | | |
346 | | txn_tablet_delta_writer_map_t* _txn_tablet_delta_writer_map = nullptr; |
347 | | std::unique_ptr<TabletVersionCache> _tablet_version_cache; |
348 | | std::shared_mutex* _txn_tablet_delta_writer_map_locks = nullptr; |
349 | | DISALLOW_COPY_AND_ASSIGN(TxnManager); |
350 | | }; // TxnManager |
351 | | |
352 | 200 | inline std::shared_mutex& TxnManager::_get_txn_map_lock(TTransactionId transactionId) { |
353 | 200 | return _txn_map_locks[transactionId & (_txn_map_shard_size - 1)]; |
354 | 200 | } |
355 | | |
356 | 198 | inline TxnManager::txn_tablet_map_t& TxnManager::_get_txn_tablet_map(TTransactionId transactionId) { |
357 | 198 | return _txn_tablet_maps[transactionId & (_txn_map_shard_size - 1)]; |
358 | 198 | } |
359 | | |
360 | | inline TxnManager::txn_partition_map_t& TxnManager::_get_txn_partition_map( |
361 | 138 | TTransactionId transactionId) { |
362 | 138 | return _txn_partition_maps[transactionId & (_txn_map_shard_size - 1)]; |
363 | 138 | } |
364 | | |
365 | 76 | inline std::shared_mutex& TxnManager::_get_txn_lock(TTransactionId transactionId) { |
366 | 76 | return _txn_mutex[transactionId & (_txn_shard_size - 1)]; |
367 | 76 | } |
368 | | |
369 | | inline std::shared_mutex& TxnManager::_get_txn_tablet_delta_writer_map_lock( |
370 | 0 | TTransactionId transactionId) { |
371 | 0 | return _txn_tablet_delta_writer_map_locks[transactionId & (_txn_map_shard_size - 1)]; |
372 | 0 | } |
373 | | |
374 | | inline TxnManager::txn_tablet_delta_writer_map_t& TxnManager::_get_txn_tablet_delta_writer_map( |
375 | 0 | TTransactionId transactionId) { |
376 | 0 | return _txn_tablet_delta_writer_map[transactionId & (_txn_map_shard_size - 1)]; |
377 | 0 | } |
378 | | |
379 | | } // namespace doris |