Skip to content

Commit ab7a3a6

Browse files
committed
HADOOP-18851: Perfm improvement for ZKDT management
1 parent da69485 commit ab7a3a6

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/delegation/AbstractDelegationTokenSecretManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,12 @@ protected byte[] createPassword(TokenIdent identifier) {
492492
sequenceNum = incrementDelegationTokenSeqNum();
493493
identifier.setIssueDate(now);
494494
identifier.setMaxDate(now + tokenMaxLifetime);
495-
identifier.setMasterKeyId(currentKey.getKeyId());
495+
DelegationKey delegationCurrentKey = currentKey;
496+
identifier.setMasterKeyId(delegationCurrentKey.getKeyId());
496497
identifier.setSequenceNumber(sequenceNum);
497498
LOG.info("Creating password for identifier: " + formatTokenId(identifier)
498-
+ ", currentKey: " + currentKey.getKeyId());
499-
byte[] password = createPassword(identifier.getBytes(), currentKey.getKey());
499+
+ ", currentKey: " + delegationCurrentKey.getKeyId());
500+
byte[] password = createPassword(identifier.getBytes(), delegationCurrentKey.getKey());
500501
DelegationTokenInformation tokenInfo = new DelegationTokenInformation(now
501502
+ tokenRenewInterval, password, getTrackingIdIfEnabled(identifier));
502503
try {

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/delegation/ZKDelegationTokenSecretManager.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.List;
2929
import java.util.concurrent.atomic.AtomicInteger;
3030
import java.util.concurrent.locks.ReentrantLock;
31-
import java.util.concurrent.locks.ReentrantReadWriteLock;
3231
import java.util.stream.Stream;
3332

3433
import 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

Comments
 (0)