diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/YarnClientImpl.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/YarnClientImpl.java index be4c8c4..f16e6bf 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/YarnClientImpl.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/YarnClientImpl.java @@ -429,16 +429,11 @@ public ApplicationReport getApplicationReport(ApplicationId appId) .newRecord(GetApplicationReportRequest.class); request.setApplicationId(appId); response = rmClient.getApplicationReport(request); - } catch (YarnException e) { + } catch (ApplicationNotFoundException e) { if (!historyServiceEnabled) { // Just throw it as usual if historyService is not enabled. throw e; } - // Even if history-service is enabled, treat all exceptions still the same - // except the following - if (!(e.getClass() == ApplicationNotFoundException.class)) { - throw e; - } return historyClient.getApplicationReport(appId); } return response.getApplicationReport(); diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/AppReportFetcher.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/AppReportFetcher.java index 6aa43eb..fc805bb 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/AppReportFetcher.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/AppReportFetcher.java @@ -26,7 +26,6 @@ import org.apache.hadoop.yarn.api.ApplicationClientProtocol; import org.apache.hadoop.yarn.api.ApplicationHistoryProtocol; import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportRequest; -import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportResponse; import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.api.records.ApplicationReport; import org.apache.hadoop.yarn.client.AHSProxy; @@ -115,28 +114,41 @@ protected ApplicationHistoryProtocol getAHSProxy(Configuration configuration) * @throws YarnException on any error. * @throws IOException */ - public ApplicationReport getApplicationReport(ApplicationId appId) + public ApplicationReportBundle getApplicationReport(ApplicationId appId) throws YarnException, IOException { + boolean isReportFromRM = false; + boolean isReportFromAHS = false; GetApplicationReportRequest request = recordFactory .newRecordInstance(GetApplicationReportRequest.class); request.setApplicationId(appId); - GetApplicationReportResponse response; + ApplicationReport appReport; try { - response = applicationsManager.getApplicationReport(request); - } catch (YarnException e) { + appReport = applicationsManager. + getApplicationReport(request).getApplicationReport(); + //set this flag to true so that WebAppProxyServlet can understand that the + //appReport was fetched from the RM + isReportFromRM = true; + } catch (ApplicationNotFoundException e) { if (!isAHSEnabled) { // Just throw it as usual if historyService is not enabled. throw e; } - // Even if history-service is enabled, treat all exceptions still the same - // except the following - if (!(e.getClass() == ApplicationNotFoundException.class)) { - throw e; - } - response = historyManager.getApplicationReport(request); + //AHS is enabled. Try to fetch the application report from there + appReport = historyManager. + getApplicationReport(request).getApplicationReport(); + + //set this flag to true so that WebAppProxyServlet can understand that the + //appReport was fetched from the AHS + isReportFromAHS = true; } - return response.getApplicationReport(); + //Create a bundle of appReport and the source from where it was fetched + //so the WebAppProxyServlet has a way to know the appReport source + ApplicationReportBundle appReportBundle = new ApplicationReportBundle(); + appReportBundle.setAppReport(appReport); + appReportBundle.setIsReportFromRM(isReportFromRM); + appReportBundle.setIsReportFromAHS(isReportFromAHS); + return appReportBundle; } public void stop() { @@ -147,4 +159,72 @@ public void stop() { RPC.stopProxy(this.historyManager); } } + + /* + * This class creates a bundle of the application report and the source from + * where the the report was fetched from. This allows the WebAppProxyServlet + * to make decisions for the application report based on the source + */ + static class ApplicationReportBundle { + private ApplicationReport appReport; + private boolean isReportFromRM; + private boolean isReportFromAHS; + + public ApplicationReportBundle() { + this.appReport = null; + this.isReportFromRM = false; + this.isReportFromAHS = false; + } + + /* + * set the application report. + * @param applicationReport + */ + void setAppReport(ApplicationReport applicationReport) { + this.appReport = applicationReport; + } + + /* + * set if the appReport source is RM or not. + * @param isReportSourceRM + */ + void setIsReportFromRM(boolean isReportSourceRM) { + this.isReportFromRM = isReportSourceRM; + } + + /* + * set if the appReport source is AHS or not. + * @param isReportFromAHS + */ + void setIsReportFromAHS(boolean isReportSourceAHS) { + this.isReportFromAHS = isReportSourceAHS; + } + + /* + * returns the application report from the ApplicationReportBundle + * @return appReport + */ + ApplicationReport getAppReport() { + if (this.appReport != null) { + return this.appReport; + } + return null; + } + + /* + * returns if the appReport source was from the RM or not. + * @return isReportFromRM + */ + boolean isReportFromRM() { + return this.isReportFromRM; + } + + /* + * returns if the appReport source was from the AHS or not. + * @return isReportFromAHS + */ + boolean isReportFromAHS() { + return this.isReportFromAHS; + } + } } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java index 33f36f0..ad8f36c 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java @@ -49,6 +49,7 @@ import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException; import org.apache.hadoop.yarn.exceptions.YarnException; +import org.apache.hadoop.yarn.server.webproxy.AppReportFetcher.ApplicationReportBundle; import org.apache.hadoop.yarn.util.Apps; import org.apache.hadoop.yarn.util.StringHelper; import org.apache.hadoop.yarn.util.TrackingUriPlugin; @@ -90,6 +91,7 @@ private transient List trackingUriPlugins; private final String rmAppPageUrlBase; + private final String ahsAppPageUrlBase; private transient YarnConfiguration conf; /** @@ -125,6 +127,9 @@ public WebAppProxyServlet() { TrackingUriPlugin.class); this.rmAppPageUrlBase = StringHelper.pjoin( WebAppUtils.getResolvedRMWebAppURLWithScheme(conf), "cluster", "app"); + this.ahsAppPageUrlBase = StringHelper.pjoin( + WebAppUtils.getHttpSchemePrefix(conf) + WebAppUtils + .getAHSWebAppURLWithoutScheme(conf), "applicationhistory", "apps"); } /** @@ -266,7 +271,7 @@ private boolean isSecurityEnabled() { return b != null ? b : false; } - private ApplicationReport getApplicationReport(ApplicationId id) + private ApplicationReportBundle getApplicationReport(ApplicationId id) throws IOException, YarnException { return ((AppReportFetcher) getServletContext() .getAttribute(WebAppProxy.FETCHER_ATTRIBUTE)).getApplicationReport(id); @@ -345,9 +350,13 @@ private void methodAction(final HttpServletRequest req, boolean checkUser = securityEnabled && (!userWasWarned || !userApproved); - ApplicationReport applicationReport; + ApplicationReportBundle reportBundle = null; + ApplicationReport applicationReport = null; try { - applicationReport = getApplicationReport(id); + reportBundle = getApplicationReport(id); + if (reportBundle != null) { + applicationReport = reportBundle.getAppReport(); + } } catch (ApplicationNotFoundException e) { applicationReport = null; } @@ -364,15 +373,27 @@ private void methodAction(final HttpServletRequest req, } notFound(resp, "Application " + appId + " could not be found, " + - "please try the history server"); + "in RM or history server"); return; } String original = applicationReport.getOriginalTrackingUrl(); URI trackingUri; - // fallback to ResourceManager's app page if no tracking URI provided - if(original == null || original.equals("N/A")) { - ProxyUtils.sendRedirect(req, resp, - StringHelper.pjoin(rmAppPageUrlBase, id.toString())); + if (original == null || original.equals("N/A") || original.equals("")) { + if (reportBundle.isReportFromRM()) { + // fallback to ResourceManager's app page if no tracking URI provided + // and Application Report was fetched from RM + LOG.debug("Original tracking url is '{}'. Redirecting to RM app page", + original == null? "NULL" : original); + ProxyUtils.sendRedirect(req, resp, + StringHelper.pjoin(rmAppPageUrlBase, id.toString())); + } else if (reportBundle.isReportFromAHS()) { + // fallback to Application History Server app page if the application + // report was fetched from AHS + LOG.debug("Original tracking url is '{}'." + " Redirecting to " + + "AHS app page", original == null? "NULL" : original); + ProxyUtils.sendRedirect(req, resp, + StringHelper.pjoin(ahsAppPageUrlBase, id.toString())); + } return; } else { if (ProxyUriUtils.getSchemeFromUrl(original).isEmpty()) { diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java index 8e68c38..59acb4f 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.ConnectException; import java.net.HttpCookie; import java.net.HttpURLConnection; import java.net.URI; @@ -76,6 +77,7 @@ private static int numberOfHeaders = 0; private static final String UNKNOWN_HEADER = "Unknown-Header"; private static boolean hasUnknownHeader = false; + Configuration configuration = new Configuration(); /** @@ -137,8 +139,6 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) @Test(timeout=5000) public void testWebAppProxyServlet() throws Exception { - - Configuration configuration = new Configuration(); configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090"); // overriding num of web server threads, see HttpServer.HTTP_MAXTHREADS configuration.setInt("hadoop.http.max.threads", 5); @@ -166,6 +166,7 @@ public void testWebAppProxyServlet() throws Exception { proxyConn.connect(); assertEquals(HttpURLConnection.HTTP_INTERNAL_ERROR, proxyConn.getResponseCode()); + // set true Application ID in url URL url = new URL("http://localhost:" + proxyPort + "/proxy/application_00_0"); proxyConn = (HttpURLConnection) url.openConnection(); @@ -221,6 +222,48 @@ public void testWebAppProxyServlet() throws Exception { assertEquals("http://localhost:" + originalPort + "/foo/bar/test/tez?a=b&x=y&h=p#main", proxyConn.getURL().toString()); + // get appReport for empty tracking url + //set AHS_ENBALED = false to simulate getting the app report from RM + configuration.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, + false); + ApplicationId app = ApplicationId.newInstance(0, 0); + appReportFetcher.answer = 6; + url = new URL("http://localhost:" + proxyPort + + "/proxy/" + app.toString()); + proxyConn = (HttpURLConnection) url.openConnection(); + proxyConn.connect(); + try { + proxyConn.getResponseCode(); + } catch (ConnectException e) { + // Connection Exception is expected as we have set + // appReportFetcher.answer = 6, which does not set anything for + // original tracking url field in the app report. + } + //get the tracking url in RM + String appAddressInRm = + WebAppUtils.getResolvedRMWebAppURLWithScheme(configuration) + + "/cluster" + "/app/" + app.toString(); + assertTrue("Webapp proxy servlet should have redirected to RM", + proxyConn.getURL().toString().equals(appAddressInRm)); + + //set AHS_ENBALED = true to simulate getting the app report from AHS + configuration.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, + true); + proxyConn = (HttpURLConnection) url.openConnection(); + proxyConn.connect(); + try { + proxyConn.getResponseCode(); + } catch (ConnectException e) { + // Connection Exception is expected as we have set + // appReportFetcher.answer = 6, which does not set anything for + // original tracking url field in the app report. + } + //get the tracking url in AHS + String appAddressInAhs = WebAppUtils.getHttpSchemePrefix(configuration) + + WebAppUtils.getAHSWebAppURLWithoutScheme(configuration) + + "/applicationhistory" + "/apps/" + app.toString(); + assertTrue("Webapp proxy servlet should have redirected to AHS", + proxyConn.getURL().toString().equals(appAddressInAhs)); } finally { proxy.close(); } @@ -398,49 +441,73 @@ protected void serviceStart() throws Exception { } private class AppReportFetcherForTest extends AppReportFetcher { - int answer = 0; public AppReportFetcherForTest(Configuration conf) { super(conf); } - public ApplicationReport getApplicationReport(ApplicationId appId) + public ApplicationReportBundle getApplicationReport(ApplicationId appId) throws YarnException { if (answer == 0) { return getDefaultApplicationReport(appId); } else if (answer == 1) { return null; } else if (answer == 2) { - ApplicationReport result = getDefaultApplicationReport(appId); - result.setUser("user"); + ApplicationReportBundle result = getDefaultApplicationReport(appId); + result.getAppReport().setUser("user"); return result; } else if (answer == 3) { - ApplicationReport result = getDefaultApplicationReport(appId); - result.setYarnApplicationState(YarnApplicationState.KILLED); + ApplicationReportBundle result = getDefaultApplicationReport(appId); + result.getAppReport(). + setYarnApplicationState(YarnApplicationState.KILLED); return result; } else if (answer == 4) { throw new ApplicationNotFoundException("Application is not found"); } else if (answer == 5) { // test user-provided path and query parameter can be appended to the // original tracking url - ApplicationReport result = getDefaultApplicationReport(appId); - result.setOriginalTrackingUrl("localhost:" + originalPort + ApplicationReportBundle result = getDefaultApplicationReport(appId); + result.getAppReport().setOriginalTrackingUrl("localhost:" + originalPort + "/foo/bar?a=b#main"); - result.setYarnApplicationState(YarnApplicationState.FINISHED); + result.getAppReport(). + setYarnApplicationState(YarnApplicationState.FINISHED); return result; + } else if (answer == 6) { + return getDefaultApplicationReport(appId, false); } return null; } - private ApplicationReport getDefaultApplicationReport(ApplicationId appId) { + /* + * If this method is called with isTrackingUrl=false, no tracking url + * will set in the app report. Hence, there will be a connection exception + * when the prxyCon tries to connect. + */ + private ApplicationReportBundle getDefaultApplicationReport + (ApplicationId appId, boolean isTrackingUrl) { + ApplicationReportBundle reportBundle = new ApplicationReportBundle(); ApplicationReport result = new ApplicationReportPBImpl(); result.setApplicationId(appId); - result.setOriginalTrackingUrl("localhost:" + originalPort + "/foo/bar"); + if (isTrackingUrl) { + result.setOriginalTrackingUrl("localhost:" + originalPort + "/foo/bar"); + } else { + if(configuration.getBoolean(YarnConfiguration. + APPLICATION_HISTORY_ENABLED, false)) { + reportBundle.setIsReportFromAHS(true); + } else { + reportBundle.setIsReportFromRM(true); + } + } result.setYarnApplicationState(YarnApplicationState.RUNNING); result.setUser(CommonConfigurationKeys.DEFAULT_HADOOP_HTTP_STATIC_USER); - return result; + reportBundle.setAppReport(result); + return reportBundle; + } + + private ApplicationReportBundle + getDefaultApplicationReport(ApplicationId appId) { + return getDefaultApplicationReport(appId, true); } - } }