Repository navigation
Expand file tree
/
Copy pathconcat_batch_reader.cpp
More file actions
220 lines (202 loc) · 8.18 KB
/
Copy pathconcat_batch_reader.cpp
File metadata and controls
220 lines (202 loc) · 8.18 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* 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 "paimon/common/reader/concat_batch_reader.h"
#include <algorithm>
#include <chrono>
#include <exception>
#include <new>
#include <utility>
#include "arrow/c/abi.h"
#include "paimon/common/executor/future.h"
#include "paimon/common/metrics/metrics_impl.h"
#include "paimon/common/reader/reader_utils.h"
#include "paimon/common/utils/arrow/mem_utils.h"
#include "paimon/reader/file_batch_reader.h"
namespace paimon {
class MemoryPool;
template <typename Func>
static auto CatchPreparationErrors(Func&& func) -> decltype(func()) {
try {
return func();
} catch (const std::bad_alloc&) {
return Status::OutOfMemory("out of memory preparing file reader");
} catch (const std::exception& e) {
return Status::UnknownError("file reader preparation failed: ", e.what());
} catch (...) {
return Status::UnknownError("file reader preparation failed with an unknown exception");
}
}
ConcatBatchReader::ConcatBatchReader(std::vector<std::unique_ptr<BatchReader>>&& readers,
const std::shared_ptr<arrow::MemoryPool>& arrow_pool)
: arrow_pool_(arrow_pool), finished_reader_metrics_(std::make_shared<MetricsImpl>()) {
for (auto& reader : readers) {
readers_.emplace_back();
readers_.back().file_reader = dynamic_cast<FileBatchReader*>(reader.get());
readers_.back().reader = std::move(reader);
}
}
ConcatBatchReader::ConcatBatchReader(size_t reader_count, std::shared_ptr<Preparation> preparation,
size_t preparation_window,
const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
const std::shared_ptr<Executor>& build_executor)
: arrow_pool_(arrow_pool),
finished_reader_metrics_(std::make_shared<MetricsImpl>()),
preparation_(std::move(preparation)),
build_executor_(build_executor),
preparation_window_(preparation_window),
reader_count_(reader_count) {}
ConcatBatchReader::~ConcatBatchReader() {
ConcatBatchReader::Close();
}
Result<std::unique_ptr<ConcatBatchReader>> ConcatBatchReader::CreateLazy(
size_t reader_count, ReaderFactory factory, int32_t preparation_window,
const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
const std::shared_ptr<Executor>& build_executor) {
if (!factory || !build_executor) {
return Status::Invalid("lazy reader requires a factory and a build executor");
}
if (preparation_window <= 0) {
return Status::Invalid("reader preparation window must be positive");
}
auto preparation = std::make_shared<Preparation>(std::move(factory));
auto reader = std::unique_ptr<ConcatBatchReader>(
new ConcatBatchReader(reader_count, std::move(preparation),
static_cast<size_t>(preparation_window), arrow_pool, build_executor));
reader->FillWindow();
for (auto& state : reader->readers_) {
PAIMON_RETURN_NOT_OK(reader->ResolveReader(&state, /*wait=*/true));
}
return reader;
}
void ConcatBatchReader::FillWindow() {
while (next_reader_ < reader_count_ && readers_.size() < preparation_window_) {
readers_.emplace_back();
readers_.back().future =
Via(build_executor_.get(),
[preparation = preparation_,
index = next_reader_++]() mutable -> Result<std::unique_ptr<BatchReader>> {
// Release the task's creator before Via publishes its result, so draining
// the future also releases the background owner of the creation context.
auto retained = std::move(preparation);
if (retained->cancelled.load()) {
return std::unique_ptr<BatchReader>();
}
return CatchPreparationErrors([&]() { return retained->factory(index); });
});
}
}
Status ConcatBatchReader::ResolveReader(ReaderState* state, bool wait) {
if (!state->future.valid() ||
(!wait && state->future.wait_for(std::chrono::seconds(0)) != std::future_status::ready)) {
return state->failure;
}
state->failure = CatchPreparationErrors([state]() -> Status {
PAIMON_ASSIGN_OR_RAISE(state->reader, state->future.get());
state->file_reader = dynamic_cast<FileBatchReader*>(state->reader.get());
return Status::OK();
});
return state->failure;
}
Result<BatchReader::ReadBatch> ConcatBatchReader::NextBatch() {
PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatchWithBitmap batch_with_bitmap,
NextBatchWithBitmap());
auto result = ReaderUtils::ApplyBitmapToReadBatch(std::move(batch_with_bitmap), arrow_pool_);
if (!result.ok()) {
failure_ = result.status();
Close();
}
return result;
}
void ConcatBatchReader::Close() {
if (preparation_) {
preparation_->cancelled.store(true);
}
for (auto& state : readers_) {
try {
(void)ResolveReader(&state, /*wait=*/true);
} catch (...) {
// Keep draining even if recording a preparation failure runs out of memory.
}
CloseAndReleaseReader(&state);
}
readers_.clear();
next_reader_ = reader_count_;
preparation_.reset();
}
std::shared_ptr<Metrics> ConcatBatchReader::GetReaderMetrics() const {
auto metrics = std::make_shared<MetricsImpl>();
metrics->Merge(finished_reader_metrics_);
for (const auto& state : readers_) {
if (state.reader) {
metrics->Merge(state.reader->GetReaderMetrics());
}
}
return metrics;
}
void ConcatBatchReader::CloseAndReleaseReader(ReaderState* state) {
std::unique_ptr<BatchReader>& reader = state->reader;
if (!reader) {
return;
}
reader->Close();
finished_reader_metrics_->Merge(reader->GetReaderMetrics());
reader.reset();
state->file_reader = nullptr;
}
void ConcatBatchReader::WarmupRange() {
const size_t end = std::min(1 + kWarmupLookahead, readers_.size());
for (size_t i = 0; i < end; i++) {
// Never stall the current file on lookahead. Keep a failed preparation until its turn.
(void)ResolveReader(&readers_[i], /*wait=*/false);
if (readers_[i].file_reader != nullptr) {
readers_[i].file_reader->Warmup();
}
}
}
Result<BatchReader::ReadBatchWithBitmap> ConcatBatchReader::NextBatchWithBitmap() {
PAIMON_RETURN_NOT_OK(failure_);
while (!readers_.empty()) {
failure_ = ResolveReader(&readers_.front(), /*wait=*/true);
if (!failure_.ok()) {
Close();
return failure_;
}
// Lookahead: when the file system reads asynchronously, the next file's first read is paid
// while this file is still being consumed, instead of serially after its EOF.
WarmupRange();
auto& current_reader = readers_.front().reader;
if (current_reader) {
auto result = current_reader->NextBatchWithBitmap();
if (!result.ok()) {
failure_ = result.status();
Close();
return failure_;
}
if (!BatchReader::IsEofBatch(result.value())) {
return result;
}
}
CloseAndReleaseReader(&readers_.front());
readers_.pop_front();
FillWindow();
}
preparation_.reset();
return BatchReader::MakeEofBatchWithBitmap();
}
} // namespace paimon