be/src/exec/exchange/local_exchanger.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/exchange/local_exchanger.h" |
19 | | |
20 | | #include "common/cast_set.h" |
21 | | #include "common/status.h" |
22 | | #include "exec/exchange/local_exchange_sink_operator.h" |
23 | | #include "exec/exchange/local_exchange_source_operator.h" |
24 | | #include "exec/partitioner/partitioner.h" |
25 | | |
26 | | namespace doris { |
27 | | template <typename BlockType> |
28 | | void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, |
29 | | LocalExchangeSinkLocalState* local_state, |
30 | 161 | BlockType&& block) { |
31 | 161 | if (local_state == nullptr) { |
32 | 0 | _enqueue_data_and_set_ready(channel_id, std::move(block)); |
33 | 0 | return; |
34 | 0 | } |
35 | | // PartitionedBlock is used by shuffle exchanger. |
36 | | // PartitionedBlock will be push into multiple queues with different row ranges, so it will be |
37 | | // referenced multiple times. Otherwise, we only ref the block once because it is only push into |
38 | | // one queue. |
39 | 161 | std::unique_lock l(*_m[channel_id]); |
40 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || |
41 | 84 | std::is_same_v<BroadcastBlock, BlockType>) { |
42 | 84 | block.first->record_channel_id(channel_id); |
43 | 84 | } else { |
44 | 77 | block->record_channel_id(channel_id); |
45 | 77 | } |
46 | | |
47 | 161 | if (_data_queue[channel_id].enqueue(std::move(block))) { |
48 | 129 | local_state->_shared_state->set_ready_to_read(channel_id); |
49 | 129 | } |
50 | 161 | } _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_ Line | Count | Source | 30 | 20 | BlockType&& block) { | 31 | 20 | if (local_state == nullptr) { | 32 | 0 | _enqueue_data_and_set_ready(channel_id, std::move(block)); | 33 | 0 | return; | 34 | 0 | } | 35 | | // PartitionedBlock is used by shuffle exchanger. | 36 | | // PartitionedBlock will be push into multiple queues with different row ranges, so it will be | 37 | | // referenced multiple times. Otherwise, we only ref the block once because it is only push into | 38 | | // one queue. | 39 | 20 | std::unique_lock l(*_m[channel_id]); | 40 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 41 | 20 | std::is_same_v<BroadcastBlock, BlockType>) { | 42 | 20 | block.first->record_channel_id(channel_id); | 43 | | } else { | 44 | | block->record_channel_id(channel_id); | 45 | | } | 46 | | | 47 | 20 | if (_data_queue[channel_id].enqueue(std::move(block))) { | 48 | 16 | local_state->_shared_state->set_ready_to_read(channel_id); | 49 | 16 | } | 50 | 20 | } |
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_ Line | Count | Source | 30 | 77 | BlockType&& block) { | 31 | 77 | if (local_state == nullptr) { | 32 | 0 | _enqueue_data_and_set_ready(channel_id, std::move(block)); | 33 | 0 | return; | 34 | 0 | } | 35 | | // PartitionedBlock is used by shuffle exchanger. | 36 | | // PartitionedBlock will be push into multiple queues with different row ranges, so it will be | 37 | | // referenced multiple times. Otherwise, we only ref the block once because it is only push into | 38 | | // one queue. | 39 | 77 | std::unique_lock l(*_m[channel_id]); | 40 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 41 | | std::is_same_v<BroadcastBlock, BlockType>) { | 42 | | block.first->record_channel_id(channel_id); | 43 | 77 | } else { | 44 | 77 | block->record_channel_id(channel_id); | 45 | 77 | } | 46 | | | 47 | 77 | if (_data_queue[channel_id].enqueue(std::move(block))) { | 48 | 65 | local_state->_shared_state->set_ready_to_read(channel_id); | 49 | 65 | } | 50 | 77 | } |
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_ Line | Count | Source | 30 | 64 | BlockType&& block) { | 31 | 64 | if (local_state == nullptr) { | 32 | 0 | _enqueue_data_and_set_ready(channel_id, std::move(block)); | 33 | 0 | return; | 34 | 0 | } | 35 | | // PartitionedBlock is used by shuffle exchanger. | 36 | | // PartitionedBlock will be push into multiple queues with different row ranges, so it will be | 37 | | // referenced multiple times. Otherwise, we only ref the block once because it is only push into | 38 | | // one queue. | 39 | 64 | std::unique_lock l(*_m[channel_id]); | 40 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 41 | 64 | std::is_same_v<BroadcastBlock, BlockType>) { | 42 | 64 | block.first->record_channel_id(channel_id); | 43 | | } else { | 44 | | block->record_channel_id(channel_id); | 45 | | } | 46 | | | 47 | 64 | if (_data_queue[channel_id].enqueue(std::move(block))) { | 48 | 48 | local_state->_shared_state->set_ready_to_read(channel_id); | 49 | 48 | } | 50 | 64 | } |
|
51 | | |
52 | | template <typename BlockType> |
53 | | bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state, |
54 | | BlockType& block, bool* eos, Block* data_block, |
55 | 196 | int channel_id) { |
56 | 196 | if (local_state == nullptr) { |
57 | 20 | return _dequeue_data(block, eos, data_block, channel_id); |
58 | 20 | } |
59 | 176 | bool all_finished = _running_sink_operators == 0; |
60 | 176 | if (_data_queue[channel_id].try_dequeue(block)) { |
61 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || |
62 | 60 | std::is_same_v<BroadcastBlock, BlockType>) { |
63 | 60 | local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes); |
64 | 65 | } else { |
65 | 65 | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); |
66 | 65 | data_block->swap(block->_data_block); |
67 | 65 | } |
68 | 125 | return true; |
69 | 125 | } else if (all_finished) { |
70 | 17 | *eos = true; |
71 | 34 | } else { |
72 | 34 | std::unique_lock l(*_m[channel_id]); |
73 | 34 | if (_data_queue[channel_id].try_dequeue(block)) { |
74 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || |
75 | 0 | std::is_same_v<BroadcastBlock, BlockType>) { |
76 | 0 | local_state->_shared_state->sub_mem_usage(channel_id, |
77 | 0 | block.first->_allocated_bytes); |
78 | 0 | } else { |
79 | 0 | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); |
80 | 0 | data_block->swap(block->_data_block); |
81 | 0 | } |
82 | 0 | return true; |
83 | 0 | } |
84 | 34 | COUNTER_UPDATE(local_state->_get_block_failed_counter, 1); |
85 | 34 | local_state->_dependency->block(); |
86 | 34 | } |
87 | 51 | return false; |
88 | 176 | } _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi Line | Count | Source | 55 | 28 | int channel_id) { | 56 | 28 | if (local_state == nullptr) { | 57 | 4 | return _dequeue_data(block, eos, data_block, channel_id); | 58 | 4 | } | 59 | 24 | bool all_finished = _running_sink_operators == 0; | 60 | 24 | if (_data_queue[channel_id].try_dequeue(block)) { | 61 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 62 | 12 | std::is_same_v<BroadcastBlock, BlockType>) { | 63 | 12 | local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes); | 64 | | } else { | 65 | | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); | 66 | | data_block->swap(block->_data_block); | 67 | | } | 68 | 12 | return true; | 69 | 12 | } else if (all_finished) { | 70 | 4 | *eos = true; | 71 | 8 | } else { | 72 | 8 | std::unique_lock l(*_m[channel_id]); | 73 | 8 | if (_data_queue[channel_id].try_dequeue(block)) { | 74 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 75 | 0 | std::is_same_v<BroadcastBlock, BlockType>) { | 76 | 0 | local_state->_shared_state->sub_mem_usage(channel_id, | 77 | 0 | block.first->_allocated_bytes); | 78 | | } else { | 79 | | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); | 80 | | data_block->swap(block->_data_block); | 81 | | } | 82 | 0 | return true; | 83 | 0 | } | 84 | 8 | COUNTER_UPDATE(local_state->_get_block_failed_counter, 1); | 85 | 8 | local_state->_dependency->block(); | 86 | 8 | } | 87 | 12 | return false; | 88 | 24 | } |
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi Line | Count | Source | 55 | 104 | int channel_id) { | 56 | 104 | if (local_state == nullptr) { | 57 | 12 | return _dequeue_data(block, eos, data_block, channel_id); | 58 | 12 | } | 59 | 92 | bool all_finished = _running_sink_operators == 0; | 60 | 92 | if (_data_queue[channel_id].try_dequeue(block)) { | 61 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 62 | | std::is_same_v<BroadcastBlock, BlockType>) { | 63 | | local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes); | 64 | 65 | } else { | 65 | 65 | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); | 66 | 65 | data_block->swap(block->_data_block); | 67 | 65 | } | 68 | 65 | return true; | 69 | 65 | } else if (all_finished) { | 70 | 9 | *eos = true; | 71 | 18 | } else { | 72 | 18 | std::unique_lock l(*_m[channel_id]); | 73 | 18 | if (_data_queue[channel_id].try_dequeue(block)) { | 74 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 75 | | std::is_same_v<BroadcastBlock, BlockType>) { | 76 | | local_state->_shared_state->sub_mem_usage(channel_id, | 77 | | block.first->_allocated_bytes); | 78 | 0 | } else { | 79 | 0 | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); | 80 | 0 | data_block->swap(block->_data_block); | 81 | 0 | } | 82 | 0 | return true; | 83 | 0 | } | 84 | 18 | COUNTER_UPDATE(local_state->_get_block_failed_counter, 1); | 85 | 18 | local_state->_dependency->block(); | 86 | 18 | } | 87 | 27 | return false; | 88 | 92 | } |
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi Line | Count | Source | 55 | 64 | int channel_id) { | 56 | 64 | if (local_state == nullptr) { | 57 | 4 | return _dequeue_data(block, eos, data_block, channel_id); | 58 | 4 | } | 59 | 60 | bool all_finished = _running_sink_operators == 0; | 60 | 60 | if (_data_queue[channel_id].try_dequeue(block)) { | 61 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 62 | 48 | std::is_same_v<BroadcastBlock, BlockType>) { | 63 | 48 | local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes); | 64 | | } else { | 65 | | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); | 66 | | data_block->swap(block->_data_block); | 67 | | } | 68 | 48 | return true; | 69 | 48 | } else if (all_finished) { | 70 | 4 | *eos = true; | 71 | 8 | } else { | 72 | 8 | std::unique_lock l(*_m[channel_id]); | 73 | 8 | if (_data_queue[channel_id].try_dequeue(block)) { | 74 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || | 75 | 0 | std::is_same_v<BroadcastBlock, BlockType>) { | 76 | 0 | local_state->_shared_state->sub_mem_usage(channel_id, | 77 | 0 | block.first->_allocated_bytes); | 78 | | } else { | 79 | | local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes); | 80 | | data_block->swap(block->_data_block); | 81 | | } | 82 | 0 | return true; | 83 | 0 | } | 84 | 8 | COUNTER_UPDATE(local_state->_get_block_failed_counter, 1); | 85 | 8 | local_state->_dependency->block(); | 86 | 8 | } | 87 | 12 | return false; | 88 | 60 | } |
|
89 | | |
90 | | template <typename BlockType> |
91 | 0 | void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) { |
92 | | if constexpr (std::is_same_v<PartitionedBlock, BlockType> || |
93 | 0 | std::is_same_v<BroadcastBlock, BlockType>) { |
94 | 0 | block.first->record_channel_id(channel_id); |
95 | 0 | } else { |
96 | 0 | block->record_channel_id(channel_id); |
97 | 0 | } |
98 | 0 | _data_queue[channel_id].enqueue(std::move(block)); |
99 | 0 | } Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiOS7_ Unexecuted instantiation: _ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiOS4_ Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiOS7_ |
100 | | |
101 | | template <typename BlockType> |
102 | | bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block, |
103 | 20 | int channel_id) { |
104 | 20 | if (_data_queue[channel_id].try_dequeue(block)) { |
105 | | if constexpr (!std::is_same_v<PartitionedBlock, BlockType> && |
106 | 0 | !std::is_same_v<BroadcastBlock, BlockType>) { |
107 | 0 | data_block->swap(block->_data_block); |
108 | 0 | } |
109 | 0 | return true; |
110 | 0 | } |
111 | 20 | return false; |
112 | 20 | } _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi Line | Count | Source | 103 | 4 | int channel_id) { | 104 | 4 | if (_data_queue[channel_id].try_dequeue(block)) { | 105 | | if constexpr (!std::is_same_v<PartitionedBlock, BlockType> && | 106 | | !std::is_same_v<BroadcastBlock, BlockType>) { | 107 | | data_block->swap(block->_data_block); | 108 | | } | 109 | 0 | return true; | 110 | 0 | } | 111 | 4 | return false; | 112 | 4 | } |
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi Line | Count | Source | 103 | 12 | int channel_id) { | 104 | 12 | if (_data_queue[channel_id].try_dequeue(block)) { | 105 | | if constexpr (!std::is_same_v<PartitionedBlock, BlockType> && | 106 | 0 | !std::is_same_v<BroadcastBlock, BlockType>) { | 107 | 0 | data_block->swap(block->_data_block); | 108 | 0 | } | 109 | 0 | return true; | 110 | 0 | } | 111 | 12 | return false; | 112 | 12 | } |
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi Line | Count | Source | 103 | 4 | int channel_id) { | 104 | 4 | if (_data_queue[channel_id].try_dequeue(block)) { | 105 | | if constexpr (!std::is_same_v<PartitionedBlock, BlockType> && | 106 | | !std::is_same_v<BroadcastBlock, BlockType>) { | 107 | | data_block->swap(block->_data_block); | 108 | | } | 109 | 0 | return true; | 110 | 0 | } | 111 | 4 | return false; | 112 | 4 | } |
|
113 | | |
114 | | Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile, |
115 | 21 | SinkInfo& sink_info) { |
116 | 21 | if (in_block->empty()) { |
117 | 0 | return Status::OK(); |
118 | 0 | } |
119 | 21 | { |
120 | 21 | SCOPED_TIMER(profile.compute_hash_value_timer); |
121 | 21 | RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block)); |
122 | 21 | } |
123 | 21 | { |
124 | 21 | SCOPED_TIMER(profile.distribute_timer); |
125 | 21 | RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block, |
126 | 21 | *sink_info.channel_id, sink_info.local_state, |
127 | 21 | sink_info.shuffle_idx_to_instance_idx)); |
128 | 21 | } |
129 | | |
130 | 20 | sink_info.local_state->_memory_used_counter->set( |
131 | 20 | sink_info.local_state->_shared_state->mem_usage); |
132 | 20 | return Status::OK(); |
133 | 21 | } |
134 | | |
135 | 4 | void ShuffleExchanger::close(SourceInfo&& source_info) { |
136 | 4 | PartitionedBlock partitioned_block; |
137 | 4 | bool eos; |
138 | 4 | Block block; |
139 | 4 | _data_queue[source_info.channel_id].set_eos(); |
140 | 4 | while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block, |
141 | 4 | source_info.channel_id)) { |
142 | | // do nothing |
143 | 0 | } |
144 | 4 | } |
145 | | |
146 | | Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile, |
147 | 12 | SourceInfo&& source_info) { |
148 | 12 | PartitionedBlock partitioned_block; |
149 | 12 | MutableBlock mutable_block; |
150 | | |
151 | 12 | auto get_data = [&]() -> Status { |
152 | 12 | do { |
153 | 12 | const auto* offset_start = partitioned_block.second.row_idxs->data() + |
154 | 12 | partitioned_block.second.offset_start; |
155 | 12 | auto block_wrapper = partitioned_block.first; |
156 | 12 | RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start, |
157 | 12 | offset_start + partitioned_block.second.length)); |
158 | 12 | } while (mutable_block.rows() < state->batch_size() && !*eos && |
159 | 12 | _dequeue_data(source_info.local_state, partitioned_block, eos, block, |
160 | 12 | source_info.channel_id)); |
161 | 8 | return Status::OK(); |
162 | 8 | }; |
163 | | |
164 | 12 | if (_dequeue_data(source_info.local_state, partitioned_block, eos, block, |
165 | 12 | source_info.channel_id)) { |
166 | 8 | SCOPED_TIMER(profile.copy_data_timer); |
167 | 8 | mutable_block = VectorizedUtils::build_mutable_mem_reuse_block( |
168 | 8 | block, partitioned_block.first->_data_block); |
169 | 8 | RETURN_IF_ERROR(get_data()); |
170 | 8 | } |
171 | 12 | return Status::OK(); |
172 | 12 | } |
173 | | |
174 | | Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids, |
175 | | Block* block, int channel_id, |
176 | | LocalExchangeSinkLocalState* local_state, |
177 | 21 | std::map<int, int>* shuffle_idx_to_instance_idx) { |
178 | 21 | if (local_state == nullptr) { |
179 | 0 | return _split_rows(state, channel_ids, block, channel_id); |
180 | 0 | } |
181 | 21 | const auto rows = cast_set<int32_t>(block->rows()); |
182 | 21 | auto row_idx = std::make_shared<PODArray<uint32_t>>(rows); |
183 | 21 | auto& partition_rows_histogram = _partition_rows_histogram[channel_id]; |
184 | 21 | { |
185 | 21 | partition_rows_histogram.assign(_num_partitions + 1, 0); |
186 | 231 | for (int32_t i = 0; i < rows; ++i) { |
187 | 210 | partition_rows_histogram[channel_ids[i]]++; |
188 | 210 | } |
189 | 105 | for (int32_t i = 1; i <= _num_partitions; ++i) { |
190 | 84 | partition_rows_histogram[i] += partition_rows_histogram[i - 1]; |
191 | 84 | } |
192 | 231 | for (int32_t i = rows - 1; i >= 0; --i) { |
193 | 210 | (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i; |
194 | 210 | partition_rows_histogram[channel_ids[i]]--; |
195 | 210 | } |
196 | 21 | } |
197 | | |
198 | 21 | Block data_block; |
199 | 21 | std::shared_ptr<BlockWrapper> new_block_wrapper; |
200 | 21 | if (!_free_blocks.try_dequeue(data_block)) { |
201 | 14 | data_block = block->clone_empty(); |
202 | 14 | } |
203 | 21 | data_block.swap(*block); |
204 | 21 | new_block_wrapper = |
205 | 21 | BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1); |
206 | 21 | if (new_block_wrapper->_data_block.empty()) { |
207 | 0 | return Status::OK(); |
208 | 0 | } |
209 | | /** |
210 | | * Data are hash-shuffled and distributed to all instances of |
211 | | * all BEs. So we need a shuffleId-To-InstanceId mapping. |
212 | | * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on |
213 | | * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3. |
214 | | */ |
215 | 21 | DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0); |
216 | 21 | const auto& map = *shuffle_idx_to_instance_idx; |
217 | 21 | int32_t enqueue_rows = 0; |
218 | 83 | for (const auto& it : map) { |
219 | 83 | DCHECK(it.second >= 0 && it.second < _num_partitions) |
220 | 0 | << it.first << " : " << it.second << " " << _num_partitions; |
221 | 83 | uint32_t start = partition_rows_histogram[it.first]; |
222 | 83 | uint32_t size = partition_rows_histogram[it.first + 1] - start; |
223 | 83 | if (size > 0) { |
224 | 20 | enqueue_rows += size; |
225 | 20 | _enqueue_data_and_set_ready( |
226 | 20 | it.second, local_state, |
227 | 20 | {new_block_wrapper, |
228 | 20 | {.row_idxs = row_idx, .offset_start = start, .length = size}}); |
229 | 20 | } |
230 | 83 | } |
231 | 21 | if (enqueue_rows != rows) [[unlikely]] { |
232 | 1 | fmt::memory_buffer debug_string_buffer; |
233 | 1 | fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ", |
234 | 1 | get_exchange_type_name(get_type()), local_state->parent()->node_id()); |
235 | 3 | for (const auto& it : map) { |
236 | 3 | fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second); |
237 | 3 | } |
238 | 1 | return Status::InternalError( |
239 | 1 | "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue " |
240 | 1 | "rows={}, Detail: {}]", |
241 | 1 | rows, enqueue_rows, fmt::to_string(debug_string_buffer)); |
242 | 1 | } |
243 | | |
244 | 20 | return Status::OK(); |
245 | 21 | } |
246 | | |
247 | | Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids, |
248 | 0 | Block* block, int channel_id) { |
249 | 0 | const auto rows = cast_set<int32_t>(block->rows()); |
250 | 0 | auto row_idx = std::make_shared<PODArray<uint32_t>>(rows); |
251 | 0 | auto& partition_rows_histogram = _partition_rows_histogram[channel_id]; |
252 | 0 | { |
253 | 0 | partition_rows_histogram.assign(_num_partitions + 1, 0); |
254 | 0 | for (int32_t i = 0; i < rows; ++i) { |
255 | 0 | partition_rows_histogram[channel_ids[i]]++; |
256 | 0 | } |
257 | 0 | for (int32_t i = 1; i <= _num_partitions; ++i) { |
258 | 0 | partition_rows_histogram[i] += partition_rows_histogram[i - 1]; |
259 | 0 | } |
260 | 0 | for (int32_t i = rows - 1; i >= 0; --i) { |
261 | 0 | (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i; |
262 | 0 | partition_rows_histogram[channel_ids[i]]--; |
263 | 0 | } |
264 | 0 | } |
265 | |
|
266 | 0 | Block data_block; |
267 | 0 | std::shared_ptr<BlockWrapper> new_block_wrapper; |
268 | 0 | if (!_free_blocks.try_dequeue(data_block)) { |
269 | 0 | data_block = block->clone_empty(); |
270 | 0 | } |
271 | 0 | data_block.swap(*block); |
272 | 0 | new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1); |
273 | 0 | if (new_block_wrapper->_data_block.empty()) { |
274 | 0 | return Status::OK(); |
275 | 0 | } |
276 | 0 | for (int i = 0; i < _num_partitions; i++) { |
277 | 0 | uint32_t start = partition_rows_histogram[i]; |
278 | 0 | uint32_t size = partition_rows_histogram[i + 1] - start; |
279 | 0 | if (size > 0) { |
280 | 0 | _enqueue_data_and_set_ready( |
281 | 0 | i, {new_block_wrapper, |
282 | 0 | {.row_idxs = row_idx, .offset_start = start, .length = size}}); |
283 | 0 | } |
284 | 0 | } |
285 | |
|
286 | 0 | return Status::OK(); |
287 | 0 | } |
288 | | |
289 | | Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile, |
290 | 28 | SinkInfo& sink_info) { |
291 | 28 | if (in_block->empty()) { |
292 | 0 | return Status::OK(); |
293 | 0 | } |
294 | 28 | Block new_block; |
295 | 28 | if (!_free_blocks.try_dequeue(new_block)) { |
296 | 21 | new_block = {in_block->clone_empty()}; |
297 | 21 | } |
298 | 28 | new_block.swap(*in_block); |
299 | 28 | auto channel_id = ((*sink_info.channel_id)++) % _num_partitions; |
300 | 28 | BlockWrapperSPtr wrapper = BlockWrapper::create_shared( |
301 | 28 | std::move(new_block), |
302 | 28 | sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id); |
303 | | |
304 | 28 | _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper)); |
305 | | |
306 | 28 | sink_info.local_state->_memory_used_counter->set( |
307 | 28 | sink_info.local_state->_shared_state->mem_usage); |
308 | | |
309 | 28 | return Status::OK(); |
310 | 28 | } |
311 | | |
312 | 4 | void PassthroughExchanger::close(SourceInfo&& source_info) { |
313 | 4 | Block next_block; |
314 | 4 | BlockWrapperSPtr wrapper; |
315 | 4 | bool eos; |
316 | 4 | _data_queue[source_info.channel_id].set_eos(); |
317 | 4 | while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block, |
318 | 4 | source_info.channel_id)) { |
319 | | // do nothing |
320 | 0 | } |
321 | 4 | } |
322 | | |
323 | 4 | void PassToOneExchanger::close(SourceInfo&& source_info) { |
324 | 4 | Block next_block; |
325 | 4 | BlockWrapperSPtr wrapper; |
326 | 4 | bool eos; |
327 | 4 | _data_queue[source_info.channel_id].set_eos(); |
328 | 4 | while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block, |
329 | 4 | source_info.channel_id)) { |
330 | | // do nothing |
331 | 0 | } |
332 | 4 | } |
333 | | |
334 | | Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos, |
335 | 36 | Profile&& profile, SourceInfo&& source_info) { |
336 | 36 | BlockWrapperSPtr next_block; |
337 | 36 | _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id); |
338 | 36 | return Status::OK(); |
339 | 36 | } |
340 | | |
341 | | Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile, |
342 | 13 | SinkInfo& sink_info) { |
343 | 13 | if (in_block->empty()) { |
344 | 0 | return Status::OK(); |
345 | 0 | } |
346 | 13 | Block new_block; |
347 | 13 | if (!_free_blocks.try_dequeue(new_block)) { |
348 | 9 | new_block = {in_block->clone_empty()}; |
349 | 9 | } |
350 | 13 | new_block.swap(*in_block); |
351 | | |
352 | 13 | BlockWrapperSPtr wrapper = BlockWrapper::create_shared( |
353 | 13 | std::move(new_block), |
354 | 13 | sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0); |
355 | 13 | _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper)); |
356 | | |
357 | 13 | sink_info.local_state->_memory_used_counter->set( |
358 | 13 | sink_info.local_state->_shared_state->mem_usage); |
359 | | |
360 | 13 | return Status::OK(); |
361 | 13 | } |
362 | | |
363 | | Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos, |
364 | 15 | Profile&& profile, SourceInfo&& source_info) { |
365 | 15 | if (source_info.channel_id != 0) { |
366 | 3 | *eos = true; |
367 | 3 | return Status::OK(); |
368 | 3 | } |
369 | 12 | BlockWrapperSPtr next_block; |
370 | 12 | _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id); |
371 | 12 | return Status::OK(); |
372 | 15 | } |
373 | | |
374 | 5 | void ExchangerBase::finalize() { |
375 | 5 | DCHECK(_running_source_operators == 0); |
376 | 5 | Block block; |
377 | 61 | while (_free_blocks.try_dequeue(block)) { |
378 | | // do nothing |
379 | 56 | } |
380 | 5 | } |
381 | | |
382 | | Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile, |
383 | 16 | SinkInfo& sink_info) { |
384 | 16 | if (in_block->empty()) { |
385 | 0 | return Status::OK(); |
386 | 0 | } |
387 | 16 | Block new_block; |
388 | 16 | if (!_free_blocks.try_dequeue(new_block)) { |
389 | 9 | new_block = {in_block->clone_empty()}; |
390 | 9 | } |
391 | 16 | new_block.swap(*in_block); |
392 | 16 | auto wrapper = BlockWrapper::create_shared( |
393 | 16 | std::move(new_block), |
394 | 16 | sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1); |
395 | 80 | for (int i = 0; i < _num_partitions; i++) { |
396 | 64 | _enqueue_data_and_set_ready( |
397 | 64 | i, sink_info.local_state, |
398 | 64 | {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}}); |
399 | 64 | } |
400 | | |
401 | 16 | return Status::OK(); |
402 | 16 | } |
403 | | |
404 | 4 | void BroadcastExchanger::close(SourceInfo&& source_info) { |
405 | 4 | BroadcastBlock partitioned_block; |
406 | 4 | bool eos; |
407 | 4 | Block block; |
408 | 4 | _data_queue[source_info.channel_id].set_eos(); |
409 | 4 | while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block, |
410 | 4 | source_info.channel_id)) { |
411 | | // do nothing |
412 | 0 | } |
413 | 4 | } |
414 | | |
415 | | Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos, |
416 | 60 | Profile&& profile, SourceInfo&& source_info) { |
417 | 60 | BroadcastBlock partitioned_block; |
418 | | |
419 | 60 | if (_dequeue_data(source_info.local_state, partitioned_block, eos, block, |
420 | 60 | source_info.channel_id)) { |
421 | 48 | SCOPED_TIMER(profile.copy_data_timer); |
422 | 48 | MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block( |
423 | 48 | block, partitioned_block.first->_data_block); |
424 | 48 | auto block_wrapper = partitioned_block.first; |
425 | 48 | RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, |
426 | 48 | partitioned_block.second.offset_start, |
427 | 48 | partitioned_block.second.length)); |
428 | 48 | } |
429 | | |
430 | 60 | return Status::OK(); |
431 | 60 | } |
432 | | |
433 | | Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block, |
434 | 20 | SinkInfo& sink_info) { |
435 | 20 | Block new_block; |
436 | 20 | if (!_free_blocks.try_dequeue(new_block)) { |
437 | 13 | new_block = {in_block->clone_empty()}; |
438 | 13 | } |
439 | 20 | new_block.swap(*in_block); |
440 | 20 | auto channel_id = ((*sink_info.channel_id)++) % _num_partitions; |
441 | 20 | _enqueue_data_and_set_ready( |
442 | 20 | channel_id, sink_info.local_state, |
443 | 20 | BlockWrapper::create_shared( |
444 | 20 | std::move(new_block), |
445 | 20 | sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, |
446 | 20 | channel_id)); |
447 | | |
448 | 20 | sink_info.local_state->_memory_used_counter->set( |
449 | 20 | sink_info.local_state->_shared_state->mem_usage); |
450 | 20 | return Status::OK(); |
451 | 20 | } |
452 | | |
453 | | Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block, |
454 | 4 | SinkInfo& sink_info) { |
455 | 4 | std::vector<uint32_t> channel_ids; |
456 | 4 | const auto num_rows = block->rows(); |
457 | 4 | channel_ids.resize(num_rows, 0); |
458 | 4 | if (num_rows <= _num_partitions) { |
459 | 0 | std::iota(channel_ids.begin(), channel_ids.end(), 0); |
460 | 4 | } else { |
461 | 4 | size_t i = 0; |
462 | 12 | for (; i < num_rows - _num_partitions; i += _num_partitions) { |
463 | 8 | std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0); |
464 | 8 | } |
465 | 4 | if (i < num_rows - 1) { |
466 | 4 | std::iota(channel_ids.begin() + i, channel_ids.end(), 0); |
467 | 4 | } |
468 | 4 | } |
469 | | |
470 | 4 | sink_info.local_state->_memory_used_counter->set( |
471 | 4 | sink_info.local_state->_shared_state->mem_usage); |
472 | 4 | RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info)); |
473 | 4 | return Status::OK(); |
474 | 4 | } |
475 | | |
476 | | Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state, |
477 | | const std::vector<uint32_t>& channel_ids, |
478 | 4 | Block* block, SinkInfo& sink_info) { |
479 | 4 | const auto rows = cast_set<int32_t>(block->rows()); |
480 | 4 | auto row_idx = std::make_shared<std::vector<uint32_t>>(rows); |
481 | 4 | auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id]; |
482 | 4 | { |
483 | 4 | partition_rows_histogram.assign(_num_partitions + 1, 0); |
484 | 52 | for (int32_t i = 0; i < rows; ++i) { |
485 | 48 | partition_rows_histogram[channel_ids[i]]++; |
486 | 48 | } |
487 | 20 | for (int32_t i = 1; i <= _num_partitions; ++i) { |
488 | 16 | partition_rows_histogram[i] += partition_rows_histogram[i - 1]; |
489 | 16 | } |
490 | | |
491 | 52 | for (int32_t i = rows - 1; i >= 0; --i) { |
492 | 48 | (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i; |
493 | 48 | partition_rows_histogram[channel_ids[i]]--; |
494 | 48 | } |
495 | 4 | } |
496 | 20 | for (int32_t i = 0; i < _num_partitions; i++) { |
497 | 16 | const size_t start = partition_rows_histogram[i]; |
498 | 16 | const size_t size = partition_rows_histogram[i + 1] - start; |
499 | 16 | if (size > 0) { |
500 | 16 | std::unique_ptr<MutableBlock> mutable_block = |
501 | 16 | MutableBlock::create_unique(block->clone_empty()); |
502 | 16 | RETURN_IF_ERROR(mutable_block->add_rows(block, start, size)); |
503 | 16 | auto new_block = mutable_block->to_block(); |
504 | | |
505 | 16 | _enqueue_data_and_set_ready( |
506 | 16 | i, sink_info.local_state, |
507 | 16 | BlockWrapper::create_shared( |
508 | 16 | std::move(new_block), |
509 | 16 | sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, |
510 | 16 | i)); |
511 | 16 | } |
512 | 16 | } |
513 | 4 | return Status::OK(); |
514 | 4 | } |
515 | | |
516 | | Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, |
517 | 24 | Profile&& profile, SinkInfo& sink_info) { |
518 | 24 | if (in_block->empty()) { |
519 | 0 | return Status::OK(); |
520 | 0 | } |
521 | 24 | if (_is_pass_through) { |
522 | 20 | return _passthrough_sink(state, in_block, sink_info); |
523 | 20 | } else { |
524 | 4 | if (++_total_block >= _num_partitions) { |
525 | 1 | _is_pass_through = true; |
526 | 1 | } |
527 | 4 | return _shuffle_sink(state, in_block, sink_info); |
528 | 4 | } |
529 | 24 | } |
530 | | |
531 | | Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos, |
532 | 44 | Profile&& profile, SourceInfo&& source_info) { |
533 | 44 | BlockWrapperSPtr next_block; |
534 | 44 | _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id); |
535 | 44 | return Status::OK(); |
536 | 44 | } |
537 | | |
538 | 4 | void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) { |
539 | 4 | Block next_block; |
540 | 4 | bool eos; |
541 | 4 | BlockWrapperSPtr wrapper; |
542 | 4 | _data_queue[source_info.channel_id].set_eos(); |
543 | 4 | while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block, |
544 | 4 | source_info.channel_id)) { |
545 | | // do nothing |
546 | 0 | } |
547 | 4 | } |
548 | | |
549 | | } // namespace doris |