Coverage Report

Created: 2026-08-07 14:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/binlog.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 <fmt/format.h>
21
22
#include <limits>
23
#include <map>
24
#include <memory>
25
#include <mutex>
26
#include <string>
27
#include <string_view>
28
#include <utility>
29
#include <vector>
30
31
#include "common/logging.h" // DCHECK
32
#include "common/status.h"
33
#include "exec/sink/autoinc_buffer.h"
34
#include "storage/olap_common.h"
35
#include "storage/olap_define.h"          // DataWriteType
36
#include "storage/tablet/tablet_fwd.h"    // BaseTabletSPtr
37
#include "storage/tablet/tablet_schema.h" // TabletSchemaSPtr
38
#include "storage/utils.h"                // BINLOG_*_COL
39
40
namespace doris {
41
42
class AutoIncIDBuffer;
43
struct PartialUpdateInfo;
44
struct MowContext;
45
46
// Row binlog op type.
47
// NOTE: The value is persisted into row binlog data, so keep it stable.
48
static constexpr int64_t ROW_BINLOG_APPEND = 0;
49
static constexpr int64_t ROW_BINLOG_UPDATE = 1;
50
static constexpr int64_t ROW_BINLOG_DELETE = 2;
51
52
constexpr std::string_view kBinlogPrefix = "binlog_";
53
constexpr std::string_view kBinlogMetaPrefix = "binlog_meta_";
54
constexpr std::string_view kBinlogDataPrefix = "binlog_data_";
55
constexpr std::string_view kRowBinlogPrefix = "binlog_row_";
56
57
constexpr int64_t kBinlogLsnAutoIncId = -1;
58
// used in file directory
59
constexpr std::string_view FDRowBinlogSuffix = "_row_binlog";
60
61
inline auto make_binlog_meta_key(const std::string_view tablet, int64_t version,
62
2
                                 const std::string_view rowset) {
63
2
    return fmt::format("{}meta_{}_{:020d}_{}", kBinlogPrefix, tablet, version, rowset);
64
2
}
65
66
inline auto make_binlog_meta_key(const std::string_view tablet, const std::string_view version_str,
67
0
                                 const std::string_view rowset) {
68
0
    // TODO(Drogon): use fmt::format not convert to version_num, only string with length prefix '0'
69
0
    int64_t version = std::atoll(version_str.data());
70
0
    return make_binlog_meta_key(tablet, version, rowset);
71
0
}
72
73
inline auto make_binlog_meta_key(const TabletUid& tablet_uid, int64_t version,
74
2
                                 const RowsetId& rowset_id) {
75
2
    return make_binlog_meta_key(tablet_uid.to_string(), version, rowset_id.to_string());
76
2
}
77
78
6
inline auto make_binlog_meta_key_prefix(const TabletUid& tablet_uid) {
79
6
    return fmt::format("{}meta_{}_", kBinlogPrefix, tablet_uid.to_string());
80
6
}
81
82
10
inline auto make_binlog_meta_key_prefix(const TabletUid& tablet_uid, int64_t version) {
83
10
    return fmt::format("{}meta_{}_{:020d}_", kBinlogPrefix, tablet_uid.to_string(), version);
84
10
}
85
86
inline auto make_binlog_data_key(const std::string_view tablet, int64_t version,
87
2
                                 const std::string_view rowset) {
88
2
    return fmt::format("{}data_{}_{:020d}_{}", kBinlogPrefix, tablet, version, rowset);
89
2
}
90
91
inline auto make_binlog_data_key(const std::string_view tablet, const std::string_view version,
92
0
                                 const std::string_view rowset) {
93
0
    return fmt::format("{}data_{}_{:0>20}_{}", kBinlogPrefix, tablet, version, rowset);
94
0
}
95
96
inline auto make_binlog_data_key(const TabletUid& tablet_uid, int64_t version,
97
2
                                 const RowsetId& rowset_id) {
98
2
    return make_binlog_data_key(tablet_uid.to_string(), version, rowset_id.to_string());
99
2
}
100
101
inline auto make_binlog_data_key(const TabletUid& tablet_uid, int64_t version,
102
0
                                 const std::string_view rowset_id) {
103
0
    return make_binlog_data_key(tablet_uid.to_string(), version, rowset_id);
104
0
}
105
106
0
inline auto make_binlog_data_key_prefix(const TabletUid& tablet_uid, int64_t version) {
107
0
    return fmt::format("{}data_{}_{:020d}_", kBinlogPrefix, tablet_uid.to_string(), version);
108
0
}
109
110
0
inline auto make_binlog_filename_key(const TabletUid& tablet_uid, const std::string_view version) {
111
0
    return fmt::format("{}meta_{}_{:0>20}_", kBinlogPrefix, tablet_uid.to_string(), version);
112
0
}
113
114
0
inline bool starts_with_binlog_meta(const std::string_view str) {
115
0
    auto prefix = kBinlogMetaPrefix;
116
0
    if (prefix.length() > str.length()) {
117
0
        return false;
118
0
    }
119
120
0
    return str.compare(0, prefix.length(), prefix) == 0;
121
0
}
122
123
1
inline std::string get_binlog_data_key_from_meta_key(const std::string_view meta_key) {
124
    // like "binlog_meta_6943f1585fe834b5-e542c2b83a21d0b7" => "binlog_data-6943f1585fe834b5-e542c2b83a21d0b7"
125
1
    return fmt::format("{}data_{}", kBinlogPrefix, meta_key.substr(kBinlogMetaPrefix.length()));
126
1
}
127
128
// Allocate per-row LSNs for row-binlog data.
129
// The caller must provide a valid auto-inc buffer (typically from GlobalAutoIncBuffers).
130
inline Status allocate_binlog_lsn(const std::shared_ptr<AutoIncIDBuffer>& lsn_buffer,
131
4
                                  size_t num_rows, std::vector<int64_t>& lsn_ids) {
132
4
    if (lsn_buffer == nullptr) {
133
0
        return Status::InternalError("binlog<row> try to get lsn buffer but null");
134
0
    }
135
4
    DCHECK(num_rows > 0);
136
137
4
    std::vector<std::pair<int64_t, size_t>> ranges;
138
4
    RETURN_IF_ERROR(lsn_buffer->sync_request_ids(num_rows, &ranges));
139
140
4
    lsn_ids.clear();
141
4
    lsn_ids.reserve(num_rows);
142
4
    for (const auto& [start, length] : ranges) {
143
11
        for (size_t i = 0; i < length; ++i) {
144
7
            DCHECK_LE(start, std::numeric_limits<int64_t>::max() - static_cast<int64_t>(i));
145
7
            lsn_ids.push_back(start + static_cast<int64_t>(i));
146
7
        }
147
4
    }
148
4
    DCHECK_EQ(lsn_ids.size(), num_rows);
149
4
    return Status::OK();
150
4
}
151
152
constexpr int64_t kTsoLogicalBits = 18;
153
154
0
inline int64_t extract_tso_physical_time(int64_t tso) {
155
0
    return tso <= 0 ? 0 : tso >> kTsoLogicalBits;
156
0
}
157
158
namespace segment_v2 {
159
160
class SegmentWriteBinlogLsnMap {
161
public:
162
31
    void insert_seg_lsn(int64_t seg_id, std::shared_ptr<std::vector<int64_t>> lsn_ids) {
163
31
        std::lock_guard<std::mutex> l(_mutex);
164
31
        _seg_id_to_lsn_ids.emplace(seg_id, std::move(lsn_ids));
165
31
    }
166
167
24
    void remove_seg(int64_t seg_id) {
168
24
        std::lock_guard<std::mutex> l(_mutex);
169
24
        _seg_id_to_lsn_ids.erase(seg_id);
170
24
    }
171
172
25
    std::shared_ptr<const std::vector<int64_t>> get_seg_lsn(int64_t seg_id) const {
173
25
        std::lock_guard<std::mutex> l(_mutex);
174
25
        auto it = _seg_id_to_lsn_ids.find(seg_id);
175
25
        CHECK(it != _seg_id_to_lsn_ids.end())
176
0
                << "SegmentWriteBinlogLsnMap::get_seg_lsn missing seg_id=" << seg_id
177
0
                << ", existing_seg_ids=[" << ([&] {
178
0
                       std::string s;
179
0
                       for (const auto& [id, _] : _seg_id_to_lsn_ids) {
180
0
                           if (!s.empty()) {
181
0
                               s.push_back(',');
182
0
                           }
183
0
                           s.append(std::to_string(id));
184
0
                       }
185
0
                       return s;
186
0
                   }())
187
0
                << "]";
188
25
        return it->second;
189
25
    }
190
191
private:
192
    mutable std::mutex _mutex;
193
    std::map<int64_t, std::shared_ptr<std::vector<int64_t>>> _seg_id_to_lsn_ids;
194
};
195
196
struct SegmentWriteBinlogOptions {
197
public:
198
    bool write_before = false;
199
200
    // source context, used for retrieving historical row and building binlog<row> block
201
    struct SourceWriteDataOptions {
202
        BaseTabletSPtr base_tablet = nullptr;
203
        TabletSchemaSPtr tablet_schema = nullptr;
204
        std::shared_ptr<PartialUpdateInfo> partial_update_info;
205
        std::shared_ptr<MowContext> mow_context;
206
        bool is_transient_rowset_writer = false;
207
        DataWriteType source_write_type = DataWriteType::TYPE_DEFAULT;
208
        // input rowset's row-binlog, read LSN from it at publish time
209
        RowsetSharedPtr row_binlog_rowset = nullptr;
210
    } source;
211
212
31
    void insert_seg_lsn(int64_t seg_id, std::shared_ptr<std::vector<int64_t>> lsn_ids) {
213
31
        DCHECK(lsn_map != nullptr);
214
31
        lsn_map->insert_seg_lsn(seg_id, std::move(lsn_ids));
215
31
    }
216
217
24
    void remove_seg(int64_t seg_id) {
218
24
        DCHECK(lsn_map != nullptr);
219
24
        lsn_map->remove_seg(seg_id);
220
24
    }
221
222
25
    std::shared_ptr<const std::vector<int64_t>> get_seg_lsn(int64_t seg_id) const {
223
        DCHECK(lsn_map != nullptr);
224
25
        return lsn_map->get_seg_lsn(seg_id);
225
25
    }
226
227
    // Shared LSN storage for row-binlog writers.
228
    // Keep it as a pointer so SegmentWriteBinlogOptions stays copyable.
229
    std::shared_ptr<SegmentWriteBinlogLsnMap> lsn_map =
230
            std::make_shared<SegmentWriteBinlogLsnMap>();
231
};
232
} // namespace segment_v2
233
234
} // namespace doris