be/src/exec/operator/data_queue.cpp
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 | | #include "exec/operator/data_queue.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <mutex> |
24 | | #include <utility> |
25 | | |
26 | | #include "core/block/block.h" |
27 | | #include "exec/pipeline/dependency.h" |
28 | | |
29 | | namespace doris { |
30 | | #include "common/compile_check_begin.h" |
31 | | DataQueue::DataQueue(int child_count) |
32 | 5.72k | : _queue_blocks_lock(child_count), |
33 | 5.72k | _queue_blocks(child_count), |
34 | 5.72k | _free_blocks_lock(child_count), |
35 | 5.72k | _free_blocks(child_count), |
36 | 5.72k | _child_count(child_count), |
37 | 5.72k | _is_finished(child_count), |
38 | 5.72k | _is_canceled(child_count), |
39 | 5.72k | _cur_bytes_in_queue(child_count), |
40 | 5.72k | _cur_blocks_nums_in_queue(child_count), |
41 | 5.72k | _flag_queue_idx(0) { |
42 | 17.6k | for (int i = 0; i < child_count; ++i) { |
43 | 11.9k | _queue_blocks_lock[i].reset(new std::mutex()); |
44 | 11.9k | _free_blocks_lock[i].reset(new std::mutex()); |
45 | 11.9k | _is_finished[i] = false; |
46 | 11.9k | _is_canceled[i] = false; |
47 | 11.9k | _cur_bytes_in_queue[i] = 0; |
48 | 11.9k | _cur_blocks_nums_in_queue[i] = 0; |
49 | 11.9k | } |
50 | 5.72k | _un_finished_counter = child_count; |
51 | 5.72k | _sink_dependencies.resize(child_count, nullptr); |
52 | 5.72k | } |
53 | | |
54 | 11.5k | std::unique_ptr<Block> DataQueue::get_free_block(int child_idx) { |
55 | 11.5k | { |
56 | 11.5k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx])); |
57 | 11.5k | if (!_free_blocks[child_idx].empty()) { |
58 | 2 | auto block = std::move(_free_blocks[child_idx].front()); |
59 | 2 | _free_blocks[child_idx].pop_front(); |
60 | 2 | return block; |
61 | 2 | } |
62 | 11.5k | } |
63 | | |
64 | 11.5k | return Block::create_unique(); |
65 | 11.5k | } |
66 | | |
67 | 11.5k | void DataQueue::push_free_block(std::unique_ptr<Block> block, int child_idx) { |
68 | 11.5k | DCHECK(block->rows() == 0); |
69 | | |
70 | 11.5k | if (!_is_low_memory_mode) { |
71 | 11.5k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx])); |
72 | 11.5k | _free_blocks[child_idx].emplace_back(std::move(block)); |
73 | 11.5k | } |
74 | 11.5k | } |
75 | | |
76 | 5.34k | void DataQueue::clear_free_blocks() { |
77 | 16.8k | for (size_t child_idx = 0; child_idx < _free_blocks.size(); ++child_idx) { |
78 | 11.5k | std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]); |
79 | 11.5k | std::deque<std::unique_ptr<Block>> tmp_queue; |
80 | 11.5k | _free_blocks[child_idx].swap(tmp_queue); |
81 | 11.5k | } |
82 | 5.34k | } |
83 | | |
84 | 5.34k | void DataQueue::terminate() { |
85 | 16.9k | for (int i = 0; i < _queue_blocks.size(); i++) { |
86 | 11.5k | set_finish(i); |
87 | 11.5k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[i])); |
88 | 11.5k | if (_cur_blocks_nums_in_queue[i] > 0) { |
89 | 8 | _queue_blocks[i].clear(); |
90 | 8 | _cur_bytes_in_queue[i] = 0; |
91 | 8 | _cur_blocks_nums_in_queue[i] = 0; |
92 | 8 | _sink_dependencies[i]->set_always_ready(); |
93 | 8 | } |
94 | 11.5k | } |
95 | 5.34k | clear_free_blocks(); |
96 | 5.34k | } |
97 | | |
98 | | //check which queue have data, and save the idx in _flag_queue_idx, |
99 | | //so next loop, will check the record idx + 1 first |
100 | | //maybe it's useful with many queue, others maybe always 0 |
101 | 41.6k | bool DataQueue::remaining_has_data() { |
102 | 41.6k | int count = _child_count; |
103 | 114k | while (--count >= 0) { |
104 | 84.4k | _flag_queue_idx++; |
105 | 84.4k | if (_flag_queue_idx == _child_count) { |
106 | 35.7k | _flag_queue_idx = 0; |
107 | 35.7k | } |
108 | 84.4k | if (_cur_blocks_nums_in_queue[_flag_queue_idx] > 0) { |
109 | 11.7k | return true; |
110 | 11.7k | } |
111 | 84.4k | } |
112 | 29.8k | return false; |
113 | 41.6k | } |
114 | | |
115 | | //the _flag_queue_idx indicate which queue has data, and in check can_read |
116 | | //will be set idx in remaining_has_data function |
117 | 30.9k | Status DataQueue::get_block_from_queue(std::unique_ptr<Block>* output_block, int* child_idx) { |
118 | 30.9k | if (_is_canceled[_flag_queue_idx]) { |
119 | 0 | return Status::InternalError("Current queue of idx {} have beed canceled: ", |
120 | 0 | _flag_queue_idx); |
121 | 0 | } |
122 | | |
123 | 30.9k | { |
124 | 30.9k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[_flag_queue_idx])); |
125 | 30.9k | if (_cur_blocks_nums_in_queue[_flag_queue_idx] > 0) { |
126 | 11.7k | *output_block = std::move(_queue_blocks[_flag_queue_idx].front()); |
127 | 11.7k | _queue_blocks[_flag_queue_idx].pop_front(); |
128 | 11.7k | if (child_idx) { |
129 | 11.7k | *child_idx = _flag_queue_idx; |
130 | 11.7k | } |
131 | 11.7k | _cur_bytes_in_queue[_flag_queue_idx] -= (*output_block)->allocated_bytes(); |
132 | 11.7k | _cur_blocks_nums_in_queue[_flag_queue_idx] -= 1; |
133 | 11.7k | if (_cur_blocks_nums_in_queue[_flag_queue_idx] == 0) { |
134 | 11.6k | _sink_dependencies[_flag_queue_idx]->set_ready(); |
135 | 11.6k | } |
136 | 11.7k | auto old_value = _cur_blocks_total_nums.fetch_sub(1); |
137 | 11.7k | if (old_value == 1 && _source_dependency) { |
138 | 10.9k | set_source_block(); |
139 | 10.9k | } |
140 | 11.7k | } |
141 | 30.9k | } |
142 | 30.9k | return Status::OK(); |
143 | 30.9k | } |
144 | | |
145 | 11.7k | Status DataQueue::push_block(std::unique_ptr<Block> block, int child_idx) { |
146 | 11.7k | if (!block) { |
147 | 0 | return Status::OK(); |
148 | 0 | } |
149 | 11.7k | { |
150 | 11.7k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx])); |
151 | 11.7k | if (_is_finished[child_idx]) { |
152 | 0 | return Status::EndOfFile("Already finish"); |
153 | 0 | } |
154 | 11.7k | _cur_bytes_in_queue[child_idx] += block->allocated_bytes(); |
155 | 11.7k | _queue_blocks[child_idx].emplace_back(std::move(block)); |
156 | 11.7k | _cur_blocks_nums_in_queue[child_idx] += 1; |
157 | | |
158 | 11.7k | if (_cur_blocks_nums_in_queue[child_idx] > _max_blocks_in_sub_queue) { |
159 | 83 | _sink_dependencies[child_idx]->block(); |
160 | 83 | } |
161 | 11.7k | _cur_blocks_total_nums++; |
162 | | |
163 | 11.7k | set_source_ready(); |
164 | 11.7k | } |
165 | 0 | return Status::OK(); |
166 | 11.7k | } |
167 | | |
168 | 23.5k | void DataQueue::set_finish(int child_idx) { |
169 | 23.5k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx])); |
170 | 23.5k | if (_is_finished[child_idx]) { |
171 | 11.5k | return; |
172 | 11.5k | } |
173 | 11.9k | _is_finished[child_idx] = true; |
174 | 11.9k | if (_un_finished_counter.fetch_sub(1) == 1) { |
175 | 5.73k | _is_all_finished = true; |
176 | 5.73k | } |
177 | 11.9k | set_source_ready(); |
178 | 11.9k | } |
179 | | |
180 | 0 | void DataQueue::set_canceled(int child_idx) { |
181 | 0 | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx])); |
182 | 0 | DCHECK(!_is_finished[child_idx]); |
183 | 0 | _is_canceled[child_idx] = true; |
184 | 0 | _is_finished[child_idx] = true; |
185 | 0 | if (_un_finished_counter.fetch_sub(1) == 1) { |
186 | 0 | _is_all_finished = true; |
187 | 0 | } |
188 | 0 | set_source_ready(); |
189 | 0 | } |
190 | | |
191 | 3 | bool DataQueue::is_finish(int child_idx) { |
192 | 3 | return _is_finished[child_idx]; |
193 | 3 | } |
194 | | |
195 | 41.1k | bool DataQueue::is_all_finish() { |
196 | 41.1k | return _is_all_finished; |
197 | 41.1k | } |
198 | | |
199 | 23.7k | void DataQueue::set_source_ready() { |
200 | 23.7k | if (_source_dependency) { |
201 | 23.7k | std::unique_lock lc(_source_lock); |
202 | 23.7k | _source_dependency->set_ready(); |
203 | 23.7k | } |
204 | 23.7k | } |
205 | | |
206 | 10.9k | void DataQueue::set_source_block() { |
207 | 10.9k | if (_cur_blocks_total_nums == 0 && !is_all_finish()) { |
208 | 5.63k | std::unique_lock lc(_source_lock); |
209 | | // Performing the judgment twice, attempting to avoid blocking the source as much as possible. |
210 | 5.64k | if (_cur_blocks_total_nums == 0 && !is_all_finish()) { |
211 | 5.64k | _source_dependency->block(); |
212 | 5.64k | } |
213 | 5.63k | } |
214 | 10.9k | } |
215 | | |
216 | | } // namespace doris |