|
| 1 | +// Copyright 2023 Greptime Team |
| 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 common_meta::instruction::{GcRegions, GcRegionsReply, InstructionReply}; |
| 16 | +use common_telemetry::{info, warn}; |
| 17 | +use mito2::gc::LocalGcWorker; |
| 18 | +use snafu::{OptionExt, ResultExt}; |
| 19 | +use store_api::storage::{FileRefsManifest, RegionId}; |
| 20 | + |
| 21 | +use crate::error::{GcMitoEngineSnafu, RegionNotFoundSnafu, Result, UnexpectedSnafu}; |
| 22 | +use crate::heartbeat::handler::HandlerContext; |
| 23 | + |
| 24 | +impl HandlerContext { |
| 25 | + pub(crate) async fn handle_gc_regions_instruction( |
| 26 | + self, |
| 27 | + gc_regions: GcRegions, |
| 28 | + ) -> Option<InstructionReply> { |
| 29 | + let region_ids = gc_regions.regions.clone(); |
| 30 | + info!("Received gc regions instruction: {:?}", region_ids); |
| 31 | + |
| 32 | + let mut table_id = None; |
| 33 | + let is_same_table = region_ids.windows(2).all(|w| { |
| 34 | + let t1 = w[0].table_id(); |
| 35 | + let t2 = w[1].table_id(); |
| 36 | + if table_id.is_none() { |
| 37 | + table_id = Some(t1); |
| 38 | + } |
| 39 | + t1 == t2 |
| 40 | + }); |
| 41 | + if !is_same_table { |
| 42 | + return Some(InstructionReply::GcRegions(GcRegionsReply { |
| 43 | + result: Err(format!( |
| 44 | + "Regions to GC should belong to the same table, found: {:?}", |
| 45 | + region_ids |
| 46 | + )), |
| 47 | + })); |
| 48 | + } |
| 49 | + |
| 50 | + let (region_id, gc_worker) = match self |
| 51 | + .create_gc_worker(region_ids, &gc_regions.file_refs_manifest) |
| 52 | + .await |
| 53 | + { |
| 54 | + Ok(worker) => worker, |
| 55 | + Err(e) => { |
| 56 | + return Some(InstructionReply::GcRegions(GcRegionsReply { |
| 57 | + result: Err(format!("Failed to create GC worker: {}", e)), |
| 58 | + })); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + let register_result = self |
| 63 | + .gc_tasks |
| 64 | + .try_register( |
| 65 | + region_id, |
| 66 | + Box::pin(async move { |
| 67 | + info!("Starting gc worker for region {}", region_id); |
| 68 | + let report = gc_worker |
| 69 | + .run() |
| 70 | + .await |
| 71 | + .context(GcMitoEngineSnafu { region_id })?; |
| 72 | + info!("Gc worker for region {} finished", region_id); |
| 73 | + Ok(report) |
| 74 | + }), |
| 75 | + ) |
| 76 | + .await; |
| 77 | + if register_result.is_busy() { |
| 78 | + warn!("Another gc task is running for the region: {region_id}"); |
| 79 | + } |
| 80 | + let mut watcher = register_result.into_watcher(); |
| 81 | + let result = self.gc_tasks.wait_until_finish(&mut watcher).await; |
| 82 | + match result { |
| 83 | + Ok(report) => Some(InstructionReply::GcRegions(GcRegionsReply { |
| 84 | + result: Ok(report), |
| 85 | + })), |
| 86 | + Err(err) => Some(InstructionReply::GcRegions(GcRegionsReply { |
| 87 | + result: Err(format!("{err:?}")), |
| 88 | + })), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + async fn create_gc_worker( |
| 93 | + &self, |
| 94 | + mut region_ids: Vec<RegionId>, |
| 95 | + file_ref_manifest: &FileRefsManifest, |
| 96 | + ) -> Result<(RegionId, LocalGcWorker)> { |
| 97 | + // always use the smallest region id on datanode as the target region id |
| 98 | + region_ids.sort_by_key(|r| r.region_number()); |
| 99 | + let mito_engine = self.region_server.mito_engine().context(UnexpectedSnafu { |
| 100 | + violated: "MitoEngine not found".to_string(), |
| 101 | + })?; |
| 102 | + let region_id = *region_ids.first().context(UnexpectedSnafu { |
| 103 | + violated: "No region ids provided".to_string(), |
| 104 | + })?; |
| 105 | + |
| 106 | + let mito_config = mito_engine.mito_config(); |
| 107 | + let region = mito_engine |
| 108 | + .find_region(region_id) |
| 109 | + .context(RegionNotFoundSnafu { region_id })?; |
| 110 | + let access_layer = region.access_layer(); |
| 111 | + |
| 112 | + let cache_manager = mito_engine.cache_manager(); |
| 113 | + |
| 114 | + let gc_worker = LocalGcWorker::try_new( |
| 115 | + access_layer.clone(), |
| 116 | + Some(cache_manager), |
| 117 | + region_ids.into_iter().collect(), |
| 118 | + Default::default(), |
| 119 | + mito_config.clone().into(), |
| 120 | + file_ref_manifest.clone(), |
| 121 | + &mito_engine.gc_limiter(), |
| 122 | + ) |
| 123 | + .await |
| 124 | + .context(GcMitoEngineSnafu { region_id })?; |
| 125 | + |
| 126 | + Ok((region_id, gc_worker)) |
| 127 | + } |
| 128 | +} |
0 commit comments