Skip to content

Commit b5cfc7c

Browse files
authored
YARN-11836. Fixed AM log fetching with YARN CLI (apache#7813)
* YARN-11836. Fixed AM log fetching with YARN CLI Change-Id: I04678a04503e01e67f1075f3fc543887cf80ac47 * YARN-11836. Fixed review comments Change-Id: Ia7a864fa77901dd63d193e0e9f0ccaab3674a75d * YARN-11836. Fixed typos Change-Id: I35b0904be1c12e57349cc7a473a0d70ba38c6482 * YARN-11836. Further refactorings Change-Id: I76250565144f8f010ece1ef9d3781259fbae4c26
1 parent 636d822 commit b5cfc7c

File tree

1 file changed

+29
-9
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util

1 file changed

+29
-9
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@
4848
import org.apache.hadoop.yarn.webapp.NotFoundException;
4949
import org.apache.http.NameValuePair;
5050
import org.apache.http.client.utils.URLEncodedUtils;
51+
import org.slf4j.Logger;
52+
import org.slf4j.LoggerFactory;
5153

5254
import javax.servlet.http.HttpServletRequest;
5355
import javax.ws.rs.container.ContainerRequestContext;
5456

5557
@Private
5658
@Evolving
5759
public class WebAppUtils {
60+
private static final Logger LOG = LoggerFactory.getLogger(WebAppUtils.class);
5861
public static final String WEB_APP_TRUSTSTORE_PASSWORD_KEY =
5962
"ssl.server.truststore.password";
6063
public static final String WEB_APP_KEYSTORE_PASSWORD_KEY =
@@ -107,17 +110,34 @@ public static void setNMWebAppHostNameAndPort(Configuration conf,
107110
*/
108111
public static <T, R> R execOnActiveRM(Configuration conf,
109112
ThrowingBiFunction<String, T, R> func, T arg) throws Exception {
110-
int haIndex = 0;
111-
if (HAUtil.isHAEnabled(conf)) {
112-
String activeRMId = RMHAUtils.findActiveRMHAId(conf);
113-
if (activeRMId != null) {
114-
haIndex = new ArrayList<>(HAUtil.getRMHAIds(conf)).indexOf(activeRMId);
115-
} else {
116-
throw new ConnectException("No Active RM available");
113+
// If HA is not enabled we are running the function on the only RM that is available.
114+
if (!HAUtil.isHAEnabled(conf)) {
115+
String rmAddress = getRMWebAppURLWithScheme(conf, 0);
116+
return func.apply(rmAddress, arg);
117+
}
118+
119+
// In HA mode we can find the active RM if user has admin permissions to check service states.
120+
// Otherwise, activeRMId will be null.
121+
List<String> rmIds = (List<String>) HAUtil.getRMHAIds(conf);
122+
String activeRMId = RMHAUtils.findActiveRMHAId(conf);
123+
if (activeRMId != null) {
124+
int activeRMIndex = rmIds.indexOf(activeRMId);
125+
String rmAddress = getRMWebAppURLWithScheme(conf, activeRMIndex);
126+
return func.apply(rmAddress, arg);
127+
}
128+
129+
// If user does not have the necessary permissions we have to iterate through the RMs
130+
// to find the active one.
131+
for (int i = 0; i < rmIds.size(); i++) {
132+
try {
133+
String rmAddress = getRMWebAppURLWithScheme(conf, i);
134+
return func.apply(rmAddress, arg);
135+
} catch (Exception e) {
136+
// Log exception and try next RM if there are any.
137+
LOG.trace("Exception while connecting to RM", e);
117138
}
118139
}
119-
String rm1Address = getRMWebAppURLWithScheme(conf, haIndex);
120-
return func.apply(rm1Address, arg);
140+
throw new ConnectException("No active RM available to execute this command");
121141
}
122142

123143
/** A BiFunction which throws on Exception. */

0 commit comments

Comments
 (0)