forked from alibaba/paimon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefetch_file_batch_reader_impl.h
More file actions
170 lines (147 loc) · 6.6 KB
/
prefetch_file_batch_reader_impl.h
File metadata and controls
170 lines (147 loc) · 6.6 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
/*
* Copyright 2024-present Alibaba Inc.
*
* Licensed 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.
*/
#pragma once
#include <atomic>
#include <cassert>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <limits>
#include <memory>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "arrow/c/abi.h"
#include "paimon/common/utils/threadsafe_queue.h"
#include "paimon/reader/batch_reader.h"
#include "paimon/reader/prefetch_file_batch_reader.h"
#include "paimon/result.h"
#include "paimon/status.h"
#include "paimon/utils/read_ahead_cache.h"
#include "paimon/utils/roaring_bitmap32.h"
struct ArrowSchema;
namespace paimon {
class ReaderBuilder;
class FileSystem;
class Executor;
class Predicate;
class Metrics;
class PrefetchFileBatchReaderImpl : public PrefetchFileBatchReader {
public:
static Result<std::unique_ptr<PrefetchFileBatchReaderImpl>> Create(
const std::string& data_file_path, const ReaderBuilder* reader_builder,
const std::shared_ptr<FileSystem>& fs, uint32_t prefetch_max_parallel_num,
int32_t batch_size, uint32_t prefetch_batch_count, bool enable_adaptive_prefetch_strategy,
const std::shared_ptr<Executor>& executor, bool initialize_read_ranges,
PrefetchCacheMode prefetch_cache_mode, const CacheConfig& cache_config,
const std::shared_ptr<MemoryPool>& pool);
~PrefetchFileBatchReaderImpl() override;
Result<FileBatchReader::ReadBatch> NextBatch() override {
return Status::Invalid(
"paimon inner reader PrefetchFileBatchReader should use NextBatchWithBitmap");
}
Result<FileBatchReader::ReadBatchWithBitmap> NextBatchWithBitmap() override;
std::shared_ptr<Metrics> GetReaderMetrics() const override;
Result<std::unique_ptr<::ArrowSchema>> GetFileSchema() const override;
Status SetReadSchema(::ArrowSchema* read_schema, const std::shared_ptr<Predicate>& predicate,
const std::optional<RoaringBitmap32>& selection_bitmap) override;
Status SeekToRow(uint64_t row_number) override;
Result<uint64_t> GetPreviousBatchFirstRowNumber() const override;
Result<uint64_t> GetNumberOfRows() const override;
uint64_t GetNextRowToRead() const override;
void Close() override;
Status SetReadRanges(const std::vector<std::pair<uint64_t, uint64_t>>& read_ranges) override;
Result<std::vector<std::pair<uint64_t, uint64_t>>> GenReadRanges(
bool* need_prefetch) const override {
assert(false);
return Status::NotImplemented("gen read ranges not implemented");
}
bool SupportPreciseBitmapSelection() const override {
return readers_[0]->SupportPreciseBitmapSelection();
}
Status RefreshReadRanges();
inline PrefetchFileBatchReader* GetFirstReader() const {
return readers_[0].get();
}
inline bool NeedPrefetch() const {
return need_prefetch_;
}
private:
struct PrefetchBatch {
std::pair<uint64_t, uint64_t> read_range;
BatchReader::ReadBatchWithBitmap batch;
uint64_t previous_batch_first_row_num;
};
PrefetchFileBatchReaderImpl(
const std::vector<std::shared_ptr<PrefetchFileBatchReader>>& readers, int32_t batch_size,
uint32_t prefetch_queue_capacity, bool enable_adaptive_prefetch_strategy,
const std::shared_ptr<Executor>& executor, const std::shared_ptr<ReadAheadCache>& cache,
PrefetchCacheMode cache_mode);
Status CleanUp();
void Workloop();
void SetReadStatus(const Status& status);
Status GetReadStatus() const;
Result<bool> IsEofRange(const std::pair<uint64_t, uint64_t>& read_range) const;
Status DoReadBatch(size_t reader_idx);
void ReadBatch(size_t reader_idx);
size_t GetEnabledReaderSize() const;
static std::vector<std::pair<uint64_t, uint64_t>> FilterReadRanges(
const std::vector<std::pair<uint64_t, uint64_t>>& read_range,
const std::optional<RoaringBitmap32>& selection_bitmap);
static std::vector<std::vector<std::pair<uint64_t, uint64_t>>> DispatchReadRanges(
const std::vector<std::pair<uint64_t, uint64_t>>& read_ranges, size_t reader_count);
Result<std::pair<uint64_t, uint64_t>> EofRange() const;
std::optional<std::pair<uint64_t, uint64_t>> GetCurrentReadRange(size_t reader_idx) const;
Status EnsureReaderPosition(size_t reader_idx,
const std::pair<uint64_t, uint64_t>& read_range) const;
Status HandleReadResult(size_t reader_idx, const std::pair<uint64_t, uint64_t>& read_range,
FileBatchReader::ReadBatchWithBitmap&& read_batch_with_bitmap);
bool NeedInitCache() const;
private:
std::vector<std::shared_ptr<PrefetchFileBatchReader>> readers_;
// The meaning of readers_pos_ is: all data before this pos has been filtered out or effectively
// consumed, and the data after this pos may need to be read in the next round of reading.
std::vector<std::unique_ptr<std::atomic<uint64_t>>> readers_pos_;
std::vector<std::unique_ptr<std::atomic<uint64_t>>> seek_cnt_;
const int32_t batch_size_;
std::optional<RoaringBitmap32> selection_bitmap_;
std::shared_ptr<Predicate> predicate_;
std::deque<std::pair<uint64_t, uint64_t>> read_ranges_;
std::vector<std::vector<std::pair<uint64_t, uint64_t>>> read_ranges_in_group_;
std::vector<std::unique_ptr<ThreadsafeQueue<PrefetchBatch>>> prefetch_queues_;
std::vector<bool> reader_is_working_;
std::mutex working_mutex_;
std::condition_variable cv_;
std::shared_ptr<Executor> executor_;
std::shared_ptr<ReadAheadCache> cache_;
PrefetchCacheMode cache_mode_;
mutable std::shared_mutex rw_mutex_;
std::unique_ptr<std::thread> background_thread_;
Status read_status_;
std::atomic<bool> is_shutdown_ = false;
uint64_t previous_batch_first_row_num_ = std::numeric_limits<uint64_t>::max();
bool need_prefetch_ = false;
bool read_ranges_freshed_ = false;
const uint32_t prefetch_queue_capacity_;
const bool enable_adaptive_prefetch_strategy_;
int32_t parallel_num_;
};
} // namespace paimon