|
| 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::collections::HashMap; |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +use databend_common_base::runtime::GlobalIORuntime; |
| 19 | +use databend_common_catalog::table_context::TableContext; |
| 20 | +use databend_common_exception::Result; |
| 21 | +use databend_common_pipeline_core::ExecutionInfo; |
| 22 | +use databend_common_pipeline_core::Pipeline; |
| 23 | +use databend_common_storages_fuse::FuseTable; |
| 24 | +use log::info; |
| 25 | + |
| 26 | +use crate::pipelines::executor::ExecutorSettings; |
| 27 | +use crate::pipelines::executor::PipelineCompleteExecutor; |
| 28 | +use crate::sessions::QueryContext; |
| 29 | + |
| 30 | +pub struct AnalyzeDesc { |
| 31 | + pub catalog: String, |
| 32 | + pub database: String, |
| 33 | + pub table: String, |
| 34 | +} |
| 35 | + |
| 36 | +/// Hook analyze action with a on-finished callback. |
| 37 | +/// errors (if any) are ignored. |
| 38 | +pub async fn hook_analyze(ctx: Arc<QueryContext>, pipeline: &mut Pipeline, desc: AnalyzeDesc) { |
| 39 | + if pipeline.is_empty() { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + pipeline.set_on_finished(move |info: &ExecutionInfo| { |
| 44 | + if info.res.is_ok() { |
| 45 | + info!("[ANALYZE-HOOK] Pipeline execution completed successfully, starting analyze job"); |
| 46 | + if !ctx.get_enable_auto_analyze() { |
| 47 | + return Ok(()); |
| 48 | + } |
| 49 | + |
| 50 | + match GlobalIORuntime::instance().block_on(do_analyze(ctx, desc)) { |
| 51 | + Ok(_) => { |
| 52 | + info!("[ANALYZE-HOOK] Analyze job completed successfully"); |
| 53 | + } |
| 54 | + Err(e) => { |
| 55 | + info!("[ANALYZE-HOOK] Analyze job failed: {:?}", e); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + Ok(()) |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +/// hook the analyze action with a on-finished callback. |
| 64 | +async fn do_analyze(ctx: Arc<QueryContext>, desc: AnalyzeDesc) -> Result<()> { |
| 65 | + // evict the table from cache |
| 66 | + ctx.evict_table_from_cache(&desc.catalog, &desc.database, &desc.table)?; |
| 67 | + ctx.clear_table_meta_timestamps_cache(); |
| 68 | + |
| 69 | + let table = ctx |
| 70 | + .get_table(&desc.catalog, &desc.database, &desc.table) |
| 71 | + .await?; |
| 72 | + let fuse_table = FuseTable::try_from_table(table.as_ref())?; |
| 73 | + let mut pipeline = Pipeline::create(); |
| 74 | + let Some(table_snapshot) = fuse_table.read_table_snapshot().await? else { |
| 75 | + return Ok(()); |
| 76 | + }; |
| 77 | + fuse_table.do_analyze( |
| 78 | + ctx.clone(), |
| 79 | + table_snapshot, |
| 80 | + &mut pipeline, |
| 81 | + HashMap::new(), |
| 82 | + true, |
| 83 | + )?; |
| 84 | + pipeline.set_max_threads(ctx.get_settings().get_max_threads()? as usize); |
| 85 | + let executor_settings = ExecutorSettings::try_create(ctx.clone())?; |
| 86 | + let pipelines = vec![pipeline]; |
| 87 | + let complete_executor = PipelineCompleteExecutor::from_pipelines(pipelines, executor_settings)?; |
| 88 | + ctx.set_executor(complete_executor.get_inner())?; |
| 89 | + complete_executor.execute()?; |
| 90 | + Ok(()) |
| 91 | +} |
0 commit comments