be/src/exec/spill/spill_file_reader.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <gen_cpp/data.pb.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <string> |
24 | | #include <vector> |
25 | | |
26 | | #include "common/status.h" |
27 | | #include "core/pod_array.h" |
28 | | #include "core/pod_array_fwd.h" |
29 | | #include "io/fs/file_reader_writer_fwd.h" |
30 | | #include "io/fs/file_system.h" |
31 | | #include "runtime/runtime_profile.h" |
32 | | #include "runtime/workload_management/resource_context.h" |
33 | | #include "util/slice.h" |
34 | | |
35 | | namespace doris { |
36 | | class RuntimeState; |
37 | | class Block; |
38 | | class SpillDataDir; |
39 | | |
40 | | /// SpillFileReader reads blocks sequentially across all parts of a SpillFile. |
41 | | /// |
42 | | /// Usage: |
43 | | /// auto reader = spill_file->create_reader(state, profile); |
44 | | /// RETURN_IF_ERROR(reader->open()); |
45 | | /// bool eos = false; |
46 | | /// while (!eos) { RETURN_IF_ERROR(reader->read(&block, &eos)); } |
47 | | /// |
48 | | /// Part boundaries are transparent to the caller. When the current part is |
49 | | /// exhausted, the reader automatically opens the next part. |
50 | | /// |
51 | | /// Parts are opened on the file system pinned in the SpillFile (local disk or object storage). |
52 | | /// Part sizes are known from the writer, so no size lookup is needed on open. |
53 | | class SpillFileReader { |
54 | | public: |
55 | | SpillFileReader(RuntimeState* state, RuntimeProfile* profile, SpillDataDir* data_dir, |
56 | | io::FileSystemSPtr fs, std::string spill_dir, std::vector<int64_t> part_sizes); |
57 | | |
58 | 222 | virtual ~SpillFileReader() { (void)close(); } |
59 | | |
60 | | /// Open the first part and read its footer metadata. |
61 | | Status open(); |
62 | | |
63 | | /// Read the next block. Automatically advances across part boundaries. |
64 | | /// Sets *eos = true when all parts are exhausted. |
65 | | Status read(Block* block, bool* eos); |
66 | | |
67 | | /// Seek to a global block index within the whole spill file. |
68 | | /// block_index is 0-based across all parts. |
69 | | /// If block_index is out of range, the reader is positioned at EOS. |
70 | | Status seek(size_t block_index); |
71 | | |
72 | | Status close(); |
73 | | |
74 | | protected: |
75 | | /// Storage-specific read sizing. Local files read the footer tail and one block at a time. |
76 | | virtual size_t _footer_probe_size(size_t file_size, bool fetch_small_part) const; |
77 | | virtual size_t _read_window_end(size_t index) const; |
78 | | |
79 | | /// Account a successful store read in the storage-specific counters. |
80 | | virtual void _record_spill_read(size_t bytes_read); |
81 | | |
82 | | private: |
83 | | /// Open a specific part file and read its footer. `fetch_small_part` allows the storage |
84 | | /// policy to fetch the whole part when it fits in one read. |
85 | | Status _open_part(size_t part_index, bool fetch_small_part); |
86 | | |
87 | | /// Read the footer (block offsets, max sub block size, block count) of the current part. |
88 | | Status _read_footer(size_t file_size, bool fetch_small_part); |
89 | | |
90 | | /// Point `out` at the serialized bytes of block `index` of the current part, reading |
91 | | /// them (and any following blocks selected by the storage policy) if not buffered. |
92 | | Status _block_slice(size_t index, Slice* out); |
93 | | |
94 | | /// Read exactly `len` bytes at `offset` of the current part into `data`. |
95 | | Status _read_exact(size_t offset, char* data, size_t len); |
96 | | |
97 | | /// Seek implementation with status propagation. |
98 | | Status _seek_to_block(size_t block_index); |
99 | | |
100 | | /// Close the current part's file reader. |
101 | | void _close_current_part(); |
102 | | |
103 | | /// Make the read buffer hold at least `size` bytes. |
104 | | void _ensure_read_buff(size_t size); |
105 | | |
106 | | /// Account bytes read from the store (local or remote) as one request. |
107 | | void _record_read(size_t bytes_read); |
108 | | |
109 | | // ── Configuration ── |
110 | | SpillDataDir* _data_dir = nullptr; |
111 | | io::FileSystemSPtr _fs; |
112 | | std::string _spill_dir; |
113 | | std::vector<int64_t> _part_sizes; |
114 | | size_t _part_count; |
115 | | |
116 | | // ── Current part state ── |
117 | | size_t _current_part_index = 0; |
118 | | bool _is_open = false; |
119 | | bool _part_opened = false; |
120 | | io::FileReaderSPtr _file_reader; |
121 | | size_t _part_block_count = 0; |
122 | | size_t _part_read_block_index = 0; |
123 | | size_t _part_max_sub_block_size = 0; |
124 | | // Holds the bytes of [_window_begin, _window_end) of the current part. |
125 | | PaddedPODArray<char> _read_buff; |
126 | | size_t _window_begin = 0; |
127 | | size_t _window_end = 0; |
128 | | |
129 | | PBlock _pb_block; |
130 | | |
131 | | // ── Counters ── |
132 | | RuntimeProfile::Counter* _read_file_timer = nullptr; |
133 | | RuntimeProfile::Counter* _deserialize_timer = nullptr; |
134 | | RuntimeProfile::Counter* _read_block_count = nullptr; |
135 | | RuntimeProfile::Counter* _read_block_data_size = nullptr; |
136 | | RuntimeProfile::Counter* _read_file_size = nullptr; |
137 | | RuntimeProfile::Counter* _read_rows_count = nullptr; |
138 | | RuntimeProfile::Counter* _read_file_count = nullptr; |
139 | | |
140 | | protected: |
141 | | // Includes the footer start as the final offset, so every block has an end offset. |
142 | | std::vector<size_t> _block_start_offsets; |
143 | | std::shared_ptr<ResourceContext> _resource_ctx = nullptr; |
144 | | }; |
145 | | |
146 | | using SpillFileReaderSPtr = std::shared_ptr<SpillFileReader>; |
147 | | |
148 | | } // namespace doris |