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 | 63 | const RowsetId& binlog_rowset_id) { |
133 | 63 | return fmt::format("{}{}_{}_{}", kRowBinlogPrefix, tablet_uid.to_string(), |
134 | 63 | rowset_id.to_string(), binlog_rowset_id.to_string()); |
135 | 63 | } |
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 | 5 | size_t num_rows, std::shared_ptr<std::vector<int64_t>>* lsn_ids) { |
141 | 5 | if (lsn_buffer == nullptr) { |
142 | 0 | return Status::InternalError("binlog<row> try to get lsn buffer but null"); |
143 | 0 | } |
144 | 5 | DCHECK(lsn_ids != nullptr); |
145 | 5 | DCHECK(num_rows > 0); |
146 | | |
147 | 5 | std::vector<std::pair<int64_t, size_t>> ranges; |
148 | 5 | RETURN_IF_ERROR(lsn_buffer->sync_request_ids(num_rows, &ranges)); |
149 | | |
150 | 5 | auto ids = std::make_shared<std::vector<int64_t>>(); |
151 | 5 | ids->reserve(num_rows); |
152 | 5 | for (const auto& [start, length] : ranges) { |
153 | 12 | for (size_t i = 0; i < length; ++i) { |
154 | 7 | DCHECK_LE(start, std::numeric_limits<int64_t>::max() - static_cast<int64_t>(i)); |
155 | 7 | ids->push_back(start + static_cast<int64_t>(i)); |
156 | 7 | } |
157 | 5 | } |
158 | 5 | DCHECK_EQ(ids->size(), num_rows); |
159 | 5 | *lsn_ids = std::move(ids); |
160 | 5 | return Status::OK(); |
161 | 5 | } |
162 | | |
163 | | constexpr int64_t kTsoLogicalBits = 18; |
164 | | |
165 | 0 | inline int64_t extract_tso_physical_time(int64_t tso) { |
166 | 0 | return tso <= 0 ? 0 : tso >> kTsoLogicalBits; |
167 | 0 | } |
168 | | |
169 | 0 | inline int128_t make_row_binlog_lsn(int64_t tso, int128_t row_id) { |
170 | 0 | static constexpr int128_t kLow64Mask = (static_cast<int128_t>(1) << 64) - 1; |
171 | 0 | return (static_cast<int128_t>(tso) << 64) | (row_id & kLow64Mask); |
172 | 0 | } |
173 | | |
174 | | namespace segment_v2 { |
175 | | |
176 | | class SegmentWriteBinlogLsnMap { |
177 | | public: |
178 | 5 | void insert_seg_lsn(int64_t seg_id, std::shared_ptr<std::vector<int64_t>> lsn_ids) { |
179 | 5 | std::lock_guard<std::mutex> l(_mutex); |
180 | 5 | _seg_id_to_lsn_ids.emplace(seg_id, std::move(lsn_ids)); |
181 | 5 | } |
182 | | |
183 | 2 | void remove_seg(int64_t seg_id) { |
184 | 2 | std::lock_guard<std::mutex> l(_mutex); |
185 | 2 | _seg_id_to_lsn_ids.erase(seg_id); |
186 | 2 | } |
187 | | |
188 | 3 | std::shared_ptr<const std::vector<int64_t>> get_seg_lsn(int64_t seg_id) const { |
189 | 3 | std::lock_guard<std::mutex> l(_mutex); |
190 | 3 | auto it = _seg_id_to_lsn_ids.find(seg_id); |
191 | 3 | CHECK(it != _seg_id_to_lsn_ids.end()) |
192 | 0 | << "SegmentWriteBinlogLsnMap::get_seg_lsn missing seg_id=" << seg_id |
193 | 0 | << ", existing_seg_ids=[" << ([&] { |
194 | 0 | std::string s; |
195 | 0 | for (const auto& [id, _] : _seg_id_to_lsn_ids) { |
196 | 0 | if (!s.empty()) { |
197 | 0 | s.push_back(','); |
198 | 0 | } |
199 | 0 | s.append(std::to_string(id)); |
200 | 0 | } |
201 | 0 | return s; |
202 | 0 | }()) |
203 | 0 | << "]"; |
204 | 3 | return it->second; |
205 | 3 | } |
206 | | |
207 | | private: |
208 | | mutable std::mutex _mutex; |
209 | | std::map<int64_t, std::shared_ptr<std::vector<int64_t>>> _seg_id_to_lsn_ids; |
210 | | }; |
211 | | |
212 | | struct SegmentWriteBinlogOptions { |
213 | | public: |
214 | | bool write_before = false; |
215 | | |
216 | | // source context, used for retrieving historical row and building binlog<row> block |
217 | | struct SourceWriteDataOptions { |
218 | | TabletSchemaSPtr tablet_schema = nullptr; |
219 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
220 | | std::shared_ptr<MowContext> mow_context; |
221 | | bool is_transient_rowset_writer = false; |
222 | | DataWriteType source_write_type = DataWriteType::TYPE_DEFAULT; |
223 | | } source; |
224 | | |
225 | 5 | void insert_seg_lsn(int64_t seg_id, std::shared_ptr<std::vector<int64_t>> lsn_ids) { |
226 | 5 | DCHECK(lsn_map != nullptr); |
227 | 5 | lsn_map->insert_seg_lsn(seg_id, std::move(lsn_ids)); |
228 | 5 | } |
229 | | |
230 | 2 | void remove_seg(int64_t seg_id) { |
231 | 2 | DCHECK(lsn_map != nullptr); |
232 | 2 | lsn_map->remove_seg(seg_id); |
233 | 2 | } |
234 | | |
235 | 3 | std::shared_ptr<const std::vector<int64_t>> get_seg_lsn(int64_t seg_id) const { |
236 | | DCHECK(lsn_map != nullptr); |
237 | 3 | return lsn_map->get_seg_lsn(seg_id); |
238 | 3 | } |
239 | | |
240 | | // Shared LSN storage for row-binlog writers. |
241 | | // Keep it as a pointer so SegmentWriteBinlogOptions stays copyable. |
242 | | std::shared_ptr<SegmentWriteBinlogLsnMap> lsn_map = |
243 | | std::make_shared<SegmentWriteBinlogLsnMap>(); |
244 | | }; |
245 | | } // namespace segment_v2 |
246 | | |
247 | | } // namespace doris |