@@ -98,12 +98,16 @@ public abstract class ZKDelegationTokenSecretManager<TokenIdent extends Abstract
9898 + "kerberos.keytab" ;
9999 public static final String ZK_DTSM_ZK_KERBEROS_PRINCIPAL = ZK_CONF_PREFIX
100100 + "kerberos.principal" ;
101+ public static final String ZK_DTSM_TOKEN_SEQNUM_BATCH_SIZE = ZK_CONF_PREFIX
102+ + "token.seqnum.batch.size" ;
101103
102104 public static final int ZK_DTSM_ZK_NUM_RETRIES_DEFAULT = 3 ;
103105 public static final int ZK_DTSM_ZK_SESSION_TIMEOUT_DEFAULT = 10000 ;
104106 public static final int ZK_DTSM_ZK_CONNECTION_TIMEOUT_DEFAULT = 10000 ;
105107 public static final int ZK_DTSM_ZK_SHUTDOWN_TIMEOUT_DEFAULT = 10000 ;
106108 public static final String ZK_DTSM_ZNODE_WORKING_PATH_DEAFULT = "zkdtsm" ;
109+ // by default it is still incrementing seq number by 1 each time
110+ public static final int ZK_DTSM_TOKEN_SEQNUM_BATCH_SIZE_DEFAULT = 1 ;
107111
108112 private static Logger LOG = LoggerFactory
109113 .getLogger (ZKDelegationTokenSecretManager .class );
@@ -135,6 +139,9 @@ public static void setCurator(CuratorFramework curator) {
135139 private PathChildrenCache tokenCache ;
136140 private ExecutorService listenerThreadPool ;
137141 private final long shutdownTimeout ;
142+ private final int seqNumBatchSize ;
143+ private int currentSeqNum ;
144+ private int currentMaxSeqNum ;
138145
139146 public ZKDelegationTokenSecretManager (Configuration conf ) {
140147 super (conf .getLong (DelegationTokenManager .UPDATE_INTERVAL ,
@@ -147,6 +154,8 @@ public ZKDelegationTokenSecretManager(Configuration conf) {
147154 DelegationTokenManager .REMOVAL_SCAN_INTERVAL_DEFAULT ) * 1000 );
148155 shutdownTimeout = conf .getLong (ZK_DTSM_ZK_SHUTDOWN_TIMEOUT ,
149156 ZK_DTSM_ZK_SHUTDOWN_TIMEOUT_DEFAULT );
157+ seqNumBatchSize = conf .getInt (ZK_DTSM_TOKEN_SEQNUM_BATCH_SIZE ,
158+ ZK_DTSM_TOKEN_SEQNUM_BATCH_SIZE_DEFAULT );
150159 if (CURATOR_TL .get () != null ) {
151160 zkClient =
152161 CURATOR_TL .get ().usingNamespace (
@@ -322,6 +331,12 @@ public void startThreads() throws IOException {
322331 if (delTokSeqCounter != null ) {
323332 delTokSeqCounter .start ();
324333 }
334+ // the first batch range should be allocated during this starting window
335+ // by calling the incrSharedCount
336+ currentSeqNum = incrSharedCount (delTokSeqCounter , seqNumBatchSize );
337+ currentMaxSeqNum = currentSeqNum + seqNumBatchSize ;
338+ LOG .info ("Fetched initial range of seq num, from {} to {} " ,
339+ currentSeqNum +1 , currentMaxSeqNum );
325340 } catch (Exception e ) {
326341 throw new IOException ("Could not start Sequence Counter" , e );
327342 }
@@ -562,28 +577,41 @@ protected int getDelegationTokenSeqNum() {
562577 return delTokSeqCounter .getCount ();
563578 }
564579
565- private void incrSharedCount (SharedCount sharedCount ) throws Exception {
580+ private int incrSharedCount (SharedCount sharedCount , int batchSize )
581+ throws Exception {
566582 while (true ) {
567583 // Loop until we successfully increment the counter
568584 VersionedValue <Integer > versionedValue = sharedCount .getVersionedValue ();
569- if (sharedCount .trySetCount (versionedValue , versionedValue .getValue () + 1 )) {
570- break ;
585+ if (sharedCount .trySetCount (
586+ versionedValue , versionedValue .getValue () + batchSize )) {
587+ return versionedValue .getValue ();
571588 }
572589 }
573590 }
574591
575592 @ Override
576593 protected int incrementDelegationTokenSeqNum () {
577- try {
578- incrSharedCount (delTokSeqCounter );
579- } catch (InterruptedException e ) {
580- // The ExpirationThread is just finishing.. so dont do anything..
581- LOG .debug ("Thread interrupted while performing token counter increment" , e );
582- Thread .currentThread ().interrupt ();
583- } catch (Exception e ) {
584- throw new RuntimeException ("Could not increment shared counter !!" , e );
594+ // The secret manager will keep a local range of seq num which won't be
595+ // seen by peers, so only when the range is exhausted it will ask zk for
596+ // another range again
597+ if (currentSeqNum >= currentMaxSeqNum ) {
598+ try {
599+ // after a successful batch request, we can get the range starting point
600+ currentSeqNum = incrSharedCount (delTokSeqCounter , seqNumBatchSize );
601+ currentMaxSeqNum = currentSeqNum + seqNumBatchSize ;
602+ LOG .info ("Fetched new range of seq num, from {} to {} " ,
603+ currentSeqNum +1 , currentMaxSeqNum );
604+ } catch (InterruptedException e ) {
605+ // The ExpirationThread is just finishing.. so dont do anything..
606+ LOG .debug (
607+ "Thread interrupted while performing token counter increment" , e );
608+ Thread .currentThread ().interrupt ();
609+ } catch (Exception e ) {
610+ throw new RuntimeException ("Could not increment shared counter !!" , e );
611+ }
585612 }
586- return delTokSeqCounter .getCount ();
613+
614+ return ++currentSeqNum ;
587615 }
588616
589617 @ Override
@@ -603,7 +631,7 @@ protected int getCurrentKeyId() {
603631 @ Override
604632 protected int incrementCurrentKeyId () {
605633 try {
606- incrSharedCount (keyIdSeqCounter );
634+ incrSharedCount (keyIdSeqCounter , 1 );
607635 } catch (InterruptedException e ) {
608636 // The ExpirationThread is just finishing.. so dont do anything..
609637 LOG .debug ("Thread interrupted while performing keyId increment" , e );
0 commit comments