Coverage Report

Created: 2026-07-18 03:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/operator/data_queue.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
#pragma once
18
19
#include <atomic>
20
#include <cstdint>
21
#include <deque>
22
#include <memory>
23
#include <string>
24
#include <vector>
25
26
#include "common/status.h"
27
#include "common/thread_safety_annotations.h"
28
#include "core/block/block.h"
29
30
namespace doris {
31
32
class Dependency;
33
34
struct DataQueueBlock {
35
    std::unique_ptr<Block> block;
36
    bool eos = false;
37
38
private:
39
    int child_idx = 0;
40
    friend class DataQueue;
41
};
42
43
// Per child sub-queue. Groups all parallel state so that the lock/field
44
// relationship is explicit and can be checked by clang -Wthread-safety.
45
struct SubQueue {
46
    // Protects the `blocks` deque and serializes high-level state
47
    // transitions (push/pop/finish/cancel) on this sub-queue.
48
    AnnotatedMutex queue_lock;
49
    std::deque<std::unique_ptr<Block>> blocks GUARDED_BY(queue_lock);
50
51
    // The following fields are only accessed while holding queue_lock.
52
    int64_t bytes_in_queue GUARDED_BY(queue_lock) = 0;
53
    bool is_finished GUARDED_BY(queue_lock) = false;
54
55
    // Protects the `free_blocks` deque only.
56
    AnnotatedMutex free_lock;
57
    std::deque<std::unique_ptr<Block>> free_blocks GUARDED_BY(free_lock);
58
59
    // blocks_in_queue is readable from lock-free fast paths (get_block_from_queue),
60
    // so it remains atomic and is intentionally not GUARDED_BY.
61
    std::atomic_uint32_t blocks_in_queue {0};
62
63
    // Maximum number of blocks allowed in this sub-queue before the sink is blocked.
64
    // Updated by DataQueue::set_max_blocks_in_sub_queue / set_low_memory_mode.
65
    std::atomic_int64_t max_blocks_in_queue {1};
66
67
    // Set once during init via set_sink_dependency, then read-only.
68
    Dependency* sink_dependency = nullptr;
69
70
    // Pop a block under queue_lock.
71
    // Notifies sink_dependency->set_ready() (outside the lock) if the queue becomes empty.
72
    // output_block is null if the queue was empty.
73
    void try_pop(std::unique_ptr<Block>* output_block);
74
75
    // Push a block under queue_lock and atomically increment total_counter.
76
    // Returns false (without incrementing) if already finished.
77
    // Calls sink_dependency->block() (outside the lock) if the queue exceeds max_blocks_in_queue.
78
    bool try_push(std::unique_ptr<Block> block, std::atomic_uint32_t& total_counter);
79
80
    // Mark this sub-queue finished. Returns false if already finished (idempotent).
81
    // Decrements unfinished_counter and may set all_finished within queue_lock.
82
    bool mark_finished(std::atomic_uint32_t& unfinished_counter, std::atomic_bool& all_finished);
83
84
    // Clear all pending blocks under queue_lock.
85
    // Calls sink_dependency->set_always_ready() (outside the lock) if any blocks were cleared.
86
    void clear_blocks();
87
};
88
89
class DataQueue {
90
public:
91
    //always one is enough, but in union node it's has more children
92
    DataQueue(int child_count = 1);
93
17
    ~DataQueue() = default;
94
95
    Result<DataQueueBlock> get_block_from_queue();
96
    Status push_block(std::unique_ptr<Block> block, int child_idx, bool eos);
97
98
    std::unique_ptr<Block> get_free_block(int child_idx = 0);
99
    void push_free_block(DataQueueBlock&& queue_block);
100
101
    std::string debug_string() const;
102
103
    void set_source_dependency(std::shared_ptr<Dependency> source_dependency)
104
            NO_THREAD_SAFETY_ANALYSIS;
105
    void set_sink_dependency(Dependency* sink_dependency, int child_idx);
106
    void set_max_blocks_in_sub_queue(int64_t max_blocks);
107
    void set_low_memory_mode();
108
109
    void terminate();
110
111
private:
112
    bool is_all_finish() const;
113
    bool has_more_data() const;
114
    void clear_free_blocks();
115
    void mark_finish(int child_idx);
116
    void set_source_ready();
117
    void set_source_always_ready();
118
    void set_source_block();
119
120
    std::vector<std::unique_ptr<SubQueue>> _sub_queues;
121
122
    //how many deque will be init, always will be one
123
    int _child_count = 0;
124
    std::atomic_uint32_t _un_finished_counter = 0;
125
    std::atomic_bool _is_all_finished = false;
126
    std::atomic_uint32_t _cur_blocks_total_nums = 0;
127
128
    //this will be indicate which queue has data, it's useful when have many queues
129
    int _flag_queue_idx = 0;
130
    // only used by streaming agg source operator
131
132
    std::atomic_bool _is_low_memory_mode = false;
133
134
    // _source_dependency is written once during initialization (set_source_dependency)
135
    // and read/used only while holding _source_lock thereafter.
136
    std::shared_ptr<Dependency> _source_dependency GUARDED_BY(_source_lock) = nullptr;
137
    AnnotatedMutex _source_lock;
138
};
139
140
} // namespace doris