001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.hadoop.yarn.client.api; 020 021 import java.util.concurrent.ConcurrentHashMap; 022 023 import org.apache.hadoop.classification.InterfaceAudience.Private; 024 import org.apache.hadoop.classification.InterfaceAudience.Public; 025 import org.apache.hadoop.classification.InterfaceStability.Evolving; 026 import org.apache.hadoop.yarn.api.records.Token; 027 028 import com.google.common.annotations.VisibleForTesting; 029 030 /** 031 * It manages NMTokens required for communicating with Node manager. Its a 032 * static token cache. 033 */ 034 @Public 035 @Evolving 036 public class NMTokenCache { 037 private static ConcurrentHashMap<String, Token> nmTokens; 038 039 040 static { 041 nmTokens = new ConcurrentHashMap<String, Token>(); 042 } 043 044 /** 045 * Returns NMToken, null if absent 046 * @param nodeAddr 047 * @return {@link Token} NMToken required for communicating with node 048 * manager 049 */ 050 @Public 051 @Evolving 052 public static Token getNMToken(String nodeAddr) { 053 return nmTokens.get(nodeAddr); 054 } 055 056 /** 057 * Sets the NMToken for node address 058 * @param nodeAddr node address (host:port) 059 * @param token NMToken 060 */ 061 @Public 062 @Evolving 063 public static void setNMToken(String nodeAddr, Token token) { 064 nmTokens.put(nodeAddr, token); 065 } 066 067 /** 068 * Returns true if NMToken is present in cache. 069 */ 070 @Private 071 @VisibleForTesting 072 public static boolean containsNMToken(String nodeAddr) { 073 return nmTokens.containsKey(nodeAddr); 074 } 075 076 /** 077 * Returns the number of NMTokens present in cache. 078 */ 079 @Private 080 @VisibleForTesting 081 public static int numberOfNMTokensInCache() { 082 return nmTokens.size(); 083 } 084 085 /** 086 * Removes NMToken for specified node manager 087 * @param nodeAddr node address (host:port) 088 */ 089 @Private 090 @VisibleForTesting 091 public static void removeNMToken(String nodeAddr) { 092 nmTokens.remove(nodeAddr); 093 } 094 095 /** 096 * It will remove all the nm tokens from its cache 097 */ 098 @Private 099 @VisibleForTesting 100 public static void clearCache() { 101 nmTokens.clear(); 102 } 103 }