Skip to main content

datanode/heartbeat/handler/
gc_worker.rs

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
15use std::collections::{BTreeMap, HashMap};
16use std::sync::Arc;
17
18use common_meta::instruction::{GcRegions, GcRegionsReply, InstructionReply};
19use common_meta::key::table_info::TableInfoManager;
20use common_meta::key::table_route::TableRouteManager;
21use common_telemetry::{debug, warn};
22use mito2::access_layer::{AccessLayer, AccessLayerRef};
23use mito2::engine::MitoEngine;
24use mito2::gc::LocalGcWorker;
25use mito2::region::MitoRegionRef;
26use snafu::{OptionExt, ResultExt};
27use store_api::path_utils::table_dir;
28use store_api::region_request::PathType;
29use store_api::storage::{FileRefsManifest, GcReport, RegionId};
30use table::requests::STORAGE_KEY;
31use tracing::Instrument;
32
33use crate::error::{GcMitoEngineSnafu, GetMetadataSnafu, Result, UnexpectedSnafu};
34use crate::heartbeat::handler::{HandlerContext, InstructionHandler};
35
36pub struct GcRegionsHandler;
37
38#[async_trait::async_trait]
39impl InstructionHandler for GcRegionsHandler {
40    type Instruction = GcRegions;
41
42    async fn handle(
43        &self,
44        ctx: &HandlerContext,
45        gc_regions: Self::Instruction,
46    ) -> Option<InstructionReply> {
47        let region_ids = gc_regions.regions.clone();
48        debug!("Received gc regions instruction: {:?}", region_ids);
49
50        if region_ids.is_empty() {
51            return Some(InstructionReply::GcRegions(GcRegionsReply {
52                result: Ok(GcReport::default()),
53            }));
54        }
55
56        // Always use the smallest region id on datanode as the target region id for task tracker
57        let mut sorted_region_ids = gc_regions.regions.clone();
58        sorted_region_ids.sort_by_key(|r| r.region_number());
59        let target_region_id = sorted_region_ids[0];
60
61        // Group regions by table_id
62        let mut table_to_regions: HashMap<u32, Vec<RegionId>> = HashMap::new();
63        for rid in region_ids {
64            table_to_regions
65                .entry(rid.table_id())
66                .or_default()
67                .push(rid);
68        }
69
70        let file_refs_manifest = gc_regions.file_refs_manifest.clone();
71        let full_file_listing = gc_regions.full_file_listing;
72
73        let ctx_clone = ctx.clone();
74        let register_result = ctx
75            .gc_tasks
76            .try_register(
77                target_region_id,
78                Box::pin(
79                    async move {
80                        let mut reports = Vec::with_capacity(table_to_regions.len());
81                        for (table_id, regions) in table_to_regions {
82                            debug!(
83                                "Starting gc worker for table {}, regions: {:?}",
84                                table_id, regions
85                            );
86                            let gc_worker = GcRegionsHandler::create_gc_worker(
87                                &ctx_clone,
88                                table_id,
89                                regions,
90                                &file_refs_manifest,
91                                full_file_listing,
92                            )
93                            .await?;
94
95                            let report = gc_worker.run().await.context(GcMitoEngineSnafu {
96                                region_id: target_region_id,
97                            })?;
98                            debug!(
99                                "Gc worker for table {} finished, report: {:?}",
100                                table_id, report
101                            );
102                            reports.push(report);
103                        }
104
105                        // Merge reports
106                        let mut merged_report = GcReport::default();
107                        for report in reports {
108                            merged_report.merge(report);
109                        }
110
111                        Ok(merged_report)
112                    }
113                    .instrument(common_telemetry::tracing::info_span!("gc_worker_run")),
114                ),
115            )
116            .await;
117
118        if register_result.is_busy() {
119            warn!("Another gc task is running for the region: {target_region_id}");
120            return Some(InstructionReply::GcRegions(GcRegionsReply {
121                result: Err(
122                    common_meta::instruction::InstructionError::legacy_internal_retryable(format!(
123                        "Another gc task is running for the region: {target_region_id}"
124                    )),
125                ),
126            }));
127        }
128        let mut watcher = register_result.into_watcher();
129        let result = ctx.gc_tasks.wait_until_finish(&mut watcher).await;
130        match result {
131            Ok(report) => Some(InstructionReply::GcRegions(GcRegionsReply {
132                result: Ok(report),
133            })),
134            Err(err) => Some(InstructionReply::GcRegions(GcRegionsReply {
135                result: Err(common_meta::instruction::InstructionError::from_error(&err)),
136            })),
137        }
138    }
139}
140
141impl GcRegionsHandler {
142    /// Create a GC worker for the given table and region IDs.
143    async fn create_gc_worker(
144        ctx: &HandlerContext,
145        table_id: u32,
146        region_ids: Vec<RegionId>,
147        file_ref_manifest: &FileRefsManifest,
148        full_file_listing: bool,
149    ) -> Result<LocalGcWorker> {
150        debug_assert!(!region_ids.is_empty(), "region_ids should not be empty");
151
152        let mito_engine = ctx
153            .region_server
154            .mito_engine()
155            .with_context(|| UnexpectedSnafu {
156                violated: "MitoEngine not found".to_string(),
157            })?;
158
159        let (access_layer, mito_regions) =
160            Self::get_access_layer(ctx, &mito_engine, table_id, &region_ids).await?;
161
162        let cache_manager = mito_engine.cache_manager();
163
164        let gc_worker = LocalGcWorker::try_new(
165            access_layer,
166            Some(cache_manager),
167            mito_regions,
168            mito_engine.mito_config().gc.clone(),
169            file_ref_manifest.clone(),
170            &mito_engine.gc_limiter(),
171            full_file_listing,
172        )
173        .await
174        .context(GcMitoEngineSnafu {
175            region_id: region_ids[0],
176        })?;
177
178        Ok(gc_worker)
179    }
180
181    /// Get the access layer for the given table and region IDs.
182    /// It also returns the mito regions if they are found in the engine.
183    ///
184    /// This method validates:
185    /// 1. Any found region must be a Leader (not Follower)
186    /// 2. Any missing region must not be routed to another datanode
187    ///
188    /// The AccessLayer is always constructed from table metadata for consistency.
189    async fn get_access_layer(
190        ctx: &HandlerContext,
191        mito_engine: &MitoEngine,
192        table_id: u32,
193        region_ids: &[RegionId],
194    ) -> Result<(AccessLayerRef, BTreeMap<RegionId, Option<MitoRegionRef>>)> {
195        // 1. Collect mito regions and validate Leader status
196        let mut mito_regions = BTreeMap::new();
197
198        for rid in region_ids {
199            let region = mito_engine.find_region(*rid);
200
201            if let Some(ref r) = region {
202                // Validation: Check if region is a leader
203                if r.is_follower() {
204                    return Err(UnexpectedSnafu {
205                        violated: format!(
206                            "Region {} is a follower, cannot perform GC on follower regions",
207                            rid
208                        ),
209                    }
210                    .build());
211                }
212            }
213            mito_regions.insert(*rid, region);
214        }
215
216        // 2. Validate that missing regions are not routed to other datanodes
217        let missing_regions: Vec<_> = mito_regions
218            .iter()
219            .filter(|(_, r)| r.is_none())
220            .map(|(rid, _)| *rid)
221            .collect();
222
223        if !missing_regions.is_empty() {
224            Self::validate_regions_not_routed_elsewhere(ctx, table_id, &missing_regions).await?;
225        }
226
227        // 3. Construct AccessLayer directly from table metadata
228        let access_layer = Self::construct_access_layer(ctx, mito_engine, table_id).await?;
229
230        Ok((access_layer, mito_regions))
231    }
232
233    /// Manually construct an access layer from table metadata.
234    async fn construct_access_layer(
235        ctx: &HandlerContext,
236        mito_engine: &MitoEngine,
237        table_id: u32,
238    ) -> Result<AccessLayerRef> {
239        let table_info_manager = TableInfoManager::new(ctx.kv_backend.clone());
240        let table_info_value = table_info_manager
241            .get(table_id)
242            .await
243            .context(GetMetadataSnafu)?
244            .with_context(|| UnexpectedSnafu {
245                violated: format!("Table metadata not found for table {}", table_id),
246            })?;
247
248        let table_dir = table_dir(&table_info_value.region_storage_path(), table_id);
249        let storage_name = table_info_value
250            .table_info
251            .meta
252            .options
253            .extra_options
254            .get(STORAGE_KEY);
255        let engine = &table_info_value.table_info.meta.engine;
256        let path_type = match engine.as_str() {
257            common_catalog::consts::MITO2_ENGINE => PathType::Bare,
258            common_catalog::consts::MITO_ENGINE => PathType::Bare,
259            common_catalog::consts::METRIC_ENGINE => PathType::Data,
260            _ => PathType::Bare,
261        };
262
263        let object_store = if let Some(name) = storage_name {
264            mito_engine
265                .object_store_manager()
266                .find(name)
267                .cloned()
268                .with_context(|| UnexpectedSnafu {
269                    violated: format!("Object store {} not found", name),
270                })?
271        } else {
272            mito_engine
273                .object_store_manager()
274                .default_object_store()
275                .clone()
276        };
277
278        Ok(Arc::new(AccessLayer::new(
279            table_dir,
280            path_type,
281            object_store,
282            mito_engine.puffin_manager_factory().clone(),
283            mito_engine.intermediate_manager().clone(),
284        )))
285    }
286
287    /// Validate that the given regions are not routed to other datanodes.
288    ///
289    /// If any region is still active on another datanode (has a leader_peer in route table),
290    /// this function returns an error to prevent accidental deletion of files
291    /// that are still in use.
292    async fn validate_regions_not_routed_elsewhere(
293        ctx: &HandlerContext,
294        table_id: u32,
295        missing_region_ids: &[RegionId],
296    ) -> Result<()> {
297        if missing_region_ids.is_empty() {
298            return Ok(());
299        }
300
301        let table_route_manager = TableRouteManager::new(ctx.kv_backend.clone());
302
303        // Get table route
304        let table_route = match table_route_manager
305            .table_route_storage()
306            .get(table_id)
307            .await
308            .context(GetMetadataSnafu)?
309        {
310            Some(route) => route,
311            None => {
312                // Table route not found, all regions are likely deleted
313                debug!(
314                    "Table route not found for table {}, regions {:?} are considered deleted",
315                    table_id, missing_region_ids
316                );
317                return Ok(());
318            }
319        };
320
321        // Get region routes for physical table
322        let region_routes = match table_route.region_routes() {
323            Ok(routes) => routes,
324            Err(_) => {
325                // Logical table, skip validation
326                debug!(
327                    "Table {} is a logical table, skipping region route validation",
328                    table_id
329                );
330                return Ok(());
331            }
332        };
333
334        let region_routes_map: HashMap<RegionId, _> = region_routes
335            .iter()
336            .map(|route| (route.region.id, route))
337            .collect();
338
339        // Check each missing region
340        for region_id in missing_region_ids {
341            if let Some(route) = region_routes_map.get(region_id) {
342                if let Some(leader_peer) = &route.leader_peer {
343                    // Region still has a leader on some datanode.
344                    return Err(UnexpectedSnafu {
345                        violated: format!(
346                            "Region {} is not on this datanode but is routed to datanode {}. \
347                             GC request may have been sent to wrong datanode.",
348                            region_id, leader_peer.id
349                        ),
350                    }
351                    .build());
352                }
353
354                return Err(UnexpectedSnafu {
355                    violated: format!(
356                        "Region {} has no leader in route table; refusing GC without explicit tombstone/deleted state.",
357                        region_id
358                    ),
359                }
360                .build());
361            }
362            // Region not in route table: treat as deleted and allow GC.
363        }
364
365        Ok(())
366    }
367}