Compare commits

...

3 Commits

25 changed files with 3689 additions and 141 deletions
+57 -1
View File
@@ -683,6 +683,7 @@ pub(crate) async fn update_validated_in<F>(
bucket: &str,
config_file: &str,
data: Vec<u8>,
expected_incarnation_id: Option<Uuid>,
validate: F,
) -> Result<OffsetDateTime>
where
@@ -690,7 +691,7 @@ where
{
Box::pin(async {
let sys = require_bucket_metadata_sys_in(ctx)?;
let guard = acquire_config_write_guard(sys.clone(), bucket).await?;
let guard = acquire_config_write_guard_for_incarnation(sys.clone(), bucket, expected_incarnation_id).await?;
validate()?;
update_under_config_write_guard(sys, &guard, config_file, data, None).await
})
@@ -5200,6 +5201,61 @@ mod tests {
);
}
#[tokio::test]
#[serial]
async fn validated_config_update_rejects_a_different_bucket_incarnation() {
let (_dirs, store) = isolated_store_over_temp_disks().await;
init_bucket_metadata_sys(store.clone(), Vec::new()).await;
let bucket = "catalog-cleanup-incarnation";
store
.make_bucket(bucket, &MakeBucketOptions::default())
.await
.expect("create bucket");
let incarnation = store
.bucket_incarnation_id_from_disk(bucket)
.await
.expect("read bucket incarnation");
let called = std::sync::atomic::AtomicBool::new(false);
let result = store
.update_bucket_metadata_config_for_incarnation_validated(
bucket,
"quota.json",
br#"{"quota":1}"#.to_vec(),
Uuid::new_v4(),
|| {
called.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(())
},
)
.await;
assert!(matches!(result, Err(Error::BucketNotFound(name)) if name == bucket));
assert!(
!called.load(std::sync::atomic::Ordering::SeqCst),
"incarnation must be checked before the external fence"
);
let (metadata, _) = get_config_from_disk_with_presence_in(&store.ctx, bucket)
.await
.expect("read unchanged bucket metadata");
assert!(
metadata.quota_config_json.is_empty(),
"a stale cleanup must not modify the replacement bucket"
);
store
.update_bucket_metadata_config_for_incarnation_validated(
bucket,
"quota.json",
br#"{"quota":1}"#.to_vec(),
incarnation,
|| Ok(()),
)
.await
.expect("the current incarnation permits the validated update");
let (metadata, _) = get_config_from_disk_with_presence_in(&store.ctx, bucket)
.await
.expect("read updated bucket metadata");
assert_eq!(metadata.quota_config_json, br#"{"quota":1}"#);
}
#[tokio::test]
#[serial]
async fn quota_admission_rejects_stale_cached_no_quota() {
+15 -5
View File
@@ -161,7 +161,7 @@ impl SetDisks {
)
});
let mut metadata = FileMeta::new();
for parts in versions.into_values() {
for (version_id, parts) in versions {
let errors = parts
.iter()
.zip(&missing)
@@ -173,15 +173,25 @@ impl SetDisks {
}
})
.collect::<Vec<_>>();
let read_quorum = match Self::object_quorum_from_meta(&parts, &errors, default_parity) {
Ok((quorum, _)) => usize::try_from(quorum).map_err(|_| DiskError::FileCorrupt)?,
let (read_quorum, write_quorum) = match Self::object_quorum_from_meta(&parts, &errors, default_parity) {
Ok((read, write)) => (
usize::try_from(read).map_err(|_| DiskError::FileCorrupt)?,
usize::try_from(write).map_err(|_| DiskError::FileCorrupt)?,
),
Err(DiskError::FileNotFound | DiskError::FileVersionNotFound) if complete => continue,
Err(DiskError::FileNotFound | DiskError::FileVersionNotFound) => return Err(DiskError::ErasureReadQuorum),
Err(DiskError::ErasureReadQuorum) if complete => continue,
Err(error) => return Err(error),
};
let mod_time = Self::common_time(&Self::list_object_modtimes(&parts, &errors), read_quorum);
let selected = Self::pick_valid_fileinfo(&parts, mod_time, None, read_quorum)?;
let selected = if crate::bucket::utils::is_meta_bucketname(bucket) && version_id.is_none() {
// A null-version system record must carry the same commit proof
// as GET. Listing only needs the selected metadata, so no disk
// handles are retained from the shared read selector.
Self::select_committed_fileinfo(&vec![None; disk_count], &parts, &errors, "", read_quorum, write_quorum)?.1
} else {
let mod_time = Self::common_time(&Self::list_object_modtimes(&parts, &errors), read_quorum);
Self::pick_valid_fileinfo(&parts, mod_time, None, read_quorum)?
};
metadata.add_version(selected)?;
}
if metadata.versions.is_empty() {
+222 -6
View File
@@ -977,7 +977,7 @@ async fn multipart_upload_paths_on_disk(disk: DiskStore, bucket: &str, root_pref
return Err(DiskError::DiskNotFound);
}
if !root_prefix.is_empty() {
if !root_prefix.is_empty() && root_prefix != DATA_MOVEMENT_MULTIPART_PREFIX {
let upload_dirs = match disk.list_dir(bucket, RUSTFS_META_MULTIPART_BUCKET, root_prefix, -1).await {
Ok(entries) => entries,
Err(DiskError::FileNotFound | DiskError::VolumeNotFound) => return Ok(Vec::new()),
@@ -1002,6 +1002,13 @@ async fn multipart_upload_paths_on_disk(disk: DiskStore, bucket: &str, root_pref
.into_iter()
.map(|sha_dir| sha_dir.trim_end_matches('/').to_owned())
.filter(|sha_dir| sha_dir.len() == 64 && sha_dir.bytes().all(|byte| byte.is_ascii_hexdigit()))
.map(|sha_dir| {
if root_prefix.is_empty() {
sha_dir
} else {
format!("{root_prefix}/{sha_dir}")
}
})
.collect::<Vec<_>>();
let listed_upload_dirs = stream::iter(sha_dirs)
.map(|sha_dir| {
@@ -1124,6 +1131,7 @@ impl SetDisks {
orig_bucket: &str,
error_path: &str,
root_prefix: &str,
require_all_disks: bool,
) -> Result<(Vec<Option<DiskStore>>, Vec<String>, usize, bool)> {
let disks = self.disks.read().await.clone();
if disks.is_empty() {
@@ -1165,7 +1173,8 @@ impl SetDisks {
}
}
if let Some(err) = reduce_read_quorum_errs(&discovery_errors, OBJECT_OP_IGNORED_ERRS, discovery_quorum) {
let required_discoveries = if require_all_disks { disks.len() } else { discovery_quorum };
if let Some(err) = reduce_read_quorum_errs(&discovery_errors, OBJECT_OP_IGNORED_ERRS, required_discoveries) {
return Err(to_object_err(err.into(), vec![orig_bucket, error_path]));
}
@@ -1187,7 +1196,7 @@ impl SetDisks {
pub(crate) async fn first_multipart_upload_path_for_decommission(&self, bucket: &str) -> Result<Option<String>> {
let (_, paths, _, _) = self
.discover_multipart_upload_paths(bucket, RUSTFS_META_MULTIPART_BUCKET, "")
.discover_multipart_upload_paths(bucket, RUSTFS_META_MULTIPART_BUCKET, "", false)
.await?;
Ok(paths.into_iter().next())
}
@@ -1200,8 +1209,9 @@ impl SetDisks {
upload_identity: &str,
) -> Result<Vec<String>> {
let expected_parent = format!("{DATA_MOVEMENT_MULTIPART_PREFIX}/{}", Self::get_multipart_sha_dir(bucket, object));
let (_, candidate_paths, _, has_minority_candidate) =
self.discover_multipart_upload_paths(bucket, object, &expected_parent).await?;
let (_, candidate_paths, _, has_minority_candidate) = self
.discover_multipart_upload_paths(bucket, object, &expected_parent, false)
.await?;
if has_minority_candidate {
return Err(Error::DecommissionCapacityBlocked {
message: "data movement multipart cleanup found an upload path on fewer than the discovery quorum of disks"
@@ -1750,6 +1760,54 @@ impl SetDisks {
})
}
pub(crate) async fn ensure_no_multipart_uploads(&self, bucket: &str) -> Result<()> {
// Every disk must confirm absence in both namespaces before cleanup can
// remove catalog protection from the bucket.
for root_prefix in ["", DATA_MOVEMENT_MULTIPART_PREFIX] {
let (disks, paths, _, has_minority_candidate) =
self.discover_multipart_upload_paths(bucket, "", root_prefix, true).await?;
if has_minority_candidate {
return Err(Error::ErasureReadQuorum);
}
for upload_path in paths {
let (sha_dir, _) = upload_path
.rsplit_once('/')
.filter(|(directory, upload_id)| !directory.is_empty() && !upload_id.is_empty())
.ok_or(DiskError::CorruptedFormat)?;
let (metadata, errors) =
Self::read_all_fileinfo(&disks, bucket, RUSTFS_META_MULTIPART_BUCKET, &upload_path, "", false, false, false)
.await?;
let (read_quorum, _) = Self::object_quorum_from_meta(&metadata, &errors, self.default_parity_count)?;
let read_quorum = usize::try_from(read_quorum).map_err(|_| DiskError::ErasureReadQuorum)?;
if let Some(error) = reduce_read_quorum_errs(&errors, OBJECT_OP_IGNORED_ERRS, read_quorum) {
return Err(to_object_err(error.into(), vec![bucket, &upload_path]));
}
let (_, modified, etag) = Self::list_online_disks(&disks, &metadata, &errors, read_quorum);
let file = Self::pick_valid_fileinfo(&metadata, modified, etag, read_quorum)?;
let (Some(owner_bucket), Some(owner_object)) = (
file.metadata.get(RUSTFS_MULTIPART_BUCKET_KEY),
file.metadata.get(RUSTFS_MULTIPART_OBJECT_KEY),
) else {
return Err(to_object_err(DiskError::CorruptedFormat.into(), vec![bucket, &upload_path]));
};
if owner_bucket.is_empty()
|| owner_object.is_empty()
|| Self::get_multipart_sha_dir(owner_bucket, owner_object)
!= sha_dir
.strip_prefix(DATA_MOVEMENT_MULTIPART_PREFIX)
.and_then(|suffix| suffix.strip_prefix('/'))
.unwrap_or(sha_dir)
{
return Err(to_object_err(DiskError::CorruptedFormat.into(), vec![bucket, &upload_path]));
}
if owner_bucket == bucket {
return Err(Error::BucketNotEmpty(bucket.to_string()));
}
}
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn list_multipart_uploads_for_incarnation(
&self,
@@ -1761,7 +1819,8 @@ impl SetDisks {
max_uploads: usize,
expected_incarnation_id: Option<Uuid>,
) -> Result<ListMultipartsInfo> {
let (disks, candidate_paths, discovery_quorum, _) = self.discover_multipart_upload_paths(bucket, prefix, "").await?;
let (disks, candidate_paths, discovery_quorum, _) =
self.discover_multipart_upload_paths(bucket, prefix, "", false).await?;
let listed_uploads = stream::iter(candidate_paths)
.map(|upload_path| {
let disks = &disks;
@@ -11194,6 +11253,163 @@ mod tests {
assert_eq!(upload_uuid_suffix(&recovered[0]), upload_uuid_suffix(&upload.upload_id));
}
#[tokio::test]
async fn multipart_absence_inspection_distinguishes_bucket_ownership() {
let (_temp_dirs, disk_stores, set_disks) = hermetic_set_disks(4).await;
let bucket = "multipart-cleanup-target";
let other = "multipart-cleanup-other";
for disk in &disk_stores {
disk.make_volume(bucket).await.expect("create target bucket");
disk.make_volume(other).await.expect("create other bucket");
}
set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect("an empty upload namespace permits cleanup");
set_disks
.new_multipart_upload(other, "object.bin", &ObjectOptions::default())
.await
.expect("create upload in other bucket");
set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect("an upload owned by another bucket permits cleanup");
set_disks
.new_multipart_upload(bucket, "object.bin", &ObjectOptions::default())
.await
.expect("create upload in target bucket");
assert!(
matches!(set_disks.ensure_no_multipart_uploads(bucket).await, Err(Error::BucketNotEmpty(name)) if name == bucket)
);
}
#[tokio::test]
async fn multipart_absence_inspection_requires_every_disk_online() {
let (_temp_dirs, disk_stores, set_disks) = hermetic_set_disks(4).await;
let bucket = "multipart-cleanup-offline";
for disk in &disk_stores {
disk.make_volume(bucket).await.expect("create bucket");
}
set_disks.disks.write().await[3] = None;
let listed = set_disks
.list_multipart_uploads_for_incarnation(bucket, "", None, None, None, 1000, None)
.await
.expect("ordinary listing preserves its discovery quorum");
assert!(listed.uploads.is_empty());
let error = set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect_err("an offline disk must prevent confirmation of an empty multipart namespace");
assert!(
matches!(&error, Error::InsufficientReadQuorum(error_bucket, error_path) if error_bucket == bucket && error_path.is_empty()),
"the offline disk must produce a bucket-scoped read quorum error: {error:?}"
);
set_disks.disks.write().await[3] = Some(disk_stores[3].clone());
set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect("all disks can confirm the empty namespace after recovery");
}
#[tokio::test]
async fn multipart_absence_inspection_rejects_a_failed_disk_enumeration() {
let (temp_dirs, disk_stores, set_disks) = hermetic_set_disks(4).await;
let bucket = "multipart-cleanup-list-error";
for disk in &disk_stores {
disk.make_volume(bucket).await.expect("create bucket");
}
let sha_dir = SetDisks::get_multipart_sha_dir(bucket, "object.bin");
let multipart_dir = temp_dirs[0].path().join(RUSTFS_META_MULTIPART_BUCKET);
tokio::fs::create_dir_all(&multipart_dir)
.await
.expect("create multipart staging directory");
tokio::fs::write(multipart_dir.join(&sha_dir), b"not a directory")
.await
.expect("replace one upload parent directory with a file");
multipart_upload_paths_on_disk(disk_stores[0].clone(), bucket, "")
.await
.expect_err("the affected disk must fail directory enumeration");
let listed = set_disks
.list_multipart_uploads_for_incarnation(bucket, "", None, None, None, 1000, None)
.await
.expect("ordinary listing still accepts the remaining discovery quorum");
assert!(listed.uploads.is_empty());
let error = set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect_err("a failed disk enumeration must prevent confirmation of an empty multipart namespace");
assert!(
matches!(&error, Error::InsufficientReadQuorum(error_bucket, error_path) if error_bucket == bucket && error_path.is_empty()),
"the failed enumeration must produce a bucket-scoped read quorum error: {error:?}"
);
tokio::fs::remove_file(temp_dirs[0].path().join(RUSTFS_META_MULTIPART_BUCKET).join(sha_dir))
.await
.expect("remove the invalid directory entry");
set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect("all disks can confirm the empty namespace after repair");
}
#[tokio::test]
async fn multipart_absence_inspection_rejects_data_movement_uploads() {
let (_temp_dirs, disk_stores, set_disks) = hermetic_set_disks(4).await;
let bucket = "multipart-cleanup-data-movement";
for disk in &disk_stores {
disk.make_volume(bucket).await.expect("create bucket");
}
let mut metadata = HashMap::new();
rustfs_utils::http::insert_str(
&mut metadata,
rustfs_utils::http::SUFFIX_DATA_MOVEMENT_UPLOAD,
format!("v1:{}:{}", Uuid::new_v4(), OffsetDateTime::now_utc().unix_timestamp_nanos()),
);
set_disks
.new_multipart_upload(
bucket,
"object.bin",
&ObjectOptions {
data_movement: true,
user_defined: metadata,
..Default::default()
},
)
.await
.expect("create data movement upload");
assert!(
matches!(set_disks.ensure_no_multipart_uploads(bucket).await, Err(Error::BucketNotEmpty(name)) if name == bucket)
);
}
#[tokio::test]
async fn multipart_absence_inspection_rejects_unreadable_ownership() {
let (temp_dirs, disk_stores, set_disks) = hermetic_set_disks(4).await;
let bucket = "multipart-cleanup-corrupt";
for disk in &disk_stores {
disk.make_volume(bucket).await.expect("create bucket");
}
set_disks
.new_multipart_upload(bucket, "object.bin", &ObjectOptions::default())
.await
.expect("create multipart upload");
let mut corrupted = 0;
for temp_dir in &temp_dirs {
for path in multipart_meta_files_on_disk(temp_dir, "xl.meta").await {
tokio::fs::write(path, b"invalid upload metadata")
.await
.expect("corrupt upload metadata");
corrupted += 1;
}
}
assert!(corrupted > 0, "upload metadata must exist before corruption");
set_disks
.ensure_no_multipart_uploads(bucket)
.await
.expect_err("unreadable upload ownership must block cleanup");
}
/// Regression (issue #5716): a single upload directory whose `xl.meta` was
/// destroyed (crash mid-write, torn disk state) must degrade to that upload
/// alone. Failing the whole ListMultipartUploads turns one piece of stale
@@ -44,15 +44,16 @@ pub(crate) mod lifecycle {
pub(crate) mod list {
pub(crate) use rustfs_storage_api::{
ListObjectVersionsInfo as StorageListObjectVersionsInfo, ListObjectsInfo, ListObjectsV2Info as StorageListObjectsV2Info,
ListOperations, ObjectInfoOrErr as StorageObjectInfoOrErr, VersionMarker, WalkOptions as StorageWalkOptions,
WalkVersionsSortOrder,
AuthoritativeListOperations, ListObjectVersionsInfo as StorageListObjectVersionsInfo, ListObjectsInfo,
ListObjectsV2Info as StorageListObjectsV2Info, ListOperations, ObjectInfoOrErr as StorageObjectInfoOrErr, VersionMarker,
WalkOptions as StorageWalkOptions, WalkVersionsSortOrder,
};
}
pub(crate) mod multipart {
pub(crate) use rustfs_storage_api::{
CompletePart, ListMultipartsInfo, ListPartsInfo, MultipartInfo, MultipartOperations, MultipartUploadResult, PartInfo,
CompletePart, ListMultipartsInfo, ListPartsInfo, MultipartInfo, MultipartOperations, MultipartUploadInspection,
MultipartUploadResult, PartInfo,
};
}
+16 -1
View File
@@ -523,7 +523,22 @@ impl ECStore {
where
F: FnOnce() -> Result<()> + Send,
{
metadata_sys::update_validated_in(&self.ctx, bucket, config_file, data, validate).await
metadata_sys::update_validated_in(&self.ctx, bucket, config_file, data, None, validate).await
}
/// Validate an external fence and the bucket incarnation in one metadata transaction.
pub async fn update_bucket_metadata_config_for_incarnation_validated<F>(
&self,
bucket: &str,
config_file: &str,
data: Vec<u8>,
expected_incarnation_id: Uuid,
validate: F,
) -> Result<OffsetDateTime>
where
F: FnOnce() -> Result<()> + Send,
{
metadata_sys::update_validated_in(&self.ctx, bucket, config_file, data, Some(expected_incarnation_id), validate).await
}
pub async fn bucket_incarnation_id(&self, bucket: &str) -> Result<Uuid> {
+244 -3
View File
@@ -37,8 +37,8 @@ use crate::error::{
use crate::object_api::{ObjectInfo, ObjectOptions};
use crate::set_disk::SetDisks;
use crate::storage_api_contracts::list::{
ListObjectsInfo as StorageListObjectsInfo, StorageListObjectVersionsInfo, StorageListObjectsV2Info, StorageObjectInfoOrErr,
StorageWalkOptions, VersionMarker, WalkVersionsSortOrder,
AuthoritativeListOperations, ListObjectsInfo as StorageListObjectsInfo, StorageListObjectVersionsInfo,
StorageListObjectsV2Info, StorageObjectInfoOrErr, StorageWalkOptions, VersionMarker, WalkVersionsSortOrder,
};
use crate::store::ECStore;
use crate::store::utils::is_reserved_or_invalid_bucket;
@@ -80,6 +80,21 @@ type ListObjectVersionsInfo = StorageListObjectVersionsInfo<ObjectInfo>;
type ObjectInfoOrErr = StorageObjectInfoOrErr<ObjectInfo, Error>;
type WalkOptions = StorageWalkOptions<fn(&rustfs_filemeta::FileInfo) -> bool>;
#[async_trait::async_trait]
impl AuthoritativeListOperations for ECStore {
async fn walk_authoritative(
self: Arc<Self>,
rx: CancellationToken,
bucket: &str,
prefix: &str,
result: Sender<ObjectInfoOrErr>,
opts: WalkOptions,
) -> Result<()> {
check_list_objs_args(bucket, prefix, &None)?;
self.walk_authoritatively(rx, bucket, prefix, result, opts).await
}
}
/// Build the versioned listing options used by recursive object walks.
///
/// `list_path_result` normally derives these scan bounds before dispatching to
@@ -1519,6 +1534,57 @@ impl ECStore {
result
}
async fn walk_authoritatively(
self: Arc<Self>,
cancellation: CancellationToken,
bucket: &str,
prefix: &str,
result: Sender<ObjectInfoOrErr>,
opts: WalkOptions,
) -> Result<()> {
let cancel = cancellation.child_token();
let _cancel_on_drop = cancel.clone().drop_guard();
let mut scans = Vec::new();
let mut inputs = Vec::new();
for pool in &self.pools {
for set in &pool.disk_set {
let (sender, receiver) = mpsc::channel(1);
inputs.push(receiver);
let mut scan = recursive_walk_list_path_options(bucket.to_owned(), prefix, &opts);
// Internal absence checks must include reserved object prefixes.
scan.skip_hidden_prefix_check = true;
scans.push(set.list_versions_authoritatively(cancel.clone(), scan, sender));
}
}
if scans.is_empty() {
return Err(StorageError::ErasureReadQuorum);
}
let (sender, receiver) = mpsc::channel(100);
let forwarder = spawn_walk_entry_forwarder(cancel.clone(), receiver, result, opts, bucket.to_owned());
let merge = spawn_listing_merge(cancel.clone(), inputs, sender);
let scan_results = join_all(scans).await;
let merge_result = merge.await.map_err(Error::from)?;
let forward_result = forwarder.await.map_err(Error::from);
// An unavailable set cannot establish absence, even when another set
// completed. Only an explicitly missing directory or volume is empty.
for scan in scan_results {
match scan {
Ok(outcome) if !outcome.limit_reached => {}
Ok(_) => return Err(StorageError::ErasureReadQuorum),
Err(StorageError::FileNotFound | StorageError::VolumeNotFound) => {}
Err(err) => return Err(err),
}
}
merge_result?;
forward_result?;
if cancellation.is_cancelled() {
return Err(Error::other("authoritative object walk was cancelled"));
}
Ok(())
}
}
/// Forwards merged raw entries to a walk consumer as `ObjectInfoOrErr`,
@@ -2489,7 +2555,8 @@ impl SetDisks {
/// Version scan for set-scoped callers that act on absence: set walks
/// (tier reference proofs, tier-delete journals, pool maintenance) and the
/// recursive prefix-delete scan. Every online drive is read and each
/// version is resolved at the read quorum of its own erasure layout; a
/// version uses the read quorum of its own erasure layout. Null versions
/// in the system bucket also retain GET's commit-proof selection. A
/// version that cannot be ruled out fails the scan instead of being
/// omitted. Merged `ListObjectVersions` pages go through `list_path`.
async fn list_versions_authoritatively(
@@ -2828,6 +2895,7 @@ mod test {
use crate::error::{Result, StorageError};
use crate::object_api::{ObjectInfo, ObjectOptions, PutObjReader};
use crate::set_disk::{SetDisks, hermetic_set_disks_isolated};
use crate::storage_api_contracts::list::AuthoritativeListOperations as _;
use crate::storage_api_contracts::namespace::NamespaceLocking as _;
use crate::storage_api_contracts::object::ObjectIO as _;
use bytes::Bytes;
@@ -5688,6 +5756,126 @@ mod test {
));
}
#[test]
fn authoritative_system_bucket_scan_rejects_uncommitted_null_version_split() {
let time = time::OffsetDateTime::from_unix_timestamp(1_700_000_000).expect("valid fixture time");
let old = test_unversioned_object_meta_entry("entry.json", time, "old", Uuid::from_u128(1));
for new_etag in ["new", "old"] {
let new =
test_unversioned_object_meta_entry("entry.json", time + time::Duration::seconds(1), new_etag, Uuid::from_u128(2));
let entries = MetaCacheEntries(vec![Some(old.clone()), Some(old.clone()), Some(new.clone()), Some(new)]);
assert!(matches!(
SetDisks::resolve_listed_versions(RUSTFS_META_BUCKET, entries, &[None, None, None, None], 4, 2),
Err(DiskError::ErasureReadQuorum)
));
}
}
#[test]
fn authoritative_system_bucket_scan_selects_committed_null_version() {
let time = time::OffsetDateTime::from_unix_timestamp(1_700_000_000).expect("valid fixture time");
let old = test_unversioned_object_meta_entry("entry.json", time, "old", Uuid::from_u128(1));
let new = test_unversioned_object_meta_entry("entry.json", time + time::Duration::seconds(1), "new", Uuid::from_u128(2));
for (new_copies, expected) in [(1, "old"), (3, "new"), (4, "new")] {
let entries = MetaCacheEntries(
(0..4)
.map(|slot| Some(if slot < new_copies { new.clone() } else { old.clone() }))
.collect(),
);
let resolved = SetDisks::resolve_listed_versions(RUSTFS_META_BUCKET, entries, &[None, None, None, None], 4, 2)
.expect("committed generation should resolve")
.expect("committed record should be present");
let info = resolved
.to_fileinfo(RUSTFS_META_BUCKET)
.expect("resolved metadata should decode");
assert_eq!(info.metadata.get("etag").map(String::as_str), Some(expected));
}
}
#[test]
fn authoritative_system_bucket_scan_matches_degraded_null_version_reads() {
let time = time::OffsetDateTime::from_unix_timestamp(1_700_000_000).expect("valid fixture time");
let old = test_unversioned_object_meta_entry("entry.json", time, "old", Uuid::from_u128(1));
let new = test_unversioned_object_meta_entry("entry.json", time + time::Duration::seconds(1), "new", Uuid::from_u128(2));
for (old_copies, new_copies, expected) in [
(2, 1, Some("old")),
(1, 2, None),
(0, 3, Some("new")),
(0, 2, Some("new")),
(0, 1, None),
] {
let entries = MetaCacheEntries(
(0..4)
.map(|slot| {
if slot < old_copies {
Some(old.clone())
} else if slot < old_copies + new_copies {
Some(new.clone())
} else {
None
}
})
.collect(),
);
let errors = (0..4)
.map(|slot| (slot >= old_copies + new_copies).then_some(DiskError::DiskNotFound))
.collect::<Vec<_>>();
let resolved = SetDisks::resolve_listed_versions(RUSTFS_META_BUCKET, entries, &errors, 4, 2);
if let Some(expected) = expected {
let info = resolved
.expect("degraded committed record should resolve")
.expect("degraded committed record should be present")
.to_fileinfo(RUSTFS_META_BUCKET)
.expect("resolved metadata should decode");
assert_eq!(info.metadata.get("etag").map(String::as_str), Some(expected));
} else {
assert!(matches!(resolved, Err(DiskError::ErasureReadQuorum)));
}
}
}
#[test]
fn authoritative_user_bucket_scan_keeps_null_version_read_quorum_selection() {
let time = time::OffsetDateTime::from_unix_timestamp(1_700_000_000).expect("valid fixture time");
let old = test_unversioned_object_meta_entry("entry.json", time, "old", Uuid::from_u128(1));
let new = test_unversioned_object_meta_entry("entry.json", time + time::Duration::seconds(1), "new", Uuid::from_u128(2));
for old_copies in [1, 2] {
let entries = MetaCacheEntries(vec![
Some(new.clone()),
Some(new.clone()),
Some(old.clone()),
(old_copies == 2).then(|| old.clone()),
]);
let errors = [None, None, None, (old_copies == 1).then_some(DiskError::DiskNotFound)];
let resolved = SetDisks::resolve_listed_versions("bucket", entries, &errors, 4, 2)
.expect("user bucket read quorum should resolve")
.expect("user bucket record should be present");
let info = resolved.to_fileinfo("bucket").expect("resolved metadata should decode");
assert_eq!(info.metadata.get("etag").map(String::as_str), Some("new"));
}
}
#[test]
fn authoritative_system_bucket_scan_keeps_explicit_version_read_quorum() {
let time = time::OffsetDateTime::from_unix_timestamp(1_700_000_000).expect("valid fixture time");
let entry = test_object_meta_entry_with_erasure_versions("entry.json", &[(time, "version", 2, 2)]);
let entries = MetaCacheEntries(vec![Some(entry.clone()), Some(entry), None, None]);
let errors = [None, None, Some(DiskError::DiskNotFound), Some(DiskError::DiskNotFound)];
let resolved = SetDisks::resolve_listed_versions(RUSTFS_META_BUCKET, entries, &errors, 4, 2)
.expect("explicit version read quorum should resolve")
.expect("explicit version should be present");
let info = resolved
.to_fileinfo(RUSTFS_META_BUCKET)
.expect("resolved metadata should decode");
assert_eq!(info.version_id, Some(Uuid::from_u128(1)));
assert_eq!(info.metadata.get("etag").map(String::as_str), Some("version"));
}
#[test]
fn authoritative_version_scan_tolerates_corrupt_replicas_only_with_version_quorum() {
let entry = test_object_meta_entry_with_erasure_versions(
@@ -6029,6 +6217,49 @@ mod test {
Bytes::from(meta.marshal_msg().expect("encode fixture xl.meta"))
}
#[tokio::test]
async fn authoritative_store_walk_includes_reserved_prefix_versions() {
let bucket = "catalog-cleanup-versions";
let name = ".rustfs-table/namespaces/ns/tables/table/metadata/metadata.json";
let (_dirs, store) = single_set_store(16).await;
let set = &store.pools[0].disk_set[0];
for (slot, disk) in set.disks.read().await.iter().enumerate() {
let disk = disk.as_ref().expect("fixture drive is online");
disk.make_volume(bucket).await.expect("create bucket volume");
disk.write_all(
bucket,
&format!("{name}/{STORAGE_FORMAT_FILE}"),
versioned_object_metadata(bucket, name, &[(2, 1_700_000_002), (1, 1_700_000_001)], slot),
)
.await
.expect("persist reserved metadata versions");
}
let (sender, mut receiver) = mpsc::channel(1);
let walk = store
.clone()
.walk_authoritative(CancellationToken::new(), bucket, "", sender, super::WalkOptions::default());
let collect = async {
let mut versions = Vec::new();
while let Some(item) = receiver.recv().await {
assert!(item.err.is_none(), "reserved metadata must decode: {:?}", item.err);
let item = item.item.expect("walk must return an object");
versions.push((item.name, item.version_id));
}
versions
};
let (walk, versions) = timeout(Duration::from_secs(10), async { tokio::join!(walk, collect) })
.await
.expect("authoritative reserved-prefix walk must terminate");
walk.expect("authoritative walk must succeed");
assert_eq!(
versions,
[
(name.to_string(), Some(Uuid::from_u128(1))),
(name.to_string(), Some(Uuid::from_u128(2)))
]
);
}
// A versioned DeleteObject(versionId) can reach write quorum while three
// drives are offline; they keep the deleted version after they return. A
// different drive going offline later must not turn that sub-quorum
@@ -6099,6 +6330,16 @@ mod test {
.await
.expect("set walk must terminate");
assert!(matches!(walk, Err(StorageError::ErasureReadQuorum)), "{walk:?}");
let (sender, mut receiver) = mpsc::channel(16);
let walk =
store
.clone()
.walk_authoritative(CancellationToken::new(), bucket, "", sender, super::WalkOptions::default());
let drain = async { while receiver.recv().await.is_some() {} };
let (walk, ()) = timeout(Duration::from_secs(10), async { tokio::join!(walk, drain) })
.await
.expect("authoritative store walk must terminate");
assert!(matches!(walk, Err(StorageError::ErasureReadQuorum)), "{walk:?}");
let recursive_delete_scan = set
.clone()
.inner_list_object_versions_for_recursive_delete(bucket, "", None, None, 1000)
+17
View File
@@ -23,6 +23,23 @@ use std::collections::HashSet;
const MULTIPART_LIST_SET_CONCURRENCY: usize = 4;
#[async_trait::async_trait]
impl crate::storage_api_contracts::multipart::MultipartUploadInspection for ECStore {
type Error = Error;
async fn ensure_no_multipart_uploads(&self, bucket: &str) -> Result<()> {
let (_, lifecycle_guard) = self
.guard_multipart_bucket_incarnation(bucket, &ObjectOptions::default())
.await?;
for pool in &self.pools {
for set in &pool.disk_set {
set.ensure_no_multipart_uploads(bucket).await?;
}
}
ensure_multipart_bucket_lifecycle_guard_held(lifecycle_guard.as_ref(), bucket, "")
}
}
#[cfg(test)]
static DATA_MOVEMENT_MULTIPART_DISCOVERY_COUNTS: std::sync::OnceLock<std::sync::Mutex<std::collections::HashMap<Uuid, usize>>> =
std::sync::OnceLock::new();
+2
View File
@@ -90,7 +90,9 @@ pub use bucket::BUCKET_LIFECYCLE_LOCK_OBJECT;
pub use bucket::{BucketInfo, BucketOperations, BucketOptions, DeleteBucketOptions, MakeBucketOptions, SRBucketDeleteOp};
pub use capability::{CapabilitySnapshotError, CapabilityState, CapabilityStatus};
pub use error::{StorageErrorCode, StorageResult};
pub use multipart::MultipartUploadInspection;
pub use multipart::{CompletePart, ListMultipartsInfo, ListPartsInfo, MultipartInfo, MultipartUploadResult, PartInfo};
pub use object::AuthoritativeListOperations;
pub use object::DeleteAccounting;
pub use object::ObjectLockDeleteOptions;
pub use object::ObjectToDeleteIdentity;
+9
View File
@@ -15,6 +15,15 @@
use std::collections::HashMap;
use time::OffsetDateTime;
/// Checks upload absence before removing a bucket's data protections.
#[async_trait::async_trait]
pub trait MultipartUploadInspection: Send + Sync {
type Error: std::error::Error + Send + Sync + 'static;
/// Unknown or unreadable upload ownership must fail this check.
async fn ensure_no_multipart_uploads(&self, bucket: &str) -> Result<(), Self::Error>;
}
#[derive(Debug, Default, Clone)]
pub struct MultipartUploadResult {
pub upload_id: String,
+23
View File
@@ -566,6 +566,29 @@ pub trait ListOperations: Send + Sync + fmt::Debug {
) -> Result<(), Self::Error>;
}
/// Quorum-backed enumeration for decisions that require proving object absence.
///
/// Consumers must drain the result stream and await successful completion before
/// trusting the scan. An error or cancellation can follow partial results and
/// leaves absence unproven. Callers coordinate concurrent mutations separately;
/// this capability does not provide a snapshot or retain a mutation lock.
#[async_trait::async_trait]
pub trait AuthoritativeListOperations: ListOperations {
/// Resolves every set at its version read quorum, including reserved prefixes.
///
/// # Errors
/// Returns an error when a set cannot establish quorum, metadata cannot be
/// resolved, or enumeration is interrupted. Retry starts a new scan.
async fn walk_authoritative(
self: Arc<Self>,
rx: Self::WalkCancellation,
bucket: &str,
prefix: &str,
result: Self::WalkResultSender,
opts: Self::WalkOptions,
) -> Result<(), Self::Error>;
}
#[async_trait::async_trait]
#[allow(clippy::too_many_arguments)]
pub trait MultipartOperations: Send + Sync + fmt::Debug {
+21 -1
View File
@@ -91,6 +91,26 @@ Use `GET /iceberg/v1/buckets/{warehouse}` to check `enabled` and `disable-suppor
Review the bucket lifecycle configuration before disabling: clearing table-bucket protection allows existing object expiration rules to run again. Objects are preserved by the disable operation itself. Catalog disablement requires no namespaces, retained table/view resources, or objects under `.rustfs-table/`; an active rename or backing migration also blocks it. Clean up through supported catalog operations; do not manually delete internal metadata to bypass these checks.
The server persists an inactive catalog entry before clearing the bucket marker. If marker publication fails, retry the same DELETE; the marker retains object protections until the retry succeeds. A subsequent PUT explicitly enables the bucket again. Stale requests cannot initialize an inactive catalog entry. No table metadata or object data is deleted by either state change.
The server persists an inactive catalog entry before clearing the bucket marker. If marker publication fails, retry the same DELETE; the marker retains object protections until the retry succeeds. A subsequent PUT explicitly enables the bucket again. Stale requests cannot initialize an inactive catalog entry. A plain DELETE and a subsequent PUT preserve table metadata and object data.
### Clean Up Deleted Empty Tables
For the object-backed catalog, `DELETE /iceberg/v1/buckets/{warehouse}?cleanup=true` removes the metadata and internal records left by deleted empty tables, then disables the table catalog. Ordinary table deletion continues to preserve files. Cleanup preserves existing table record formats and can process empty tables deleted before the server upgrade.
Complete the server upgrade across all serving nodes and set the existing `RUSTFS_TABLE_CATALOG_PUBLICATION_FENCE_FLEET_CONFIRMED=true` setting before requesting cleanup. Delete the empty tables and their namespaces through the catalog APIs, then send the cleanup request with `admin:SetTableBucket` and `s3:GetObject`/`s3:DeleteObject` permissions for the target metadata objects. The `/_iceberg/v1` alias accepts the same option. Omitting `cleanup` or setting it to `false` keeps the ordinary disable behavior; repeated or invalid values are rejected.
Cleanup accepts only recognized empty-table metadata in the default managed warehouse. Active catalog resources, snapshots or other data references, user objects, object versions, incomplete multipart uploads, corrupt or unknown metadata, and unfinished catalog operations block cleanup. The server validates the complete cleanup plan and object permissions before deleting any object. Durable-strong catalogs retain their existing disable procedure.
If a storage operation fails during cleanup, retry the same request. The server checks the remaining objects again and keeps the bucket's table protection enabled until cleanup and catalog disablement complete. No separate cleanup task file is created. After a successful response with `enabled: false`, delete the empty bucket through the normal S3 `DeleteBucket` operation.
## Object-Backed Registration Reads
Table registration checks table ID uniqueness and warehouse directory overlap using the existing catalog and warehouse index records. Each registration obtains a complete inventory under the existing publication and migration guards. System-bucket inventory records must satisfy the same metadata commit checks as ordinary catalog reads. An incomplete or failed inventory rejects registration.
Each server keeps a bounded memory cache of validated ownership records. A record can be reused only when its object identity and ETag match the current inventory. New or changed records are read and validated again. Cache eviction and process restarts cause additional reads; catalog state continues to come from the existing persisted records. The cache is automatic and requires no additional configuration or stored objects.
Registration still enumerates the namespace and warehouse index prefixes, so enumeration work grows with catalog size. Cache hits reduce object-body reads and JSON parsing. A cache hit verifies the current metadata identity and reuses previously validated content; it does not re-read the current body to check its physical readability. Conditional writes, rollback, and lock-loss fencing continue to control publication.
Existing catalog formats and Console and Iceberg client request formats are preserved. Concurrent writers must follow the existing publication, migration, and storage fencing protocol; the memory cache does not provide synchronization for writers that bypass those guards.
Before rolling back to a server version without this endpoint, re-enable disabled catalogs with PUT while the supporting version is running. Older enable handlers do not reactivate inactive catalog entries. Complete the server upgrade across the cluster before using disablement.
@@ -50,17 +50,23 @@ pub struct DisableTableBucketHandler {}
#[async_trait::async_trait]
impl Operation for DisableTableBucketHandler {
async fn call(&self, req: S3Request<Body>, params: Params<'_, '_>) -> S3Result<S3Response<(StatusCode, Body)>> {
async fn call(&self, mut req: S3Request<Body>, params: Params<'_, '_>) -> S3Result<S3Response<(StatusCode, Body)>> {
let warehouse = warehouse_from_params(&params)?;
let resource = TableCatalogResource::warehouse(&warehouse);
authorize_table_catalog_resource_request(&req, &resource, AdminAction::SetTableBucketAction).await?;
let principal = authorize_table_catalog_resource_request(&req, &resource, AdminAction::SetTableBucketAction).await?;
let cleanup = table_bucket_cleanup_requested_from_query(&req.uri)?;
let app_context = runtime_sources::app_context_from_extensions(&req.extensions);
let backend = table_catalog_backend_from_extensions(&req.extensions)?;
let object_store = runtime_sources::object_store_from_req(&req)
.ok_or_else(|| table_catalog_internal_error("request object store is not initialized"))?;
let store = table_catalog_store_from_backend(backend.clone())?;
let publication = TableCommitObjectBackend::preauthorized(backend);
let response = disable_table_bucket_response(&store, &publication, object_store.as_ref(), &warehouse).await?;
install_table_catalog_s3_request_info(&mut req, &principal)?;
let publication = TableCommitObjectBackend::for_request(backend, req);
let response = if cleanup {
cleanup_empty_table_bucket_response(&store, &publication, object_store.as_ref(), &warehouse).await?
} else {
disable_table_bucket_response(&store, &publication, object_store.as_ref(), &warehouse).await?
};
crate::app::bucket_usecase::notify_table_bucket_metadata_reload(warehouse.clone(), "disable table bucket", app_context)
.await;
build_json_response(StatusCode::OK, &response)
+72 -8
View File
@@ -2092,17 +2092,25 @@ fn warehouse_from_config_query(uri: &http::Uri) -> S3Result<Option<String>> {
}
fn rest_purge_requested_from_query(uri: &http::Uri) -> S3Result<bool> {
let mut purge_requested = None;
table_catalog_boolean_query(uri, "purgeRequested")
}
fn table_bucket_cleanup_requested_from_query(uri: &http::Uri) -> S3Result<bool> {
table_catalog_boolean_query(uri, "cleanup")
}
fn table_catalog_boolean_query(uri: &http::Uri, parameter: &str) -> S3Result<bool> {
let mut requested = None;
if let Some(query) = uri.query() {
for (key, value) in url::form_urlencoded::parse(query.as_bytes()) {
if key != "purgeRequested" {
if key != parameter {
continue;
}
if purge_requested.is_some() {
if requested.is_some() {
return Err(iceberg_rest_error(
ICEBERG_ERROR_BAD_REQUEST,
StatusCode::BAD_REQUEST,
"purgeRequested query parameter must not be repeated",
format!("{parameter} query parameter must not be repeated"),
));
}
let value = if value.eq_ignore_ascii_case("true") {
@@ -2113,13 +2121,13 @@ fn rest_purge_requested_from_query(uri: &http::Uri) -> S3Result<bool> {
return Err(iceberg_rest_error(
ICEBERG_ERROR_BAD_REQUEST,
StatusCode::BAD_REQUEST,
"purgeRequested query parameter must be true or false",
format!("{parameter} query parameter must be true or false"),
));
};
purge_requested = Some(value);
requested = Some(value);
}
}
Ok(purge_requested.unwrap_or(false))
Ok(requested.unwrap_or(false))
}
fn rest_table_snapshot_selection_from_query(uri: &http::Uri) -> S3Result<RestTableSnapshotSelection> {
@@ -2417,9 +2425,10 @@ fn table_catalog_backend_from_extensions(
) -> S3Result<crate::table_catalog::EcStoreTableCatalogObjectBackend<ECStore>> {
let context = runtime_sources::app_context_from_extensions(extensions)
.ok_or_else(|| table_catalog_internal_error("request application context is not initialized"))?;
Ok(crate::table_catalog::EcStoreTableCatalogObjectBackend::new_with_strong_runtime(
Ok(crate::table_catalog::EcStoreTableCatalogObjectBackend::new_with_runtimes(
context.object_store(),
context.table_catalog_strong_runtime(),
context.table_catalog_registration_cache(),
))
}
@@ -2572,6 +2581,61 @@ where
table_bucket_response(store, bucket, true).await
}
async fn cleanup_empty_table_bucket_response<B>(
store: &crate::table_catalog::ConfiguredTableCatalogStore<B>,
publication: &TableCommitObjectBackend<B>,
object_store: &ECStore,
bucket: &str,
) -> S3Result<TableBucketResponse>
where
B: crate::table_catalog::TableCatalogObjectBackend,
{
if !publication.publication_fence_fleet_confirmed {
return Err(catalog_store_error(crate::table_catalog::TableCatalogStoreError::Conflict(
"empty table bucket cleanup requires publication fences on every cluster node".to_string(),
)));
}
crate::table_catalog::TableCommitPublication::begin_table_bucket(publication, bucket)
.await
.map_err(catalog_store_error)?;
let _publication_completion = crate::table_catalog::TableCommitPublicationCompletion::new(publication);
let incarnation = object_store
.bucket_incarnation_id_from_disk(bucket)
.await
.map_err(|err| ApiError::other(format!("failed to read table bucket identity: {err}")))?;
let prepared = store
.prepare_empty_table_bucket_cleanup(table_bucket_entry_from_metadata_marker(bucket), publication)
.await
.map_err(catalog_store_error);
let mut cleanup = publication.finish(prepared).await?;
for object in cleanup.metadata_objects() {
if let Err(err) = publication.authorize(bucket, object, S3Action::DeleteObjectAction).await {
return publication.finish(Err(catalog_store_error(err))).await;
}
}
store
.execute_empty_table_bucket_cleanup(&mut cleanup, incarnation)
.await
.map_err(catalog_store_error)?;
let fence = cleanup.mutation_fence();
object_store
.update_bucket_metadata_config_for_incarnation_validated(
bucket,
crate::table_catalog::TABLE_BUCKET_MARKER_CONFIG,
Vec::new(),
incarnation,
|| {
fence
.ensure_held()
.map_err(|err| crate::admin::storage_api::error::StorageError::other(err.to_string()))
},
)
.await
.map_err(|err| ApiError::other(format!("failed to disable table bucket: {err}")))?;
drop(cleanup);
table_bucket_response(store, bucket, false).await
}
async fn disable_table_bucket_response<S, B>(
store: &S,
publication: &TableCommitObjectBackend<B>,
@@ -87,6 +87,15 @@ async fn call_load_table_handler(request: S3Request<Body>) -> S3Result<S3Respons
RestLoadTableHandler {}.call(request, matched.params).await
}
async fn call_disable_table_bucket_handler(request: S3Request<Body>) -> S3Result<S3Response<(StatusCode, Body)>> {
let mut router = matchit::Router::new();
router.insert("/iceberg/v1/buckets/{warehouse}", ()).unwrap();
router.insert("/_iceberg/v1/buckets/{warehouse}", ()).unwrap();
let path = request.uri.path().to_string();
let matched = router.at(&path).unwrap();
DisableTableBucketHandler {}.call(request, matched.params).await
}
async fn call_maintenance_worker_handler(request: S3Request<Body>) -> S3Result<S3Response<(StatusCode, Body)>> {
let mut router = matchit::Router::new();
router
@@ -1255,6 +1264,28 @@ fn drop_table_purge_query_is_explicit_and_strict() {
}
}
#[test]
fn table_bucket_cleanup_query_is_explicit_and_strict() {
for (uri, expected) in [
("/iceberg/v1/buckets/analytics", false),
("/iceberg/v1/buckets/analytics?cleanup=false", false),
("/iceberg/v1/buckets/analytics?cleanup=true", true),
("/_iceberg/v1/buckets/analytics?cleanup=true", true),
] {
assert_eq!(table_bucket_cleanup_requested_from_query(&uri.parse().unwrap()).unwrap(), expected);
}
for query in [
"cleanup=",
"cleanup=1",
"cleanup=yes",
"cleanup=true&cleanup=false",
"cleanup=true&cleanup=true",
] {
let uri = format!("/iceberg/v1/buckets/analytics?{query}").parse().unwrap();
assert!(table_bucket_cleanup_requested_from_query(&uri).is_err(), "invalid query: {query}");
}
}
#[test]
fn catalog_conflicts_use_operation_specific_iceberg_errors() {
let already_exists = catalog_store_already_exists_error(crate::table_catalog::TableCatalogStoreError::Conflict(
@@ -1689,9 +1720,7 @@ fn table_bucket_toggle_handlers_reload_peer_metadata_after_publication() {
(enable, "enable_table_bucket_response", "enable table bucket"),
(disable, "disable_table_bucket_response", "disable table bucket"),
] {
let response_offset = block
.find(&format!("let response = {response}"))
.expect("table bucket toggle should publish local state");
let response_offset = block.find(response).expect("table bucket toggle should publish local state");
let reload = block
.find(&format!("notify_table_bucket_metadata_reload(warehouse.clone(), \"{operation}\""))
.expect("table bucket toggle should reload peer metadata");
@@ -16274,3 +16303,799 @@ async fn disable_table_bucket_preserves_objects_and_can_be_reenabled() {
.table_bucket_enabled()
);
}
async fn create_dropped_empty_tables_for_cleanup<S, B>(
catalog: &S,
backend: &B,
object_store: &ECStore,
bucket: &str,
table_count: usize,
) -> Vec<String>
where
S: TableCatalogStore + ?Sized,
B: TableCatalogObjectBackend,
{
object_store.make_bucket(bucket, &MakeBucketOptions::default()).await.unwrap();
enable_table_bucket_response(catalog, &TableCommitObjectBackend::trusted(backend.clone()), object_store, bucket)
.await
.unwrap();
let namespace = crate::table_catalog::Namespace::parse("analytics").unwrap();
create_namespace_response(
catalog,
bucket,
CreateNamespaceRequest {
namespace: vec!["analytics".to_string()],
properties: BTreeMap::new(),
},
true,
)
.await
.unwrap();
let mut metadata_objects = Vec::new();
for number in 0..table_count {
let name = format!("events_{number:02}");
let request = serde_json::from_value(serde_json::json!({
"name": name,
"schema": {
"type": "struct",
"schema-id": 0,
"fields": [{"id": 1, "name": "id", "required": true, "type": "long"}]
}
}))
.unwrap();
create_table_response(
catalog,
&TableCommitObjectBackend::trusted(backend.clone()),
bucket,
&namespace,
request,
true,
)
.await
.unwrap();
metadata_objects.push(
catalog
.load_table(bucket, &namespace.public_name(), &name)
.await
.unwrap()
.unwrap()
.metadata_location,
);
}
for number in 0..table_count {
catalog
.drop_table(bucket, &namespace.public_name(), &format!("events_{number:02}"))
.await
.unwrap();
}
catalog.drop_namespace(bucket, &namespace.public_name()).await.unwrap();
metadata_objects
}
#[tokio::test]
#[serial_test::serial]
async fn object_catalog_registration_cache_checks_real_records_without_additional_objects() {
use crate::admin::storage_api::RUSTFS_META_BUCKET;
use crate::table_catalog::{
ConfiguredTableCatalogStore, EcStoreTableCatalogObjectBackend, StrongTableCatalogRuntime, TableCatalogBackingMode,
TableCatalogObjectPaths, TableCatalogStoreError,
};
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("registration-cache-{}", Uuid::new_v4().simple());
let backend =
EcStoreTableCatalogObjectBackend::new_with_strong_runtime(object_store.clone(), StrongTableCatalogRuntime::default());
let catalog = ConfiguredTableCatalogStore::new_for_test(backend.clone(), TableCatalogBackingMode::ObjectBacked);
object_store
.make_bucket(&bucket, &MakeBucketOptions::default())
.await
.unwrap();
enable_table_bucket_response(&catalog, &TableCommitObjectBackend::trusted(backend.clone()), &object_store, &bucket)
.await
.unwrap();
let namespace = crate::table_catalog::Namespace::parse("analytics").unwrap();
create_namespace_response(
&catalog,
&bucket,
CreateNamespaceRequest {
namespace: vec!["analytics".to_string()],
properties: BTreeMap::new(),
},
true,
)
.await
.unwrap();
let paths = TableCatalogObjectPaths::default();
let bucket_entry = paths.table_bucket_entry_path(&bucket);
let catalog_prefix = format!("{}/", bucket_entry.rsplit_once('/').unwrap().0);
let mut create_micros = Vec::new();
for number in 0..100 {
let store = ConfiguredTableCatalogStore::new_for_test(backend.clone(), TableCatalogBackingMode::ObjectBacked);
let request = serde_json::from_value(serde_json::json!({
"name": format!("events_{number:03}"),
"schema": {
"type": "struct",
"schema-id": 0,
"fields": [{"id": 1, "name": "id", "required": true, "type": "long"}]
}
}))
.unwrap();
let started = std::time::Instant::now();
create_table_response(
&store,
&TableCommitObjectBackend::trusted(backend.clone()),
&bucket,
&namespace,
request,
true,
)
.await
.expect("fresh store instances share the runtime cache and create independent tables");
create_micros.push(started.elapsed().as_micros());
if matches!(number + 1, 20 | 100) {
let before = backend.list_objects(RUSTFS_META_BUCKET, &catalog_prefix).await.unwrap();
let cold_backend = EcStoreTableCatalogObjectBackend::new_with_strong_runtime(
object_store.clone(),
StrongTableCatalogRuntime::default(),
);
let probe = ConfiguredTableCatalogStore::new_for_test(cold_backend, TableCatalogBackingMode::ObjectBacked);
let mut duplicate = catalog
.load_table(&bucket, "analytics", &format!("events_{number:03}"))
.await
.unwrap()
.unwrap();
duplicate.table = "duplicate_probe".to_string();
for cache_state in ["cold", "warm"] {
let started = std::time::Instant::now();
assert!(matches!(
probe.create_table(duplicate.clone()).await,
Err(TableCatalogStoreError::Conflict(_))
));
eprintln!(
"registration_check tables={} cache={} elapsed_us={}",
number + 1,
cache_state,
started.elapsed().as_micros()
);
}
assert_eq!(before, backend.list_objects(RUSTFS_META_BUCKET, &catalog_prefix).await.unwrap());
let mut recent = create_micros[create_micros.len() - 5..].to_vec();
recent.sort_unstable();
eprintln!("create_table_response tables={} last_five_median_us={}", number + 1, recent[2]);
}
}
assert_eq!(catalog.list_all_tables(&bucket).await.unwrap().len(), 100);
let namespace_entry = paths.namespace_entry_path(&bucket, &namespace);
let index_prefix = paths.warehouse_index_entries_prefix(&bucket);
let objects = backend.list_objects(RUSTFS_META_BUCKET, &catalog_prefix).await.unwrap();
assert!(objects.iter().all(|key| {
key == &bucket_entry || key == &namespace_entry || key.starts_with(&index_prefix) || key.ends_with("/table-entry.json")
}));
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_reclaims_twenty_dropped_tables_and_allows_bucket_deletion() {
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-{}", Uuid::new_v4().simple());
let backend = crate::table_catalog::EcStoreTableCatalogObjectBackend::new_with_strong_runtime(
object_store.clone(),
crate::table_catalog::StrongTableCatalogRuntime::default(),
);
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let metadata_objects = create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 20).await;
assert!(
disable_table_bucket_response(
&catalog,
&TableCommitObjectBackend::trusted(backend.clone()),
object_store.as_ref(),
&bucket,
)
.await
.is_err()
);
for object in &metadata_objects {
assert!(backend.object_exists(&bucket, object).await.unwrap());
}
let authorizations = Arc::new(tokio::sync::Mutex::new(Vec::new()));
let publication = TableCommitObjectBackend::test(backend.clone(), authorizations.clone(), None);
let result = cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket).await;
assert!(!publication.finish(result).await.unwrap().enabled);
for object in &metadata_objects {
assert!(!backend.object_exists(&bucket, object).await.unwrap());
let authorized = authorizations.lock().await;
assert!(authorized.contains(&(object.clone(), S3Action::GetObjectAction)));
assert!(authorized.contains(&(object.clone(), S3Action::DeleteObjectAction)));
}
assert!(
!object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
let publication = TableCommitObjectBackend::trusted(backend);
assert!(
!cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap()
.enabled
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_validates_authorization_and_metadata_before_deleting() {
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-check-{}", Uuid::new_v4().simple());
let backend = crate::table_catalog::EcStoreTableCatalogObjectBackend::new_with_strong_runtime(
object_store.clone(),
crate::table_catalog::StrongTableCatalogRuntime::default(),
);
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let metadata_objects = create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 2).await;
let publication = TableCommitObjectBackend::rolling_upgrade(backend.clone());
assert!(
cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.is_err()
);
for object in &metadata_objects {
assert!(backend.object_exists(&bucket, object).await.unwrap());
}
let authorizations = Arc::new(tokio::sync::Mutex::new(Vec::new()));
let denied_object = metadata_objects.last().unwrap().clone();
let publication = TableCommitObjectBackend::test(backend.clone(), authorizations, Some(denied_object.clone()));
let result = cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket).await;
assert_eq!(publication.finish(result).await.unwrap_err().code(), &S3ErrorCode::AccessDenied);
for object in &metadata_objects {
assert!(backend.object_exists(&bucket, object).await.unwrap());
}
let original = backend.read_object(&bucket, &denied_object).await.unwrap().unwrap().data;
backend
.put_object(
&bucket,
&denied_object,
b"{".to_vec(),
crate::table_catalog::TableCatalogPutPrecondition::Any,
)
.await
.unwrap();
let publication = TableCommitObjectBackend::trusted(backend.clone());
assert!(
cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.is_err()
);
for object in &metadata_objects {
assert!(backend.object_exists(&bucket, object).await.unwrap());
}
assert!(
object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
backend
.put_object(&bucket, &denied_object, original, crate::table_catalog::TableCatalogPutPrecondition::Any)
.await
.unwrap();
let publication = TableCommitObjectBackend::trusted(backend);
assert!(
!cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap()
.enabled
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_handler_enforces_request_object_permissions() {
temp_env::async_with_vars(
[
(
crate::table_catalog::ENV_TABLE_CATALOG_BACKING,
Some(crate::table_catalog::TABLE_CATALOG_BACKING_OBJECT),
),
(crate::table_catalog::ENV_TABLE_CATALOG_PUBLICATION_FENCE_FLEET_CONFIRMED, Some("true")),
],
async {
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-request-{}", Uuid::new_v4().simple());
rustfs_iam::store::object::ObjectStore::new(object_store.clone())
.save_iam_config(
serde_json::json!({"version": 1}),
format!("{}/format.json", *rustfs_iam::store::object::IAM_CONFIG_PREFIX),
)
.await
.unwrap();
let iam = rustfs_iam::build_iam_sys(object_store.clone()).await.unwrap();
let access_key = "cleanup-request-user";
let secret_key = "cleanup-request-user-secret";
iam.create_user(
access_key,
&AddOrUpdateUserReq {
secret_key: secret_key.to_string(),
policy: None,
status: AccountStatus::Enabled,
},
)
.await
.unwrap();
let context = Arc::new(AppContext::new(
object_store.clone(),
Arc::new(RequestIam { handle: iam.clone() }),
Arc::new(RequestKms),
));
assert!(context.publish_action_credentials(rustfs_credentials::Credentials {
access_key: "cleanup-request-root".to_string(),
secret_key: "cleanup-request-root-secret".to_string(),
status: "on".to_string(),
..Default::default()
}));
let backend = crate::table_catalog::EcStoreTableCatalogObjectBackend::new_with_strong_runtime(
object_store.clone(),
context.table_catalog_strong_runtime(),
);
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let metadata_objects =
create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 2).await;
let request = |prefix: &str| {
let mut request = table_catalog_handler_request(context.clone(), access_key, secret_key);
request.method = Method::DELETE;
request.uri = format!("{prefix}/v1/buckets/{bucket}?cleanup=true").parse().unwrap();
request
};
let object_resource = format!("arn:aws:s3:::{bucket}/*");
let first_metadata_resource = format!("arn:aws:s3:::{bucket}/{}", metadata_objects[0]);
for (policy_name, object_statements, allowed) in [
("cleanup-request-no-read", Vec::new(), false),
(
"cleanup-request-partial-delete",
vec![
serde_json::json!({"Effect":"Allow","Action":["s3:GetObject"],"Resource":[object_resource]}),
serde_json::json!({"Effect":"Allow","Action":["s3:DeleteObject"],"Resource":[first_metadata_resource]}),
],
false,
),
(
"cleanup-request-full",
vec![serde_json::json!({
"Effect":"Allow",
"Action":["s3:GetObject","s3:DeleteObject"],
"Resource":[object_resource],
})],
true,
),
] {
let mut statements = vec![serde_json::json!({"Effect":"Allow","Action":["admin:SetTableBucket"]})];
statements.extend(object_statements);
let policy = serde_json::to_vec(&serde_json::json!({"Version":"2012-10-17","Statement":statements})).unwrap();
iam.set_policy(policy_name, Policy::parse_config(&policy).unwrap())
.await
.unwrap();
iam.policy_db_set(access_key, UserType::Reg, false, policy_name)
.await
.unwrap();
let result = call_disable_table_bucket_handler(request(if allowed { "/_iceberg" } else { "/iceberg" })).await;
if allowed {
let response = result.expect("request-scoped object permissions should authorize cleanup");
assert_eq!(response.output.0, StatusCode::OK);
let body: serde_json::Value = serde_json::from_slice(&response.output.1.bytes().unwrap()).unwrap();
assert_eq!(body["enabled"], false);
for object in &metadata_objects {
assert!(!backend.object_exists(&bucket, object).await.unwrap());
}
} else {
assert_eq!(result.unwrap_err().code(), &S3ErrorCode::AccessDenied);
for object in &metadata_objects {
assert!(backend.object_exists(&bucket, object).await.unwrap());
}
assert!(
object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
assert_eq!(
catalog.get_table_bucket(&bucket).await.unwrap().unwrap().state,
crate::table_catalog::TableCatalogEntryState::Active,
);
}
}
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
},
)
.await;
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_retries_after_marker_write_rejection() {
use crate::table_catalog::TableCommitPublication;
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-marker-{}", Uuid::new_v4().simple());
let backend = TestTableCatalogObjectBackend::default();
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let metadata_objects = create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 2).await;
let publication = TableCommitObjectBackend::trusted(backend.clone());
TableCommitPublication::begin_table_bucket(&publication, &bucket)
.await
.unwrap();
let incarnation = object_store.bucket_incarnation_id_from_disk(&bucket).await.unwrap();
let mut cleanup = catalog
.prepare_empty_table_bucket_cleanup(table_bucket_entry_from_metadata_marker(&bucket), &publication)
.await
.unwrap();
catalog
.execute_empty_table_bucket_cleanup(&mut cleanup, incarnation)
.await
.unwrap();
let marker_result = object_store
.update_bucket_metadata_config_for_incarnation_validated(
&bucket,
crate::table_catalog::TABLE_BUCKET_MARKER_CONFIG,
Vec::new(),
incarnation,
|| {
Err(crate::admin::storage_api::error::StorageError::other(
"injected marker publication rejection",
))
},
)
.await;
assert!(marker_result.is_err());
drop(cleanup);
TableCommitPublication::complete(&publication);
for object in &metadata_objects {
assert!(!backend.object_exists(&bucket, object).await.unwrap());
}
assert_eq!(
catalog.get_table_bucket(&bucket).await.unwrap().unwrap().state,
crate::table_catalog::TableCatalogEntryState::Deleted,
);
assert!(
object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
let publication = TableCommitObjectBackend::trusted(backend);
assert!(
!cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap()
.enabled
);
assert!(
!object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_reads_incarnation_after_publication_wait() {
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-recreated-{}", Uuid::new_v4().simple());
let backend = TestTableCatalogObjectBackend::default();
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 0).await;
let original_incarnation = object_store.bucket_incarnation_id_from_disk(&bucket).await.unwrap();
let publication_lock = crate::table_catalog::default_table_bucket_publication_lock_path();
let guard = backend.acquire_write_lock(&bucket, &publication_lock).await.unwrap();
let attempts = backend.write_lock_acquisition_count(&bucket, &publication_lock).await;
let cleanup_bucket = bucket.clone();
let cleanup_store = object_store.clone();
let publication = TableCommitObjectBackend::trusted(backend.clone());
let cleanup = tokio::spawn(async move {
cleanup_empty_table_bucket_response(&catalog, &publication, cleanup_store.as_ref(), &cleanup_bucket).await
});
tokio::time::timeout(StdDuration::from_secs(5), async {
while backend.write_lock_acquisition_count(&bucket, &publication_lock).await == attempts {
tokio::task::yield_now().await;
}
})
.await
.expect("cleanup should wait for its publication lock");
// Replace the real bucket while the in-memory publication backend holds cleanup at its lock boundary.
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
object_store
.make_bucket(&bucket, &MakeBucketOptions::default())
.await
.unwrap();
enable_table_bucket_marker(object_store.as_ref(), &bucket).await.unwrap();
let replacement_incarnation = object_store.bucket_incarnation_id_from_disk(&bucket).await.unwrap();
assert_ne!(replacement_incarnation, original_incarnation);
drop(guard);
let response = tokio::time::timeout(StdDuration::from_secs(10), cleanup)
.await
.expect("cleanup should resume after publication becomes available")
.unwrap()
.expect("cleanup must use the incarnation observed while holding its publication lock");
assert!(!response.enabled);
assert_eq!(
object_store.bucket_incarnation_id_from_disk(&bucket).await.unwrap(),
replacement_incarnation
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_retries_metadata_delete_failures_without_removing_protection() {
use crate::admin::storage_api::RUSTFS_META_BUCKET;
use crate::table_catalog::{TableCatalogEntryState, TableCatalogObjectPaths};
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
for fail_after_delete in [false, true] {
let bucket = format!("cleanup-retry-{}", Uuid::new_v4().simple());
let backend = TestTableCatalogObjectBackend::content_addressed();
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let mut metadata_objects =
create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 3).await;
metadata_objects.sort();
let paths = TableCatalogObjectPaths::default();
let index_prefix = paths.warehouse_index_entries_prefix(&bucket);
let indexes = backend.list_objects(RUSTFS_META_BUCKET, &index_prefix).await.unwrap();
assert!(indexes.len() >= metadata_objects.len());
if fail_after_delete {
backend.fail_after_next_delete(&bucket, &metadata_objects[1]).await;
} else {
backend.fail_delete_attempt(&bucket, &metadata_objects[1], 1).await;
}
let publication = TableCommitObjectBackend::trusted(backend.clone());
let error = cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap_err();
assert!(error.to_string().contains("injected"));
assert!(!backend.object_exists(&bucket, &metadata_objects[0]).await.unwrap());
assert_eq!(backend.object_exists(&bucket, &metadata_objects[1]).await.unwrap(), !fail_after_delete);
assert!(backend.object_exists(&bucket, &metadata_objects[2]).await.unwrap());
assert_eq!(backend.list_objects(RUSTFS_META_BUCKET, &index_prefix).await.unwrap(), indexes);
assert_eq!(
catalog.get_table_bucket(&bucket).await.unwrap().unwrap().state,
TableCatalogEntryState::Active
);
assert!(
object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
let restarted = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let publication = TableCommitObjectBackend::trusted(backend.clone());
assert!(
!cleanup_empty_table_bucket_response(&restarted, &publication, object_store.as_ref(), &bucket)
.await
.unwrap()
.enabled
);
assert!(backend.list_objects(&bucket, "").await.unwrap().is_empty());
assert!(
backend
.list_objects(RUSTFS_META_BUCKET, &index_prefix)
.await
.unwrap()
.is_empty()
);
assert_eq!(
restarted.get_table_bucket(&bucket).await.unwrap().unwrap().state,
TableCatalogEntryState::Deleted
);
assert!(
!object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
}
#[tokio::test]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_retries_partial_warehouse_index_deletion() {
use crate::admin::storage_api::RUSTFS_META_BUCKET;
use crate::table_catalog::{TableCatalogEntryState, TableCatalogObjectPaths};
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-index-retry-{}", Uuid::new_v4().simple());
let backend = TestTableCatalogObjectBackend::content_addressed();
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 2).await;
let index_prefix = TableCatalogObjectPaths::default().warehouse_index_entries_prefix(&bucket);
let indexes = backend.list_objects(RUSTFS_META_BUCKET, &index_prefix).await.unwrap();
assert!(indexes.len() >= 2);
let last_index = indexes.last().unwrap();
backend.fail_delete_attempt(RUSTFS_META_BUCKET, last_index, 1).await;
let publication = TableCommitObjectBackend::trusted(backend.clone());
let error = cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap_err();
assert!(error.to_string().contains("injected"));
assert!(backend.list_objects(&bucket, "").await.unwrap().is_empty());
assert_eq!(
backend.list_objects(RUSTFS_META_BUCKET, &index_prefix).await.unwrap(),
vec![last_index.clone()]
);
assert_eq!(
catalog.get_table_bucket(&bucket).await.unwrap().unwrap().state,
TableCatalogEntryState::Active
);
assert!(
object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
let publication = TableCommitObjectBackend::trusted(backend.clone());
assert!(
!cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap()
.enabled
);
assert!(
backend
.list_objects(RUSTFS_META_BUCKET, &index_prefix)
.await
.unwrap()
.is_empty()
);
assert!(
!object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial_test::serial]
async fn cleanup_empty_table_bucket_stops_before_delete_when_publication_lease_is_lost() {
use crate::admin::storage_api::RUSTFS_META_BUCKET;
use crate::table_catalog::test_support::RecordingLeaseClient;
use crate::table_catalog::{TableCatalogEntryState, TableCatalogLockGuard, TableCatalogObjectPaths, TableCommitPublication};
use rustfs_lock::LockClient;
let (_temp_dir, _disk_paths, object_store) = crate::app::gating_test_env::isolated_multi_pool_ecstore().await;
let bucket = format!("cleanup-lease-{}", Uuid::new_v4().simple());
let backend = TestTableCatalogObjectBackend::content_addressed();
let catalog = crate::table_catalog::ConfiguredTableCatalogStore::new_for_test(
backend.clone(),
crate::table_catalog::TableCatalogBackingMode::ObjectBacked,
);
let metadata_objects = create_dropped_empty_tables_for_cleanup(&catalog, &backend, object_store.as_ref(), &bucket, 1).await;
let index_prefix = TableCatalogObjectPaths::default().warehouse_index_entries_prefix(&bucket);
let indexes = backend.list_objects(RUSTFS_META_BUCKET, &index_prefix).await.unwrap();
let publication = TableCommitObjectBackend::trusted(backend.clone());
TableCommitPublication::begin_table_bucket(&publication, &bucket)
.await
.unwrap();
let client = Arc::new(RecordingLeaseClient {
inner: rustfs_lock::LocalClient::with_manager(Arc::new(rustfs_lock::GlobalLockManager::Enabled(Arc::new(
rustfs_lock::FastObjectLockManager::new(),
)))),
acquired: std::sync::Mutex::new(None),
});
let lock = rustfs_lock::NamespaceLockWrapper::new(
rustfs_lock::NamespaceLock::with_clients_and_quorum(
"cleanup-publication-fence".to_string(),
vec![client.clone() as Arc<dyn LockClient>],
1,
),
rustfs_lock::ObjectKey::new(bucket.as_str(), "cleanup-publication-fence"),
"cleanup-publication-owner".to_string(),
);
let guard = lock.get_read_lock(StdDuration::from_secs(5)).await.unwrap();
let signal = guard.lock_lost_signal().unwrap();
let lock_id = client.acquired.lock().unwrap().clone().unwrap();
publication
.publication
.lock()
.guards
.push(TableCatalogLockGuard::test_namespace(guard));
let mut cleanup = catalog
.prepare_empty_table_bucket_cleanup(table_bucket_entry_from_metadata_marker(&bucket), &publication)
.await
.unwrap();
let incarnation = object_store.bucket_incarnation_id_from_disk(&bucket).await.unwrap();
let pause = backend.pause_next_delete(&bucket, &metadata_objects[0]).await;
let operation = catalog.execute_empty_table_bucket_cleanup(&mut cleanup, incarnation);
let interfere = async {
tokio::time::timeout(StdDuration::from_secs(5), pause.wait_started())
.await
.unwrap();
assert!(client.force_release(&lock_id).await.unwrap());
tokio::time::timeout(StdDuration::from_secs(15), signal.notified())
.await
.expect("cleanup publication must observe lease loss");
pause.release();
};
let (result, ()) = tokio::join!(operation, interfere);
assert!(matches!(result, Err(crate::table_catalog::TableCatalogStoreError::Unavailable(_))));
drop(cleanup);
TableCommitPublication::complete(&publication);
assert!(backend.object_exists(&bucket, &metadata_objects[0]).await.unwrap());
assert_eq!(backend.list_objects(RUSTFS_META_BUCKET, &index_prefix).await.unwrap(), indexes);
assert_eq!(
catalog.get_table_bucket(&bucket).await.unwrap().unwrap().state,
TableCatalogEntryState::Active
);
assert!(
object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
let publication = TableCommitObjectBackend::trusted(backend.clone());
assert!(
!cleanup_empty_table_bucket_response(&catalog, &publication, object_store.as_ref(), &bucket)
.await
.unwrap()
.enabled
);
assert!(!backend.object_exists(&bucket, &metadata_objects[0]).await.unwrap());
assert!(
!object_store
.get_bucket_metadata(&bucket)
.await
.unwrap()
.table_bucket_enabled()
);
object_store.delete_bucket(&bucket, &Default::default()).await.unwrap();
}
+7
View File
@@ -76,6 +76,7 @@ pub struct AppContext {
object_data_cache: Arc<ObjectDataCacheAdapter>,
object_traffic_health: Arc<ObjectTrafficHealth>,
table_catalog_strong_runtime: crate::table_catalog::StrongTableCatalogRuntime,
table_catalog_registration_cache: crate::table_catalog::TableCatalogRegistrationCache,
}
impl AppContext {
@@ -127,6 +128,7 @@ impl AppContext {
object_data_cache,
object_traffic_health: Arc::new(ObjectTrafficHealth::from_env()),
table_catalog_strong_runtime: crate::table_catalog::StrongTableCatalogRuntime::default(),
table_catalog_registration_cache: crate::table_catalog::TableCatalogRegistrationCache::default(),
}
}
@@ -150,6 +152,10 @@ impl AppContext {
self.table_catalog_strong_runtime.clone()
}
pub(crate) fn table_catalog_registration_cache(&self) -> crate::table_catalog::TableCatalogRegistrationCache {
self.table_catalog_registration_cache.clone()
}
pub fn iam(&self) -> Arc<dyn IamInterface> {
self.iam.clone()
}
@@ -360,6 +366,7 @@ impl AppContext {
object_data_cache: ObjectDataCacheAdapter::disabled_arc(),
object_traffic_health: Arc::new(ObjectTrafficHealth::from_env()),
table_catalog_strong_runtime: crate::table_catalog::StrongTableCatalogRuntime::default(),
table_catalog_registration_cache: crate::table_catalog::TableCatalogRegistrationCache::default(),
}
}
+6 -1
View File
@@ -490,7 +490,8 @@ pub(crate) mod table {
pub(crate) mod list {
pub(crate) use super::super::super::storage_contracts::{
ListObjectVersionsInfo, ListObjectsV2Info, ListOperations, ObjectInfoOrErr, WalkOptions,
AuthoritativeListOperations, ListObjectVersionsInfo, ListObjectsV2Info, ListOperations, ObjectInfoOrErr,
WalkOptions,
};
}
@@ -498,6 +499,10 @@ pub(crate) mod table {
pub(crate) use super::super::super::storage_contracts::NamespaceLocking;
}
pub(crate) mod multipart {
pub(crate) use super::super::super::storage_contracts::MultipartUploadInspection;
}
pub(crate) mod object {
pub(crate) use super::super::super::storage_contracts::{ObjectIO, ObjectOperations};
}
@@ -0,0 +1,342 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
/// Validates one metadata object; callers must also validate the complete object inventory.
pub(crate) fn validate_empty_table_cleanup_metadata(
table_bucket: &str,
object_key: &str,
metadata: &[u8],
) -> TableCatalogStoreResult<()> {
let (namespace, table) = table_identity_from_metadata_object_key(object_key).ok_or_else(|| {
TableCatalogStoreError::Invalid("cleanup metadata must use the default table metadata directory".to_string())
})?;
let metadata = decode_table_metadata_json(object_key, metadata)?;
validate_supported_table_metadata(&metadata)?;
// Future metadata fields may introduce additional object references.
for field in metadata.as_object().expect("validated metadata is a JSON object").keys() {
if !matches!(
field.as_str(),
"format-version"
| "table-uuid"
| "location"
| "last-sequence-number"
| "last-updated-ms"
| "last-column-id"
| "schema"
| "schemas"
| "current-schema-id"
| "partition-spec"
| "partition-specs"
| "default-spec-id"
| "last-partition-id"
| "properties"
| "current-snapshot-id"
| "snapshots"
| "snapshot-log"
| "metadata-log"
| "sort-orders"
| "default-sort-order-id"
| "refs"
| "statistics"
| "partition-statistics"
) {
return Err(TableCatalogStoreError::Unsupported(format!(
"empty table cleanup does not support metadata field {field}"
)));
}
}
for field in ["snapshots", "snapshot-log", "statistics", "partition-statistics"] {
let Some(value) = metadata.get(field) else {
continue;
};
let entries = value
.as_array()
.ok_or_else(|| TableCatalogStoreError::Invalid(format!("cleanup metadata field {field} must be an array")))?;
if !entries.is_empty() {
return Err(TableCatalogStoreError::Conflict(format!(
"empty table cleanup requires metadata field {field} to be empty"
)));
}
}
if let Some(value) = metadata.get("refs") {
let refs = value
.as_object()
.ok_or_else(|| TableCatalogStoreError::Invalid("cleanup metadata refs must be an object".to_string()))?;
if !refs.is_empty() {
return Err(TableCatalogStoreError::Conflict(
"empty table cleanup requires metadata refs to be empty".to_string(),
));
}
}
let table_uuid = table_metadata_uuid(&metadata)?;
if Uuid::parse_str(table_uuid).ok().filter(|uuid| !uuid.is_nil()).is_none() {
return Err(TableCatalogStoreError::Invalid(
"cleanup metadata table-uuid must be a non-nil UUID".to_string(),
));
}
let warehouse_prefix = table_warehouse_object_prefix_from_location(table_bucket, table_metadata_location(&metadata)?)?;
if warehouse_prefix
.strip_prefix("tables/")
.and_then(|prefix| prefix.strip_suffix('/'))
.and_then(|table_id| Uuid::parse_str(table_id).ok())
.filter(|table_id| !table_id.is_nil())
.is_none()
{
return Err(TableCatalogStoreError::Conflict(
"empty table cleanup requires the default tables/<table-id> warehouse location".to_string(),
));
}
if let Some(entries) = metadata.get("metadata-log").and_then(serde_json::Value::as_array) {
for entry in entries {
let entry = entry.as_object().expect("validated metadata log entries are JSON objects");
if entry
.keys()
.any(|field| !matches!(field.as_str(), "timestamp-ms" | "metadata-file"))
{
return Err(TableCatalogStoreError::Unsupported(
"empty table cleanup does not support unknown metadata-log fields".to_string(),
));
}
let location = entry["metadata-file"]
.as_str()
.expect("validated metadata log entries contain metadata-file");
table_catalog_object_key_from_location(table_bucket, location)
.filter(|location| is_valid_table_metadata_location(&namespace, &table, location))
.filter(|location| location != object_key)
.ok_or_else(|| {
TableCatalogStoreError::Invalid(
"cleanup metadata-log must reference another file in the same table metadata directory".to_string(),
)
})?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::table_catalog::test_support::table_metadata_json;
use serde_json::{Value, json};
use std::io::Write;
const BUCKET: &str = "warehouse";
const TABLE_UUID: &str = "f6dd46c2-4971-4d41-b188-452d5f5c2c01";
const WAREHOUSE_LOCATION: &str = "s3://warehouse/tables/a9d8f3b1-abde-4a0b-b6fb-e36a30abff16";
const OBJECT: &str =
".rustfs-table/warehouses/default/namespaces/analytics/events/tables/orders/metadata/00002.metadata.json";
const PREVIOUS: &str =
".rustfs-table/warehouses/default/namespaces/analytics/events/tables/orders/metadata/00001.metadata.json";
fn empty_metadata() -> Value {
table_metadata_json(TABLE_UUID, WAREHOUSE_LOCATION)
}
fn validate(metadata: &Value) -> TableCatalogStoreResult<()> {
validate_empty_table_cleanup_metadata(BUCKET, OBJECT, &serde_json::to_vec(metadata).unwrap())
}
#[test]
fn empty_table_cleanup_accepts_v2_and_same_directory_history() {
let mut metadata = empty_metadata();
metadata["metadata-log"] = json!([
{"timestamp-ms": 1, "metadata-file": PREVIOUS},
{"timestamp-ms": 1, "metadata-file": format!("s3://{BUCKET}/{PREVIOUS}")}
]);
assert!(validate(&metadata).is_ok());
}
#[test]
fn empty_table_cleanup_accepts_v1_without_snapshots() {
let mut metadata = json!({
"format-version": 1,
"table-uuid": TABLE_UUID,
"location": WAREHOUSE_LOCATION,
"last-updated-ms": 1,
"last-column-id": 0,
"schema": {"type": "struct", "fields": []},
"partition-spec": [],
"current-snapshot-id": -1
});
assert!(validate(&metadata).is_ok());
metadata["snapshots"] = json!([{
"snapshot-id": 1,
"timestamp-ms": 1,
"manifests": ["s3://warehouse/tables/table-id/metadata/manifest.avro"]
}]);
validate_supported_table_metadata(&metadata).unwrap();
assert!(matches!(validate(&metadata), Err(TableCatalogStoreError::Conflict(_))));
}
#[test]
fn empty_table_cleanup_rejects_unsupported_versions_and_unknown_references() {
for version in [0, 3, 4] {
let mut metadata = empty_metadata();
metadata["format-version"] = json!(version);
assert!(validate(&metadata).is_err(), "version {version}");
}
for field in [
"manifest-list",
"manifests",
"data-files",
"delete-files",
"next-row-id",
"future-field",
] {
let mut metadata = empty_metadata();
metadata[field] = json!([]);
assert!(validate(&metadata).is_err(), "field {field}");
}
}
#[test]
fn empty_table_cleanup_rejects_snapshot_and_history_references() {
let mut metadata = empty_metadata();
metadata["snapshots"] = json!([{
"snapshot-id": 1,
"sequence-number": 0,
"timestamp-ms": 1,
"manifest-list": "s3://warehouse/tables/table-id/metadata/list.avro",
"summary": {"operation": "append"}
}]);
validate_supported_table_metadata(&metadata).unwrap();
assert!(matches!(validate(&metadata), Err(TableCatalogStoreError::Conflict(_))));
let mut metadata = empty_metadata();
metadata["snapshot-log"] = json!([{"timestamp-ms": 1, "snapshot-id": 1}]);
assert!(validate(&metadata).is_err());
metadata = empty_metadata();
metadata["refs"] = json!({"main": {"snapshot-id": 1, "type": "branch"}});
assert!(validate(&metadata).is_err());
metadata = empty_metadata();
metadata["current-snapshot-id"] = json!(1);
assert!(validate(&metadata).is_err());
}
#[test]
fn empty_table_cleanup_accepts_only_absent_null_or_minus_one_current_snapshot() {
for version in [1, 2] {
let mut metadata = empty_metadata();
metadata["format-version"] = json!(version);
if version == 1 {
metadata["schema"] = metadata["schemas"][0].clone();
metadata["partition-spec"] = json!([]);
}
assert!(validate(&metadata).is_ok());
for value in [Value::Null, json!(-1)] {
metadata["current-snapshot-id"] = value;
assert!(validate(&metadata).is_ok(), "version {version}");
}
for value in [json!(1), json!(0), json!(-2), json!("1"), json!(false), json!([])] {
metadata["current-snapshot-id"] = value;
assert!(validate(&metadata).is_err(), "version {version}");
}
}
}
#[test]
fn empty_table_cleanup_rejects_statistics_references_and_malformed_empty_fields() {
for field in ["snapshots", "snapshot-log", "statistics", "partition-statistics"] {
for value in [Value::Null, json!({}), json!("empty"), json!([{}])] {
let mut metadata = empty_metadata();
metadata[field] = value;
assert!(validate(&metadata).is_err(), "field {field}");
}
}
for value in [Value::Null, json!([]), json!("empty")] {
let mut metadata = empty_metadata();
metadata["refs"] = value;
assert!(validate(&metadata).is_err());
}
}
#[test]
fn empty_table_cleanup_rejects_unknown_or_cross_table_history() {
for location in [
format!("s3://other/{PREVIOUS}"),
PREVIOUS.replace("/orders/", "/other/"),
PREVIOUS.replace("/analytics/events/", "/another/"),
PREVIOUS.replace("00001.metadata.json", "../00001.metadata.json"),
PREVIOUS.replace("00001.metadata.json", "%2e%2e.json"),
format!("{PREVIOUS}?versionId=previous"),
format!("/{PREVIOUS}"),
OBJECT.to_string(),
] {
let mut metadata = empty_metadata();
metadata["metadata-log"] = json!([{"timestamp-ms": 1, "metadata-file": location}]);
assert!(validate(&metadata).is_err(), "location {location}");
}
let mut metadata = empty_metadata();
metadata["metadata-log"] = json!([{"timestamp-ms": 1, "metadata-file": PREVIOUS, "future-file": "data.parquet"}]);
assert!(validate(&metadata).is_err());
}
#[test]
fn empty_table_cleanup_requires_valid_schema_uuid_and_warehouse() {
for uuid in ["table-uuid", "00000000-0000-0000-0000-000000000000", ""] {
let mut metadata = empty_metadata();
metadata["table-uuid"] = json!(uuid);
assert!(validate(&metadata).is_err());
}
for location in [
"s3://other/tables/table-id",
"s3://warehouse/",
"s3://warehouse/.rustfs-table/metadata",
"file:///table",
"s3://warehouse/custom/table-id",
"s3://warehouse/tables/table-id",
"s3://warehouse/tables/00000000-0000-0000-0000-000000000000",
"s3://warehouse/tables/a9d8f3b1-abde-4a0b-b6fb-e36a30abff16/nested",
] {
let mut metadata = empty_metadata();
metadata["location"] = json!(location);
assert!(validate(&metadata).is_err());
}
let mut metadata = empty_metadata();
metadata["schemas"] = json!([]);
assert!(validate(&metadata).is_err());
}
#[test]
fn empty_table_cleanup_rejects_non_default_metadata_paths() {
let bytes = serde_json::to_vec(&empty_metadata()).unwrap();
for object in [
OBJECT.replace("/default/", "/custom/"),
OBJECT.replace("/tables/", "/views/"),
OBJECT.replace("/metadata/", "/data/"),
OBJECT.replace("00002.metadata.json", "part.parquet"),
OBJECT.replace("00002.metadata.json", "nested/00002.metadata.json"),
format!("s3://{BUCKET}/{OBJECT}"),
] {
assert!(validate_empty_table_cleanup_metadata(BUCKET, &object, &bytes).is_err(), "object {object}");
}
}
#[test]
fn empty_table_cleanup_decodes_gzip_and_rejects_invalid_json() {
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
encoder.write_all(&serde_json::to_vec(&empty_metadata()).unwrap()).unwrap();
let bytes = encoder.finish().unwrap();
let object = OBJECT.replace(".metadata.json", ".gz.metadata.json");
assert!(validate_empty_table_cleanup_metadata(BUCKET, &object, &bytes).is_ok());
assert!(validate_empty_table_cleanup_metadata(BUCKET, OBJECT, b"{}").is_err());
assert!(validate_empty_table_cleanup_metadata(BUCKET, OBJECT, b"[").is_err());
assert!(validate_empty_table_cleanup_metadata(BUCKET, &object, b"invalid gzip").is_err());
}
}
+11 -4
View File
@@ -28,9 +28,11 @@ use std::{
use crate::storage_api::table::contract::http::HTTPPreconditions;
use crate::storage_api::table::contract::list::{
ListObjectVersionsInfo as StorageListObjectVersionsInfo, ListObjectsV2Info as StorageListObjectsV2Info,
ListOperations as StorageListOperations, ObjectInfoOrErr as StorageObjectInfoOrErr, WalkOptions as StorageWalkOptions,
AuthoritativeListOperations as StorageAuthoritativeListOperations, ListObjectVersionsInfo as StorageListObjectVersionsInfo,
ListObjectsV2Info as StorageListObjectsV2Info, ListOperations as StorageListOperations,
ObjectInfoOrErr as StorageObjectInfoOrErr, WalkOptions as StorageWalkOptions,
};
use crate::storage_api::table::contract::multipart::MultipartUploadInspection as StorageMultipartUploadInspection;
use crate::storage_api::table::contract::namespace::NamespaceLocking as StorageNamespaceLocking;
use crate::storage_api::table::contract::object::{ObjectIO as StorageObjectIO, ObjectOperations as StorageObjectOperations};
use crate::storage_api::table::contract::range::HTTPRangeSpec;
@@ -58,6 +60,7 @@ use crate::storage_api::table::{
StorageObjectOptions as ObjectOptions, StorageObjectToDelete as ObjectToDelete, StoragePutObjReader as PutObjReader,
};
mod cleanup_metadata;
mod error;
mod iceberg;
mod identifier;
@@ -246,7 +249,9 @@ pub(crate) trait TableCatalogStorage:
WalkOptions = CatalogWalkOptions,
WalkCancellation = tokio_util::sync::CancellationToken,
WalkResultSender = tokio::sync::mpsc::Sender<CatalogObjectInfoOrErr>,
> + StorageNamespaceLocking<Error = EcstoreError, NamespaceLock = rustfs_lock::NamespaceLockWrapper>
> + StorageAuthoritativeListOperations
+ StorageNamespaceLocking<Error = EcstoreError, NamespaceLock = rustfs_lock::NamespaceLockWrapper>
+ StorageMultipartUploadInspection<Error = EcstoreError>
{
}
@@ -274,7 +279,9 @@ impl<T> TableCatalogStorage for T where
WalkOptions = CatalogWalkOptions,
WalkCancellation = tokio_util::sync::CancellationToken,
WalkResultSender = tokio::sync::mpsc::Sender<CatalogObjectInfoOrErr>,
> + StorageNamespaceLocking<Error = EcstoreError, NamespaceLock = rustfs_lock::NamespaceLockWrapper>
> + StorageAuthoritativeListOperations
+ StorageNamespaceLocking<Error = EcstoreError, NamespaceLock = rustfs_lock::NamespaceLockWrapper>
+ StorageMultipartUploadInspection<Error = EcstoreError>
{
}
+267
View File
@@ -0,0 +1,267 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::object::{
next_table_catalog_update_time, table_warehouse_index_state_ready, validate_table_warehouse_index_entry_object,
};
use super::*;
pub(super) struct TableBucketDisablement {
pub(super) entry: TableBucketEntry,
guards: Vec<TableCatalogLockGuard>,
}
/// An in-memory, fenced cleanup plan. No mutation occurs until every object has
/// been validated and the caller has authorized all metadata deletions.
pub(crate) struct EmptyTableBucketCleanup {
disablement: TableBucketDisablement,
metadata: Vec<(String, String)>,
indexes: Vec<(String, String)>,
fence: TableCatalogObjectMutationFence,
}
impl EmptyTableBucketCleanup {
pub(crate) fn metadata_objects(&self) -> impl Iterator<Item = &str> {
self.metadata.iter().map(|(key, _)| key.as_str())
}
pub(crate) fn ensure_held(&self) -> TableCatalogStoreResult<()> {
self.fence.ensure_held()
}
pub(crate) fn mutation_fence(&self) -> TableCatalogObjectMutationFence {
self.fence.clone()
}
}
fn cleanup_conflict(message: impl Into<String>) -> TableCatalogStoreError {
TableCatalogStoreError::Conflict(message.into())
}
impl<B: TableCatalogObjectBackend> ObjectTableCatalogStore<B> {
pub(super) async fn begin_table_bucket_disablement(
&self,
mut entry: TableBucketEntry,
publication: Option<&dyn TableCommitPublication>,
) -> TableCatalogStoreResult<TableBucketDisablement> {
validate_table_bucket_entry(&entry)?;
let mut guards = Vec::new();
if publication.is_some_and(|publication| publication.catalog_migration_read_permit_status() == Some(true)) {
if self
.backend
.read_object(self.catalog_bucket(), &self.paths.backing_migration_global_fence_path())
.await?
.is_some()
{
return Err(cleanup_conflict("table bucket registry migration is in progress"));
}
self.ensure_object_backed_catalog_write_permit_after_lock(&entry.table_bucket)
.await?;
} else {
guards.push(self.acquire_table_bucket_registry_write_permit().await?);
guards.push(self.acquire_object_backed_catalog_write_permit(&entry.table_bucket).await?);
}
let object = self.paths.table_bucket_entry_path(&entry.table_bucket);
guards.push(self.backend.acquire_write_lock(self.catalog_bucket(), &object).await?);
if let Some((current, _)) = self.read_table_bucket_with_etag_unlocked(&entry.table_bucket).await? {
if current.active_rename_id.is_some()
|| !matches!(current.state, TableCatalogEntryState::Active | TableCatalogEntryState::Deleted)
{
return Err(cleanup_conflict("table bucket has an operation in progress"));
}
entry = current;
}
Ok(TableBucketDisablement { entry, guards })
}
pub(super) async fn finish_table_bucket_disablement(
&self,
disablement: &mut TableBucketDisablement,
) -> TableCatalogStoreResult<()> {
let mut entry = disablement.entry.clone();
entry.state = TableCatalogEntryState::Deleted;
entry.updated_at = Some(next_table_catalog_update_time(entry.updated_at.as_deref()));
self.write_entry_unlocked(
self.catalog_bucket(),
&self.paths.table_bucket_entry_path(&entry.table_bucket),
&entry,
TableCatalogPutPrecondition::Any,
disablement
.guards
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
)
.await
}
async fn prepare_empty_table_bucket_cleanup(
&self,
entry: TableBucketEntry,
publication: &impl TableCatalogObjectBackend,
) -> TableCatalogStoreResult<EmptyTableBucketCleanup> {
if !publication.holds_table_bucket(&entry.table_bucket) {
return Err(cleanup_conflict("empty table bucket cleanup requires a publication fence"));
}
let disablement = self.begin_table_bucket_disablement(entry, Some(publication)).await?;
let bucket = &disablement.entry.table_bucket;
let mut fence = publication
.object_mutation_fence()
.ok_or_else(|| cleanup_conflict("cleanup requires storage fencing"))?;
fence
.signals
.extend(disablement.guards.iter().filter_map(TableCatalogLockGuard::lock_lost_signal));
fence.ensure_held()?;
let catalog_objects = self
.backend
.list_objects_for_cleanup(self.catalog_bucket(), &self.paths.table_bucket_root_prefix(bucket))
.await?;
let bucket_path = self.paths.table_bucket_entry_path(bucket);
let index_state_path = self.paths.warehouse_index_state_path(bucket);
let index_prefix = self.paths.warehouse_index_entries_prefix(bucket);
let mut indexes = Vec::new();
for object in catalog_objects {
if object.delete_marker
|| object
.version_id
.as_deref()
.is_some_and(|version| !version.is_empty() && version != "null")
{
return Err(cleanup_conflict("catalog contains versioned or deleted records"));
}
if object.key == bucket_path {
continue;
}
let etag = if object.key == index_state_path {
let (state, etag) = self
.read_entry::<TableWarehouseIndexStateEntry>(self.catalog_bucket(), &object.key)
.await?
.ok_or_else(|| cleanup_conflict("warehouse index state changed during cleanup"))?;
table_warehouse_index_state_ready(&state, bucket)?;
if !matches!(state.state, TableCatalogEntryState::Active | TableCatalogEntryState::Deleted) {
return Err(cleanup_conflict("warehouse index has an operation in progress"));
}
etag
} else if object.key.starts_with(&index_prefix) {
let (index, etag) = self
.read_entry::<TableWarehouseIndexEntry>(self.catalog_bucket(), &object.key)
.await?
.ok_or_else(|| cleanup_conflict("warehouse index changed during cleanup"))?;
validate_table_warehouse_index_entry_object(&self.paths, &object.key, &index)?;
if index.table_bucket != *bucket || index.state != TableCatalogEntryState::Deleted {
return Err(cleanup_conflict("table bucket contains an active warehouse index"));
}
Namespace::parse(&index.namespace).map_err(|err| cleanup_conflict(err.to_string()))?;
IdentifierSegment::parse(index.table.clone()).map_err(|err| cleanup_conflict(err.to_string()))?;
if Uuid::parse_str(&index.table_id).ok().filter(|id| !id.is_nil()).is_none() {
return Err(cleanup_conflict("warehouse index table identity is invalid"));
}
let expected = format!("tables/{}/", index.table_id);
if index.warehouse_object_prefix != expected {
return Err(cleanup_conflict("cleanup requires the default table warehouse location"));
}
etag
} else {
return Err(cleanup_conflict("table bucket contains namespaces or retained catalog resources"));
};
let etag = etag.ok_or_else(|| cleanup_conflict("catalog cleanup object has no etag"))?;
indexes.push((object.key, etag));
}
self.backend.ensure_no_multipart_uploads(bucket).await?;
let mut metadata = Vec::new();
for object in self.backend.list_objects_for_cleanup(bucket, "").await? {
if object.delete_marker
|| object
.version_id
.as_deref()
.is_some_and(|version| !version.is_empty() && version != "null")
{
return Err(cleanup_conflict("table bucket contains object versions or delete markers"));
}
let value = publication
.read_object_limited(bucket, &object.key, TABLE_METADATA_JSON_MAX_SIZE)
.await?
.ok_or_else(|| cleanup_conflict("metadata object changed during cleanup"))?;
crate::table_catalog::cleanup_metadata::validate_empty_table_cleanup_metadata(bucket, &object.key, &value.data)?;
let etag = value
.etag
.ok_or_else(|| cleanup_conflict("metadata cleanup object has no etag"))?;
metadata.push((object.key, etag));
}
fence.ensure_held()?;
Ok(EmptyTableBucketCleanup {
disablement,
metadata,
indexes,
fence,
})
}
async fn execute_empty_table_bucket_cleanup(
&self,
cleanup: &mut EmptyTableBucketCleanup,
incarnation: Uuid,
) -> TableCatalogStoreResult<()> {
let bucket = &cleanup.disablement.entry.table_bucket;
for (key, etag) in &cleanup.metadata {
cleanup.ensure_held()?;
self.backend
.delete_object_if_match_fenced_for_incarnation(bucket, key, etag, incarnation, &cleanup.fence)
.await?;
}
// A retry inventories the remaining metadata. Keep warehouse protection
// until all user objects and uploads have been checked again.
if !self.backend.list_objects_for_cleanup(bucket, "").await?.is_empty() {
return Err(cleanup_conflict("table bucket changed during cleanup"));
}
self.backend.ensure_no_multipart_uploads(bucket).await?;
for (key, etag) in &cleanup.indexes {
cleanup.ensure_held()?;
self.backend
.delete_object_if_match_fenced(self.catalog_bucket(), key, etag, &cleanup.fence)
.await?;
}
cleanup.ensure_held()?;
self.finish_table_bucket_disablement(&mut cleanup.disablement).await
}
}
impl<B: TableCatalogObjectBackend> ConfiguredTableCatalogStore<B> {
pub(crate) async fn prepare_empty_table_bucket_cleanup(
&self,
entry: TableBucketEntry,
publication: &impl TableCatalogObjectBackend,
) -> TableCatalogStoreResult<EmptyTableBucketCleanup> {
match self {
Self::ObjectBacked(store) => store.prepare_empty_table_bucket_cleanup(entry, publication).await,
Self::DurableStrong(_) => Err(TableCatalogStoreError::Unsupported(
"empty table bucket cleanup requires object-backed catalog storage".to_string(),
)),
}
}
pub(crate) async fn execute_empty_table_bucket_cleanup(
&self,
cleanup: &mut EmptyTableBucketCleanup,
incarnation: Uuid,
) -> TableCatalogStoreResult<()> {
match self {
Self::ObjectBacked(store) => store.execute_empty_table_bucket_cleanup(cleanup, incarnation).await,
Self::DurableStrong(_) => Err(TableCatalogStoreError::Unsupported(
"empty table bucket cleanup requires object-backed catalog storage".to_string(),
)),
}
}
}
+202 -3
View File
@@ -17,8 +17,13 @@ use super::*;
use crate::storage_api::table::{WriteCommitGuard, WriteCompletion};
mod backup;
mod cleanup;
mod migration;
mod object;
mod registration_cache;
#[cfg(test)]
mod registration_cache_tests;
pub(crate) use registration_cache::TableCatalogRegistrationCache;
mod strong;
pub(crate) use backup::{TableCatalogBackupReport, TableCatalogRestoreReport};
@@ -775,6 +780,14 @@ pub(crate) struct TableCatalogObjectListPage {
pub is_truncated: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TableCatalogCleanupObject {
pub key: String,
pub version_id: Option<String>,
pub etag: Option<String>,
pub delete_marker: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TableCatalogPutPrecondition {
Any,
@@ -803,6 +816,10 @@ pub(in crate::table_catalog) fn catalog_list_next_continuation(
#[async_trait::async_trait]
pub(crate) trait TableCatalogObjectBackend: Clone + Send + Sync + 'static {
fn registration_cache(&self) -> Option<TableCatalogRegistrationCache> {
None
}
fn strong_catalog_runtime(&self) -> Option<StrongTableCatalogRuntime> {
None
}
@@ -918,6 +935,30 @@ pub(crate) trait TableCatalogObjectBackend: Clone + Send + Sync + 'static {
fence.ensure_held()
}
async fn put_object_unlocked_fenced(
&self,
bucket: &str,
object: &str,
data: Vec<u8>,
precondition: TableCatalogPutPrecondition,
_write_guards: Vec<WriteCommitGuard>,
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
self.put_object_fenced(bucket, object, data, precondition, fence).await
}
async fn delete_object_unlocked_fenced(
&self,
bucket: &str,
object: &str,
_write_guards: Vec<WriteCommitGuard>,
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
fence.ensure_held()?;
self.delete_object_unlocked(bucket, object).await?;
fence.ensure_held()
}
async fn delete_object(&self, bucket: &str, object: &str) -> TableCatalogStoreResult<()>;
async fn delete_object_if_match(&self, bucket: &str, object: &str, expected_etag: &str) -> TableCatalogStoreResult<()>;
@@ -934,12 +975,41 @@ pub(crate) trait TableCatalogObjectBackend: Clone + Send + Sync + 'static {
fence.ensure_held()
}
async fn delete_object_if_match_fenced_for_incarnation(
&self,
_bucket: &str,
_object: &str,
_expected_etag: &str,
_expected_incarnation_id: Uuid,
_fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
Err(TableCatalogStoreError::Unsupported(
"incarnation-checked cleanup is not supported by this backend".to_string(),
))
}
async fn delete_object_unlocked(&self, bucket: &str, object: &str) -> TableCatalogStoreResult<()> {
self.delete_object(bucket, object).await
}
async fn list_objects(&self, bucket: &str, prefix: &str) -> TableCatalogStoreResult<Vec<String>>;
async fn list_objects_for_cleanup(
&self,
_bucket: &str,
_prefix: &str,
) -> TableCatalogStoreResult<Vec<TableCatalogCleanupObject>> {
Err(TableCatalogStoreError::Unsupported(
"authoritative cleanup listing is not supported by this backend".to_string(),
))
}
async fn ensure_no_multipart_uploads(&self, _bucket: &str) -> TableCatalogStoreResult<()> {
Err(TableCatalogStoreError::Unsupported(
"multipart cleanup validation is not supported by this backend".to_string(),
))
}
async fn list_objects_page(
&self,
bucket: &str,
@@ -2204,6 +2274,7 @@ where
pub(crate) struct EcStoreTableCatalogObjectBackend<S> {
store: Arc<S>,
strong_runtime: StrongTableCatalogRuntime,
registration_cache: TableCatalogRegistrationCache,
}
impl<S> Clone for EcStoreTableCatalogObjectBackend<S> {
@@ -2211,6 +2282,7 @@ impl<S> Clone for EcStoreTableCatalogObjectBackend<S> {
Self {
store: self.store.clone(),
strong_runtime: self.strong_runtime.clone(),
registration_cache: self.registration_cache.clone(),
}
}
}
@@ -2220,7 +2292,19 @@ where
S: TableCatalogStorage,
{
pub fn new_with_strong_runtime(store: Arc<S>, strong_runtime: StrongTableCatalogRuntime) -> Self {
Self { store, strong_runtime }
Self::new_with_runtimes(store, strong_runtime, TableCatalogRegistrationCache::default())
}
pub fn new_with_runtimes(
store: Arc<S>,
strong_runtime: StrongTableCatalogRuntime,
registration_cache: TableCatalogRegistrationCache,
) -> Self {
Self {
store,
strong_runtime,
registration_cache,
}
}
}
@@ -2231,6 +2315,10 @@ impl<S> TableCatalogObjectBackend for EcStoreTableCatalogObjectBackend<S>
where
S: TableCatalogStorage,
{
fn registration_cache(&self) -> Option<TableCatalogRegistrationCache> {
Some(self.registration_cache.clone())
}
fn strong_catalog_runtime(&self) -> Option<StrongTableCatalogRuntime> {
Some(self.strong_runtime.clone())
}
@@ -2390,6 +2478,46 @@ where
.await
}
async fn put_object_unlocked_fenced(
&self,
bucket: &str,
object: &str,
data: Vec<u8>,
precondition: TableCatalogPutPrecondition,
write_guards: Vec<WriteCommitGuard>,
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
fence.ensure_held()?;
self.put_object_with_options(bucket, object, data, precondition, Some(write_guards), Some(fence))
.await
}
async fn delete_object_unlocked_fenced(
&self,
bucket: &str,
object: &str,
write_guards: Vec<WriteCommitGuard>,
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
fence.ensure_held()?;
if write_guards.is_empty() {
return Err(TableCatalogStoreError::Internal("catalog delete has no namespace owner".to_string()));
}
let mut options = ObjectOptions {
no_lock: true,
..Default::default()
};
fence.add_to_object_options(&mut options);
for guard in &write_guards {
options.add_write_commit_guard(guard);
}
match self.store.delete_object(bucket, object, options).await {
Ok(_) => fence.ensure_held(),
Err(err) if is_missing_storage_error(&err) => fence.ensure_held(),
Err(err) => Err(storage_error_to_catalog("delete fenced catalog object", err)),
}
}
async fn put_object_fenced(
&self,
bucket: &str,
@@ -2412,7 +2540,7 @@ where
}
async fn delete_object_if_match(&self, bucket: &str, object: &str, expected_etag: &str) -> TableCatalogStoreResult<()> {
self.delete_object_if_match_with_fence(bucket, object, expected_etag, None)
self.delete_object_if_match_with_fence(bucket, object, expected_etag, None, None)
.await
}
@@ -2424,7 +2552,25 @@ where
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
fence.ensure_held()?;
self.delete_object_if_match_with_fence(bucket, object, expected_etag, Some(fence))
self.delete_object_if_match_with_fence(bucket, object, expected_etag, Some(fence), None)
.await
}
async fn delete_object_if_match_fenced_for_incarnation(
&self,
bucket: &str,
object: &str,
expected_etag: &str,
expected_incarnation_id: Uuid,
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
if expected_incarnation_id.is_nil() {
return Err(TableCatalogStoreError::Invalid(
"cleanup requires a non-nil bucket incarnation".to_string(),
));
}
fence.ensure_held()?;
self.delete_object_if_match_with_fence(bucket, object, expected_etag, Some(fence), Some(expected_incarnation_id))
.await
}
@@ -2498,6 +2644,57 @@ where
})
}
async fn list_objects_for_cleanup(
&self,
bucket: &str,
prefix: &str,
) -> TableCatalogStoreResult<Vec<TableCatalogCleanupObject>> {
let cancel = tokio_util::sync::CancellationToken::new();
let _cancel_on_drop = cancel.clone().drop_guard();
let (sender, mut receiver) = tokio::sync::mpsc::channel::<CatalogObjectInfoOrErr>(100);
let walk = self.store.clone().walk_authoritative(
cancel,
bucket,
prefix,
sender,
CatalogWalkOptions {
include_free_versions: true,
..Default::default()
},
);
let collect = async move {
let mut objects = Vec::new();
while let Some(result) = receiver.recv().await {
if let Some(err) = result.err {
return Err(storage_error_to_catalog("inspect cleanup object versions", err));
}
let object = result.item.ok_or_else(|| {
TableCatalogStoreError::Internal("cleanup object walk returned an empty result".to_string())
})?;
objects.push(TableCatalogCleanupObject {
key: object.name,
version_id: object.version_id.filter(|id| !id.is_nil()).map(|id| id.to_string()),
etag: object.etag,
delete_marker: object.delete_marker,
});
}
Ok(objects)
};
let (walk_result, objects) = tokio::join!(walk, collect);
let objects = objects?;
walk_result.map_err(|err| storage_error_to_catalog("list cleanup object versions", err))?;
Ok(objects)
}
async fn ensure_no_multipart_uploads(&self, bucket: &str) -> TableCatalogStoreResult<()> {
self.store.ensure_no_multipart_uploads(bucket).await.map_err(|err| match err {
StorageError::BucketNotEmpty(_) => {
TableCatalogStoreError::Conflict(format!("table bucket {bucket} contains unfinished multipart uploads"))
}
other => storage_error_to_catalog("inspect unfinished multipart uploads", other),
})
}
async fn acquire_write_lock(&self, bucket: &str, object: &str) -> TableCatalogStoreResult<TableCatalogLockGuard> {
let lock = self
.store
@@ -2534,12 +2731,14 @@ where
object: &str,
expected_etag: &str,
fence: Option<&TableCatalogObjectMutationFence>,
expected_incarnation_id: Option<Uuid>,
) -> TableCatalogStoreResult<()> {
let mut options = ObjectOptions {
http_preconditions: Some(HTTPPreconditions {
if_match: Some(expected_etag.to_string()),
..Default::default()
}),
expected_bucket_incarnation_id: expected_incarnation_id,
..Default::default()
};
if let Some(fence) = fence {
+154 -94
View File
@@ -13,7 +13,9 @@
// limitations under the License.
use super::*;
use futures::{StreamExt, TryStreamExt, stream};
pub(super) const TABLE_REGISTRATION_READ_CONCURRENCY: usize = 8;
const TABLE_MAINTENANCE_RENAME_MAX_OBJECTS: usize = 4096;
const TABLE_MAINTENANCE_SCHEDULER_HISTORY_MAX_OBJECTS: usize = 4096;
@@ -261,7 +263,7 @@ fn validate_table_rename_intent_object(
Ok(())
}
fn next_table_catalog_update_time(previous: Option<&str>) -> String {
pub(super) fn next_table_catalog_update_time(previous: Option<&str>) -> String {
let now = OffsetDateTime::now_utc();
let update_time = previous
.and_then(|value| OffsetDateTime::parse(value, &time::format_description::well_known::Rfc3339).ok())
@@ -360,7 +362,7 @@ pub(super) fn validate_table_maintenance_report_owner(
Ok(())
}
fn validate_table_warehouse_index_entry_object(
pub(super) fn validate_table_warehouse_index_entry_object(
paths: &TableCatalogObjectPaths,
object: &str,
index: &TableWarehouseIndexEntry,
@@ -374,7 +376,10 @@ fn validate_table_warehouse_index_entry_object(
Ok(())
}
fn table_warehouse_index_state_ready(state: &TableWarehouseIndexStateEntry, table_bucket: &str) -> TableCatalogStoreResult<bool> {
pub(super) fn table_warehouse_index_state_ready(
state: &TableWarehouseIndexStateEntry,
table_bucket: &str,
) -> TableCatalogStoreResult<bool> {
if state.version == 0 || state.version > TABLE_WAREHOUSE_INDEX_STATE_VERSION {
return Err(TableCatalogStoreError::Invalid(format!(
"unsupported warehouse index state version: {}",
@@ -1217,7 +1222,7 @@ where
.await
}
async fn write_entry_unlocked<T>(
pub(super) async fn write_entry_unlocked<T>(
&self,
bucket: &str,
object: &str,
@@ -1225,14 +1230,39 @@ where
precondition: TableCatalogPutPrecondition,
write_guards: Vec<WriteCommitGuard>,
) -> TableCatalogStoreResult<()>
where
T: Serialize,
{
self.write_entry_unlocked_with_fence(bucket, object, entry, precondition, write_guards, None)
.await
}
async fn write_entry_unlocked_with_fence<T>(
&self,
bucket: &str,
object: &str,
entry: &T,
precondition: TableCatalogPutPrecondition,
write_guards: Vec<WriteCommitGuard>,
fence: Option<&TableCatalogObjectMutationFence>,
) -> TableCatalogStoreResult<()>
where
T: Serialize,
{
let data = serde_json::to_vec(entry)
.map_err(|err| TableCatalogStoreError::Internal(format!("failed to serialize catalog entry {object}: {err}")))?;
self.backend
.put_object_unlocked(bucket, object, data, precondition, write_guards)
.await
match fence {
Some(fence) => {
self.backend
.put_object_unlocked_fenced(bucket, object, data, precondition, write_guards, fence)
.await
}
None => {
self.backend
.put_object_unlocked(bucket, object, data, precondition, write_guards)
.await
}
}
}
async fn write_exact_entry_unlocked<T>(
@@ -1243,11 +1273,27 @@ where
precondition: TableCatalogPutPrecondition,
write_guards: Vec<WriteCommitGuard>,
) -> TableCatalogStoreResult<String>
where
T: DeserializeOwned + PartialEq + Serialize,
{
self.write_exact_entry_unlocked_with_fence(bucket, object, entry, precondition, write_guards, None)
.await
}
async fn write_exact_entry_unlocked_with_fence<T>(
&self,
bucket: &str,
object: &str,
entry: &T,
precondition: TableCatalogPutPrecondition,
write_guards: Vec<WriteCommitGuard>,
fence: Option<&TableCatalogObjectMutationFence>,
) -> TableCatalogStoreResult<String>
where
T: DeserializeOwned + PartialEq + Serialize,
{
let write_result = self
.write_entry_unlocked(bucket, object, entry, precondition, write_guards)
.write_entry_unlocked_with_fence(bucket, object, entry, precondition, write_guards, fence)
.await;
let current = self.read_entry_unlocked::<T>(bucket, object).await?;
match current {
@@ -1264,7 +1310,7 @@ where
}
}
async fn read_table_bucket_with_etag_unlocked(
pub(super) async fn read_table_bucket_with_etag_unlocked(
&self,
table_bucket: &str,
) -> TableCatalogStoreResult<Option<(TableBucketEntry, String)>> {
@@ -1387,6 +1433,7 @@ where
"table rename recovery requires a table-bucket publication fence".to_string(),
));
}
let mutation_fence = publication.object_mutation_fence().unwrap_or_default();
let bucket_object = self.paths.table_bucket_entry_path(table_bucket);
let Some((observed_bucket, _)) = self
.read_entry::<TableBucketEntry>(self.catalog_bucket(), &bucket_object)
@@ -1511,7 +1558,7 @@ where
{
Some((current, _)) if current == source_fence || current == source_tombstone => {}
Some((current, current_etag)) if current == intent.source && current_etag == intent.source_etag => {
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&source_object,
&source_fence,
@@ -1520,6 +1567,7 @@ where
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
Some(&mutation_fence),
)
.await?;
}
@@ -1549,7 +1597,7 @@ where
{
Some((current, _)) if current == destination_fence || current == intent.destination => {}
None if intent.destination_etag.is_none() => {
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&destination_object,
&destination_fence,
@@ -1558,6 +1606,7 @@ where
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
Some(&mutation_fence),
)
.await?;
}
@@ -1565,7 +1614,7 @@ where
if current.state == TableCatalogEntryState::Deleted
&& Some(current_etag.as_str()) == intent.destination_etag.as_deref() =>
{
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&destination_object,
&destination_fence,
@@ -1574,6 +1623,7 @@ where
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
Some(&mutation_fence),
)
.await?;
}
@@ -1601,7 +1651,7 @@ where
{
Some((current, _)) if current == source_tombstone => {}
Some((current, current_etag)) if current == source_fence => {
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&source_object,
&source_tombstone,
@@ -1610,6 +1660,7 @@ where
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
Some(&mutation_fence),
)
.await?;
}
@@ -1647,7 +1698,7 @@ where
"table rename warehouse index changed during recovery".to_string(),
));
}
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&index_object,
&destination_index,
@@ -1656,6 +1707,7 @@ where
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
Some(&mutation_fence),
)
.await?;
}
@@ -1674,7 +1726,7 @@ where
{
Some((current, _)) if current == intent.destination => {}
Some((current, current_etag)) if current == destination_fence => {
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&destination_object,
&intent.destination,
@@ -1683,6 +1735,7 @@ where
.iter()
.flat_map(TableCatalogLockGuard::write_commit_guards)
.collect(),
Some(&mutation_fence),
)
.await?;
}
@@ -1720,12 +1773,13 @@ where
}
bucket_entry.active_rename_id = None;
bucket_entry.updated_at = Some(next_table_catalog_update_time(bucket_entry.updated_at.as_deref()));
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&bucket_object,
&bucket_entry,
TableCatalogPutPrecondition::IfMatch(bucket_etag),
_bucket_guard.write_commit_guards(),
Some(&mutation_fence),
)
.await?;
Ok(())
@@ -1873,7 +1927,12 @@ where
let candidate = table_warehouse_index_entry(entry)?;
validate_table_entry_version_and_id(entry)?;
for existing in self
.list_all_table_entries_with_limit(&candidate.table_bucket, None, locked_table_entry)
.list_all_table_entries_with_limit(
&candidate.table_bucket,
None,
locked_table_entry,
TABLE_REGISTRATION_READ_CONCURRENCY,
)
.await?
{
if existing.state != TableCatalogEntryState::Active {
@@ -1896,34 +1955,38 @@ where
}
}
let state_object = self.paths.warehouse_index_state_path(&candidate.table_bucket);
for object in self
let index_objects = self
.backend
.list_objects(self.catalog_bucket(), &self.paths.warehouse_index_entries_prefix(&candidate.table_bucket))
.await?
{
if object == state_object {
continue;
}
let Some((existing, _)) = self
.read_entry::<TableWarehouseIndexEntry>(self.catalog_bucket(), &object)
.await?
else {
continue;
};
validate_table_warehouse_index_entry_object(&self.paths, &object, &existing)?;
if existing.table_id == candidate.table_id || existing.state != TableCatalogEntryState::Active {
continue;
}
if warehouse_object_prefixes_overlap(&existing.warehouse_object_prefix, &candidate.warehouse_object_prefix)
&& self.warehouse_index_entry_has_active_owner(&existing).await?
{
return Err(TableCatalogStoreError::Conflict(format!(
"table warehouse location overlaps an active table: {}",
candidate.warehouse_object_prefix
)));
}
}
Ok(())
.await?;
stream::iter(index_objects.into_iter().filter(|object| object != &state_object))
.map(|object| {
let candidate = &candidate;
async move {
let Some((existing, _)) = self
.read_entry::<TableWarehouseIndexEntry>(self.catalog_bucket(), &object)
.await?
else {
return Ok(());
};
validate_table_warehouse_index_entry_object(&self.paths, &object, &existing)?;
if existing.table_id == candidate.table_id || existing.state != TableCatalogEntryState::Active {
return Ok(());
}
if warehouse_object_prefixes_overlap(&existing.warehouse_object_prefix, &candidate.warehouse_object_prefix)
&& self.warehouse_index_entry_has_active_owner(&existing).await?
{
return Err(TableCatalogStoreError::Conflict(format!(
"table warehouse location overlaps an active table: {}",
candidate.warehouse_object_prefix
)));
}
Ok::<_, TableCatalogStoreError>(())
}
})
.buffered(TABLE_REGISTRATION_READ_CONCURRENCY)
.try_for_each(|()| async { Ok(()) })
.await
}
async fn reserve_table_warehouse_index(
@@ -2364,7 +2427,7 @@ where
) -> TableCatalogStoreResult<Option<TableDataPlaneResource>> {
let mut matched: Option<TableDataPlaneResource> = None;
for table in self
.list_all_table_entries_with_limit(table_bucket, Some(TABLE_DATA_PLANE_INDEX_MISS_SCAN_MAX_CATALOG_OBJECTS), None)
.list_all_table_entries_with_limit(table_bucket, Some(TABLE_DATA_PLANE_INDEX_MISS_SCAN_MAX_CATALOG_OBJECTS), None, 1)
.await?
{
if table.state != TableCatalogEntryState::Active {
@@ -2456,7 +2519,7 @@ where
return Ok(());
}
let tables = self
.list_all_table_entries_with_limit(table_bucket, max_catalog_objects, None)
.list_all_table_entries_with_limit(table_bucket, max_catalog_objects, None, 1)
.await?
.into_iter()
.filter(|table| table.state == TableCatalogEntryState::Active)
@@ -2537,7 +2600,7 @@ where
}
async fn list_all_table_entries(&self, table_bucket: &str) -> TableCatalogStoreResult<Vec<TableEntry>> {
self.list_all_table_entries_with_limit(table_bucket, None, None).await
self.list_all_table_entries_with_limit(table_bucket, None, None, 1).await
}
async fn list_table_entry_objects_for_data_plane_scan(
@@ -2586,8 +2649,8 @@ where
table_bucket: &str,
max_catalog_objects: Option<usize>,
locked_table_entry: Option<&str>,
read_concurrency: usize,
) -> TableCatalogStoreResult<Vec<TableEntry>> {
let mut entries = Vec::new();
let table_objects = match max_catalog_objects {
Some(max_catalog_objects) => {
self.list_table_entry_objects_for_data_plane_scan(table_bucket, max_catalog_objects)
@@ -2601,19 +2664,24 @@ where
.filter(|object| object.ends_with(TABLE_ENTRY_FILE))
.collect(),
};
for object in table_objects {
// Only the exact entry protected by the caller's write lock can bypass a read lock.
let entry = if locked_table_entry == Some(object.as_str()) {
self.read_entry_unlocked::<TableEntry>(self.catalog_bucket(), &object).await?
} else {
self.read_entry::<TableEntry>(self.catalog_bucket(), &object).await?
};
let Some((entry, _)) = entry else {
continue;
};
validate_table_entry_object(&self.paths, &object, &entry)?;
entries.push(entry);
}
let mut entries = stream::iter(table_objects)
.map(|object| async move {
// Only the exact entry protected by the caller's write lock can bypass a read lock.
let entry = if locked_table_entry == Some(object.as_str()) {
self.read_entry_unlocked::<TableEntry>(self.catalog_bucket(), &object).await?
} else {
self.read_entry::<TableEntry>(self.catalog_bucket(), &object).await?
};
let Some((entry, _)) = entry else {
return Ok(None);
};
validate_table_entry_object(&self.paths, &object, &entry)?;
Ok::<_, TableCatalogStoreError>(Some(entry))
})
.buffered(read_concurrency)
.try_filter_map(|entry| async move { Ok(entry) })
.try_collect::<Vec<_>>()
.await?;
entries.sort_by(|left, right| (&left.namespace, &left.table).cmp(&(&right.namespace, &right.table)));
Ok(entries)
}
@@ -2772,8 +2840,7 @@ where
"table registration requires a table publication fence".to_string(),
));
}
self.ensure_table_warehouse_prefix_available(&entry, Some(&table_path))
.await?;
self.check_registration_ownership(&entry, &table_path, publication).await?;
let reservation = self.reserve_table_warehouse_index(&entry, true, &table_path).await?;
if !publication.holds_table_bucket(&entry.table_bucket)
|| !publication.holds_table(&entry.table_bucket, &entry.namespace, &entry.table)
@@ -2784,20 +2851,23 @@ where
"table registration publication fence was lost before catalog update".to_string(),
));
}
let mutation_fence = publication.object_mutation_fence().unwrap_or_default();
let result = self
.write_entry_unlocked(
.write_entry_unlocked_with_fence(
self.catalog_bucket(),
&table_path,
&entry,
precondition,
_table_guard.write_commit_guards(),
Some(&mutation_fence),
)
.await;
if result.is_err() {
self.rollback_table_warehouse_index_reservation(&entry, reservation, "table entry write failed")
.await;
}
result
result?;
Ok(())
}
async fn write_view_entry(&self, entry: ViewEntry, precondition: TableCatalogPutPrecondition) -> TableCatalogStoreResult<()> {
@@ -6258,27 +6328,15 @@ where
.await
}
async fn disable_empty_table_bucket(&self, mut entry: TableBucketEntry) -> TableCatalogStoreResult<()> {
validate_table_bucket_entry(&entry)?;
let _registry_guard = self.acquire_table_bucket_registry_write_permit().await?;
let _migration_guard = self.acquire_object_backed_catalog_write_permit(&entry.table_bucket).await?;
let object = self.paths.table_bucket_entry_path(&entry.table_bucket);
let _guard = self.backend.acquire_write_lock(self.catalog_bucket(), &object).await?;
if let Some((current, _)) = self.read_table_bucket_with_etag_unlocked(&entry.table_bucket).await? {
if current.active_rename_id.is_some()
|| !matches!(current.state, TableCatalogEntryState::Active | TableCatalogEntryState::Deleted)
{
return Err(TableCatalogStoreError::Conflict("table bucket has an operation in progress".to_string()));
}
entry = current;
}
async fn disable_empty_table_bucket(&self, entry: TableBucketEntry) -> TableCatalogStoreResult<()> {
let mut disablement = self.begin_table_bucket_disablement(entry, None).await?;
// Namespace creation holds this same bucket lock. Retained table, view, and
// maintenance records also prevent removing their data-plane protections.
let page = self
.backend
.list_objects_page(
self.catalog_bucket(),
&self.paths.namespace_entries_prefix(&entry.table_bucket),
&self.paths.namespace_entries_prefix(&disablement.entry.table_bucket),
None,
NonZeroUsize::MIN,
)
@@ -6288,16 +6346,7 @@ where
"table bucket contains namespaces or retained catalog resources".to_string(),
));
}
entry.state = TableCatalogEntryState::Deleted;
entry.updated_at = Some(next_table_catalog_update_time(entry.updated_at.as_deref()));
self.write_entry_unlocked(
self.catalog_bucket(),
&object,
&entry,
TableCatalogPutPrecondition::Any,
_guard.write_commit_guards(),
)
.await
self.finish_table_bucket_disablement(&mut disablement).await
}
async fn create_namespace(&self, entry: NamespaceEntry) -> TableCatalogStoreResult<()> {
@@ -6681,6 +6730,7 @@ where
));
}
let _publication_completion = TableCommitPublicationCompletion::new(&publication);
let mutation_fence = publication.object_mutation_fence().unwrap_or_default();
self.recover_active_table_rename(table_bucket, &publication).await?;
{
@@ -6820,12 +6870,13 @@ where
));
}
None => {
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&index_object,
&source_index,
TableCatalogPutPrecondition::IfAbsent,
_index_guard.write_commit_guards(),
Some(&mutation_fence),
)
.await?
}
@@ -6846,22 +6897,24 @@ where
validate_table_rename_intent_object(&self.paths, &intent_object, &intent)?;
// An orphan intent has no catalog effect; a published bucket fence without its intent cannot be recovered safely.
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&intent_object,
&intent,
TableCatalogPutPrecondition::IfAbsent,
_intent_guard.write_commit_guards(),
Some(&mutation_fence),
)
.await?;
bucket_entry.active_rename_id = Some(rename_id);
bucket_entry.updated_at = Some(now);
self.write_exact_entry_unlocked(
self.write_exact_entry_unlocked_with_fence(
self.catalog_bucket(),
&bucket_object,
&bucket_entry,
TableCatalogPutPrecondition::IfMatch(bucket_etag),
_bucket_guard.write_commit_guards(),
Some(&mutation_fence),
)
.await?;
}
@@ -7311,13 +7364,15 @@ where
}
let cas_started = Instant::now();
let mutation_fence = publication.object_mutation_fence().unwrap_or_default();
let cas_result = self
.write_entry_unlocked(
.write_entry_unlocked_with_fence(
self.catalog_bucket(),
&table_path,
&next,
TableCatalogPutPrecondition::IfMatch(current_etag),
_guard.write_commit_guards(),
Some(&mutation_fence),
)
.await;
record_table_commit_cas_result(&request.operation, cas_started, &cas_result);
@@ -7388,6 +7443,7 @@ where
table.as_str()
)));
};
let mutation_fence = publication.object_mutation_fence().unwrap_or_default();
self.tombstone_table_warehouse_index_for_drop(&entry, false).await?;
if !publication.holds_table_bucket(table_bucket)
|| !publication.holds_table(table_bucket, &namespace.public_name(), table.as_str())
@@ -7398,7 +7454,11 @@ where
"table drop publication fence was lost before catalog update".to_string(),
));
}
if let Err(err) = self.backend.delete_object_unlocked(self.catalog_bucket(), &object).await {
if let Err(err) = self
.backend
.delete_object_unlocked_fenced(self.catalog_bucket(), &object, _guard.write_commit_guards(), &mutation_fence)
.await
{
match self.read_table_with_etag_unlocked(table_bucket, &namespace, &table).await {
Ok(None) => return Ok(()),
Ok(Some((current, _))) if current == entry => {
@@ -0,0 +1,298 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::object::{
TABLE_REGISTRATION_READ_CONCURRENCY, table_warehouse_index_state_ready, validate_table_entry_object,
validate_table_warehouse_index_entry_object,
};
use super::*;
use futures::{StreamExt, TryStreamExt, stream};
use std::sync::OnceLock;
const REGISTRATION_CACHE_BYTES: u64 = 16 * 1024 * 1024;
#[derive(Clone)]
struct CachedOwner {
etag: String,
owner: Arc<TableWarehouseIndexEntry>,
}
/// Runtime-local object projections. Every lookup requires a fresh committed inventory.
#[derive(Clone)]
pub(crate) struct TableCatalogRegistrationCache {
entries: Arc<OnceLock<moka::sync::Cache<String, CachedOwner>>>,
capacity_bytes: u64,
}
impl Default for TableCatalogRegistrationCache {
fn default() -> Self {
Self::with_capacity(REGISTRATION_CACHE_BYTES)
}
}
impl TableCatalogRegistrationCache {
fn with_capacity(bytes: u64) -> Self {
Self {
entries: Arc::new(OnceLock::new()),
capacity_bytes: bytes,
}
}
fn entries(&self) -> &moka::sync::Cache<String, CachedOwner> {
self.entries.get_or_init(|| {
moka::sync::Cache::builder()
.max_capacity(self.capacity_bytes)
.weigher(|key: &String, value: &CachedOwner| {
let owner = &value.owner;
let bytes = [
key.len(),
value.etag.len(),
owner.table_bucket.len(),
owner.namespace.len(),
owner.table.len(),
owner.table_id.len(),
owner.warehouse_object_prefix.len(),
]
.into_iter()
.fold(256_usize, usize::saturating_add);
u32::try_from(bytes).unwrap_or(u32::MAX)
})
.build()
})
}
fn get(&self, object: &TableCatalogCleanupObject) -> Option<Arc<TableWarehouseIndexEntry>> {
let etag = object.etag.as_deref().filter(|etag| !etag.is_empty())?;
self.entries
.get()?
.get(&object.key)
.filter(|cached| cached.etag == etag)
.map(|cached| cached.owner)
}
fn insert(&self, object: String, etag: Option<String>, owner: Arc<TableWarehouseIndexEntry>) {
if let Some(etag) = etag.filter(|etag| !etag.is_empty()) {
self.entries().insert(object, CachedOwner { etag, owner });
}
}
#[cfg(test)]
pub(in crate::table_catalog) fn clear(&self) {
if let Some(entries) = self.entries.get() {
entries.invalidate_all();
entries.run_pending_tasks();
}
}
}
fn validate_inventory(objects: &mut [TableCatalogCleanupObject], prefix: &str) -> TableCatalogStoreResult<()> {
objects.sort_unstable_by(|left, right| left.key.cmp(&right.key));
if objects.iter().any(|object| {
!object.key.starts_with(prefix)
|| object.delete_marker
|| object
.version_id
.as_deref()
.is_some_and(|version| !version.is_empty() && version != "null")
}) || objects.windows(2).any(|pair| pair[0].key == pair[1].key)
{
return Err(TableCatalogStoreError::Invalid(
"registration requires an unversioned, complete catalog inventory".to_string(),
));
}
Ok(())
}
impl<B: TableCatalogObjectBackend> ObjectTableCatalogStore<B> {
async fn registration_owner(
&self,
object: TableCatalogCleanupObject,
table_record: bool,
locked_table_entry: &str,
cache: Option<&TableCatalogRegistrationCache>,
) -> TableCatalogStoreResult<Arc<TableWarehouseIndexEntry>> {
if let Some(owner) = cache.and_then(|cache| cache.get(&object)) {
return Ok(owner);
}
let (owner, etag) = if table_record {
let value = if object.key == locked_table_entry {
self.read_entry_unlocked::<TableEntry>(self.catalog_bucket(), &object.key)
.await?
} else {
self.read_entry::<TableEntry>(self.catalog_bucket(), &object.key).await?
};
let (entry, etag) = value.ok_or_else(|| {
TableCatalogStoreError::Unavailable("table record changed during registration inventory".to_string())
})?;
validate_table_entry_object(&self.paths, &object.key, &entry)?;
(table_warehouse_index_entry(&entry)?, etag)
} else {
let (entry, etag) = self
.read_entry::<TableWarehouseIndexEntry>(self.catalog_bucket(), &object.key)
.await?
.ok_or_else(|| {
TableCatalogStoreError::Unavailable("warehouse index changed during registration inventory".to_string())
})?;
validate_table_warehouse_index_entry_object(&self.paths, &object.key, &entry)?;
(entry, etag)
};
let owner = Arc::new(owner);
if let Some(cache) = cache {
// A concurrent index repair may change its ETag after enumeration.
// Associate the projection only with the object that GET validated.
cache.insert(object.key, etag, Arc::clone(&owner));
}
Ok(owner)
}
pub(super) async fn check_registration_ownership(
&self,
entry: &TableEntry,
locked_table_entry: &str,
publication: &(dyn TableCommitPublication + Sync),
) -> TableCatalogStoreResult<()> {
if !publication.holds_table_bucket(&entry.table_bucket) {
return Err(TableCatalogStoreError::Unavailable("registration publication fence was lost".to_string()));
}
validate_table_entry_version_and_id(entry)?;
let candidate = table_warehouse_index_entry(entry)?;
let namespace_prefix = self.paths.namespace_entries_prefix(&entry.table_bucket);
let index_prefix = self.paths.warehouse_index_entries_prefix(&entry.table_bucket);
// The backend must preserve system-bucket committed-read semantics as
// well as completeness; ordinary S3 LIST is insufficient for cache hits.
let mut tables = self
.backend
.list_objects_for_cleanup(self.catalog_bucket(), &namespace_prefix)
.await?;
let mut indexes = self
.backend
.list_objects_for_cleanup(self.catalog_bucket(), &index_prefix)
.await?;
validate_inventory(&mut tables, &namespace_prefix)?;
validate_inventory(&mut indexes, &index_prefix)?;
let cache = self.backend.registration_cache();
let mut owners = BTreeMap::new();
let mut table_ids = BTreeSet::new();
let mut table_reads = stream::iter(tables.into_iter().filter(|object| object.key.ends_with(TABLE_ENTRY_FILE)))
.map(|object| self.registration_owner(object, true, locked_table_entry, cache.as_ref()))
.buffered(TABLE_REGISTRATION_READ_CONCURRENCY);
while let Some(owner) = table_reads.try_next().await? {
if owner.state != TableCatalogEntryState::Active {
continue;
}
if !table_ids.insert(owner.table_id.clone())
|| (owner.table_id == candidate.table_id
&& (owner.namespace != candidate.namespace || owner.table != candidate.table))
{
return Err(TableCatalogStoreError::Conflict(
"table id is already registered in this table bucket".to_string(),
));
}
if owner.table_id != candidate.table_id
&& warehouse_object_prefixes_overlap(&owner.warehouse_object_prefix, &candidate.warehouse_object_prefix)
{
return Err(TableCatalogStoreError::Conflict(
"table warehouse location overlaps an active table".to_string(),
));
}
owners.insert((owner.namespace.clone(), owner.table.clone()), owner);
}
let mut prefixes = owners
.values()
.map(|owner| owner.warehouse_object_prefix.as_str())
.collect::<Vec<_>>();
prefixes.sort_unstable();
if prefixes
.windows(2)
.any(|pair| warehouse_object_prefixes_overlap(pair[0], pair[1]))
{
return Err(TableCatalogStoreError::Conflict("active table warehouse locations overlap".to_string()));
}
let state_path = self.paths.warehouse_index_state_path(&entry.table_bucket);
let has_state = indexes.iter().any(|object| object.key == state_path);
let mut index_reads = stream::iter(indexes.into_iter().filter(|object| object.key != state_path))
.map(|object| self.registration_owner(object, false, locked_table_entry, cache.as_ref()))
.buffered(TABLE_REGISTRATION_READ_CONCURRENCY);
while let Some(index) = index_reads.try_next().await? {
if index.table_id == candidate.table_id || index.state != TableCatalogEntryState::Active {
continue;
}
if warehouse_object_prefixes_overlap(&index.warehouse_object_prefix, &candidate.warehouse_object_prefix) {
parse_namespace_for_store(&index.namespace)?;
parse_table_for_store(&index.table)?;
if owners
.get(&(index.namespace.clone(), index.table.clone()))
.is_some_and(|owner| {
owner.table_id == index.table_id && owner.warehouse_object_prefix == index.warehouse_object_prefix
})
{
return Err(TableCatalogStoreError::Conflict(
"table warehouse location overlaps an active table".to_string(),
));
}
}
}
if has_state {
let (state, _) = self
.read_entry::<TableWarehouseIndexStateEntry>(self.catalog_bucket(), &state_path)
.await?
.ok_or_else(|| {
TableCatalogStoreError::Unavailable("warehouse index state changed during registration inventory".to_string())
})?;
table_warehouse_index_state_ready(&state, &entry.table_bucket)?;
}
if !publication.holds_table_bucket(&entry.table_bucket) {
return Err(TableCatalogStoreError::Unavailable("registration publication fence was lost".to_string()));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::super::registration_cache_tests::{cache_fixture, registration_entry};
use super::*;
#[tokio::test]
async fn registration_cache_capacity_and_oversized_owners_preserve_checks() {
let (backend, _, namespace) = cache_fixture().await;
let cache = TableCatalogRegistrationCache::with_capacity(1024);
let backend = crate::table_catalog::test_support::TestCatalogObjectBackend {
registration_cache: cache.clone(),
..backend
};
let store = ObjectTableCatalogStore::new(backend.clone());
let mut oversized = registration_entry(&namespace, "oversized");
oversized.warehouse_location = format!("s3://analytics/{}", "directory".repeat(512));
store.create_table(oversized.clone()).await.unwrap();
for number in 0..12 {
store
.create_table(registration_entry(&namespace, &format!("table_{number}")))
.await
.unwrap();
}
cache.entries().run_pending_tasks();
assert!(cache.entries().weighted_size() <= 1024);
backend.reset_call_counts().await;
let mut candidate = registration_entry(&namespace, "conflicting");
candidate.warehouse_location = format!("{}/child", oversized.warehouse_location);
assert!(matches!(store.create_table(candidate).await, Err(TableCatalogStoreError::Conflict(_))));
assert!(
backend.read_call_count().await > 0,
"eviction must cause reads instead of accepting an absent owner"
);
cache.entries().run_pending_tasks();
assert!(cache.entries().weighted_size() <= 1024);
}
}
@@ -0,0 +1,376 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
use crate::table_catalog::test_support::{RecordingLeaseClient, TestCatalogObjectBackend};
use crate::table_catalog::tests::{test_bucket_entry, test_namespace_entry, test_table_entry};
use rustfs_lock::LockClient;
use std::sync::{Arc, Mutex};
use std::time::Duration;
pub(super) fn registration_entry(namespace: &Namespace, name: &str) -> TableEntry {
let table = IdentifierSegment::parse(name).expect("valid test table name");
let mut entry = test_table_entry(
"analytics",
namespace,
&table,
default_table_metadata_file_path(namespace, &table, "00001.metadata.json"),
);
entry.table_id = format!("id-{name}");
entry.warehouse_location = format!("s3://analytics/tables/id-{name}");
entry
}
pub(super) async fn cache_fixture() -> (TestCatalogObjectBackend, ObjectTableCatalogStore<TestCatalogObjectBackend>, Namespace) {
let backend = TestCatalogObjectBackend::default();
let store = ObjectTableCatalogStore::new(backend.clone());
let namespace = Namespace::parse("sales").unwrap();
store.put_table_bucket(test_bucket_entry("analytics")).await.unwrap();
store
.create_namespace(test_namespace_entry("analytics", &namespace))
.await
.unwrap();
store.create_table(registration_entry(&namespace, "orders")).await.unwrap();
store.create_table(registration_entry(&namespace, "warmup")).await.unwrap();
(backend, store, namespace)
}
#[tokio::test]
async fn registration_cache_binds_projection_to_the_get_etag() {
let (backend, store, namespace) = cache_fixture().await;
backend.registration_cache.clear();
let path = store
.paths
.table_entry_path("analytics", &namespace, &IdentifierSegment::parse("orders").unwrap());
let key = (RUSTFS_META_BUCKET.to_string(), path.clone());
let original = backend.state.lock().await.objects.get(&key).unwrap().clone();
let pause = backend.pause_before_next_read(RUSTFS_META_BUCKET, &path).await;
let task_store = store.clone();
let entry = registration_entry(&namespace, "unrelated");
let task = tokio::spawn(async move { task_store.create_table(entry).await });
tokio::time::timeout(Duration::from_secs(5), pause.wait_started())
.await
.unwrap();
let mut changed = registration_entry(&namespace, "orders");
changed.warehouse_location = "s3://analytics/changed".to_string();
backend
.seed_object(RUSTFS_META_BUCKET, &path, serde_json::to_vec(&changed).unwrap())
.await;
pause.release();
tokio::time::timeout(Duration::from_secs(5), task)
.await
.unwrap()
.unwrap()
.unwrap();
backend.state.lock().await.objects.insert(key, original);
let mut candidate = registration_entry(&namespace, "candidate");
candidate.warehouse_location = "s3://analytics/changed/child".to_string();
store
.create_table(candidate)
.await
.expect("a GET projection cannot inherit an earlier inventory ETag");
}
#[tokio::test]
async fn registration_cache_observes_changed_and_removed_objects() {
let (backend, store, namespace) = cache_fixture().await;
let path = store
.paths
.table_entry_path("analytics", &namespace, &IdentifierSegment::parse("orders").unwrap());
let mut changed = registration_entry(&namespace, "orders");
changed.warehouse_location = "s3://analytics/relocated".to_string();
backend
.seed_object(RUSTFS_META_BUCKET, &path, serde_json::to_vec(&changed).unwrap())
.await;
let mut candidate = registration_entry(&namespace, "candidate");
candidate.warehouse_location = "s3://analytics/relocated/child".to_string();
assert!(matches!(
store.create_table(candidate.clone()).await,
Err(TableCatalogStoreError::Conflict(_))
));
backend.delete_object(RUSTFS_META_BUCKET, &path).await.unwrap();
candidate.table_id = changed.table_id;
store
.create_table(candidate)
.await
.expect("removed records cannot remain active through the cache");
}
#[tokio::test]
async fn registration_cache_revalidates_missing_etags_and_corrupt_changed_indexes() {
let (backend, store, namespace) = cache_fixture().await;
let path = store
.paths
.table_entry_path("analytics", &namespace, &IdentifierSegment::parse("orders").unwrap());
backend
.state
.lock()
.await
.etagless_objects
.insert((RUSTFS_META_BUCKET.to_string(), path.clone()));
backend.fail_next_read(RUSTFS_META_BUCKET, &path).await;
assert!(store.create_table(registration_entry(&namespace, "candidate")).await.is_err());
backend.state.lock().await.etagless_objects.clear();
let index = store.paths.warehouse_index_entry_path("analytics", "tables/id-orders/");
backend
.seed_object(RUSTFS_META_BUCKET, &index, b"invalid json".to_vec())
.await;
assert!(store.create_table(registration_entry(&namespace, "candidate")).await.is_err());
assert!(store.load_table("analytics", "sales", "candidate").await.unwrap().is_none());
}
#[tokio::test]
async fn registration_cache_rejects_incomplete_and_versioned_inventories() {
let (backend, store, namespace) = cache_fixture().await;
let namespace_prefix = store.paths.namespace_entries_prefix("analytics");
let index_prefix = store.paths.warehouse_index_entries_prefix("analytics");
for prefix in [&namespace_prefix, &index_prefix] {
backend.state.lock().await.failed_inventory_prefixes.insert(prefix.clone());
assert!(store.create_table(registration_entry(&namespace, "candidate")).await.is_err());
backend.state.lock().await.failed_inventory_prefixes.clear();
}
for (version_id, delete_marker) in [(Some(Uuid::new_v4().to_string()), false), (None, true)] {
backend.state.lock().await.inventory_overrides.insert(
namespace_prefix.clone(),
vec![TableCatalogCleanupObject {
key: format!("{namespace_prefix}unexpected"),
version_id,
etag: Some("etag".to_string()),
delete_marker,
}],
);
assert!(store.create_table(registration_entry(&namespace, "candidate")).await.is_err());
}
backend.state.lock().await.inventory_overrides.clear();
assert!(store.load_table("analytics", "sales", "candidate").await.unwrap().is_none());
let mut duplicate = registration_entry(&namespace, "candidate");
duplicate.table_id = "id-orders".to_string();
assert!(matches!(store.create_table(duplicate).await, Err(TableCatalogStoreError::Conflict(_))));
}
#[tokio::test]
async fn registration_cache_cancellation_does_not_publish_a_partial_catalog() {
let (backend, store, namespace) = cache_fixture().await;
backend.registration_cache.clear();
let path = store
.paths
.table_entry_path("analytics", &namespace, &IdentifierSegment::parse("warmup").unwrap());
let pause = backend.pause_before_next_read(RUSTFS_META_BUCKET, &path).await;
let task_store = store.clone();
let candidate = registration_entry(&namespace, "cancelled");
let task = tokio::spawn(async move { task_store.create_table(candidate).await });
tokio::time::timeout(Duration::from_secs(5), pause.wait_started())
.await
.unwrap();
task.abort();
assert!(task.await.unwrap_err().is_cancelled());
pause.release();
assert!(store.load_table("analytics", "sales", "cancelled").await.unwrap().is_none());
let mut changed = registration_entry(&namespace, "warmup");
changed.warehouse_location = "s3://analytics/changed".to_string();
backend
.seed_object(RUSTFS_META_BUCKET, &path, serde_json::to_vec(&changed).unwrap())
.await;
let mut candidate = registration_entry(&namespace, "after_cancel");
candidate.warehouse_location = "s3://analytics/changed/child".to_string();
assert!(matches!(store.create_table(candidate).await, Err(TableCatalogStoreError::Conflict(_))));
}
#[tokio::test]
async fn registration_cache_isolated_runtimes_do_not_share_matching_etags() {
let (first, first_store, namespace) = cache_fixture().await;
let (second, second_store, _) = cache_fixture().await;
let path = first_store
.paths
.table_entry_path("analytics", &namespace, &IdentifierSegment::parse("orders").unwrap());
let mut changed = registration_entry(&namespace, "orders");
changed.warehouse_location = "s3://analytics/second-only".to_string();
let first_etag = first
.state
.lock()
.await
.objects
.get(&(RUSTFS_META_BUCKET.to_string(), path.clone()))
.unwrap()
.etag
.clone();
second.registration_cache.clear();
{
let mut state = second.state.lock().await;
let object = state.objects.get_mut(&(RUSTFS_META_BUCKET.to_string(), path)).unwrap();
object.data = serde_json::to_vec(&changed).unwrap();
object.etag = first_etag;
}
let mut candidate = registration_entry(&namespace, "candidate");
candidate.warehouse_location = "s3://analytics/second-only/child".to_string();
assert!(matches!(
second_store.create_table(candidate.clone()).await,
Err(TableCatalogStoreError::Conflict(_))
));
first_store
.create_table(candidate)
.await
.expect("the first runtime has no such owner");
}
#[tokio::test]
async fn registration_cache_preserves_identity_after_lost_response_and_failed_rollback() {
for lost_response in [false, true] {
let (backend, store, namespace) = cache_fixture().await;
let entry = registration_entry(&namespace, "ambiguous");
let path = store
.paths
.table_entry_path("analytics", &namespace, &IdentifierSegment::parse(&entry.table).unwrap());
let index = store.paths.warehouse_index_entry_path("analytics", "tables/id-ambiguous/");
if lost_response {
backend.fail_after_next_put(RUSTFS_META_BUCKET, &path).await;
} else {
backend.fail_next_put(RUSTFS_META_BUCKET, &path).await;
}
backend.fail_delete_attempt(RUSTFS_META_BUCKET, &index, 1).await;
assert!(store.create_table(entry.clone()).await.is_err());
let mut candidate = registration_entry(&namespace, "candidate");
candidate.table_id = entry.table_id;
let result = store.create_table(candidate).await;
if lost_response {
assert!(matches!(result, Err(TableCatalogStoreError::Conflict(_))));
} else {
result.expect("failed primary writes cannot reserve identity through cached results");
}
}
}
async fn leased_registration_publication<'a>(
backend: &'a TestCatalogObjectBackend,
lock: &rustfs_lock::NamespaceLock,
owner: &str,
) -> (
TableCommitLockPublication<'a, TestCatalogObjectBackend>,
Arc<rustfs_lock::distributed_lock::LockLostSignal>,
) {
let guard = lock
.lock_guard(
rustfs_lock::ObjectKey::new("analytics", default_table_bucket_publication_lock_path()),
owner,
Duration::from_secs(5),
Duration::from_secs(3),
)
.await
.expect("publication lease acquisition succeeds")
.expect("publication lease is acquired");
let signal = guard.lock_lost_signal().expect("distributed publication has a loss signal");
let publication = TableCommitLockPublication::new(backend);
{
let mut state = publication.state.lock();
state.table_bucket = Some("analytics".to_string());
state.guards.push(TableCatalogLockGuard::namespace(guard));
}
(publication, signal)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn registration_cache_rejects_delayed_table_write_after_publication_lease_loss() {
let backend = TestCatalogObjectBackend {
reject_reads_while_write_locked: true,
..Default::default()
};
let store = ObjectTableCatalogStore::new(backend.clone());
let stalled_namespace = Namespace::parse("stalled").unwrap();
let active_namespace = Namespace::parse("active").unwrap();
store.put_table_bucket(test_bucket_entry("analytics")).await.unwrap();
for namespace in [&stalled_namespace, &active_namespace] {
store
.create_namespace(test_namespace_entry("analytics", namespace))
.await
.unwrap();
}
let initial = registration_entry(&active_namespace, "initial");
store.create_table(initial.clone()).await.unwrap();
let client = Arc::new(RecordingLeaseClient {
inner: rustfs_lock::LocalClient::with_manager(Arc::new(rustfs_lock::GlobalLockManager::Enabled(Arc::new(
rustfs_lock::FastObjectLockManager::new(),
)))),
acquired: Mutex::new(None),
});
let lock = rustfs_lock::NamespaceLock::with_clients_and_quorum(
"registration-publication-loss".to_string(),
vec![client.clone() as Arc<dyn LockClient>],
1,
);
let (old_publication, old_signal) = leased_registration_publication(&backend, &lock, "old-writer").await;
let old_lease = client.acquired.lock().unwrap().clone().unwrap();
let delayed = registration_entry(&stalled_namespace, "delayed");
let delayed_path =
store
.paths
.table_entry_path("analytics", &stalled_namespace, &IdentifierSegment::parse(&delayed.table).unwrap());
let pause = backend.pause_put_attempt(RUSTFS_META_BUCKET, &delayed_path, 1).await;
let replacement = registration_entry(&active_namespace, "replacement");
let delayed_write = store.register_table_with_publication(delayed.clone(), &old_publication);
let takeover = async {
tokio::time::timeout(Duration::from_secs(5), pause.wait_started())
.await
.expect("old writer reaches the table-entry commit");
assert!(client.force_release(&old_lease).await.unwrap());
tokio::time::timeout(Duration::from_secs(10), old_signal.notified())
.await
.expect("heartbeat observes publication lease loss");
assert!(!old_publication.holds_table_bucket("analytics"));
let (new_publication, _) = leased_registration_publication(&backend, &lock, "new-writer").await;
backend.reset_call_counts().await;
tokio::time::timeout(
Duration::from_secs(5),
store.register_table_with_publication(replacement.clone(), &new_publication),
)
.await
.expect("replacement writer completes while the old put is paused")
.expect("replacement writer publishes its table after a fresh inventory");
assert!(backend.list_call_count().await > 0, "takeover inventories the catalog");
pause.release();
};
let (delayed_result, ()) = tokio::time::timeout(Duration::from_secs(25), async { tokio::join!(delayed_write, takeover) })
.await
.expect("both writers finish after the paused put is resumed");
assert!(
matches!(delayed_result, Err(TableCatalogStoreError::Unavailable(_))),
"the delayed storage commit must reject the lost publication fence: {delayed_result:?}"
);
assert_eq!(backend.put_attempt_count(RUSTFS_META_BUCKET, &delayed_path).await, 1);
assert!(
backend
.read_object(RUSTFS_META_BUCKET, &delayed_path)
.await
.unwrap()
.is_none()
);
for entry in [&initial, &replacement] {
assert_eq!(
store.load_table("analytics", &entry.namespace, &entry.table).await.unwrap(),
Some(entry.clone())
);
}
let subsequent = registration_entry(&active_namespace, "subsequent");
store.create_table(subsequent.clone()).await.unwrap();
for entry in [&initial, &replacement, &subsequent] {
let mut duplicate = registration_entry(&active_namespace, "duplicate");
duplicate.table_id = entry.table_id.clone();
assert!(matches!(store.create_table(duplicate).await, Err(TableCatalogStoreError::Conflict(_))));
}
store
.create_table(delayed)
.await
.expect("the rejected writer left no active table");
}
+53
View File
@@ -411,6 +411,7 @@ pub(crate) struct TestCatalogObjectBackend {
pub(crate) state: Arc<tokio::sync::Mutex<TestCatalogObjectState>>,
pub(crate) locks: TestCatalogObjectLocks,
pub(crate) strong_runtime: Option<StrongTableCatalogRuntime>,
pub(crate) registration_cache: TableCatalogRegistrationCache,
// One-shot, path-keyed injection knobs from the admin handler tests'
// former TestTableCatalogObjectBackend (backlog#1837 PR2): each fires
// once for the named object and clears itself, mirroring the original
@@ -543,8 +544,11 @@ pub(crate) struct TestCatalogObjectState {
pub(crate) write_lock_acquisitions: BTreeMap<(String, String), usize>,
pub(crate) read_lock_acquisitions: BTreeMap<(String, String), usize>,
pub(crate) read_calls: usize,
pub(crate) read_bytes: usize,
pub(crate) metadata_calls: usize,
pub(crate) list_calls: usize,
pub(crate) failed_inventory_prefixes: BTreeSet<String>,
pub(crate) inventory_overrides: BTreeMap<String, Vec<TableCatalogCleanupObject>>,
pub(crate) next_etag: u64,
}
@@ -608,6 +612,7 @@ impl TestCatalogObjectBackend {
pub(crate) async fn reset_call_counts(&self) {
let mut state = self.state.lock().await;
state.read_calls = 0;
state.read_bytes = 0;
state.metadata_calls = 0;
state.list_calls = 0;
}
@@ -955,6 +960,10 @@ impl TestCatalogObjectState {
#[async_trait::async_trait]
impl TableCatalogObjectBackend for TestCatalogObjectBackend {
fn registration_cache(&self) -> Option<TableCatalogRegistrationCache> {
Some(self.registration_cache.clone())
}
fn strong_catalog_runtime(&self) -> Option<StrongTableCatalogRuntime> {
self.strong_runtime.clone()
}
@@ -1018,6 +1027,7 @@ impl TableCatalogObjectBackend for TestCatalogObjectBackend {
etag: (!etagless).then(|| record.etag.clone()),
mod_time: record.mod_time,
});
state.read_bytes += result.as_ref().map_or(0, |object| object.data.len());
let pause = state
.pause_read_attempts
.get_mut(&key)
@@ -1142,6 +1152,17 @@ impl TableCatalogObjectBackend for TestCatalogObjectBackend {
.await
}
async fn delete_object_if_match_fenced_for_incarnation(
&self,
bucket: &str,
object: &str,
expected_etag: &str,
_expected_incarnation_id: Uuid,
fence: &TableCatalogObjectMutationFence,
) -> TableCatalogStoreResult<()> {
self.delete_object_if_match_fenced(bucket, object, expected_etag, fence).await
}
async fn list_objects(&self, bucket: &str, prefix: &str) -> TableCatalogStoreResult<Vec<String>> {
let mut state = self.state.lock().await;
state.list_calls += 1;
@@ -1153,6 +1174,38 @@ impl TableCatalogObjectBackend for TestCatalogObjectBackend {
.collect())
}
async fn list_objects_for_cleanup(
&self,
bucket: &str,
prefix: &str,
) -> TableCatalogStoreResult<Vec<TableCatalogCleanupObject>> {
let mut state = self.state.lock().await;
state.list_calls += 1;
if state.failed_inventory_prefixes.contains(prefix) {
return Err(TableCatalogStoreError::Unavailable(
"injected authoritative inventory failure".to_string(),
));
}
if let Some(objects) = state.inventory_overrides.get(prefix) {
return Ok(objects.clone());
}
Ok(state
.objects
.iter()
.filter(|((entry_bucket, object), _)| entry_bucket == bucket && object.starts_with(prefix))
.map(|(key, record)| TableCatalogCleanupObject {
key: key.1.clone(),
version_id: None,
etag: (!state.etagless_objects.contains(key)).then(|| record.etag.clone()),
delete_marker: false,
})
.collect())
}
async fn ensure_no_multipart_uploads(&self, _bucket: &str) -> TableCatalogStoreResult<()> {
Ok(())
}
async fn acquire_write_lock(&self, bucket: &str, object: &str) -> TableCatalogStoreResult<TableCatalogLockGuard> {
self.lock_attempts.lock().await.push((bucket.to_string(), object.to_string()));
{
+432 -3
View File
@@ -7145,6 +7145,228 @@ async fn table_data_plane_resource_does_not_match_sibling_prefix() {
assert_eq!(backend.list_call_count().await, 1);
}
async fn cached_registration_fixture() -> (TestCatalogObjectBackend, ObjectTableCatalogStore<TestCatalogObjectBackend>, Namespace)
{
let backend = TestCatalogObjectBackend {
reject_reads_while_write_locked: true,
..Default::default()
};
let store = ObjectTableCatalogStore::new(backend.clone());
let namespace = Namespace::parse("sales").expect("test namespace is valid");
store
.put_table_bucket(test_bucket_entry("analytics"))
.await
.expect("bucket is created");
store
.create_namespace(test_namespace_entry("analytics", &namespace))
.await
.expect("namespace is created");
(backend, store, namespace)
}
fn cached_registration_entry(namespace: &Namespace, name: &str) -> TableEntry {
let table = IdentifierSegment::parse(name).expect("test table name is valid");
let mut entry = test_table_entry(
"analytics",
namespace,
&table,
default_table_metadata_file_path(namespace, &table, "00001.metadata.json"),
);
entry.table_id = format!("id-{name}");
entry.warehouse_location = format!("s3://analytics/tables/id-{name}");
entry
}
#[tokio::test]
async fn object_catalog_registration_cache_bounds_reads_across_store_instances() {
let (backend, _, namespace) = cached_registration_fixture().await;
for number in 0..256 {
let store = ObjectTableCatalogStore::new(backend.clone());
backend.reset_call_counts().await;
store
.create_table(cached_registration_entry(&namespace, &format!("table_{number:04}")))
.await
.expect("each new store instance shares validated registration projections");
if number >= 32 {
let state = backend.state.lock().await;
assert_eq!(state.list_calls, 2, "each registration inventories both catalog ranges");
assert!(
state.read_calls < 24,
"registration used {} object reads at table {number}",
state.read_calls
);
assert!(
state.read_bytes < 256 * 1024,
"registration read {} bytes at table {number}",
state.read_bytes
);
}
}
}
#[tokio::test]
async fn object_catalog_registration_cache_preserves_identity_and_overlap_checks() {
let (backend, store, namespace) = cached_registration_fixture().await;
let mut existing = cached_registration_entry(&namespace, "orders");
existing.warehouse_location = "s3://analytics/shared/orders".to_string();
store.create_table(existing.clone()).await.expect("initial table is created");
backend
.delete_object(RUSTFS_META_BUCKET, &store.paths.warehouse_index_entry_path("analytics", "shared/orders/"))
.await
.expect("simulate a missing materialized warehouse index");
let mut duplicate = cached_registration_entry(&namespace, "duplicate");
duplicate.table_id = existing.table_id.clone();
assert_matches!(store.create_table(duplicate).await, Err(TableCatalogStoreError::Conflict(_)));
for location in [
"s3://analytics/shared",
"s3://analytics/shared/orders",
"s3://analytics/shared/orders/child",
] {
let mut conflicting = cached_registration_entry(&namespace, "conflicting");
conflicting.warehouse_location = location.to_string();
assert_matches!(store.create_table(conflicting).await, Err(TableCatalogStoreError::Conflict(_)));
}
let mut adjacent = cached_registration_entry(&namespace, "adjacent");
adjacent.warehouse_location = "s3://analytics/shared/orders2".to_string();
store
.create_table(adjacent)
.await
.expect("a different directory remains available");
}
#[tokio::test]
async fn object_catalog_registration_cache_rechecks_after_failed_write_and_drop() {
let (backend, store, namespace) = cached_registration_fixture().await;
store
.create_table(cached_registration_entry(&namespace, "orders"))
.await
.expect("initial table is created");
let returns = IdentifierSegment::parse("returns").expect("test name is valid");
let table_path = store.paths.table_entry_path("analytics", &namespace, &returns);
backend.fail_next_put(RUSTFS_META_BUCKET, &table_path).await;
assert!(
store
.create_table(cached_registration_entry(&namespace, "returns"))
.await
.is_err()
);
assert!(
store
.load_table("analytics", "sales", "returns")
.await
.expect("table lookup succeeds")
.is_none()
);
store
.create_table(cached_registration_entry(&namespace, "returns"))
.await
.expect("retry reads the authoritative records");
store
.drop_table("analytics", "sales", "orders")
.await
.expect("drop removes the active owner");
let mut replacement = cached_registration_entry(&namespace, "replacement");
replacement.table_id = "id-orders".to_string();
replacement.warehouse_location = "s3://analytics/tables/id-orders".to_string();
store
.create_table(replacement)
.await
.expect("a dropped owner no longer reserves its identity");
}
#[tokio::test]
async fn object_catalog_registration_cache_observes_pointer_commits_and_rename() {
let (backend, store, namespace) = cached_registration_fixture().await;
let entry = cached_registration_entry(&namespace, "orders");
store.create_table(entry.clone()).await.expect("initial table is created");
let metadata_directory = entry
.metadata_location
.rsplit_once('/')
.expect("metadata has a parent directory")
.0;
let next_metadata = format!("{metadata_directory}/00002.metadata.json");
backend
.seed_object(
"analytics",
&next_metadata,
serde_json::to_vec(&serde_json::json!({"location": entry.warehouse_location})).unwrap(),
)
.await;
store
.commit_table(TableCommitRequest {
table_bucket: "analytics".to_string(),
namespace: "sales".to_string(),
table: "orders".to_string(),
commit_id: "pointer-only".to_string(),
idempotency_key: None,
operation: "append".to_string(),
expected_version_token: entry.version_token,
expected_metadata_location: entry.metadata_location,
new_metadata_location: next_metadata,
requirements: Vec::new(),
writer: None,
})
.await
.expect("metadata pointer commit succeeds");
backend.reset_call_counts().await;
store
.create_table(cached_registration_entry(&namespace, "returns"))
.await
.unwrap();
assert_eq!(
backend.list_call_count().await,
2,
"pointer changes are checked through fresh inventories"
);
store
.rename_table("analytics", "sales", "orders", "sales", "renamed")
.await
.unwrap();
backend.reset_call_counts().await;
store
.create_table(cached_registration_entry(&namespace, "shipments"))
.await
.unwrap();
assert!(
backend.list_call_count().await > 0,
"rename is visible in the next authoritative inventory"
);
let mut duplicate = cached_registration_entry(&namespace, "duplicate");
duplicate.table_id = "id-orders".to_string();
assert_matches!(store.create_table(duplicate).await, Err(TableCatalogStoreError::Conflict(_)));
}
#[tokio::test]
async fn object_catalog_registration_cache_observes_an_independent_writer() {
let (backend, store, namespace) = cached_registration_fixture().await;
store
.create_table(cached_registration_entry(&namespace, "orders"))
.await
.unwrap();
let scan_store = ObjectTableCatalogStore::new(TestCatalogObjectBackend {
registration_cache: TableCatalogRegistrationCache::default(),
..backend.clone()
});
scan_store
.create_table(cached_registration_entry(&namespace, "returns"))
.await
.unwrap();
backend.reset_call_counts().await;
store
.create_table(cached_registration_entry(&namespace, "shipments"))
.await
.unwrap();
assert!(
backend.list_call_count().await > 0,
"an independent writer is visible in the next authoritative inventory"
);
let mut duplicate = cached_registration_entry(&namespace, "duplicate");
duplicate.table_id = "id-returns".to_string();
assert_matches!(store.create_table(duplicate).await, Err(TableCatalogStoreError::Conflict(_)));
}
#[tokio::test]
async fn object_catalog_registration_scans_warehouse_prefixes_once() {
let backend = TestCatalogObjectBackend::default();
@@ -7173,10 +7395,217 @@ async fn object_catalog_registration_scans_warehouse_prefixes_once() {
reads.push(backend.read_call_count().await);
assert_eq!(backend.list_call_count().await, 2, "one table scan and one warehouse index scan");
}
assert_eq!(reads[2] - reads[1], 2, "each existing table and its index should be read once");
assert_eq!(reads[2], reads[1], "unchanged validated objects should not require another GET");
assert_eq!(store.list_tables(bucket, &namespace.public_name()).await.unwrap().len(), 3);
}
#[tokio::test]
async fn object_catalog_registration_bounds_concurrent_catalog_reads() {
const MAX_CONCURRENT_READS: usize = 8;
let backend = TestCatalogObjectBackend {
reject_reads_while_write_locked: true,
..Default::default()
};
let store = ObjectTableCatalogStore::new(backend.clone());
let bucket = "analytics";
let namespace = Namespace::parse("sales").unwrap();
store.put_table_bucket(test_bucket_entry(bucket)).await.unwrap();
store
.create_namespace(test_namespace_entry(bucket, &namespace))
.await
.unwrap();
let mut table_objects = Vec::new();
let mut index_objects = Vec::new();
for number in 0..MAX_CONCURRENT_READS + 2 {
let name = format!("orders_{number:02}");
let table = IdentifierSegment::parse(&name).unwrap();
let mut entry = test_table_entry(
bucket,
&namespace,
&table,
default_table_metadata_file_path(&namespace, &table, "00001.metadata.json"),
);
entry.table_id = format!("table-{name}");
entry.warehouse_location = format!("s3://{bucket}/tables/{name}");
store.create_table(entry).await.unwrap();
table_objects.push(store.paths.table_entry_path(bucket, &namespace, &table));
index_objects.push(store.paths.warehouse_index_entry_path(bucket, &format!("tables/{name}/")));
}
backend.registration_cache.clear();
let mut scans = Vec::new();
for mut objects in [table_objects, index_objects] {
objects.sort();
let mut scan = Vec::new();
for object in objects {
let previous_reads = backend
.state
.lock()
.await
.read_attempts
.get(&(RUSTFS_META_BUCKET.to_string(), object.clone()))
.copied()
.unwrap_or_default();
let pause = backend.pause_before_next_read(RUSTFS_META_BUCKET, &object).await;
scan.push((object, pause, previous_reads));
}
scans.push(scan);
}
let table = IdentifierSegment::parse("new_orders").unwrap();
let mut entry = test_table_entry(
bucket,
&namespace,
&table,
default_table_metadata_file_path(&namespace, &table, "00001.metadata.json"),
);
entry.table_id = "new-table-id".to_string();
entry.warehouse_location = format!("s3://{bucket}/tables/new_orders");
let registering_store = store.clone();
let registration = tokio::spawn(async move { registering_store.create_table(entry).await });
for scan in scans {
for (_, pause, _) in scan.iter().take(MAX_CONCURRENT_READS) {
tokio::time::timeout(TABLE_CATALOG_TEST_TIMEOUT, pause.wait_started())
.await
.expect("independent catalog reads should start concurrently");
}
let state = backend.state.lock().await;
let started = scan
.iter()
.filter(|(object, _, previous_reads)| {
state
.read_attempts
.get(&(RUSTFS_META_BUCKET.to_string(), object.clone()))
.copied()
.unwrap_or_default()
> *previous_reads
})
.count();
assert_eq!(started, MAX_CONCURRENT_READS, "catalog reads must respect the concurrency limit");
drop(state);
for (_, pause, _) in scan {
pause.release();
}
}
tokio::time::timeout(TABLE_CATALOG_TEST_TIMEOUT, registration)
.await
.expect("registration should finish after its reads are released")
.unwrap()
.unwrap();
assert!(
store
.load_table(bucket, &namespace.public_name(), table.as_str())
.await
.unwrap()
.is_some()
);
}
#[tokio::test]
async fn object_catalog_registration_propagates_catalog_scan_read_errors() {
for fail_index_read in [false, true] {
let backend = TestCatalogObjectBackend::default();
let store = ObjectTableCatalogStore::new(backend.clone());
let bucket = "analytics";
let namespace = Namespace::parse("sales").unwrap();
let existing = IdentifierSegment::parse("orders").unwrap();
seed_table_for_metadata_maintenance(
&store,
bucket,
&namespace,
&existing,
default_table_metadata_file_path(&namespace, &existing, "00001.metadata.json"),
)
.await;
let failed_object = if fail_index_read {
store.paths.warehouse_index_entry_path(bucket, "tables/table-id/")
} else {
store.paths.table_entry_path(bucket, &namespace, &existing)
};
backend.fail_next_read(RUSTFS_META_BUCKET, &failed_object).await;
let table = IdentifierSegment::parse("returns").unwrap();
let mut entry = test_table_entry(
bucket,
&namespace,
&table,
default_table_metadata_file_path(&namespace, &table, "00001.metadata.json"),
);
entry.table_id = "new-table-id".to_string();
entry.warehouse_location = format!("s3://{bucket}/tables/new-table-id");
assert_matches!(
store.create_table(entry.clone()).await,
Err(TableCatalogStoreError::Internal(message)) if message.contains(&failed_object)
);
assert!(
store
.load_table(bucket, &namespace.public_name(), table.as_str())
.await
.unwrap()
.is_none()
);
assert!(
backend
.read_object(
RUSTFS_META_BUCKET,
&store.paths.warehouse_index_entry_path(bucket, "tables/new-table-id/")
)
.await
.unwrap()
.is_none()
);
store
.create_table(entry)
.await
.expect("a failed scan must release its read locks for retry");
}
}
#[tokio::test]
async fn object_catalog_registration_reuses_locked_deleted_entry() {
let backend = TestCatalogObjectBackend {
reject_reads_while_write_locked: true,
..Default::default()
};
let store = ObjectTableCatalogStore::new(backend.clone());
let bucket = "analytics";
let namespace = Namespace::parse("sales").unwrap();
let table = IdentifierSegment::parse("orders").unwrap();
store.put_table_bucket(test_bucket_entry(bucket)).await.unwrap();
store
.create_namespace(test_namespace_entry(bucket, &namespace))
.await
.unwrap();
let mut entry = test_table_entry(
bucket,
&namespace,
&table,
default_table_metadata_file_path(&namespace, &table, "00001.metadata.json"),
);
entry.state = TableCatalogEntryState::Deleted;
backend
.seed_object(
RUSTFS_META_BUCKET,
&store.paths.table_entry_path(bucket, &namespace, &table),
serde_json::to_vec(&entry).unwrap(),
)
.await;
entry.state = TableCatalogEntryState::Active;
entry.table_id = "replacement-table-id".to_string();
store.create_table(entry.clone()).await.unwrap();
assert_eq!(
store
.load_table(bucket, &namespace.public_name(), table.as_str())
.await
.unwrap(),
Some(entry)
);
}
#[tokio::test]
async fn catalog_backings_reject_overlapping_registered_warehouse_prefixes() {
let backend = TestCatalogObjectBackend::default();
@@ -20526,9 +20955,9 @@ async fn object_table_catalog_store_reuses_old_prefix_after_failed_relocation_in
let mut next_entry = test_table_entry(bucket, &namespace, &next_table, current_metadata);
next_entry.table_id = "next-table-id".to_string();
next_entry.warehouse_location = format!("s3://{bucket}/tables/table-id");
store
.create_table(next_entry)
tokio::time::timeout(std::time::Duration::from_secs(5), store.create_table(next_entry))
.await
.expect("stale warehouse prefix reuse completes")
.expect("stale old warehouse index should not block prefix reuse");
let (index, _) = store