be/src/exec/spill/spill_file_writer.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 <atomic> |
21 | | #include <memory> |
22 | | #include <string> |
23 | | |
24 | | #include "core/block/block.h" |
25 | | #include "io/fs/file_system.h" |
26 | | #include "io/fs/file_writer.h" |
27 | | #include "runtime/runtime_profile.h" |
28 | | #include "runtime/workload_management/resource_context.h" |
29 | | namespace doris { |
30 | | class RuntimeState; |
31 | | |
32 | | class SpillDataDir; |
33 | | class SpillFile; |
34 | | |
35 | | /// SpillFileWriter writes blocks to a SpillFile, automatically managing |
36 | | /// part-file rotation when a part exceeds the configured size threshold |
37 | | /// (config::spill_file_part_size_bytes). |
38 | | /// |
39 | | /// Usage: |
40 | | /// SpillFileWriterSPtr writer; |
41 | | /// RETURN_IF_ERROR(spill_file->create_writer(state, profile, writer)); |
42 | | /// RETURN_IF_ERROR(writer->write_block(state, block)); |
43 | | /// RETURN_IF_ERROR(writer->close()); |
44 | | /// |
45 | | /// Part rotation is fully internal. Each part file has its own footer with |
46 | | /// block offset metadata. Parts are named 0, 1, 2, ... within the SpillFile's |
47 | | /// directory. A part is closed synchronously when it is rotated out or when close() is called. |
48 | | /// If the SpillFile is destroyed before the writer is closed, the unfinished part is discarded. |
49 | | /// |
50 | | /// Files are created on the file system pinned in the SpillFile. This base class handles |
51 | | /// common part lifecycle and local writes; SpillS3FileWriter provides object-storage writes. |
52 | | class SpillFileWriter { |
53 | | public: |
54 | | SpillFileWriter(const std::shared_ptr<SpillFile>& spill_file, RuntimeState* state, |
55 | | RuntimeProfile* profile, SpillDataDir* data_dir, io::FileSystemSPtr fs, |
56 | | std::string spill_dir); |
57 | | |
58 | | virtual ~SpillFileWriter(); |
59 | | |
60 | | /// Write a block. Automatically opens the first part, splits large blocks, |
61 | | /// and rotates to a new part when the current one exceeds max_part_size. |
62 | | Status write_block(RuntimeState* state, const Block& block); |
63 | | |
64 | | /// Finalize: close the current part, record cumulative stats in SpillFile. After close(), |
65 | | /// no more writes are allowed. After a failed write_block() the file cannot be completed: |
66 | | /// close() drops the current part and returns that failure. |
67 | | Status close(); |
68 | | |
69 | | /// Largest serialized record and source Block written to this file, across all parts. |
70 | | /// Analytic replay uses these values before opening its readers. |
71 | 641 | size_t max_record_bytes() const { return _max_record_bytes; } |
72 | 641 | size_t max_block_bytes() const { return _max_block_bytes; } |
73 | 641 | size_t max_footer_bytes() const { return _max_footer_bytes; } |
74 | | |
75 | | protected: |
76 | | /// Preserve the first append failure for the whole part, including its footer. |
77 | | virtual Status _append_to_part(const Slice& data); |
78 | | |
79 | | /// Record bytes in the storage-specific query I/O context. |
80 | | virtual void _record_spill_write_bytes(int64_t bytes); |
81 | | |
82 | | /// Account a closed or discarded part after its writer has drained on failure. |
83 | | virtual void _on_part_closed(const std::shared_ptr<SpillFile>& spill_file, bool closed, |
84 | | int64_t close_ns); |
85 | | |
86 | 0 | const std::string& _spill_directory() const { return _spill_dir; } |
87 | | |
88 | | private: |
89 | | Status _close_impl(const std::shared_ptr<SpillFile>& spill_file); |
90 | | |
91 | | friend class SpillFile; |
92 | | |
93 | | Status _write_block(RuntimeState* state, const Block& block); |
94 | | |
95 | | /// Open the next part file (spill_dir/{_current_part_index}). |
96 | | Status _open_next_part(const std::shared_ptr<SpillFile>& spill_file); |
97 | | |
98 | | /// Close the current part: write the footer, close the file writer synchronously |
99 | | /// (draining in-flight uploads on failure), account its statistics, register |
100 | | /// the successful part with the SpillFile, and advance to the next part index. |
101 | | /// Statistics are accounted whether it succeeds or not. |
102 | | Status _close_current_part(const std::shared_ptr<SpillFile>& spill_file); |
103 | | |
104 | | /// Reset the per-part state and move on to the next part index. |
105 | | void _advance_part(); |
106 | | |
107 | | /// Called by SpillFile::gc() when the SpillFile goes away before this writer was closed |
108 | | /// (e.g. an error unwound the owner of the file first). Drops the current part without |
109 | | /// a footer and without publishing it, and unregisters from the SpillFile, so that nothing |
110 | | /// is charged to or written under a SpillFile that no longer exists. |
111 | | void _discard(SpillFile* spill_file); |
112 | | |
113 | | /// If current part size >= _max_part_size, close it. |
114 | | Status _rotate_if_needed(const std::shared_ptr<SpillFile>& spill_file); |
115 | | |
116 | | /// Serialize and write a single block to the current part. |
117 | | Status _write_internal(const Block& block, const std::shared_ptr<SpillFile>& spill_file); |
118 | | |
119 | | // ── Back-reference ── |
120 | | std::weak_ptr<SpillFile> _spill_file_wptr; // weak ref; use lock() in close() |
121 | | |
122 | | // ── Configuration ── |
123 | | SpillDataDir* _data_dir = nullptr; |
124 | | io::FileSystemSPtr _fs; |
125 | | std::string _spill_dir; |
126 | | int64_t _max_part_size; |
127 | | |
128 | | // ── Current part state (reset on rotation) ── |
129 | | size_t _current_part_index = 0; |
130 | | std::string _current_part_path; |
131 | | size_t _part_written_blocks = 0; |
132 | | size_t _part_max_sub_block_size = 0; |
133 | | std::string _part_meta; |
134 | | |
135 | | protected: |
136 | | std::unique_ptr<doris::io::FileWriter> _file_writer; |
137 | | int64_t _part_written_bytes = 0; |
138 | | Status _part_append_status; |
139 | | std::shared_ptr<ResourceContext> _resource_ctx = nullptr; |
140 | | |
141 | | private: |
142 | | bool _closed = false; |
143 | | // First failed write_block(); every later write and close() returns it. |
144 | | Status _write_status; |
145 | | size_t _max_record_bytes = 0; |
146 | | size_t _max_block_bytes = 0; |
147 | | size_t _max_footer_bytes = 0; |
148 | | |
149 | | // ── Counters ── |
150 | | RuntimeProfile::Counter* _write_file_timer = nullptr; |
151 | | RuntimeProfile::Counter* _serialize_timer = nullptr; |
152 | | RuntimeProfile::Counter* _write_block_counter = nullptr; |
153 | | RuntimeProfile::Counter* _write_block_bytes_counter = nullptr; |
154 | | RuntimeProfile::Counter* _write_file_total_size = nullptr; |
155 | | RuntimeProfile::Counter* _write_file_current_size = nullptr; |
156 | | RuntimeProfile::Counter* _write_rows_counter = nullptr; |
157 | | RuntimeProfile::Counter* _memory_used_counter = nullptr; |
158 | | RuntimeProfile::Counter* _total_file_count = nullptr; |
159 | | }; |
160 | | using SpillFileWriterSPtr = std::shared_ptr<SpillFileWriter>; |
161 | | } // namespace doris |