Coverage Report

Created: 2026-10-30 15:50

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