nsivabalan commented on code in PR #13126:
URL: https://github.com/apache/hudi/pull/13126#discussion_r2040300584


##########
hudi-aws/src/main/java/org/apache/hudi/aws/transaction/lock/S3StorageLockClient.java:
##########
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.hudi.aws.transaction.lock;
+
+import org.apache.hudi.aws.credentials.HoodieAWSCredentialsProviderFactory;
+import org.apache.hudi.client.transaction.lock.models.LockGetResult;
+import org.apache.hudi.client.transaction.lock.models.LockUpdateResult;
+import org.apache.hudi.client.transaction.lock.StorageLock;
+import org.apache.hudi.client.transaction.lock.models.StorageLockData;
+import org.apache.hudi.client.transaction.lock.models.StorageLockFile;
+import org.apache.hudi.common.util.VisibleForTesting;
+import org.apache.hudi.common.util.collection.Pair;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.awscore.exception.AwsServiceException;
+import software.amazon.awssdk.core.ResponseInputStream;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.core.sync.RequestBody;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
+import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.model.GetObjectRequest;
+import software.amazon.awssdk.services.s3.model.GetObjectResponse;
+import software.amazon.awssdk.services.s3.model.PutObjectRequest;
+import software.amazon.awssdk.services.s3.model.PutObjectResponse;
+import software.amazon.awssdk.services.s3.model.S3Exception;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Properties;
+import java.util.function.Supplier;
+
+/**
+ * S3-based distributed lock service using ETag checks (AWS SDK v2).
+ * See RFC: <a 
href="https://github.com/apache/hudi/blob/master/rfc/rfc-91/rfc-91.md";>RFC-91</a>
+ */
+@ThreadSafe
+public class S3StorageLockClient implements StorageLock {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(S3StorageLockClient.class);
+  private static final long WAIT_TIME_FOR_RETRY_MS = 1000L;
+  private static final int PRECONDITION_FAILURE_ERROR_CODE = 412;
+  private static final int NOT_FOUND_ERROR_CODE = 404;
+  private static final int CONDITIONAL_REQUEST_CONFLICT_ERROR_CODE = 409;
+  private static final int RATE_LIMIT_ERROR_CODE = 429;
+  private static final int INTERNAL_SERVER_ERROR_CODE_MIN = 500;
+
+  private final Logger logger;
+  private final S3Client s3Client;
+  private final String bucketName;
+  private final String lockFilePath;
+  private final String ownerId;
+
+  /** Constructor that is used by reflection to instantiate an S3-based 
storage locking client.
+   * @param ownerId The owner id.
+   * @param bucketName The name of the bucket.
+   * @param lockFilePath The path within the bucket where to write lock files.
+   * @param props The properties for the lock config, can be used to customize 
client.
+   */
+  public S3StorageLockClient(String ownerId, String bucketName, String 
lockFilePath, Properties props) {
+    this(
+            ownerId,
+            bucketName,
+            lockFilePath,
+            createDefaultS3Client(props),
+            LOG);
+  }
+
+  @VisibleForTesting
+  S3StorageLockClient(String ownerId,
+                                String bucketName,
+                                String lockFilePath,
+                                S3Client s3Client,
+                                Logger logger) {
+    this.s3Client = s3Client;
+    this.lockFilePath = lockFilePath;
+    this.bucketName = bucketName;
+    this.ownerId = ownerId;
+    this.logger = logger;
+  }
+
+  private static S3Client createDefaultS3Client(Properties props) {
+    Region region;
+    try {
+      region = DefaultAwsRegionProviderChain.builder().build().getRegion();
+    } catch (SdkClientException e) {
+      // Fallback to us-east-1 if no region is found
+      region = Region.US_EAST_1;

Review Comment:
   not sure if this is unnecessary overkill. 
   but we could create a S3 client w/ US_EAST_1 and get the bucket location 
using 
   ```
   GetBucketLocationResponse bucketLocationResponse = 
s3Client.getBucketLocation(GetBucketLocationRequest.builder().bucket(bucketName).build());
       String bucketRegion = 
bucketLocationResponse.locationConstraint().toString();
   ```
   
   and then re-instantiate the S3client if region is different. 



-- 
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: commits-unsubscr...@hudi.apache.org

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

Reply via email to