From 6a22bead54068d12a70291b6cce6c6dc9102f14d Mon Sep 17 00:00:00 2001 From: Thinker313 <47049042+ThinkerLei@users.noreply.github.com> Date: Wed, 6 Dec 2023 21:34:46 +0800 Subject: [PATCH] HDFS-17270. RBF: Fix ZKDelegationTokenSecretManagerImpl use closed zookeeper client to get token in some case (#6315). Contributed by lei w. Signed-off-by: Shuyan Zhang Signed-off-by: He Xiaoqiao --- .../ZKDelegationTokenSecretManagerImpl.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/security/token/ZKDelegationTokenSecretManagerImpl.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/security/token/ZKDelegationTokenSecretManagerImpl.java index a83be71e59..bc7b48d127 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/security/token/ZKDelegationTokenSecretManagerImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/security/token/ZKDelegationTokenSecretManagerImpl.java @@ -60,8 +60,7 @@ public class ZKDelegationTokenSecretManagerImpl extends // currentTokenMap private final Set localTokenCache = new HashSet<>(); - // Native zk client for getting all tokens - private ZooKeeper zookeeper; + private final String TOKEN_PATH = "/" + zkClient.getNamespace() + ZK_DTSM_TOKENS_ROOT; // The flag used to issue an extra check before deletion @@ -103,16 +102,6 @@ public void startThreads() throws IOException { .withMode(CreateMode.PERSISTENT) .forPath(ZK_DTSM_TOKENS_ROOT); } - // Set up zookeeper client - try { - zookeeper = zkClient.getZookeeperClient().getZooKeeper(); - } catch (Exception e) { - LOG.info("Cannot get zookeeper client ", e); - } finally { - if (zookeeper == null) { - throw new IOException("Zookeeper client is null"); - } - } LOG.info("Start loading token cache"); long start = Time.now(); @@ -163,7 +152,7 @@ private void rebuildTokenCache(boolean initial) throws IOException { // millions of tokens List zkTokens; try { - zkTokens = zookeeper.getChildren(TOKEN_PATH, false); + zkTokens = getZooKeeperClient().getChildren(TOKEN_PATH, false); } catch (KeeperException | InterruptedException e) { throw new IOException("Tokens cannot be fetched from path " + TOKEN_PATH, e); @@ -222,4 +211,19 @@ protected void addOrUpdateToken(AbstractDelegationTokenIdentifier ident, currentTokens.put(ident, info); super.addOrUpdateToken(ident, info, isUpdate); } + + private ZooKeeper getZooKeeperClient() throws IOException { + // get zookeeper client + ZooKeeper zookeeper = null; + try { + zookeeper = zkClient.getZookeeperClient().getZooKeeper(); + } catch (Exception e) { + LOG.info("Cannot get zookeeper client ", e); + } finally { + if (zookeeper == null) { + throw new IOException("Zookeeper client is null"); + } + } + return zookeeper; + } }