Coverage Report

Created: 2026-10-17 14:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
///
54
/// On object storage every read is a GET, so the reader keeps the request count low:
55
/// the part footer is fetched with one tail read of at most `_coalesce_bytes` (plus one more
56
/// read for the rest of a block offset array that does not fit), adjacent blocks are coalesced
57
/// into one read of at most `_coalesce_bytes`, and a part no larger than that is fetched whole.
58
/// A single block, or the rest of the offset array, larger than `_coalesce_bytes` is read whole.
59
class SpillFileReader {
60
public:
61
    SpillFileReader(RuntimeState* state, RuntimeProfile* profile, SpillDataDir* data_dir,
62
                    io::FileSystemSPtr fs, std::string spill_dir, std::vector<int64_t> part_sizes);
63
64
199
    ~SpillFileReader() { (void)close(); }
65
66
    /// Open the first part and read its footer metadata.
67
    Status open();
68
69
    /// Read the next block. Automatically advances across part boundaries.
70
    /// Sets *eos = true when all parts are exhausted.
71
    Status read(Block* block, bool* eos);
72
73
    /// Seek to a global block index within the whole spill file.
74
    /// block_index is 0-based across all parts.
75
    /// If block_index is out of range, the reader is positioned at EOS.
76
    Status seek(size_t block_index);
77
78
    Status close();
79
80
private:
81
    /// Open a specific part file and read its footer. With `fetch_small_part`, a part that
82
    /// fits in one coalesced read is fetched whole, so its blocks need no further reads.
83
    Status _open_part(size_t part_index, bool fetch_small_part);
84
85
    /// Read the footer (block offsets, max sub block size, block count) of the current part.
86
    Status _read_footer(size_t file_size, bool fetch_small_part);
87
88
    /// Point `out` at the serialized bytes of block `index` of the current part, reading
89
    /// them (and the following blocks that fit in the coalesce window) if not buffered.
90
    Status _block_slice(size_t index, Slice* out);
91
92
    /// Read exactly `len` bytes at `offset` of the current part into `data`.
93
    Status _read_exact(size_t offset, char* data, size_t len);
94
95
    /// Seek implementation with status propagation.
96
    Status _seek_to_block(size_t block_index);
97
98
    /// Close the current part's file reader.
99
    void _close_current_part();
100
101
    /// Make the read buffer hold at least `size` bytes.
102
    void _ensure_read_buff(size_t size);
103
104
    /// Account bytes read from the store (local or remote) as one request.
105
    void _record_read(size_t bytes_read);
106
107
    // ── Configuration ──
108
    SpillDataDir* _data_dir = nullptr;
109
    io::FileSystemSPtr _fs;
110
    std::string _spill_dir;
111
    std::vector<int64_t> _part_sizes;
112
    size_t _part_count;
113
    bool _is_remote = false;
114
    // Upper bound of one coalesced read; 0 reads block by block (local disk).
115
    size_t _coalesce_bytes = 0;
116
117
    // ── Current part state ──
118
    size_t _current_part_index = 0;
119
    bool _is_open = false;
120
    bool _part_opened = false;
121
    io::FileReaderSPtr _file_reader;
122
    size_t _part_block_count = 0;
123
    size_t _part_read_block_index = 0;
124
    size_t _part_max_sub_block_size = 0;
125
    // Holds the bytes of [_window_begin, _window_end) of the current part.
126
    PaddedPODArray<char> _read_buff;
127
    size_t _window_begin = 0;
128
    size_t _window_end = 0;
129
    std::vector<size_t> _block_start_offsets;
130
131
    PBlock _pb_block;
132
133
    // ── Counters ──
134
    RuntimeProfile::Counter* _read_file_timer = nullptr;
135
    RuntimeProfile::Counter* _deserialize_timer = nullptr;
136
    RuntimeProfile::Counter* _read_block_count = nullptr;
137
    RuntimeProfile::Counter* _read_block_data_size = nullptr;
138
    RuntimeProfile::Counter* _read_file_size = nullptr;
139
    RuntimeProfile::Counter* _read_rows_count = nullptr;
140
    RuntimeProfile::Counter* _read_file_count = nullptr;
141
    // Remote only, may be null when the profile does not register it.
142
    RuntimeProfile::Counter* _remote_read_requests = nullptr;
143
144
    std::shared_ptr<ResourceContext> _resource_ctx = nullptr;
145
};
146
147
using SpillFileReaderSPtr = std::shared_ptr<SpillFileReader>;
148
149
} // namespace doris