/root/doris/be/src/olap/schema_change.cpp
Line | Count | Source (jump to first uncovered line) |
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 "olap/schema_change.h" |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <glog/logging.h> |
22 | | #include <thrift/protocol/TDebugProtocol.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <exception> |
26 | | #include <map> |
27 | | #include <memory> |
28 | | #include <mutex> |
29 | | #include <roaring/roaring.hh> |
30 | | #include <tuple> |
31 | | #include <utility> |
32 | | |
33 | | #include "agent/be_exec_version_manager.h" |
34 | | #include "cloud/cloud_schema_change_job.h" |
35 | | #include "cloud/config.h" |
36 | | #include "common/consts.h" |
37 | | #include "common/logging.h" |
38 | | #include "common/signal_handler.h" |
39 | | #include "common/status.h" |
40 | | #include "exec/schema_scanner/schema_metadata_name_ids_scanner.h" |
41 | | #include "gutil/integral_types.h" |
42 | | #include "gutil/strings/numbers.h" |
43 | | #include "io/fs/file_system.h" |
44 | | #include "io/io_common.h" |
45 | | #include "olap/base_tablet.h" |
46 | | #include "olap/data_dir.h" |
47 | | #include "olap/delete_handler.h" |
48 | | #include "olap/field.h" |
49 | | #include "olap/iterators.h" |
50 | | #include "olap/merger.h" |
51 | | #include "olap/olap_common.h" |
52 | | #include "olap/olap_define.h" |
53 | | #include "olap/rowset/beta_rowset.h" |
54 | | #include "olap/rowset/pending_rowset_helper.h" |
55 | | #include "olap/rowset/rowset_meta.h" |
56 | | #include "olap/rowset/rowset_reader_context.h" |
57 | | #include "olap/rowset/rowset_writer_context.h" |
58 | | #include "olap/rowset/segment_v2/column_reader.h" |
59 | | #include "olap/rowset/segment_v2/inverted_index_desc.h" |
60 | | #include "olap/rowset/segment_v2/inverted_index_writer.h" |
61 | | #include "olap/rowset/segment_v2/segment.h" |
62 | | #include "olap/schema.h" |
63 | | #include "olap/segment_loader.h" |
64 | | #include "olap/storage_engine.h" |
65 | | #include "olap/tablet.h" |
66 | | #include "olap/tablet_fwd.h" |
67 | | #include "olap/tablet_manager.h" |
68 | | #include "olap/tablet_meta.h" |
69 | | #include "olap/tablet_schema.h" |
70 | | #include "olap/types.h" |
71 | | #include "olap/utils.h" |
72 | | #include "olap/wrapper_field.h" |
73 | | #include "runtime/exec_env.h" |
74 | | #include "runtime/memory/mem_tracker.h" |
75 | | #include "runtime/runtime_state.h" |
76 | | #include "util/debug_points.h" |
77 | | #include "util/defer_op.h" |
78 | | #include "util/trace.h" |
79 | | #include "vec/aggregate_functions/aggregate_function.h" |
80 | | #include "vec/aggregate_functions/aggregate_function_reader.h" |
81 | | #include "vec/columns/column.h" |
82 | | #include "vec/columns/column_nullable.h" |
83 | | #include "vec/common/assert_cast.h" |
84 | | #include "vec/common/schema_util.h" |
85 | | #include "vec/core/block.h" |
86 | | #include "vec/core/column_with_type_and_name.h" |
87 | | #include "vec/exprs/vexpr.h" |
88 | | #include "vec/exprs/vexpr_context.h" |
89 | | #include "vec/olap/olap_data_convertor.h" |
90 | | |
91 | | namespace doris { |
92 | | class CollectionValue; |
93 | | |
94 | | using namespace ErrorCode; |
95 | | |
96 | | constexpr int ALTER_TABLE_BATCH_SIZE = 4064; |
97 | | |
98 | | class MultiBlockMerger { |
99 | | public: |
100 | 0 | MultiBlockMerger(BaseTabletSPtr tablet) : _tablet(tablet), _cmp(*tablet) {} |
101 | | |
102 | | Status merge(const std::vector<std::unique_ptr<vectorized::Block>>& blocks, |
103 | 0 | RowsetWriter* rowset_writer, uint64_t* merged_rows) { |
104 | 0 | int rows = 0; |
105 | 0 | for (const auto& block : blocks) { |
106 | 0 | rows += block->rows(); |
107 | 0 | } |
108 | 0 | if (!rows) { |
109 | 0 | return Status::OK(); |
110 | 0 | } |
111 | | |
112 | 0 | std::vector<RowRef> row_refs; |
113 | 0 | row_refs.reserve(rows); |
114 | 0 | for (const auto& block : blocks) { |
115 | 0 | for (uint16_t i = 0; i < block->rows(); i++) { |
116 | 0 | row_refs.emplace_back(block.get(), i); |
117 | 0 | } |
118 | 0 | } |
119 | | // TODO: try to use pdqsort to replace std::sort |
120 | | // The block version is incremental. |
121 | 0 | std::stable_sort(row_refs.begin(), row_refs.end(), _cmp); |
122 | |
|
123 | 0 | auto finalized_block = _tablet->tablet_schema()->create_block(); |
124 | 0 | int columns = finalized_block.columns(); |
125 | 0 | *merged_rows += rows; |
126 | |
|
127 | 0 | if (_tablet->keys_type() == KeysType::AGG_KEYS) { |
128 | 0 | auto tablet_schema = _tablet->tablet_schema(); |
129 | 0 | int key_number = _tablet->num_key_columns(); |
130 | |
|
131 | 0 | std::vector<vectorized::AggregateFunctionPtr> agg_functions; |
132 | 0 | std::vector<vectorized::AggregateDataPtr> agg_places; |
133 | |
|
134 | 0 | for (int i = key_number; i < columns; i++) { |
135 | 0 | try { |
136 | 0 | vectorized::AggregateFunctionPtr function = |
137 | 0 | tablet_schema->column(i).get_aggregate_function( |
138 | 0 | vectorized::AGG_LOAD_SUFFIX, |
139 | 0 | tablet_schema->column(i).get_be_exec_version()); |
140 | 0 | agg_functions.push_back(function); |
141 | | // create aggregate data |
142 | 0 | auto* place = new char[function->size_of_data()]; |
143 | 0 | function->create(place); |
144 | 0 | agg_places.push_back(place); |
145 | 0 | } catch (...) { |
146 | 0 | for (int j = 0; j < i - key_number; ++j) { |
147 | 0 | agg_functions[j]->destroy(agg_places[j]); |
148 | 0 | delete[] agg_places[j]; |
149 | 0 | } |
150 | 0 | throw; |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | 0 | DEFER({ |
155 | 0 | for (int i = 0; i < columns - key_number; i++) { |
156 | 0 | agg_functions[i]->destroy(agg_places[i]); |
157 | 0 | delete[] agg_places[i]; |
158 | 0 | } |
159 | 0 | }); |
160 | |
|
161 | 0 | for (int i = 0; i < rows; i++) { |
162 | 0 | auto row_ref = row_refs[i]; |
163 | 0 | for (int j = key_number; j < columns; j++) { |
164 | 0 | const auto* column_ptr = row_ref.get_column(j).get(); |
165 | 0 | agg_functions[j - key_number]->add( |
166 | 0 | agg_places[j - key_number], |
167 | 0 | const_cast<const vectorized::IColumn**>(&column_ptr), row_ref.position, |
168 | 0 | &_arena); |
169 | 0 | } |
170 | |
|
171 | 0 | if (i == rows - 1 || _cmp.compare(row_refs[i], row_refs[i + 1])) { |
172 | 0 | for (int j = 0; j < key_number; j++) { |
173 | 0 | finalized_block.get_by_position(j).column->assume_mutable()->insert_from( |
174 | 0 | *row_ref.get_column(j), row_ref.position); |
175 | 0 | } |
176 | |
|
177 | 0 | for (int j = key_number; j < columns; j++) { |
178 | 0 | agg_functions[j - key_number]->insert_result_into( |
179 | 0 | agg_places[j - key_number], |
180 | 0 | finalized_block.get_by_position(j).column->assume_mutable_ref()); |
181 | 0 | agg_functions[j - key_number]->reset(agg_places[j - key_number]); |
182 | 0 | } |
183 | |
|
184 | 0 | if (i == rows - 1 || finalized_block.rows() == ALTER_TABLE_BATCH_SIZE) { |
185 | 0 | *merged_rows -= finalized_block.rows(); |
186 | 0 | RETURN_IF_ERROR(rowset_writer->add_block(&finalized_block)); |
187 | 0 | finalized_block.clear_column_data(); |
188 | 0 | } |
189 | 0 | } |
190 | 0 | } |
191 | 0 | } else { |
192 | 0 | std::vector<RowRef> pushed_row_refs; |
193 | 0 | if (_tablet->keys_type() == KeysType::DUP_KEYS) { |
194 | 0 | std::swap(pushed_row_refs, row_refs); |
195 | 0 | } else if (_tablet->keys_type() == KeysType::UNIQUE_KEYS) { |
196 | 0 | for (int i = 0; i < rows; i++) { |
197 | 0 | if (i == rows - 1 || _cmp.compare(row_refs[i], row_refs[i + 1])) { |
198 | 0 | pushed_row_refs.push_back(row_refs[i]); |
199 | 0 | } |
200 | 0 | } |
201 | 0 | } |
202 | | |
203 | | // update real inserted row number |
204 | 0 | rows = pushed_row_refs.size(); |
205 | 0 | *merged_rows -= rows; |
206 | |
|
207 | 0 | for (int i = 0; i < rows; i += ALTER_TABLE_BATCH_SIZE) { |
208 | 0 | int limit = std::min(ALTER_TABLE_BATCH_SIZE, rows - i); |
209 | |
|
210 | 0 | for (int idx = 0; idx < columns; idx++) { |
211 | 0 | auto column = finalized_block.get_by_position(idx).column->assume_mutable(); |
212 | |
|
213 | 0 | for (int j = 0; j < limit; j++) { |
214 | 0 | auto row_ref = pushed_row_refs[i + j]; |
215 | 0 | column->insert_from(*row_ref.get_column(idx), row_ref.position); |
216 | 0 | } |
217 | 0 | } |
218 | 0 | RETURN_IF_ERROR(rowset_writer->add_block(&finalized_block)); |
219 | 0 | finalized_block.clear_column_data(); |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | 0 | RETURN_IF_ERROR(rowset_writer->flush()); |
224 | 0 | return Status::OK(); |
225 | 0 | } |
226 | | |
227 | | private: |
228 | | struct RowRef { |
229 | | RowRef(vectorized::Block* block_, uint16_t position_) |
230 | 0 | : block(block_), position(position_) {} |
231 | 0 | vectorized::ColumnPtr get_column(int index) const { |
232 | 0 | return block->get_by_position(index).column; |
233 | 0 | } |
234 | | const vectorized::Block* block; |
235 | | uint16_t position; |
236 | | }; |
237 | | |
238 | | struct RowRefComparator { |
239 | 0 | RowRefComparator(const BaseTablet& tablet) : _num_columns(tablet.num_key_columns()) {} |
240 | | |
241 | 0 | int compare(const RowRef& lhs, const RowRef& rhs) const { |
242 | 0 | return lhs.block->compare_at(lhs.position, rhs.position, _num_columns, *rhs.block, -1); |
243 | 0 | } |
244 | | |
245 | 0 | bool operator()(const RowRef& lhs, const RowRef& rhs) const { |
246 | 0 | return compare(lhs, rhs) < 0; |
247 | 0 | } |
248 | | |
249 | | const size_t _num_columns; |
250 | | }; |
251 | | |
252 | | BaseTabletSPtr _tablet; |
253 | | RowRefComparator _cmp; |
254 | | vectorized::Arena _arena; |
255 | | }; |
256 | | |
257 | | BlockChanger::BlockChanger(TabletSchemaSPtr tablet_schema, DescriptorTbl desc_tbl) |
258 | 0 | : _desc_tbl(std::move(desc_tbl)) { |
259 | 0 | _schema_mapping.resize(tablet_schema->num_columns()); |
260 | 0 | } |
261 | | |
262 | 0 | BlockChanger::~BlockChanger() { |
263 | 0 | for (auto it = _schema_mapping.begin(); it != _schema_mapping.end(); ++it) { |
264 | 0 | SAFE_DELETE(it->default_value); |
265 | 0 | } |
266 | 0 | _schema_mapping.clear(); |
267 | 0 | } |
268 | | |
269 | 0 | ColumnMapping* BlockChanger::get_mutable_column_mapping(size_t column_index) { |
270 | 0 | if (column_index >= _schema_mapping.size()) { |
271 | 0 | return nullptr; |
272 | 0 | } |
273 | | |
274 | 0 | return &(_schema_mapping[column_index]); |
275 | 0 | } |
276 | | |
277 | | Status BlockChanger::change_block(vectorized::Block* ref_block, |
278 | 0 | vectorized::Block* new_block) const { |
279 | 0 | std::unique_ptr<RuntimeState> state = RuntimeState::create_unique(); |
280 | 0 | state->set_desc_tbl(&_desc_tbl); |
281 | 0 | state->set_be_exec_version(_fe_compatible_version); |
282 | 0 | RowDescriptor row_desc = |
283 | 0 | RowDescriptor(_desc_tbl.get_tuple_descriptor(_desc_tbl.get_row_tuples()[0]), false); |
284 | |
|
285 | 0 | if (_where_expr != nullptr) { |
286 | 0 | vectorized::VExprContextSPtr ctx = nullptr; |
287 | 0 | RETURN_IF_ERROR(vectorized::VExpr::create_expr_tree(*_where_expr, ctx)); |
288 | 0 | RETURN_IF_ERROR(ctx->prepare(state.get(), row_desc)); |
289 | 0 | RETURN_IF_ERROR(ctx->open(state.get())); |
290 | | |
291 | 0 | RETURN_IF_ERROR( |
292 | 0 | vectorized::VExprContext::filter_block(ctx.get(), ref_block, ref_block->columns())); |
293 | 0 | } |
294 | | |
295 | 0 | const int row_num = ref_block->rows(); |
296 | 0 | const int new_schema_cols_num = new_block->columns(); |
297 | | |
298 | | // will be used for swaping ref_block[entry.first] and new_block[entry.second] |
299 | 0 | std::list<std::pair<int, int>> swap_idx_list; |
300 | 0 | for (int idx = 0; idx < new_schema_cols_num; idx++) { |
301 | 0 | auto expr = _schema_mapping[idx].expr; |
302 | 0 | if (expr != nullptr) { |
303 | 0 | vectorized::VExprContextSPtr ctx; |
304 | 0 | RETURN_IF_ERROR(vectorized::VExpr::create_expr_tree(*expr, ctx)); |
305 | 0 | RETURN_IF_ERROR(ctx->prepare(state.get(), row_desc)); |
306 | 0 | RETURN_IF_ERROR(ctx->open(state.get())); |
307 | | |
308 | 0 | int result_tmp_column_idx = -1; |
309 | 0 | RETURN_IF_ERROR(ctx->execute(ref_block, &result_tmp_column_idx)); |
310 | 0 | auto& result_tmp_column_def = ref_block->get_by_position(result_tmp_column_idx); |
311 | 0 | if (result_tmp_column_def.column == nullptr) { |
312 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
313 | 0 | "result column={} is nullptr, input expr={}", result_tmp_column_def.name, |
314 | 0 | apache::thrift::ThriftDebugString(*expr)); |
315 | 0 | } |
316 | 0 | ref_block->replace_by_position_if_const(result_tmp_column_idx); |
317 | |
|
318 | 0 | if (result_tmp_column_def.column->size() != row_num) { |
319 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
320 | 0 | "result size invalid, expect={}, real={}; input expr={}, block={}", row_num, |
321 | 0 | result_tmp_column_def.column->size(), |
322 | 0 | apache::thrift::ThriftDebugString(*expr), ref_block->dump_structure()); |
323 | 0 | } |
324 | | |
325 | 0 | if (_type == SCHEMA_CHANGE) { |
326 | | // danger casts (expected to be rejected by upstream caller) may cause data to be null and result in data loss in schema change |
327 | | // for rollup, this check is unecessary, and ref columns are not set in this case, it works on exprs |
328 | | |
329 | | // column_idx in base schema |
330 | 0 | int32_t ref_column_idx = _schema_mapping[idx].ref_column_idx; |
331 | 0 | DCHECK_GE(ref_column_idx, 0); |
332 | 0 | auto& ref_column_def = ref_block->get_by_position(ref_column_idx); |
333 | 0 | RETURN_IF_ERROR( |
334 | 0 | _check_cast_valid(ref_column_def.column, result_tmp_column_def.column)); |
335 | 0 | } |
336 | 0 | swap_idx_list.emplace_back(result_tmp_column_idx, idx); |
337 | 0 | } else if (_schema_mapping[idx].ref_column_idx < 0) { |
338 | | // new column, write default value |
339 | 0 | auto* value = _schema_mapping[idx].default_value; |
340 | 0 | auto column = new_block->get_by_position(idx).column->assume_mutable(); |
341 | 0 | if (value->is_null()) { |
342 | 0 | DCHECK(column->is_nullable()); |
343 | 0 | column->insert_many_defaults(row_num); |
344 | 0 | } else { |
345 | 0 | auto type_info = get_type_info(_schema_mapping[idx].new_column); |
346 | 0 | DefaultValueColumnIterator::insert_default_data(type_info.get(), value->size(), |
347 | 0 | value->ptr(), column, row_num); |
348 | 0 | } |
349 | 0 | } else { |
350 | | // same type, just swap column |
351 | 0 | swap_idx_list.emplace_back(_schema_mapping[idx].ref_column_idx, idx); |
352 | 0 | } |
353 | 0 | } |
354 | | |
355 | 0 | for (auto it : swap_idx_list) { |
356 | 0 | auto& ref_col = ref_block->get_by_position(it.first).column; |
357 | 0 | auto& new_col = new_block->get_by_position(it.second).column; |
358 | |
|
359 | 0 | bool ref_col_nullable = ref_col->is_nullable(); |
360 | 0 | bool new_col_nullable = new_col->is_nullable(); |
361 | |
|
362 | 0 | if (ref_col_nullable != new_col_nullable) { |
363 | | // not nullable to nullable |
364 | 0 | if (new_col_nullable) { |
365 | 0 | auto* new_nullable_col = |
366 | 0 | assert_cast<vectorized::ColumnNullable*>(new_col->assume_mutable().get()); |
367 | |
|
368 | 0 | new_nullable_col->change_nested_column(ref_col); |
369 | 0 | new_nullable_col->get_null_map_data().resize_fill(ref_col->size()); |
370 | 0 | } else { |
371 | | // nullable to not nullable: |
372 | | // suppose column `c_phone` is originally varchar(16) NOT NULL, |
373 | | // then do schema change `alter table test modify column c_phone int not null`, |
374 | | // the cast expr of schema change is `CastExpr(CAST String to Nullable(Int32))`, |
375 | | // so need to handle nullable to not nullable here |
376 | 0 | auto* ref_nullable_col = |
377 | 0 | assert_cast<vectorized::ColumnNullable*>(ref_col->assume_mutable().get()); |
378 | |
|
379 | 0 | new_col = ref_nullable_col->get_nested_column_ptr(); |
380 | 0 | } |
381 | 0 | } else { |
382 | 0 | new_block->get_by_position(it.second).column = |
383 | 0 | ref_block->get_by_position(it.first).column; |
384 | 0 | } |
385 | 0 | } |
386 | 0 | return Status::OK(); |
387 | 0 | } |
388 | | |
389 | | // This check can prevent schema-change from causing data loss after type cast |
390 | | Status BlockChanger::_check_cast_valid(vectorized::ColumnPtr input_column, |
391 | 0 | vectorized::ColumnPtr output_column) { |
392 | 0 | if (input_column->size() != output_column->size()) { |
393 | 0 | return Status::InternalError( |
394 | 0 | "column size is changed, input_column_size={}, output_column_size={}; " |
395 | 0 | "input_column={}", |
396 | 0 | input_column->size(), output_column->size(), input_column->get_name()); |
397 | 0 | } |
398 | 0 | DCHECK_EQ(input_column->size(), output_column->size()) |
399 | 0 | << "length check should have done before calling this function!"; |
400 | |
|
401 | 0 | if (input_column->is_nullable() != output_column->is_nullable()) { |
402 | 0 | if (input_column->is_nullable()) { |
403 | 0 | const auto* ref_null_map = |
404 | 0 | vectorized::check_and_get_column<vectorized::ColumnNullable>(input_column) |
405 | 0 | ->get_null_map_column() |
406 | 0 | .get_data() |
407 | 0 | .data(); |
408 | |
|
409 | 0 | bool is_changed = false; |
410 | 0 | for (size_t i = 0; i < input_column->size(); i++) { |
411 | 0 | is_changed |= ref_null_map[i]; |
412 | 0 | } |
413 | 0 | if (is_changed) { |
414 | 0 | return Status::DataQualityError( |
415 | 0 | "some null data is changed to not null, intput_column={}", |
416 | 0 | input_column->get_name()); |
417 | 0 | } |
418 | 0 | } else { |
419 | 0 | const auto& null_map_column = |
420 | 0 | vectorized::check_and_get_column<vectorized::ColumnNullable>(output_column) |
421 | 0 | ->get_null_map_column(); |
422 | 0 | const auto& nested_column = |
423 | 0 | vectorized::check_and_get_column<vectorized::ColumnNullable>(output_column) |
424 | 0 | ->get_nested_column(); |
425 | 0 | const auto* new_null_map = null_map_column.get_data().data(); |
426 | |
|
427 | 0 | if (null_map_column.size() != output_column->size()) { |
428 | 0 | return Status::InternalError( |
429 | 0 | "null_map_column size mismatch output_column_size, " |
430 | 0 | "null_map_column_size={}, output_column_size={}; input_column={}", |
431 | 0 | null_map_column.size(), output_column->size(), input_column->get_name()); |
432 | 0 | } |
433 | | |
434 | 0 | if (nested_column.size() != output_column->size()) { |
435 | 0 | return Status::InternalError( |
436 | 0 | "nested_column size is changed, nested_column_size={}, " |
437 | 0 | "ouput_column_size={}; input_column={}", |
438 | 0 | nested_column.size(), output_column->size(), input_column->get_name()); |
439 | 0 | } |
440 | | |
441 | 0 | bool is_changed = false; |
442 | 0 | for (size_t i = 0; i < input_column->size(); i++) { |
443 | 0 | is_changed |= new_null_map[i]; |
444 | 0 | } |
445 | 0 | if (is_changed) { |
446 | 0 | return Status::DataQualityError( |
447 | 0 | "some not null data is changed to null, intput_column={}", |
448 | 0 | input_column->get_name()); |
449 | 0 | } |
450 | 0 | } |
451 | 0 | } |
452 | | |
453 | 0 | if (input_column->is_nullable() && output_column->is_nullable()) { |
454 | 0 | const auto* ref_null_map = |
455 | 0 | vectorized::check_and_get_column<vectorized::ColumnNullable>(input_column) |
456 | 0 | ->get_null_map_column() |
457 | 0 | .get_data() |
458 | 0 | .data(); |
459 | 0 | const auto* new_null_map = |
460 | 0 | vectorized::check_and_get_column<vectorized::ColumnNullable>(output_column) |
461 | 0 | ->get_null_map_column() |
462 | 0 | .get_data() |
463 | 0 | .data(); |
464 | |
|
465 | 0 | bool is_changed = false; |
466 | 0 | for (size_t i = 0; i < input_column->size(); i++) { |
467 | 0 | is_changed |= (ref_null_map[i] != new_null_map[i]); |
468 | 0 | } |
469 | 0 | if (is_changed) { |
470 | 0 | return Status::DataQualityError( |
471 | 0 | "null map is changed after calculation, input_column={}", |
472 | 0 | input_column->get_name()); |
473 | 0 | } |
474 | 0 | } |
475 | 0 | return Status::OK(); |
476 | 0 | } |
477 | | |
478 | | Status LinkedSchemaChange::process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer, |
479 | | BaseTabletSPtr new_tablet, BaseTabletSPtr base_tablet, |
480 | | TabletSchemaSPtr base_tablet_schema, |
481 | 0 | TabletSchemaSPtr new_tablet_schema) { |
482 | 0 | Status status = rowset_writer->add_rowset_for_linked_schema_change(rowset_reader->rowset()); |
483 | 0 | if (!status) { |
484 | 0 | LOG(WARNING) << "fail to convert rowset." |
485 | 0 | << ", new_tablet=" << new_tablet->tablet_id() |
486 | 0 | << ", version=" << rowset_writer->version().first << "-" |
487 | 0 | << rowset_writer->version().second << ", error status " << status; |
488 | 0 | return status; |
489 | 0 | } |
490 | | // copy delete bitmap to new tablet. |
491 | 0 | if (new_tablet->keys_type() == UNIQUE_KEYS && new_tablet->enable_unique_key_merge_on_write()) { |
492 | 0 | DeleteBitmap origin_delete_bitmap(base_tablet->tablet_id()); |
493 | 0 | base_tablet->tablet_meta()->delete_bitmap().subset( |
494 | 0 | {rowset_reader->rowset()->rowset_id(), 0, 0}, |
495 | 0 | {rowset_reader->rowset()->rowset_id(), UINT32_MAX, INT64_MAX}, |
496 | 0 | &origin_delete_bitmap); |
497 | 0 | for (auto& iter : origin_delete_bitmap.delete_bitmap) { |
498 | 0 | int ret = new_tablet->tablet_meta()->delete_bitmap().set( |
499 | 0 | {rowset_writer->rowset_id(), std::get<1>(iter.first), std::get<2>(iter.first)}, |
500 | 0 | iter.second); |
501 | 0 | DCHECK(ret == 1); |
502 | 0 | } |
503 | 0 | } |
504 | 0 | return Status::OK(); |
505 | 0 | } |
506 | | |
507 | | Status VSchemaChangeDirectly::_inner_process(RowsetReaderSharedPtr rowset_reader, |
508 | | RowsetWriter* rowset_writer, BaseTabletSPtr new_tablet, |
509 | | TabletSchemaSPtr base_tablet_schema, |
510 | 0 | TabletSchemaSPtr new_tablet_schema) { |
511 | 0 | bool eof = false; |
512 | 0 | do { |
513 | 0 | auto new_block = vectorized::Block::create_unique(new_tablet_schema->create_block()); |
514 | 0 | auto ref_block = vectorized::Block::create_unique(base_tablet_schema->create_block()); |
515 | |
|
516 | 0 | auto st = rowset_reader->next_block(ref_block.get()); |
517 | 0 | if (!st) { |
518 | 0 | if (st.is<ErrorCode::END_OF_FILE>()) { |
519 | 0 | if (ref_block->rows() == 0) { |
520 | 0 | break; |
521 | 0 | } else { |
522 | 0 | eof = true; |
523 | 0 | } |
524 | 0 | } else { |
525 | 0 | return st; |
526 | 0 | } |
527 | 0 | } |
528 | | |
529 | 0 | RETURN_IF_ERROR(_changer.change_block(ref_block.get(), new_block.get())); |
530 | 0 | RETURN_IF_ERROR(rowset_writer->add_block(new_block.get())); |
531 | 0 | } while (!eof); |
532 | | |
533 | 0 | RETURN_IF_ERROR(rowset_writer->flush()); |
534 | 0 | return Status::OK(); |
535 | 0 | } |
536 | | |
537 | | VBaseSchemaChangeWithSorting::VBaseSchemaChangeWithSorting(const BlockChanger& changer, |
538 | | size_t memory_limitation) |
539 | | : _changer(changer), |
540 | | _memory_limitation(memory_limitation), |
541 | 0 | _temp_delta_versions(Version::mock()) { |
542 | 0 | _mem_tracker = std::make_unique<MemTracker>( |
543 | 0 | fmt::format("VSchemaChangeWithSorting:changer={}", std::to_string(int64(&changer)))); |
544 | 0 | } |
545 | | |
546 | | Status VBaseSchemaChangeWithSorting::_inner_process(RowsetReaderSharedPtr rowset_reader, |
547 | | RowsetWriter* rowset_writer, |
548 | | BaseTabletSPtr new_tablet, |
549 | | TabletSchemaSPtr base_tablet_schema, |
550 | 0 | TabletSchemaSPtr new_tablet_schema) { |
551 | | // for internal sorting |
552 | 0 | std::vector<std::unique_ptr<vectorized::Block>> blocks; |
553 | |
|
554 | 0 | RowsetSharedPtr rowset = rowset_reader->rowset(); |
555 | 0 | SegmentsOverlapPB segments_overlap = rowset->rowset_meta()->segments_overlap(); |
556 | 0 | int64_t newest_write_timestamp = rowset->newest_write_timestamp(); |
557 | 0 | _temp_delta_versions.first = _temp_delta_versions.second; |
558 | 0 | _src_rowsets.clear(); // init _src_rowsets |
559 | 0 | auto create_rowset = [&]() -> Status { |
560 | 0 | if (blocks.empty()) { |
561 | 0 | return Status::OK(); |
562 | 0 | } |
563 | | |
564 | 0 | auto rowset = DORIS_TRY(_internal_sorting( |
565 | 0 | blocks, Version(_temp_delta_versions.second, _temp_delta_versions.second), |
566 | 0 | newest_write_timestamp, new_tablet, BETA_ROWSET, segments_overlap, |
567 | 0 | new_tablet_schema)); |
568 | 0 | _src_rowsets.push_back(std::move(rowset)); |
569 | 0 | for (auto& block : blocks) { |
570 | 0 | _mem_tracker->release(block->allocated_bytes()); |
571 | 0 | } |
572 | 0 | blocks.clear(); |
573 | | |
574 | | // increase temp version |
575 | 0 | _temp_delta_versions.second++; |
576 | 0 | return Status::OK(); |
577 | 0 | }; |
578 | |
|
579 | 0 | auto new_block = vectorized::Block::create_unique(new_tablet_schema->create_block()); |
580 | |
|
581 | 0 | bool eof = false; |
582 | 0 | do { |
583 | 0 | auto ref_block = vectorized::Block::create_unique(base_tablet_schema->create_block()); |
584 | 0 | auto st = rowset_reader->next_block(ref_block.get()); |
585 | 0 | if (!st) { |
586 | 0 | if (st.is<ErrorCode::END_OF_FILE>()) { |
587 | 0 | if (ref_block->rows() == 0) { |
588 | 0 | break; |
589 | 0 | } else { |
590 | 0 | eof = true; |
591 | 0 | } |
592 | 0 | } else { |
593 | 0 | return st; |
594 | 0 | } |
595 | 0 | } |
596 | | |
597 | 0 | RETURN_IF_ERROR(_changer.change_block(ref_block.get(), new_block.get())); |
598 | | |
599 | 0 | constexpr double HOLD_BLOCK_MEMORY_RATE = |
600 | 0 | 0.66; // Reserve some memory for use by other parts of this job |
601 | 0 | if (_mem_tracker->consumption() + new_block->allocated_bytes() > _memory_limitation || |
602 | 0 | _mem_tracker->consumption() > _memory_limitation * HOLD_BLOCK_MEMORY_RATE) { |
603 | 0 | RETURN_IF_ERROR(create_rowset()); |
604 | | |
605 | 0 | if (_mem_tracker->consumption() + new_block->allocated_bytes() > _memory_limitation) { |
606 | 0 | return Status::Error<INVALID_ARGUMENT>( |
607 | 0 | "Memory limitation is too small for Schema Change. _memory_limitation={}, " |
608 | 0 | "new_block->allocated_bytes()={}, consumption={}", |
609 | 0 | _memory_limitation, new_block->allocated_bytes(), |
610 | 0 | _mem_tracker->consumption()); |
611 | 0 | } |
612 | 0 | } |
613 | 0 | _mem_tracker->consume(new_block->allocated_bytes()); |
614 | | |
615 | | // move unique ptr |
616 | 0 | blocks.push_back(vectorized::Block::create_unique(new_tablet_schema->create_block())); |
617 | 0 | swap(blocks.back(), new_block); |
618 | 0 | } while (!eof); |
619 | | |
620 | 0 | RETURN_IF_ERROR(create_rowset()); |
621 | | |
622 | 0 | if (_src_rowsets.empty()) { |
623 | 0 | RETURN_IF_ERROR(rowset_writer->flush()); |
624 | 0 | } else { |
625 | 0 | RETURN_IF_ERROR( |
626 | 0 | _external_sorting(_src_rowsets, rowset_writer, new_tablet, new_tablet_schema)); |
627 | 0 | } |
628 | | |
629 | 0 | return Status::OK(); |
630 | 0 | } |
631 | | |
632 | | Result<RowsetSharedPtr> VBaseSchemaChangeWithSorting::_internal_sorting( |
633 | | const std::vector<std::unique_ptr<vectorized::Block>>& blocks, const Version& version, |
634 | | int64_t newest_write_timestamp, BaseTabletSPtr new_tablet, RowsetTypePB new_rowset_type, |
635 | 0 | SegmentsOverlapPB segments_overlap, TabletSchemaSPtr new_tablet_schema) { |
636 | 0 | uint64_t merged_rows = 0; |
637 | 0 | MultiBlockMerger merger(new_tablet); |
638 | 0 | RowsetWriterContext context; |
639 | 0 | context.version = version; |
640 | 0 | context.rowset_state = VISIBLE; |
641 | 0 | context.segments_overlap = segments_overlap; |
642 | 0 | context.tablet_schema = new_tablet_schema; |
643 | 0 | context.newest_write_timestamp = newest_write_timestamp; |
644 | 0 | context.write_type = DataWriteType::TYPE_SCHEMA_CHANGE; |
645 | 0 | std::unique_ptr<RowsetWriter> rowset_writer; |
646 | | // TODO(plat1ko): Use monad op |
647 | 0 | if (auto result = new_tablet->create_rowset_writer(context, false); !result.has_value()) |
648 | 0 | [[unlikely]] { |
649 | 0 | return unexpected(std::move(result).error()); |
650 | 0 | } else { |
651 | 0 | rowset_writer = std::move(result).value(); |
652 | 0 | } |
653 | 0 | RETURN_IF_ERROR_RESULT(merger.merge(blocks, rowset_writer.get(), &merged_rows)); |
654 | 0 | _add_merged_rows(merged_rows); |
655 | 0 | RowsetSharedPtr rowset; |
656 | 0 | RETURN_IF_ERROR_RESULT(rowset_writer->build(rowset)); |
657 | 0 | return rowset; |
658 | 0 | } |
659 | | |
660 | | Result<RowsetSharedPtr> VLocalSchemaChangeWithSorting::_internal_sorting( |
661 | | const std::vector<std::unique_ptr<vectorized::Block>>& blocks, const Version& version, |
662 | | int64_t newest_write_timestamp, BaseTabletSPtr new_tablet, RowsetTypePB new_rowset_type, |
663 | 0 | SegmentsOverlapPB segments_overlap, TabletSchemaSPtr new_tablet_schema) { |
664 | 0 | uint64_t merged_rows = 0; |
665 | 0 | MultiBlockMerger merger(new_tablet); |
666 | 0 | RowsetWriterContext context; |
667 | 0 | context.version = version; |
668 | 0 | context.rowset_state = VISIBLE; |
669 | 0 | context.segments_overlap = segments_overlap; |
670 | 0 | context.tablet_schema = new_tablet_schema; |
671 | 0 | context.newest_write_timestamp = newest_write_timestamp; |
672 | 0 | context.write_type = DataWriteType::TYPE_SCHEMA_CHANGE; |
673 | 0 | std::unique_ptr<RowsetWriter> rowset_writer; |
674 | | // TODO(plat1ko): Use monad op |
675 | 0 | if (auto result = new_tablet->create_rowset_writer(context, false); !result.has_value()) |
676 | 0 | [[unlikely]] { |
677 | 0 | return unexpected(std::move(result).error()); |
678 | 0 | } else { |
679 | 0 | rowset_writer = std::move(result).value(); |
680 | 0 | } |
681 | 0 | auto guard = _local_storage_engine.pending_local_rowsets().add(context.rowset_id); |
682 | 0 | _pending_rs_guards.push_back(std::move(guard)); |
683 | 0 | RETURN_IF_ERROR_RESULT(merger.merge(blocks, rowset_writer.get(), &merged_rows)); |
684 | 0 | _add_merged_rows(merged_rows); |
685 | 0 | RowsetSharedPtr rowset; |
686 | 0 | RETURN_IF_ERROR_RESULT(rowset_writer->build(rowset)); |
687 | 0 | return rowset; |
688 | 0 | } |
689 | | |
690 | | Status VBaseSchemaChangeWithSorting::_external_sorting(vector<RowsetSharedPtr>& src_rowsets, |
691 | | RowsetWriter* rowset_writer, |
692 | | BaseTabletSPtr new_tablet, |
693 | 0 | TabletSchemaSPtr new_tablet_schema) { |
694 | 0 | std::vector<RowsetReaderSharedPtr> rs_readers; |
695 | 0 | for (auto& rowset : src_rowsets) { |
696 | 0 | RowsetReaderSharedPtr rs_reader; |
697 | 0 | RETURN_IF_ERROR(rowset->create_reader(&rs_reader)); |
698 | 0 | rs_readers.push_back(rs_reader); |
699 | 0 | } |
700 | | |
701 | 0 | Merger::Statistics stats; |
702 | 0 | RETURN_IF_ERROR(Merger::vmerge_rowsets(new_tablet, ReaderType::READER_ALTER_TABLE, |
703 | 0 | *new_tablet_schema, rs_readers, rowset_writer, &stats)); |
704 | 0 | _add_merged_rows(stats.merged_rows); |
705 | 0 | _add_filtered_rows(stats.filtered_rows); |
706 | 0 | return Status::OK(); |
707 | 0 | } |
708 | | |
709 | | Status VLocalSchemaChangeWithSorting::_inner_process(RowsetReaderSharedPtr rowset_reader, |
710 | | RowsetWriter* rowset_writer, |
711 | | BaseTabletSPtr new_tablet, |
712 | | TabletSchemaSPtr base_tablet_schema, |
713 | 0 | TabletSchemaSPtr new_tablet_schema) { |
714 | 0 | Defer defer {[&]() { |
715 | | // remove the intermediate rowsets generated by internal sorting |
716 | 0 | for (auto& row_set : _src_rowsets) { |
717 | 0 | _local_storage_engine.add_unused_rowset(row_set); |
718 | 0 | } |
719 | 0 | }}; |
720 | 0 | _pending_rs_guards.clear(); |
721 | 0 | return VBaseSchemaChangeWithSorting::_inner_process(rowset_reader, rowset_writer, new_tablet, |
722 | 0 | base_tablet_schema, new_tablet_schema); |
723 | 0 | } |
724 | | |
725 | 0 | Status SchemaChangeJob::process_alter_tablet(const TAlterTabletReqV2& request) { |
726 | 0 | if (!request.__isset.desc_tbl) { |
727 | 0 | return Status::Error<INVALID_ARGUMENT>( |
728 | 0 | "desc_tbl is not set. Maybe the FE version is not equal to the BE " |
729 | 0 | "version."); |
730 | 0 | } |
731 | 0 | if (_base_tablet == nullptr) { |
732 | 0 | return Status::Error<TABLE_NOT_FOUND>("fail to find base tablet. base_tablet={}", |
733 | 0 | request.base_tablet_id); |
734 | 0 | } |
735 | 0 | if (_new_tablet == nullptr) { |
736 | 0 | return Status::Error<TABLE_NOT_FOUND>("fail to find new tablet. new_tablet={}", |
737 | 0 | request.new_tablet_id); |
738 | 0 | } |
739 | | |
740 | 0 | LOG(INFO) << "begin to do request alter tablet: base_tablet_id=" << request.base_tablet_id |
741 | 0 | << ", new_tablet_id=" << request.new_tablet_id |
742 | 0 | << ", alter_version=" << request.alter_version; |
743 | | |
744 | | // Lock schema_change_lock util schema change info is stored in tablet header |
745 | 0 | std::unique_lock<std::mutex> schema_change_lock(_base_tablet->get_schema_change_lock(), |
746 | 0 | std::try_to_lock); |
747 | 0 | if (!schema_change_lock.owns_lock()) { |
748 | 0 | return Status::Error<TRY_LOCK_FAILED>("failed to obtain schema change lock. base_tablet={}", |
749 | 0 | request.base_tablet_id); |
750 | 0 | } |
751 | | |
752 | 0 | Status res = _do_process_alter_tablet(request); |
753 | 0 | LOG(INFO) << "finished alter tablet process, res=" << res; |
754 | 0 | return res; |
755 | 0 | } |
756 | | |
757 | | SchemaChangeJob::SchemaChangeJob(StorageEngine& local_storage_engine, |
758 | | const TAlterTabletReqV2& request, const std::string& job_id) |
759 | 0 | : _local_storage_engine(local_storage_engine) { |
760 | 0 | _base_tablet = _local_storage_engine.tablet_manager()->get_tablet(request.base_tablet_id); |
761 | 0 | _new_tablet = _local_storage_engine.tablet_manager()->get_tablet(request.new_tablet_id); |
762 | 0 | if (_base_tablet && _new_tablet) { |
763 | 0 | _base_tablet_schema = std::make_shared<TabletSchema>(); |
764 | 0 | _base_tablet_schema->update_tablet_columns(*_base_tablet->tablet_schema(), request.columns); |
765 | | // The request only include column info, do not include bitmap or bloomfilter index info, |
766 | | // So we also need to copy index info from the real base tablet |
767 | 0 | _base_tablet_schema->update_index_info_from(*_base_tablet->tablet_schema()); |
768 | | // During a schema change, the extracted columns of a variant should not be included in the tablet schema. |
769 | | // This is because the schema change for a variant needs to ignore the extracted columns. |
770 | | // Otherwise, the schema types in different rowsets might be inconsistent. When performing a schema change, |
771 | | // the complete variant is constructed by reading all the sub-columns of the variant. |
772 | 0 | _new_tablet_schema = _new_tablet->tablet_schema()->copy_without_variant_extracted_columns(); |
773 | 0 | } |
774 | 0 | _job_id = job_id; |
775 | 0 | } |
776 | | |
777 | | // In the past schema change and rollup will create new tablet and will wait for txns starting before the task to finished |
778 | | // It will cost a lot of time to wait and the task is very difficult to understand. |
779 | | // In alter task v2, FE will call BE to create tablet and send an alter task to BE to convert historical data. |
780 | | // The admin should upgrade all BE and then upgrade FE. |
781 | | // Should delete the old code after upgrade finished. |
782 | 0 | Status SchemaChangeJob::_do_process_alter_tablet(const TAlterTabletReqV2& request) { |
783 | 0 | DBUG_EXECUTE_IF("SchemaChangeJob._do_process_alter_tablet.sleep", { sleep(10); }) |
784 | 0 | Status res; |
785 | 0 | signal::tablet_id = _base_tablet->get_table_id(); |
786 | | |
787 | | // check if tablet's state is not_ready, if it is ready, it means the tablet already finished |
788 | | // check whether the tablet's max continuous version == request.version |
789 | 0 | if (_new_tablet->tablet_state() != TABLET_NOTREADY) { |
790 | 0 | res = _validate_alter_result(request); |
791 | 0 | LOG(INFO) << "tablet's state=" << _new_tablet->tablet_state() |
792 | 0 | << " the convert job already finished, check its version" |
793 | 0 | << " res=" << res; |
794 | 0 | return res; |
795 | 0 | } |
796 | 0 | _new_tablet->set_alter_failed(false); |
797 | 0 | Defer defer([this] { |
798 | | // if tablet state is not TABLET_RUNNING when return, indicates that alter has failed. |
799 | 0 | if (_new_tablet->tablet_state() != TABLET_RUNNING) { |
800 | 0 | _new_tablet->set_alter_failed(true); |
801 | 0 | } |
802 | 0 | }); |
803 | |
|
804 | 0 | LOG(INFO) << "finish to validate alter tablet request. begin to convert data from base tablet " |
805 | 0 | "to new tablet" |
806 | 0 | << " base_tablet=" << _base_tablet->tablet_id() |
807 | 0 | << " new_tablet=" << _new_tablet->tablet_id(); |
808 | |
|
809 | 0 | std::shared_lock base_migration_rlock(_base_tablet->get_migration_lock(), std::try_to_lock); |
810 | 0 | if (!base_migration_rlock.owns_lock()) { |
811 | 0 | return Status::Error<TRY_LOCK_FAILED>( |
812 | 0 | "SchemaChangeJob::_do_process_alter_tablet get lock failed"); |
813 | 0 | } |
814 | 0 | std::shared_lock new_migration_rlock(_new_tablet->get_migration_lock(), std::try_to_lock); |
815 | 0 | if (!new_migration_rlock.owns_lock()) { |
816 | 0 | return Status::Error<TRY_LOCK_FAILED>( |
817 | 0 | "SchemaChangeJob::_do_process_alter_tablet get lock failed"); |
818 | 0 | } |
819 | | |
820 | 0 | std::vector<Version> versions_to_be_changed; |
821 | 0 | int64_t end_version = -1; |
822 | | // reader_context is stack variables, it's lifetime should keep the same |
823 | | // with rs_readers |
824 | 0 | RowsetReaderContext reader_context; |
825 | 0 | std::vector<RowSetSplits> rs_splits; |
826 | | // delete handlers for new tablet |
827 | 0 | DeleteHandler delete_handler; |
828 | 0 | std::vector<ColumnId> return_columns; |
829 | | |
830 | | // Use tablet schema directly from base tablet, they are the newest schema, not contain |
831 | | // dropped column during light weight schema change. |
832 | | // But the tablet schema in base tablet maybe not the latest from FE, so that if fe pass through |
833 | | // a tablet schema, then use request schema. |
834 | 0 | size_t num_cols = |
835 | 0 | request.columns.empty() ? _base_tablet_schema->num_columns() : request.columns.size(); |
836 | 0 | return_columns.resize(num_cols); |
837 | 0 | for (int i = 0; i < num_cols; ++i) { |
838 | 0 | return_columns[i] = i; |
839 | 0 | } |
840 | | |
841 | | // begin to find deltas to convert from base tablet to new tablet so that |
842 | | // obtain base tablet and new tablet's push lock and header write lock to prevent loading data |
843 | 0 | { |
844 | 0 | std::lock_guard base_tablet_lock(_base_tablet->get_push_lock()); |
845 | 0 | std::lock_guard new_tablet_lock(_new_tablet->get_push_lock()); |
846 | 0 | std::lock_guard base_tablet_wlock(_base_tablet->get_header_lock()); |
847 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
848 | 0 | std::lock_guard<std::shared_mutex> new_tablet_wlock(_new_tablet->get_header_lock()); |
849 | |
|
850 | 0 | do { |
851 | 0 | RowsetSharedPtr max_rowset; |
852 | | // get history data to be converted and it will check if there is hold in base tablet |
853 | 0 | res = _get_versions_to_be_changed(&versions_to_be_changed, &max_rowset); |
854 | 0 | if (!res) { |
855 | 0 | LOG(WARNING) << "fail to get version to be changed. res=" << res; |
856 | 0 | break; |
857 | 0 | } |
858 | | |
859 | 0 | DBUG_EXECUTE_IF("SchemaChangeJob.process_alter_tablet.alter_fail", { |
860 | 0 | res = Status::InternalError( |
861 | 0 | "inject alter tablet failed. base_tablet={}, new_tablet={}", |
862 | 0 | request.base_tablet_id, request.new_tablet_id); |
863 | 0 | LOG(WARNING) << "inject error. res=" << res; |
864 | 0 | break; |
865 | 0 | }); |
866 | | |
867 | | // should check the max_version >= request.alter_version, if not the convert is useless |
868 | 0 | if (max_rowset == nullptr || max_rowset->end_version() < request.alter_version) { |
869 | 0 | res = Status::InternalError( |
870 | 0 | "base tablet's max version={} is less than request version={}", |
871 | 0 | (max_rowset == nullptr ? 0 : max_rowset->end_version()), |
872 | 0 | request.alter_version); |
873 | 0 | break; |
874 | 0 | } |
875 | | // before calculating version_to_be_changed, |
876 | | // remove all data from new tablet, prevent to rewrite data(those double pushed when wait) |
877 | 0 | LOG(INFO) << "begin to remove all data before end version from new tablet to prevent " |
878 | 0 | "rewrite." |
879 | 0 | << " new_tablet=" << _new_tablet->tablet_id() |
880 | 0 | << ", end_version=" << max_rowset->end_version(); |
881 | 0 | std::vector<RowsetSharedPtr> rowsets_to_delete; |
882 | 0 | std::vector<std::pair<Version, RowsetSharedPtr>> version_rowsets; |
883 | 0 | _new_tablet->acquire_version_and_rowsets(&version_rowsets); |
884 | 0 | std::sort(version_rowsets.begin(), version_rowsets.end(), |
885 | 0 | [](const std::pair<Version, RowsetSharedPtr>& l, |
886 | 0 | const std::pair<Version, RowsetSharedPtr>& r) { |
887 | 0 | return l.first.first < r.first.first; |
888 | 0 | }); |
889 | 0 | for (auto& pair : version_rowsets) { |
890 | 0 | if (pair.first.second <= max_rowset->end_version()) { |
891 | 0 | rowsets_to_delete.push_back(pair.second); |
892 | 0 | } else if (pair.first.first <= max_rowset->end_version()) { |
893 | | // If max version is [X-10] and new tablet has version [7-9][10-12], |
894 | | // we only can remove [7-9] from new tablet. If we add [X-10] to new tablet, it will has version |
895 | | // cross: [X-10] [10-12]. |
896 | | // So, we should return OLAP_ERR_VERSION_ALREADY_MERGED for fast fail. |
897 | 0 | return Status::Error<VERSION_ALREADY_MERGED>( |
898 | 0 | "New tablet has a version {} crossing base tablet's max_version={}", |
899 | 0 | pair.first.to_string(), max_rowset->end_version()); |
900 | 0 | } |
901 | 0 | } |
902 | 0 | std::vector<RowsetSharedPtr> empty_vec; |
903 | 0 | RETURN_IF_ERROR(_new_tablet->delete_rowsets(rowsets_to_delete, false)); |
904 | | // inherit cumulative_layer_point from base_tablet |
905 | | // check if new_tablet.ce_point > base_tablet.ce_point? |
906 | 0 | _new_tablet->set_cumulative_layer_point(-1); |
907 | | // save tablet meta |
908 | 0 | _new_tablet->save_meta(); |
909 | 0 | for (auto& rowset : rowsets_to_delete) { |
910 | | // do not call rowset.remove directly, using gc thread to delete it |
911 | 0 | _local_storage_engine.add_unused_rowset(rowset); |
912 | 0 | } |
913 | | |
914 | | // init one delete handler |
915 | 0 | for (auto& version : versions_to_be_changed) { |
916 | 0 | end_version = std::max(end_version, version.second); |
917 | 0 | } |
918 | | |
919 | | // acquire data sources correspond to history versions |
920 | 0 | RETURN_IF_ERROR( |
921 | 0 | _base_tablet->capture_rs_readers_unlocked(versions_to_be_changed, &rs_splits)); |
922 | 0 | if (rs_splits.empty()) { |
923 | 0 | res = Status::Error<ALTER_DELTA_DOES_NOT_EXISTS>( |
924 | 0 | "fail to acquire all data sources. version_num={}, data_source_num={}", |
925 | 0 | versions_to_be_changed.size(), rs_splits.size()); |
926 | 0 | break; |
927 | 0 | } |
928 | 0 | std::vector<RowsetMetaSharedPtr> del_preds; |
929 | 0 | for (auto&& split : rs_splits) { |
930 | 0 | const auto& rs_meta = split.rs_reader->rowset()->rowset_meta(); |
931 | 0 | if (!rs_meta->has_delete_predicate() || rs_meta->start_version() > end_version) { |
932 | 0 | continue; |
933 | 0 | } |
934 | 0 | _base_tablet_schema->merge_dropped_columns(*rs_meta->tablet_schema()); |
935 | 0 | del_preds.push_back(rs_meta); |
936 | 0 | } |
937 | 0 | res = delete_handler.init(_base_tablet_schema, del_preds, end_version); |
938 | 0 | if (!res) { |
939 | 0 | LOG(WARNING) << "init delete handler failed. base_tablet=" |
940 | 0 | << _base_tablet->tablet_id() << ", end_version=" << end_version; |
941 | 0 | break; |
942 | 0 | } |
943 | | |
944 | 0 | reader_context.reader_type = ReaderType::READER_ALTER_TABLE; |
945 | 0 | reader_context.tablet_schema = _base_tablet_schema; |
946 | 0 | reader_context.need_ordered_result = true; |
947 | 0 | reader_context.delete_handler = &delete_handler; |
948 | 0 | reader_context.return_columns = &return_columns; |
949 | 0 | reader_context.sequence_id_idx = reader_context.tablet_schema->sequence_col_idx(); |
950 | 0 | reader_context.is_unique = _base_tablet->keys_type() == UNIQUE_KEYS; |
951 | 0 | reader_context.batch_size = ALTER_TABLE_BATCH_SIZE; |
952 | 0 | reader_context.delete_bitmap = &_base_tablet->tablet_meta()->delete_bitmap(); |
953 | 0 | reader_context.version = Version(0, end_version); |
954 | 0 | for (auto& rs_split : rs_splits) { |
955 | 0 | res = rs_split.rs_reader->init(&reader_context); |
956 | 0 | if (!res) { |
957 | 0 | LOG(WARNING) << "failed to init rowset reader: " << _base_tablet->tablet_id(); |
958 | 0 | break; |
959 | 0 | } |
960 | 0 | } |
961 | 0 | } while (false); |
962 | 0 | } |
963 | | |
964 | 0 | do { |
965 | 0 | if (!res) { |
966 | 0 | break; |
967 | 0 | } |
968 | 0 | SchemaChangeParams sc_params; |
969 | |
|
970 | 0 | RETURN_IF_ERROR( |
971 | 0 | DescriptorTbl::create(&sc_params.pool, request.desc_tbl, &sc_params.desc_tbl)); |
972 | 0 | sc_params.ref_rowset_readers.reserve(rs_splits.size()); |
973 | 0 | for (RowSetSplits& split : rs_splits) { |
974 | 0 | sc_params.ref_rowset_readers.emplace_back(split.rs_reader); |
975 | 0 | } |
976 | 0 | sc_params.delete_handler = &delete_handler; |
977 | 0 | sc_params.be_exec_version = request.be_exec_version; |
978 | 0 | DCHECK(request.__isset.alter_tablet_type); |
979 | 0 | switch (request.alter_tablet_type) { |
980 | 0 | case TAlterTabletType::SCHEMA_CHANGE: |
981 | 0 | sc_params.alter_tablet_type = AlterTabletType::SCHEMA_CHANGE; |
982 | 0 | break; |
983 | 0 | case TAlterTabletType::ROLLUP: |
984 | 0 | sc_params.alter_tablet_type = AlterTabletType::ROLLUP; |
985 | 0 | break; |
986 | 0 | case TAlterTabletType::MIGRATION: |
987 | 0 | sc_params.alter_tablet_type = AlterTabletType::MIGRATION; |
988 | 0 | break; |
989 | 0 | } |
990 | 0 | if (request.__isset.materialized_view_params) { |
991 | 0 | for (auto item : request.materialized_view_params) { |
992 | 0 | AlterMaterializedViewParam mv_param; |
993 | 0 | mv_param.column_name = item.column_name; |
994 | |
|
995 | 0 | if (item.__isset.mv_expr) { |
996 | 0 | mv_param.expr = std::make_shared<TExpr>(item.mv_expr); |
997 | 0 | } |
998 | 0 | sc_params.materialized_params_map.insert( |
999 | 0 | std::make_pair(to_lower(item.column_name), mv_param)); |
1000 | 0 | } |
1001 | 0 | } |
1002 | 0 | { |
1003 | 0 | std::lock_guard<std::shared_mutex> wrlock(_mutex); |
1004 | 0 | _tablet_ids_in_converting.insert(_new_tablet->tablet_id()); |
1005 | 0 | } |
1006 | 0 | int64_t real_alter_version = 0; |
1007 | 0 | sc_params.enable_unique_key_merge_on_write = |
1008 | 0 | _new_tablet->enable_unique_key_merge_on_write(); |
1009 | 0 | res = _convert_historical_rowsets(sc_params, &real_alter_version); |
1010 | 0 | { |
1011 | 0 | std::lock_guard<std::shared_mutex> wrlock(_mutex); |
1012 | 0 | _tablet_ids_in_converting.erase(_new_tablet->tablet_id()); |
1013 | 0 | } |
1014 | 0 | if (!res) { |
1015 | 0 | break; |
1016 | 0 | } |
1017 | | |
1018 | 0 | DCHECK_GE(real_alter_version, request.alter_version); |
1019 | |
|
1020 | 0 | if (_new_tablet->keys_type() == UNIQUE_KEYS && |
1021 | 0 | _new_tablet->enable_unique_key_merge_on_write()) { |
1022 | 0 | res = _calc_delete_bitmap_for_mow_table(real_alter_version); |
1023 | 0 | if (!res) { |
1024 | 0 | break; |
1025 | 0 | } |
1026 | 0 | } else { |
1027 | | // set state to ready |
1028 | 0 | std::lock_guard<std::shared_mutex> new_wlock(_new_tablet->get_header_lock()); |
1029 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1030 | 0 | res = _new_tablet->set_tablet_state(TabletState::TABLET_RUNNING); |
1031 | 0 | if (!res) { |
1032 | 0 | break; |
1033 | 0 | } |
1034 | 0 | _new_tablet->save_meta(); |
1035 | 0 | } |
1036 | 0 | } while (false); |
1037 | | |
1038 | 0 | if (res) { |
1039 | | // _validate_alter_result should be outside the above while loop. |
1040 | | // to avoid requiring the header lock twice. |
1041 | 0 | res = _validate_alter_result(request); |
1042 | 0 | } |
1043 | | |
1044 | | // if failed convert history data, then just remove the new tablet |
1045 | 0 | if (!res) { |
1046 | 0 | LOG(WARNING) << "failed to alter tablet. base_tablet=" << _base_tablet->tablet_id() |
1047 | 0 | << ", drop new_tablet=" << _new_tablet->tablet_id(); |
1048 | | // do not drop the new tablet and its data. GC thread will |
1049 | 0 | } |
1050 | |
|
1051 | 0 | return res; |
1052 | 0 | } |
1053 | | |
1054 | 0 | bool SchemaChangeJob::tablet_in_converting(int64_t tablet_id) { |
1055 | 0 | std::shared_lock rdlock(_mutex); |
1056 | 0 | return _tablet_ids_in_converting.find(tablet_id) != _tablet_ids_in_converting.end(); |
1057 | 0 | } |
1058 | | |
1059 | | Status SchemaChangeJob::_get_versions_to_be_changed(std::vector<Version>* versions_to_be_changed, |
1060 | 0 | RowsetSharedPtr* max_rowset) { |
1061 | 0 | RowsetSharedPtr rowset = _base_tablet->get_rowset_with_max_version(); |
1062 | 0 | if (rowset == nullptr) { |
1063 | 0 | return Status::Error<ALTER_DELTA_DOES_NOT_EXISTS>("Tablet has no version. base_tablet={}", |
1064 | 0 | _base_tablet->tablet_id()); |
1065 | 0 | } |
1066 | 0 | *max_rowset = rowset; |
1067 | |
|
1068 | 0 | RETURN_IF_ERROR(_base_tablet->capture_consistent_versions_unlocked( |
1069 | 0 | Version(0, rowset->version().second), versions_to_be_changed, false, false)); |
1070 | | |
1071 | 0 | return Status::OK(); |
1072 | 0 | } |
1073 | | |
1074 | | // The `real_alter_version` parameter indicates that the version of [0-real_alter_version] is |
1075 | | // converted from a base tablet, only used for the mow table now. |
1076 | | Status SchemaChangeJob::_convert_historical_rowsets(const SchemaChangeParams& sc_params, |
1077 | 0 | int64_t* real_alter_version) { |
1078 | 0 | LOG(INFO) << "begin to convert historical rowsets for new_tablet from base_tablet." |
1079 | 0 | << " base_tablet=" << _base_tablet->tablet_id() |
1080 | 0 | << ", new_tablet=" << _new_tablet->tablet_id() << ", job_id=" << _job_id; |
1081 | | |
1082 | | // find end version |
1083 | 0 | int32_t end_version = -1; |
1084 | 0 | for (const auto& ref_rowset_reader : sc_params.ref_rowset_readers) { |
1085 | 0 | if (ref_rowset_reader->version().second > end_version) { |
1086 | 0 | end_version = ref_rowset_reader->version().second; |
1087 | 0 | } |
1088 | 0 | } |
1089 | | |
1090 | | // Add filter information in change, and filter column information will be set in parse_request |
1091 | | // And filter some data every time the row block changes |
1092 | 0 | BlockChanger changer(_new_tablet_schema, *sc_params.desc_tbl); |
1093 | |
|
1094 | 0 | bool sc_sorting = false; |
1095 | 0 | bool sc_directly = false; |
1096 | | |
1097 | | // a.Parse the Alter request and convert it into an internal representation |
1098 | 0 | Status res = parse_request(sc_params, _base_tablet_schema.get(), _new_tablet_schema.get(), |
1099 | 0 | &changer, &sc_sorting, &sc_directly); |
1100 | 0 | LOG(INFO) << "schema change type, sc_sorting: " << sc_sorting |
1101 | 0 | << ", sc_directly: " << sc_directly << ", base_tablet=" << _base_tablet->tablet_id() |
1102 | 0 | << ", new_tablet=" << _new_tablet->tablet_id(); |
1103 | |
|
1104 | 0 | auto process_alter_exit = [&]() -> Status { |
1105 | 0 | { |
1106 | | // save tablet meta here because rowset meta is not saved during add rowset |
1107 | 0 | std::lock_guard new_wlock(_new_tablet->get_header_lock()); |
1108 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1109 | 0 | _new_tablet->save_meta(); |
1110 | 0 | } |
1111 | 0 | if (res) { |
1112 | 0 | Version test_version(0, end_version); |
1113 | 0 | res = _new_tablet->check_version_integrity(test_version); |
1114 | 0 | } |
1115 | |
|
1116 | 0 | LOG(INFO) << "finish converting rowsets for new_tablet from base_tablet. " |
1117 | 0 | << "base_tablet=" << _base_tablet->tablet_id() |
1118 | 0 | << ", new_tablet=" << _new_tablet->tablet_id(); |
1119 | 0 | return res; |
1120 | 0 | }; |
1121 | |
|
1122 | 0 | if (!res) { |
1123 | 0 | LOG(WARNING) << "failed to parse the request. res=" << res; |
1124 | 0 | return process_alter_exit(); |
1125 | 0 | } |
1126 | | |
1127 | 0 | if (!sc_sorting && !sc_directly && sc_params.alter_tablet_type == AlterTabletType::ROLLUP) { |
1128 | 0 | res = Status::Error<SCHEMA_SCHEMA_INVALID>( |
1129 | 0 | "Don't support to add materialized view by linked schema change"); |
1130 | 0 | return process_alter_exit(); |
1131 | 0 | } |
1132 | | |
1133 | | // b. Generate historical data converter |
1134 | 0 | auto sc_procedure = _get_sc_procedure( |
1135 | 0 | changer, sc_sorting, sc_directly, |
1136 | 0 | _local_storage_engine.memory_limitation_bytes_per_thread_for_schema_change()); |
1137 | |
|
1138 | 0 | DBUG_EXECUTE_IF("SchemaChangeJob::_convert_historical_rowsets.block", DBUG_BLOCK); |
1139 | | |
1140 | | // c.Convert historical data |
1141 | 0 | bool have_failure_rowset = false; |
1142 | 0 | for (const auto& rs_reader : sc_params.ref_rowset_readers) { |
1143 | | // set status for monitor |
1144 | | // As long as there is a new_table as running, ref table is set as running |
1145 | | // NOTE If the first sub_table fails first, it will continue to go as normal here |
1146 | | // When tablet create new rowset writer, it may change rowset type, in this case |
1147 | | // linked schema change will not be used. |
1148 | 0 | RowsetWriterContext context; |
1149 | 0 | context.version = rs_reader->version(); |
1150 | 0 | context.rowset_state = VISIBLE; |
1151 | 0 | context.segments_overlap = rs_reader->rowset()->rowset_meta()->segments_overlap(); |
1152 | 0 | context.tablet_schema = _new_tablet_schema; |
1153 | 0 | context.newest_write_timestamp = rs_reader->newest_write_timestamp(); |
1154 | |
|
1155 | 0 | if (!rs_reader->rowset()->is_local()) { |
1156 | 0 | context.storage_resource = |
1157 | 0 | *DORIS_TRY(rs_reader->rowset()->rowset_meta()->remote_storage_resource()); |
1158 | 0 | } |
1159 | | |
1160 | 0 | context.write_type = DataWriteType::TYPE_SCHEMA_CHANGE; |
1161 | 0 | auto result = _new_tablet->create_rowset_writer(context, false); |
1162 | 0 | if (!result.has_value()) { |
1163 | 0 | res = Status::Error<ROWSET_BUILDER_INIT>("create_rowset_writer failed, reason={}", |
1164 | 0 | result.error().to_string()); |
1165 | 0 | return process_alter_exit(); |
1166 | 0 | } |
1167 | 0 | auto rowset_writer = std::move(result).value(); |
1168 | 0 | auto pending_rs_guard = _local_storage_engine.add_pending_rowset(context); |
1169 | |
|
1170 | 0 | if (res = sc_procedure->process(rs_reader, rowset_writer.get(), _new_tablet, _base_tablet, |
1171 | 0 | _base_tablet_schema, _new_tablet_schema); |
1172 | 0 | !res) { |
1173 | 0 | LOG(WARNING) << "failed to process the version." |
1174 | 0 | << " version=" << rs_reader->version().first << "-" |
1175 | 0 | << rs_reader->version().second << ", " << res.to_string(); |
1176 | 0 | return process_alter_exit(); |
1177 | 0 | } |
1178 | | // Add the new version of the data to the header |
1179 | | // In order to prevent the occurrence of deadlock, we must first lock the old table, and then lock the new table |
1180 | 0 | std::lock_guard lock(_new_tablet->get_push_lock()); |
1181 | 0 | RowsetSharedPtr new_rowset; |
1182 | 0 | if (!(res = rowset_writer->build(new_rowset)).ok()) { |
1183 | 0 | LOG(WARNING) << "failed to build rowset, exit alter process"; |
1184 | 0 | return process_alter_exit(); |
1185 | 0 | } |
1186 | 0 | res = _new_tablet->add_rowset(new_rowset); |
1187 | 0 | if (res.is<PUSH_VERSION_ALREADY_EXIST>()) { |
1188 | 0 | LOG(WARNING) << "version already exist, version revert occurred. " |
1189 | 0 | << "tablet=" << _new_tablet->tablet_id() << ", version='" |
1190 | 0 | << rs_reader->version().first << "-" << rs_reader->version().second; |
1191 | 0 | _local_storage_engine.add_unused_rowset(new_rowset); |
1192 | 0 | have_failure_rowset = true; |
1193 | 0 | res = Status::OK(); |
1194 | 0 | } else if (!res) { |
1195 | 0 | LOG(WARNING) << "failed to register new version. " |
1196 | 0 | << " tablet=" << _new_tablet->tablet_id() |
1197 | 0 | << ", version=" << rs_reader->version().first << "-" |
1198 | 0 | << rs_reader->version().second; |
1199 | 0 | _local_storage_engine.add_unused_rowset(new_rowset); |
1200 | 0 | return process_alter_exit(); |
1201 | 0 | } else { |
1202 | 0 | VLOG_NOTICE << "register new version. tablet=" << _new_tablet->tablet_id() |
1203 | 0 | << ", version=" << rs_reader->version().first << "-" |
1204 | 0 | << rs_reader->version().second; |
1205 | 0 | } |
1206 | 0 | if (!have_failure_rowset) { |
1207 | 0 | *real_alter_version = rs_reader->version().second; |
1208 | 0 | } |
1209 | |
|
1210 | 0 | VLOG_TRACE << "succeed to convert a history version." |
1211 | 0 | << " version=" << rs_reader->version().first << "-" |
1212 | 0 | << rs_reader->version().second; |
1213 | 0 | } |
1214 | | |
1215 | | // XXX:The SchemaChange state should not be canceled at this time, because the new Delta has to be converted to the old and new Schema version |
1216 | 0 | return process_alter_exit(); |
1217 | 0 | } |
1218 | | |
1219 | | static const std::string WHERE_SIGN_LOWER = to_lower("__DORIS_WHERE_SIGN__"); |
1220 | | |
1221 | | // @static |
1222 | | // Analyze the mapping of the column and the mapping of the filter key |
1223 | | Status SchemaChangeJob::parse_request(const SchemaChangeParams& sc_params, |
1224 | | TabletSchema* base_tablet_schema, |
1225 | | TabletSchema* new_tablet_schema, BlockChanger* changer, |
1226 | 0 | bool* sc_sorting, bool* sc_directly) { |
1227 | 0 | changer->set_type(sc_params.alter_tablet_type); |
1228 | 0 | changer->set_compatible_version(sc_params.be_exec_version); |
1229 | |
|
1230 | 0 | const std::unordered_map<std::string, AlterMaterializedViewParam>& materialized_function_map = |
1231 | 0 | sc_params.materialized_params_map; |
1232 | 0 | DescriptorTbl desc_tbl = *sc_params.desc_tbl; |
1233 | | |
1234 | | // set column mapping |
1235 | 0 | for (int i = 0, new_schema_size = new_tablet_schema->num_columns(); i < new_schema_size; ++i) { |
1236 | 0 | const TabletColumn& new_column = new_tablet_schema->column(i); |
1237 | 0 | const std::string& column_name_lower = to_lower(new_column.name()); |
1238 | 0 | ColumnMapping* column_mapping = changer->get_mutable_column_mapping(i); |
1239 | 0 | column_mapping->new_column = &new_column; |
1240 | |
|
1241 | 0 | column_mapping->ref_column_idx = base_tablet_schema->field_index(new_column.name()); |
1242 | |
|
1243 | 0 | if (materialized_function_map.find(column_name_lower) != materialized_function_map.end()) { |
1244 | 0 | auto mv_param = materialized_function_map.find(column_name_lower)->second; |
1245 | 0 | column_mapping->expr = mv_param.expr; |
1246 | 0 | if (column_mapping->expr != nullptr) { |
1247 | 0 | continue; |
1248 | 0 | } |
1249 | 0 | } |
1250 | | |
1251 | 0 | if (column_mapping->ref_column_idx >= 0) { |
1252 | 0 | continue; |
1253 | 0 | } |
1254 | | |
1255 | 0 | if (sc_params.alter_tablet_type == ROLLUP) { |
1256 | 0 | std::string materialized_function_map_str; |
1257 | 0 | for (auto str : materialized_function_map) { |
1258 | 0 | if (!materialized_function_map_str.empty()) { |
1259 | 0 | materialized_function_map_str += ','; |
1260 | 0 | } |
1261 | 0 | materialized_function_map_str += str.first; |
1262 | 0 | } |
1263 | 0 | return Status::InternalError( |
1264 | 0 | "referenced column was missing. [column={},materialized_function_map={}]", |
1265 | 0 | new_column.name(), materialized_function_map_str); |
1266 | 0 | } |
1267 | | |
1268 | 0 | if (new_column.name().find("__doris_shadow_") == 0) { |
1269 | | // Should delete in the future, just a protection for bug. |
1270 | 0 | LOG(INFO) << "a shadow column is encountered " << new_column.name(); |
1271 | 0 | return Status::InternalError("failed due to operate on shadow column"); |
1272 | 0 | } |
1273 | | // Newly added column go here |
1274 | 0 | column_mapping->ref_column_idx = -1; |
1275 | |
|
1276 | 0 | if (i < base_tablet_schema->num_short_key_columns()) { |
1277 | 0 | *sc_directly = true; |
1278 | 0 | } |
1279 | 0 | RETURN_IF_ERROR( |
1280 | 0 | _init_column_mapping(column_mapping, new_column, new_column.default_value())); |
1281 | | |
1282 | 0 | LOG(INFO) << "A column with default value will be added after schema changing. " |
1283 | 0 | << "column=" << new_column.name() |
1284 | 0 | << ", default_value=" << new_column.default_value(); |
1285 | 0 | } |
1286 | | |
1287 | 0 | if (materialized_function_map.contains(WHERE_SIGN_LOWER)) { |
1288 | 0 | changer->set_where_expr(materialized_function_map.find(WHERE_SIGN_LOWER)->second.expr); |
1289 | 0 | } |
1290 | | |
1291 | | // If the reference sequence of the Key column is out of order, it needs to be reordered |
1292 | 0 | int num_default_value = 0; |
1293 | |
|
1294 | 0 | for (int i = 0, new_schema_size = new_tablet_schema->num_key_columns(); i < new_schema_size; |
1295 | 0 | ++i) { |
1296 | 0 | ColumnMapping* column_mapping = changer->get_mutable_column_mapping(i); |
1297 | |
|
1298 | 0 | if (!column_mapping->has_reference()) { |
1299 | 0 | num_default_value++; |
1300 | 0 | continue; |
1301 | 0 | } |
1302 | | |
1303 | 0 | if (column_mapping->ref_column_idx != i - num_default_value) { |
1304 | 0 | *sc_sorting = true; |
1305 | 0 | return Status::OK(); |
1306 | 0 | } |
1307 | 0 | } |
1308 | | |
1309 | 0 | if (base_tablet_schema->keys_type() != new_tablet_schema->keys_type()) { |
1310 | | // only when base table is dup and mv is agg |
1311 | | // the rollup job must be reagg. |
1312 | 0 | *sc_sorting = true; |
1313 | 0 | return Status::OK(); |
1314 | 0 | } |
1315 | | |
1316 | | // If the sort of key has not been changed but the new keys num is less then base's, |
1317 | | // the new table should be re agg. |
1318 | | // So we also need to set sc_sorting = true. |
1319 | | // A, B, C are keys(sort keys), D is value |
1320 | | // followings need resort: |
1321 | | // old keys: A B C D |
1322 | | // new keys: A B |
1323 | 0 | if (new_tablet_schema->keys_type() != KeysType::DUP_KEYS && |
1324 | 0 | new_tablet_schema->num_key_columns() < base_tablet_schema->num_key_columns()) { |
1325 | | // this is a table with aggregate key type, and num of key columns in new schema |
1326 | | // is less, which means the data in new tablet should be more aggregated. |
1327 | | // so we use sorting schema change to sort and merge the data. |
1328 | 0 | *sc_sorting = true; |
1329 | 0 | return Status::OK(); |
1330 | 0 | } |
1331 | | |
1332 | 0 | if (sc_params.alter_tablet_type == ROLLUP) { |
1333 | 0 | *sc_directly = true; |
1334 | 0 | return Status::OK(); |
1335 | 0 | } |
1336 | | |
1337 | 0 | if (sc_params.enable_unique_key_merge_on_write && |
1338 | 0 | new_tablet_schema->num_key_columns() > base_tablet_schema->num_key_columns()) { |
1339 | 0 | *sc_directly = true; |
1340 | 0 | return Status::OK(); |
1341 | 0 | } |
1342 | | |
1343 | 0 | if (base_tablet_schema->num_short_key_columns() != new_tablet_schema->num_short_key_columns()) { |
1344 | | // the number of short_keys changed, can't do linked schema change |
1345 | 0 | *sc_directly = true; |
1346 | 0 | return Status::OK(); |
1347 | 0 | } |
1348 | | |
1349 | 0 | if (!sc_params.delete_handler->empty()) { |
1350 | | // there exists delete condition in header, can't do linked schema change |
1351 | 0 | *sc_directly = true; |
1352 | 0 | return Status::OK(); |
1353 | 0 | } |
1354 | | |
1355 | | // if new tablet enable row store, or new tablet has different row store columns |
1356 | 0 | if ((!base_tablet_schema->exist_column(BeConsts::ROW_STORE_COL) && |
1357 | 0 | new_tablet_schema->exist_column(BeConsts::ROW_STORE_COL)) || |
1358 | 0 | !std::equal(new_tablet_schema->row_columns_uids().begin(), |
1359 | 0 | new_tablet_schema->row_columns_uids().end(), |
1360 | 0 | base_tablet_schema->row_columns_uids().begin(), |
1361 | 0 | base_tablet_schema->row_columns_uids().end())) { |
1362 | 0 | *sc_directly = true; |
1363 | 0 | } |
1364 | |
|
1365 | 0 | for (size_t i = 0; i < new_tablet_schema->num_columns(); ++i) { |
1366 | 0 | ColumnMapping* column_mapping = changer->get_mutable_column_mapping(i); |
1367 | 0 | if (column_mapping->expr != nullptr) { |
1368 | 0 | *sc_directly = true; |
1369 | 0 | return Status::OK(); |
1370 | 0 | } else if (column_mapping->ref_column_idx >= 0) { |
1371 | | // index changed |
1372 | 0 | if (vectorized::schema_util::has_schema_index_diff( |
1373 | 0 | new_tablet_schema, base_tablet_schema, i, column_mapping->ref_column_idx)) { |
1374 | 0 | *sc_directly = true; |
1375 | 0 | return Status::OK(); |
1376 | 0 | } |
1377 | 0 | } |
1378 | 0 | } |
1379 | | |
1380 | | // if rs_reader has remote files, link schema change is not supported, |
1381 | | // use directly schema change instead. |
1382 | 0 | if (!(*sc_directly) && !(*sc_sorting)) { |
1383 | | // check has remote rowset |
1384 | | // work for cloud and cold storage |
1385 | 0 | for (const auto& rs_reader : sc_params.ref_rowset_readers) { |
1386 | 0 | if (!rs_reader->rowset()->is_local()) { |
1387 | 0 | *sc_directly = true; |
1388 | 0 | break; |
1389 | 0 | } |
1390 | 0 | } |
1391 | 0 | } |
1392 | |
|
1393 | 0 | return Status::OK(); |
1394 | 0 | } |
1395 | | |
1396 | | Status SchemaChangeJob::_init_column_mapping(ColumnMapping* column_mapping, |
1397 | | const TabletColumn& column_schema, |
1398 | 0 | const std::string& value) { |
1399 | 0 | if (auto field = WrapperField::create(column_schema); field.has_value()) { |
1400 | 0 | column_mapping->default_value = field.value(); |
1401 | 0 | } else { |
1402 | 0 | return field.error(); |
1403 | 0 | } |
1404 | | |
1405 | 0 | if (column_schema.is_nullable() && value.length() == 0) { |
1406 | 0 | column_mapping->default_value->set_null(); |
1407 | 0 | } else { |
1408 | 0 | RETURN_IF_ERROR(column_mapping->default_value->from_string(value, column_schema.precision(), |
1409 | 0 | column_schema.frac())); |
1410 | 0 | } |
1411 | | |
1412 | 0 | return Status::OK(); |
1413 | 0 | } |
1414 | | |
1415 | 0 | Status SchemaChangeJob::_validate_alter_result(const TAlterTabletReqV2& request) { |
1416 | 0 | Version max_continuous_version = {-1, 0}; |
1417 | 0 | _new_tablet->max_continuous_version_from_beginning(&max_continuous_version); |
1418 | 0 | LOG(INFO) << "find max continuous version of tablet=" << _new_tablet->tablet_id() |
1419 | 0 | << ", start_version=" << max_continuous_version.first |
1420 | 0 | << ", end_version=" << max_continuous_version.second; |
1421 | 0 | if (max_continuous_version.second < request.alter_version) { |
1422 | 0 | return Status::InternalError("result version={} is less than request version={}", |
1423 | 0 | max_continuous_version.second, request.alter_version); |
1424 | 0 | } |
1425 | | |
1426 | 0 | std::vector<std::pair<Version, RowsetSharedPtr>> version_rowsets; |
1427 | 0 | { |
1428 | 0 | std::shared_lock rdlock(_new_tablet->get_header_lock()); |
1429 | 0 | _new_tablet->acquire_version_and_rowsets(&version_rowsets); |
1430 | 0 | } |
1431 | 0 | for (auto& pair : version_rowsets) { |
1432 | 0 | RowsetSharedPtr rowset = pair.second; |
1433 | 0 | if (!rowset->check_file_exist()) { |
1434 | 0 | return Status::Error<NOT_FOUND>( |
1435 | 0 | "SchemaChangeJob::_validate_alter_result meet invalid rowset"); |
1436 | 0 | } |
1437 | 0 | } |
1438 | 0 | return Status::OK(); |
1439 | 0 | } |
1440 | | |
1441 | | // For unique with merge-on-write table, should process delete bitmap here. |
1442 | | // 1. During double write, the newly imported rowsets does not calculate |
1443 | | // delete bitmap and publish successfully. |
1444 | | // 2. After conversion, calculate delete bitmap for the rowsets imported |
1445 | | // during double write. During this period, new data can still be imported |
1446 | | // witout calculating delete bitmap and publish successfully. |
1447 | | // 3. Block the new publish, calculate the delete bitmap of the |
1448 | | // incremental rowsets. |
1449 | | // 4. Switch the tablet status to TABLET_RUNNING. The newly imported |
1450 | | // data will calculate delete bitmap. |
1451 | 0 | Status SchemaChangeJob::_calc_delete_bitmap_for_mow_table(int64_t alter_version) { |
1452 | 0 | DBUG_EXECUTE_IF("SchemaChangeJob._calc_delete_bitmap_for_mow_table.random_failed", { |
1453 | 0 | if (rand() % 100 < (100 * dp->param("percent", 0.1))) { |
1454 | 0 | LOG_WARNING("SchemaChangeJob._calc_delete_bitmap_for_mow_table.random_failed"); |
1455 | 0 | return Status::InternalError("debug schema change calc delete bitmap random failed"); |
1456 | 0 | } |
1457 | 0 | }); |
1458 | | |
1459 | | // can't do compaction when calc delete bitmap, if the rowset being calculated does |
1460 | | // a compaction, it may cause the delete bitmap to be missed. |
1461 | 0 | std::lock_guard base_compaction_lock(_new_tablet->get_base_compaction_lock()); |
1462 | 0 | std::lock_guard cumu_compaction_lock(_new_tablet->get_cumulative_compaction_lock()); |
1463 | | |
1464 | | // step 2 |
1465 | 0 | int64_t max_version = _new_tablet->max_version().second; |
1466 | 0 | std::vector<RowsetSharedPtr> rowsets; |
1467 | 0 | if (alter_version < max_version) { |
1468 | 0 | LOG(INFO) << "alter table for unique with merge-on-write, calculate delete bitmap of " |
1469 | 0 | << "double write rowsets for version: " << alter_version + 1 << "-" << max_version |
1470 | 0 | << " new_tablet=" << _new_tablet->tablet_id(); |
1471 | 0 | std::shared_lock rlock(_new_tablet->get_header_lock()); |
1472 | 0 | RETURN_IF_ERROR(_new_tablet->capture_consistent_rowsets_unlocked( |
1473 | 0 | {alter_version + 1, max_version}, &rowsets)); |
1474 | 0 | } |
1475 | 0 | for (auto rowset_ptr : rowsets) { |
1476 | 0 | std::lock_guard rwlock(_new_tablet->get_rowset_update_lock()); |
1477 | 0 | std::shared_lock rlock(_new_tablet->get_header_lock()); |
1478 | 0 | RETURN_IF_ERROR(Tablet::update_delete_bitmap_without_lock(_new_tablet, rowset_ptr)); |
1479 | 0 | } |
1480 | | |
1481 | | // step 3 |
1482 | 0 | std::lock_guard rwlock(_new_tablet->get_rowset_update_lock()); |
1483 | 0 | std::lock_guard new_wlock(_new_tablet->get_header_lock()); |
1484 | 0 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1485 | 0 | int64_t new_max_version = _new_tablet->max_version_unlocked(); |
1486 | 0 | rowsets.clear(); |
1487 | 0 | if (max_version < new_max_version) { |
1488 | 0 | LOG(INFO) << "alter table for unique with merge-on-write, calculate delete bitmap of " |
1489 | 0 | << "incremental rowsets for version: " << max_version + 1 << "-" |
1490 | 0 | << new_max_version << " new_tablet=" << _new_tablet->tablet_id(); |
1491 | 0 | RETURN_IF_ERROR(_new_tablet->capture_consistent_rowsets_unlocked( |
1492 | 0 | {max_version + 1, new_max_version}, &rowsets)); |
1493 | 0 | } |
1494 | 0 | for (auto&& rowset_ptr : rowsets) { |
1495 | 0 | RETURN_IF_ERROR(Tablet::update_delete_bitmap_without_lock(_new_tablet, rowset_ptr)); |
1496 | 0 | } |
1497 | | // step 4 |
1498 | 0 | RETURN_IF_ERROR(_new_tablet->set_tablet_state(TabletState::TABLET_RUNNING)); |
1499 | 0 | _new_tablet->save_meta(); |
1500 | 0 | return Status::OK(); |
1501 | 0 | } |
1502 | | |
1503 | | } // namespace doris |