diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/client/TimelineDelegationTokenIdentifier.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/client/TimelineDelegationTokenIdentifier.java index 1f01d0f12eb..348354007da 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/client/TimelineDelegationTokenIdentifier.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/client/TimelineDelegationTokenIdentifier.java @@ -19,12 +19,16 @@ package org.apache.hadoop.yarn.security.client; import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import org.apache.commons.collections.map.LRUMap; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.Text; +import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.token.Token; import org.apache.hadoop.security.token.TokenRenewer; import org.apache.hadoop.yarn.client.api.TimelineClient; @@ -60,6 +64,9 @@ public Text getKind() { @InterfaceAudience.Private public static class Renewer extends TokenRenewer { + private static final Map + timelineClientCache = Collections.synchronizedMap(new LRUMap()); + @Override public boolean handleKind(Text kind) { return KIND_NAME.equals(kind); @@ -74,10 +81,9 @@ public boolean isManaged(Token token) throws IOException { @Override public long renew(Token token, Configuration conf) throws IOException, InterruptedException { - TimelineClient client = TimelineClient.createTimelineClient(); + TimelineClient client = getCachedTimelineClient(conf, + UserGroupInformation.getCurrentUser()); try { - client.init(conf); - client.start(); return client.renewDelegationToken( (Token) token); } catch (YarnException e) { @@ -91,10 +97,9 @@ public long renew(Token token, Configuration conf) throws IOException, @Override public void cancel(Token token, Configuration conf) throws IOException, InterruptedException { - TimelineClient client = TimelineClient.createTimelineClient(); + TimelineClient client = getCachedTimelineClient(conf, + UserGroupInformation.getCurrentUser()); try { - client.init(conf); - client.start(); client.cancelDelegationToken( (Token) token); } catch (YarnException e) { @@ -103,6 +108,18 @@ public void cancel(Token token, Configuration conf) throws IOException, client.stop(); } } + + private static TimelineClient getCachedTimelineClient(Configuration conf, + UserGroupInformation ugi) { + TimelineClient client = timelineClientCache.get(ugi); + if (client == null) { + client = TimelineClient.createTimelineClient(); + client.init(conf); + client.start(); + timelineClientCache.put(ugi, client); + } + return client; + } } }