Coverage Report

Created: 2026-09-22 06:55

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