Coverage Report

Created: 2026-10-24 03:36

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
#include "io/fs/file_system.h"
25
26
namespace doris {
27
class RuntimeProfile;
28
class RuntimeState;
29
30
class Block;
31
class SpillDataDir;
32
class SpillFileWriter;
33
class SpillFileReader;
34
using SpillFileWriterSPtr = std::shared_ptr<SpillFileWriter>;
35
using SpillFileReaderSPtr = std::shared_ptr<SpillFileReader>;
36
37
/// SpillFile represents a logical spill file that may consist of multiple
38
/// physical "part" files on disk. Parts are managed automatically by
39
/// SpillFileWriter when a part exceeds the configured size threshold.
40
///
41
/// On-disk layout (a directory on a local disk, or a key prefix in object storage;
42
/// which one is decided by the SpillDataDir the file was created on):
43
///   spill_dir/         (created lazily by SpillFileWriter on first write)
44
///   +-- 0              (part 0)
45
///   +-- 1              (part 1)
46
///   +-- ...
47
///
48
/// Writing workflow:
49
///   SpillFileWriterSPtr writer;
50
///   RETURN_IF_ERROR(spill_file->create_writer(state, profile, writer));
51
///   RETURN_IF_ERROR(writer->write_block(state, block)); // auto-rotates parts
52
///   RETURN_IF_ERROR(writer->close());                   // finalizes all parts
53
///
54
/// Reading workflow:
55
///   auto reader = spill_file->create_reader(state, profile);
56
///   RETURN_IF_ERROR(reader->open());
57
///   while (!eos) { RETURN_IF_ERROR(reader->read(&block, &eos)); }
58
class SpillFile : public std::enable_shared_from_this<SpillFile> {
59
public:
60
    // to avoid too many small file writes
61
    static constexpr size_t MIN_SPILL_WRITE_BATCH_MEM = 512 * 1024;
62
    static constexpr size_t MAX_SPILL_WRITE_BATCH_MEM = 32 * 1024 * 1024;
63
64
    /// @param data_dir       The spill storage directory (disk) selected by SpillFileManager.
65
    /// @param relative_path  Relative path under the spill root, formatted by the operator.
66
    ///                       e.g. "query_id/sort-node_id-task_id-unique_id"
67
    SpillFile(SpillDataDir* data_dir, io::FileSystemSPtr fs, std::string relative_path);
68
    // Convenience for local stores, whose filesystem cannot rotate.
69
    SpillFile(SpillDataDir* data_dir, std::string relative_path);
70
71
    SpillFile() = delete;
72
    SpillFile(const SpillFile&) = delete;
73
    SpillFile& operator=(const SpillFile&) = delete;
74
75
    ~SpillFile();
76
77
    void gc();
78
79
    /// Returns true after the writer has been closed (all data flushed).
80
41
    bool ready_for_reading() const { return _ready_for_reading; }
81
82
    /// Create a SpillFileWriter that automatically manages multi-part rotation.
83
    /// Only one writer should exist per SpillFile at a time.
84
    /// Part size threshold is read from config::spill_file_part_size_bytes.
85
    Status create_writer(RuntimeState* state, RuntimeProfile* profile, SpillFileWriterSPtr& writer);
86
87
    /// Create a SpillFileReader that reads sequentially across all parts.
88
    /// The caller should call reader->open() before reading.
89
    SpillFileReaderSPtr create_reader(RuntimeState* state, RuntimeProfile* profile) const;
90
91
private:
92
    friend class SpillFileWriter;
93
    friend class SpillFileManager;
94
95
    /// Called by SpillFileWriter::close() to mark writing as complete.
96
    void finish_writing();
97
98
    /// Called by SpillFileWriter to incrementally track bytes written to disk.
99
    /// This ensures SpillFile always knows the correct _total_written_bytes for
100
    /// gc() accounting, even if the writer's close() is never properly called.
101
    void update_written_bytes(int64_t delta_bytes);
102
103
    /// Called by SpillFileWriter when a part file is completed, in part order.
104
    /// @param part_bytes  size of the part file including its footer
105
    void add_part(int64_t part_bytes);
106
107
    /// A failed S3 close may still have published an object. It is not readable as a part,
108
    /// but remains billable until cleanup; list the prefix to remove publication ambiguity.
109
    void account_potential_part(int64_t part_bytes);
110
111
    /// Stable query/storage identity and current size of this logical file for lifecycle logs.
112
    std::string _log_context() const;
113
114
    SpillDataDir* _data_dir = nullptr;
115
    // Vault binding at file creation; never follow a later change of the spill vault.
116
    io::FileSystemSPtr _fs;
117
    std::string _query_id;
118
    // Path of this spill file: data_dir->get_spill_data_path() + "/" + relative_path.
119
    // Absolute for local stores, relative to the vault prefix for remote stores.
120
    std::string _spill_dir;
121
    // Full S3 URI or local path, pinned with _fs even if the selected vault changes later.
122
    std::string _log_path;
123
    int64_t _total_written_bytes = 0;
124
    int64_t _persisted_bytes = 0;
125
    // Size of every completed part, in part order. Passed to readers so that they never
126
    // have to ask the storage for the file size (one HEAD request per part on S3).
127
    std::vector<int64_t> _part_sizes;
128
    // Set by SpillFileWriter when it creates the directory of the first part, before the part
129
    // itself is created. Files that were never written have nothing to delete.
130
    bool _dir_created = false;
131
    bool _ready_for_reading = false;
132
    // gc() can be called explicitly before destruction. Emit/count logical cleanup only once.
133
    bool _gc_started = false;
134
    // Pointer to the currently-active writer. Mutable to allow checks from const
135
    // methods like create_reader(). Only one writer may be active at a time. Cleared when
136
    // the writer finished, was discarded by gc(), or was destroyed.
137
    mutable SpillFileWriter* _active_writer = nullptr;
138
};
139
using SpillFileSPtr = std::shared_ptr<SpillFile>;
140
} // namespace doris