be/src/exec/scan/scanner.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 <stdint.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <atomic> |
24 | | #include <vector> |
25 | | |
26 | | #include "common/status.h" |
27 | | #include "core/block/block.h" |
28 | | #include "runtime/exec_env.h" |
29 | | #include "runtime/runtime_state.h" |
30 | | #include "storage/tablet/tablet.h" |
31 | | #include "util/stopwatch.hpp" |
32 | | |
33 | | namespace doris { |
34 | | class RuntimeProfile; |
35 | | class TupleDescriptor; |
36 | | |
37 | | class VExprContext; |
38 | | |
39 | | class ScanLocalStateBase; |
40 | | } // namespace doris |
41 | | |
42 | | namespace doris { |
43 | | |
44 | | // Counter for load |
45 | | struct ScannerCounter { |
46 | 1.32M | ScannerCounter() : num_rows_filtered(0), num_rows_unselected(0) {} |
47 | | |
48 | | int64_t num_rows_filtered; // unqualified rows (unmatched the dest schema, or no partition) |
49 | | int64_t num_rows_unselected; // rows filtered by predicates |
50 | | }; |
51 | | |
52 | | class Scanner { |
53 | | public: |
54 | | Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit, |
55 | | RuntimeProfile* profile); |
56 | | |
57 | | //only used for FileScanner read one line. |
58 | | Scanner(RuntimeState* state, RuntimeProfile* profile) |
59 | 3.16k | : _state(state), _limit(1), _profile(profile), _total_rf_num(0), _has_prepared(false) { |
60 | 3.16k | DorisMetrics::instance()->scanner_cnt->increment(1); |
61 | 3.16k | }; |
62 | | |
63 | 1.32M | virtual ~Scanner() { |
64 | 1.32M | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_state->query_mem_tracker()); |
65 | 1.32M | _input_block.clear(); |
66 | 1.32M | _conjuncts.clear(); |
67 | 1.32M | _projections.clear(); |
68 | 1.32M | _origin_block.clear(); |
69 | 1.32M | _common_expr_ctxs_push_down.clear(); |
70 | 1.32M | DorisMetrics::instance()->scanner_cnt->increment(-1); |
71 | 1.32M | } |
72 | | |
73 | | virtual Status init(RuntimeState* state, const VExprContextSPtrs& conjuncts); |
74 | 1.31M | Status prepare() { |
75 | 1.31M | SCOPED_RAW_TIMER(&_per_scanner_timer); |
76 | 1.31M | SCOPED_RAW_TIMER(&_per_scanner_prepare_timer); |
77 | 1.31M | return _prepare_impl(); |
78 | 1.31M | } |
79 | | |
80 | 1.31M | Status open(RuntimeState* state) { |
81 | 1.31M | SCOPED_RAW_TIMER(&_per_scanner_timer); |
82 | 1.31M | SCOPED_RAW_TIMER(&_per_scanner_open_timer); |
83 | 1.31M | return _open_impl(state); |
84 | 1.31M | } |
85 | | |
86 | | Status get_block(RuntimeState* state, Block* block, bool* eos); |
87 | | Status get_block_after_projects(RuntimeState* state, Block* block, bool* eos); |
88 | | |
89 | | virtual Status close(RuntimeState* state); |
90 | | |
91 | | // Try to stop scanner, and all running readers. |
92 | 1.32M | virtual void try_stop() { _should_stop = true; }; |
93 | | |
94 | 0 | virtual std::string get_name() { return ""; } |
95 | | |
96 | | // return the readable name of current scan range. |
97 | | // eg, for file scanner, return the current file path. |
98 | 0 | virtual std::string get_current_scan_range_name() { return "not implemented"; } |
99 | | |
100 | | protected: |
101 | 93.2k | virtual Status _prepare_impl() { |
102 | 93.2k | _has_prepared = true; |
103 | 93.2k | return Status::OK(); |
104 | 93.2k | } |
105 | | |
106 | 1.31M | virtual Status _open_impl(RuntimeState* state) { |
107 | 1.31M | _block_avg_bytes = state->batch_size() * 8; |
108 | 1.31M | return Status::OK(); |
109 | 1.31M | } |
110 | | |
111 | | // Subclass should implement this to return data. |
112 | | virtual Status _get_block_impl(RuntimeState* state, Block* block, bool* eof) = 0; |
113 | | |
114 | 1.49M | Status _merge_padding_block() { |
115 | 1.49M | if (_padding_block.empty()) { |
116 | 1.21M | _padding_block.swap(_origin_block); |
117 | 1.21M | } else if (_origin_block.rows()) { |
118 | 64.1k | RETURN_IF_ERROR( |
119 | 64.1k | MutableBlock::build_mutable_block(&_padding_block).merge(_origin_block)); |
120 | 64.1k | } |
121 | 1.49M | return Status::OK(); |
122 | 1.49M | } |
123 | | |
124 | | // Update the counters before closing this scanner |
125 | | virtual void _collect_profile_before_close(); |
126 | | |
127 | | // Check if scanner is already closed, if not, mark it as closed. |
128 | | // Returns true if the scanner was successfully marked as closed (first time). |
129 | | // Returns false if the scanner was already closed. |
130 | | bool _try_close(); |
131 | | |
132 | | // Filter the output block finally. |
133 | | Status _filter_output_block(Block* block); |
134 | | |
135 | | Status _do_projections(Block* origin_block, Block* output_block); |
136 | | |
137 | | private: |
138 | 1.42M | void _start_scan_cpu_timer() { |
139 | 1.42M | _cpu_watch.reset(); |
140 | 1.42M | _cpu_watch.start(); |
141 | 1.42M | } |
142 | | |
143 | 1.42M | void _update_wait_worker_timer() { _scanner_wait_worker_timer += _watch.elapsed_time(); } |
144 | | void _update_scan_cpu_timer(); |
145 | | |
146 | | public: |
147 | | // Call start_wait_worker_timer() when submit the scanner to the thread pool. |
148 | | // And call update_wait_worker_timer() when it is actually being executed. |
149 | 2.84M | void start_wait_worker_timer() { |
150 | 2.84M | _watch.reset(); |
151 | 2.84M | _watch.start(); |
152 | 2.84M | } |
153 | | |
154 | 1.42M | void resume() { |
155 | 1.42M | _update_wait_worker_timer(); |
156 | 1.42M | _start_scan_cpu_timer(); |
157 | 1.42M | } |
158 | 1.41M | void pause() { |
159 | 1.41M | _update_scan_cpu_timer(); |
160 | 1.41M | start_wait_worker_timer(); |
161 | 1.41M | } |
162 | 3.52k | int64_t get_time_cost_ns() const { return _per_scanner_timer; } |
163 | 3.52k | int64_t get_prepare_time_cost_ns() const { return _per_scanner_prepare_timer; } |
164 | 3.52k | int64_t get_open_time_cost_ns() const { return _per_scanner_open_timer; } |
165 | | |
166 | 3.52k | int64_t projection_time() const { return _projection_timer; } |
167 | 3.52k | int64_t get_rows_read() const { return _num_rows_read; } |
168 | | |
169 | 1.42M | bool has_prepared() const { return _has_prepared; } |
170 | | |
171 | | Status try_append_late_arrival_runtime_filter(); |
172 | | |
173 | 3.52k | int64_t get_scanner_wait_worker_timer() const { return _scanner_wait_worker_timer; } |
174 | | |
175 | | // Some counters need to be updated realtime, for example, workload group policy need |
176 | | // scan bytes to cancel the query exceed limit. |
177 | 6.18k | virtual void update_realtime_counters() {} |
178 | | |
179 | 2.65M | RuntimeState* runtime_state() { return _state; } |
180 | | |
181 | 1.42M | bool is_open() const { return _is_open; } |
182 | 1.31M | void set_opened() { _is_open = true; } |
183 | | |
184 | 147k | virtual doris::TabletStorageType get_storage_type() { |
185 | 147k | return doris::TabletStorageType::STORAGE_TYPE_REMOTE; |
186 | 147k | } |
187 | | |
188 | 0 | bool need_to_close() const { return _need_to_close; } |
189 | | |
190 | 1.31M | void mark_to_need_to_close() { |
191 | | // If the scanner is failed during init or open, then not need update counters |
192 | | // because the query is fail and the counter is useless. And it may core during |
193 | | // update counters. For example, update counters depend on scanner's tablet, but |
194 | | // the tablet == null when init failed. |
195 | 1.31M | if (_is_open) { |
196 | 1.31M | _collect_profile_before_close(); |
197 | 1.31M | } |
198 | 1.31M | _need_to_close = true; |
199 | 1.31M | } |
200 | | |
201 | 0 | void set_status_on_failure(const Status& st) { _status = st; } |
202 | | |
203 | 1.42M | int64_t limit() const { return _limit; } |
204 | | |
205 | 0 | auto get_block_avg_bytes() const { return _block_avg_bytes; } |
206 | | |
207 | 311k | void update_block_avg_bytes(size_t block_avg_bytes) { _block_avg_bytes = block_avg_bytes; } |
208 | | |
209 | | protected: |
210 | | RuntimeState* _state = nullptr; |
211 | | ScanLocalStateBase* _local_state = nullptr; |
212 | | |
213 | | // Set if scan node has sort limit info |
214 | | int64_t _limit = -1; |
215 | | |
216 | | RuntimeProfile* _profile = nullptr; |
217 | | |
218 | | const TupleDescriptor* _output_tuple_desc = nullptr; |
219 | | const RowDescriptor* _output_row_descriptor = nullptr; |
220 | | |
221 | | // If _input_tuple_desc is set, the scanner will read data into |
222 | | // this _input_block first, then convert to the output block. |
223 | | Block _input_block; |
224 | | |
225 | | bool _is_open = false; |
226 | | std::atomic<bool> _is_closed {false}; |
227 | | bool _need_to_close = false; |
228 | | Status _status; |
229 | | |
230 | | // If _applied_rf_num == _total_rf_num |
231 | | // means all runtime filters are arrived and applied. |
232 | | int _applied_rf_num = 0; |
233 | | int _total_rf_num = 0; |
234 | | // Cloned from _conjuncts of scan node. |
235 | | // It includes predicate in SQL and runtime filters. |
236 | | VExprContextSPtrs _conjuncts; |
237 | | VExprContextSPtrs _projections; |
238 | | // Used in common subexpression elimination to compute intermediate results. |
239 | | std::vector<VExprContextSPtrs> _intermediate_projections; |
240 | | Block _origin_block; |
241 | | Block _padding_block; |
242 | | bool _alreay_eos = false; |
243 | | |
244 | | VExprContextSPtrs _common_expr_ctxs_push_down; |
245 | | |
246 | | // num of rows read from scanner |
247 | | int64_t _num_rows_read = 0; |
248 | | |
249 | | int64_t _num_byte_read = 0; |
250 | | |
251 | | // num of rows return from scanner, after filter block |
252 | | int64_t _num_rows_return = 0; |
253 | | |
254 | | size_t _block_avg_bytes = 0; |
255 | | |
256 | | // Set true after counter is updated finally |
257 | | bool _has_updated_counter = false; |
258 | | |
259 | | // watch to count the time wait for scanner thread |
260 | | MonotonicStopWatch _watch; |
261 | | // Do not use ScopedTimer. There is no guarantee that, the counter |
262 | | ThreadCpuStopWatch _cpu_watch; |
263 | | int64_t _scanner_wait_worker_timer = 0; |
264 | | int64_t _scan_cpu_timer = 0; |
265 | | |
266 | | bool _is_load = false; |
267 | | |
268 | | bool _has_prepared = false; |
269 | | |
270 | | ScannerCounter _counter; |
271 | | int64_t _per_scanner_timer = 0; |
272 | | int64_t _per_scanner_prepare_timer = 0; |
273 | | int64_t _per_scanner_open_timer = 0; |
274 | | int64_t _projection_timer = 0; |
275 | | |
276 | | bool _should_stop = false; |
277 | | }; |
278 | | |
279 | | using ScannerSPtr = std::shared_ptr<Scanner>; |
280 | | |
281 | | } // namespace doris |