Coverage Report

Created: 2026-09-30 04:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/spill/spill_file.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
#include <memory>
20
#include <string>
21
#include <vector>
22
23
#include "common/status.h"
24
25
namespace doris {
26
class RuntimeProfile;
27
class RuntimeState;
28
29
class Block;
30
class SpillDataDir;
31
class SpillFileWriter;
32
class SpillFileReader;
33
using SpillFileWriterSPtr = std::shared_ptr<SpillFileWriter>;
34
using SpillFileReaderSPtr = std::shared_ptr<SpillFileReader>;
35
36
/// SpillFile represents a logical spill file that may consist of multiple
37
/// physical "part" files on disk. Parts are managed automatically by
38
/// SpillFileWriter when a part exceeds the configured size threshold.
39
///
40
/// On-disk layout (a directory on a local disk, or a key prefix in object storage;
41
/// which one is decided by the SpillDataDir the file was created on):
42
///   spill_dir/         (created lazily by SpillFileWriter on first write)
43
///   +-- 0              (part 0)
44
///   +-- 1              (part 1)
45
///   +-- ...
46
///
47
/// Writing workflow:
48
///   SpillFileWriterSPtr writer;
49
///   RETURN_IF_ERROR(spill_file->create_writer(state, profile, writer));
50
///   RETURN_IF_ERROR(writer->write_block(state, block)); // auto-rotates parts
51
///   RETURN_IF_ERROR(writer->close());                   // finalizes all parts
52
///
53
/// Reading workflow:
54
///   auto reader = spill_file->create_reader(state, profile);
55
///   RETURN_IF_ERROR(reader->open());
56
///   while (!eos) { RETURN_IF_ERROR(reader->read(&block, &eos)); }
57
class SpillFile : public std::enable_shared_from_this<SpillFile> {
58
public:
59
    // to avoid too many small file writes
60
    static constexpr size_t MIN_SPILL_WRITE_BATCH_MEM = 512 * 1024;
61
    static constexpr size_t MAX_SPILL_WRITE_BATCH_MEM = 32 * 1024 * 1024;
62
63
    /// @param data_dir       The spill storage directory (disk) selected by SpillFileManager.
64
    /// @param relative_path  Relative path under the spill root, formatted by the operator.
65
    ///                       e.g. "query_id/sort-node_id-task_id-unique_id"
66
    SpillFile(SpillDataDir* data_dir, std::string relative_path);
67
68
    SpillFile() = delete;
69
    SpillFile(const SpillFile&) = delete;
70
    SpillFile& operator=(const SpillFile&) = delete;
71
72
    ~SpillFile();
73
74
    void gc();
75
76
    /// Returns true after the writer has been closed (all data flushed).
77
36
    bool ready_for_reading() const { return _ready_for_reading; }
78
79
    /// Create a SpillFileWriter that automatically manages multi-part rotation.
80
    /// Only one writer should exist per SpillFile at a time.
81
    /// Part size threshold is read from config::spill_file_part_size_bytes.
82
    Status create_writer(RuntimeState* state, RuntimeProfile* profile, SpillFileWriterSPtr& writer);
83
84
    /// Create a SpillFileReader that reads sequentially across all parts.
85
    /// The caller should call reader->open() before reading.
86
    SpillFileReaderSPtr create_reader(RuntimeState* state, RuntimeProfile* profile) const;
87
88
private:
89
    friend class SpillFileWriter;
90
    friend class SpillFileManager;
91
92
    /// Called by SpillFileWriter::close() to mark writing as complete.
93
    void finish_writing();
94
95
    /// Called by SpillFileWriter to incrementally track bytes written to disk.
96
    /// This ensures SpillFile always knows the correct _total_written_bytes for
97
    /// gc() accounting, even if the writer's close() is never properly called.
98
    void update_written_bytes(int64_t delta_bytes);
99
100
    /// Called by SpillFileWriter when a part file is completed, in part order.
101
    /// @param part_bytes  size of the part file including its footer
102
    void add_part(int64_t part_bytes);
103
104
    SpillDataDir* _data_dir = nullptr;
105
    // Path of this spill file: data_dir->get_spill_data_path() + "/" + relative_path.
106
    // Absolute for local stores, relative to the vault prefix for remote stores.
107
    std::string _spill_dir;
108
    int64_t _total_written_bytes = 0;
109
    // Size of every completed part, in part order. Passed to readers so that they never
110
    // have to ask the storage for the file size (one HEAD request per part on S3).
111
    std::vector<int64_t> _part_sizes;
112
    // Set by SpillFileWriter once the first part has been created. Files that were never
113
    // written have nothing to delete.
114
    bool _dir_created = false;
115
    bool _ready_for_reading = false;
116
    // Pointer to the currently-active writer. Mutable to allow checks from const
117
    // methods like create_reader(). Only one writer may be active at a time. Cleared when
118
    // the writer finished, was discarded by gc(), or was destroyed.
119
    mutable SpillFileWriter* _active_writer = nullptr;
120
};
121
using SpillFileSPtr = std::shared_ptr<SpillFile>;
122
} // namespace doris