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..58108545311 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,17 @@ public Text getKind() { @InterfaceAudience.Private public static class Renewer extends TokenRenewer { + @SuppressWarnings("unchecked") + private static final Map + timelineClientCache = Collections.synchronizedMap(new LRUMap() { + @Override + protected boolean removeLRU(LinkEntry entry) { + TimelineClient client = (TimelineClient) entry.getValue(); + client.stop(); + return true; + } + }); + @Override public boolean handleKind(Text kind) { return KIND_NAME.equals(kind); @@ -74,16 +89,13 @@ 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) { throw new IOException(e); - } finally { - client.stop(); } } @@ -91,18 +103,27 @@ 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) { throw new IOException(e); - } finally { - 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; + } } }