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