From 4b5f6ce1210e12b6f407893fde52837a18bb05b0 Mon Sep 17 00:00:00 2001 From: sabith Date: Wed, 18 Nov 2026 14:33:47 -0800 Subject: [PATCH] feat(cas): allow Suspended S3 bucket versioning via cas_allow_versioning_suspended Allows mounting and reclaiming objects on an S3 bucket whose versioning is Suspended (never Enabled) when opted in via true. 1. IObjectStorage / S3ObjectStorage: add BucketVersioningStatus (Unknown, Disabled, Suspended, Enabled) and getBucketVersioningStatus() so S3ObjectStorage distinguishes Enabled from Suspended while preserving isBucketVersioningEnabled(). 2. ContentAddressedSettings / ContentAddressedMetadataStorage: add cas_allow_versioning_suspended (default false) and wire it into ObjectStorageBackend. 3. CasObjectStorageBackend / CasProbe: verify getBucketVersioningStatus() != Enabled when opted in (always refusing Enabled buckets) and accept S3 null-version delete markers (created_delete_marker=true) as Removed with a startup warning advising an ExpiredObjectDeleteMarker lifecycle rule. 4. Add unit test S3SuspendedVersioningRequiresOptInAndStillRefusesEnabled in gtest_cas_backend_generation.cpp and update docs/en/antalya/cas/{bucket-requirements,configuration}.md. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: sabith --- docs/en/antalya/cas/bucket-requirements.md | 2 + docs/en/antalya/cas/configuration.md | 1 + .../Backend/CasObjectStorageBackend.cpp | 74 +++++++++++--- .../Backend/CasObjectStorageBackend.h | 12 ++- .../ContentAddressed/Backend/CasProbe.cpp | 13 +-- .../ContentAddressedMetadataStorage.cpp | 5 +- .../ContentAddressedMetadataStorage.h | 1 + .../ContentAddressedSettings.cpp | 1 + .../ObjectStorages/IObjectStorage.h | 19 ++++ .../ObjectStorages/S3/S3ObjectStorage.cpp | 29 +++++- .../ObjectStorages/S3/S3ObjectStorage.h | 1 + .../tests/gtest_cas_backend_generation.cpp | 97 +++++++++++++++++++ 12 files changed, 230 insertions(+), 25 deletions(-) diff --git a/docs/en/antalya/cas/bucket-requirements.md b/docs/en/antalya/cas/bucket-requirements.md index c3f25b58794fa..e8c8e06a22dd3 100644 --- a/docs/en/antalya/cas/bucket-requirements.md +++ b/docs/en/antalya/cas/bucket-requirements.md @@ -35,6 +35,8 @@ Bucket **versioning is not required** — in fact it must be **disabled** on the dialect (see below), because a token-exact delete on a versioned bucket archives a noncurrent generation instead of reclaiming storage, silently stopping GC reclamation. +On S3 (`ETag` dialect), an unversioned bucket is the default and recommended setup. If a bucket previously had versioning enabled and is now **`Suspended`**, `DeleteObject` on S3 still creates a `"null"`-version delete marker (`x-amz-delete-marker: true`), which `CasProbe` and GC reject by default (`CAS_DELETE_MARKER` / `NOT_IMPLEMENTED`). When the bucket's versioning status is `Suspended` (never `Enabled`) and an S3 lifecycle rule with `ExpiredObjectDeleteMarker: true` (and `NoncurrentVersionExpiration`) is configured, set `true` on the disk to allow mounting and delete-marker acceptance while still rejecting `Enabled` buckets. + On the generation-token dialect that requirement is checked at mount. A bucket reported as versioned refuses the mount. A probe that could not answer — the credential may not read the bucket's versioning configuration (`storage.buckets.get` on GCS), or the backend cannot report it — does not: the mount diff --git a/docs/en/antalya/cas/configuration.md b/docs/en/antalya/cas/configuration.md index 0e0ec0aa4f66c..9eab3201f4ba5 100644 --- a/docs/en/antalya/cas/configuration.md +++ b/docs/en/antalya/cas/configuration.md @@ -96,6 +96,7 @@ entirely before release. Treat this table as a snapshot of the current build, no | `cas_blob_hash` | `cityhash128` | Pool blob content-hash function (`cityhash128` \| `xxh3-128` \| `sha256`). Recorded in the pool at creation; a mismatching config is refused at mount | | `cas_blob_hash_allow_new` | `false` | Explicit opt-in to admit a new hash algorithm into an existing pool. One-way: once admitted, the pool carries both algorithms permanently | | `cas_allow_packed_parts` | `false` | Allow creation of new `Packed` MergeTree parts on this disk. The decision uses the actual destination disk, not every disk in its storage policy. A `type=cache` disk inherits this capability from the wrapped `CAS` disk; set the key in the `metadata_type=cas` disk block. Existing `Packed` parts remain readable when this setting is `false` | +| `cas_allow_versioning_suspended` | `false` | Allow mounting an S3 (`ETag`-dialect) bucket whose versioning status is `Suspended` and accept `"null"`-version delete markers on conditional deletes (`DeleteObject`). Buckets with versioning `Enabled` are still refused. Pair with an S3 lifecycle rule setting `ExpiredObjectDeleteMarker: true` (and `NoncurrentVersionExpiration` for any prior noncurrent versions) | | `skip_access_check` | `false` | Skip the boot-time capability probe (start now, fix later). Only the preflight probe is skipped — the check that the object storage supports single-attempt conditional writes still runs on every ordinary writable start. **Not available on a writable generation-token (GCS) disk**, which refuses to mount with it: there, the probe battery is the only proof that a token-exact delete carries its generation precondition. Mount such a disk read-only if you need to defer the check | | `cas_mount_lease_ttl_ms` | `40000` | How long this server may write after its last confirmed renewal; every other server waits at least this long before fencing or reclaiming its slot (≥ 1, at most 600000 — a larger value is clamped to 10 minutes with a warning at startup, so no holder declares a longer lease). Lower values shorten recovery after this server's crash but reduce tolerance for object-storage and scheduling delays. Set `30000` for faster recovery after a crash, at the costs [below](#mount-lease-costs) | | `cas_mount_renew_period_ms` | `10000` | Milliseconds between background mount-lease renewals (≥ 1). It must leave enough time for two attempt envelopes (a renewal write and its settlement read) and the lease safety margin before the TTL expires: `period + 2 × envelope + margin < TTL` | diff --git a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.cpp b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.cpp index f0cbdf2e4dc90..f9694d27b7ac6 100644 --- a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.cpp +++ b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.cpp @@ -128,32 +128,75 @@ void recordConditionalWriteOutcome(CasWriteOutcome outcome) ObjectStorageBackend::ObjectStorageBackend(ObjectStoragePtr object_storage_, Mode mode_, bool single_attempt_control_plane_, uint64_t attempt_timeout_ms_, - uint64_t connect_timeout_cap_ms_) + uint64_t connect_timeout_cap_ms_, + bool allow_versioning_suspended_) : object_storage(std::move(object_storage_)) , mode(mode_) , single_attempt_control_plane(single_attempt_control_plane_) , attempt_timeout_ms(attempt_timeout_ms_) , connect_timeout_cap_ms(connect_timeout_cap_ms_) + , allow_versioning_suspended(allow_versioning_suspended_) , emu_root(object_storage->getCommonKeyPrefix()) { if (mode == Mode::Native && object_storage->conditionalOpsUseGenerationTokens()) native_token_type = Dialect::Generation; } -/// See Backend::checkPoolPreconditions. Only the Native, generation-dialect (GCS) combination has -/// anything to check: a token-exact DELETE on a versioned bucket archives a noncurrent generation -/// instead of reclaiming storage, so GC "reclaim" would silently stop reclaiming. A bucket VERIFIED -/// to have versioning enabled refuses the mount. A probe that cannot answer does not: it is not -/// evidence of a versioned bucket, its usual cause is a credential without permission to read the -/// bucket configuration, and refusing on it would turn a missing IAM grant into a hard outage. The -/// mount proceeds with a warning that names what was not verified and how the operator can verify it. -/// The probe uses this backend's control-plane profile, because the default client would retry a failed -/// attempt with SDK backoff, outside the mount's attempt budget. +bool ObjectStorageBackend::verifySuspendedVersioningAllowed() const +{ + if (!allow_versioning_suspended || mode != Mode::Native || native_token_type == Dialect::Generation) + return false; + + if (suspended_delete_markers_checked.load(std::memory_order_acquire)) + return suspended_delete_markers_allowed.load(std::memory_order_acquire); + + const auto status = object_storage->getBucketVersioningStatus(controlRequest(/*attempt_no=*/0)); + if (status == IObjectStorage::BucketVersioningStatus::Enabled) + { + throw Exception(ErrorCodes::NOT_IMPLEMENTED, + "CAS on S3: the bucket has object VERSIONING Enabled. `cas_allow_versioning_suspended=true` " + "only permits Suspended (or unversioned) buckets; an Enabled bucket archives noncurrent " + "versions on every overwrite and delete forever. Suspend or disable versioning and expire " + "noncurrent versions before mounting."); + } + + if (status == IObjectStorage::BucketVersioningStatus::Suspended) + { + LOG_WARNING(getLogger("CasObjectStorageBackend"), + "CAS on S3: bucket versioning is Suspended and `cas_allow_versioning_suspended=true`; " + "allowing null-version delete markers on conditional deletes. Configure an S3 lifecycle rule " + "with `ExpiredObjectDeleteMarker: true` (and expire any prior noncurrent versions) so delete " + "markers do not accumulate."); + } + else if (status == IObjectStorage::BucketVersioningStatus::Unknown) + { + LOG_WARNING(getLogger("CasObjectStorageBackend"), + "CAS on S3: could not verify bucket versioning status (GetBucketVersioning failed or " + "unsupported); allowing delete markers because `cas_allow_versioning_suspended=true`. Confirm " + "by hand that versioning is Suspended (never Enabled) and configure an " + "`ExpiredObjectDeleteMarker: true` lifecycle rule."); + } + + suspended_delete_markers_allowed.store(true, std::memory_order_release); + suspended_delete_markers_checked.store(true, std::memory_order_release); + return true; +} + +/// See Backend::checkPoolPreconditions. +/// - Native + Generation (GCS): refuses Enabled/Suspended buckets; tolerates Unknown with warning. +/// - Native + ETag (S3): when `allow_versioning_suspended` is true, verifies the bucket is not +/// `Enabled` and arms delete-marker acceptance for `Suspended` buckets before the probe runs. void ObjectStorageBackend::checkPoolPreconditions() { - if (mode != Mode::Native || native_token_type != Dialect::Generation) + if (mode != Mode::Native) return; + if (native_token_type != Dialect::Generation) + { + (void)verifySuspendedVersioningAllowed(); + return; + } + const auto versioned = object_storage->isBucketVersioningEnabled(controlRequest(/*attempt_no=*/0)); if (!versioned.has_value()) { @@ -970,9 +1013,12 @@ Backend::RawRemoval ObjectStorageBackend::removeUnder( switch (result.outcome) { case ConditionalRemoveOutcome::Removed: - /// A delete marker means the storage archived a noncurrent version instead of - /// reclaiming the current object -- a removal that did not reclaim. - return result.created_delete_marker ? RawRemoval::DeleteMarker : RawRemoval::Removed; + /// A delete marker means the storage archived a noncurrent version (or minted a + /// null-version delete marker on a Suspended S3 bucket). Only permit it when + /// `cas_allow_versioning_suspended=true` and `Enabled` versioning has been ruled out. + if (result.created_delete_marker) + return verifySuspendedVersioningAllowed() ? RawRemoval::Removed : RawRemoval::DeleteMarker; + return RawRemoval::Removed; case ConditionalRemoveOutcome::TokenMismatch: return RawRemoval::Mismatch; case ConditionalRemoveOutcome::NotFound: diff --git a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.h b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.h index 5395bdcfc0f39..904cf3e39872e 100644 --- a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.h +++ b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasObjectStorageBackend.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -71,7 +72,8 @@ class ObjectStorageBackend final : public Backend /// that opens the pool; the defaults are what a narrow unit test constructing a bare backend gets. ObjectStorageBackend(ObjectStoragePtr object_storage_, Mode mode_, bool single_attempt_control_plane_ = false, uint64_t attempt_timeout_ms_ = 0, - uint64_t connect_timeout_cap_ms_ = 0); + uint64_t connect_timeout_cap_ms_ = 0, + bool allow_versioning_suspended_ = false); /// Read the whole object, or return `nullopt` if it is absent. Native mode reads the incarnation /// value out of the GET response itself, so no HEAD precedes it; a not-found race is reported as @@ -225,6 +227,14 @@ class ObjectStorageBackend final : public Backend const bool single_attempt_control_plane; const uint64_t attempt_timeout_ms; const uint64_t connect_timeout_cap_ms; + const bool allow_versioning_suspended; + mutable std::atomic suspended_delete_markers_checked{false}; + mutable std::atomic suspended_delete_markers_allowed{false}; + + /// Checks `getBucketVersioningStatus` once when `allow_versioning_suspended` is enabled; + /// always refuses `BucketVersioningStatus::Enabled` and permits `Suspended` (or `Unknown` + /// with an explicit warning) when opted in. + bool verifySuspendedVersioningAllowed() const; ObjectStorageRetryProfile controlPlaneProfile() const { return single_attempt_control_plane ? ObjectStorageRetryProfile::SingleAttempt : ObjectStorageRetryProfile::Default; diff --git a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasProbe.cpp b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasProbe.cpp index f72436c58ef6d..6a2a0f7c105c4 100644 --- a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasProbe.cpp +++ b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/Backend/CasProbe.cpp @@ -36,12 +36,13 @@ Removal removeOrReportDeleteMarker(CasOperation & op, const String & key, const throw; throw DB::Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "CasProbe: remove succeeded but created a versioning delete marker — the bucket has " - "object VERSIONING enabled, and a content-addressed pool cannot run on a versioned bucket: " - "every GC delete would archive a noncurrent version instead of reclaiming storage (the bucket " - "grows forever), and the constantly-rewritten ref objects would pile up versions on every " - "commit. Use a bucket where versioning was NEVER enabled; skip_access_check skips this probe " - "but does not make a versioned bucket usable — note that merely SUSPENDING versioning is not enough (deletes on a " - "versioning-suspended bucket still mint delete markers, so this probe will refuse again)"); + "object VERSIONING enabled or suspended, and a content-addressed pool cannot run on an " + "actively versioned bucket: every GC delete would archive a noncurrent version instead of " + "reclaiming storage (the bucket grows forever), and the constantly-rewritten ref objects " + "would pile up versions on every commit. Use an unversioned bucket, or — if versioning is " + "Suspended (never Enabled) and an ExpiredObjectDeleteMarker lifecycle rule is configured — " + "set true on the disk; " + "skip_access_check skips this probe but does not bypass delete-marker refusal"); } } diff --git a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.cpp b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.cpp index 872baee99f817..9c768d5ad3b3f 100644 --- a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.cpp +++ b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.cpp @@ -84,6 +84,7 @@ namespace ContentAddressedSetting extern const ContentAddressedSettingsUInt64 mount_lease_ttl_ms; extern const ContentAddressedSettingsUInt64 mount_renew_period_ms; extern const ContentAddressedSettingsBool unsafe_remount_no_delay; + extern const ContentAddressedSettingsBool allow_versioning_suspended; extern const ContentAddressedSettingsUInt64 part_folder_cache_bytes; extern const ContentAddressedSettingsUInt64 part_folder_cache_max_entries; extern const ContentAddressedSettingsUInt64 part_folder_cache_max_entry_bytes; @@ -311,6 +312,7 @@ ContentAddressedMetadataStorage::ContentAddressedMetadataStorage( , mount_lease_ttl(std::chrono::milliseconds(settings_[ContentAddressedSetting::mount_lease_ttl_ms].value)) , mount_renew_period(std::chrono::milliseconds(settings_[ContentAddressedSetting::mount_renew_period_ms].value)) , cas_unsafe_remount_no_delay(settings_[ContentAddressedSetting::unsafe_remount_no_delay].value) + , cas_allow_versioning_suspended(settings_[ContentAddressedSetting::allow_versioning_suspended].value) , cas_part_folder_cache_bytes(settings_[ContentAddressedSetting::part_folder_cache_bytes].value) , cas_part_folder_cache_max_entries(settings_[ContentAddressedSetting::part_folder_cache_max_entries].value) , cas_part_folder_cache_max_entry_bytes(settings_[ContentAddressedSetting::part_folder_cache_max_entry_bytes].value) @@ -851,7 +853,8 @@ ContentAddressedMetadataStorage::PoolView ContentAddressedMetadataStorage::openP object_storage, mode, /*single_attempt_control_plane_=*/!read_only && mode == Cas::ObjectStorageBackend::Mode::Native, pool_config.cas_request_budget.attempt_timeout_ms, - pool_config.cas_request_budget.connect_timeout_cap_ms.value_or(0)); + pool_config.cas_request_budget.connect_timeout_cap_ms.value_or(0), + cas_allow_versioning_suspended); /// A programmer error, not an input one: the handoff above is code, not configuration. The pool's /// lease arithmetic was validated against `pool_config.cas_request_budget` alone (never against the diff --git a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.h b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.h index 0183da0cd34b4..b7e5d5de451e9 100644 --- a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.h +++ b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedMetadataStorage.h @@ -674,6 +674,7 @@ class ContentAddressedMetadataStorage final : public IMetadataStorage, public IC /// See `PoolConfig::unsafe_remount_no_delay` -- the operator's explicit acceptance of an /// unobserved same-uuid reclaim. const bool cas_unsafe_remount_no_delay; + const bool cas_allow_versioning_suspended; /// Part-folder view cache settings. `cas_part_folder_cache_bytes == 0` disables retention. const uint64_t cas_part_folder_cache_bytes; const uint64_t cas_part_folder_cache_max_entries; diff --git a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedSettings.cpp b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedSettings.cpp index 7933094762862..c37e282e9381a 100644 --- a/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedSettings.cpp +++ b/src/Disks/DiskObjectStorage/MetadataStorages/ContentAddressed/ContentAddressedSettings.cpp @@ -94,6 +94,7 @@ constexpr std::string_view CAS_KEY_PREFIX = "cas_"; DECLARE(UInt64, request_window_ms, 120000, "How long a failing request, including the blob publication loop, keeps retrying before it gives up with a retryable error, in milliseconds, measured from the call's start. Must exceed twice the attempt envelope for a writable disk and the envelope for a read-only one, or the disk refuses to open. The ref-table recovery budget is twice this value", 0) \ DECLARE(UInt64, lease_safety_margin_ms, 2000, "Startup-only margin validated against the mount lease TTL: attempt envelope + this must be strictly less than the TTL, and renew period + 2 × envelope + this too", 0) \ DECLARE(String, staging_backend, "local", "Blob staging backend (local | s3); s3 is opt-in", 0) \ + DECLARE(Bool, allow_versioning_suspended, false, "Allow mounting an S3 bucket whose versioning status is Suspended (never Enabled); requires an S3 lifecycle rule expiring null delete markers", 0) \ DECLARE_SETTINGS_TRAITS(ContentAddressedSettingsTraits, LIST_OF_CONTENT_ADDRESSED_SETTINGS, CONTENT_ADDRESSED_SETTINGS_SUPPORTED_TYPES) diff --git a/src/Disks/DiskObjectStorage/ObjectStorages/IObjectStorage.h b/src/Disks/DiskObjectStorage/ObjectStorages/IObjectStorage.h index d9d17af0f2eda..1905dc0824a9d 100644 --- a/src/Disks/DiskObjectStorage/ObjectStorages/IObjectStorage.h +++ b/src/Disks/DiskObjectStorage/ObjectStorages/IObjectStorage.h @@ -548,6 +548,25 @@ class IObjectStorage /// Unpinned by default, so an ordinary storage's settings and reload behaviour are unchanged. virtual void pinConditionalOpsGenerationDialect(bool /*expect_generation_tokens*/) {} + /// Bucket object-versioning state reported by object storage backends that expose it. + enum class BucketVersioningStatus : uint8_t + { + Unknown, + Disabled, + Suspended, + Enabled, + }; + + /// Returns the bucket's object-versioning state when the backend can inspect it. + /// Defaults to mapping `isBucketVersioningEnabled(request)` for test stubs that only override it. + virtual BucketVersioningStatus getBucketVersioningStatus(const ObjectStorageControlRequest & request) const + { + const auto enabled = isBucketVersioningEnabled(request); + if (!enabled.has_value()) + return BucketVersioningStatus::Unknown; + return *enabled ? BucketVersioningStatus::Enabled : BucketVersioningStatus::Disabled; + } + /// Whether the underlying bucket has object versioning enabled; nullopt when unknown or not /// applicable. Used by the CAS capability probe on GCS: a bucket verified as versioned refuses /// the mount (a token-exact DELETE there archives a noncurrent generation instead of reclaiming diff --git a/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.cpp b/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.cpp index 41c3ba97b0b47..097a2dfa2fc8c 100644 --- a/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.cpp +++ b/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.cpp @@ -831,7 +831,7 @@ void S3ObjectStorage::pinConditionalOpsGenerationDialect(bool expect_generation_ pinned_generation_dialect.store(expect_generation_tokens ? 1 : 0); } -std::optional S3ObjectStorage::isBucketVersioningEnabled(const ObjectStorageControlRequest & control_request) const +IObjectStorage::BucketVersioningStatus S3ObjectStorage::getBucketVersioningStatus(const ObjectStorageControlRequest & control_request) const { S3::GetBucketVersioningRequest request; request.SetBucket(uri.bucket); @@ -850,10 +850,33 @@ std::optional S3ObjectStorage::isBucketVersioningEnabled(const ObjectStora /// The caller only learns "unknown"; the reason is what the operator needs to act on. LOG_WARNING(log, "GetBucketVersioning for bucket `{}` failed: {} ({})", uri.bucket, outcome.GetError().GetMessage(), outcome.GetError().GetExceptionName()); - return std::nullopt; + return BucketVersioningStatus::Unknown; } - return outcome.GetResult().GetStatus() == Aws::S3::Model::BucketVersioningStatus::Enabled; + switch (outcome.GetResult().GetStatus()) + { + case Aws::S3::Model::BucketVersioningStatus::Enabled: + return BucketVersioningStatus::Enabled; + case Aws::S3::Model::BucketVersioningStatus::Suspended: + return BucketVersioningStatus::Suspended; + default: + return BucketVersioningStatus::Disabled; + } +} + +std::optional S3ObjectStorage::isBucketVersioningEnabled(const ObjectStorageControlRequest & control_request) const +{ + switch (getBucketVersioningStatus(control_request)) + { + case BucketVersioningStatus::Enabled: + return true; + case BucketVersioningStatus::Disabled: + case BucketVersioningStatus::Suspended: + return false; + case BucketVersioningStatus::Unknown: + return std::nullopt; + } + return std::nullopt; } static void putObjectsTagOnS3( diff --git a/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.h b/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.h index 2d285e289d9b0..7b2e5e7e1f748 100644 --- a/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.h +++ b/src/Disks/DiskObjectStorage/ObjectStorages/S3/S3ObjectStorage.h @@ -195,6 +195,7 @@ class S3ObjectStorage : public IObjectStorage void pinConditionalOpsGenerationDialect(bool expect_generation_tokens) override; + BucketVersioningStatus getBucketVersioningStatus(const ObjectStorageControlRequest & control_request) const override; std::optional isBucketVersioningEnabled(const ObjectStorageControlRequest & control_request) const override; bool supportsRetryProfile(ObjectStorageRetryProfile) const override { return true; } diff --git a/src/Disks/tests/gtest_cas_backend_generation.cpp b/src/Disks/tests/gtest_cas_backend_generation.cpp index f4100060a4ef2..ad2142810c884 100644 --- a/src/Disks/tests/gtest_cas_backend_generation.cpp +++ b/src/Disks/tests/gtest_cas_backend_generation.cpp @@ -38,6 +38,7 @@ using namespace DB::Cas; namespace DB::ErrorCodes { + extern const int CAS_DELETE_MARKER; extern const int CORRUPTED_DATA; extern const int NOT_IMPLEMENTED; } @@ -407,6 +408,102 @@ TEST(CASBackendGeneration, ConditionalWriteSettingsForceSinglePutOnGenerationSto EXPECT_FALSE(*ws2.s3_check_objects_after_upload_override); } +namespace +{ +class SuspendedVersioningObjectStorage final : public DB::LocalObjectStorage +{ +public: + SuspendedVersioningObjectStorage(DB::LocalObjectStorageSettings settings_, BucketVersioningStatus status_) + : DB::LocalObjectStorage(std::move(settings_)), status(status_) + { + } + + BucketVersioningStatus getBucketVersioningStatus(const DB::ObjectStorageControlRequest &) const override + { + return status; + } + + DB::ConditionalRemoveResult removeObjectIfTokenMatches( + const DB::StoredObject &, const std::string &, const DB::ObjectStorageControlRequest &) override + { + return {.outcome = DB::ConditionalRemoveOutcome::Removed, .created_delete_marker = true}; + } + +private: + const BucketVersioningStatus status; +}; + +std::shared_ptr makeSuspendedVersioningStorage( + DB::IObjectStorage::BucketVersioningStatus status) +{ + static std::atomic counter{0}; + const auto unique = std::to_string(::getpid()) + "_susp_" + std::to_string(counter.fetch_add(1)); + const auto root = (std::filesystem::temp_directory_path() / ("cas_unit_susp_" + unique)).string(); + std::error_code ec; + std::filesystem::remove_all(root, ec); + std::filesystem::create_directories(root, ec); + DB::LocalObjectStorageSettings settings("test", root, /*read_only_=*/false); + return DB::Cas::tests::ownedByDirectory( + std::make_shared(std::move(settings), status), root); +} +} + +TEST(CASBackendGeneration, S3SuspendedVersioningRequiresOptInAndStillRefusesEnabled) +{ + using Status = DB::IObjectStorage::BucketVersioningStatus; + auto seed_object = [](const std::shared_ptr & storage) -> String + { + const String key = DB::Cas::tests::nativeKeyUnder(storage, "p/k"); + auto out = storage->writeObject( + DB::StoredObject(key), DB::WriteMode::Rewrite, {}, DB::DBMS_DEFAULT_BUFFER_SIZE, DB::WriteSettings{}); + DB::writeString(String("v1"), *out); + out->finalize(); + return key; + }; + + /// Default (allow_versioning_suspended=false): delete marker throws CAS_DELETE_MARKER. + { + auto storage = makeSuspendedVersioningStorage(Status::Suspended); + const String key = seed_object(storage); + auto b = std::make_shared( + storage, ObjectStorageBackend::Mode::Native, false, 0, 0, /*allow_versioning_suspended=*/false); + EXPECT_NO_THROW(b->checkPoolPreconditions()); + DB::Cas::tests::OperationForTest op(*b); + const auto meta = (*op).head(key, Retry::once()); + ASSERT_TRUE(meta.has_value()); + DB::Cas::tests::expectThrowsCode(DB::ErrorCodes::CAS_DELETE_MARKER, + [&] { (void)(*op).remove(key, meta->etag, Retry::once()); }); + } + + /// Opted in (allow_versioning_suspended=true) on Suspended: checkPoolPreconditions passes and + /// delete markers are accepted as Removed. + { + auto storage = makeSuspendedVersioningStorage(Status::Suspended); + const String key = seed_object(storage); + auto b = std::make_shared( + storage, ObjectStorageBackend::Mode::Native, false, 0, 0, /*allow_versioning_suspended=*/true); + EXPECT_NO_THROW(b->checkPoolPreconditions()); + DB::Cas::tests::OperationForTest op(*b); + const auto meta = (*op).head(key, Retry::once()); + ASSERT_TRUE(meta.has_value()); + EXPECT_EQ((*op).remove(key, meta->etag, Retry::once()), Removal::Removed); + } + + /// Opted in (allow_versioning_suspended=true) on Enabled: checkPoolPreconditions and remove() still refuse. + { + auto storage = makeSuspendedVersioningStorage(Status::Enabled); + const String key = seed_object(storage); + auto b = std::make_shared( + storage, ObjectStorageBackend::Mode::Native, false, 0, 0, /*allow_versioning_suspended=*/true); + expectThrowsNotImplementedSaying("VERSIONING Enabled", [&] { b->checkPoolPreconditions(); }); + DB::Cas::tests::OperationForTest op(*b); + const auto meta = (*op).head(key, Retry::once()); + ASSERT_TRUE(meta.has_value()); + expectThrowsNotImplementedSaying("VERSIONING Enabled", + [&] { (void)(*op).remove(key, meta->etag, Retry::once()); }); + } +} + /// `tokenForHead` and `tokenMatches` were deleted with the `Token` type they built: minting an /// incarnation and comparing it against a precondition are now `CasRequests::mint`/`tryMint` and /// `CasRequests::valueFor`, always stamped with the observing backend's own dialect, so no caller can