Coverage Report

Created: 2026-08-14 13:56

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