Coverage Report

Created: 2025-04-22 21:03

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