You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "mrcnc (via GitHub)" <gi...@apache.org> on 2024/11/16 14:32:32 UTC

[PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

mrcnc opened a new pull request, #11565:
URL: https://github.com/apache/iceberg/pull/11565

   Inspired by [the initial implementation in S3FileIO](https://github.com/apache/iceberg/pull/10721), this PR implements `SupportsRecoveryOperations` for GCSFileIO.  
   
   GCP recommends using soft delete instead of [object versioning](https://cloud.google.com/storage/docs/object-versioning), so this implementation attempts to-restore a soft deleted object first.  If soft delete is not enabled (which may be common since it's a [relatively new feature](https://cloud.google.com/blog/products/storage-data-transfer/understanding-cloud-storages-new-soft-delete-feature)), then it attempts to restore a previously deleted version by coping it to the live location, similar to the S3FileIO implementation. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #11565: GCP: Implement SupportsRecoveryOperations for GCSFileIO
URL: https://github.com/apache/iceberg/pull/11565


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#issuecomment-2581488404

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#issuecomment-2599402092

   This pull request has been closed due to lack of activity. This is not a judgement on the merit of the PR in any way. It is just a way of keeping the PR queue manageable. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1878222555


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +248,106 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }

Review Comment:
   Actually just noticed this, do we really need the explicit catch for IllegalArgumentException in case the path format is incorrect? I consider this just like the null or empty case where `recoverFile` can just throw. The best effort really is for the calls to attempt recovery, not the other validation imo.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "mrcnc (via GitHub)" <gi...@apache.org>.
mrcnc commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1866849501


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +250,116 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      BucketInfo.SoftDeletePolicy policy = client().get(blobId.getBucket()).getSoftDeletePolicy();
+      if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {

Review Comment:
   Also did the same for the bucket versioning check in https://github.com/apache/iceberg/pull/11565/commits/360eadd351a803f24c901560c461d9ab37d150e6



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1857166857


##########
gcp/src/test/java/org/apache/iceberg/gcp/gcs/GCSFileIOTest.java:
##########
@@ -270,4 +283,136 @@ public void refreshCredentialsEndpointSetButRefreshDisabled() {
 
     assertThat(client.getOptions().getCredentials()).isInstanceOf(OAuth2Credentials.class);
   }
+
+  @Test
+  void recoverSoftDeletedObject_WhenSoftDeleteDisabled_ReturnsFalse() {
+    BlobId blobId = BlobId.of(TEST_BUCKET, "object");
+    Bucket bucket = mock(Bucket.class);
+    BucketInfo.SoftDeletePolicy policy = mock(BucketInfo.SoftDeletePolicy.class);
+
+    when(storage.get(TEST_BUCKET)).thenReturn(bucket);
+    when(bucket.getSoftDeletePolicy()).thenReturn(policy);
+    when(policy.getRetentionDuration()).thenReturn(Duration.ofSeconds(0)); // soft-delete disabled
+
+    assertThat(io.recoverSoftDeletedObject(blobId)).isFalse();
+  }
+
+  @Test
+  void recoverSoftDeletedObject_WhenSoftDeletedBlobExists_ReturnsTrue() {

Review Comment:
   I feel like we can shrink the method names here and remove the underscore separators as part of that, just so it fits the existing naming pattern in these tests.
   
   e.g. `recoverSoftDeletedObject`



##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +250,116 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      BucketInfo.SoftDeletePolicy policy = client().get(blobId.getBucket()).getSoftDeletePolicy();
+      if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {

Review Comment:
   Actually do we need the soft delete policy check to begin with? Is it not possible to jump directly to attempting the listing of versions with softDeleted(true) as done below, and if nothing gets returned we know that recovering via soft delete wasn't effective? Or will the list versions call fail?



##########
gcp/src/test/java/org/apache/iceberg/gcp/gcs/GCSFileIOTest.java:
##########
@@ -270,4 +283,136 @@ public void refreshCredentialsEndpointSetButRefreshDisabled() {
 
     assertThat(client.getOptions().getCredentials()).isInstanceOf(OAuth2Credentials.class);
   }
+
+  @Test
+  void recoverSoftDeletedObject_WhenSoftDeleteDisabled_ReturnsFalse() {
+    BlobId blobId = BlobId.of(TEST_BUCKET, "object");
+    Bucket bucket = mock(Bucket.class);
+    BucketInfo.SoftDeletePolicy policy = mock(BucketInfo.SoftDeletePolicy.class);
+
+    when(storage.get(TEST_BUCKET)).thenReturn(bucket);
+    when(bucket.getSoftDeletePolicy()).thenReturn(policy);
+    when(policy.getRetentionDuration()).thenReturn(Duration.ofSeconds(0)); // soft-delete disabled
+
+    assertThat(io.recoverSoftDeletedObject(blobId)).isFalse();
+  }
+
+  @Test
+  void recoverSoftDeletedObject_WhenSoftDeletedBlobExists_ReturnsTrue() {
+    BlobId blobId = BlobId.of(TEST_BUCKET, "object");
+    Bucket bucket = mock(Bucket.class);
+    BucketInfo.SoftDeletePolicy policy = mock(BucketInfo.SoftDeletePolicy.class);
+    Page<Blob> page = mock(Page.class);
+    Blob softDeletedBlob = mock(Blob.class);
+
+    when(storage.get(TEST_BUCKET)).thenReturn(bucket);
+    when(bucket.getSoftDeletePolicy()).thenReturn(policy);
+    when(policy.getRetentionDuration()).thenReturn(Duration.ofDays(7)); // soft-delete enabled
+    doAnswer(invoke -> page).when(storage).list(eq(TEST_BUCKET), any(), any());
+    when(page.streamAll()).thenReturn(Stream.of(softDeletedBlob));
+    when(softDeletedBlob.getName()).thenReturn("object");
+    when(softDeletedBlob.getSoftDeleteTime()).thenReturn(OffsetDateTime.now());
+    when(softDeletedBlob.getBlobId()).thenReturn(blobId);
+    doAnswer(invocation -> softDeletedBlob).when(storage).restore(any(), any());
+
+    assertThat(io.recoverSoftDeletedObject(blobId)).isTrue();
+  }
+
+  @Test
+  void recoverSoftDeletedObject_WhenNoSoftDeletedBlobExists_ReturnsFalse() {

Review Comment:
   `cannotRecoverSoftDeletedObjectWhenNonExistant` 



##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +250,116 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      BucketInfo.SoftDeletePolicy policy = client().get(blobId.getBucket()).getSoftDeletePolicy();
+      if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {

Review Comment:
   I think we'll need a null check for policy == null before so I think it should be
   
   ```
   if (policy == null || Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {
        return null
   }
   ```
   
   The GCP sdk https://github.com/googleapis/google-api-java-client-services/blob/350652f6480c1dddf1c7dc2a966434deaeaa4964/clients/google-api-services-storage/v1/2.0.0/com/google/api/services/storage/model/Bucket.java#L911 API docs indicate it's that the soft delete policy will be null in case it doesn't exist. Looks like Beam does the same check (inverted) as well https://github.com/apache/beam/blob/78630d15445fdf54935d6ba99c2fd9d0930b6af8/sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/options/GcpOptions.java#L419



##########
gcp/src/test/java/org/apache/iceberg/gcp/gcs/GCSFileIOTest.java:
##########
@@ -270,4 +283,136 @@ public void refreshCredentialsEndpointSetButRefreshDisabled() {
 
     assertThat(client.getOptions().getCredentials()).isInstanceOf(OAuth2Credentials.class);
   }
+
+  @Test
+  void recoverSoftDeletedObject_WhenSoftDeleteDisabled_ReturnsFalse() {
+    BlobId blobId = BlobId.of(TEST_BUCKET, "object");
+    Bucket bucket = mock(Bucket.class);
+    BucketInfo.SoftDeletePolicy policy = mock(BucketInfo.SoftDeletePolicy.class);
+
+    when(storage.get(TEST_BUCKET)).thenReturn(bucket);
+    when(bucket.getSoftDeletePolicy()).thenReturn(policy);
+    when(policy.getRetentionDuration()).thenReturn(Duration.ofSeconds(0)); // soft-delete disabled
+
+    assertThat(io.recoverSoftDeletedObject(blobId)).isFalse();
+  }
+
+  @Test
+  void recoverSoftDeletedObject_WhenSoftDeletedBlobExists_ReturnsTrue() {
+    BlobId blobId = BlobId.of(TEST_BUCKET, "object");
+    Bucket bucket = mock(Bucket.class);
+    BucketInfo.SoftDeletePolicy policy = mock(BucketInfo.SoftDeletePolicy.class);
+    Page<Blob> page = mock(Page.class);
+    Blob softDeletedBlob = mock(Blob.class);
+
+    when(storage.get(TEST_BUCKET)).thenReturn(bucket);
+    when(bucket.getSoftDeletePolicy()).thenReturn(policy);
+    when(policy.getRetentionDuration()).thenReturn(Duration.ofDays(7)); // soft-delete enabled
+    doAnswer(invoke -> page).when(storage).list(eq(TEST_BUCKET), any(), any());
+    when(page.streamAll()).thenReturn(Stream.of(softDeletedBlob));
+    when(softDeletedBlob.getName()).thenReturn("object");
+    when(softDeletedBlob.getSoftDeleteTime()).thenReturn(OffsetDateTime.now());
+    when(softDeletedBlob.getBlobId()).thenReturn(blobId);
+    doAnswer(invocation -> softDeletedBlob).when(storage).restore(any(), any());
+
+    assertThat(io.recoverSoftDeletedObject(blobId)).isTrue();
+  }
+
+  @Test
+  void recoverSoftDeletedObject_WhenNoSoftDeletedBlobExists_ReturnsFalse() {
+    BlobId blobId = BlobId.of(TEST_BUCKET, "object");
+    Bucket bucket = mock(Bucket.class);
+    BucketInfo.SoftDeletePolicy policy = mock(BucketInfo.SoftDeletePolicy.class);
+    Page<Blob> page = mock(Page.class);
+
+    when(storage.get(TEST_BUCKET)).thenReturn(bucket);
+    when(bucket.getSoftDeletePolicy()).thenReturn(policy);
+    when(policy.getRetentionDuration()).thenReturn(Duration.ofDays(7)); // soft-delete enabled
+    when(page.streamAll()).thenReturn(Stream.empty());
+
+    assertThat(io.recoverSoftDeletedObject(blobId)).isFalse();
+  }
+
+  @Test
+  void recoverSoftDelete_WhenStorageException_ReturnsFalse() {

Review Comment:
   `cannotRecoverSoftDeletedWhenStorageExceptionThrown`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1857172654


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +250,116 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      BucketInfo.SoftDeletePolicy policy = client().get(blobId.getBucket()).getSoftDeletePolicy();
+      if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {
+        LOG.warn("Soft delete is disabled for {}", blobId.getBucket());
+        return false;
+      }
+
+      Optional<Blob> latestSoftDeletedBlob =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.softDeleted(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getSoftDeleteTime));
+
+      if (latestSoftDeletedBlob.isPresent()) {
+        client().restore(latestSoftDeletedBlob.get().getBlobId());
+        LOG.info("Soft delete object restored file {}", blobId);
+        return true;
+      }
+      LOG.warn("No soft deleted object was found");
+
+    } catch (StorageException e) {
+      LOG.warn("Failed to restore", e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore the latest deleted object version.
+   *
+   * <p>See <a href="https://cloud.google.com/storage/docs/using-versioned-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverLatestVersion(BlobId blobId) {
+    try {
+      if (!client().get(blobId.getBucket()).versioningEnabled()) {
+        LOG.warn("Object versioning is disabled for {}", blobId.getBucket());
+        return false;
+      }
+
+      Optional<Blob> latestVersion =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.versions(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getUpdateTimeOffsetDateTime));
+
+      if (latestVersion.isPresent() && latestVersion.get().getDeleteTimeOffsetDateTime() != null) {

Review Comment:
   Could we embed the `latestVersion.getDeleteTimeOffsetDateTime` check in the filter above? Then it's the latestVersionBeforeDeletion  or latestVersionBeforeRemoval 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1857172654


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +250,116 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      BucketInfo.SoftDeletePolicy policy = client().get(blobId.getBucket()).getSoftDeletePolicy();
+      if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {
+        LOG.warn("Soft delete is disabled for {}", blobId.getBucket());
+        return false;
+      }
+
+      Optional<Blob> latestSoftDeletedBlob =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.softDeleted(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getSoftDeleteTime));
+
+      if (latestSoftDeletedBlob.isPresent()) {
+        client().restore(latestSoftDeletedBlob.get().getBlobId());
+        LOG.info("Soft delete object restored file {}", blobId);
+        return true;
+      }
+      LOG.warn("No soft deleted object was found");
+
+    } catch (StorageException e) {
+      LOG.warn("Failed to restore", e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore the latest deleted object version.
+   *
+   * <p>See <a href="https://cloud.google.com/storage/docs/using-versioned-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverLatestVersion(BlobId blobId) {
+    try {
+      if (!client().get(blobId.getBucket()).versioningEnabled()) {
+        LOG.warn("Object versioning is disabled for {}", blobId.getBucket());
+        return false;
+      }
+
+      Optional<Blob> latestVersion =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.versions(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getUpdateTimeOffsetDateTime));
+
+      if (latestVersion.isPresent() && latestVersion.get().getDeleteTimeOffsetDateTime() != null) {

Review Comment:
   Could we embed the `latestVersion.getDeleteTimeOffsetDateTime` check in the filter above? Then it's the latestVersionBeforeDeletion 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1878222555


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +248,106 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }

Review Comment:
   Actually just noticed this, not sure it makes sense to swallow the IllegalArgumentException in case the path format is incorrect? I consider this just like the null or empty case where `recoverFile` can just throw. The best effort really is for the calls to attempt recovery, not the other validation imo.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "mrcnc (via GitHub)" <gi...@apache.org>.
mrcnc commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1866755677


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +250,116 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      BucketInfo.SoftDeletePolicy policy = client().get(blobId.getBucket()).getSoftDeletePolicy();
+      if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) {

Review Comment:
   Good point!  I think we can remove the check for the soft delete policy b/c the list will return 0 soft deleted objects



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] GCP: Implement SupportsRecoveryOperations for GCSFileIO [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #11565:
URL: https://github.com/apache/iceberg/pull/11565#discussion_r1878209991


##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +248,106 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      Optional<Blob> latestSoftDeletedBlob =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.softDeleted(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getSoftDeleteTime));
+
+      if (latestSoftDeletedBlob.isPresent()) {
+        client().restore(latestSoftDeletedBlob.get().getBlobId());
+        LOG.info("Soft delete object restored file {}", blobId);
+        return true;
+      }
+      LOG.warn("No soft deleted object was found");
+
+    } catch (StorageException e) {
+      LOG.warn("Failed to restore", e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore the latest deleted object version.
+   *
+   * <p>See <a href="https://cloud.google.com/storage/docs/using-versioned-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverLatestVersion(BlobId blobId) {
+    try {
+      Optional<Blob> latestDeletedVersion =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.versions(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getUpdateTimeOffsetDateTime))
+              .filter(blob -> blob.getDeleteTimeOffsetDateTime() != null);
+
+      if (latestDeletedVersion.isPresent()) {
+        Storage.CopyRequest copyRequest =
+            Storage.CopyRequest.newBuilder()
+                .setSource(latestDeletedVersion.get().getBlobId())
+                .setTarget(blobId)
+                .build();
+        Blob blob = client().copy(copyRequest).getResult();
+        LOG.info("Latest deleted version was restored for {}", blob.getBlobId());
+        return true;
+      }
+      LOG.warn("No latest deleted version was found");
+
+    } catch (StorageException e) {
+      LOG.warn("Failed to restore latest deleted version", e);

Review Comment:
   Same as above



##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +248,106 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      Optional<Blob> latestSoftDeletedBlob =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.softDeleted(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getSoftDeleteTime));
+
+      if (latestSoftDeletedBlob.isPresent()) {
+        client().restore(latestSoftDeletedBlob.get().getBlobId());
+        LOG.info("Soft delete object restored file {}", blobId);
+        return true;
+      }
+      LOG.warn("No soft deleted object was found");
+
+    } catch (StorageException e) {
+      LOG.warn("Failed to restore", e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore the latest deleted object version.
+   *
+   * <p>See <a href="https://cloud.google.com/storage/docs/using-versioned-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverLatestVersion(BlobId blobId) {
+    try {
+      Optional<Blob> latestDeletedVersion =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.versions(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getUpdateTimeOffsetDateTime))
+              .filter(blob -> blob.getDeleteTimeOffsetDateTime() != null);
+
+      if (latestDeletedVersion.isPresent()) {
+        Storage.CopyRequest copyRequest =
+            Storage.CopyRequest.newBuilder()
+                .setSource(latestDeletedVersion.get().getBlobId())
+                .setTarget(blobId)
+                .build();
+        Blob blob = client().copy(copyRequest).getResult();
+        LOG.info("Latest deleted version was restored for {}", blob.getBlobId());
+        return true;
+      }
+      LOG.warn("No latest deleted version was found");

Review Comment:
   Could we include the blobID in the log? 



##########
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java:
##########
@@ -242,4 +248,106 @@ private void internalDeleteFiles(Stream<BlobId> blobIdsToDelete) {
     Streams.stream(Iterators.partition(blobIdsToDelete.iterator(), gcpProperties.deleteBatchSize()))
         .forEach(batch -> client().delete(batch));
   }
+
+  @Override
+  public boolean recoverFile(String path) {
+    Preconditions.checkArgument(
+        !Strings.isNullOrEmpty(path), "Cannot recover file: path must not be null or empty");
+
+    try {
+      BlobId blobId = BlobId.fromGsUtilUri(path);
+
+      // first attempt to restore with soft-delete
+      if (recoverSoftDeletedObject(blobId)) {
+        return true;
+      }
+
+      // fallback to restoring by copying the latest version
+      if (recoverLatestVersion(blobId)) {
+        return true;
+      }
+
+    } catch (IllegalArgumentException e) {
+      LOG.warn("Invalid GCS path format: {}", path, e);
+    }
+
+    return false;
+  }
+
+  /**
+   * Attempts to restore a soft-deleted object.
+   *
+   * <p>Requires {@code storage.objects.restore} permission
+   *
+   * <p>See <a
+   * href="https://cloud.google.com/storage/docs/use-soft-deleted-objects#restore">docs</a>
+   *
+   * @param blobId the blob identifier
+   * @return {@code true} if blob was recovered, {@code false} if not
+   */
+  protected boolean recoverSoftDeletedObject(BlobId blobId) {
+    try {
+      Optional<Blob> latestSoftDeletedBlob =
+          client()
+              .list(
+                  blobId.getBucket(),
+                  Storage.BlobListOption.prefix(blobId.getName()),
+                  Storage.BlobListOption.softDeleted(true))
+              .streamAll()
+              .filter(blob -> blob.getName().equals(blobId.getName()))
+              .max(Comparator.comparing(Blob::getSoftDeleteTime));
+
+      if (latestSoftDeletedBlob.isPresent()) {
+        client().restore(latestSoftDeletedBlob.get().getBlobId());
+        LOG.info("Soft delete object restored file {}", blobId);
+        return true;
+      }
+      LOG.warn("No soft deleted object was found");
+
+    } catch (StorageException e) {
+      LOG.warn("Failed to restore", e);

Review Comment:
   Same as below, could we include the blobID in these log messages? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org