|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashSet; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +use futures::stream::{self, StreamExt}; |
| 22 | +use futures::TryStreamExt; |
| 23 | + |
| 24 | +use crate::error::Result; |
| 25 | +use crate::io::FileIO; |
| 26 | +use crate::spec::{ManifestFile, Snapshot, TableMetadataRef}; |
| 27 | + |
| 28 | +const DEFAULT_DELETE_CONCURRENCY_LIMIT: usize = 10; |
| 29 | + |
| 30 | +pub struct ReachableFileCleanupStrategy { |
| 31 | + file_io: FileIO, |
| 32 | +} |
| 33 | + |
| 34 | +impl ReachableFileCleanupStrategy { |
| 35 | + pub fn new(file_io: FileIO) -> Self { |
| 36 | + Self { file_io } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl ReachableFileCleanupStrategy { |
| 41 | + pub async fn clean_files( |
| 42 | + &self, |
| 43 | + before_expiration: &TableMetadataRef, |
| 44 | + after_expiration: &TableMetadataRef, |
| 45 | + ) -> Result<()> { |
| 46 | + let mut manifest_lists_to_delete: HashSet<&str> = HashSet::default(); |
| 47 | + let mut expired_snapshots = Vec::default(); |
| 48 | + for snapshot in before_expiration.snapshots() { |
| 49 | + if after_expiration |
| 50 | + .snapshot_by_id(snapshot.snapshot_id()) |
| 51 | + .is_none() |
| 52 | + { |
| 53 | + expired_snapshots.push(snapshot); |
| 54 | + manifest_lists_to_delete.insert(snapshot.manifest_list()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + let deletion_candidates = { |
| 59 | + let mut deletion_candidates = HashSet::default(); |
| 60 | + // This part can also be parallelized if `load_manifest_list` is a bottleneck |
| 61 | + // and if the underlying FileIO supports concurrent reads efficiently. |
| 62 | + for snapshot in expired_snapshots { |
| 63 | + let manifest_list = snapshot |
| 64 | + .load_manifest_list(&self.file_io, before_expiration) |
| 65 | + .await?; |
| 66 | + |
| 67 | + for manifest_file in manifest_list.entries() { |
| 68 | + deletion_candidates.insert(manifest_file.clone()); |
| 69 | + } |
| 70 | + } |
| 71 | + deletion_candidates |
| 72 | + }; |
| 73 | + |
| 74 | + if !deletion_candidates.is_empty() { |
| 75 | + let (manifests_to_delete, referenced_manifests) = self |
| 76 | + .prune_referenced_manifests( |
| 77 | + after_expiration.snapshots(), |
| 78 | + after_expiration, |
| 79 | + deletion_candidates, |
| 80 | + ) |
| 81 | + .await?; |
| 82 | + |
| 83 | + if !manifests_to_delete.is_empty() { |
| 84 | + let files_to_delete = self |
| 85 | + .find_files_to_delete(&manifests_to_delete, &referenced_manifests) |
| 86 | + .await?; |
| 87 | + |
| 88 | + stream::iter(files_to_delete) |
| 89 | + .map(|file_path| self.file_io.delete(file_path)) |
| 90 | + .buffer_unordered(DEFAULT_DELETE_CONCURRENCY_LIMIT) |
| 91 | + .try_collect::<Vec<_>>() |
| 92 | + .await?; |
| 93 | + |
| 94 | + stream::iter(manifests_to_delete) |
| 95 | + .map(|manifest_file| self.file_io.delete(manifest_file.manifest_path)) |
| 96 | + .buffer_unordered(DEFAULT_DELETE_CONCURRENCY_LIMIT) |
| 97 | + .try_collect::<Vec<_>>() |
| 98 | + .await?; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + stream::iter(manifest_lists_to_delete) |
| 103 | + .map(|path| self.file_io.delete(path)) |
| 104 | + .buffer_unordered(DEFAULT_DELETE_CONCURRENCY_LIMIT) |
| 105 | + .try_collect::<Vec<_>>() |
| 106 | + .await?; |
| 107 | + |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | + |
| 111 | + async fn prune_referenced_manifests( |
| 112 | + &self, |
| 113 | + snapshots: impl Iterator<Item = &Arc<Snapshot>>, |
| 114 | + table_meta_data_ref: &TableMetadataRef, |
| 115 | + mut deletion_candidates: HashSet<ManifestFile>, |
| 116 | + ) -> Result<(HashSet<ManifestFile>, HashSet<ManifestFile>)> { |
| 117 | + let mut referenced_manifests = HashSet::default(); |
| 118 | + for snapshot in snapshots { |
| 119 | + let manifest_list = snapshot |
| 120 | + .load_manifest_list(&self.file_io, table_meta_data_ref) |
| 121 | + .await?; |
| 122 | + |
| 123 | + for manifest_file in manifest_list.entries() { |
| 124 | + deletion_candidates.remove(manifest_file); |
| 125 | + referenced_manifests.insert(manifest_file.clone()); |
| 126 | + |
| 127 | + if deletion_candidates.is_empty() { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + Ok((deletion_candidates, referenced_manifests)) |
| 134 | + } |
| 135 | + |
| 136 | + async fn find_files_to_delete( |
| 137 | + &self, |
| 138 | + manifest_files: &HashSet<ManifestFile>, |
| 139 | + referenced_manifests: &HashSet<ManifestFile>, |
| 140 | + ) -> Result<HashSet<String>> { |
| 141 | + let mut files_to_delete = HashSet::default(); |
| 142 | + for manifest_file in manifest_files { |
| 143 | + let m = manifest_file.load_manifest(&self.file_io).await.unwrap(); |
| 144 | + for entry in m.entries() { |
| 145 | + files_to_delete.insert(entry.data_file().file_path().to_owned()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if files_to_delete.is_empty() { |
| 150 | + return Ok(files_to_delete); |
| 151 | + } |
| 152 | + |
| 153 | + for manifest_file in referenced_manifests { |
| 154 | + let m = manifest_file.load_manifest(&self.file_io).await.unwrap(); |
| 155 | + for entry in m.entries() { |
| 156 | + files_to_delete.remove(entry.data_file().file_path()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + Ok(files_to_delete) |
| 161 | + } |
| 162 | +} |
0 commit comments