Skip to content

Commit 7f8300c

Browse files
slfan1989Melissa You
authored andcommitted
HADOOP-18427. Improve ZKDelegationTokenSecretManager#startThead With recommended methods. (apache#4812)
1 parent b0f6cc1 commit 7f8300c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

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

137+
@VisibleForTesting
138+
protected static CuratorFramework getCurator() {
139+
return CURATOR_TL.get();
140+
}
141+
138142
private final boolean isExternalClient;
139143
protected final CuratorFramework zkClient;
140144
private SharedCount delTokSeqCounter;
@@ -261,9 +265,8 @@ public void startThreads() throws IOException {
261265
// If namespace parents are implicitly created, they won't have ACLs.
262266
// So, let's explicitly create them.
263267
CuratorFramework nullNsFw = zkClient.usingNamespace(null);
264-
EnsurePath ensureNs = nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace());
265268
try {
266-
ensureNs.ensure(nullNsFw.getZookeeperClient());
269+
nullNsFw.create().creatingParentContainersIfNeeded().forPath("/" + zkClient.getNamespace());
267270
} catch (Exception e) {
268271
throw new IOException("Could not create namespace", e);
269272
}

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
@@ -40,6 +40,7 @@
4040
import org.apache.zookeeper.ZooDefs;
4141
import org.apache.zookeeper.data.ACL;
4242
import org.apache.zookeeper.data.Id;
43+
import org.apache.zookeeper.data.Stat;
4344
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
4445
import org.junit.After;
4546
import org.junit.Assert;
@@ -506,4 +507,32 @@ public Boolean get() {
506507
}
507508
}, 1000, 5000);
508509
}
510+
511+
@Test
512+
public void testCreatingParentContainersIfNeeded() throws Exception {
513+
514+
String connectString = zkServer.getConnectString();
515+
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
516+
Configuration conf = getSecretConf(connectString);
517+
CuratorFramework curatorFramework =
518+
CuratorFrameworkFactory.builder()
519+
.connectString(connectString)
520+
.retryPolicy(retryPolicy)
521+
.build();
522+
curatorFramework.start();
523+
ZKDelegationTokenSecretManager.setCurator(curatorFramework);
524+
DelegationTokenManager tm1 = new DelegationTokenManager(conf, new Text("foo"));
525+
526+
// When the init method is called,
527+
// the ZKDelegationTokenSecretManager#startThread method will be called,
528+
// and the creatingParentContainersIfNeeded will be called to create the nameSpace.
529+
tm1.init();
530+
531+
String workingPath = "/" + conf.get(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH,
532+
ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH_DEAFULT) + "/ZKDTSMRoot";
533+
534+
// Check if the created NameSpace exists.
535+
Stat stat = curatorFramework.checkExists().forPath(workingPath);
536+
Assert.assertNotNull(stat);
537+
}
509538
}

0 commit comments

Comments
 (0)