Skip to content

Commit 5a41445

Browse files
author
slfan1989
committed
HADOOP-18427. Improve ZKDelegationTokenSecretManager#startThead With recommended methods.
1 parent 5567154 commit 5a41445

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.apache.curator.framework.recipes.shared.SharedCount;
4343
import org.apache.curator.framework.recipes.shared.VersionedValue;
4444
import org.apache.curator.retry.RetryNTimes;
45-
import org.apache.curator.utils.EnsurePath;
4645
import org.apache.hadoop.classification.InterfaceAudience;
4746
import org.apache.hadoop.classification.InterfaceAudience.Private;
4847
import org.apache.hadoop.classification.InterfaceStability.Unstable;
@@ -134,6 +133,11 @@ public static void setCurator(CuratorFramework curator) {
134133
CURATOR_TL.set(curator);
135134
}
136135

136+
@VisibleForTesting
137+
protected static CuratorFramework getCurator() {
138+
return CURATOR_TL.get();
139+
}
140+
137141
private final boolean isExternalClient;
138142
protected final CuratorFramework zkClient;
139143
private SharedCount delTokSeqCounter;
@@ -260,10 +264,8 @@ public void startThreads() throws IOException {
260264
// If namespace parents are implicitly created, they won't have ACLs.
261265
// So, let's explicitly create them.
262266
CuratorFramework nullNsFw = zkClient.usingNamespace(null);
263-
EnsurePath ensureNs =
264-
nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace());
265267
try {
266-
ensureNs.ensure(nullNsFw.getZookeeperClient());
268+
nullNsFw.create().creatingParentContainersIfNeeded().forPath("/" + zkClient.getNamespace());
267269
} catch (Exception e) {
268270
throw new IOException("Could not create namespace", e);
269271
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestZKDelegationTokenSecretManager.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.zookeeper.ZooDefs;
4242
import org.apache.zookeeper.data.ACL;
4343
import org.apache.zookeeper.data.Id;
44+
import org.apache.zookeeper.data.Stat;
4445
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
4546
import org.junit.After;
4647
import org.junit.Assert;
@@ -507,4 +508,32 @@ public Boolean get() {
507508
}
508509
}, 1000, 5000);
509510
}
511+
512+
@Test
513+
public void testCreatingParentContainersIfNeeded() throws Exception {
514+
515+
String connectString = zkServer.getConnectString();
516+
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
517+
Configuration conf = getSecretConf(connectString);
518+
CuratorFramework curatorFramework =
519+
CuratorFrameworkFactory.builder()
520+
.connectString(connectString)
521+
.retryPolicy(retryPolicy)
522+
.build();
523+
curatorFramework.start();
524+
ZKDelegationTokenSecretManager.setCurator(curatorFramework);
525+
DelegationTokenManager tm1 = new DelegationTokenManager(conf, new Text("foo"));
526+
527+
// When the init method is called,
528+
// the ZKDelegationTokenSecretManager#startThread method will be called,
529+
// and the creatingParentContainersIfNeeded will be called to create the nameSpace
530+
tm1.init();
531+
532+
String workingPath = "/" + conf.get(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH,
533+
ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH_DEAFULT) + "/ZKDTSMRoot";
534+
535+
// Check if the created NameSpace exists
536+
Stat stat = curatorFramework.checkExists().forPath(workingPath);
537+
Assert.assertNotNull(stat);
538+
}
510539
}

0 commit comments

Comments
 (0)