Coverage Report

Created: 2026-06-23 15:30

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