Reputation: 57
Using the Azure blob storage SDK I am using the BlobLeaseClient acquirelease method and passing in 15 seconds. However if I kill the process after acquiring the lease the lease remains even after the 15 second duration has passed.
How can I revert the blob lease status to available after a process which took out the lease has crashed? The MS SDK docs are unclear on this usecase.
Upvotes: 2
Views: 70
Reputation: 10455
Java blobleaseclient acquireLease does not return lease to available after due time.
The blob lease is the operation creates and manages a lock on a blob for write and delete operations. The lock duration can be 15 to 60 seconds, or can be infinite.
You can use the below code to handle blob leases, troubleshoot stale leases, and forcefully break them when necessary, using the Azure Storage Blob
SDK.
Code:
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobClientBuilder;
import com.azure.storage.blob.models.BlobProperties;
import com.azure.storage.blob.specialized.BlobLeaseClient;
import com.azure.storage.blob.specialized.BlobLeaseClientBuilder;
public class App {
public static void main(String[] args) {
String connectionString = "xxx";
String containerName = "result";
String blobName = "demo.txt";
BlobClient blobClient = new BlobClientBuilder()
.connectionString(connectionString)
.containerName(containerName)
.blobName(blobName)
.buildClient();
BlobLeaseClient leaseClient = new BlobLeaseClientBuilder()
.blobClient(blobClient)
.buildClient();
try {
// Acquire a lease for 15 seconds
String leaseId = leaseClient.acquireLease(15);
System.out.println("Lease acquired. Lease ID: " + leaseId);
Thread.sleep(5000);
leaseClient.releaseLease();
System.out.println("Lease released successfully.");
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
}
try {
BlobProperties properties = blobClient.getProperties();
System.out.println("Lease Status: " + properties.getLeaseStatus());
System.out.println("Lease State: " + properties.getLeaseState());
if (properties.getLeaseState().toString().equalsIgnoreCase("leased")) {
leaseClient.breakLease();
System.out.println("Stale lease broken successfully.");
}
} catch (Exception e) {
System.err.println("Error while checking or breaking lease: " + e.getMessage());
}
}
}
Output:
Lease acquired. Lease ID: 14b0xxxxx-bb0b-92208d68f765
Lease released successfully.
Lease Status: unlocked
Lease State: available
LeaseStatus
and LeaseState
with getProperties
, and use breakLease()
to release stale leases.Upvotes: 0