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