forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwal_reader.cpp
More file actions
145 lines (133 loc) · 5.91 KB
/
wal_reader.cpp
File metadata and controls
145 lines (133 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "wal_reader.h"
#include <absl/strings/str_split.h>
#include "agent/be_exec_version_manager.h"
#include "common/logging.h"
#include "core/block/block.h"
#include "cpp/sync_point.h"
#include "load/group_commit/wal/wal_manager.h"
#include "runtime/runtime_state.h"
namespace doris {
WalReader::WalReader(RuntimeState* state) : _state(state) {
_wal_id = state->wal_id();
}
Status WalReader::init_reader(const TupleDescriptor* tuple_descriptor) {
_tuple_descriptor = tuple_descriptor;
RETURN_IF_ERROR(_state->exec_env()->wal_mgr()->get_wal_path(_wal_id, _wal_path));
_wal_reader = std::make_shared<doris::WalFileReader>(_wal_path);
RETURN_IF_ERROR(_wal_reader->init());
return Status::OK();
}
// ---- Unified init_reader(ReaderInitContext*) overrides ----
Status WalReader::_open_file_reader(ReaderInitContext* ctx) {
auto* wal_ctx = checked_context_cast<WalInitContext>(ctx);
_tuple_descriptor = wal_ctx->output_tuple_descriptor;
RETURN_IF_ERROR(_state->exec_env()->wal_mgr()->get_wal_path(_wal_id, _wal_path));
_wal_reader = std::make_shared<doris::WalFileReader>(_wal_path);
RETURN_IF_ERROR(_wal_reader->init());
return Status::OK();
}
Status WalReader::_do_init_reader(ReaderInitContext* /*base_ctx*/) {
return Status::OK();
}
Status WalReader::_do_get_next_block(Block* block, size_t* read_rows, bool* eof) {
//read src block
PBlock pblock;
auto st = _wal_reader->read_block(pblock);
if (st.is<ErrorCode::END_OF_FILE>()) {
LOG(INFO) << "read eof on wal:" << _wal_path;
*read_rows = 0;
*eof = true;
return Status::OK();
}
if (!st.ok()) {
LOG(WARNING) << "Failed to read wal on path = " << _wal_path;
return st;
}
int be_exec_version = pblock.has_be_exec_version() ? pblock.be_exec_version() : 0;
if (!BeExecVersionManager::check_be_exec_version(be_exec_version)) {
return Status::DataQualityError("check be exec version fail when reading wal file {}",
_wal_path);
}
Block src_block;
size_t uncompressed_size = 0;
int64_t uncompressed_time = 0;
RETURN_IF_ERROR(src_block.deserialize(pblock, &uncompressed_size, &uncompressed_time));
//convert to dst block
Block dst_block;
int index = 0;
auto output_block_columns = block->get_columns_with_type_and_name();
size_t output_block_column_size = output_block_columns.size();
TEST_SYNC_POINT_CALLBACK("WalReader::set_column_id_count", &_column_id_count);
TEST_SYNC_POINT_CALLBACK("WalReader::set_out_block_column_size", &output_block_column_size);
if (_column_id_count != src_block.columns() ||
output_block_column_size != _tuple_descriptor->slots().size()) {
return Status::InternalError(
"not equal wal _column_id_count={} vs wal block columns size={}, "
"output block columns size={} vs tuple_descriptor size={}",
std::to_string(_column_id_count), std::to_string(src_block.columns()),
std::to_string(output_block_column_size),
std::to_string(_tuple_descriptor->slots().size()));
}
for (auto* slot_desc : _tuple_descriptor->slots()) {
auto pos = _column_pos_map[slot_desc->col_unique_id()];
if (pos >= src_block.columns()) {
return Status::InternalError("read wal {} fail, pos {}, columns size {}", _wal_path,
pos, src_block.columns());
}
ColumnPtr column_ptr = src_block.get_by_position(pos).column;
if (!column_ptr && slot_desc->is_nullable()) {
column_ptr = make_nullable(column_ptr);
}
dst_block.insert(index, ColumnWithTypeAndName(std::move(column_ptr),
output_block_columns[index].type,
output_block_columns[index].name));
index++;
}
block->swap(dst_block);
*read_rows = block->rows();
VLOG_DEBUG << "read block rows:" << *read_rows;
return Status::OK();
}
Status WalReader::_get_columns_impl(std::unordered_map<std::string, DataTypePtr>* name_to_type) {
std::string col_ids;
RETURN_IF_ERROR(_wal_reader->read_header(_version, col_ids));
std::vector<std::string> column_id_vector =
absl::StrSplit(col_ids, ",", absl::SkipWhitespace());
_column_id_count = column_id_vector.size();
try {
int64_t pos = 0;
for (auto col_id_str : column_id_vector) {
auto col_id = std::strtoll(col_id_str.c_str(), nullptr, 10);
_column_pos_map.emplace(col_id, pos);
pos++;
}
} catch (const std::invalid_argument& e) {
return Status::InvalidArgument("Invalid format, {}", e.what());
}
// Report WAL columns so on_before_init_reader does not mark them as missing.
if (_tuple_descriptor) {
for (auto* slot_desc : _tuple_descriptor->slots()) {
if (_column_pos_map.contains(slot_desc->col_unique_id())) {
name_to_type->emplace(slot_desc->col_name(), slot_desc->get_data_type_ptr());
}
}
}
return Status::OK();
}
} // namespace doris