yihua commented on code in PR #18472:
URL: https://github.com/apache/hudi/pull/18472#discussion_r3040755616


##########
hudi-azure/src/main/java/org/apache/hudi/azure/transaction/lock/AzureStorageLockClient.java:
##########
@@ -0,0 +1,366 @@
+/*
+ * 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.azure.transaction.lock;
+
+import org.apache.hudi.azure.utils.AzureStorageUtils;
+import org.apache.hudi.azure.utils.AzureStorageUtils.AzureStorageUriComponents;
+import org.apache.hudi.client.transaction.lock.StorageLockClient;
+import org.apache.hudi.client.transaction.lock.models.LockGetResult;
+import org.apache.hudi.client.transaction.lock.models.LockUpsertResult;
+import org.apache.hudi.client.transaction.lock.models.StorageLockData;
+import org.apache.hudi.client.transaction.lock.models.StorageLockFile;
+import org.apache.hudi.common.util.Functions;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+import org.apache.hudi.common.util.VisibleForTesting;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.config.StorageBasedLockConfig;
+
+import com.azure.core.exception.HttpResponseException;
+import com.azure.core.http.rest.Response;
+import com.azure.core.util.BinaryData;
+import com.azure.core.util.Context;
+import com.azure.core.util.HttpClientOptions;
+import com.azure.identity.DefaultAzureCredentialBuilder;
+import com.azure.storage.blob.BlobClient;
+import com.azure.storage.blob.BlobContainerClient;
+import com.azure.storage.blob.BlobServiceClient;
+import com.azure.storage.blob.BlobServiceClientBuilder;
+import com.azure.storage.blob.models.BlobDownloadContentResponse;
+import com.azure.storage.blob.models.BlobErrorCode;
+import com.azure.storage.blob.models.BlobRequestConditions;
+import com.azure.storage.blob.models.BlobStorageException;
+import com.azure.storage.blob.models.BlockBlobItem;
+import com.azure.storage.common.policy.RequestRetryOptions;
+import com.azure.storage.common.policy.RetryPolicyType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import java.io.ByteArrayInputStream;
+import java.net.URI;
+import java.time.Duration;
+import java.util.Properties;
+
+/**
+ * Azure Blob Storage-based distributed lock client using ETag checks.
+ * Supports both WASB(S) and ABFS(S) URI schemes for Azure Data Lake Storage 
Gen2.
+ *
+ * <p>Authentication is handled automatically via DefaultAzureCredential which 
supports:
+ * <ul>
+ *   <li>Managed Identity (Azure VMs, App Service, Container Instances, 
AKS)</li>
+ *   <li>Workload Identity (Kubernetes service accounts)</li>
+ *   <li>Environment variables (AZURE_CLIENT_ID, AZURE_TENANT_ID, 
AZURE_CLIENT_SECRET)</li>
+ *   <li>Azure CLI credentials (for local development)</li>
+ *   <li>VS Code, IntelliJ, and Azure PowerShell credentials</li>
+ * </ul>
+ *
+ * <p>No configuration is required when running on Azure infrastructure.
+ * See RFC: <a 
href="https://github.com/apache/hudi/blob/master/rfc/rfc-91/rfc-91.md";>RFC-91</a>
+ */
+@ThreadSafe
+public class AzureStorageLockClient implements StorageLockClient {
+  private static final Logger LOG = 
LoggerFactory.getLogger(AzureStorageLockClient.class);
+  private static final int PRECONDITION_FAILURE_ERROR_CODE = 412;
+  private static final int NOT_FOUND_ERROR_CODE = 404;
+  private static final int 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 BlobClient blobClient;
+  private final String containerName;
+  private final String blobPath;
+  private final String ownerId;
+
+  /**
+   * Constructor that is used by reflection to instantiate an Azure-based 
storage locking client.
+   *
+   * @param ownerId     The owner id.
+   * @param lockFileUri The full table base path where the lock will be 
written.
+   * @param props       The properties for the lock config, can be used to 
customize client.
+   */
+  public AzureStorageLockClient(String ownerId, String lockFileUri, Properties 
props) {
+    this(ownerId, lockFileUri, props, createDefaultBlobClient(), LOG);
+  }
+
+  @VisibleForTesting
+  AzureStorageLockClient(String ownerId, String lockFileUri, Properties props,
+                         Functions.Function2<String, Properties, BlobClient> 
blobClientSupplier,
+                         Logger logger) {
+    AzureStorageUriComponents uriComponents = 
AzureStorageUtils.parseAzureUri(lockFileUri);
+    this.containerName = uriComponents.containerName;
+    this.blobPath = uriComponents.blobPath;
+
+    this.blobClient = blobClientSupplier.apply(lockFileUri, props);
+    this.ownerId = ownerId;
+    this.logger = logger;
+  }
+
+  @Override
+  public Pair<LockGetResult, Option<StorageLockFile>> readCurrentLockFile() {
+    try {
+      BlobDownloadContentResponse response = 
blobClient.downloadContentWithResponse(
+          null,
+          null,
+          null,
+          Context.NONE
+      );
+
+      // Get ETag from response headers
+      String eTag = response.getHeaders().getValue("ETag");
+      if (eTag != null) {
+        // Azure returns ETags wrapped in quotes, remove them
+        eTag = eTag.replaceAll("^\"|\"$", "");

Review Comment:
   🤖 This quote-stripping creates an ETag format inconsistency that breaks 
conditional writes for the expired-lock takeover path. 
`BlockBlobItem.getETag()` (used in `createOrUpdateLockFileInternal`) returns 
the ETag WITH surrounding double-quotes (e.g. `"0x8D4A"`), which is exactly 
what `BlobRequestConditions.setIfMatch()` expects — it passes the value 
directly to the `If-Match` header, so the quotes must be present for a valid 
HTTP conditional request. By stripping them here, any `setIfMatch` call using 
an ETag sourced from `readCurrentLockFile` sends `If-Match: 0x8D4A` (unquoted), 
while a call using an ETag from a write sends `If-Match: "0x8D4A"` (correctly 
quoted). Removing the `replaceAll` and keeping the raw header value would align 
both paths and avoid this failure mode.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to