Coverage Report

Created: 2026-05-12 22:35

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
38
namespace doris {
39
40
class AutoIncIDBuffer;
41
struct PartialUpdateInfo;
42
struct MowContext;
43
44
// Row binlog op type.
45
// NOTE: The value is persisted into row binlog data, so keep it stable.
46
static constexpr int64_t ROW_BINLOG_APPEND = 0;
47
static constexpr int64_t ROW_BINLOG_UPDATE = 1;
48
static constexpr int64_t ROW_BINLOG_DELETE = 2;
49
50
constexpr std::string_view kBinlogPrefix = "binlog_";
51
constexpr std::string_view kBinlogMetaPrefix = "binlog_meta_";
52
constexpr std::string_view kBinlogDataPrefix = "binlog_data_";
53
constexpr std::string_view kRowBinlogPrefix = "binlog_row_";
54
constexpr std::string_view kRowBinlogLsnColName = "__DORIS_BINLOG_LSN__";
55
constexpr std::string_view kRowBinlogTimestampColName = "__DORIS_BINLOG_TIMESTAMP__";
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
                                  size_t num_rows,
141
5
                                  std::shared_ptr<std::vector<int128_t>>* lsn_ids) {
142
5
    if (lsn_buffer == nullptr) {
143
0
        return Status::InternalError("binlog<row> try to get lsn buffer but null");
144
0
    }
145
5
    DCHECK(lsn_ids != nullptr);
146
5
    DCHECK(num_rows > 0);
147
148
5
    std::vector<std::pair<int64_t, size_t>> ranges;
149
5
    RETURN_IF_ERROR(lsn_buffer->sync_request_ids(num_rows, &ranges));
150
151
5
    auto ids = std::make_shared<std::vector<int128_t>>();
152
5
    ids->reserve(num_rows);
153
5
    for (const auto& [start, length] : ranges) {
154
12
        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
            ids->push_back(static_cast<int128_t>(start + static_cast<int64_t>(i)));
157
7
        }
158
5
    }
159
5
    DCHECK_EQ(ids->size(), num_rows);
160
5
    *lsn_ids = std::move(ids);
161
5
    return Status::OK();
162
5
}
163
164
namespace segment_v2 {
165
166
class SegmentWriteBinlogLsnMap {
167
public:
168
5
    void insert_seg_lsn(int64_t seg_id, std::shared_ptr<std::vector<int128_t>> lsn_ids) {
169
5
        std::lock_guard<std::mutex> l(_mutex);
170
5
        _seg_id_to_lsn_ids.emplace(seg_id, std::move(lsn_ids));
171
5
    }
172
173
2
    void remove_seg(int64_t seg_id) {
174
2
        std::lock_guard<std::mutex> l(_mutex);
175
2
        _seg_id_to_lsn_ids.erase(seg_id);
176
2
    }
177
178
3
    std::shared_ptr<const std::vector<int128_t>> get_seg_lsn(int64_t seg_id) const {
179
3
        std::lock_guard<std::mutex> l(_mutex);
180
3
        auto it = _seg_id_to_lsn_ids.find(seg_id);
181
3
        CHECK(it != _seg_id_to_lsn_ids.end())
182
0
                << "SegmentWriteBinlogLsnMap::get_seg_lsn missing seg_id=" << seg_id
183
0
                << ", existing_seg_ids=[" << ([&] {
184
0
                       std::string s;
185
0
                       for (const auto& [id, _] : _seg_id_to_lsn_ids) {
186
0
                           if (!s.empty()) {
187
0
                               s.push_back(',');
188
0
                           }
189
0
                           s.append(std::to_string(id));
190
0
                       }
191
0
                       return s;
192
0
                   }())
193
0
                << "]";
194
3
        return it->second;
195
3
    }
196
197
private:
198
    mutable std::mutex _mutex;
199
    std::map<int64_t, std::shared_ptr<std::vector<int128_t>>> _seg_id_to_lsn_ids;
200
};
201
202
struct SegmentWriteBinlogOptions {
203
public:
204
    bool write_before = false;
205
206
    // source context, used for retrieving historical row and building binlog<row> block
207
    struct SourceWriteDataOptions {
208
        TabletSchemaSPtr tablet_schema = nullptr;
209
        std::shared_ptr<PartialUpdateInfo> partial_update_info;
210
        std::shared_ptr<MowContext> mow_context;
211
        bool is_transient_rowset_writer = false;
212
        DataWriteType source_write_type = DataWriteType::TYPE_DEFAULT;
213
    } source;
214
215
5
    void insert_seg_lsn(int64_t seg_id, std::shared_ptr<std::vector<int128_t>> lsn_ids) {
216
5
        DCHECK(lsn_map != nullptr);
217
5
        lsn_map->insert_seg_lsn(seg_id, std::move(lsn_ids));
218
5
    }
219
220
2
    void remove_seg(int64_t seg_id) {
221
2
        DCHECK(lsn_map != nullptr);
222
2
        lsn_map->remove_seg(seg_id);
223
2
    }
224
225
3
    std::shared_ptr<const std::vector<int128_t>> get_seg_lsn(int64_t seg_id) const {
226
        DCHECK(lsn_map != nullptr);
227
3
        return lsn_map->get_seg_lsn(seg_id);
228
3
    }
229
230
    // Shared LSN storage for row-binlog writers.
231
    // Keep it as a pointer so SegmentWriteBinlogOptions stays copyable.
232
    std::shared_ptr<SegmentWriteBinlogLsnMap> lsn_map =
233
            std::make_shared<SegmentWriteBinlogLsnMap>();
234
};
235
} // namespace segment_v2
236
237
} // namespace doris