|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::any::Any; |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +use databend_common_base::base::Progress; |
| 19 | +use databend_common_base::base::ProgressValues; |
| 20 | +use databend_common_base::runtime::profile::Profile; |
| 21 | +use databend_common_base::runtime::profile::ProfileStatisticsName; |
| 22 | +use databend_common_catalog::table_context::TableContext; |
| 23 | +use databend_common_exception::Result; |
| 24 | +use databend_common_expression::DataBlock; |
| 25 | +use databend_common_pipeline_core::processors::Event; |
| 26 | +use databend_common_pipeline_core::processors::EventCause; |
| 27 | +use databend_common_pipeline_core::processors::OutputPort; |
| 28 | +use databend_common_pipeline_core::processors::Processor; |
| 29 | +use databend_common_pipeline_core::processors::ProcessorPtr; |
| 30 | + |
| 31 | +#[async_trait::async_trait] |
| 32 | +pub trait PrefetchAsyncSource: Send { |
| 33 | + const NAME: &'static str; |
| 34 | + const SKIP_EMPTY_DATA_BLOCK: bool = true; |
| 35 | + |
| 36 | + #[async_trait::unboxed_simple] |
| 37 | + async fn generate(&mut self) -> Result<Option<DataBlock>>; |
| 38 | + fn is_full(&self, prefetched: &[DataBlock]) -> bool; |
| 39 | + |
| 40 | + fn un_reacted(&self) -> Result<()> { |
| 41 | + Ok(()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TODO: This can be refactored using proc macros |
| 46 | +// TODO: Most of its current code is consistent with sync. We need refactor this with better async |
| 47 | +// scheduling after supported expand processors. It will be implemented using a similar dynamic window. |
| 48 | +pub struct PrefetchAsyncSourcer<T: 'static + PrefetchAsyncSource> { |
| 49 | + is_inner_finish: bool, |
| 50 | + |
| 51 | + inner: T, |
| 52 | + output: Arc<OutputPort>, |
| 53 | + scan_progress: Arc<Progress>, |
| 54 | + generated_data: Vec<DataBlock>, |
| 55 | +} |
| 56 | + |
| 57 | +impl<T: 'static + PrefetchAsyncSource> PrefetchAsyncSourcer<T> { |
| 58 | + pub fn create( |
| 59 | + ctx: Arc<dyn TableContext>, |
| 60 | + output: Arc<OutputPort>, |
| 61 | + inner: T, |
| 62 | + ) -> Result<ProcessorPtr> { |
| 63 | + let scan_progress = ctx.get_scan_progress(); |
| 64 | + Ok(ProcessorPtr::create(Box::new(Self { |
| 65 | + inner, |
| 66 | + output, |
| 67 | + scan_progress, |
| 68 | + is_inner_finish: false, |
| 69 | + generated_data: vec![], |
| 70 | + }))) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[async_trait::async_trait] |
| 75 | +impl<T: 'static + PrefetchAsyncSource> Processor for PrefetchAsyncSourcer<T> { |
| 76 | + fn name(&self) -> String { |
| 77 | + T::NAME.to_string() |
| 78 | + } |
| 79 | + |
| 80 | + fn as_any(&mut self) -> &mut dyn Any { |
| 81 | + self |
| 82 | + } |
| 83 | + |
| 84 | + fn event(&mut self) -> Result<Event> { |
| 85 | + if self.is_inner_finish && self.generated_data.is_empty() { |
| 86 | + self.output.finish(); |
| 87 | + return Ok(Event::Finished); |
| 88 | + } |
| 89 | + |
| 90 | + if self.output.is_finished() { |
| 91 | + return Ok(Event::Finished); |
| 92 | + } |
| 93 | + |
| 94 | + if self.output.can_push() { |
| 95 | + if let Some(data_block) = self.generated_data.pop() { |
| 96 | + self.output.push_data(Ok(data_block)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if self.is_inner_finish || self.inner.is_full(&self.generated_data) { |
| 101 | + Ok(Event::NeedConsume) |
| 102 | + } else { |
| 103 | + Ok(Event::Async) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fn un_reacted(&self, cause: EventCause, _id: usize) -> Result<()> { |
| 108 | + if let EventCause::Output(_) = cause { |
| 109 | + self.inner.un_reacted()?; |
| 110 | + } |
| 111 | + |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | + |
| 115 | + #[async_backtrace::framed] |
| 116 | + async fn async_process(&mut self) -> Result<()> { |
| 117 | + match self.inner.generate().await? { |
| 118 | + None => self.is_inner_finish = true, |
| 119 | + Some(data_block) => { |
| 120 | + // Don't need to record the scan progress of `MaterializedCteSource` |
| 121 | + // Because it reads data from memory. |
| 122 | + if !data_block.is_empty() && self.name() != "MaterializedCteSource" { |
| 123 | + let progress_values = ProgressValues { |
| 124 | + rows: data_block.num_rows(), |
| 125 | + bytes: data_block.memory_size(), |
| 126 | + }; |
| 127 | + self.scan_progress.incr(&progress_values); |
| 128 | + Profile::record_usize_profile( |
| 129 | + ProfileStatisticsName::ScanBytes, |
| 130 | + data_block.memory_size(), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + if !T::SKIP_EMPTY_DATA_BLOCK || !data_block.is_empty() { |
| 135 | + self.generated_data.push(data_block) |
| 136 | + } |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | +} |
0 commit comments