|
@@ -20,6 +20,7 @@ package org.apache.hadoop.yarn.server.nodemanager.webapp;
|
|
|
|
|
|
import static org.apache.hadoop.yarn.webapp.WebServicesTestUtils.assertResponseStatusCode;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.assertFalse;
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
@@ -27,7 +28,11 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URL;
|
|
|
import java.util.List;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
@@ -59,6 +64,7 @@ import org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.Conta
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.webapp.WebServer.NMWebApp;
|
|
|
import org.apache.hadoop.yarn.server.security.ApplicationACLsManager;
|
|
|
import org.apache.hadoop.yarn.server.utils.BuilderUtils;
|
|
|
+import org.apache.hadoop.yarn.server.webapp.YarnWebServiceParams;
|
|
|
import org.apache.hadoop.yarn.server.webapp.dao.ContainerLogsInfo;
|
|
|
import org.apache.hadoop.yarn.util.YarnVersionInfo;
|
|
|
import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
|
|
@@ -97,6 +103,7 @@ public class TestNMWebServices extends JerseyTestBase {
|
|
|
private static ApplicationACLsManager aclsManager;
|
|
|
private static LocalDirsHandlerService dirsHandler;
|
|
|
private static WebApp nmWebApp;
|
|
|
+ private static final String LOGSERVICEWSADDR = "test:1234";
|
|
|
|
|
|
private static final File testRootDir = new File("target",
|
|
|
TestNMWebServices.class.getSimpleName());
|
|
@@ -115,6 +122,8 @@ public class TestNMWebServices extends JerseyTestBase {
|
|
|
conf.setBoolean(YarnConfiguration.LOG_AGGREGATION_ENABLED, true);
|
|
|
conf.set(YarnConfiguration.NM_REMOTE_APP_LOG_DIR,
|
|
|
testRemoteLogDir.getAbsolutePath());
|
|
|
+ conf.set(YarnConfiguration.YARN_LOG_SERVER_WEBSERVICE_URL,
|
|
|
+ LOGSERVICEWSADDR);
|
|
|
dirsHandler = new LocalDirsHandlerService();
|
|
|
NodeHealthCheckerService healthChecker = new NodeHealthCheckerService(
|
|
|
NodeManager.getNodeHealthScriptRunner(conf), dirsHandler);
|
|
@@ -351,6 +360,58 @@ public class TestNMWebServices extends JerseyTestBase {
|
|
|
testContainerLogs(r, containerId);
|
|
|
}
|
|
|
|
|
|
+ @Test (timeout = 10000)
|
|
|
+ public void testNMRedirect() {
|
|
|
+ ApplicationId noExistAppId = ApplicationId.newInstance(
|
|
|
+ System.currentTimeMillis(), 2000);
|
|
|
+ ApplicationAttemptId noExistAttemptId = ApplicationAttemptId.newInstance(
|
|
|
+ noExistAppId, 150);
|
|
|
+ ContainerId noExistContainerId = ContainerId.newContainerId(
|
|
|
+ noExistAttemptId, 250);
|
|
|
+ String fileName = "syslog";
|
|
|
+ WebResource r = resource();
|
|
|
+
|
|
|
+ // check the old api
|
|
|
+ URI requestURI = r.path("ws").path("v1").path("node")
|
|
|
+ .path("containerlogs").path(noExistContainerId.toString())
|
|
|
+ .path(fileName).queryParam("user.name", "user")
|
|
|
+ .queryParam(YarnWebServiceParams.NM_ID, "localhost:1111")
|
|
|
+ .getURI();
|
|
|
+ String redirectURL = getRedirectURL(requestURI.toString());
|
|
|
+ assertTrue(redirectURL != null);
|
|
|
+ assertTrue(redirectURL.contains(LOGSERVICEWSADDR));
|
|
|
+ assertTrue(redirectURL.contains(noExistContainerId.toString()));
|
|
|
+ assertTrue(redirectURL.contains("/logs/" + fileName));
|
|
|
+ assertTrue(redirectURL.contains("user.name=" + "user"));
|
|
|
+ assertFalse(redirectURL.contains(YarnWebServiceParams.NM_ID));
|
|
|
+
|
|
|
+ // check the new api
|
|
|
+ requestURI = r.path("ws").path("v1").path("node")
|
|
|
+ .path("containers").path(noExistContainerId.toString())
|
|
|
+ .path("logs").path(fileName).queryParam("user.name", "user")
|
|
|
+ .queryParam(YarnWebServiceParams.NM_ID, "localhost:1111")
|
|
|
+ .getURI();
|
|
|
+ redirectURL = getRedirectURL(requestURI.toString());
|
|
|
+ assertTrue(redirectURL != null);
|
|
|
+ assertTrue(redirectURL.contains(LOGSERVICEWSADDR));
|
|
|
+ assertTrue(redirectURL.contains(noExistContainerId.toString()));
|
|
|
+ assertTrue(redirectURL.contains("/logs/" + fileName));
|
|
|
+ assertTrue(redirectURL.contains("user.name=" + "user"));
|
|
|
+ assertFalse(redirectURL.contains(YarnWebServiceParams.NM_ID));
|
|
|
+
|
|
|
+ requestURI = r.path("ws").path("v1").path("node")
|
|
|
+ .path("containers").path(noExistContainerId.toString())
|
|
|
+ .path("logs").queryParam("user.name", "user")
|
|
|
+ .queryParam(YarnWebServiceParams.NM_ID, "localhost:1111")
|
|
|
+ .getURI();
|
|
|
+ redirectURL = getRedirectURL(requestURI.toString());
|
|
|
+ assertTrue(redirectURL != null);
|
|
|
+ assertTrue(redirectURL.contains(LOGSERVICEWSADDR));
|
|
|
+ assertTrue(redirectURL.contains(noExistContainerId.toString()));
|
|
|
+ assertTrue(redirectURL.contains("user.name=" + "user"));
|
|
|
+ assertFalse(redirectURL.contains(YarnWebServiceParams.NM_ID));
|
|
|
+ }
|
|
|
+
|
|
|
private void testContainerLogs(WebResource r, ContainerId containerId)
|
|
|
throws IOException {
|
|
|
final String containerIdStr = containerId.toString();
|
|
@@ -451,13 +512,12 @@ public class TestNMWebServices extends JerseyTestBase {
|
|
|
+ WebAppUtils.listSupportedLogContentType(), responseText);
|
|
|
assertEquals(400, response.getStatus());
|
|
|
|
|
|
- // ask for file that doesn't exist
|
|
|
- response = r.path("uhhh")
|
|
|
- .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
|
|
|
- assertEquals(Status.NOT_FOUND.getStatusCode(),
|
|
|
- response.getStatus());
|
|
|
- responseText = response.getEntity(String.class);
|
|
|
- assertTrue(responseText.contains("Cannot find this log on the local disk."));
|
|
|
+ // ask for file that doesn't exist and it will re-direct to
|
|
|
+ // the log server
|
|
|
+ URI requestURI = r.path("uhhh").getURI();
|
|
|
+ String redirectURL = getRedirectURL(requestURI.toString());
|
|
|
+ assertTrue(redirectURL != null);
|
|
|
+ assertTrue(redirectURL.contains(LOGSERVICEWSADDR));
|
|
|
|
|
|
// Get container log files' name
|
|
|
WebResource r1 = resource();
|
|
@@ -630,4 +690,21 @@ public class TestNMWebServices extends JerseyTestBase {
|
|
|
int postfixIndex = fullMessage.indexOf(postfix);
|
|
|
return fullMessage.substring(prefixIndex, postfixIndex);
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ private static String getRedirectURL(String url) {
|
|
|
+ String redirectUrl = null;
|
|
|
+ try {
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) new URL(url)
|
|
|
+ .openConnection();
|
|
|
+ // do not automatically follow the redirection
|
|
|
+ // otherwise we get too many redirections exception
|
|
|
+ conn.setInstanceFollowRedirects(false);
|
|
|
+ if(conn.getResponseCode() == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
|
|
|
+ redirectUrl = conn.getHeaderField("Location");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return redirectUrl;
|
|
|
+ }
|
|
|
+}
|