2828import java .util .List ;
2929import java .util .concurrent .atomic .AtomicInteger ;
3030import java .util .concurrent .locks .ReentrantLock ;
31- import java .util .concurrent .locks .ReentrantReadWriteLock ;
3231import java .util .stream .Stream ;
3332
3433import org .apache .curator .ensemble .fixed .FixedEnsembleProvider ;
@@ -148,9 +147,9 @@ protected static CuratorFramework getCurator() {
148147 private CuratorCacheBridge keyCache ;
149148 private CuratorCacheBridge tokenCache ;
150149 private final int seqNumBatchSize ;
151- private AtomicInteger currentSeqNum ;
152- private AtomicInteger currentMaxSeqNum ;
153- private final ReentrantReadWriteLock currentSeqNumLock ;
150+ private int currentSeqNum ;
151+ private int currentMaxSeqNum ;
152+ private final ReentrantLock currentSeqNumLock ;
154153 private final boolean isTokenWatcherEnabled ;
155154
156155 public ZKDelegationTokenSecretManager (Configuration conf ) {
@@ -166,7 +165,7 @@ public ZKDelegationTokenSecretManager(Configuration conf) {
166165 ZK_DTSM_TOKEN_SEQNUM_BATCH_SIZE_DEFAULT );
167166 isTokenWatcherEnabled = conf .getBoolean (ZK_DTSM_TOKEN_WATCHER_ENABLED ,
168167 ZK_DTSM_TOKEN_WATCHER_ENABLED_DEFAULT );
169- this .currentSeqNumLock = new ReentrantReadWriteLock (true );
168+ this .currentSeqNumLock = new ReentrantLock (true );
170169 if (CURATOR_TL .get () != null ) {
171170 zkClient =
172171 CURATOR_TL .get ().usingNamespace (
@@ -285,10 +284,10 @@ public void startThreads() throws IOException {
285284 }
286285 // the first batch range should be allocated during this starting window
287286 // by calling the incrSharedCount
288- currentSeqNum . set ( incrSharedCount (delTokSeqCounter , seqNumBatchSize ) );
289- currentMaxSeqNum . set ( currentSeqNum . get () + seqNumBatchSize ) ;
287+ currentSeqNum = incrSharedCount (delTokSeqCounter , seqNumBatchSize );
288+ currentMaxSeqNum = currentSeqNum + seqNumBatchSize ;
290289 LOG .info ("Fetched initial range of seq num, from {} to {} " ,
291- currentSeqNum . incrementAndGet () , currentMaxSeqNum );
290+ currentSeqNum + 1 , currentMaxSeqNum );
292291 } catch (Exception e ) {
293292 throw new IOException ("Could not start Sequence Counter" , e );
294293 }
@@ -524,22 +523,14 @@ protected int incrementDelegationTokenSeqNum() {
524523 // seen by peers, so only when the range is exhausted it will ask zk for
525524 // another range again
526525 try {
527- this .currentSeqNumLock .readLock (). lock ();
528- if (currentSeqNum . get () >= currentMaxSeqNum . get () ) {
526+ this .currentSeqNumLock .lock ();
527+ if (currentSeqNum >= currentMaxSeqNum ) {
529528 try {
530529 // after a successful batch request, we can get the range starting point
531- this .currentSeqNumLock .readLock ().unlock ();
532- try {
533- this .currentSeqNumLock .writeLock ().lock ();
534- if (currentSeqNum .get () >= currentMaxSeqNum .get ()) {
535- currentSeqNum .set (incrSharedCount (delTokSeqCounter , seqNumBatchSize ));
536- currentMaxSeqNum .set (currentSeqNum .get () + seqNumBatchSize );
537- LOG .info ("Fetched new range of seq num, from {} to {} " ,
538- currentSeqNum .get ()+1 , currentMaxSeqNum );
539- }
540- } finally {
541- this .currentSeqNumLock .writeLock ().unlock ();
542- }
530+ currentSeqNum = incrSharedCount (delTokSeqCounter , seqNumBatchSize );
531+ currentMaxSeqNum = currentSeqNum + seqNumBatchSize ;
532+ LOG .info ("Fetched new range of seq num, from {} to {} " ,
533+ currentSeqNum +1 , currentMaxSeqNum );
543534 } catch (InterruptedException e ) {
544535 // The ExpirationThread is just finishing.. so dont do anything..
545536 LOG .debug (
@@ -549,12 +540,10 @@ protected int incrementDelegationTokenSeqNum() {
549540 throw new RuntimeException ("Could not increment shared counter !!" , e );
550541 }
551542 }
552- return currentSeqNum . incrementAndGet () ;
543+ return ++ currentSeqNum ;
553544 } finally {
554- if ( this .currentSeqNumLock .getReadHoldCount () > 0 ) {
555- this .currentSeqNumLock .readLock ().unlock ();
545+ this .currentSeqNumLock .unlock ();
556546 }
557- }
558547 }
559548
560549 @ Override
0 commit comments