2121import java .io .IOException ;
2222import java .util .concurrent .TimeUnit ;
2323
24+ import org .apache .hadoop .thirdparty .com .google .common .annotations .VisibleForTesting ;
2425import org .apache .hadoop .thirdparty .com .google .common .util .concurrent .FutureCallback ;
2526import org .apache .hadoop .thirdparty .com .google .common .util .concurrent .ListenableScheduledFuture ;
2627import org .apache .hadoop .thirdparty .org .checkerframework .checker .nullness .qual .Nullable ;
28+
2729import org .slf4j .Logger ;
2830import org .slf4j .LoggerFactory ;
2931
5153public final class AbfsLease {
5254 private static final Logger LOG = LoggerFactory .getLogger (AbfsLease .class );
5355
54- static final int LEASE_ACQUIRE_RETRY_INTERVAL = 10 ; // Retry interval for acquiring lease in secs
55- static final int LEASE_ACQUIRE_MAX_RETRIES = 7 ; // Number of retries for acquiring lease
56+ // Number of retries for acquiring lease
57+ static final int DEFAULT_LEASE_ACQUIRE_MAX_RETRIES = 7 ;
58+ // Retry interval for acquiring lease in secs
59+ static final int DEFAULT_LEASE_ACQUIRE_RETRY_INTERVAL = 10 ;
5660
5761 private final AbfsClient client ;
5862 private final String path ;
@@ -61,6 +65,7 @@ public final class AbfsLease {
6165 private volatile boolean leaseFreed ;
6266 private volatile String leaseID = null ;
6367 private volatile Throwable exception = null ;
68+ private volatile int acquireRetryCount = 0 ;
6469 private volatile ListenableScheduledFuture <AbfsRestOperation > future = null ;
6570
6671 public static class LeaseException extends AzureBlobFileSystemException {
@@ -74,6 +79,12 @@ public LeaseException(String s) {
7479 }
7580
7681 public AbfsLease (AbfsClient client , String path ) throws AzureBlobFileSystemException {
82+ this (client , path , DEFAULT_LEASE_ACQUIRE_MAX_RETRIES , DEFAULT_LEASE_ACQUIRE_RETRY_INTERVAL );
83+ }
84+
85+ @ VisibleForTesting
86+ public AbfsLease (AbfsClient client , String path , int acquireMaxRetries ,
87+ int acquireRetryInterval ) throws AzureBlobFileSystemException {
7788 this .leaseFreed = false ;
7889 this .client = client ;
7990 this .path = path ;
@@ -84,8 +95,8 @@ public AbfsLease(AbfsClient client, String path) throws AzureBlobFileSystemExcep
8495
8596 // Try to get the lease a specified number of times, else throw an error
8697 RetryPolicy retryPolicy = RetryPolicies .retryUpToMaximumCountWithFixedSleep (
87- LEASE_ACQUIRE_MAX_RETRIES , LEASE_ACQUIRE_RETRY_INTERVAL , TimeUnit .SECONDS );
88- acquireLease (retryPolicy , 0 , 0 );
98+ acquireMaxRetries , acquireRetryInterval , TimeUnit .SECONDS );
99+ acquireLease (retryPolicy , 0 , acquireRetryInterval , 0 );
89100
90101 while (leaseID == null && exception == null ) {
91102 try {
@@ -103,7 +114,7 @@ public AbfsLease(AbfsClient client, String path) throws AzureBlobFileSystemExcep
103114 LOG .debug ("Acquired lease {} on {}" , leaseID , path );
104115 }
105116
106- private void acquireLease (RetryPolicy retryPolicy , int numRetries , long delay )
117+ private void acquireLease (RetryPolicy retryPolicy , int numRetries , int retryInterval , long delay )
107118 throws LeaseException {
108119 LOG .debug ("Attempting to acquire lease on {}, retry {}" , path , numRetries );
109120 if (future != null && !future .isDone ()) {
@@ -124,7 +135,8 @@ public void onFailure(Throwable throwable) {
124135 if (RetryPolicy .RetryAction .RetryDecision .RETRY
125136 == retryPolicy .shouldRetry (null , numRetries , 0 , true ).action ) {
126137 LOG .debug ("Failed to acquire lease on {}, retrying: {}" , path , throwable );
127- acquireLease (retryPolicy , numRetries + 1 , LEASE_ACQUIRE_RETRY_INTERVAL );
138+ acquireRetryCount ++;
139+ acquireLease (retryPolicy , numRetries + 1 , retryInterval , retryInterval );
128140 } else {
129141 exception = throwable ;
130142 }
@@ -168,4 +180,9 @@ public boolean isFreed() {
168180 public String getLeaseID () {
169181 return leaseID ;
170182 }
183+
184+ @ VisibleForTesting
185+ public int getAcquireRetryCount () {
186+ return acquireRetryCount ;
187+ }
171188}
0 commit comments