Coverage Report

Created: 2026-08-07 16:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_txn_delete_bitmap_cache.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 <mutex>
21
22
#include "storage/olap_common.h"
23
#include "storage/partial_update_info.h"
24
#include "storage/rowset/rowset.h"
25
#include "storage/tablet/tablet_meta.h"
26
#include "storage/txn/txn_manager.h"
27
#include "util/countdown_latch.h"
28
#include "util/lru_cache.h"
29
30
namespace doris {
31
32
// Record transaction related delete bitmaps using a lru cache.
33
class CloudTxnDeleteBitmapCache : public LRUCachePolicy {
34
public:
35
    CloudTxnDeleteBitmapCache(size_t size_in_bytes);
36
37
    ~CloudTxnDeleteBitmapCache() override;
38
39
    Status init();
40
41
    Status get_tablet_txn_info(TTransactionId transaction_id, int64_t tablet_id,
42
                               RowsetSharedPtr* rowset, DeleteBitmapPtr* delete_bitmap,
43
                               RowsetIdUnorderedSet* rowset_ids, int64_t* txn_expiration,
44
                               std::shared_ptr<PartialUpdateInfo>* partial_update_info,
45
                               std::shared_ptr<PublishStatus>* publish_status,
46
                               TxnPublishInfo* previous_publish_info,
47
                               RowBinlogTxnInfo* attach_row_binlog = nullptr);
48
49
    void set_tablet_txn_info(TTransactionId transaction_id, int64_t tablet_id,
50
                             DeleteBitmapPtr delete_bitmap, const RowsetIdUnorderedSet& rowset_ids,
51
                             RowsetSharedPtr rowset, int64_t txn_expirationm,
52
                             std::shared_ptr<PartialUpdateInfo> partial_update_info,
53
                             const RowBinlogTxnInfo& attach_row_binlog = {});
54
55
    Status update_tablet_txn_info(TTransactionId transaction_id, int64_t tablet_id,
56
                                  DeleteBitmapPtr delete_bitmap,
57
                                  const RowsetIdUnorderedSet& rowset_ids,
58
                                  PublishStatus publish_status, TxnPublishInfo publish_info = {});
59
60
    void remove_expired_tablet_txn_info();
61
62
    void remove_unused_tablet_txn_info(TTransactionId transaction_id, int64_t tablet_id);
63
64
    // Mark a rowset as empty/skipped (lightweight marker, no rowset stored)
65
    // Used for empty rowsets when skip_writing_empty_rowset_metadata is enabled
66
    void mark_empty_rowset(TTransactionId txn_id, int64_t tablet_id, int64_t txn_expiration);
67
68
    // Check if this is a known empty/skipped rowset
69
    // Returns true if was marked as empty rowset
70
    // Note: Does not remove the marker, as CalcDeleteBitmapTask may retry.
71
    // Cleanup is handled by expiration-based removal in remove_expired_tablet_txn_info()
72
    bool is_empty_rowset(TTransactionId txn_id, int64_t tablet_id);
73
74
    // !!!ATTENTION!!!: the delete bitmap stored in CloudTxnDeleteBitmapCache contains sentinel marks,
75
    // and the version in BitmapKey is DeleteBitmap::TEMP_VERSION_COMMON.
76
    // when using delete bitmap from this cache, the caller should manually remove these marks if don't need it
77
    // and should replace versions in BitmapKey by the correct version
78
    Status get_delete_bitmap(TTransactionId transaction_id, int64_t tablet_id,
79
                             DeleteBitmapPtr* delete_bitmap, RowsetIdUnorderedSet* rowset_ids,
80
                             std::shared_ptr<PublishStatus>* publish_status);
81
82
    // the caller should guarantee that the txn `transaction_id` has been published successfully in MS
83
    Result<std::pair<RowsetSharedPtr, DeleteBitmapPtr>> get_rowset_and_delete_bitmap(
84
            TTransactionId transaction_id, int64_t tablet_id);
85
86
private:
87
    void _clean_thread_callback();
88
89
    class DeleteBitmapCacheValue : public LRUCacheValueBase {
90
    public:
91
        DeleteBitmapPtr delete_bitmap;
92
        // records rowsets calc in commit txn
93
        RowsetIdUnorderedSet rowset_ids;
94
95
        DeleteBitmapCacheValue(DeleteBitmapPtr delete_bitmap_, const RowsetIdUnorderedSet& ids_)
96
0
                : delete_bitmap(std::move(delete_bitmap_)), rowset_ids(ids_) {}
97
    };
98
99
    struct TxnKey {
100
        TTransactionId txn_id;
101
        int64_t tablet_id;
102
        TxnKey(TTransactionId txn_id_, int64_t tablet_id_)
103
0
                : txn_id(txn_id_), tablet_id(tablet_id_) {}
104
0
        auto operator<=>(const TxnKey&) const = default;
105
    };
106
107
    struct TxnVal {
108
        RowsetSharedPtr rowset;
109
        int64_t txn_expiration;
110
        std::shared_ptr<PartialUpdateInfo> partial_update_info;
111
        std::shared_ptr<PublishStatus> publish_status = nullptr;
112
        // used to determine if the retry needs to re-calculate the delete bitmap
113
        TxnPublishInfo publish_info;
114
        RowBinlogTxnInfo attach_row_binlog;
115
0
        TxnVal() : txn_expiration(0) {};
116
        TxnVal(RowsetSharedPtr rowset_, int64_t txn_expiration_,
117
               std::shared_ptr<PartialUpdateInfo> partial_update_info_,
118
               std::shared_ptr<PublishStatus> publish_status_,
119
               const RowBinlogTxnInfo& attach_row_binlog_)
120
0
                : rowset(std::move(rowset_)),
121
0
                  txn_expiration(txn_expiration_),
122
0
                  partial_update_info(std::move(partial_update_info_)),
123
0
                  publish_status(std::move(publish_status_)),
124
0
                  attach_row_binlog(attach_row_binlog_) {}
125
    };
126
127
    std::map<TxnKey, TxnVal> _txn_map;
128
    std::multimap<int64_t, TxnKey> _expiration_txn;
129
    // Lightweight markers for empty/skipped rowsets (only stores TxnKey, ~16 bytes per entry)
130
    // Used to track empty rowsets that were not committed to meta-service
131
    std::set<TxnKey> _empty_rowset_markers;
132
    std::shared_mutex _rwlock;
133
    std::shared_ptr<Thread> _clean_thread;
134
    CountDownLatch _stop_latch;
135
};
136
137
} // namespace doris