Coverage Report

Created: 2026-10-19 02:26

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