mito2/worker/
handle_drop.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
15//! Handling drop request.
16
17use std::time::Duration;
18
19use bytes::Bytes;
20use common_telemetry::{error, info, warn};
21use futures::TryStreamExt;
22use object_store::util::join_path;
23use object_store::{EntryMode, ObjectStore};
24use snafu::ResultExt;
25use store_api::logstore::LogStore;
26use store_api::region_request::AffectedRows;
27use store_api::storage::RegionId;
28use tokio::time::sleep;
29
30use crate::error::{OpenDalSnafu, Result};
31use crate::region::{RegionLeaderState, RegionMapRef};
32use crate::worker::{RegionWorkerLoop, DROPPING_MARKER_FILE};
33
34const GC_TASK_INTERVAL_SEC: u64 = 5 * 60; // 5 minutes
35const MAX_RETRY_TIMES: u64 = 12; // 1 hours (5m * 12)
36
37impl<S> RegionWorkerLoop<S>
38where
39    S: LogStore,
40{
41    pub(crate) async fn handle_drop_request(
42        &mut self,
43        region_id: RegionId,
44    ) -> Result<AffectedRows> {
45        let region = self.regions.writable_region(region_id)?;
46
47        info!("Try to drop region: {}, worker: {}", region_id, self.id);
48
49        // Marks the region as dropping.
50        region.set_dropping()?;
51        // Writes dropping marker
52        // We rarely drop a region so we still operate in the worker loop.
53        let marker_path = join_path(region.access_layer.region_dir(), DROPPING_MARKER_FILE);
54        region
55            .access_layer
56            .object_store()
57            .write(&marker_path, Bytes::new())
58            .await
59            .context(OpenDalSnafu)
60            .inspect_err(|e| {
61                error!(e; "Failed to write the drop marker file for region {}", region_id);
62
63                // Sets the state back to writable. It's possible that the marker file has been written.
64                // We set the state back to writable so we can retry the drop operation.
65                region.switch_state_to_writable(RegionLeaderState::Dropping);
66            })?;
67
68        region.stop().await;
69        // Removes this region from region map to prevent other requests from accessing this region
70        self.regions.remove_region(region_id);
71        self.dropping_regions.insert_region(region.clone());
72
73        // Delete region data in WAL.
74        self.wal
75            .obsolete(
76                region_id,
77                region.version_control.current().last_entry_id,
78                &region.provider,
79            )
80            .await?;
81        // Notifies flush scheduler.
82        self.flush_scheduler.on_region_dropped(region_id);
83        // Notifies compaction scheduler.
84        self.compaction_scheduler.on_region_dropped(region_id);
85
86        // Marks region version as dropped
87        region
88            .version_control
89            .mark_dropped(&region.memtable_builder);
90        info!(
91            "Region {} is dropped logically, but some files are not deleted yet",
92            region_id
93        );
94
95        self.region_count.dec();
96
97        // Detaches a background task to delete the region dir
98        let region_dir = region.access_layer.region_dir().to_owned();
99        let object_store = region.access_layer.object_store().clone();
100        let dropping_regions = self.dropping_regions.clone();
101        let listener = self.listener.clone();
102        common_runtime::spawn_global(async move {
103            let gc_duration = listener
104                .on_later_drop_begin(region_id)
105                .unwrap_or(Duration::from_secs(GC_TASK_INTERVAL_SEC));
106            let removed = later_drop_task(
107                region_id,
108                region_dir,
109                object_store,
110                dropping_regions,
111                gc_duration,
112            )
113            .await;
114            listener.on_later_drop_end(region_id, removed);
115        });
116
117        Ok(0)
118    }
119}
120
121/// Background GC task to remove the entire region path once one of the following
122/// conditions is true:
123/// - It finds there is no parquet file left.
124/// - After `gc_duration`.
125///
126/// Returns whether the path is removed.
127///
128/// This task will retry on failure and keep running until finished. Any resource
129/// captured by it will not be released before then. Be sure to only pass weak reference
130/// if something is depended on ref-count mechanism.
131async fn later_drop_task(
132    region_id: RegionId,
133    region_path: String,
134    object_store: ObjectStore,
135    dropping_regions: RegionMapRef,
136    gc_duration: Duration,
137) -> bool {
138    let mut force = false;
139    for _ in 0..MAX_RETRY_TIMES {
140        let result = remove_region_dir_once(&region_path, &object_store, force).await;
141        match result {
142            Err(err) => {
143                warn!(
144                    "Error occurs during trying to GC region dir {}: {}",
145                    region_path, err
146                );
147            }
148            Ok(true) => {
149                dropping_regions.remove_region(region_id);
150                info!("Region {} is dropped, force: {}", region_path, force);
151                return true;
152            }
153            Ok(false) => (),
154        }
155        sleep(gc_duration).await;
156        // Force recycle after gc duration.
157        force = true;
158    }
159
160    warn!(
161        "Failed to GC region dir {} after {} retries, giving up",
162        region_path, MAX_RETRY_TIMES
163    );
164
165    false
166}
167
168// TODO(ruihang): place the marker in a separate dir
169/// Removes region dir if there is no parquet files, returns whether the directory is removed.
170/// If `force = true`, always removes the dir.
171pub(crate) async fn remove_region_dir_once(
172    region_path: &str,
173    object_store: &ObjectStore,
174    force: bool,
175) -> Result<bool> {
176    // list all files under the given region path to check if there are un-deleted parquet files
177    let mut has_parquet_file = false;
178    // record all paths that neither ends with .parquet nor the marker file
179    let mut files_to_remove_first = vec![];
180    let mut files = object_store
181        .lister_with(region_path)
182        .await
183        .context(OpenDalSnafu)?;
184    while let Some(file) = files.try_next().await.context(OpenDalSnafu)? {
185        if !force && file.path().ends_with(".parquet") {
186            // If not in force mode, we only remove the region dir if there is no parquet file
187            has_parquet_file = true;
188            break;
189        } else if !file.path().ends_with(DROPPING_MARKER_FILE) {
190            let meta = file.metadata();
191            if meta.mode() == EntryMode::FILE {
192                files_to_remove_first.push(file.path().to_string());
193            }
194        }
195    }
196
197    if !has_parquet_file {
198        // no parquet file found, delete the region path
199        // first delete all files other than the marker
200        object_store
201            .delete_iter(files_to_remove_first)
202            .await
203            .context(OpenDalSnafu)?;
204        // then remove the marker with this dir
205        object_store
206            .remove_all(region_path)
207            .await
208            .context(OpenDalSnafu)?;
209        Ok(true)
210    } else {
211        Ok(false)
212    }
213}