Coverage Report

Created: 2026-08-27 20:04

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