be/src/storage/rowset/rowset_meta_manager.cpp
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 | | #include "storage/rowset/rowset_meta_manager.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/olap_file.pb.h> |
22 | | |
23 | | #include <boost/algorithm/string/trim.hpp> |
24 | | #include <fstream> |
25 | | #include <functional> |
26 | | #include <memory> |
27 | | #include <new> |
28 | | #include <string> |
29 | | #include <string_view> |
30 | | #include <vector> |
31 | | |
32 | | #include "common/logging.h" |
33 | | #include "storage/binlog.h" |
34 | | #include "storage/olap_define.h" |
35 | | #include "storage/olap_meta.h" |
36 | | #include "storage/utils.h" |
37 | | #include "util/debug_points.h" |
38 | | |
39 | | namespace doris { |
40 | | using namespace ErrorCode; |
41 | | |
42 | | bool RowsetMetaManager::check_rowset_meta(OlapMeta* meta, TabletUid tablet_uid, |
43 | 0 | const RowsetId& rowset_id) { |
44 | 0 | std::string key = ROWSET_PREFIX + tablet_uid.to_string() + "_" + rowset_id.to_string(); |
45 | 0 | std::string value; |
46 | 0 | return meta->key_may_exist(META_COLUMN_FAMILY_INDEX, key, &value); |
47 | 0 | } |
48 | | |
49 | 45 | Status RowsetMetaManager::exists(OlapMeta* meta, TabletUid tablet_uid, const RowsetId& rowset_id) { |
50 | 45 | std::string key = ROWSET_PREFIX + tablet_uid.to_string() + "_" + rowset_id.to_string(); |
51 | 45 | std::string value; |
52 | 45 | return meta->get(META_COLUMN_FAMILY_INDEX, key, &value); |
53 | 45 | } |
54 | | |
55 | | Status RowsetMetaManager::get_rowset_meta(OlapMeta* meta, TabletUid tablet_uid, |
56 | | const RowsetId& rowset_id, |
57 | 16 | RowsetMetaSharedPtr rowset_meta) { |
58 | 16 | std::string key = ROWSET_PREFIX + tablet_uid.to_string() + "_" + rowset_id.to_string(); |
59 | 16 | std::string value; |
60 | 16 | Status s = meta->get(META_COLUMN_FAMILY_INDEX, key, &value); |
61 | 16 | if (s.is<META_KEY_NOT_FOUND>()) { |
62 | 3 | return Status::Error<META_KEY_NOT_FOUND>("rowset id: {} not found.", key); |
63 | 13 | } else if (!s.ok()) { |
64 | 0 | return Status::Error<IO_ERROR>("load rowset id: {} failed.", key); |
65 | 0 | } |
66 | 13 | bool ret = rowset_meta->init(value); |
67 | 13 | if (!ret) { |
68 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>("parse rowset meta failed. rowset id: {}", |
69 | 0 | key); |
70 | 0 | } |
71 | 13 | return Status::OK(); |
72 | 13 | } |
73 | | |
74 | | Status RowsetMetaManager::save(OlapMeta* meta, TabletUid tablet_uid, const RowsetId& rowset_id, |
75 | | const RowsetMetaPB& rowset_meta_pb, |
76 | | std::optional<BinlogFormatPB> binlog_format, |
77 | 77 | const std::optional<RowsetMetaPB>& attach_row_binlog_rowset_meta) { |
78 | 77 | if (rowset_meta_pb.partition_id() <= 0) { |
79 | 22 | LOG(WARNING) << "invalid partition id " << rowset_meta_pb.partition_id() << " tablet " |
80 | 22 | << rowset_meta_pb.tablet_id(); |
81 | | // TODO(dx): after fix partition id eq 0 bug, fix it |
82 | | // return Status::InternalError("invaid partition id {} tablet {}", |
83 | | // rowset_meta_pb.partition_id(), rowset_meta_pb.tablet_id()); |
84 | 22 | } |
85 | 77 | DBUG_EXECUTE_IF("RowsetMetaManager::save::zero_partition_id", { |
86 | 77 | long partition_id = rowset_meta_pb.partition_id(); |
87 | 77 | auto& rs_pb = const_cast<std::decay_t<decltype(rowset_meta_pb)>&>(rowset_meta_pb); |
88 | 77 | rs_pb.set_partition_id(0); |
89 | 77 | LOG(WARNING) << "set debug point RowsetMetaManager::save::zero_partition_id old=" |
90 | 77 | << partition_id << " new=" << rowset_meta_pb.DebugString(); |
91 | 77 | }); |
92 | 77 | if (!binlog_format.has_value()) { |
93 | 69 | return _save(meta, tablet_uid, rowset_id, rowset_meta_pb); |
94 | 69 | } |
95 | 8 | if (*binlog_format == BinlogFormatPB::STATEMENT_AND_SNAPSHOT) { |
96 | 2 | return _save_with_ccr_binlog(meta, tablet_uid, rowset_id, rowset_meta_pb); |
97 | 2 | } |
98 | 8 | DCHECK_EQ(*binlog_format, BinlogFormatPB::ROW); |
99 | 6 | DCHECK(attach_row_binlog_rowset_meta.has_value()); |
100 | 6 | return _save_with_row_binlog(meta, tablet_uid, rowset_id, rowset_meta_pb, |
101 | 6 | *attach_row_binlog_rowset_meta); |
102 | 8 | } |
103 | | |
104 | | Status RowsetMetaManager::_save(OlapMeta* meta, TabletUid tablet_uid, const RowsetId& rowset_id, |
105 | 69 | const RowsetMetaPB& rowset_meta_pb) { |
106 | 69 | std::string key = |
107 | 69 | fmt::format("{}{}_{}", ROWSET_PREFIX, tablet_uid.to_string(), rowset_id.to_string()); |
108 | 69 | std::string value; |
109 | 69 | if (!rowset_meta_pb.SerializeToString(&value)) { |
110 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>("serialize rowset pb failed. rowset id:{}", |
111 | 0 | key); |
112 | 0 | } |
113 | | |
114 | 69 | return meta->put(META_COLUMN_FAMILY_INDEX, key, value); |
115 | 69 | } |
116 | | |
117 | | Status RowsetMetaManager::_save_with_ccr_binlog(OlapMeta* meta, TabletUid tablet_uid, |
118 | | const RowsetId& rowset_id, |
119 | 2 | const RowsetMetaPB& rowset_meta_pb) { |
120 | | // create rowset write data |
121 | 2 | std::string rowset_key = |
122 | 2 | fmt::format("{}{}_{}", ROWSET_PREFIX, tablet_uid.to_string(), rowset_id.to_string()); |
123 | 2 | std::string rowset_value; |
124 | 2 | if (!rowset_meta_pb.SerializeToString(&rowset_value)) { |
125 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>("serialize rowset pb failed. rowset id:{}", |
126 | 0 | rowset_key); |
127 | 0 | } |
128 | | |
129 | | // create binlog write data |
130 | | // binlog_meta_key format: {kBinlogPrefix}meta_{tablet_uid}_{version}_{rowset_id} |
131 | | // binlog_data_key format: {kBinlogPrefix}data_{tablet_uid}_{version}_{rowset_id} |
132 | | // version is formatted to 20 bytes to avoid the problem of sorting, version is lower, timestamp is lower |
133 | | // binlog key is not supported for cumulative rowset |
134 | 2 | if (rowset_meta_pb.start_version() != rowset_meta_pb.end_version()) { |
135 | 0 | return Status::Error<ROWSET_BINLOG_NOT_ONLY_ONE_VERSION>( |
136 | 0 | "binlog key is not supported for cumulative rowset. rowset id:{}", rowset_key); |
137 | 0 | } |
138 | 2 | auto version = rowset_meta_pb.start_version(); |
139 | 2 | std::string binlog_meta_key = make_binlog_meta_key(tablet_uid, version, rowset_id); |
140 | 2 | std::string binlog_data_key = make_binlog_data_key(tablet_uid, version, rowset_id); |
141 | 2 | BinlogMetaEntryPB binlog_meta_entry_pb; |
142 | 2 | binlog_meta_entry_pb.set_version(version); |
143 | 2 | binlog_meta_entry_pb.set_tablet_id(rowset_meta_pb.tablet_id()); |
144 | 2 | binlog_meta_entry_pb.set_rowset_id(rowset_meta_pb.rowset_id()); |
145 | 2 | binlog_meta_entry_pb.set_num_segments(rowset_meta_pb.num_segments()); |
146 | 2 | binlog_meta_entry_pb.set_creation_time(rowset_meta_pb.creation_time()); |
147 | 2 | binlog_meta_entry_pb.set_rowset_id_v2(rowset_meta_pb.rowset_id_v2()); |
148 | 2 | std::string binlog_meta_value; |
149 | 2 | if (!binlog_meta_entry_pb.SerializeToString(&binlog_meta_value)) { |
150 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>("serialize binlog pb failed. rowset id:{}", |
151 | 0 | binlog_meta_key); |
152 | 0 | } |
153 | | |
154 | | // create batch entries |
155 | 2 | std::vector<OlapMeta::BatchEntry> entries = { |
156 | 2 | {std::cref(rowset_key), std::cref(rowset_value)}, |
157 | 2 | {std::cref(binlog_meta_key), std::cref(binlog_meta_value)}, |
158 | 2 | {std::cref(binlog_data_key), std::cref(rowset_value)}}; |
159 | | |
160 | 2 | return meta->put(META_COLUMN_FAMILY_INDEX, entries); |
161 | 2 | } |
162 | | |
163 | | Status RowsetMetaManager::_save_with_row_binlog(OlapMeta* meta, TabletUid tablet_uid, |
164 | | const RowsetId& rowset_id, |
165 | | const RowsetMetaPB& rowset_meta_pb, |
166 | 6 | const RowsetMetaPB& attach_row_binlog_rowset_meta) { |
167 | 6 | std::string rowset_key = |
168 | 6 | fmt::format("{}{}_{}", ROWSET_PREFIX, tablet_uid.to_string(), rowset_id.to_string()); |
169 | 6 | std::string rowset_value; |
170 | 6 | if (!rowset_meta_pb.SerializeToString(&rowset_value)) { |
171 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>("serialize rowset pb failed. rowset id:{}", |
172 | 0 | rowset_key); |
173 | 0 | } |
174 | | |
175 | | // save binlog rowset meta as a normal rowset under its own tablet uid in the same batch. |
176 | 6 | TabletUid row_binlog_tablet_uid(attach_row_binlog_rowset_meta.tablet_uid()); |
177 | 6 | RowsetId row_binlog_rowset_id; |
178 | 6 | row_binlog_rowset_id.init(attach_row_binlog_rowset_meta.rowset_id_v2()); |
179 | 6 | std::string row_binlog_rowset_key = |
180 | 6 | fmt::format("{}{}_{}", ROWSET_PREFIX, row_binlog_tablet_uid.to_string(), |
181 | 6 | row_binlog_rowset_id.to_string()); |
182 | 6 | std::string row_binlog_rowset_value; |
183 | 6 | if (!attach_row_binlog_rowset_meta.SerializeToString(&row_binlog_rowset_value)) { |
184 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>("serialize rowset pb failed. rowset id:{}", |
185 | 0 | row_binlog_rowset_key); |
186 | 0 | } |
187 | | |
188 | 6 | std::vector<OlapMeta::BatchEntry> entries = { |
189 | 6 | {std::cref(rowset_key), std::cref(rowset_value)}, |
190 | 6 | {std::cref(row_binlog_rowset_key), std::cref(row_binlog_rowset_value)}}; |
191 | 6 | return meta->put(META_COLUMN_FAMILY_INDEX, entries); |
192 | 6 | } |
193 | | |
194 | | std::vector<std::string> RowsetMetaManager::get_binlog_filenames(OlapMeta* meta, |
195 | | TabletUid tablet_uid, |
196 | | std::string_view binlog_version, |
197 | 0 | int64_t segment_idx) { |
198 | 0 | auto prefix_key = make_binlog_filename_key(tablet_uid, binlog_version); |
199 | 0 | VLOG_DEBUG << fmt::format("prefix_key:{}", prefix_key); |
200 | |
|
201 | 0 | std::vector<std::string> binlog_files; |
202 | 0 | std::string rowset_id; |
203 | 0 | int64_t num_segments = -1; |
204 | 0 | auto traverse_func = [&rowset_id, &num_segments](std::string_view key, |
205 | 0 | std::string_view value) -> bool { |
206 | 0 | VLOG_DEBUG << fmt::format("key:{}, value:{}", key, value); |
207 | | // key is 'binlog_meta_6943f1585fe834b5-e542c2b83a21d0b7_00000000000000000069_020000000000000135449d7cd7eadfe672aa0f928fa99593', extract last part '020000000000000135449d7cd7eadfe672aa0f928fa99593' |
208 | | // check starts with "binlog_meta_" |
209 | 0 | if (!starts_with_binlog_meta(key)) { |
210 | 0 | LOG(WARNING) << fmt::format("invalid binlog meta key:{}", key); |
211 | 0 | return false; |
212 | 0 | } |
213 | 0 | if (auto pos = key.rfind('_'); pos == std::string::npos) { |
214 | 0 | LOG(WARNING) << fmt::format("invalid binlog meta key:{}", key); |
215 | 0 | return false; |
216 | 0 | } else { |
217 | 0 | rowset_id = key.substr(pos + 1); |
218 | 0 | } |
219 | | |
220 | 0 | BinlogMetaEntryPB binlog_meta_entry_pb; |
221 | 0 | if (!binlog_meta_entry_pb.ParseFromArray(value.data(), cast_set<int32_t>(value.size()))) { |
222 | 0 | LOG(WARNING) << fmt::format("invalid binlog meta value:{}", value); |
223 | 0 | return false; |
224 | 0 | } |
225 | 0 | num_segments = binlog_meta_entry_pb.num_segments(); |
226 | |
|
227 | 0 | return false; |
228 | 0 | }; |
229 | | |
230 | | // get binlog meta by prefix |
231 | 0 | Status status = meta->iterate(META_COLUMN_FAMILY_INDEX, prefix_key, traverse_func); |
232 | 0 | if (!status.ok() || rowset_id.empty() || num_segments < 0) { |
233 | 0 | LOG(WARNING) << fmt::format( |
234 | 0 | "fail to get binlog filename. tablet uid:{}, binlog version:{}, status:{}, " |
235 | 0 | "rowset_id:{}, num_segments:{}", |
236 | 0 | tablet_uid.to_string(), binlog_version, status.to_string(), rowset_id, |
237 | 0 | num_segments); |
238 | 0 | } |
239 | | |
240 | | // construct binlog_files list |
241 | 0 | if (segment_idx >= num_segments) { |
242 | 0 | LOG(WARNING) << fmt::format("invalid segment idx:{}, num_segments:{}", segment_idx, |
243 | 0 | num_segments); |
244 | 0 | return binlog_files; |
245 | 0 | } |
246 | 0 | for (int64_t i = 0; i < num_segments; ++i) { |
247 | | // TODO(Drogon): Update to filesystem path |
248 | 0 | auto segment_file = fmt::format("{}_{}.dat", rowset_id, i); |
249 | 0 | binlog_files.emplace_back(std::move(segment_file)); |
250 | 0 | } |
251 | 0 | return binlog_files; |
252 | 0 | } |
253 | | |
254 | | std::pair<std::string, int64_t> RowsetMetaManager::get_binlog_info( |
255 | 0 | OlapMeta* meta, TabletUid tablet_uid, std::string_view binlog_version) { |
256 | 0 | VLOG_DEBUG << fmt::format("tablet_uid:{}, binlog_version:{}", tablet_uid.to_string(), |
257 | 0 | binlog_version); |
258 | 0 | auto prefix_key = make_binlog_filename_key(tablet_uid, binlog_version); |
259 | 0 | VLOG_DEBUG << fmt::format("prefix_key:{}", prefix_key); |
260 | |
|
261 | 0 | std::string rowset_id; |
262 | 0 | int64_t num_segments = -1; |
263 | 0 | auto traverse_func = [&rowset_id, &num_segments](std::string_view key, |
264 | 0 | std::string_view value) -> bool { |
265 | 0 | VLOG_DEBUG << fmt::format("key:{}, value:{}", key, value); |
266 | | // key is 'binlog_meta_6943f1585fe834b5-e542c2b83a21d0b7_00000000000000000069_020000000000000135449d7cd7eadfe672aa0f928fa99593', extract last part '020000000000000135449d7cd7eadfe672aa0f928fa99593' |
267 | 0 | auto pos = key.rfind('_'); |
268 | 0 | if (pos == std::string::npos) { |
269 | 0 | LOG(WARNING) << fmt::format("invalid binlog meta key:{}", key); |
270 | 0 | return false; |
271 | 0 | } |
272 | 0 | rowset_id = key.substr(pos + 1); |
273 | |
|
274 | 0 | BinlogMetaEntryPB binlog_meta_entry_pb; |
275 | 0 | binlog_meta_entry_pb.ParseFromArray(value.data(), cast_set<int32_t>(value.size())); |
276 | 0 | num_segments = binlog_meta_entry_pb.num_segments(); |
277 | |
|
278 | 0 | return false; |
279 | 0 | }; |
280 | | |
281 | | // get binlog meta by prefix |
282 | 0 | Status status = meta->iterate(META_COLUMN_FAMILY_INDEX, prefix_key, traverse_func); |
283 | 0 | if (!status.ok() || rowset_id.empty() || num_segments < 0) { |
284 | 0 | LOG(WARNING) << fmt::format( |
285 | 0 | "fail to get binlog filename. tablet uid:{}, binlog version:{}, status:{}, " |
286 | 0 | "rowset_id:{}, num_segments:{}", |
287 | 0 | tablet_uid.to_string(), binlog_version, status.to_string(), rowset_id, |
288 | 0 | num_segments); |
289 | 0 | } |
290 | |
|
291 | 0 | return std::make_pair(rowset_id, num_segments); |
292 | 0 | } |
293 | | |
294 | | std::string RowsetMetaManager::get_rowset_binlog_meta(OlapMeta* meta, TabletUid tablet_uid, |
295 | | std::string_view binlog_version, |
296 | 0 | std::string_view rowset_id) { |
297 | 0 | auto binlog_data_key = make_binlog_data_key(tablet_uid.to_string(), binlog_version, rowset_id); |
298 | 0 | VLOG_DEBUG << fmt::format("get binlog_meta_key:{}", binlog_data_key); |
299 | |
|
300 | 0 | std::string binlog_meta_value; |
301 | 0 | Status status = meta->get(META_COLUMN_FAMILY_INDEX, binlog_data_key, &binlog_meta_value); |
302 | 0 | if (!status.ok()) { |
303 | 0 | LOG(WARNING) << fmt::format( |
304 | 0 | "fail to get binlog meta. tablet uid:{}, binlog version:{}, " |
305 | 0 | "rowset_id:{}, status:{}", |
306 | 0 | tablet_uid.to_string(), binlog_version, rowset_id, status.to_string()); |
307 | 0 | return ""; |
308 | 0 | } |
309 | 0 | return binlog_meta_value; |
310 | 0 | } |
311 | | |
312 | | Status RowsetMetaManager::get_rowset_binlog_metas(OlapMeta* meta, const TabletUid tablet_uid, |
313 | | const std::vector<int64_t>& binlog_versions, |
314 | 0 | RowsetBinlogMetasPB* metas_pb) { |
315 | 0 | if (binlog_versions.empty()) { |
316 | 0 | return _get_all_rowset_binlog_metas(meta, tablet_uid, metas_pb); |
317 | 0 | } else { |
318 | 0 | return _get_rowset_binlog_metas(meta, tablet_uid, binlog_versions, metas_pb); |
319 | 0 | } |
320 | 0 | } |
321 | | |
322 | | Status RowsetMetaManager::_get_rowset_binlog_metas(OlapMeta* meta, const TabletUid tablet_uid, |
323 | | const std::vector<int64_t>& binlog_versions, |
324 | 0 | RowsetBinlogMetasPB* metas_pb) { |
325 | 0 | Status status; |
326 | 0 | auto tablet_uid_str = tablet_uid.to_string(); |
327 | 0 | auto traverse_func = [meta, metas_pb, &status, &tablet_uid_str]( |
328 | 0 | std::string_view key, std::string_view value) -> bool { |
329 | 0 | VLOG_DEBUG << fmt::format("key:{}, value:{}", key, value); |
330 | 0 | if (!starts_with_binlog_meta(key)) { |
331 | 0 | auto err_msg = fmt::format("invalid binlog meta key:{}", key); |
332 | 0 | status = Status::InternalError(err_msg); |
333 | 0 | LOG(WARNING) << err_msg; |
334 | 0 | return false; |
335 | 0 | } |
336 | | |
337 | 0 | BinlogMetaEntryPB binlog_meta_entry_pb; |
338 | 0 | if (!binlog_meta_entry_pb.ParseFromArray(value.data(), cast_set<int32_t>(value.size()))) { |
339 | 0 | auto err_msg = fmt::format("fail to parse binlog meta value:{}", value); |
340 | 0 | status = Status::InternalError(err_msg); |
341 | 0 | LOG(WARNING) << err_msg; |
342 | 0 | return false; |
343 | 0 | } |
344 | 0 | auto& rowset_id = binlog_meta_entry_pb.rowset_id_v2(); |
345 | |
|
346 | 0 | auto binlog_meta_pb = metas_pb->add_rowset_binlog_metas(); |
347 | 0 | binlog_meta_pb->set_rowset_id(rowset_id); |
348 | 0 | binlog_meta_pb->set_version(binlog_meta_entry_pb.version()); |
349 | 0 | binlog_meta_pb->set_num_segments(binlog_meta_entry_pb.num_segments()); |
350 | 0 | binlog_meta_pb->set_meta_key(std::string {key}); |
351 | 0 | binlog_meta_pb->set_meta(std::string {value}); |
352 | |
|
353 | 0 | auto binlog_data_key = |
354 | 0 | make_binlog_data_key(tablet_uid_str, binlog_meta_entry_pb.version(), rowset_id); |
355 | 0 | std::string binlog_data; |
356 | 0 | status = meta->get(META_COLUMN_FAMILY_INDEX, binlog_data_key, &binlog_data); |
357 | 0 | if (!status.ok()) { |
358 | 0 | LOG(WARNING) << status.to_string(); |
359 | 0 | return false; |
360 | 0 | } |
361 | 0 | binlog_meta_pb->set_data_key(binlog_data_key); |
362 | 0 | binlog_meta_pb->set_data(binlog_data); |
363 | |
|
364 | 0 | return false; |
365 | 0 | }; |
366 | |
|
367 | 0 | for (auto& binlog_version : binlog_versions) { |
368 | 0 | auto prefix_key = make_binlog_meta_key_prefix(tablet_uid, binlog_version); |
369 | 0 | Status iterStatus = meta->iterate(META_COLUMN_FAMILY_INDEX, prefix_key, traverse_func); |
370 | 0 | if (!iterStatus.ok()) { |
371 | 0 | LOG(WARNING) << fmt::format("fail to iterate binlog meta. prefix_key:{}, status:{}", |
372 | 0 | prefix_key, iterStatus.to_string()); |
373 | 0 | return iterStatus; |
374 | 0 | } |
375 | 0 | if (!status.ok()) { |
376 | 0 | return status; |
377 | 0 | } |
378 | 0 | } |
379 | 0 | return status; |
380 | 0 | } |
381 | | |
382 | | Status RowsetMetaManager::get_rowset_binlog_metas(OlapMeta* meta, TabletUid tablet_uid, |
383 | 4 | Version version, RowsetBinlogMetasPB* metas_pb) { |
384 | 4 | Status status; |
385 | 4 | auto tablet_uid_str = tablet_uid.to_string(); |
386 | 4 | auto prefix_key = make_binlog_meta_key_prefix(tablet_uid); |
387 | 4 | auto begin_key = make_binlog_meta_key_prefix(tablet_uid, version.first); |
388 | 4 | auto end_key = make_binlog_meta_key_prefix(tablet_uid, version.second + 1); |
389 | 4 | auto traverse_func = [meta, metas_pb, &status, &tablet_uid_str, &end_key]( |
390 | 4 | std::string_view key, std::string_view value) -> bool { |
391 | 0 | VLOG_DEBUG << fmt::format("get rowset binlog metas, key={}, value={}", key, value); |
392 | 0 | if (key.compare(end_key) > 0) { // the binlog meta key is binary comparable. |
393 | | // All binlog meta has been scanned |
394 | 0 | return false; |
395 | 0 | } |
396 | | |
397 | 0 | if (!starts_with_binlog_meta(key)) { |
398 | 0 | auto err_msg = fmt::format("invalid binlog meta key:{}", key); |
399 | 0 | status = Status::InternalError(err_msg); |
400 | 0 | LOG(WARNING) << err_msg; |
401 | 0 | return false; |
402 | 0 | } |
403 | | |
404 | 0 | BinlogMetaEntryPB binlog_meta_entry_pb; |
405 | 0 | if (!binlog_meta_entry_pb.ParseFromArray(value.data(), cast_set<int32_t>(value.size()))) { |
406 | 0 | auto err_msg = fmt::format("fail to parse binlog meta value:{}", value); |
407 | 0 | status = Status::InternalError(err_msg); |
408 | 0 | LOG(WARNING) << err_msg; |
409 | 0 | return false; |
410 | 0 | } |
411 | | |
412 | 0 | const auto& rowset_id = binlog_meta_entry_pb.rowset_id_v2(); |
413 | 0 | auto* binlog_meta_pb = metas_pb->add_rowset_binlog_metas(); |
414 | 0 | binlog_meta_pb->set_rowset_id(rowset_id); |
415 | 0 | binlog_meta_pb->set_version(binlog_meta_entry_pb.version()); |
416 | 0 | binlog_meta_pb->set_num_segments(binlog_meta_entry_pb.num_segments()); |
417 | 0 | binlog_meta_pb->set_meta_key(std::string {key}); |
418 | 0 | binlog_meta_pb->set_meta(std::string {value}); |
419 | |
|
420 | 0 | auto binlog_data_key = |
421 | 0 | make_binlog_data_key(tablet_uid_str, binlog_meta_entry_pb.version(), rowset_id); |
422 | 0 | std::string binlog_data; |
423 | 0 | status = meta->get(META_COLUMN_FAMILY_INDEX, binlog_data_key, &binlog_data); |
424 | 0 | if (!status.ok()) { |
425 | 0 | LOG(WARNING) << status.to_string(); |
426 | 0 | return false; |
427 | 0 | } |
428 | 0 | binlog_meta_pb->set_data_key(binlog_data_key); |
429 | 0 | binlog_meta_pb->set_data(binlog_data); |
430 | |
|
431 | 0 | return true; |
432 | 0 | }; |
433 | | |
434 | 4 | Status iterStatus = |
435 | 4 | meta->iterate(META_COLUMN_FAMILY_INDEX, begin_key, prefix_key, traverse_func); |
436 | 4 | if (!iterStatus.ok()) { |
437 | 0 | LOG(WARNING) << fmt::format( |
438 | 0 | "fail to iterate binlog meta. prefix_key:{}, version:{}, status:{}", prefix_key, |
439 | 0 | version.to_string(), iterStatus.to_string()); |
440 | 0 | return iterStatus; |
441 | 0 | } |
442 | 4 | return status; |
443 | 4 | } |
444 | | |
445 | | Status RowsetMetaManager::_get_all_rowset_binlog_metas(OlapMeta* meta, const TabletUid tablet_uid, |
446 | 0 | RowsetBinlogMetasPB* metas_pb) { |
447 | 0 | Status status; |
448 | 0 | auto tablet_uid_str = tablet_uid.to_string(); |
449 | 0 | int64_t tablet_id = 0; |
450 | 0 | auto traverse_func = [meta, metas_pb, &status, &tablet_uid_str, &tablet_id]( |
451 | 0 | std::string_view key, std::string_view value) -> bool { |
452 | 0 | VLOG_DEBUG << fmt::format("key:{}, value:{}", key, value); |
453 | 0 | if (!starts_with_binlog_meta(key)) { |
454 | 0 | LOG(INFO) << fmt::format("end scan binlog meta. key:{}", key); |
455 | 0 | return false; |
456 | 0 | } |
457 | | |
458 | 0 | BinlogMetaEntryPB binlog_meta_entry_pb; |
459 | 0 | if (!binlog_meta_entry_pb.ParseFromArray(value.data(), cast_set<int32_t>(value.size()))) { |
460 | 0 | auto err_msg = fmt::format("fail to parse binlog meta value:{}", value); |
461 | 0 | status = Status::InternalError(err_msg); |
462 | 0 | LOG(WARNING) << err_msg; |
463 | 0 | return false; |
464 | 0 | } |
465 | 0 | if (tablet_id == 0) { |
466 | 0 | tablet_id = binlog_meta_entry_pb.tablet_id(); |
467 | 0 | } else if (tablet_id != binlog_meta_entry_pb.tablet_id()) { |
468 | | // scan all binlog meta, so tablet_id should be same: |
469 | 0 | return false; |
470 | 0 | } |
471 | 0 | auto& rowset_id = binlog_meta_entry_pb.rowset_id_v2(); |
472 | |
|
473 | 0 | auto binlog_meta_pb = metas_pb->add_rowset_binlog_metas(); |
474 | 0 | binlog_meta_pb->set_rowset_id(rowset_id); |
475 | 0 | binlog_meta_pb->set_version(binlog_meta_entry_pb.version()); |
476 | 0 | binlog_meta_pb->set_num_segments(binlog_meta_entry_pb.num_segments()); |
477 | 0 | binlog_meta_pb->set_meta_key(std::string {key}); |
478 | 0 | binlog_meta_pb->set_meta(std::string {value}); |
479 | |
|
480 | 0 | auto binlog_data_key = |
481 | 0 | make_binlog_data_key(tablet_uid_str, binlog_meta_entry_pb.version(), rowset_id); |
482 | 0 | std::string binlog_data; |
483 | 0 | status = meta->get(META_COLUMN_FAMILY_INDEX, binlog_data_key, &binlog_data); |
484 | 0 | if (!status.ok()) { |
485 | 0 | LOG(WARNING) << status; |
486 | 0 | return false; |
487 | 0 | } |
488 | 0 | binlog_meta_pb->set_data_key(binlog_data_key); |
489 | 0 | binlog_meta_pb->set_data(binlog_data); |
490 | |
|
491 | 0 | return true; |
492 | 0 | }; |
493 | |
|
494 | 0 | auto prefix_key = make_binlog_meta_key_prefix(tablet_uid); |
495 | 0 | Status iterStatus = meta->iterate(META_COLUMN_FAMILY_INDEX, prefix_key, traverse_func); |
496 | 0 | if (!iterStatus.ok()) { |
497 | 0 | LOG(WARNING) << fmt::format("fail to iterate binlog meta. prefix_key:{}, status:{}", |
498 | 0 | prefix_key, iterStatus.to_string()); |
499 | 0 | return iterStatus; |
500 | 0 | } |
501 | 0 | return status; |
502 | 0 | } |
503 | | |
504 | 11 | Status RowsetMetaManager::remove(OlapMeta* meta, TabletUid tablet_uid, const RowsetId& rowset_id) { |
505 | 11 | std::string key = ROWSET_PREFIX + tablet_uid.to_string() + "_" + rowset_id.to_string(); |
506 | 11 | VLOG_NOTICE << "start to remove rowset, key:" << key; |
507 | 11 | Status status = meta->remove(META_COLUMN_FAMILY_INDEX, key); |
508 | 11 | VLOG_NOTICE << "remove rowset key:" << key << " finished"; |
509 | 11 | return status; |
510 | 11 | } |
511 | | |
512 | 0 | Status RowsetMetaManager::remove_binlog(OlapMeta* meta, const std::string& suffix) { |
513 | | // Please do not remove std::vector<std::string>, more info refer to pr#23190 |
514 | 0 | return meta->remove(META_COLUMN_FAMILY_INDEX, |
515 | 0 | std::vector<std::string> {kBinlogMetaPrefix.data() + suffix, |
516 | 0 | kBinlogDataPrefix.data() + suffix}); |
517 | 0 | } |
518 | | |
519 | | Status RowsetMetaManager::ingest_binlog_metas(OlapMeta* meta, TabletUid tablet_uid, |
520 | 0 | RowsetBinlogMetasPB* metas_pb) { |
521 | 0 | std::vector<OlapMeta::BatchEntry> entries; |
522 | 0 | const auto tablet_uid_str = tablet_uid.to_string(); |
523 | |
|
524 | 0 | for (auto& rowset_binlog_meta : *metas_pb->mutable_rowset_binlog_metas()) { |
525 | 0 | auto& rowset_id = rowset_binlog_meta.rowset_id(); |
526 | 0 | auto version = rowset_binlog_meta.version(); |
527 | |
|
528 | 0 | auto meta_key = rowset_binlog_meta.mutable_meta_key(); |
529 | 0 | *meta_key = make_binlog_meta_key(tablet_uid_str, version, rowset_id); |
530 | 0 | auto data_key = rowset_binlog_meta.mutable_data_key(); |
531 | 0 | *data_key = make_binlog_data_key(tablet_uid_str, version, rowset_id); |
532 | |
|
533 | 0 | entries.emplace_back(*meta_key, rowset_binlog_meta.meta()); |
534 | 0 | entries.emplace_back(*data_key, rowset_binlog_meta.data()); |
535 | 0 | } |
536 | |
|
537 | 0 | return meta->put(META_COLUMN_FAMILY_INDEX, entries); |
538 | 0 | } |
539 | | |
540 | | Status RowsetMetaManager::traverse_rowset_metas( |
541 | | OlapMeta* meta, |
542 | 76 | std::function<bool(const TabletUid&, const RowsetId&, std::string_view)> const& func) { |
543 | 76 | auto traverse_rowset_meta_func = [&func](std::string_view key, std::string_view value) -> bool { |
544 | 0 | std::vector<std::string> parts; |
545 | | // key format: rst_uuid_rowset_id |
546 | 0 | RETURN_IF_ERROR(split_string(key, '_', &parts)); |
547 | 0 | if (parts.size() != 3) { |
548 | 0 | LOG(WARNING) << "invalid rowset key:" << key << ", splitted size:" << parts.size(); |
549 | 0 | return true; |
550 | 0 | } |
551 | 0 | RowsetId rowset_id; |
552 | 0 | rowset_id.init(parts[2]); |
553 | 0 | std::vector<std::string> uid_parts; |
554 | 0 | RETURN_IF_ERROR(split_string(parts[1], '-', &uid_parts)); |
555 | 0 | TabletUid tablet_uid(uid_parts[0], uid_parts[1]); |
556 | 0 | return func(tablet_uid, rowset_id, value); |
557 | 0 | }; |
558 | 76 | Status status = |
559 | 76 | meta->iterate(META_COLUMN_FAMILY_INDEX, ROWSET_PREFIX, traverse_rowset_meta_func); |
560 | 76 | return status; |
561 | 76 | } |
562 | | |
563 | | Status RowsetMetaManager::traverse_binlog_metas( |
564 | | OlapMeta* meta, |
565 | 0 | std::function<bool(std::string_view, std::string_view, bool)> const& collector) { |
566 | 0 | std::pair<std::string, bool> last_info = std::make_pair(kBinlogMetaPrefix.data(), false); |
567 | 0 | bool seek_found = false; |
568 | 0 | Status status; |
569 | 0 | auto traverse_binlog_meta_func = [&last_info, &seek_found, &collector]( |
570 | 0 | std::string_view key, std::string_view value) -> bool { |
571 | 0 | seek_found = true; |
572 | 0 | auto& [last_prefix, need_collect] = last_info; |
573 | 0 | size_t pos = key.find('_', kBinlogMetaPrefix.size()); |
574 | 0 | if (pos == std::string::npos) { |
575 | 0 | LOG(WARNING) << "invalid binlog meta key: " << key; |
576 | 0 | return true; |
577 | 0 | } |
578 | 0 | std::string_view key_view(key.data(), pos); |
579 | 0 | std::string_view last_prefix_view(last_prefix.data(), last_prefix.size() - 1); |
580 | |
|
581 | 0 | if (last_prefix_view != key_view) { |
582 | 0 | need_collect = collector(key, value, true); |
583 | 0 | last_prefix = std::string(key_view) + "~"; |
584 | 0 | } else if (need_collect) { |
585 | 0 | collector(key, value, false); |
586 | 0 | } |
587 | |
|
588 | 0 | return need_collect; |
589 | 0 | }; |
590 | |
|
591 | 0 | do { |
592 | 0 | seek_found = false; |
593 | 0 | status = meta->iterate(META_COLUMN_FAMILY_INDEX, last_info.first, kBinlogMetaPrefix.data(), |
594 | 0 | traverse_binlog_meta_func); |
595 | 0 | } while (status.ok() && seek_found); |
596 | |
|
597 | 0 | return status; |
598 | 0 | } |
599 | | |
600 | | Status RowsetMetaManager::save_partial_update_info( |
601 | | OlapMeta* meta, int64_t tablet_id, int64_t partition_id, int64_t txn_id, |
602 | 0 | const PartialUpdateInfoPB& partial_update_info_pb) { |
603 | 0 | std::string key = |
604 | 0 | fmt::format("{}{}_{}_{}", PARTIAL_UPDATE_INFO_PREFIX, tablet_id, partition_id, txn_id); |
605 | 0 | std::string value; |
606 | 0 | if (!partial_update_info_pb.SerializeToString(&value)) { |
607 | 0 | return Status::Error<SERIALIZE_PROTOBUF_ERROR>( |
608 | 0 | "serialize partial update info failed. key={}", key); |
609 | 0 | } |
610 | 0 | VLOG_NOTICE << "save partial update info, key=" << key << ", value_size=" << value.size(); |
611 | 0 | return meta->put(META_COLUMN_FAMILY_INDEX, key, value); |
612 | 0 | } |
613 | | |
614 | | Status RowsetMetaManager::try_get_partial_update_info(OlapMeta* meta, int64_t tablet_id, |
615 | | int64_t partition_id, int64_t txn_id, |
616 | 0 | PartialUpdateInfoPB* partial_update_info_pb) { |
617 | 0 | std::string key = |
618 | 0 | fmt::format("{}{}_{}_{}", PARTIAL_UPDATE_INFO_PREFIX, tablet_id, partition_id, txn_id); |
619 | 0 | std::string value; |
620 | 0 | Status status = meta->get(META_COLUMN_FAMILY_INDEX, key, &value); |
621 | 0 | if (status.is<META_KEY_NOT_FOUND>()) { |
622 | 0 | return status; |
623 | 0 | } |
624 | 0 | if (!status.ok()) { |
625 | 0 | LOG_WARNING("failed to get partial update info. tablet_id={}, partition_id={}, txn_id={}", |
626 | 0 | tablet_id, partition_id, txn_id); |
627 | 0 | return status; |
628 | 0 | } |
629 | 0 | if (!partial_update_info_pb->ParseFromString(value)) { |
630 | 0 | return Status::Error<ErrorCode::PARSE_PROTOBUF_ERROR>( |
631 | 0 | "fail to parse partial update info content to protobuf object. tablet_id={}, " |
632 | 0 | "partition_id={}, txn_id={}", |
633 | 0 | tablet_id, partition_id, txn_id); |
634 | 0 | } |
635 | 0 | return Status::OK(); |
636 | 0 | } |
637 | | |
638 | | Status RowsetMetaManager::traverse_partial_update_info( |
639 | | OlapMeta* meta, |
640 | 0 | std::function<bool(int64_t, int64_t, int64_t, std::string_view)> const& func) { |
641 | 0 | auto traverse_partial_update_info_func = [&func](std::string_view key, |
642 | 0 | std::string_view value) -> bool { |
643 | 0 | std::vector<std::string> parts; |
644 | | // key format: pui_{tablet_id}_{partition_id}_{txn_id} |
645 | 0 | RETURN_IF_ERROR(split_string(key, '_', &parts)); |
646 | 0 | if (parts.size() != 4) { |
647 | 0 | LOG_WARNING("invalid rowset key={}, splitted size={}", key, parts.size()); |
648 | 0 | return true; |
649 | 0 | } |
650 | 0 | int64_t tablet_id = std::stoll(parts[1]); |
651 | 0 | int64_t partition_id = std::stoll(parts[2]); |
652 | 0 | int64_t txn_id = std::stoll(parts[3]); |
653 | 0 | return func(tablet_id, partition_id, txn_id, value); |
654 | 0 | }; |
655 | 0 | return meta->iterate(META_COLUMN_FAMILY_INDEX, PARTIAL_UPDATE_INFO_PREFIX, |
656 | 0 | traverse_partial_update_info_func); |
657 | 0 | } |
658 | | |
659 | | Status RowsetMetaManager::remove_partial_update_info(OlapMeta* meta, int64_t tablet_id, |
660 | 0 | int64_t partition_id, int64_t txn_id) { |
661 | 0 | std::string key = |
662 | 0 | fmt::format("{}{}_{}_{}", PARTIAL_UPDATE_INFO_PREFIX, tablet_id, partition_id, txn_id); |
663 | 0 | Status res = meta->remove(META_COLUMN_FAMILY_INDEX, key); |
664 | 0 | VLOG_NOTICE << "remove partial update info, key=" << key; |
665 | 0 | return res; |
666 | 0 | } |
667 | | |
668 | | Status RowsetMetaManager::remove_partial_update_infos( |
669 | 0 | OlapMeta* meta, const std::vector<std::tuple<int64_t, int64_t, int64_t>>& keys) { |
670 | 0 | std::vector<std::string> remove_keys; |
671 | 0 | for (auto [tablet_id, partition_id, txn_id] : keys) { |
672 | 0 | remove_keys.push_back(fmt::format("{}{}_{}_{}", PARTIAL_UPDATE_INFO_PREFIX, tablet_id, |
673 | 0 | partition_id, txn_id)); |
674 | 0 | } |
675 | 0 | Status res = meta->remove(META_COLUMN_FAMILY_INDEX, remove_keys); |
676 | 0 | VLOG_NOTICE << "remove partial update info, remove_keys.size()=" << remove_keys.size(); |
677 | 0 | return res; |
678 | 0 | } |
679 | | |
680 | | Status RowsetMetaManager::remove_tablet_related_partial_update_info(OlapMeta* meta, |
681 | 0 | int64_t tablet_id) { |
682 | 0 | std::string prefix = fmt::format("{}{}", PARTIAL_UPDATE_INFO_PREFIX, tablet_id); |
683 | 0 | std::vector<std::string> remove_keys; |
684 | 0 | auto get_remove_keys_func = [&](std::string_view key, std::string_view val) -> bool { |
685 | 0 | remove_keys.emplace_back(key); |
686 | 0 | return true; |
687 | 0 | }; |
688 | 0 | VLOG_NOTICE << "remove tablet related partial update info, tablet_id: " << tablet_id |
689 | 0 | << " removed keys size: " << remove_keys.size(); |
690 | 0 | RETURN_IF_ERROR(meta->iterate(META_COLUMN_FAMILY_INDEX, prefix, get_remove_keys_func)); |
691 | 0 | return meta->remove(META_COLUMN_FAMILY_INDEX, remove_keys); |
692 | 0 | } |
693 | | } // namespace doris |