Coverage Report

Created: 2026-03-13 05:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/spill/spill_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_writer.h"
26
#include "runtime/runtime_profile.h"
27
#include "runtime/workload_management/resource_context.h"
28
namespace doris {
29
#include "common/compile_check_begin.h"
30
class RuntimeState;
31
32
class SpillDataDir;
33
class SpillWriter {
34
public:
35
    SpillWriter(std::shared_ptr<ResourceContext> resource_context, RuntimeProfile* profile,
36
                int64_t id, size_t batch_size, SpillDataDir* data_dir, const std::string& dir)
37
2.10k
            : data_dir_(data_dir),
38
2.10k
              stream_id_(id),
39
2.10k
              batch_size_(batch_size),
40
2.10k
              _resource_ctx(std::move(resource_context)) {
41
        // Directory path format specified in SpillStreamManager::register_spill_stream:
42
        // storage_root/spill/query_id/partitioned_hash_join-node_id-task_id-stream_id/0
43
2.10k
        file_path_ = dir + "/0";
44
2.10k
        RuntimeProfile* common_profile = profile->get_child("CommonCounters");
45
2.10k
        DCHECK(common_profile != nullptr);
46
2.10k
        _memory_used_counter = common_profile->get_counter("MemoryUsage");
47
2.10k
    }
48
49
    Status open();
50
51
    Status close();
52
53
    Status write(RuntimeState* state, const Block& block, size_t& written_bytes);
54
55
0
    int64_t get_id() const { return stream_id_; }
56
57
6.39k
    int64_t get_written_bytes() const { return total_written_bytes_; }
58
59
2.12k
    const std::string& get_file_path() const { return file_path_; }
60
61
2.10k
    void set_counters(RuntimeProfile* operator_profile) {
62
2.10k
        RuntimeProfile* custom_profile = operator_profile->get_child("CustomCounters");
63
2.10k
        _write_file_timer = custom_profile->get_counter("SpillWriteFileTime");
64
2.10k
        _serialize_timer = custom_profile->get_counter("SpillWriteSerializeBlockTime");
65
2.10k
        _write_block_counter = custom_profile->get_counter("SpillWriteBlockCount");
66
2.10k
        _write_block_bytes_counter = custom_profile->get_counter("SpillWriteBlockBytes");
67
2.10k
        _write_file_total_size = custom_profile->get_counter("SpillWriteFileBytes");
68
2.10k
        _write_file_current_size = custom_profile->get_counter("SpillWriteFileCurrentBytes");
69
2.10k
        _write_rows_counter = custom_profile->get_counter("SpillWriteRows");
70
2.10k
    }
71
72
private:
73
    Status _write_internal(const Block& block, size_t& written_bytes);
74
75
    // not owned, point to the data dir of this rowset
76
    // for checking disk capacity when write data to disk.
77
    SpillDataDir* data_dir_ = nullptr;
78
    std::atomic_bool closed_ = false;
79
    int64_t stream_id_;
80
    size_t batch_size_;
81
    size_t max_sub_block_size_ = 0;
82
    std::string file_path_;
83
    std::unique_ptr<doris::io::FileWriter> file_writer_;
84
85
    size_t written_blocks_ = 0;
86
    int64_t total_written_bytes_ = 0;
87
    std::string meta_;
88
89
    RuntimeProfile::Counter* _write_file_timer = nullptr;
90
    RuntimeProfile::Counter* _serialize_timer = nullptr;
91
    RuntimeProfile::Counter* _write_block_counter = nullptr;
92
    RuntimeProfile::Counter* _write_block_bytes_counter = nullptr;
93
    RuntimeProfile::Counter* _write_file_total_size = nullptr;
94
    RuntimeProfile::Counter* _write_file_current_size = nullptr;
95
    RuntimeProfile::Counter* _write_rows_counter = nullptr;
96
    RuntimeProfile::Counter* _memory_used_counter = nullptr;
97
98
    std::shared_ptr<ResourceContext> _resource_ctx = nullptr;
99
};
100
using SpillWriterUPtr = std::unique_ptr<SpillWriter>;
101
} // namespace doris
102
103
#include "common/compile_check_end.h"