From 518ccbaf57bf3e8bde72893046cc5d446930ea2d Mon Sep 17 00:00:00 2001 From: Viraj Jasani Date: Fri, 22 Jul 2022 15:59:11 -0700 Subject: [PATCH] HADOOP-15984. Update jersey from 1.19 to 2.x --- LICENSE-binary | 13 +- .../hadoop-client-minicluster/pom.xml | 93 +- .../hadoop-cloud-storage/pom.xml | 12 - hadoop-common-project/hadoop-common/pom.xml | 66 +- .../org/apache/hadoop/http/HttpServer2.java | 25 +- .../main/java/org/apache/hadoop/net/DNS.java | 14 +- hadoop-common-project/hadoop-kms/pom.xml | 21 +- .../src/main/conf/kms-log4j.properties | 1 - .../hadoop/crypto/key/kms/server/KMS.java | 15 +- .../key/kms/server/KMSExceptionsProvider.java | 3 +- .../resources/webapps/kms/WEB-INF/web.xml | 16 +- .../src/test/resources/log4j.properties | 2 - .../hadoop-hdfs-httpfs/pom.xml | 6 +- .../http/server/HttpFSExceptionProvider.java | 2 +- .../http/server/HttpFSParametersProvider.java | 4 +- .../hadoop/fs/http/server/HttpFSServer.java | 46 +- .../hadoop/lib/wsrs/ParametersProvider.java | 73 +- .../resources/webapps/webhdfs/WEB-INF/web.xml | 16 +- .../src/main/webapp/WEB-INF/web.xml | 16 +- .../test/resources/default-log4j.properties | 1 - hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml | 10 - hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml | 4 + .../router/RouterWebHdfsMethods.java | 3 - hadoop-hdfs-project/hadoop-hdfs/pom.xml | 16 +- .../web/webhdfs/ExceptionHandler.java | 4 +- .../server/namenode/NameNodeHttpServer.java | 27 +- .../web/resources/NamenodeWebHdfsMethods.java | 28 +- .../apache/hadoop/hdfs/web/ParamFilter.java | 104 +- .../hdfs/web/resources/ExceptionHandler.java | 4 +- .../hdfs/web/resources/UserProvider.java | 61 +- .../src/test/resources/log4j.properties | 3 - .../hadoop-mapreduce-client-app/pom.xml | 4 + .../jobhistory/JobHistoryEventHandler.java | 5 +- .../v2/app/webapp/JAXBContextResolver.java | 13 +- .../v2/app/webapp/TestAMWebServices.java | 183 ++- .../app/webapp/TestAMWebServicesAttempt.java | 8 +- .../app/webapp/TestAMWebServicesAttempts.java | 166 ++- .../app/webapp/TestAMWebServicesJobConf.java | 67 +- .../v2/app/webapp/TestAMWebServicesTasks.java | 236 ++-- .../v2/hs/webapp/JAXBContextResolver.java | 8 +- .../hadoop-mapreduce-client/pom.xml | 9 +- .../hadoop-mapreduce-examples/pom.xml | 5 - hadoop-mapreduce-project/pom.xml | 8 - hadoop-project/pom.xml | 179 +-- hadoop-tools/hadoop-azure/pom.xml | 2 +- hadoop-tools/hadoop-resourceestimator/pom.xml | 27 - .../hadoop-yarn/hadoop-yarn-api/pom.xml | 4 + .../pom.xml | 25 +- .../pom.xml | 4 + .../distributedshell/TestDSTimelineV10.java | 7 +- .../hadoop-yarn-services-api/pom.xml | 8 - .../hadoop-yarn-services-core/pom.xml | 5 + .../hadoop-yarn/hadoop-yarn-client/pom.xml | 10 +- .../hadoop/yarn/client/cli/LogsCLI.java | 238 +--- .../hadoop/yarn/client/cli/SchedConfCLI.java | 138 ++- .../hadoop/yarn/client/cli/TestLogsCLI.java | 67 +- .../yarn/client/cli/TestSchedConfCLI.java | 19 +- .../hadoop-yarn/hadoop-yarn-common/pom.xml | 53 +- .../client/api/impl/DirectTimelineWriter.java | 7 +- .../api/impl/FileSystemTimelineWriter.java | 9 +- .../client/api/impl/TimelineClientImpl.java | 12 +- .../client/api/impl/TimelineConnector.java | 225 +--- .../api/impl/TimelineReaderClientImpl.java | 53 +- .../client/api/impl/TimelineV2ClientImpl.java | 68 +- .../yarn/client/api/impl/TimelineWriter.java | 49 +- .../yarn/logaggregation/LogToolUtils.java | 16 +- .../yarn/webapp/GenericExceptionHandler.java | 4 +- .../org/apache/hadoop/yarn/webapp/WebApp.java | 20 +- .../apache/hadoop/yarn/webapp/WebApps.java | 6 +- .../hadoop/yarn/webapp/util/WebAppUtils.java | 30 + .../yarn/webapp/util/WebServiceClient.java | 54 +- .../yarn/webapp/util/YarnWebServiceUtils.java | 67 +- .../client/api/impl/TestTimelineClient.java | 65 +- .../api/impl/TestTimelineClientForATS1_5.java | 16 +- .../impl/TestTimelineReaderClientImpl.java | 20 +- .../webapp/MyTestJAXBContextResolver.java | 56 - ....java => ObjectWriterContextResolver.java} | 35 +- .../apache/hadoop/yarn/webapp/TestWebApp.java | 6 +- .../webapp/util/TestWebServiceClient.java | 7 +- .../pom.xml | 58 +- .../webapp/JAXBContextResolver.java | 9 +- .../webapp/TestAHSWebServices.java | 614 ++++++---- .../webapp/TestTimelineWebServices.java | 780 ++++++++----- .../TestTimelineWebServicesWithSSL.java | 19 +- .../hadoop-yarn-server-common/pom.xml | 4 + .../policies/dao/WeightedPolicyInfo.java | 69 +- .../store/records/SubClusterIdInfo.java | 4 + .../hadoop/yarn/server/webapp/LogServlet.java | 5 +- .../yarn/server/webapp/LogWebService.java | 68 +- .../server/webapp/LogWebServiceUtils.java | 5 +- .../hadoop-yarn-server-nodemanager/pom.xml | 37 +- .../webapp/JAXBContextResolver.java | 10 +- .../nodemanager/webapp/NMWebAppFilter.java | 40 +- .../server/nodemanager/webapp/WebServer.java | 4 +- .../nodemanager/webapp/TestNMWebServices.java | 411 ++++--- .../webapp/TestNMWebServicesApps.java | 272 ++--- .../webapp/TestNMWebServicesAuxServices.java | 77 +- .../webapp/TestNMWebServicesContainers.java | 163 +-- .../nodemanager/webapp/TestNMWebTerminal.java | 17 +- .../pom.xml | 64 +- .../resourcemanager/ResourceManager.java | 3 +- .../FederationStateStoreHeartbeat.java | 31 +- .../webapp/JAXBContextResolver.java | 13 +- .../resourcemanager/webapp/RMWebApp.java | 6 +- .../webapp/RMWebAppFilter.java | 27 +- .../resourcemanager/webapp/RMWebServices.java | 8 +- .../yarn/server/resourcemanager/TestRMHA.java | 24 +- .../TestFederationRMStateStoreService.java | 27 +- .../webapp/ActivitiesTestUtils.java | 20 +- .../webapp/TestRMWebServiceAppsNodelabel.java | 42 +- .../webapp/TestRMWebServices.java | 677 +++++------ .../webapp/TestRMWebServicesAppAttempts.java | 97 +- ...stRMWebServicesAppCustomResourceTypes.java | 42 +- .../webapp/TestRMWebServicesApps.java | 627 +++++----- ...tRMWebServicesAppsCustomResourceTypes.java | 33 +- .../TestRMWebServicesAppsModification.java | 483 ++++---- .../TestRMWebServicesCapacitySched.java | 72 +- ...WebServicesCapacitySchedDynamicConfig.java | 18 +- ...estRMWebServicesConfigurationMutation.java | 428 +++---- .../webapp/TestRMWebServicesContainers.java | 103 +- ...ServicesDelegationTokenAuthentication.java | 20 +- .../TestRMWebServicesDelegationTokens.java | 443 +++---- .../TestRMWebServicesForCSWithPartitions.java | 64 +- ...MWebServicesHttpStaticUserPermissions.java | 8 +- .../webapp/TestRMWebServicesNodeLabels.java | 197 ++-- .../webapp/TestRMWebServicesNodes.java | 291 +++-- .../webapp/TestRMWebServicesReservation.java | 258 ++--- .../TestRMWebServicesSchedulerActivities.java | 1021 +++++++---------- ...edulerActivitiesWithMultiNodesEnabled.java | 90 +- .../webapp/TestRMWebappAuthentication.java | 3 +- .../TestRMWebServicesFairScheduler.java | 50 +- ...vicesFairSchedulerCustomResourceTypes.java | 56 +- .../webapp/helper/BufferedClientResponse.java | 22 +- .../JsonCustomResourceTypeTestcase.java | 8 +- .../helper/XmlCustomResourceTypeTestCase.java | 8 +- .../yarn/webapp/TestRMWithCSRFFilter.java | 59 +- .../hadoop-yarn-server-router/pom.xml | 5 + .../server/router/webapp/FederationBlock.java | 15 +- .../hadoop-yarn-server-tests/pom.xml | 4 + .../pom.xml | 4 + .../pom.xml | 16 +- .../AbstractTimelineReaderHBaseTestBase.java | 41 +- .../pom.xml | 5 + .../pom.xml | 8 +- .../reader/TestTimelineReaderWebServices.java | 266 ++--- .../TestTimelineReaderWebServicesACL.java | 53 +- .../hadoop-yarn-server-web-proxy/pom.xml | 13 +- 147 files changed, 5306 insertions(+), 6265 deletions(-) delete mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestJAXBContextResolver.java rename hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/{JerseyTestBase.java => ObjectWriterContextResolver.java} (54%) diff --git a/LICENSE-binary b/LICENSE-binary index 4e6f688fcb896..0343e3ac9fb46 100644 --- a/LICENSE-binary +++ b/LICENSE-binary @@ -218,12 +218,12 @@ com.aliyun.oss:aliyun-sdk-oss:3.13.2 com.amazonaws:aws-java-sdk-bundle:1.11.901 com.cedarsoftware:java-util:1.9.0 com.cedarsoftware:json-io:2.5.1 -com.fasterxml.jackson.core:jackson-annotations:2.12.7 -com.fasterxml.jackson.core:jackson-core:2.12.7 -com.fasterxml.jackson.core:jackson-databind:2.12.7 -com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.12.7 -com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.12.7 -com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.12.7 +com.fasterxml.jackson.core:jackson-annotations:2.13.2 +com.fasterxml.jackson.core:jackson-core:2.13.2 +com.fasterxml.jackson.core:jackson-databind:2.13.2.2 +com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.13.2 +com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.13.2 +com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.13.2 com.fasterxml.uuid:java-uuid-generator:3.1.4 com.fasterxml.woodstox:woodstox-core:5.3.0 com.github.davidmoten:rxjava-extras:0.8.0.17 @@ -510,6 +510,7 @@ javax.annotation:javax.annotation-api:1.3.2 javax.servlet:javax.servlet-api:3.1.0 javax.servlet.jsp:jsp-api:2.1 javax.websocket:javax.websocket-api:1.0 +javax.ws.rs:javax.ws.rs-api:2.1.1 javax.ws.rs:jsr311-api:1.1.1 javax.xml.bind:jaxb-api:2.2.11 diff --git a/hadoop-client-modules/hadoop-client-minicluster/pom.xml b/hadoop-client-modules/hadoop-client-minicluster/pom.xml index b8cb525669b1f..cecda5a0f793c 100644 --- a/hadoop-client-modules/hadoop-client-minicluster/pom.xml +++ b/hadoop-client-modules/hadoop-client-minicluster/pom.xml @@ -396,78 +396,19 @@ - - - org.mockito - mockito-core - true - - - - - - - com.sun.jersey - jersey-core - true - - - javax.ws.rs - jsr311-api - - - - - com.sun.jersey - jersey-client - true - + - com.github.pjfanning - jersey-json - true - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - javax.xml.bind - jaxb-api - - - - - com.sun.jersey - jersey-server + javax.ws.rs + javax.ws.rs-api + compile true + + - com.sun.jersey - jersey-servlet + org.mockito + mockito-core true - - - javax.servlet - servlet-api - - - javax.enterprise - cdi-api - - - ch.qos.cal10n - cal10n-api - - @@ -611,24 +552,7 @@ - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - true - - - - org.glassfish - javax.servlet - - - - - com.sun.jersey.contribs - jersey-guice - true - @@ -704,6 +628,7 @@ org.bouncycastle:* org.xerial.snappy:* + javax.ws.rs:javax.ws.rs-api diff --git a/hadoop-cloud-storage-project/hadoop-cloud-storage/pom.xml b/hadoop-cloud-storage-project/hadoop-cloud-storage/pom.xml index 33d3f9578172f..dee6da16ab8c4 100644 --- a/hadoop-cloud-storage-project/hadoop-cloud-storage/pom.xml +++ b/hadoop-cloud-storage-project/hadoop-cloud-storage/pom.xml @@ -69,18 +69,6 @@ org.mortbay.jetty servlet-api-2.5 - - com.sun.jersey - jersey-core - - - com.sun.jersey - jersey-json - - - com.sun.jersey - jersey-server - org.eclipse.jdt core diff --git a/hadoop-common-project/hadoop-common/pom.xml b/hadoop-common-project/hadoop-common/pom.xml index d8e2dd3542223..ce0263f6cbfd9 100644 --- a/hadoop-common-project/hadoop-common/pom.xml +++ b/hadoop-common-project/hadoop-common/pom.xml @@ -133,57 +133,38 @@ runtime - com.sun.jersey - jersey-core + commons-logging + commons-logging compile - com.sun.jersey - jersey-servlet + org.glassfish.jersey.core + jersey-server compile - - - javax.enterprise - cdi-api - - - javax.servlet - servlet-api - - - ch.qos.cal10n - cal10n-api - - - com.github.pjfanning - jersey-json - compile - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - + javax.ws.rs + javax.ws.rs-api - com.sun.jersey - jersey-server + org.glassfish.jersey.containers + jersey-container-servlet-core compile - commons-logging - commons-logging - compile + org.glassfish.grizzly + grizzly-http-servlet + test + + + org.glassfish.grizzly + grizzly-http + test + + + org.glassfish.grizzly + grizzly-http-server + test log4j @@ -200,11 +181,6 @@ assertj-core test - - org.glassfish.grizzly - grizzly-http-servlet - test - commons-beanutils commons-beanutils diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java index 1db8c750cef93..8fb72be55e71f 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java @@ -58,7 +58,6 @@ import org.apache.hadoop.classification.VisibleForTesting; import org.apache.hadoop.util.Preconditions; import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableMap; -import com.sun.jersey.spi.container.servlet.ServletContainer; import org.apache.hadoop.HadoopIllegalArgumentException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -114,6 +113,8 @@ import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.webapp.WebAppContext; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.servlet.ServletContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -1017,10 +1018,24 @@ public void addJerseyResourcePackage(final String packageName, final String pathSpec, Map params) { LOG.info("addJerseyResourcePackage: packageName=" + packageName + ", pathSpec=" + pathSpec); - final ServletHolder sh = new ServletHolder(ServletContainer.class); - sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", - "com.sun.jersey.api.core.PackagesResourceConfig"); - sh.setInitParameter("com.sun.jersey.config.property.packages", packageName); + final ResourceConfig config = new ResourceConfig().packages(packageName); + final ServletHolder sh = new ServletHolder(new ServletContainer(config)); + for (Map.Entry entry : params.entrySet()) { + sh.setInitParameter(entry.getKey(), entry.getValue()); + } + webAppContext.addServlet(sh, pathSpec); + } + + /** + * Add a Jersey resource config. + * @param config The Jersey ResourceConfig to be registered. + * @param pathSpec The path spec for the servlet + * @param params properties and features for ResourceConfig + */ + public void addJerseyResourceConfig(final ResourceConfig config, + final String pathSpec, Map params) { + LOG.info("addJerseryResourceConfig: pathSpec={}", pathSpec); + final ServletHolder sh = new ServletHolder(new ServletContainer(config)); for (Map.Entry entry : params.entrySet()) { sh.setInitParameter(entry.getKey(), entry.getValue()); } diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java index 53bb44fb2e9ef..806d5db6e51a2 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java @@ -73,7 +73,7 @@ public class DNS { * @return The host name associated with the provided IP * @throws NamingException If a NamingException is encountered */ - public static String reverseDns(InetAddress hostIp, @Nullable String ns) + public static String reverseDns(InetAddress hostIp, String ns) throws NamingException { // // Builds the reverse IP lookup form @@ -246,7 +246,7 @@ public static String getDefaultIP(String strInterface) * @throws UnknownHostException if the given interface is invalid */ public static String[] getHosts(String strInterface, - @Nullable String nameserver, + String nameserver, boolean tryfallbackResolution) throws UnknownHostException { final List hosts = new Vector(); @@ -357,8 +357,8 @@ public static String[] getHosts(String strInterface) * @throws UnknownHostException * If one is encountered while querying the default interface */ - public static String getDefaultHost(@Nullable String strInterface, - @Nullable String nameserver, + public static String getDefaultHost(String strInterface, + String nameserver, boolean tryfallbackResolution) throws UnknownHostException { if (strInterface == null || "default".equals(strInterface)) { @@ -385,7 +385,7 @@ public static String getDefaultHost(@Nullable String strInterface, * @throws UnknownHostException * If one is encountered while querying the default interface */ - public static String getDefaultHost(@Nullable String strInterface) + public static String getDefaultHost(String strInterface) throws UnknownHostException { return getDefaultHost(strInterface, null, false); } @@ -401,8 +401,8 @@ public static String getDefaultHost(@Nullable String strInterface) * @throws UnknownHostException * If one is encountered while querying the default interface */ - public static String getDefaultHost(@Nullable String strInterface, - @Nullable String nameserver) + public static String getDefaultHost(String strInterface, + String nameserver) throws UnknownHostException { return getDefaultHost(strInterface, nameserver, false); } diff --git a/hadoop-common-project/hadoop-kms/pom.xml b/hadoop-common-project/hadoop-kms/pom.xml index 96588a22b9419..88ba9b4938d7e 100644 --- a/hadoop-common-project/hadoop-kms/pom.xml +++ b/hadoop-common-project/hadoop-kms/pom.xml @@ -59,15 +59,30 @@ compile - com.sun.jersey - jersey-core + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider compile - com.sun.jersey + org.glassfish.jersey.core + jersey-common + compile + + + org.glassfish.jersey.core jersey-server compile + + org.glassfish.jersey.containers + jersey-container-servlet-core + compile + + + org.glassfish.jersey.inject + jersey-hk2 + runtime + javax.servlet javax.servlet-api diff --git a/hadoop-common-project/hadoop-kms/src/main/conf/kms-log4j.properties b/hadoop-common-project/hadoop-kms/src/main/conf/kms-log4j.properties index e2afd41be0898..10a26ff5b7481 100644 --- a/hadoop-common-project/hadoop-kms/src/main/conf/kms-log4j.properties +++ b/hadoop-common-project/hadoop-kms/src/main/conf/kms-log4j.properties @@ -34,7 +34,6 @@ log4j.additivity.kms-audit=false log4j.rootLogger=INFO, kms log4j.logger.org.apache.hadoop=INFO -log4j.logger.com.sun.jersey.server.wadl.generators.WadlGeneratorJAXBGrammarGenerator=OFF # make zookeeper log level an explicit config, and not changing with rootLogger. log4j.logger.org.apache.zookeeper=INFO log4j.logger.org.apache.curator=INFO diff --git a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMS.java b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMS.java index 82f1bde1fac64..4e0daba9dc05e 100644 --- a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMS.java +++ b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMS.java @@ -39,6 +39,7 @@ import javax.ws.rs.DELETE; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; +import javax.ws.rs.OPTIONS; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -114,6 +115,14 @@ private static URI getKeyURI(String domain, String keyName) { .build(domain, KMSRESTConstants.KEY_RESOURCE, keyName); } + @OPTIONS + public Response handleOptions() { + return Response.ok() + .header("Allow", "GET") + .header("Allow", "OPTIONS") + .build(); + } + @POST @Path(KMSRESTConstants.KEYS_RESOURCE) @Consumes(MediaType.APPLICATION_JSON) @@ -175,13 +184,9 @@ public KeyVersion run() throws Exception { keyVersion = removeKeyMaterial(keyVersion); } Map json = KMSUtil.toJSON(keyVersion); - String requestURL = KMSMDCFilter.getURL(); - int idx = requestURL.lastIndexOf(KMSRESTConstants.KEYS_RESOURCE); - requestURL = requestURL.substring(0, idx); LOG.trace("Exiting createKey Method."); return Response.created(getKeyURI(KMSRESTConstants.SERVICE_VERSION, name)) - .type(MediaType.APPLICATION_JSON) - .header("Location", getKeyURI(requestURL, name)).entity(json).build(); + .type(MediaType.APPLICATION_JSON).entity(json).build(); } catch (Exception e) { LOG.debug("Exception in createKey.", e); throw e; diff --git a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSExceptionsProvider.java b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSExceptionsProvider.java index ceaa8bc815e5f..1bc142773666d 100644 --- a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSExceptionsProvider.java +++ b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSExceptionsProvider.java @@ -19,13 +19,12 @@ import org.apache.hadoop.classification.InterfaceAudience; -import com.sun.jersey.api.container.ContainerException; - import org.apache.hadoop.security.AccessControlException; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.authentication.client.AuthenticationException; import org.apache.hadoop.security.authorize.AuthorizationException; import org.apache.hadoop.util.HttpExceptionUtils; +import org.glassfish.jersey.server.ContainerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/hadoop-common-project/hadoop-kms/src/main/resources/webapps/kms/WEB-INF/web.xml b/hadoop-common-project/hadoop-kms/src/main/resources/webapps/kms/WEB-INF/web.xml index 737236c24a1b5..9565d378d444e 100644 --- a/hadoop-common-project/hadoop-kms/src/main/resources/webapps/kms/WEB-INF/web.xml +++ b/hadoop-common-project/hadoop-kms/src/main/resources/webapps/kms/WEB-INF/web.xml @@ -20,23 +20,11 @@ webservices-driver - com.sun.jersey.spi.container.servlet.ServletContainer + org.glassfish.jersey.servlet.ServletContainer - com.sun.jersey.config.property.packages + jersey.config.server.provider.packages org.apache.hadoop.crypto.key.kms.server - - - 1 diff --git a/hadoop-common-project/hadoop-kms/src/test/resources/log4j.properties b/hadoop-common-project/hadoop-kms/src/test/resources/log4j.properties index 73c48534a0a01..50f5d4edf8c8b 100644 --- a/hadoop-common-project/hadoop-kms/src/test/resources/log4j.properties +++ b/hadoop-common-project/hadoop-kms/src/test/resources/log4j.properties @@ -25,8 +25,6 @@ log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{1} - %m%n log4j.rootLogger=INFO, stdout log4j.logger.org.apache.hadoop.conf=ERROR log4j.logger.org.apache.hadoop.crytpo.key.kms.server=ALL -log4j.logger.com.sun.jersey.server.wadl.generators.WadlGeneratorJAXBGrammarGenerator=OFF -log4j.logger.com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator=OFF log4j.logger.org.apache.hadoop.security=OFF log4j.logger.org.apache.directory.server.core=OFF log4j.logger.org.apache.hadoop.util.NativeCodeLoader=OFF diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/pom.xml b/hadoop-hdfs-project/hadoop-hdfs-httpfs/pom.xml index a1b3ab1f92397..5ad4d229cde1b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/pom.xml +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/pom.xml @@ -58,12 +58,12 @@ compile - com.sun.jersey - jersey-core + org.glassfish.jersey.core + jersey-common compile - com.sun.jersey + org.glassfish.jersey.core jersey-server compile diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSExceptionProvider.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSExceptionProvider.java index 4739e42137ccb..e1eaf424087b3 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSExceptionProvider.java +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSExceptionProvider.java @@ -18,10 +18,10 @@ package org.apache.hadoop.fs.http.server; -import com.sun.jersey.api.container.ContainerException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.lib.service.FileSystemAccessException; import org.apache.hadoop.lib.wsrs.ExceptionProvider; +import org.glassfish.jersey.server.ContainerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSParametersProvider.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSParametersProvider.java index 41009c7b5169f..283647aba7ade 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSParametersProvider.java +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSParametersProvider.java @@ -34,7 +34,6 @@ import org.apache.hadoop.lib.wsrs.ShortParam; import org.apache.hadoop.lib.wsrs.StringParam; import org.apache.hadoop.util.StringUtils; -import javax.ws.rs.ext.Provider; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; @@ -42,7 +41,6 @@ /** * HttpFS ParametersProvider. */ -@Provider @InterfaceAudience.Private @SuppressWarnings("unchecked") public class HttpFSParametersProvider extends ParametersProvider { @@ -129,7 +127,7 @@ public class HttpFSParametersProvider extends ParametersProvider { PARAMS_DEF.put(Operation.SATISFYSTORAGEPOLICY, new Class[] {}); } - public HttpFSParametersProvider() { + HttpFSParametersProvider() { super(HttpFSFileSystem.OP_PARAM, HttpFSFileSystem.Operation.class, PARAMS_DEF); } diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSServer.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSServer.java index 399ff3bde9c5b..f5b6330e000b6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSServer.java +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSServer.java @@ -140,6 +140,15 @@ private UserGroupInformation getHttpUGI(HttpServletRequest request) { } + private static final HttpFSParametersProvider PARAMETERS_PROVIDER = + new HttpFSParametersProvider(); + + private Parameters getParams(HttpServletRequest request) { + return PARAMETERS_PROVIDER.get(request); + } + + private static final Object[] NULL = new Object[0]; + /** * Executes a {@link FileSystemAccess.FileSystemExecutor} using a filesystem for the effective * user. @@ -199,7 +208,6 @@ private void enforceRootPath(HttpFSFileSystem.Operation op, String path) { * * @param uriInfo uri info of the request. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. * * @return the request response. * @@ -213,10 +221,9 @@ private void enforceRootPath(HttpFSFileSystem.Operation op, String path) { @Produces(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8) public Response getRoot(@Context UriInfo uriInfo, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) throws IOException, FileSystemAccessException { - return get("", uriInfo, op, params, request); + return get("", uriInfo, op, request); } private String makeAbsolute(String path) { @@ -229,7 +236,6 @@ private String makeAbsolute(String path) { * @param path the path for operation. * @param uriInfo uri info of the request. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. * * @return the request response. * @@ -246,7 +252,6 @@ private String makeAbsolute(String path) { public Response get(@PathParam("path") String path, @Context UriInfo uriInfo, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) throws IOException, FileSystemAccessException { // Restrict access to only GETFILESTATUS and LISTSTATUS in write-only mode @@ -258,6 +263,7 @@ public Response get(@PathParam("path") String path, UserGroupInformation user = HttpUserGroupInformation.get(); Response response; path = makeAbsolute(path); + final Parameters params = getParams(request); MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name()); MDC.put("hostname", request.getRemoteAddr()); switch (op.value()) { @@ -535,7 +541,6 @@ private URI createOpenRedirectionURL(UriInfo uriInfo) { * * @param path the path for operation. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. * * @return the request response. * @@ -550,7 +555,6 @@ private URI createOpenRedirectionURL(UriInfo uriInfo) { @Produces(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8) public Response delete(@PathParam("path") String path, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) throws IOException, FileSystemAccessException { // Do not allow DELETE commands in read-only mode @@ -560,6 +564,7 @@ public Response delete(@PathParam("path") String path, UserGroupInformation user = HttpUserGroupInformation.get(); Response response; path = makeAbsolute(path); + final Parameters params = getParams(request); MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name()); MDC.put("hostname", request.getRemoteAddr()); switch (op.value()) { @@ -597,7 +602,7 @@ public Response delete(@PathParam("path") String path, * @param is the inputstream for the request payload. * @param uriInfo the of the request. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. + * @param request the HttpFS request. * * @return the request response. * @@ -611,9 +616,9 @@ public Response delete(@PathParam("path") String path, @Produces({ MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8 }) public Response postRoot(InputStream is, @Context UriInfo uriInfo, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) + @Context HttpServletRequest request) throws IOException, FileSystemAccessException { - return post(is, uriInfo, "/", op, params, request); + return post(is, uriInfo, "/", op, request); } /** @@ -623,7 +628,6 @@ public Response postRoot(InputStream is, @Context UriInfo uriInfo, * @param uriInfo the of the request. * @param path the path for operation. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. * * @return the request response. * @@ -641,7 +645,6 @@ public Response post(InputStream is, @Context UriInfo uriInfo, @PathParam("path") String path, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) throws IOException, FileSystemAccessException { // Do not allow POST commands in read-only mode @@ -651,6 +654,7 @@ public Response post(InputStream is, UserGroupInformation user = HttpUserGroupInformation.get(); Response response; path = makeAbsolute(path); + final Parameters params = getParams(request); MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name()); MDC.put("hostname", request.getRemoteAddr()); switch (op.value()) { @@ -727,10 +731,11 @@ public Response post(InputStream is, */ protected URI createUploadRedirectionURL(UriInfo uriInfo, Enum uploadOperation) { UriBuilder uriBuilder = uriInfo.getRequestUriBuilder(); - uriBuilder = uriBuilder.replaceQueryParam(OperationParam.NAME, uploadOperation). - queryParam(DataParam.NAME, Boolean.TRUE) - .replaceQueryParam(NoRedirectParam.NAME, (Object[]) null); - return uriBuilder.build(null); + uriBuilder = uriBuilder.replaceQueryParam(OperationParam.NAME, uploadOperation) + .queryParam(DataParam.NAME, Boolean.TRUE) + .replaceQueryParam(NoRedirectParam.NAME, (Object[]) null); + // Workaround: NPE occurs when using null in Jersey 2.29 + return uriBuilder.build(NULL); } /** @@ -738,7 +743,7 @@ protected URI createUploadRedirectionURL(UriInfo uriInfo, Enum uploadOperatio * @param is the inputstream for the request payload. * @param uriInfo the of the request. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. + * @param request the HttpFS request. * * @return the request response. * @@ -752,9 +757,9 @@ protected URI createUploadRedirectionURL(UriInfo uriInfo, Enum uploadOperatio @Produces({ MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8 }) public Response putRoot(InputStream is, @Context UriInfo uriInfo, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) + @Context HttpServletRequest request) throws IOException, FileSystemAccessException { - return put(is, uriInfo, "/", op, params, request); + return put(is, uriInfo, "/", op, request); } /** @@ -764,7 +769,6 @@ public Response putRoot(InputStream is, @Context UriInfo uriInfo, * @param uriInfo the of the request. * @param path the path for operation. * @param op the HttpFS operation of the request. - * @param params the HttpFS parameters of the request. * * @return the request response. * @@ -782,7 +786,6 @@ public Response put(InputStream is, @Context UriInfo uriInfo, @PathParam("path") String path, @QueryParam(OperationParam.NAME) OperationParam op, - @Context Parameters params, @Context HttpServletRequest request) throws IOException, FileSystemAccessException { // Do not allow PUT commands in read-only mode @@ -792,6 +795,7 @@ public Response put(InputStream is, UserGroupInformation user = HttpUserGroupInformation.get(); Response response; path = makeAbsolute(path); + final Parameters params = getParams(request); MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name()); MDC.put("hostname", request.getRemoteAddr()); switch (op.value()) { diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/wsrs/ParametersProvider.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/wsrs/ParametersProvider.java index 7addec58c2fb2..3ddb0faab561e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/wsrs/ParametersProvider.java +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/wsrs/ParametersProvider.java @@ -18,32 +18,22 @@ package org.apache.hadoop.lib.wsrs; -import com.sun.jersey.api.core.HttpContext; -import com.sun.jersey.core.spi.component.ComponentContext; -import com.sun.jersey.core.spi.component.ComponentScope; -import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable; -import com.sun.jersey.spi.inject.Injectable; -import com.sun.jersey.spi.inject.InjectableProvider; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.util.Lists; import org.apache.hadoop.util.StringUtils; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MultivaluedMap; -import java.lang.reflect.Type; +import javax.servlet.http.HttpServletRequest; import java.text.MessageFormat; import java.util.HashMap; import java.util.List; import java.util.Map; /** - * Jersey provider that parses the request parameters based on the - * given parameter definition. + * Provider that parses the request parameters based on the + * given parameter definition. */ @InterfaceAudience.Private -public class ParametersProvider - extends AbstractHttpContextInjectable - implements InjectableProvider { +public class ParametersProvider { private String driverParam; private Class enumClass; @@ -56,34 +46,42 @@ public ParametersProvider(String driverParam, Class enumClass, this.paramsDef = paramsDef; } - @Override - @SuppressWarnings("unchecked") - public Parameters getValue(HttpContext httpContext) { - Map>> map = new HashMap>>(); - Map> queryString = - httpContext.getRequest().getQueryParameters(); - String str = ((MultivaluedMap) queryString). - getFirst(driverParam); + private Param newParam(Class> paramClass) { + try { + return paramClass.newInstance(); + } catch (Exception ex) { + throw new UnsupportedOperationException( + MessageFormat.format( + "Param class [{0}] does not have default constructor", + paramClass.getName())); + } + } + + public Parameters get(HttpServletRequest request) { + Map>> map = new HashMap<>(); + + Map queryString = request.getParameterMap(); + String str = queryString.get(driverParam)[0]; if (str == null) { throw new IllegalArgumentException( - MessageFormat.format("Missing Operation parameter [{0}]", - driverParam)); + MessageFormat.format("Missing Operation parameter [{0}]", + driverParam)); } Enum op; try { op = Enum.valueOf(enumClass, StringUtils.toUpperCase(str)); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException( - MessageFormat.format("Invalid Operation [{0}]", str)); + MessageFormat.format("Invalid Operation [{0}]", str)); } if (!paramsDef.containsKey(op)) { throw new IllegalArgumentException( - MessageFormat.format("Unsupported Operation [{0}]", op)); + MessageFormat.format("Unsupported Operation [{0}]", op)); } for (Class> paramClass : paramsDef.get(op)) { Param param = newParam(paramClass); List> paramList = Lists.newArrayList(); - List ps = queryString.get(param.getName()); + String[] ps = queryString.get(param.getName()); if (ps != null) { for (String p : ps) { try { @@ -103,25 +101,4 @@ public Parameters getValue(HttpContext httpContext) { } return new Parameters(map); } - - private Param newParam(Class> paramClass) { - try { - return paramClass.newInstance(); - } catch (Exception ex) { - throw new UnsupportedOperationException( - MessageFormat.format( - "Param class [{0}] does not have default constructor", - paramClass.getName())); - } - } - - @Override - public ComponentScope getScope() { - return ComponentScope.PerRequest; - } - - @Override - public Injectable getInjectable(ComponentContext componentContext, Context context, Type type) { - return (type.equals(Parameters.class)) ? this : null; - } } diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/resources/webapps/webhdfs/WEB-INF/web.xml b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/resources/webapps/webhdfs/WEB-INF/web.xml index 3da9a5cf9761f..c0567ba96edcf 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/resources/webapps/webhdfs/WEB-INF/web.xml +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/resources/webapps/webhdfs/WEB-INF/web.xml @@ -20,23 +20,11 @@ webservices-driver - com.sun.jersey.spi.container.servlet.ServletContainer + org.glassfish.jersey.servlet.ServletContainer - com.sun.jersey.config.property.packages + jersey.config.server.provider.packages org.apache.hadoop.fs.http.server,org.apache.hadoop.lib.wsrs - - - 1 diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/webapp/WEB-INF/web.xml b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/webapp/WEB-INF/web.xml index 4c0b3aedb0ef0..a4ba0abd892ea 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/webapp/WEB-INF/web.xml +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/webapp/WEB-INF/web.xml @@ -20,23 +20,11 @@ webservices-driver - com.sun.jersey.spi.container.servlet.ServletContainer + org.glassfish.jersey.servlet.ServletContainer - com.sun.jersey.config.property.packages + jersey.config.server.provider.packages org.apache.hadoop.fs.http.server,org.apache.hadoop.lib.wsrs - - - 1 diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/default-log4j.properties b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/default-log4j.properties index 45a8412f5a76c..9ef5ec63d3464 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/default-log4j.properties +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/resources/default-log4j.properties @@ -23,4 +23,3 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{1} - %m%n -log4j.logger.com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator=OFF diff --git a/hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml b/hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml index 24599a2b244a4..78f5878e2c6e0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml +++ b/hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml @@ -108,16 +108,6 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd"> jetty-util compile - - com.sun.jersey - jersey-core - compile - - - com.sun.jersey - jersey-server - compile - commons-cli commons-cli diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml b/hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml index d4d5c1eb33983..9d30b2ec3c720 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml @@ -121,6 +121,10 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd"> com.fasterxml.jackson.core jackson-databind + + org.codehaus.jettison + jettison + junit junit diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterWebHdfsMethods.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterWebHdfsMethods.java index a66953b1bd752..567d595c20df6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterWebHdfsMethods.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterWebHdfsMethods.java @@ -37,9 +37,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import com.sun.jersey.spi.container.ResourceFilters; import org.apache.hadoop.hdfs.web.JsonUtil; -import org.apache.hadoop.hdfs.web.ParamFilter; import org.apache.hadoop.hdfs.web.WebHdfsFileSystem; import org.apache.hadoop.hdfs.web.resources.AccessTimeParam; import org.apache.hadoop.hdfs.web.resources.AclPermissionParam; @@ -117,7 +115,6 @@ * {@link NamenodeWebHdfsMethods}, and tries to reuse as much as possible. */ @Path("") -@ResourceFilters(ParamFilter.class) public class RouterWebHdfsMethods extends NamenodeWebHdfsMethods { private static final Logger LOG = LoggerFactory.getLogger(RouterWebHdfsMethods.class); diff --git a/hadoop-hdfs-project/hadoop-hdfs/pom.xml b/hadoop-hdfs-project/hadoop-hdfs/pom.xml index b51c7154f7b92..b0239f4a5d5b6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/pom.xml +++ b/hadoop-hdfs-project/hadoop-hdfs/pom.xml @@ -93,15 +93,25 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd"> compile - com.sun.jersey - jersey-core + org.glassfish.jersey.core + jersey-common compile - com.sun.jersey + org.glassfish.jersey.core jersey-server compile + + org.glassfish.jersey.inject + jersey-hk2 + runtime + + + org.glassfish.jersey.media + jersey-media-json-jettison + test + commons-cli commons-cli diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/web/webhdfs/ExceptionHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/web/webhdfs/ExceptionHandler.java index 02ec25c13c874..b74558aaf631c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/web/webhdfs/ExceptionHandler.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/web/webhdfs/ExceptionHandler.java @@ -18,11 +18,11 @@ package org.apache.hadoop.hdfs.server.datanode.web.webhdfs; import org.apache.hadoop.thirdparty.com.google.common.base.Charsets; -import com.sun.jersey.api.ParamException; -import com.sun.jersey.api.container.ContainerException; import io.netty.buffer.Unpooled; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; +import org.glassfish.jersey.server.ContainerException; +import org.glassfish.jersey.server.ParamException; import org.slf4j.Logger; import org.apache.hadoop.hdfs.web.JsonUtil; import org.apache.hadoop.ipc.RemoteException; diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeHttpServer.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeHttpServer.java index e265742978d85..562156d2d8c0a 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeHttpServer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeHttpServer.java @@ -39,16 +39,19 @@ import org.apache.hadoop.hdfs.server.common.TokenVerifier; import org.apache.hadoop.hdfs.server.namenode.startupprogress.StartupProgress; import org.apache.hadoop.hdfs.server.namenode.web.resources.NamenodeWebHdfsMethods; +import org.apache.hadoop.hdfs.web.ParamFilter; import org.apache.hadoop.hdfs.web.WebHdfsFileSystem; import org.apache.hadoop.hdfs.web.resources.AclPermissionParam; import org.apache.hadoop.hdfs.web.resources.Param; import org.apache.hadoop.hdfs.web.resources.UserParam; +import org.apache.hadoop.hdfs.web.resources.UserProvider; import org.apache.hadoop.http.HttpConfig; import org.apache.hadoop.http.HttpServer2; import org.apache.hadoop.net.NetUtils; +import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.http.RestCsrfPreventionFilter; - -import com.sun.jersey.api.core.ResourceConfig; +import org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; /** * Encapsulates the HTTP server started by the NameNode. @@ -101,12 +104,24 @@ public static void initWebHdfs(Configuration conf, String hostname, new String[] {pathSpec}); } + // add a filter to change parameter names to lower cases + HttpServer2.defineFilter(httpServer2.getWebAppContext(), + ParamFilter.class.getName(), ParamFilter.class.getName(), null, + new String[] {pathSpec}); + // add webhdfs packages final Map params = new HashMap<>(); - params.put(ResourceConfig.FEATURE_MATCH_MATRIX_PARAMS, "true"); - httpServer2.addJerseyResourcePackage( - jerseyResourcePackage + ";" + Param.class.getPackage().getName(), - pathSpec, params); + ResourceConfig config = new ResourceConfig(); + config.packages(jerseyResourcePackage, Param.class.getPackage().getName()); + config.register(new AbstractBinder() { + // add a factory to generate UserGroupInformation + @Override + protected void configure() { + bindFactory(UserProvider.class) + .to(UserGroupInformation.class); + } + }); + httpServer2.addJerseyResourceConfig(config, pathSpec, params); } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java index a3250c213caf0..9120c3f50ac5a 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java @@ -54,6 +54,7 @@ import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; +import javax.ws.rs.core.UriInfo; import org.apache.hadoop.fs.InvalidPathException; import org.apache.hadoop.fs.QuotaUsage; @@ -100,7 +101,6 @@ import org.apache.hadoop.hdfs.server.namenode.NameNode; import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols; import org.apache.hadoop.hdfs.web.JsonUtil; -import org.apache.hadoop.hdfs.web.ParamFilter; import org.apache.hadoop.hdfs.web.WebHdfsConstants; import org.apache.hadoop.hdfs.web.WebHdfsFileSystem; import org.apache.hadoop.hdfs.web.resources.*; @@ -119,11 +119,9 @@ import org.apache.hadoop.classification.VisibleForTesting; import org.apache.hadoop.thirdparty.com.google.common.base.Charsets; -import com.sun.jersey.spi.container.ResourceFilters; /** Web-hdfs NameNode implementation. */ @Path("") -@ResourceFilters(ParamFilter.class) public class NamenodeWebHdfsMethods { public static final Logger LOG = LoggerFactory.getLogger(NamenodeWebHdfsMethods.class); @@ -463,12 +461,12 @@ private URI redirectURI(ResponseBuilder rb, final NameNode namenode, /** Handle HTTP PUT request for the root. */ @PUT - @Path("/") @Consumes({"*/*"}) @Produces({MediaType.APPLICATION_OCTET_STREAM + "; " + JettyUtils.UTF_8, MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8}) public Response putRoot( @Context final UserGroupInformation ugi, + @Context final UriInfo uriInfo, @QueryParam(DelegationParam.NAME) @DefaultValue(DelegationParam.DEFAULT) final DelegationParam delegation, @QueryParam(UserParam.NAME) @DefaultValue(UserParam.DEFAULT) @@ -538,7 +536,7 @@ public Response putRoot( @DefaultValue(StorageTypeParam.DEFAULT) final StorageTypeParam storageType ) throws IOException, InterruptedException { - return put(ugi, delegation, username, doAsUser, ROOT, op, destination, + return put(ugi, uriInfo, delegation, username, doAsUser, op, destination, owner, group, permission, unmaskedPermission, overwrite, bufferSize, replication, blockSize, modificationTime, accessTime, renameOptions, createParent, delegationTokenArgument, aclPermission, xattrName, @@ -567,13 +565,13 @@ protected void validateOpParams(HttpOpParam op, Param... params) { MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8}) public Response put( @Context final UserGroupInformation ugi, + @Context final UriInfo uriInfo, @QueryParam(DelegationParam.NAME) @DefaultValue(DelegationParam.DEFAULT) final DelegationParam delegation, @QueryParam(UserParam.NAME) @DefaultValue(UserParam.DEFAULT) final UserParam username, @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) final DoAsParam doAsUser, - @PathParam(UriFsPathParam.NAME) final UriFsPathParam path, @QueryParam(PutOpParam.NAME) @DefaultValue(PutOpParam.DEFAULT) final PutOpParam op, @QueryParam(DestinationParam.NAME) @DefaultValue(DestinationParam.DEFAULT) @@ -636,6 +634,7 @@ public Response put( @QueryParam(StorageTypeParam.NAME) @DefaultValue(StorageTypeParam.DEFAULT) final StorageTypeParam storageType ) throws IOException, InterruptedException { + final UriFsPathParam path = new UriFsPathParam(uriInfo.getPath()); init(ugi, delegation, username, doAsUser, path, op, destination, owner, group, permission, unmaskedPermission, overwrite, bufferSize, replication, blockSize, modificationTime, accessTime, renameOptions, @@ -889,7 +888,6 @@ protected Response put( /** Handle HTTP POST request for the root. */ @POST - @Path("/") @Consumes({"*/*"}) @Produces({MediaType.APPLICATION_OCTET_STREAM + "; " + JettyUtils.UTF_8, MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8}) @@ -1019,11 +1017,11 @@ protected Response post( /** Handle HTTP GET request for the root. */ @GET - @Path("/") @Produces({MediaType.APPLICATION_OCTET_STREAM + "; " + JettyUtils.UTF_8, MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8}) public Response getRoot( @Context final UserGroupInformation ugi, + @Context final UriInfo uriInfo, @QueryParam(DelegationParam.NAME) @DefaultValue(DelegationParam.DEFAULT) final DelegationParam delegation, @QueryParam(UserParam.NAME) @DefaultValue(UserParam.DEFAULT) @@ -1065,7 +1063,7 @@ public Response getRoot( @QueryParam(StartAfterParam.NAME) @DefaultValue(StartAfterParam.DEFAULT) final StartAfterParam startAfter ) throws IOException, InterruptedException { - return get(ugi, delegation, username, doAsUser, ROOT, op, offset, length, + return get(ugi, uriInfo, delegation, username, doAsUser, op, offset, length, renewer, bufferSize, xattrNames, xattrEncoding, excludeDatanodes, fsAction, snapshotName, oldSnapshotName, snapshotDiffStartPath, snapshotDiffIndex, @@ -1080,13 +1078,13 @@ public Response getRoot( MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8}) public Response get( @Context final UserGroupInformation ugi, + @Context final UriInfo uriInfo, @QueryParam(DelegationParam.NAME) @DefaultValue(DelegationParam.DEFAULT) final DelegationParam delegation, @QueryParam(UserParam.NAME) @DefaultValue(UserParam.DEFAULT) final UserParam username, @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) final DoAsParam doAsUser, - @PathParam(UriFsPathParam.NAME) final UriFsPathParam path, @QueryParam(GetOpParam.NAME) @DefaultValue(GetOpParam.DEFAULT) final GetOpParam op, @QueryParam(OffsetParam.NAME) @DefaultValue(OffsetParam.DEFAULT) @@ -1122,7 +1120,7 @@ public Response get( @QueryParam(StartAfterParam.NAME) @DefaultValue(StartAfterParam.DEFAULT) final StartAfterParam startAfter ) throws IOException, InterruptedException { - + final UriFsPathParam path = new UriFsPathParam(uriInfo.getPath()); init(ugi, delegation, username, doAsUser, path, op, offset, length, renewer, bufferSize, xattrEncoding, excludeDatanodes, fsAction, snapshotName, oldSnapshotName, tokenKind, tokenService, startAfter); @@ -1524,10 +1522,10 @@ public Void run() throws IOException { /** Handle HTTP DELETE request for the root. */ @DELETE - @Path("/") @Produces(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8) public Response deleteRoot( @Context final UserGroupInformation ugi, + @Context final UriInfo uriInfo, @QueryParam(DelegationParam.NAME) @DefaultValue(DelegationParam.DEFAULT) final DelegationParam delegation, @QueryParam(UserParam.NAME) @DefaultValue(UserParam.DEFAULT) @@ -1541,7 +1539,7 @@ public Response deleteRoot( @QueryParam(SnapshotNameParam.NAME) @DefaultValue(SnapshotNameParam.DEFAULT) final SnapshotNameParam snapshotName ) throws IOException, InterruptedException { - return delete(ugi, delegation, username, doAsUser, ROOT, op, recursive, + return delete(ugi, uriInfo, delegation, username, doAsUser, op, recursive, snapshotName); } @@ -1551,13 +1549,13 @@ public Response deleteRoot( @Produces(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8) public Response delete( @Context final UserGroupInformation ugi, + @Context final UriInfo uriInfo, @QueryParam(DelegationParam.NAME) @DefaultValue(DelegationParam.DEFAULT) final DelegationParam delegation, @QueryParam(UserParam.NAME) @DefaultValue(UserParam.DEFAULT) final UserParam username, @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) final DoAsParam doAsUser, - @PathParam(UriFsPathParam.NAME) final UriFsPathParam path, @QueryParam(DeleteOpParam.NAME) @DefaultValue(DeleteOpParam.DEFAULT) final DeleteOpParam op, @QueryParam(RecursiveParam.NAME) @DefaultValue(RecursiveParam.DEFAULT) @@ -1565,7 +1563,7 @@ public Response delete( @QueryParam(SnapshotNameParam.NAME) @DefaultValue(SnapshotNameParam.DEFAULT) final SnapshotNameParam snapshotName ) throws IOException, InterruptedException { - + final UriFsPathParam path = new UriFsPathParam(uriInfo.getPath()); init(ugi, delegation, username, doAsUser, path, op, recursive, snapshotName); return doAs(ugi, new PrivilegedExceptionAction() { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/ParamFilter.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/ParamFilter.java index febe1253a829d..e8d0f7f515174 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/ParamFilter.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/ParamFilter.java @@ -17,70 +17,82 @@ */ package org.apache.hadoop.hdfs.web; -import java.net.URI; -import java.util.List; +import java.io.IOException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; import java.util.Map; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.UriBuilder; - -import com.sun.jersey.spi.container.ContainerRequest; -import com.sun.jersey.spi.container.ContainerRequestFilter; -import com.sun.jersey.spi.container.ContainerResponseFilter; -import com.sun.jersey.spi.container.ResourceFilter; -import org.apache.hadoop.util.StringUtils; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; /** * A filter to change parameter names to lower cases * so that parameter names are considered as case insensitive. */ -public class ParamFilter implements ResourceFilter { - private static final ContainerRequestFilter LOWER_CASE - = new ContainerRequestFilter() { - @Override - public ContainerRequest filter(final ContainerRequest request) { - final MultivaluedMap parameters = request.getQueryParameters(); - if (containsUpperCase(parameters.keySet())) { - //rebuild URI - final URI lower = rebuildQuery(request.getRequestUri(), parameters); - request.setUris(request.getBaseUri(), lower); - } - return request; - } - }; +public class ParamFilter implements Filter { @Override - public ContainerRequestFilter getRequestFilter() { - return LOWER_CASE; + public void init(FilterConfig filterConfig) throws ServletException { } @Override - public ContainerResponseFilter getResponseFilter() { - return null; + public void destroy() { } - /** Do the strings contain upper case letters? */ - static boolean containsUpperCase(final Iterable strings) { - for(String s : strings) { - for(int i = 0; i < s.length(); i++) { - if (Character.isUpperCase(s.charAt(i))) { - return true; - } - } + @Override + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) + throws IOException, ServletException { + if (request instanceof HttpServletRequest) { + chain.doFilter(new CustomHttpServletRequestWrapper( + (HttpServletRequest) request), response); + } else { + chain.doFilter(request, response); } - return false; } - /** Rebuild the URI query with lower case parameter names. */ - private static URI rebuildQuery(final URI uri, - final MultivaluedMap parameters) { - UriBuilder b = UriBuilder.fromUri(uri).replaceQuery(""); - for(Map.Entry> e : parameters.entrySet()) { - final String key = StringUtils.toLowerCase(e.getKey()); - for(String v : e.getValue()) { - b = b.queryParam(key, v); + private static final class CustomHttpServletRequestWrapper + extends HttpServletRequestWrapper { + + private Map lowerCaseParams = new HashMap<>(); + + private CustomHttpServletRequestWrapper(HttpServletRequest request) { + super(request); + Map originalParams = request.getParameterMap(); + for (Map.Entry entry : originalParams.entrySet()) { + lowerCaseParams.put(entry.getKey().toLowerCase(), entry.getValue()); + } + } + + public String getParameter(String name) { + String[] values = getParameterValues(name); + if (values != null && values.length > 0) { + return values[0]; + } else { + return null; } } - return b.build(); + + @Override + public Map getParameterMap() { + return Collections.unmodifiableMap(lowerCaseParams); + } + + @Override + public Enumeration getParameterNames() { + return Collections.enumeration(lowerCaseParams.keySet()); + } + + @Override + public String[] getParameterValues(String name) { + return lowerCaseParams.get(name); + } } } \ No newline at end of file diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ExceptionHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ExceptionHandler.java index f4704f77b104f..bbc35e22f8d5d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ExceptionHandler.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ExceptionHandler.java @@ -27,6 +27,8 @@ import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; +import org.glassfish.jersey.server.ContainerException; +import org.glassfish.jersey.server.ParamException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.hdfs.web.JsonUtil; @@ -36,8 +38,6 @@ import org.apache.hadoop.security.token.SecretManager.InvalidToken; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.ParamException; -import com.sun.jersey.api.container.ContainerException; /** Handle exceptions. */ @Provider diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/UserProvider.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/UserProvider.java index 32b3369f7a5a2..8e8977d4dd3b5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/UserProvider.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/UserProvider.java @@ -17,57 +17,44 @@ */ package org.apache.hadoop.hdfs.web.resources; -import java.io.IOException; -import java.lang.reflect.Type; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.Context; -import javax.ws.rs.ext.Provider; - +import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hdfs.server.common.JspHelper; import org.apache.hadoop.security.SecurityUtil; import org.apache.hadoop.security.UserGroupInformation; -import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod; -import com.sun.jersey.api.core.HttpContext; -import com.sun.jersey.core.spi.component.ComponentContext; -import com.sun.jersey.core.spi.component.ComponentScope; -import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable; -import com.sun.jersey.spi.inject.Injectable; -import com.sun.jersey.spi.inject.InjectableProvider; +import javax.inject.Inject; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.function.Supplier; -/** Inject user information to http operations. */ -@Provider -public class UserProvider - extends AbstractHttpContextInjectable - implements InjectableProvider { - @Context HttpServletRequest request; - @Context ServletContext servletcontext; +/** + * Jersey provider to acquire {@link UserGroupInformation} + * from {@link HttpServletRequest}. + */ +@InterfaceAudience.Private +public class UserProvider implements Supplier { + + private final HttpServletRequest request; + private final ServletContext servletcontext; + + @Inject + public UserProvider(HttpServletRequest request) { + this.request = request; + this.servletcontext = request.getServletContext(); + } @Override - public UserGroupInformation getValue(final HttpContext context) { + public UserGroupInformation get() { final Configuration conf = (Configuration) servletcontext .getAttribute(JspHelper.CURRENT_CONF); try { return JspHelper.getUGI(servletcontext, request, conf, - AuthenticationMethod.KERBEROS, false); + UserGroupInformation.AuthenticationMethod.KERBEROS, false); } catch (IOException e) { throw new SecurityException( SecurityUtil.FAILED_TO_GET_UGI_MSG_HEADER + " " + e, e); } } - - @Override - public ComponentScope getScope() { - return ComponentScope.PerRequest; - } - - @Override - public Injectable getInjectable( - final ComponentContext componentContext, final Context context, - final Type type) { - return type.equals(UserGroupInformation.class)? this : null; - } -} \ No newline at end of file +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/log4j.properties b/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/log4j.properties index 997854dcce7a3..73788467112e8 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/log4j.properties +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/resources/log4j.properties @@ -47,6 +47,3 @@ log4j.appender.DNMETRICSRFA.layout=org.apache.log4j.PatternLayout log4j.appender.DNMETRICSRFA.layout.ConversionPattern=%d{ISO8601} %m%n log4j.appender.DNMETRICSRFA.MaxBackupIndex=1 log4j.appender.DNMETRICSRFA.MaxFileSize=64MB - -# Supress KMS error log -log4j.logger.com.sun.jersey.server.wadl.generators.WadlGeneratorJAXBGrammarGenerator=OFF \ No newline at end of file diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/pom.xml b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/pom.xml index e3b3511c0ce17..fa9dcb96bb0c6 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/pom.xml +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/pom.xml @@ -57,6 +57,10 @@ + + org.glassfish.jersey.media + jersey-media-json-jettison + org.apache.hadoop hadoop-yarn-server-common diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/jobhistory/JobHistoryEventHandler.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/jobhistory/JobHistoryEventHandler.java index 9023f6a11ad84..00d168567207b 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/jobhistory/JobHistoryEventHandler.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/jobhistory/JobHistoryEventHandler.java @@ -31,6 +31,8 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; +import javax.ws.rs.ProcessingException; + import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; @@ -80,7 +82,6 @@ import org.apache.hadoop.yarn.util.TimelineServiceHelper; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.ClientHandlerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -1141,7 +1142,7 @@ private void processEventForTimelineServer(HistoryEvent event, JobId jobId, + error.getErrorCode()); } } - } catch (YarnException | IOException | ClientHandlerException ex) { + } catch (YarnException | IOException | ProcessingException ex) { LOG.error("Error putting entity " + tEntity.getEntityId() + " to Timeline" + "Server", ex); } diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/webapp/JAXBContextResolver.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/webapp/JAXBContextResolver.java index 625eb4ef32e42..3372587fb54e9 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/webapp/JAXBContextResolver.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/main/java/org/apache/hadoop/mapreduce/v2/app/webapp/JAXBContextResolver.java @@ -21,9 +21,8 @@ import java.util.HashMap; import java.util.Map; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; import com.google.inject.Singleton; +import org.glassfish.jersey.jettison.JettisonJaxbContext; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; @@ -72,13 +71,9 @@ public JAXBContextResolver() throws Exception { JAXBContext context; JAXBContext unWrappedRootContext; - this.typesContextMap = new HashMap(); - context = - new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false) - .build(), cTypes); - unWrappedRootContext = - new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(true) - .build(), rootUnwrappedTypes); + this.typesContextMap = new HashMap<>(); + context = new JettisonJaxbContext(cTypes); + unWrappedRootContext = new JettisonJaxbContext(rootUnwrappedTypes); for (Class type : cTypes) { typesContextMap.put(type, context); } diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServices.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServices.java index 5def1d91494c4..f76232d183a6e 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServices.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServices.java @@ -26,7 +26,11 @@ import java.io.StringReader; import java.util.Set; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -37,11 +41,14 @@ import org.apache.hadoop.util.Sets; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -51,12 +58,6 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Test the MapReduce Application master info web services api's. Also test @@ -65,7 +66,7 @@ * /ws/v1/mapreduce * /ws/v1/mapreduce/info */ -public class TestAMWebServices extends JerseyTestBase { +public class TestAMWebServices extends JerseyTest { private static Configuration conf = new Configuration(); private static MockAppContext appContext; @@ -83,7 +84,7 @@ protected void configureServlets() { bind(AppContext.class).toInstance(appContext); bind(Configuration.class).toInstance(conf); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -101,191 +102,179 @@ public void setUp() throws Exception { } public TestAMWebServices() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.mapreduce.v2.app.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testAM() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("mapreduce").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAMInfo(json.getJSONObject("info"), appContext); } @Test public void testAMSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("mapreduce/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAMInfo(json.getJSONObject("info"), appContext); } @Test public void testAMDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce/") - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("mapreduce/").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAMInfo(json.getJSONObject("info"), appContext); } @Test public void testAMXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("mapreduce").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyAMInfoXML(xml, appContext); } @Test public void testInfo() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("info").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("mapreduce").path("info").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAMInfo(json.getJSONObject("info"), appContext); } @Test public void testInfoSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("info/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("mapreduce").path("info/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAMInfo(json.getJSONObject("info"), appContext); } @Test public void testInfoDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("info/").get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("mapreduce").path("info/").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAMInfo(json.getJSONObject("info"), appContext); } @Test public void testInfoXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("info/").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("mapreduce").path("info/").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyAMInfoXML(xml, appContext); } @Test public void testInvalidUri() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); String responseStr = ""; try { responseStr = r.path("ws").path("v1").path("mapreduce").path("bogus") - .accept(MediaType.APPLICATION_JSON).get(String.class); + .request(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); - WebServicesTestUtils.checkStringMatch( - "error string exists and shouldn't", "", responseStr); + WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } } @Test public void testInvalidUri2() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); String responseStr = ""; try { - responseStr = r.path("ws").path("v1").path("invalid") - .accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = r.path("ws").path("v1").path("invalid").request(MediaType.APPLICATION_JSON) + .get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); - WebServicesTestUtils.checkStringMatch( - "error string exists and shouldn't", "", responseStr); + WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } } @Test public void testInvalidAccept() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); String responseStr = ""; try { - responseStr = r.path("ws").path("v1").path("mapreduce") - .accept(MediaType.TEXT_PLAIN).get(String.class); + responseStr = + r.path("ws").path("v1").path("mapreduce").request(MediaType.TEXT_PLAIN).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.INTERNAL_SERVER_ERROR, - response.getStatusInfo()); - WebServicesTestUtils.checkStringMatch( - "error string exists and shouldn't", "", responseStr); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); + assertResponseStatusCode(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo()); + WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } } - + @Test public void testBlacklistedNodes() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("blacklistednodes").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("mapreduce").path("blacklistednodes") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyBlacklistedNodesInfo(json, appContext); } - + @Test public void testBlacklistedNodesXML() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("blacklistednodes").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("mapreduce").path("blacklistednodes") + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyBlacklistedNodesInfoXML(xml, appContext); } - public void verifyAMInfo(JSONObject info, AppContext ctx) - throws JSONException { + public void verifyAMInfo(JSONObject info, AppContext ctx) throws JSONException { assertEquals("incorrect number of elements", 5, info.length()); verifyAMInfoGeneric(ctx, info.getString("appId"), info.getString("user"), - info.getString("name"), info.getLong("startedOn"), - info.getLong("elapsedTime")); + info.getString("name"), info.getLong("startedOn"), info.getLong("elapsedTime")); } public void verifyAMInfoXML(String xml, AppContext ctx) diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempt.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempt.java index f20ac6ff1b88d..8214cd8acf6ad 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempt.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempt.java @@ -46,9 +46,9 @@ import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -59,10 +59,6 @@ import com.google.inject.Guice; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Test the app master web service Rest API for getting task attempts, a @@ -70,7 +66,7 @@ * * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid}/state */ -public class TestAMWebServicesAttempt extends JerseyTestBase { +public class TestAMWebServicesAttempt extends JerseyTest { private static Configuration conf = new Configuration(); private static AppContext appContext; diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempts.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempts.java index 32d054ff5c514..be42c23546570 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempts.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesAttempts.java @@ -29,7 +29,10 @@ import java.util.List; import java.util.Map; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -46,11 +49,14 @@ import org.apache.hadoop.mapreduce.v2.util.MRApps; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -60,12 +66,6 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Test the app master web service Rest API for getting task attempts, a @@ -75,7 +75,7 @@ * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid} * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid}/counters */ -public class TestAMWebServicesAttempts extends JerseyTestBase { +public class TestAMWebServicesAttempts extends JerseyTest { private static Configuration conf = new Configuration(); private static AppContext appContext; @@ -91,7 +91,7 @@ protected void configureServlets() { bind(AppContext.class).toInstance(appContext); bind(Configuration.class).toInstance(conf); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -109,28 +109,23 @@ public void setUp() throws Exception { } public TestAMWebServicesAttempts() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.mapreduce.v2.app.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testTaskAttempts() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid).path("attempts") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyAMTaskAttempts(json, task); } } @@ -138,19 +133,20 @@ public void testTaskAttempts() throws JSONException, Exception { @Test public void testTaskAttemptsSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid).path("attempts/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyAMTaskAttempts(json, task); } } @@ -158,19 +154,19 @@ public void testTaskAttemptsSlash() throws JSONException, Exception { @Test public void testTaskAttemptsDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid).path("attempts") - .get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyAMTaskAttempts(json, task); } } @@ -178,20 +174,20 @@ public void testTaskAttemptsDefault() throws JSONException, Exception { @Test public void testTaskAttemptsXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid).path("attempts") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -208,7 +204,7 @@ public void testTaskAttemptsXML() throws JSONException, Exception { @Test public void testTaskAttemptId() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { @@ -221,13 +217,13 @@ public void testTaskAttemptId() throws JSONException, Exception { TaskAttemptId attemptid = att.getID(); String attid = MRApps.toString(attemptid); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid) - .path("attempts").path(attid).accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " - + JettyUtils.UTF_8, response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").path(attid).request(MediaType.APPLICATION_JSON) + .get(Response.class); + assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("taskAttempt"); verifyAMTaskAttempt(info, att, task.getType()); @@ -238,7 +234,7 @@ public void testTaskAttemptId() throws JSONException, Exception { @Test public void testTaskAttemptIdSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { @@ -251,13 +247,13 @@ public void testTaskAttemptIdSlash() throws JSONException, Exception { TaskAttemptId attemptid = att.getID(); String attid = MRApps.toString(attemptid); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid) - .path("attempts").path(attid + "/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " - + JettyUtils.UTF_8, response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").path(attid + "/").request(MediaType.APPLICATION_JSON) + .get(Response.class); + assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("taskAttempt"); verifyAMTaskAttempt(info, att, task.getType()); @@ -268,7 +264,7 @@ public void testTaskAttemptIdSlash() throws JSONException, Exception { @Test public void testTaskAttemptIdDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { @@ -281,12 +277,12 @@ public void testTaskAttemptIdDefault() throws JSONException, Exception { TaskAttemptId attemptid = att.getID(); String attid = MRApps.toString(attemptid); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid) - .path("attempts").path(attid).get(ClientResponse.class); - assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " - + JettyUtils.UTF_8, response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").path(attid).request().get(Response.class); + assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("taskAttempt"); verifyAMTaskAttempt(info, att, task.getType()); @@ -297,7 +293,7 @@ public void testTaskAttemptIdDefault() throws JSONException, Exception { @Test public void testTaskAttemptIdXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); @@ -308,14 +304,14 @@ public void testTaskAttemptIdXML() throws JSONException, Exception { TaskAttemptId attemptid = att.getID(); String attid = MRApps.toString(attemptid); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").path(tid) - .path("attempts").path(attid).accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("tasks") + .path(tid).path("attempts").path(attid).request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -369,7 +365,7 @@ public void testTaskAttemptIdInvalid3() throws JSONException, Exception { private void testTaskAttemptIdErrorGeneric(String attid, String error) throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { @@ -381,14 +377,14 @@ private void testTaskAttemptIdErrorGeneric(String attid, String error) try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) .path("tasks").path(tid).path("attempts").path(attid) - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " - + JettyUtils.UTF_8, response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + + JettyUtils.UTF_8, response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -551,7 +547,7 @@ public void verifyReduceTaskAttemptGeneric(TaskAttempt ta, @Test public void testTaskAttemptIdCounters() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { @@ -564,13 +560,13 @@ public void testTaskAttemptIdCounters() throws JSONException, Exception { TaskAttemptId attemptid = att.getID(); String attid = MRApps.toString(attemptid); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid) .path("attempts").path(attid).path("counters") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " - + JettyUtils.UTF_8, response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + + JettyUtils.UTF_8, response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("jobTaskAttemptCounters"); verifyAMJobTaskAttemptCounters(info, att); @@ -581,7 +577,7 @@ public void testTaskAttemptIdCounters() throws JSONException, Exception { @Test public void testTaskAttemptIdXMLCounters() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); @@ -592,14 +588,14 @@ public void testTaskAttemptIdXMLCounters() throws JSONException, Exception { TaskAttemptId attemptid = att.getID(); String attid = MRApps.toString(attemptid); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid) .path("attempts").path(attid).path("counters") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesJobConf.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesJobConf.java index ee7bb0e3c27dd..69698546deaa9 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesJobConf.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesJobConf.java @@ -29,7 +29,9 @@ import java.io.StringReader; import java.util.Map; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -46,11 +48,14 @@ import org.apache.hadoop.mapreduce.v2.util.MRApps; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -61,10 +66,6 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Test the app master web service Rest API for getting the job conf. This @@ -72,7 +73,7 @@ * * /ws/v1/mapreduce/job/{jobid}/conf */ -public class TestAMWebServicesJobConf extends JerseyTestBase { +public class TestAMWebServicesJobConf extends JerseyTest { private static Configuration conf = new Configuration(); private static AppContext appContext; @@ -116,7 +117,7 @@ protected void configureServlets() { bind(AppContext.class).toInstance(appContext); bind(Configuration.class).toInstance(conf); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } }; @@ -140,26 +141,21 @@ static public void stop() { } public TestAMWebServicesJobConf() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.mapreduce.v2.app.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testJobConf() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("conf") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("conf") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("conf"); verifyAMJobConf(info, jobsMap.get(id)); @@ -168,17 +164,17 @@ public void testJobConf() throws JSONException, Exception { @Test public void testJobConfSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("conf/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("conf/") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("conf"); verifyAMJobConf(info, jobsMap.get(id)); @@ -187,16 +183,17 @@ public void testJobConfSlash() throws JSONException, Exception { @Test public void testJobConfDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("conf").get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("conf").request() + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("conf"); verifyAMJobConf(info, jobsMap.get(id)); @@ -205,17 +202,17 @@ public void testJobConfDefault() throws JSONException, Exception { @Test public void testJobConfXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("conf") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId).path("conf") + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesTasks.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesTasks.java index ab4d818f33868..0ac5fd26bb44f 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesTasks.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServicesTasks.java @@ -27,7 +27,11 @@ import java.io.StringReader; import java.util.Map; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -44,11 +48,14 @@ import org.apache.hadoop.mapreduce.v2.util.MRApps; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -58,12 +65,6 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Test the app master web service Rest API for getting tasks, a specific task, @@ -73,7 +74,7 @@ * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid} * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/counters */ -public class TestAMWebServicesTasks extends JerseyTestBase { +public class TestAMWebServicesTasks extends JerseyTest { private static Configuration conf = new Configuration(); private static AppContext appContext; @@ -89,7 +90,7 @@ protected void configureServlets() { bind(AppContext.class).toInstance(appContext); bind(Configuration.class).toInstance(conf); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -107,25 +108,20 @@ public void setUp() throws Exception { } public TestAMWebServicesTasks() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.mapreduce.v2.app.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testTasks() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); @@ -137,15 +133,15 @@ public void testTasks() throws JSONException, Exception { @Test public void testTasksDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") - .path("jobs").path(jobId).path("tasks").get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("mapreduce") + .path("jobs").path(jobId).path("tasks").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); @@ -157,16 +153,16 @@ public void testTasksDefault() throws JSONException, Exception { @Test public void testTasksSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); @@ -179,16 +175,16 @@ public void testTasksSlash() throws JSONException, Exception { @Test public void testTasksXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -203,17 +199,17 @@ public void testTasksXML() throws JSONException, Exception { @Test public void testTasksQueryMap() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String type = "m"; - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").queryParam("type", type) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); @@ -224,17 +220,17 @@ public void testTasksQueryMap() throws JSONException, Exception { @Test public void testTasksQueryReduce() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String type = "r"; - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").queryParam("type", type) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); @@ -245,7 +241,7 @@ public void testTasksQueryReduce() throws JSONException, Exception { @Test public void testTasksQueryInvalid() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); @@ -255,14 +251,14 @@ public void testTasksQueryInvalid() throws JSONException, Exception { try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) .path("tasks").queryParam("type", tasktype) - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -280,19 +276,19 @@ public void testTasksQueryInvalid() throws JSONException, Exception { @Test public void testTaskId() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("task"); verifyAMSingleTask(info, task); @@ -302,19 +298,19 @@ public void testTaskId() throws JSONException, Exception { @Test public void testTaskIdSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid + "/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("task"); verifyAMSingleTask(info, task); @@ -324,19 +320,19 @@ public void testTaskIdSlash() throws JSONException, Exception { @Test public void testTaskIdDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid) - .get(ClientResponse.class); + .request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("task"); verifyAMSingleTask(info, task); @@ -346,21 +342,21 @@ public void testTaskIdDefault() throws JSONException, Exception { @Test public void testTaskIdBogus() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "bogustaskid"; try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) - .path("tasks").path(tid).get(JSONObject.class); + .path("tasks").path(tid).request().get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -381,21 +377,21 @@ public void testTaskIdBogus() throws JSONException, Exception { @Test public void testTaskIdNonExist() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "task_0_0000_m_000000"; try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) - .path("tasks").path(tid).get(JSONObject.class); + .path("tasks").path(tid).request().get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -414,21 +410,21 @@ public void testTaskIdNonExist() throws JSONException, Exception { @Test public void testTaskIdInvalid() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "task_0_0000_d_000000"; try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) - .path("tasks").path(tid).get(JSONObject.class); + .path("tasks").path(tid).request().get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -449,21 +445,21 @@ public void testTaskIdInvalid() throws JSONException, Exception { @Test public void testTaskIdInvalid2() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "task_0_m_000000"; try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) - .path("tasks").path(tid).get(JSONObject.class); + .path("tasks").path(tid).request().get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -484,21 +480,21 @@ public void testTaskIdInvalid2() throws JSONException, Exception { @Test public void testTaskIdInvalid3() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "task_0_0000_m"; try { r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId) - .path("tasks").path(tid).get(JSONObject.class); + .path("tasks").path(tid).request().get(JSONObject.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -519,20 +515,20 @@ public void testTaskIdInvalid3() throws JSONException, Exception { @Test public void testTaskIdXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid) - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -634,19 +630,19 @@ public void verifyAMTaskXML(NodeList nodes, Job job) { @Test public void testTaskIdCounters() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid).path("counters") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("jobTaskCounters"); verifyAMJobTaskCounters(info, task); @@ -656,19 +652,19 @@ public void testTaskIdCounters() throws JSONException, Exception { @Test public void testTaskIdCountersSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid).path("counters/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("jobTaskCounters"); verifyAMJobTaskCounters(info, task); @@ -678,19 +674,19 @@ public void testTaskIdCountersSlash() throws JSONException, Exception { @Test public void testTaskIdCountersDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid).path("counters") - .get(ClientResponse.class); + .request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("jobTaskCounters"); verifyAMJobTaskCounters(info, task); @@ -700,19 +696,19 @@ public void testTaskIdCountersDefault() throws JSONException, Exception { @Test public void testJobTaskCountersXML() throws Exception { - WebResource r = resource(); + WebTarget r = target(); Map jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); - ClientResponse response = r.path("ws").path("v1").path("mapreduce") + Response response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").path(tid).path("counters") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/JAXBContextResolver.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/JAXBContextResolver.java index ad1c7a9fad2a0..0368f6a1dca57 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/JAXBContextResolver.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/JAXBContextResolver.java @@ -19,8 +19,7 @@ package org.apache.hadoop.mapreduce.v2.hs.webapp; import com.google.inject.Singleton; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; +import org.glassfish.jersey.jettison.JettisonJaxbContext; import java.util.Arrays; import java.util.HashSet; @@ -69,9 +68,8 @@ public class JAXBContextResolver implements ContextResolver { RemoteExceptionData.class }; public JAXBContextResolver() throws Exception { - this.types = new HashSet(Arrays.asList(cTypes)); - this.context = new JSONJAXBContext(JSONConfiguration.natural() - .rootUnwrapping(false).build(), cTypes); + this.types = new HashSet<>(Arrays.asList(cTypes)); + this.context = new JettisonJaxbContext(cTypes); } @Override diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/pom.xml b/hadoop-mapreduce-project/hadoop-mapreduce-client/pom.xml index b394fe5be18ef..487cb873e9d3a 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/pom.xml +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/pom.xml @@ -74,6 +74,10 @@ + + javax.ws.rs + javax.ws.rs-api + org.apache.hadoop hadoop-common @@ -157,11 +161,6 @@ commons-collections provided - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test - diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-examples/pom.xml b/hadoop-mapreduce-project/hadoop-mapreduce-examples/pom.xml index 11932e04e3784..15b21e542de07 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-examples/pom.xml +++ b/hadoop-mapreduce-project/hadoop-mapreduce-examples/pom.xml @@ -98,11 +98,6 @@ test-jar test - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test - org.apache.hadoop hadoop-mapreduce-client-hs diff --git a/hadoop-mapreduce-project/pom.xml b/hadoop-mapreduce-project/pom.xml index 3ce66a10a84f7..52db98456517a 100644 --- a/hadoop-mapreduce-project/pom.xml +++ b/hadoop-mapreduce-project/pom.xml @@ -118,14 +118,6 @@ com.google.inject guice - - com.sun.jersey - jersey-server - - - com.sun.jersey.contribs - jersey-guice - com.google.inject.extensions guice-servlet diff --git a/hadoop-project/pom.xml b/hadoop-project/pom.xml index 2caad449b5cbf..a2d478581b92f 100644 --- a/hadoop-project/pom.xml +++ b/hadoop-project/pom.xml @@ -66,11 +66,16 @@ 1.9.2 - 1.19.4 + 2.36 + + 2.4.4 - 2.12.7 - 2.12.7 + 2.13.2 + 2.13.2.2 + + + 2.1.1 4.5.13 @@ -137,7 +142,6 @@ 1.4.10 2.0.6.1 5.2.0 - 2.2.21 2.9.0 3.2.4 3.10.6.Final @@ -795,8 +799,8 @@ javax.ws.rs - jsr311-api - 1.1.1 + javax.ws.rs-api + ${javax.ws.rs-api.version} org.eclipse.jetty @@ -882,44 +886,99 @@ 43.0 - com.sun.jersey - jersey-core - ${jersey.version} - - - org.osgi - org.osgi.core - - + org.glassfish.jersey.core + jersey-common + ${jersey2.version} + + + org.glassfish.jersey.core + jersey-server + ${jersey2.version} - com.sun.jersey - jersey-servlet - ${jersey.version} + org.glassfish.jersey.core + jersey-client + ${jersey2.version} - com.github.pjfanning - jersey-json - 1.20 + org.glassfish.jersey.containers + jersey-container-servlet-core + ${jersey2.version} + + + org.glassfish.jersey.inject + jersey-hk2 + ${jersey2.version} + + + org.glassfish.jersey.media + jersey-media-json-jackson + ${jersey2.version} + + + org.glassfish.jersey.media + jersey-media-json-jettison + ${jersey2.version} + + + org.glassfish.jersey.test-framework + jersey-test-framework-core + ${jersey2.version} - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind + com.google.inject + guice + + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 + ${jersey2.version} + - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider + com.google.inject + guice - com.sun.jersey - jersey-server - ${jersey.version} + org.glassfish.hk2 + guice-bridge + 2.6.1 + + + jakarta.annotation + jakarta.annotation-api + 1.3.5 + + + net.jodah + failsafe + 2.4.4 + + + org.javassist + javassist + 3.29.0-GA + + + org.glassfish.grizzly + grizzly-http-servlet + ${grizzly.version} + test + + + org.glassfish.grizzly + grizzly-http + ${grizzly.version} + test + + + org.glassfish.grizzly + grizzly-http-server + ${grizzly.version} + test @@ -940,37 +999,6 @@ ${guice.version} - - com.sun.jersey.contribs - jersey-guice - ${jersey.version} - - - - com.sun.jersey.jersey-test-framework - jersey-test-framework-core - ${jersey.version} - test - - - javax.servlet - javax.servlet-api - - - - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - ${jersey.version} - - - javax.servlet - javax.servlet-api - - - - - io.netty netty @@ -1506,27 +1534,6 @@ - - com.sun.jersey - jersey-client - ${jersey.version} - - - - org.glassfish.grizzly - grizzly-http-servlet - ${grizzly.version} - - - org.glassfish.grizzly - grizzly-http - ${grizzly.version} - - - org.glassfish.grizzly - grizzly-http-server - ${grizzly.version} - ${leveldbjni.group} @@ -1868,6 +1875,10 @@ org.osgi org.osgi.core + + javax.ws.rs + jsr311-api + diff --git a/hadoop-tools/hadoop-azure/pom.xml b/hadoop-tools/hadoop-azure/pom.xml index 40aeec07026cc..ebb38237e4084 100644 --- a/hadoop-tools/hadoop-azure/pom.xml +++ b/hadoop-tools/hadoop-azure/pom.xml @@ -301,7 +301,7 @@ javax.ws.rs - jsr311-api + javax.ws.rs-api test diff --git a/hadoop-tools/hadoop-resourceestimator/pom.xml b/hadoop-tools/hadoop-resourceestimator/pom.xml index 89e248f9a7efe..c46d7f22f6b56 100644 --- a/hadoop-tools/hadoop-resourceestimator/pom.xml +++ b/hadoop-tools/hadoop-resourceestimator/pom.xml @@ -65,37 +65,10 @@ javax.inject 1 - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test - javax.servlet javax.servlet-api - - com.sun.jersey - jersey-server - - - com.github.pjfanning - jersey-json - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - junit junit diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml index 61747c2cd8028..6e7c27a23ea85 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml @@ -115,6 +115,10 @@ com.fasterxml.jackson.core jackson-annotations + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-catalog/hadoop-yarn-applications-catalog-webapp/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-catalog/hadoop-yarn-applications-catalog-webapp/pom.xml index 21b3172a59f22..e49083f43fcd0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-catalog/hadoop-yarn-applications-catalog-webapp/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-catalog/hadoop-yarn-applications-catalog-webapp/pom.xml @@ -77,7 +77,6 @@ org.glassfish.jersey.media jersey-media-json-jackson - 2.12 test @@ -91,25 +90,6 @@ - - com.github.pjfanning - jersey-json - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - - org.apache.solr solr-solrj @@ -261,6 +241,11 @@ jackson-jaxrs-base ${jackson2.version} + + + javax.ws.rs + javax.ws.rs-api + ${artifact.name} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/pom.xml index d1cd362c7d65d..7e468bc75684e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/pom.xml @@ -166,6 +166,10 @@ + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/test/java/org/apache/hadoop/yarn/applications/distributedshell/TestDSTimelineV10.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/test/java/org/apache/hadoop/yarn/applications/distributedshell/TestDSTimelineV10.java index 15dc1cb04ee4a..cd5db0eab9f6e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/test/java/org/apache/hadoop/yarn/applications/distributedshell/TestDSTimelineV10.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/test/java/org/apache/hadoop/yarn/applications/distributedshell/TestDSTimelineV10.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import net.jodah.failsafe.RetryPolicy; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -521,10 +522,10 @@ public void testDSTimelineClientWithConnectionRefuse() throws Exception { TimelineClientImpl client = new TimelineClientImpl() { @Override protected TimelineWriter createTimelineWriter(Configuration conf, - UserGroupInformation authUgi, com.sun.jersey.api.client.Client client, - URI resURI) throws IOException { + UserGroupInformation authUgi, javax.ws.rs.client.Client client, + URI resURI, RetryPolicy retryPolicy) throws IOException { TimelineWriter timelineWriter = - new DirectTimelineWriter(authUgi, client, resURI); + new DirectTimelineWriter(authUgi, client, resURI, retryPolicy); spyTimelineWriterRef.set(spy(timelineWriter)); return spyTimelineWriterRef.get(); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-api/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-api/pom.xml index 2da2cdd42a82a..5e5cf5f6186b6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-api/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-api/pom.xml @@ -131,10 +131,6 @@ com.google.inject guice - - javax.ws.rs - jsr311-api - javax.servlet javax.servlet-api @@ -155,10 +151,6 @@ org.apache.hadoop.thirdparty hadoop-shaded-guava - - com.sun.jersey - jersey-client - org.eclipse.jetty jetty-server diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/pom.xml index 746abf385e6bd..4daac9fa235ae 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/pom.xml @@ -137,6 +137,11 @@ jackson-annotations + + javax.ws.rs + javax.ws.rs-api + + org.apache.hadoop hadoop-hdfs-client diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/pom.xml index dbe4e9048b2df..1db234e30ea9e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/pom.xml @@ -85,14 +85,8 @@ test - com.sun.jersey.jersey-test-framework - jersey-test-framework-core - test - - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test + org.codehaus.jettison + jettison diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java index 8c09063d137b1..5813d3ec92470 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java @@ -20,21 +20,10 @@ import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientRequest; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.WebResource.Builder; -import com.sun.jersey.api.client.filter.ClientFilter; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; -import java.net.ConnectException; -import java.net.SocketException; -import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -48,7 +37,12 @@ import java.util.Set; import java.util.TreeMap; import java.util.regex.Pattern; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; @@ -131,10 +125,6 @@ public class LogsCLI extends Configured implements Tool { private long logSizeLeft = LOG_SIZE_LIMIT_DEFAULT * 1024 * 1024; private long specifedLogLimits = LOG_SIZE_LIMIT_DEFAULT; - @Private - @VisibleForTesting - ClientConnectionRetry connectionRetry; - @Override public int run(String[] args) throws Exception { try { @@ -145,7 +135,7 @@ public int run(String[] args) throws Exception { yarnClient.close(); } if (webServiceClient != null) { - webServiceClient.destroy(); + webServiceClient.close(); } } } @@ -324,10 +314,10 @@ private int runCommand(String[] args) throws Exception { } } - // Set up Retry WebService Client - connectionRetry = new ClientConnectionRetry(maxRetries, retryInterval); - ClientJerseyRetryFilter retryFilter = new ClientJerseyRetryFilter(); - webServiceClient.addFilter(retryFilter); + // TODO : Set up Retry WebService Client + // Jersey 2 supports ClientRequestFilter and ClientResponseFilter. The implementations + // no longer supports getNext().handle(ClientRequest) way of calling next client handler + // to invoke in the chain of filters. LogCLIHelpers logCliHelper = new LogCLIHelpers(); logCliHelper.setConf(getConf()); @@ -450,44 +440,38 @@ protected List getAMContainerInfoForRMWebService( } private List getAMContainerInfoFromRM( - String webAppAddress, String appId) throws ClientHandlerException, - UniformInterfaceException, JSONException { + String webAppAddress, String appId) throws ProcessingException, + IllegalStateException, JSONException { List amContainersList = new ArrayList(); - ClientResponse response = null; - try { - Builder builder = webServiceClient.resource(webAppAddress) - .path("ws").path("v1").path("cluster") - .path("apps").path(appId).path("appattempts") - .accept(MediaType.APPLICATION_JSON); - response = builder.get(ClientResponse.class); - JSONObject json = response.getEntity(JSONObject.class) + final WebTarget target = webServiceClient.target(webAppAddress) + .path("ws").path("v1").path("cluster") + .path("apps").path(appId).path("appattempts"); + try (Response response = target.request(MediaType.APPLICATION_JSON) + .get(Response.class)) { + JSONObject json = response.readEntity(JSONObject.class) .getJSONObject("appAttempts"); JSONArray requests = json.getJSONArray("appAttempt"); for (int j = 0; j < requests.length(); j++) { amContainersList.add(requests.getJSONObject(j)); } return amContainersList; - } finally { - if (response != null) { - response.close(); - } } } private List getAMContainerInfoForAHSWebService( - Configuration conf, String appId) throws ClientHandlerException, - UniformInterfaceException, JSONException { + Configuration conf, String appId) throws ProcessingException, + IllegalStateException, JSONException { String webAppAddress = WebAppUtils.getHttpSchemePrefix(conf) + WebAppUtils.getAHSWebAppURLWithoutScheme(conf); - WebResource webResource = webServiceClient.resource(webAppAddress); + final WebTarget target = webServiceClient.target(webAppAddress); - ClientResponse response = - webResource.path("ws").path("v1").path("applicationhistory") + Response response = + target.path("ws").path("v1").path("applicationhistory") .path("apps").path(appId).path("appattempts") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); - JSONObject json = response.getEntity(JSONObject.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); + JSONObject json = response.readEntity(JSONObject.class); JSONArray requests = json.getJSONArray("appAttempt"); List amContainersList = new ArrayList(); for (int i = 0; i < requests.length(); i++) { @@ -530,18 +514,18 @@ private List> getContainerLogFiles( List> logFileInfos = new ArrayList<>(); try { - WebResource webResource = webServiceClient - .resource(WebAppUtils.getHttpSchemePrefix(conf) + nodeHttpAddress); - ClientResponse response = - webResource.path("ws").path("v1").path("node").path("containers") + WebTarget target = webServiceClient + .target(WebAppUtils.getHttpSchemePrefix(conf) + nodeHttpAddress); + Response response = + target.path("ws").path("v1").path("node").path("containers") .path(containerIdStr).path("logs") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (response.getStatusInfo().getStatusCode() == - ClientResponse.Status.OK.getStatusCode()) { + Response.Status.OK.getStatusCode()) { try { JSONArray array = new JSONArray(); - JSONObject json = response.getEntity(JSONObject.class); + JSONObject json = response.readEntity(JSONObject.class); if (!json.has("containerLogsInfo")) { return logFileInfos; } @@ -582,7 +566,7 @@ private List> getContainerLogFiles( } } - } catch (ClientHandlerException | UniformInterfaceException ex) { + } catch (ProcessingException | IllegalStateException ex) { System.err.println("Unable to fetch log files list"); throw new IOException(ex); } @@ -617,11 +601,12 @@ public int printContainerLogsFromRunningApplication(Configuration conf, for (String logFile : request.getLogTypes()) { InputStream is = null; try { - ClientResponse response = getResponseFromNMWebService(conf, + Response response = getResponseFromNMWebService(conf, webServiceClient, request, logFile); if (response != null && response.getStatusInfo().getStatusCode() == - ClientResponse.Status.OK.getStatusCode()) { - is = response.getEntityInputStream(); + Response.Status.OK.getStatusCode()) { + + is = response.readEntity(InputStream.class); int len = 0; while((len = is.read(buffer)) != -1) { out.write(buffer, 0, len); @@ -633,12 +618,12 @@ public int printContainerLogsFromRunningApplication(Configuration conf, " WebService is " + ((response == null) ? "null": "not successful," + " HTTP error code: " + response.getStatus() + ", Server response:\n" + - response.getEntity(String.class)); + response.readEntity(String.class)); out.println(msg); } out.flush(); foundAnyLogs = true; - } catch (ClientHandlerException | UniformInterfaceException ex) { + } catch (ProcessingException | IllegalStateException ex) { System.err.println("Can not find the log file:" + logFile + " for the container:" + containerIdStr + " in NodeManager:" + nodeId); @@ -811,11 +796,11 @@ private boolean createContainerLogsRequestForMasterContainer( private List getAMContainerInfoFromTimelineReader( Configuration conf, String appId) - throws IOException, ClientHandlerException, UniformInterfaceException, + throws IOException, ProcessingException, IllegalStateException, JSONException { - ClientResponse response = getClientResponseFromTimelineReader(conf, appId); + final Response response = getClientResponseFromTimelineReader(conf, appId); - JSONArray appAttemptEntities = response.getEntity(JSONArray.class); + JSONArray appAttemptEntities = response.readEntity(JSONArray.class); List amContainersList = new ArrayList(); for (int i = 0; i < appAttemptEntities.length(); i++) { JSONObject appAttemptEntity = appAttemptEntities.getJSONObject(i); @@ -826,28 +811,28 @@ private List getAMContainerInfoFromTimelineReader( return amContainersList; } - protected ClientResponse getClientResponseFromTimelineReader( + protected Response getClientResponseFromTimelineReader( Configuration conf, String appId) throws IOException { String webAppAddress = WebAppUtils.getHttpSchemePrefix(conf) + WebAppUtils .getTimelineReaderWebAppURLWithoutScheme(conf); - WebResource webResource = webServiceClient.resource(webAppAddress); + final WebTarget target = webServiceClient.target(webAppAddress); - ClientResponse response = - webResource.path("ws").path("v2").path("timeline").path("clusters") + final Response response = + target.path("ws").path("v2").path("timeline").path("clusters") .path(conf.get(YarnConfiguration.RM_CLUSTER_ID)).path("apps") .path(appId).path("entities") .path(TimelineEntityType.YARN_APPLICATION_ATTEMPT.toString()) - .queryParam("fields", "INFO").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .queryParam("fields", "INFO").request(MediaType.APPLICATION_JSON) + .get(Response.class); if (response == null - || response.getStatusInfo().getStatusCode() != ClientResponse.Status.OK + || response.getStatusInfo().getStatusCode() != Response.Status.OK .getStatusCode()) { String msg = "Response from the timeline reader server is " + ((response == null) ? "null" : "not successful," + " HTTP error code: " + response.getStatus() - + ", Server response:\n" + response.getEntity(String.class)); + + ", Server response:\n" + response.readEntity(String.class)); System.out.println(msg); throw new IOException(msg); } @@ -884,7 +869,7 @@ private void outputAMContainerLogs(ContainerLogsRequest request, private int showContainerLogInfo(ContainerLogsRequest request, LogCLIHelpers logCliHelper) throws IOException, YarnException, - ClientHandlerException, UniformInterfaceException, JSONException { + ProcessingException, IllegalStateException, JSONException { if (!request.isAppFinished()) { return printContainerInfoFromRunningApplication(request, logCliHelper); } else { @@ -1105,7 +1090,7 @@ private int fetchAMContainerLogs(ContainerLogsRequest request, private int fetchContainerLogs(ContainerLogsRequest request, LogCLIHelpers logCliHelper, boolean useRegex, boolean ignoreSizeLimit) - throws IOException, ClientHandlerException, UniformInterfaceException, + throws IOException, ProcessingException, IllegalStateException, JSONException { String appIdStr = request.getAppId().toString(); String containerIdStr = request.getContainerId(); @@ -1400,8 +1385,8 @@ private List filterContainersInfo( private int printContainerInfoFromRunningApplication( ContainerLogsRequest options, LogCLIHelpers logCliHelper) - throws YarnException, IOException, ClientHandlerException, - UniformInterfaceException, JSONException { + throws YarnException, IOException, ProcessingException, + IllegalStateException, JSONException { String containerIdStr = options.getContainerId(); String nodeIdStr = options.getNodeId(); List reports = @@ -1485,7 +1470,7 @@ public Set getMatchedContainerLogFiles(ContainerLogsRequest request, } @VisibleForTesting - public ClientResponse getResponseFromNMWebService(Configuration conf, + public Response getResponseFromNMWebService(Configuration conf, Client webServiceClient, ContainerLogsRequest request, String logFile) { return LogToolUtils.getResponseFromNMWebService( conf, webServiceClient, request, logFile); @@ -1493,7 +1478,7 @@ public ClientResponse getResponseFromNMWebService(Configuration conf, @VisibleForTesting public String getNodeHttpAddressFromRMWebString(ContainerLogsRequest request) - throws ClientHandlerException, UniformInterfaceException, JSONException { + throws ProcessingException, IllegalStateException, JSONException { if (request.getNodeId() == null || request.getNodeId().isEmpty()) { return null; } @@ -1504,113 +1489,6 @@ public String getNodeHttpAddressFromRMWebString(ContainerLogsRequest request) nodeInfo.getString("nodeHTTPAddress") : null; } - // Class to handle retry - static class ClientConnectionRetry { - - // maxRetries < 0 means keep trying - @Private - @VisibleForTesting - public int maxRetries; - - @Private - @VisibleForTesting - public long retryInterval; - - // Indicates if retries happened last time. Only tests should read it. - // In unit tests, retryOn() calls should _not_ be concurrent. - private boolean retried = false; - - @Private - @VisibleForTesting - boolean getRetired() { - return retried; - } - - // Constructor with default retry settings - public ClientConnectionRetry(int inputMaxRetries, - long inputRetryInterval) { - this.maxRetries = inputMaxRetries; - this.retryInterval = inputRetryInterval; - } - - public Object retryOn(ClientRetryOp op) - throws RuntimeException, IOException { - int leftRetries = maxRetries; - retried = false; - - // keep trying - while (true) { - try { - // try perform the op, if fail, keep retrying - return op.run(); - } catch (IOException | RuntimeException e) { - // break if there's no retries left - if (leftRetries == 0) { - break; - } - if (op.shouldRetryOn(e)) { - logException(e, leftRetries); - } else { - throw e; - } - } - if (leftRetries > 0) { - leftRetries--; - } - retried = true; - try { - // sleep for the given time interval - Thread.sleep(retryInterval); - } catch (InterruptedException ie) { - System.out.println("Client retry sleep interrupted! "); - } - } - throw new RuntimeException("Connection retries limit exceeded."); - }; - - private void logException(Exception e, int leftRetries) { - if (leftRetries > 0) { - System.out.println("Exception caught by ClientConnectionRetry," - + " will try " + leftRetries + " more time(s).\nMessage: " - + e.getMessage()); - } else { - // note that maxRetries may be -1 at the very beginning - System.out.println("ConnectionException caught by ClientConnectionRetry," - + " will keep retrying.\nMessage: " - + e.getMessage()); - } - } - } - - private class ClientJerseyRetryFilter extends ClientFilter { - @Override - public ClientResponse handle(final ClientRequest cr) - throws ClientHandlerException { - // Set up the retry operation - ClientRetryOp jerseyRetryOp = new ClientRetryOp() { - @Override - public Object run() { - // Try pass the request, if fail, keep retrying - return getNext().handle(cr); - } - - @Override - public boolean shouldRetryOn(Exception e) { - // Only retry on connection exceptions - return (e instanceof ClientHandlerException) - && (e.getCause() instanceof ConnectException || - e.getCause() instanceof SocketTimeoutException || - e.getCause() instanceof SocketException); - } - }; - try { - return (ClientResponse) connectionRetry.retryOn(jerseyRetryOp); - } catch (IOException e) { - throw new ClientHandlerException("Jersey retry failed!\nMessage: " - + e.getMessage()); - } - } - } // Abstract class for an operation that should be retried by client private static abstract class ClientRetryOp { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/SchedConfCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/SchedConfCLI.java index a9f1c542ab217..6ef2368938a21 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/SchedConfCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/SchedConfCLI.java @@ -19,12 +19,6 @@ package org.apache.hadoop.yarn.client.cli; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.WebResource.Builder; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.MissingArgumentException; @@ -44,6 +38,11 @@ import org.apache.hadoop.yarn.webapp.util.WebAppUtils; import org.apache.hadoop.yarn.webapp.util.YarnWebServiceUtils; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.transform.OutputKeys; @@ -52,19 +51,23 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import javax.ws.rs.client.Client; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response.Status; import java.io.StringReader; import java.io.StringWriter; import java.io.IOException; import java.net.HttpURLConnection; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.ClientProperties; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; + /** * CLI for modifying scheduler configuration. */ @@ -198,18 +201,18 @@ private static void prettyFormatWithIndent(String input, int indent) System.out.println(xmlOutput.getWriter().toString()); } - private WebResource initializeWebResource(String webAppAddress) { + private WebTarget initializeWebResource(String webAppAddress) { Configuration conf = getConf(); if (YarnConfiguration.useHttps(conf)) { sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf); } client = createWebServiceClient(sslFactory); - return client.resource(webAppAddress); + return client.target(webAppAddress); } private void destroyClient() { if (client != null) { - client.destroy(); + client.close(); } if (sslFactory != null) { sslFactory.destroy(); @@ -217,27 +220,25 @@ private void destroyClient() { } @VisibleForTesting - int getSchedulerConf(String webAppAddress, WebResource resource) + int getSchedulerConf(String webAppAddress, WebTarget resource) throws Exception { - ClientResponse response = null; + Response response = null; resource = (resource != null) ? resource : initializeWebResource(webAppAddress); try { - Builder builder; + Invocation.Builder builder; if (UserGroupInformation.isSecurityEnabled()) { - builder = resource - .path("ws").path("v1").path("cluster") - .path("scheduler-conf").accept(MediaType.APPLICATION_XML); + builder = resource.path("ws").path("v1").path("cluster").path("scheduler-conf") + .request(MediaType.APPLICATION_XML); } else { - builder = resource - .path("ws").path("v1").path("cluster").path("scheduler-conf") - .queryParam("user.name", UserGroupInformation.getCurrentUser() - .getShortUserName()).accept(MediaType.APPLICATION_XML); + builder = resource.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", UserGroupInformation.getCurrentUser().getShortUserName()) + .request(MediaType.APPLICATION_XML); } - response = builder.get(ClientResponse.class); + response = builder.get(Response.class); if (response != null) { if (response.getStatus() == Status.OK.getStatusCode()) { - ConfInfo schedulerConf = response.getEntity(ConfInfo.class); + ConfInfo schedulerConf = response.readEntity(ConfInfo.class); JAXBContext jaxbContext = JAXBContext.newInstance(ConfInfo.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); StringWriter sw = new StringWriter(); @@ -246,7 +247,7 @@ int getSchedulerConf(String webAppAddress, WebResource resource) return 0; } else { System.err.println("Failed to get scheduler configuration: " - + response.getEntity(String.class)); + + response.readEntity(String.class)); } } else { System.err.println("Failed to get scheduler configuration: " + @@ -262,34 +263,30 @@ int getSchedulerConf(String webAppAddress, WebResource resource) } @VisibleForTesting - int formatSchedulerConf(String webAppAddress, WebResource resource) + int formatSchedulerConf(String webAppAddress, WebTarget resource) throws Exception { - ClientResponse response = null; + Response response = null; resource = (resource != null) ? resource : initializeWebResource(webAppAddress); try { - Builder builder; + Invocation.Builder builder; if (UserGroupInformation.isSecurityEnabled()) { - builder = resource - .path("ws").path("v1").path("cluster") - .path("/scheduler-conf/format") - .accept(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON); + builder = resource.path("ws").path("v1").path("cluster").path("/scheduler-conf/format") + .request(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON); } else { - builder = resource - .path("ws").path("v1").path("cluster") - .path("/scheduler-conf/format").queryParam("user.name", - UserGroupInformation.getCurrentUser().getShortUserName()) - .accept(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON); + builder = resource.path("ws").path("v1").path("cluster").path("/scheduler-conf/format") + .queryParam("user.name", UserGroupInformation.getCurrentUser().getShortUserName()) + .request(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON); } - response = builder.get(ClientResponse.class); + response = builder.get(Response.class); if (response != null) { if (response.getStatus() == Status.OK.getStatusCode()) { - System.out.println(response.getEntity(String.class)); + System.out.println(response.readEntity(String.class)); return 0; } else { System.err.println("Failed to format scheduler configuration: " + - response.getEntity(String.class)); + response.readEntity(String.class)); } } else { System.err.println("Failed to format scheduler configuration: " + @@ -307,30 +304,29 @@ int formatSchedulerConf(String webAppAddress, WebResource resource) @VisibleForTesting int updateSchedulerConfOnRMNode(String webAppAddress, SchedConfUpdateInfo updateInfo) throws Exception { - ClientResponse response = null; - WebResource resource = initializeWebResource(webAppAddress); + Response response = null; + WebTarget resource = initializeWebResource(webAppAddress); try { - Builder builder = null; + Invocation.Builder builder = null; if (UserGroupInformation.isSecurityEnabled()) { - builder = resource.path("ws").path("v1").path("cluster") - .path("scheduler-conf").accept(MediaType.APPLICATION_JSON); + builder = resource.path("ws").path("v1").path("cluster").path("scheduler-conf") + .request(MediaType.APPLICATION_JSON); } else { builder = resource.path("ws").path("v1").path("cluster") - .queryParam("user.name", - UserGroupInformation.getCurrentUser().getShortUserName()) - .path("scheduler-conf").accept(MediaType.APPLICATION_JSON); + .queryParam("user.name", UserGroupInformation.getCurrentUser().getShortUserName()) + .path("scheduler-conf").request(MediaType.APPLICATION_JSON); } - builder.entity(YarnWebServiceUtils.toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON); - response = builder.put(ClientResponse.class); + response = builder.put( + Entity.entity(YarnWebServiceUtils.toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); if (response != null) { if (response.getStatus() == Status.OK.getStatusCode()) { System.out.println("Configuration changed successfully."); return 0; } else { System.err.println("Configuration change unsuccessful: " - + response.getEntity(String.class)); + + response.readEntity(String.class)); } } else { System.err.println("Configuration change unsuccessful: null response"); @@ -345,30 +341,26 @@ int updateSchedulerConfOnRMNode(String webAppAddress, } private Client createWebServiceClient(SSLFactory clientSslFactory) { - Client webServiceClient = new Client(new URLConnectionClientHandler( - new HttpURLConnectionFactory() { - @Override - public HttpURLConnection getHttpURLConnection(URL url) - throws IOException { - AuthenticatedURL.Token token = new AuthenticatedURL.Token(); - AuthenticatedURL aUrl; - HttpURLConnection conn = null; - try { - if (clientSslFactory != null) { - clientSslFactory.init(); - aUrl = new AuthenticatedURL(null, clientSslFactory); - } else { - aUrl = new AuthenticatedURL(); - } - conn = aUrl.openConnection(url, token); - } catch (Exception e) { - throw new IOException(e); - } - return conn; + ClientConfig cfg = new ClientConfig(); + cfg.connectorProvider(new HttpUrlConnectorProvider().connectionFactory(url -> { + AuthenticatedURL.Token token = new AuthenticatedURL.Token(); + AuthenticatedURL aUrl; + HttpURLConnection conn = null; + try { + if (clientSslFactory != null) { + clientSslFactory.init(); + aUrl = new AuthenticatedURL(null, clientSslFactory); + } else { + aUrl = new AuthenticatedURL(); } - })); - webServiceClient.setChunkedEncodingSize(null); - return webServiceClient; + conn = aUrl.openConnection(url, token); + } catch (Exception e) { + throw new IOException(e); + } + return conn; + })); + cfg.property(ClientProperties.CHUNKED_ENCODING_SIZE, null); + return ClientBuilder.newClient(cfg); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java index 6ec8549be9d98..f4a69fbc77aa3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java @@ -38,15 +38,13 @@ import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableList; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; @@ -60,6 +58,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; + import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; @@ -206,8 +207,8 @@ public void testAMContainerInfoFetchFromTimelineReader() throws Exception { + "esto\":{}}]"; JSONArray obj = new JSONArray(appInfoEntity); - ClientResponse response = mock(ClientResponse.class); - doReturn(obj).when(response).getEntity(JSONArray.class); + Response response = mock(Response.class); + doReturn(obj).when(response).readEntity(JSONArray.class); doReturn(response).when(cli) .getClientResponseFromTimelineReader(any(Configuration.class), @@ -820,56 +821,6 @@ public ContainerReport getContainerReport(String containerIdStr) fs.delete(new Path(rootLogDir), true); } - @Test - public void testCheckRetryCount() throws Exception { - UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); - - NodeId nodeId = NodeId.newInstance("localhost", 1234); - ApplicationId appId = ApplicationId.newInstance(0, 1); - ApplicationAttemptId appAttemptId = ApplicationAttemptId - .newInstance(appId, 1); - - // Create a mock ApplicationAttempt Report - ApplicationAttemptReport mockAttemptReport = mock( - ApplicationAttemptReport.class); - doReturn(appAttemptId).when(mockAttemptReport).getApplicationAttemptId(); - List attemptReports = Arrays.asList( - mockAttemptReport); - - // Create one mock containerReport - ContainerId containerId1 = ContainerId.newContainerId(appAttemptId, 1); - ContainerReport mockContainerReport1 = mock(ContainerReport.class); - doReturn(containerId1).when(mockContainerReport1).getContainerId(); - doReturn(nodeId).when(mockContainerReport1).getAssignedNode(); - doReturn("http://localhost:2345").when(mockContainerReport1) - .getNodeHttpAddress(); - doReturn(ContainerState.RUNNING).when(mockContainerReport1) - .getContainerState(); - List containerReports = Arrays.asList( - mockContainerReport1); - // Mock the YarnClient, and it would report the previous created - // mockAttemptReport and previous two created mockContainerReports - YarnClient mockYarnClient = createMockYarnClient( - YarnApplicationState.RUNNING, ugi.getShortUserName(), true, - attemptReports, containerReports); - doReturn(mockContainerReport1).when(mockYarnClient).getContainerReport( - any(ContainerId.class)); - LogsCLI cli = new LogsCLIForTest(mockYarnClient); - cli.setConf(new YarnConfiguration()); - try { - cli.run(new String[] {"-containerId", - containerId1.toString(), "-client_max_retries", "5"}); - Assert.fail("Exception expected! " - + "NodeManager should be off to run this test. "); - } catch (RuntimeException ce) { - Assert.assertTrue( - "Handler exception for reason other than retry: " + ce.getMessage(), - ce.getMessage().contains("Connection retries limit exceeded")); - Assert.assertTrue("Retry filter didn't perform any retries! ", cli - .connectionRetry.getRetired()); - } - } - @Test (timeout = 5000) public void testGetRunningContainerLogs() throws Exception { UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); @@ -939,9 +890,9 @@ public void testGetRunningContainerLogs() throws Exception { logsSet.add(fileName); doReturn(logsSet).when(cli).getMatchedContainerLogFiles( any(ContainerLogsRequest.class), anyBoolean(), anyBoolean()); - ClientResponse mockReponse = mock(ClientResponse.class); - doReturn(Status.OK).when(mockReponse).getStatusInfo(); - doReturn(fis).when(mockReponse).getEntityInputStream(); + Response mockReponse = mock(Response.class); + doReturn(Response.Status.OK).when(mockReponse).getStatusInfo(); + doReturn(fis).when(mockReponse).readEntity(InputStream.class); doReturn(mockReponse).when(cli).getResponseFromNMWebService( any(Configuration.class), any(Client.class), diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java index 4df20da68ccca..2058c8e069ad0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestSchedConfCLI.java @@ -18,6 +18,9 @@ package org.apache.hadoop.yarn.client.cli; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; @@ -34,8 +37,6 @@ import com.google.inject.Guice; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.authentication.server.AuthenticationFilter; @@ -52,7 +53,6 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.RMWebServices; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.dao.QueueConfigInfo; import org.apache.hadoop.yarn.webapp.dao.SchedConfUpdateInfo; @@ -73,7 +73,7 @@ /** * Class for testing {@link SchedConfCLI}. */ -public class TestSchedConfCLI extends JerseyTestBase { +public class TestSchedConfCLI extends JerseyTest { private SchedConfCLI cli; @@ -86,11 +86,6 @@ public class TestSchedConfCLI extends JerseyTestBase { "test-classes"), YarnConfiguration.CS_CONFIGURATION_FILE + ".tmp"); public TestSchedConfCLI() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Before @@ -136,7 +131,7 @@ protected void configureServlets() { rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); filter("/*").through(TestRMCustomAuthFilter.class); } } @@ -213,7 +208,7 @@ public void testGetSchedulerConf() throws Exception { super.setUp(); GuiceServletConfig.setInjector( Guice.createInjector(new WebServletModule())); - int exitCode = cli.getSchedulerConf("", resource()); + int exitCode = cli.getSchedulerConf("", target()); assertEquals("SchedConfCLI failed to run", 0, exitCode); assertTrue("Failed to get scheduler configuration", sysOutStream.toString().contains("testqueue")); @@ -245,7 +240,7 @@ public void testFormatSchedulerConf() throws Exception { Configuration schedulerConf = provider.getConfiguration(); assertEquals("schedVal1", schedulerConf.get("schedKey1")); - int exitCode = cli.formatSchedulerConf("", resource()); + int exitCode = cli.formatSchedulerConf("", target()); assertEquals(0, exitCode); schedulerConf = provider.getConfiguration(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/pom.xml index a04ff52be9749..6851301577f01 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/pom.xml @@ -77,17 +77,29 @@ jetty-util - com.sun.jersey - jersey-core + org.glassfish.jersey.core + jersey-common - com.sun.jersey + org.glassfish.jersey.core jersey-client org.apache.hadoop.thirdparty hadoop-shaded-guava + + org.glassfish.jersey.media + jersey-media-json-jettison + + + org.glassfish.hk2 + guice-bridge + + + net.jodah + failsafe + commons-cli commons-cli @@ -138,11 +150,6 @@ bcprov-jdk15on test - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test - commons-io commons-io @@ -151,32 +158,6 @@ com.google.inject guice - - com.sun.jersey - jersey-server - - - com.github.pjfanning - jersey-json - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - - - com.sun.jersey.contribs - jersey-guice - log4j log4j @@ -197,6 +178,10 @@ com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/DirectTimelineWriter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/DirectTimelineWriter.java index 7fea1dbcdbea5..57587c12668c6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/DirectTimelineWriter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/DirectTimelineWriter.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.net.URI; +import net.jodah.failsafe.RetryPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.classification.InterfaceAudience.Private; @@ -33,7 +34,7 @@ import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse; import org.apache.hadoop.yarn.exceptions.YarnException; -import com.sun.jersey.api.client.Client; +import javax.ws.rs.client.Client; /** * A simple writer class for storing Timeline data into Leveldb store. @@ -46,8 +47,8 @@ public class DirectTimelineWriter extends TimelineWriter{ .getLogger(DirectTimelineWriter.class); public DirectTimelineWriter(UserGroupInformation authUgi, - Client client, URI resURI) { - super(authUgi, client, resURI); + Client client, URI resURI, RetryPolicy retryPolicy) { + super(authUgi, client, resURI, retryPolicy); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/FileSystemTimelineWriter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/FileSystemTimelineWriter.java index b92f4e412347c..a94d57e4466d4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/FileSystemTimelineWriter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/FileSystemTimelineWriter.java @@ -38,6 +38,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; +import net.jodah.failsafe.RetryPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.classification.InterfaceAudience.Private; @@ -67,7 +68,8 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; -import com.sun.jersey.api.client.Client; + +import javax.ws.rs.client.Client; /** * A simple writer class for storing Timeline data in any storage that @@ -102,9 +104,10 @@ public class FileSystemTimelineWriter extends TimelineWriter{ private final AttemptDirCache attemptDirCache; public FileSystemTimelineWriter(Configuration conf, - UserGroupInformation authUgi, Client client, URI resURI) + UserGroupInformation authUgi, Client client, URI resURI, + RetryPolicy retryPolicy) throws IOException { - super(authUgi, client, resURI); + super(authUgi, client, resURI, retryPolicy); Configuration fsConf = new Configuration(conf); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineClientImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineClientImpl.java index 2b9ce4fa8f2ad..823a754b1bfb5 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineClientImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineClientImpl.java @@ -24,6 +24,7 @@ import java.net.URI; import java.security.PrivilegedExceptionAction; +import net.jodah.failsafe.RetryPolicy; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; @@ -54,7 +55,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.Client; +import javax.ws.rs.client.Client; @Private @Evolving @@ -152,17 +153,18 @@ protected TimelineConnector createTimelineConnector() { protected void serviceStart() throws Exception { timelineWriter = createTimelineWriter(getConfig(), authUgi, connector.getClient(), TimelineConnector.constructResURI(getConfig(), - timelineServiceAddress, RESOURCE_URI_STR_V1)); + timelineServiceAddress, RESOURCE_URI_STR_V1), connector.getRetryPolicy()); } protected TimelineWriter createTimelineWriter(Configuration conf, - UserGroupInformation ugi, Client webClient, URI uri) + UserGroupInformation ugi, Client webClient, URI uri, + RetryPolicy retryPolicy) throws IOException { if (timelineServiceV15Enabled) { return new FileSystemTimelineWriter( - conf, ugi, webClient, uri); + conf, ugi, webClient, uri, retryPolicy); } else { - return new DirectTimelineWriter(ugi, webClient, uri); + return new DirectTimelineWriter(ugi, webClient, uri, retryPolicy); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineConnector.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineConnector.java index 5a216d20d35d6..04b7a668f0c55 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineConnector.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineConnector.java @@ -19,7 +19,6 @@ package org.apache.hadoop.yarn.client.api.impl; import java.io.IOException; -import java.io.InterruptedIOException; import java.lang.reflect.UndeclaredThrowableException; import java.net.ConnectException; import java.net.HttpURLConnection; @@ -30,10 +29,19 @@ import java.net.URLConnection; import java.security.GeneralSecurityException; import java.security.PrivilegedExceptionAction; +import java.time.Duration; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; + +import org.glassfish.jersey.client.ClientConfig; +import net.jodah.failsafe.Failsafe; +import net.jodah.failsafe.RetryPolicy; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler; import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler; @@ -58,15 +66,6 @@ import org.apache.hadoop.classification.VisibleForTesting; import org.apache.hadoop.thirdparty.com.google.common.base.Joiner; import org.apache.hadoop.util.Preconditions; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientRequest; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.api.client.filter.ClientFilter; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; /** * Utility Connector class which is used by timeline clients to securely get @@ -87,9 +86,8 @@ public class TimelineConnector extends AbstractService { private DelegationTokenAuthenticatedURL.Token token; private UserGroupInformation authUgi; private String doAsUser; - @VisibleForTesting - TimelineClientConnectionRetry connectionRetry; private boolean requireConnectionRetry; + private RetryPolicy retryPolicy; public TimelineConnector(boolean requireConnectionRetry, UserGroupInformation authUgi, String doAsUser, @@ -104,8 +102,8 @@ public TimelineConnector(boolean requireConnectionRetry, @Override protected void serviceInit(Configuration conf) throws Exception { super.serviceInit(conf); - ClientConfig cc = new DefaultClientConfig(); - cc.getClasses().add(YarnJacksonJaxbJsonProvider.class); + ClientConfig cc = new ClientConfig(); + cc.register(YarnJacksonJaxbJsonProvider.class); if (YarnConfiguration.useHttps(conf)) { // If https is chosen, configures SSL client. @@ -126,17 +124,13 @@ protected void serviceInit(Configuration conf) throws Exception { } authenticator.setConnectionConfigurator(connConfigurator); - connectionRetry = new TimelineClientConnectionRetry(conf); - client = - new Client( - new URLConnectionClientHandler(new TimelineURLConnectionFactory( - authUgi, authenticator, connConfigurator, token, doAsUser)), - cc); - if (requireConnectionRetry) { - TimelineJerseyRetryFilter retryFilter = - new TimelineJerseyRetryFilter(connectionRetry); - client.addFilter(retryFilter); - } + retryPolicy = createRetryPolicy(conf); + + cc.connectorProvider(new HttpUrlConnectorProvider().connectionFactory( + new TimelineURLConnectionFactory( + authUgi, authenticator, connConfigurator, token, doAsUser))); + + client = ClientBuilder.newClient(cc); } private static final ConnectionConfigurator DEFAULT_TIMEOUT_CONN_CONFIGURATOR @@ -208,7 +202,7 @@ DelegationTokenAuthenticatedURL getDelegationTokenAuthenticatedURL() { protected void serviceStop() { if (this.client != null) { - this.client.destroy(); + this.client.close(); } if (this.sslFactory != null) { this.sslFactory.destroy(); @@ -225,8 +219,7 @@ public Object operateDelegationToken( // Set up the retry operation TimelineClientRetryOp tokenRetryOp = createRetryOpForOperateDelegationToken(action); - - return connectionRetry.retryOn(tokenRetryOp); + return Failsafe.with(retryPolicy).get(tokenRetryOp::run); } @Private @@ -245,20 +238,17 @@ TimelineClientRetryOp createRetryOpForOperateDelegationToken( public static abstract class TimelineClientRetryOp { // The operation that should be retried public abstract Object run() throws IOException; - - // The method to indicate if we should retry given the incoming exception - public abstract boolean shouldRetryOn(Exception e); } private static class TimelineURLConnectionFactory - implements HttpURLConnectionFactory { + implements HttpUrlConnectorProvider.ConnectionFactory { private DelegationTokenAuthenticator authenticator; private UserGroupInformation authUgi; private ConnectionConfigurator connConfigurator; private Token token; private String doAsUser; - public TimelineURLConnectionFactory(UserGroupInformation authUgi, + TimelineURLConnectionFactory(UserGroupInformation authUgi, DelegationTokenAuthenticator authenticator, ConnectionConfigurator connConfigurator, DelegationTokenAuthenticatedURL.Token token, String doAsUser) { @@ -270,8 +260,7 @@ public TimelineURLConnectionFactory(UserGroupInformation authUgi, } @Override - public HttpURLConnection getHttpURLConnection(final URL url) - throws IOException { + public HttpURLConnection getConnection(URL url) throws IOException { authUgi.checkTGTAndReloginFromKeytab(); try { return new DelegationTokenAuthenticatedURL(authenticator, @@ -282,143 +271,43 @@ public HttpURLConnection getHttpURLConnection(final URL url) throw new IOException(ae); } } - } - // Class to handle retry - // Outside this class, only visible to tests - @Private - @VisibleForTesting - static class TimelineClientConnectionRetry { - - // maxRetries < 0 means keep trying - @Private - @VisibleForTesting - public int maxRetries; - - @Private - @VisibleForTesting - public long retryInterval; - - // Indicates if retries happened last time. Only tests should read it. - // In unit tests, retryOn() calls should _not_ be concurrent. - private boolean retried = false; - - @Private - @VisibleForTesting - boolean getRetired() { - return retried; - } - - // Constructor with default retry settings - public TimelineClientConnectionRetry(Configuration conf) { - Preconditions.checkArgument( - conf.getInt(YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES, - YarnConfiguration.DEFAULT_TIMELINE_SERVICE_CLIENT_MAX_RETRIES) - >= -1, - "%s property value should be greater than or equal to -1", - YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES); - Preconditions.checkArgument( - conf.getLong( - YarnConfiguration.TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS, - YarnConfiguration. + private RetryPolicy createRetryPolicy(Configuration conf) { + Preconditions.checkArgument( + conf.getInt(YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES, + YarnConfiguration.DEFAULT_TIMELINE_SERVICE_CLIENT_MAX_RETRIES) + >= -1, + "%s property value should be greater than or equal to -1", + YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES); + Preconditions.checkArgument( + conf.getLong( + YarnConfiguration.TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS, + YarnConfiguration. DEFAULT_TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS) > 0, "%s property value should be greater than zero", YarnConfiguration.TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS); - maxRetries = - conf.getInt(YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES, - YarnConfiguration.DEFAULT_TIMELINE_SERVICE_CLIENT_MAX_RETRIES); - retryInterval = conf.getLong( - YarnConfiguration.TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS, - YarnConfiguration.DEFAULT_TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS); - } - - public Object retryOn(TimelineClientRetryOp op) - throws RuntimeException, IOException { - int leftRetries = maxRetries; - retried = false; - - // keep trying - while (true) { - try { - // try perform the op, if fail, keep retrying - return op.run(); - } catch (IOException | RuntimeException e) { - // break if there's no retries left - if (leftRetries == 0) { - break; - } - if (op.shouldRetryOn(e)) { - logException(e, leftRetries); - } else { - throw e; - } - } - if (leftRetries > 0) { - leftRetries--; - } - retried = true; - try { - // sleep for the given time interval - Thread.sleep(retryInterval); - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - throw new InterruptedIOException("Client retry sleep interrupted!"); - } - } - throw new RuntimeException("Failed to connect to timeline server. " - + "Connection retries limit exceeded. " - + "The posted timeline event may be missing"); - }; - - private void logException(Exception e, int leftRetries) { - if (leftRetries > 0) { - LOG.info( - "Exception caught by TimelineClientConnectionRetry," + " will try " - + leftRetries + " more time(s).\nMessage: " + e.getMessage()); - } else { - // note that maxRetries may be -1 at the very beginning - LOG.info("ConnectionException caught by TimelineClientConnectionRetry," - + " will keep retrying.\nMessage: " + e.getMessage()); - } + int maxRetries = 0; + if (requireConnectionRetry) { + // maxRetries < 0 means keep trying + maxRetries = conf.getInt(YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES, + YarnConfiguration.DEFAULT_TIMELINE_SERVICE_CLIENT_MAX_RETRIES); } + long retryInterval = conf.getLong( + YarnConfiguration.TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS, + YarnConfiguration.DEFAULT_TIMELINE_SERVICE_CLIENT_RETRY_INTERVAL_MS); + return retryPolicy = new RetryPolicy<>() + .handle(IOException.class, RuntimeException.class) + .handleIf(e -> e instanceof ProcessingException + && (e.getCause() instanceof ConnectException + || e.getCause() instanceof SocketTimeoutException + || e.getCause() instanceof SocketException)) + .withDelay(Duration.ofMillis(retryInterval)) + .withMaxRetries(maxRetries); } - private static class TimelineJerseyRetryFilter extends ClientFilter { - private TimelineClientConnectionRetry connectionRetry; - - public TimelineJerseyRetryFilter( - TimelineClientConnectionRetry connectionRetry) { - this.connectionRetry = connectionRetry; - } - - @Override - public ClientResponse handle(final ClientRequest cr) - throws ClientHandlerException { - // Set up the retry operation - TimelineClientRetryOp jerseyRetryOp = new TimelineClientRetryOp() { - @Override - public Object run() { - // Try pass the request, if fail, keep retrying - return getNext().handle(cr); - } - - @Override - public boolean shouldRetryOn(Exception e) { - // Only retry on connection exceptions - return (e instanceof ClientHandlerException) - && (e.getCause() instanceof ConnectException - || e.getCause() instanceof SocketTimeoutException - || e.getCause() instanceof SocketException); - } - }; - try { - return (ClientResponse) connectionRetry.retryOn(jerseyRetryOp); - } catch (IOException e) { - throw new ClientHandlerException( - "Jersey retry failed!\nMessage: " + e.getMessage()); - } - } + RetryPolicy getRetryPolicy() { + return retryPolicy; } @Private @@ -447,13 +336,5 @@ public Object run() throws IOException { throw new IOException(e); } } - - @Override - public boolean shouldRetryOn(Exception e) { - // retry on connection exceptions - // and SocketTimeoutException - return (e instanceof ConnectException - || e instanceof SocketTimeoutException); - } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineReaderClientImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineReaderClientImpl.java index 71bf13220b016..42fe60b62fa55 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineReaderClientImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineReaderClientImpl.java @@ -18,7 +18,7 @@ package org.apache.hadoop.yarn.client.api.impl; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import net.jodah.failsafe.Failsafe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.classification.InterfaceAudience; @@ -31,12 +31,15 @@ import org.apache.hadoop.yarn.api.records.ContainerId; import org.apache.hadoop.yarn.api.records.timelineservice.TimelineEntity; import org.apache.hadoop.yarn.client.api.TimelineReaderClient; -import com.sun.jersey.api.client.ClientResponse; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; @@ -111,12 +114,12 @@ public TimelineEntity getApplicationEntity(ApplicationId appId, String fields, if (fields == null || fields.isEmpty()) { fields = "INFO"; } - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", fields); mergeFilters(params, filters); - ClientResponse response = doGetUri(baseUri, path, params); - TimelineEntity entity = response.getEntity(TimelineEntity.class); + Response response = doGetUri(baseUri, path, params); + TimelineEntity entity = response.readEntity(TimelineEntity.class); return entity; } @@ -131,12 +134,12 @@ public TimelineEntity getApplicationAttemptEntity( if (fields == null || fields.isEmpty()) { fields = "INFO"; } - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", fields); mergeFilters(params, filters); - ClientResponse response = doGetUri(baseUri, path, params); - TimelineEntity entity = response.getEntity(TimelineEntity.class); + Response response = doGetUri(baseUri, path, params); + TimelineEntity entity = response.readEntity(TimelineEntity.class); return entity; } @@ -150,7 +153,7 @@ public List getApplicationAttemptEntities( if (fields == null || fields.isEmpty()) { fields = "INFO"; } - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", fields); if (limit > 0) { params.add("limit", Long.toString(limit)); @@ -160,8 +163,8 @@ public List getApplicationAttemptEntities( } mergeFilters(params, filters); - ClientResponse response = doGetUri(baseUri, path, params); - TimelineEntity[] entities = response.getEntity(TimelineEntity[].class); + Response response = doGetUri(baseUri, path, params); + TimelineEntity[] entities = response.readEntity(TimelineEntity[].class); return Arrays.asList(entities); } @@ -176,12 +179,12 @@ public TimelineEntity getContainerEntity(ContainerId containerId, if (fields == null || fields.isEmpty()) { fields = "INFO"; } - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", fields); mergeFilters(params, filters); - ClientResponse response = doGetUri(baseUri, path, params); - TimelineEntity entity = response.getEntity(TimelineEntity.class); + Response response = doGetUri(baseUri, path, params); + TimelineEntity entity = response.readEntity(TimelineEntity.class); return entity; } @@ -196,7 +199,7 @@ public List getContainerEntities( if (fields == null || fields.isEmpty()) { fields = "INFO"; } - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", fields); if (limit > 0) { params.add("limit", Long.toString(limit)); @@ -206,8 +209,8 @@ public List getContainerEntities( } mergeFilters(params, filters); - ClientResponse response = doGetUri(baseUri, path, params); - TimelineEntity[] entity = response.getEntity(TimelineEntity[].class); + Response response = doGetUri(baseUri, path, params); + TimelineEntity[] entity = response.readEntity(TimelineEntity[].class); return Arrays.asList(entity); } @@ -232,19 +235,23 @@ private void mergeFilters(MultivaluedMap defaults, } @VisibleForTesting - protected ClientResponse doGetUri(URI base, String path, + protected Response doGetUri(URI base, String path, MultivaluedMap params) throws IOException { - ClientResponse resp = connector.getClient().resource(base).path(path) - .queryParams(params).accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = connector.getClient().target(base).path(path); + for(Map.Entry> param : params.entrySet()) { + target = target.queryParam(param.getKey(), param.getValue()); + } + Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON); + Response resp = Failsafe.with(connector.getRetryPolicy()) + .get(() -> builder.get(Response.class)); if (resp == null || - resp.getStatusInfo().getStatusCode() != ClientResponse.Status.OK + resp.getStatusInfo().getStatusCode() != Response.Status.OK .getStatusCode()) { String msg = "Response from the timeline reader server is " + ((resp == null) ? "null" : "not successful," + " HTTP error code: " + resp.getStatus() + - ", Server response:\n" + resp.getEntity(String.class)); + ", Server response:\n" + resp.readEntity(String.class)); LOG.error(msg); throw new IOException(msg); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java index 83a4df451bb97..54b77a183ebc0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java @@ -24,8 +24,9 @@ import java.net.InetSocketAddress; import java.net.URI; import java.security.PrivilegedExceptionAction; +import java.util.List; +import java.util.Map; import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -34,8 +35,13 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.CancellationException; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,10 +63,6 @@ import org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.core.util.MultivaluedMapImpl; /** * Implementation of timeline v2 client interface. @@ -288,24 +290,23 @@ private void checkRetryWithSleep(int retries, IOException e) } } - private ClientResponse doPutObjects(URI base, String path, - MultivaluedMap params, Object obj) { - return connector.getClient().resource(base).path(path).queryParams(params) - .accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, obj); + private Response doPutObjects(URI base, String path, + MultivaluedMap params, Object obj) { + WebTarget target = connector.getClient().target(base).path(path); + for(Map.Entry> param : params.entrySet()) { + target = target.queryParam(param.getKey(), param.getValue()); + } + return target.request(MediaType.APPLICATION_JSON) + .put(Entity.json(obj), Response.class); } protected void putObjects(URI base, String path, MultivaluedMap params, Object obj) throws IOException, YarnException { - ClientResponse resp = null; + Response resp = null; try { - resp = authUgi.doAs(new PrivilegedExceptionAction() { - @Override - public ClientResponse run() throws Exception { - return doPutObjects(base, path, params, obj); - } - }); + resp = authUgi.doAs((PrivilegedExceptionAction) () + -> doPutObjects(base, path, params, obj)); } catch (UndeclaredThrowableException ue) { Throwable cause = ue.getCause(); if (cause instanceof IOException) { @@ -325,23 +326,20 @@ public ClientResponse run() throws Exception { LOG.error(msg); throw new YarnException(msg); } else if (resp.getStatusInfo().getStatusCode() - == ClientResponse.Status.OK.getStatusCode()) { + == Response.Status.OK.getStatusCode()) { try { resp.close(); - } catch(ClientHandlerException che) { - LOG.warn("Error closing the HTTP response's inputstream. ", che); + } catch(ProcessingException e) { + LOG.warn("Error closing the HTTP response's inputstream. ", e); } } else { String msg = ""; try { - String stringType = resp.getEntity(String.class); + String stringType = resp.readEntity(String.class); msg = "Server response:\n" + stringType; - } catch (ClientHandlerException | UniformInterfaceException chuie) { - msg = "Error getting entity from the HTTP response." - + chuie.getLocalizedMessage(); - } catch (Throwable t) { + } catch (ProcessingException | IllegalStateException e) { msg = "Error getting entity from the HTTP response." - + t.getLocalizedMessage(); + + e.getLocalizedMessage(); } finally { msg = "Response from the timeline server is not successful" + ", HTTP error code: " + resp.getStatus() @@ -394,16 +392,14 @@ private final class EntitiesHolder extends FutureTask { EntitiesHolder(final TimelineEntities entities, final boolean isSync, final boolean subappwrite) { - super(new Callable() { - // publishEntities() - public Void call() throws Exception { - MultivaluedMap params = new MultivaluedMapImpl(); - params.add("appid", getContextAppId().toString()); - params.add("async", Boolean.toString(!isSync)); - params.add("subappwrite", Boolean.toString(subappwrite)); - putObjects("entities", params, entities); - return null; - } + // publishEntities() + super(() -> { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.add("appid", getContextAppId().toString()); + params.add("async", Boolean.toString(!isSync)); + params.add("subappwrite", Boolean.toString(subappwrite)); + putObjects("entities", params, entities); + return null; }); this.entities = entities; this.isSync = isSync; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineWriter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineWriter.java index 957501cb48632..362e1cd2b851d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineWriter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineWriter.java @@ -24,8 +24,14 @@ import java.lang.reflect.UndeclaredThrowableException; import java.net.URI; import java.security.PrivilegedExceptionAction; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import net.jodah.failsafe.Failsafe; +import net.jodah.failsafe.RetryPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.classification.InterfaceAudience.Private; @@ -41,9 +47,6 @@ import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; /** * Base writer class to write the Timeline data. @@ -58,12 +61,14 @@ public abstract class TimelineWriter implements Flushable { private UserGroupInformation authUgi; private Client client; private URI resURI; + private final RetryPolicy retryPolicy; public TimelineWriter(UserGroupInformation authUgi, Client client, - URI resURI) { + URI resURI, RetryPolicy retryPolicy) { this.authUgi = authUgi; this.client = client; this.resURI = resURI; + this.retryPolicy = retryPolicy; } public void close() throws Exception { @@ -89,8 +94,8 @@ public TimelinePutResponse putEntities( } entitiesContainer.addEntity(entity); } - ClientResponse resp = doPosting(entitiesContainer, null); - return resp.getEntity(TimelinePutResponse.class); + Response resp = doPosting(entitiesContainer, null); + return resp.readEntity(TimelinePutResponse.class); } public void putDomain(TimelineDomain domain) throws IOException, @@ -105,16 +110,12 @@ public abstract TimelinePutResponse putEntities( public abstract void putDomain(ApplicationAttemptId appAttemptId, TimelineDomain domain) throws IOException, YarnException; - private ClientResponse doPosting(final Object obj, final String path) + private Response doPosting(final Object obj, final String path) throws IOException, YarnException { - ClientResponse resp; + Response resp; try { - resp = authUgi.doAs(new PrivilegedExceptionAction() { - @Override - public ClientResponse run() throws Exception { - return doPostingObject(obj, path); - } - }); + resp = authUgi.doAs((PrivilegedExceptionAction) + () -> doPostingObject(obj, path)); } catch (UndeclaredThrowableException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) { @@ -127,14 +128,14 @@ public ClientResponse run() throws Exception { } if (resp == null || resp.getStatusInfo().getStatusCode() - != ClientResponse.Status.OK.getStatusCode()) { + != Response.Status.OK.getStatusCode()) { String msg = "Failed to get the response from the timeline server."; LOG.error(msg); if (resp != null) { msg += " HTTP error code: " + resp.getStatus(); LOG.debug("HTTP error code: {} Server response : \n{}", - resp.getStatus(), resp.getEntity(String.class)); + resp.getStatus(), resp.readEntity(String.class)); } throw new YarnException(msg); } @@ -143,20 +144,20 @@ public ClientResponse run() throws Exception { @Private @VisibleForTesting - public ClientResponse doPostingObject(Object object, String path) { - WebResource webResource = client.resource(resURI); + public Response doPostingObject(Object object, String path) { + final WebTarget webTarget = client.target(resURI); if (path == null) { LOG.debug("POST to {}", resURI); - ClientResponse r = webResource.accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, object); + Response r = Failsafe.with(retryPolicy).get( + () -> webTarget.request(MediaType.APPLICATION_JSON) + .post(Entity.json(object), Response.class)); r.bufferEntity(); return r; } else if (path.equals("domain")) { LOG.debug("PUT to {}/{}", resURI, path); - ClientResponse r = webResource.path(path).accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, object); + Response r = Failsafe.with(retryPolicy).get( + () -> webTarget.path(path).request(MediaType.APPLICATION_JSON) + .post(Entity.json(object), Response.class)); r.bufferEntity(); return r; } else { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogToolUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogToolUtils.java index 3c56b0290d74e..229daad597c6c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogToolUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogToolUtils.java @@ -29,15 +29,15 @@ import java.nio.file.Files; import java.nio.file.Paths; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; /** * This class contains several utility function which could be used in different @@ -201,15 +201,15 @@ public static PrintStream createPrintStream(String localDir, String nodeId, * @param logFile name of the log file * @return response from NMWebServices */ - public static ClientResponse getResponseFromNMWebService(Configuration conf, + public static Response getResponseFromNMWebService(Configuration conf, Client webServiceClient, ContainerLogsRequest request, String logFile) { - WebResource webResource = - webServiceClient.resource(WebAppUtils.getHttpSchemePrefix(conf) + WebTarget target = + webServiceClient.target(WebAppUtils.getHttpSchemePrefix(conf) + request.getNodeHttpAddress()); - return webResource.path("ws").path("v1").path("node") + return target.path("ws").path("v1").path("node") .path("containers").path(request.getContainerId()).path("logs") .path(logFile) .queryParam("size", Long.toString(request.getBytes())) - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/GenericExceptionHandler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/GenericExceptionHandler.java index b8fc9e00541d8..d9ff82f02c3fd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/GenericExceptionHandler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/GenericExceptionHandler.java @@ -58,8 +58,8 @@ public Response toResponse(Exception e) { // Don't catch this as filter forward on 404 // (ServletContainer.FEATURE_FILTER_FORWARD_ON_404) // won't work and the web UI won't work! - if (e instanceof com.sun.jersey.api.NotFoundException) { - return ((com.sun.jersey.api.NotFoundException) e).getResponse(); + if (e instanceof javax.ws.rs.NotFoundException) { + return ((javax.ws.rs.NotFoundException) e).getResponse(); } // clear content type response.setContentType(null); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApp.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApp.java index 4898c2667775f..3241de9bc8183 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApp.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApp.java @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.classification.InterfaceAudience; @@ -41,11 +40,8 @@ import com.google.inject.Provides; import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.container.filter.GZIPContentEncodingFilter; -import com.sun.jersey.api.core.ResourceConfig; -import com.sun.jersey.core.util.FeaturesAndProperties; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.spi.container.servlet.ServletContainer; + +import javax.servlet.Filter; /** * @see WebApps for a usage example @@ -184,18 +180,12 @@ protected void configureWebAppServlets() { String regex = "(?!/" + this.wsName + ")"; serveRegex(regex).with(DefaultWrapperServlet.class); - Map params = new HashMap(); - params.put(ResourceConfig.FEATURE_IMPLICIT_VIEWABLES, "true"); - params.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, "true"); - params.put(FeaturesAndProperties.FEATURE_XMLROOTELEMENT_PROCESSING, "true"); - params.put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, GZIPContentEncodingFilter.class.getName()); - params.put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, GZIPContentEncodingFilter.class.getName()); - filter("/*").through(getWebAppFilterClass(), params); + filter("/*").through(getWebAppFilterClass(), new HashMap<>()); } } - protected Class getWebAppFilterClass() { - return GuiceContainer.class; + protected Class getWebAppFilterClass() { + return Filter.class; } /** diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApps.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApps.java index 9fef076196e4e..fb41359a27af9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApps.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/WebApps.java @@ -45,6 +45,7 @@ import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; import org.eclipse.jetty.webapp.WebAppContext; +import org.glassfish.jersey.servlet.ServletProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -407,8 +408,11 @@ public void setup() { new String[] {"/*"}); } + final Map guiceFilterParams = new HashMap<>(); + guiceFilterParams.put(ServletProperties.FILTER_FORWARD_ON_404, "true"); HttpServer2.defineFilter(server.getWebAppContext(), "guice", - GuiceFilter.class.getName(), null, new String[] { "/*" }); + GuiceFilter.class.getName(), guiceFilterParams, + new String[] { "/*" }); webapp.setConf(conf); webapp.setHttpServer(server); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java index 5b578197100a1..20570f93cfcda 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java @@ -25,6 +25,7 @@ import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -49,6 +50,7 @@ import org.apache.http.client.utils.URLEncodedUtils; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.container.ContainerRequestContext; @Private @Evolving @@ -556,6 +558,20 @@ private static String getURLEncodedQueryString(HttpServletRequest request, return null; } + private static String getURLEncodedQueryString(ContainerRequestContext request, + String parameterToRemove) { + String queryString = request.getUriInfo().getPath(); + if (queryString != null && !queryString.isEmpty()) { + List params = URLEncodedUtils.parse(queryString, + StandardCharsets.ISO_8859_1); + if (parameterToRemove != null && !parameterToRemove.isEmpty()) { + params.removeIf(current -> current.getName().equals(parameterToRemove)); + } + return URLEncodedUtils.format(params, StandardCharsets.ISO_8859_1); + } + return null; + } + /** * Get a query string. * @param request HttpServletRequest with the request details @@ -577,6 +593,20 @@ public static List getURLEncodedQueryParam( return null; } + /** + * Get a query string. + * @param request ContainerRequestContext with the request details + * @return the query parameter string + */ + public static List getURLEncodedQueryParam( + ContainerRequestContext request) { + String queryString = request.getUriInfo().getPath(); + if (queryString != null && !queryString.isEmpty()) { + return URLEncodedUtils.parse(queryString, StandardCharsets.ISO_8859_1); + } + return null; + } + /** * Get a query string which removes the passed parameter. * @param httpRequest HttpServletRequest with the request details diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebServiceClient.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebServiceClient.java index 3f61645461b8b..f118c71e96b38 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebServiceClient.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebServiceClient.java @@ -19,7 +19,12 @@ import java.io.IOException; import java.net.HttpURLConnection; -import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.security.authentication.client.AuthenticatedURL; @@ -29,9 +34,6 @@ import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; /** * Utility for handling Web client. @@ -92,33 +94,31 @@ private static SSLFactory createSSLFactory(Configuration conf) * @return Client */ public Client createClient() { - return new Client( - new URLConnectionClientHandler(getHttpURLConnectionFactory())); + final ClientConfig cc = new ClientConfig(); + cc.connectorProvider(getHttpURLConnectionFactory()); + return ClientBuilder.newClient(cc); } @VisibleForTesting - protected HttpURLConnectionFactory getHttpURLConnectionFactory() { - return new HttpURLConnectionFactory() { - @Override - public HttpURLConnection getHttpURLConnection(URL url) - throws IOException { - AuthenticatedURL.Token token = new AuthenticatedURL.Token(); - HttpURLConnection conn = null; - try { - HttpURLConnection.setFollowRedirects(false); - // If https is chosen, configures SSL client. - if (isHttps) { - conn = new AuthenticatedURL(new KerberosAuthenticator(), - sslFactory).openConnection(url, token); - } else { - conn = new AuthenticatedURL().openConnection(url, token); + protected HttpUrlConnectorProvider getHttpURLConnectionFactory() { + return new HttpUrlConnectorProvider().connectionFactory( + url -> { + AuthenticatedURL.Token token = new AuthenticatedURL.Token(); + HttpURLConnection conn; + try { + HttpURLConnection.setFollowRedirects(false); + // If https is chosen, configures SSL client. + if (isHttps) { + conn = new AuthenticatedURL(new KerberosAuthenticator(), + sslFactory).openConnection(url, token); + } else { + conn = new AuthenticatedURL().openConnection(url, token); + } + } catch (AuthenticationException e) { + throw new IOException(e); } - } catch (AuthenticationException e) { - throw new IOException(e); - } - return conn; - } - }; + return conn; + }); } public synchronized static void destroy() { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/YarnWebServiceUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/YarnWebServiceUtils.java index fccb3e1415f8e..634fa359616fc 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/YarnWebServiceUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/YarnWebServiceUtils.java @@ -17,19 +17,16 @@ */ package org.apache.hadoop.yarn.webapp.util; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource.Builder; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONMarshaller; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hadoop.conf.Configuration; -import org.codehaus.jettison.json.JSONObject; -import java.io.StringWriter; +import org.codehaus.jettison.json.JSONObject; /** * This class contains several utility function which could be used to generate @@ -40,57 +37,41 @@ public final class YarnWebServiceUtils { private YarnWebServiceUtils() {} + private static final ObjectMapper mapper = new ObjectMapper(); + /** * Utility function to get NodeInfo by calling RM WebService. * @param conf the configuration - * @param nodeId the nodeId + * @param nodeId the node * @return a JSONObject which contains the NodeInfo - * @throws ClientHandlerException if there is an error - * processing the response. - * @throws UniformInterfaceException if the response status - * is 204 (No Content). */ public static JSONObject getNodeInfoFromRMWebService(Configuration conf, - String nodeId) throws ClientHandlerException, - UniformInterfaceException { + String nodeId) throws ProcessingException, IllegalStateException { try { return WebAppUtils.execOnActiveRM(conf, YarnWebServiceUtils::getNodeInfoFromRM, nodeId); + } catch (ProcessingException | IllegalStateException e) { + throw e; } catch (Exception e) { - if (e instanceof ClientHandlerException) { - throw ((ClientHandlerException) e); - } else if (e instanceof UniformInterfaceException) { - throw ((UniformInterfaceException) e); - } else { - throw new RuntimeException(e); - } + throw new RuntimeException(e); } } private static JSONObject getNodeInfoFromRM(String webAppAddress, - String nodeId) throws ClientHandlerException, UniformInterfaceException { - Client webServiceClient = Client.create(); - ClientResponse response = null; - try { - Builder builder = webServiceClient.resource(webAppAddress) - .path("ws").path("v1").path("cluster") - .path("nodes").path(nodeId).accept(MediaType.APPLICATION_JSON); - response = builder.get(ClientResponse.class); - return response.getEntity(JSONObject.class); + String nodeId) { + Client webServiceClient = ClientBuilder.newClient(); + try (Response response = webServiceClient.target(webAppAddress) + .path("ws").path("v1").path("cluster") + .path("nodes").path(nodeId) + .request(MediaType.APPLICATION_JSON) + .get(Response.class)) { + return response.readEntity(JSONObject.class); } finally { - if (response != null) { - response.close(); - } - webServiceClient.destroy(); + webServiceClient.close(); } } - @SuppressWarnings("rawtypes") - public static String toJson(Object nsli, Class klass) throws Exception { - StringWriter sw = new StringWriter(); - JSONJAXBContext ctx = new JSONJAXBContext(klass); - JSONMarshaller jm = ctx.createJSONMarshaller(); - jm.marshallToJSON(nsli, sw); - return sw.toString(); + public static String toJson(Object obj, Class klass) throws Exception { + return mapper.writerFor(klass).writeValueAsString(obj); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClient.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClient.java index e7110dda9ffcc..2415fab57159a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClient.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClient.java @@ -34,6 +34,7 @@ import java.net.URI; import java.security.PrivilegedExceptionAction; +import net.jodah.failsafe.RetryPolicy; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; import org.apache.hadoop.http.HttpConfig.Policy; @@ -44,6 +45,7 @@ import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager; import static org.apache.hadoop.security.ssl.FileBasedKeyStoresFactory.SSL_MONITORING_THREAD_NAME; import org.apache.hadoop.test.TestGenericTestUtils; +import org.apache.hadoop.util.Time; import org.apache.hadoop.yarn.api.records.timeline.TimelineDomain; import org.apache.hadoop.yarn.api.records.timeline.TimelineEntities; import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity; @@ -52,14 +54,15 @@ import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier; +import org.glassfish.jersey.client.ClientResponse; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientResponse; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; public class TestTimelineClient { @@ -88,7 +91,7 @@ public void tearDown() throws Exception { @Test public void testPostEntities() throws Exception { - mockEntityClientResponse(spyTimelineWriter, ClientResponse.Status.OK, + mockEntityClientResponse(spyTimelineWriter, Response.Status.OK, false, false); try { TimelinePutResponse response = client.putEntities(generateEntity()); @@ -100,7 +103,7 @@ public void testPostEntities() throws Exception { @Test public void testPostEntitiesWithError() throws Exception { - mockEntityClientResponse(spyTimelineWriter, ClientResponse.Status.OK, true, + mockEntityClientResponse(spyTimelineWriter, Response.Status.OK, true, false); try { TimelinePutResponse response = client.putEntities(generateEntity()); @@ -128,7 +131,7 @@ public void testPostIncompleteEntities() throws Exception { @Test public void testPostEntitiesNoResponse() throws Exception { mockEntityClientResponse(spyTimelineWriter, - ClientResponse.Status.INTERNAL_SERVER_ERROR, false, false); + Response.Status.INTERNAL_SERVER_ERROR, false, false); try { client.putEntities(generateEntity()); Assert.fail("Exception is expected"); @@ -145,13 +148,13 @@ public void testPostEntitiesConnectionRefused() throws Exception { client.putEntities(generateEntity()); Assert.fail("RuntimeException is expected"); } catch (RuntimeException re) { - Assert.assertTrue(re instanceof ClientHandlerException); + Assert.assertTrue(re instanceof ProcessingException); } } @Test public void testPutDomain() throws Exception { - mockDomainClientResponse(spyTimelineWriter, ClientResponse.Status.OK, false); + mockDomainClientResponse(spyTimelineWriter, Response.Status.OK, false); try { client.putDomain(generateDomain()); } catch (YarnException e) { @@ -162,7 +165,7 @@ public void testPutDomain() throws Exception { @Test public void testPutDomainNoResponse() throws Exception { mockDomainClientResponse(spyTimelineWriter, - ClientResponse.Status.FORBIDDEN, false); + Response.Status.FORBIDDEN, false); try { client.putDomain(generateDomain()); Assert.fail("Exception is expected"); @@ -179,7 +182,7 @@ public void testPutDomainConnectionRefused() throws Exception { client.putDomain(generateDomain()); Assert.fail("RuntimeException is expected"); } catch (RuntimeException re) { - Assert.assertTrue(re instanceof ClientHandlerException); + Assert.assertTrue(re instanceof ProcessingException); } } @@ -217,18 +220,17 @@ public void testCheckRetryCount() throws Exception { newIntervalMs); conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, true); TimelineClientImpl client = createTimelineClient(conf); + long start = Time.monotonicNow(); try { // This call should fail because there is no timeline server client.putEntities(generateEntity()); Assert.fail("Exception expected! " + "Timeline server should be off to run this test. "); } catch (RuntimeException ce) { - Assert.assertTrue( - "Handler exception for reason other than retry: " + ce.getMessage(), - ce.getMessage().contains("Connection retries limit exceeded")); - // we would expect this exception here, check if the client has retried - Assert.assertTrue("Retry filter didn't perform any retries! ", - client.connector.connectionRetry.getRetired()); + long end = Time.monotonicNow(); + ce.printStackTrace(); + Assert.assertTrue("Failed without retries.", + end - start >= newIntervalMs * newMaxRetries); } } @@ -263,7 +265,6 @@ public void testDelegationTokenOperationsRetry() throws Exception { UserGroupInformation.getCurrentUser().getShortUserName()); assertFail(); } catch (RuntimeException ce) { - assertException(client, ce); } try { @@ -278,7 +279,6 @@ public void testDelegationTokenOperationsRetry() throws Exception { new Text("0.0.0.0:8188"))); assertFail(); } catch (RuntimeException ce) { - assertException(client, ce); } try { @@ -293,7 +293,6 @@ public void testDelegationTokenOperationsRetry() throws Exception { new Text("0.0.0.0:8188"))); assertFail(); } catch (RuntimeException ce) { - assertException(client, ce); } // Test DelegationTokenOperationsRetry on SocketTimeoutException @@ -308,7 +307,6 @@ public void testDelegationTokenOperationsRetry() throws Exception { new Text("0.0.0.0:8188"))); assertFail(); } catch (RuntimeException ce) { - assertException(clientFake, ce); } } finally { client.stop(); @@ -360,21 +358,12 @@ private static void assertFail() { + "Timeline server should be off to run this test."); } - private void assertException(TimelineClientImpl client, RuntimeException ce) { - Assert.assertTrue( - "Handler exception for reason other than retry: " + ce.toString(), ce - .getMessage().contains("Connection retries limit exceeded")); - // we would expect this exception here, check if the client has retried - Assert.assertTrue("Retry filter didn't perform any retries! ", - client.connector.connectionRetry.getRetired()); - } - public static ClientResponse mockEntityClientResponse( - TimelineWriter spyTimelineWriter, ClientResponse.Status status, + TimelineWriter spyTimelineWriter, Response.Status status, boolean hasError, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { - doThrow(new ClientHandlerException(new ConnectException())).when( + doThrow(new ProcessingException(new ConnectException())).when( spyTimelineWriter).doPostingObject( any(TimelineEntities.class), any()); return response; @@ -391,16 +380,16 @@ public static ClientResponse mockEntityClientResponse( if (hasError) { putResponse.addError(error); } - when(response.getEntity(TimelinePutResponse.class)).thenReturn(putResponse); + when(response.readEntity(TimelinePutResponse.class)).thenReturn(putResponse); return response; } private static ClientResponse mockDomainClientResponse( - TimelineWriter spyTimelineWriter, ClientResponse.Status status, + TimelineWriter spyTimelineWriter, Response.Status status, boolean hasRuntimeError) { ClientResponse response = mock(ClientResponse.class); if (hasRuntimeError) { - doThrow(new ClientHandlerException(new ConnectException())).when( + doThrow(new ProcessingException(new ConnectException())).when( spyTimelineWriter).doPostingObject(any(TimelineDomain.class), any(String.class)); return response; @@ -451,10 +440,10 @@ private TimelineClientImpl createTimelineClient( TimelineClientImpl client = new TimelineClientImpl() { @Override protected TimelineWriter createTimelineWriter(Configuration conf, - UserGroupInformation authUgi, Client client, URI resURI) - throws IOException { + UserGroupInformation authUgi, Client client, URI resURI, + RetryPolicy retryPolicy) { TimelineWriter timelineWriter = - new DirectTimelineWriter(authUgi, client, resURI); + new DirectTimelineWriter(authUgi, client, resURI, retryPolicy); spyTimelineWriter = spy(timelineWriter); return spyTimelineWriter; } @@ -543,7 +532,7 @@ public void testTimelineConnectorDestroy() { Client mockJerseyClient = mock(Client.class); client.connector.client = mockJerseyClient; client.stop(); - verify(mockJerseyClient, times(1)).destroy(); + verify(mockJerseyClient, times(1)).close(); } private void setupSSLConfig(YarnConfiguration conf) throws Exception { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClientForATS1_5.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClientForATS1_5.java index 26dd7f42bc5f8..6f8b3ff655d1d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClientForATS1_5.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineClientForATS1_5.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.net.URI; +import net.jodah.failsafe.RetryPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.conf.Configuration; @@ -47,8 +48,8 @@ import org.junit.Before; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; public class TestTimelineClientForATS1_5 { @@ -251,14 +252,15 @@ private TimelineClientImpl createTimelineClient(YarnConfiguration conf) { TimelineClientImpl client = new TimelineClientImpl() { @Override protected TimelineWriter createTimelineWriter(Configuration conf, - UserGroupInformation authUgi, Client client, URI resURI) + UserGroupInformation authUgi, Client client, URI resURI, + RetryPolicy retryPolicy) throws IOException { TimelineWriter timelineWriter = - new FileSystemTimelineWriter(conf, authUgi, client, resURI) { - public ClientResponse doPostingObject(Object object, String path) { - ClientResponse response = mock(ClientResponse.class); + new FileSystemTimelineWriter(conf, authUgi, client, resURI, retryPolicy) { + public Response doPostingObject(Object object, String path) { + Response response = mock(Response.class); when(response.getStatusInfo()).thenReturn( - ClientResponse.Status.OK); + Response.Status.OK); return response; } }; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineReaderClientImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineReaderClientImpl.java index 757aeb8c31dc3..895d72dee5610 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineReaderClientImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestTimelineReaderClientImpl.java @@ -23,7 +23,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.sun.jersey.api.client.ClientResponse; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableMap; import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; @@ -38,6 +37,7 @@ import org.junit.Test; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import java.io.IOException; import java.net.URI; import java.util.ArrayList; @@ -145,27 +145,27 @@ private static TimelineEntity[] createTimelineEntities(String... ids) { private class MockTimelineReaderClient extends TimelineReaderClientImpl { @Override - protected ClientResponse doGetUri(URI base, String path, + protected Response doGetUri(URI base, String path, MultivaluedMap params) throws IOException { - ClientResponse mockClientResponse = mock(ClientResponse.class); + Response mockClientResponse = mock(Response.class); if (path.contains(YARN_CONTAINER.toString()) && !params.containsKey("infofilters")) { - when(mockClientResponse.getEntity(TimelineEntity.class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity.class)).thenReturn( createTimelineEntity("mockContainer1")); - when(mockClientResponse.getEntity(TimelineEntity[].class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity[].class)).thenReturn( createTimelineEntities("mockContainer1", "mockContainer2")); } else if (path.contains(YARN_CONTAINER.toString()) && params.containsKey("infofilters")) { Assert.assertEquals(encodeValue(appAttemptInfoFilter), params.get("infofilters").get(0)); - when(mockClientResponse.getEntity(TimelineEntity[].class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity[].class)).thenReturn( createTimelineEntities("mockContainer3", "mockContainer4")); } else if (path.contains(YARN_APPLICATION_ATTEMPT.toString())) { - when(mockClientResponse.getEntity(TimelineEntity.class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity.class)).thenReturn( createTimelineEntity("mockAppAttempt1")); - when(mockClientResponse.getEntity(TimelineEntity[].class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity[].class)).thenReturn( createTimelineEntities("mockAppAttempt1", "mockAppAttempt2")); } else { - when(mockClientResponse.getEntity(TimelineEntity.class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity.class)).thenReturn( createTimelineEntity("mockApp1")); - when(mockClientResponse.getEntity(TimelineEntity[].class)).thenReturn( + when(mockClientResponse.readEntity(TimelineEntity[].class)).thenReturn( createTimelineEntities("mockApp1", "mockApp2")); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestJAXBContextResolver.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestJAXBContextResolver.java deleted file mode 100644 index 6f6ee5d4b53ab..0000000000000 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestJAXBContextResolver.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by joblicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.yarn.webapp; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import javax.ws.rs.ext.ContextResolver; -import javax.ws.rs.ext.Provider; -import javax.xml.bind.JAXBContext; - -import org.apache.hadoop.yarn.webapp.MyTestWebService.MyInfo; - -import com.google.inject.Singleton; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; - -@Singleton -@Provider -public class MyTestJAXBContextResolver implements ContextResolver { - - private JAXBContext context; - private final Set types; - - // you have to specify all the dao classes here - private final Class[] cTypes = { MyInfo.class }; - - public MyTestJAXBContextResolver() throws Exception { - this.types = new HashSet(Arrays.asList(cTypes)); - this.context = - new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false) - .build(), cTypes); - } - - @Override - public JAXBContext getContext(Class objectType) { - return (types.contains(objectType)) ? context : null; - } -} \ No newline at end of file diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/JerseyTestBase.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/ObjectWriterContextResolver.java similarity index 54% rename from hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/JerseyTestBase.java rename to hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/ObjectWriterContextResolver.java index d537fa748f9bc..1c9835f4973e7 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/JerseyTestBase.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/ObjectWriterContextResolver.java @@ -15,32 +15,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.hadoop.yarn.webapp; -import java.io.IOException; -import java.util.Random; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +import javax.ws.rs.ext.ContextResolver; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; -import org.apache.hadoop.net.ServerSocketUtil; +public class ObjectWriterContextResolver implements ContextResolver { -import com.sun.jersey.test.framework.JerseyTest; -import com.sun.jersey.test.framework.WebAppDescriptor; + private final ObjectWriter writer; + private final Set types; + private final Class[] cTypes = { MyTestWebService.MyInfo.class }; -public abstract class JerseyTestBase extends JerseyTest { - public JerseyTestBase(WebAppDescriptor appDescriptor) { - super(appDescriptor); + public ObjectWriterContextResolver() throws Exception { + this.types = new HashSet<>(Arrays.asList(cTypes)); + this.writer = new ObjectMapper().writerFor(MyTestWebService.MyInfo.class); } @Override - protected int getPort(int port) { - Random rand = new Random(); - int jerseyPort = port + rand.nextInt(1000); - try { - jerseyPort = ServerSocketUtil.getPort(jerseyPort, 10); - } catch (IOException e) { - // Ignore exception even after 10 times free port is - // not received. - } - return super.getPort(jerseyPort); + public ObjectWriter getContext(Class type) { + return (types.contains(type)) ? writer : null; } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java index 98b7505426896..673f9b1013702 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java @@ -237,7 +237,7 @@ public void testCreateWithNonZeroPort() { WebApps.$for("test", TestWebApp.class, this, "ws").start(new WebApp() { @Override public void setup() { - bind(MyTestJAXBContextResolver.class); + bind(ObjectWriterContextResolver.class); bind(MyTestWebService.class); route("/:foo", FooController.class); @@ -268,7 +268,7 @@ public void setup() { WebApps.$for("test", TestWebApp.class, this, "ws").start(new WebApp() { @Override public void setup() { - bind(MyTestJAXBContextResolver.class); + bind(ObjectWriterContextResolver.class); bind(MyTestWebService.class); route("/:foo", FooController.class); @@ -297,7 +297,7 @@ public void setup() { WebApps.$for("test", TestWebApp.class, this, "ws").start(new WebApp() { @Override public void setup() { - bind(MyTestJAXBContextResolver.class); + bind(ObjectWriterContextResolver.class); bind(MyTestWebService.class); } }); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebServiceClient.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebServiceClient.java index 99a5783628e9a..b38ed3c7084a9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebServiceClient.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebServiceClient.java @@ -32,6 +32,8 @@ import org.junit.Assert; import org.junit.Test; +import javax.ws.rs.core.Response; + public class TestWebServiceClient { private static final String BASEDIR = System.getProperty("test.build.dir", @@ -89,9 +91,8 @@ public void testCreateClient() throws Exception { URL u = new URL(baseUrl, SERVLET_PATH_ECHO + "?a=b&c=d"); WebServiceClient.initialize(sslConf); WebServiceClient client = WebServiceClient.getWebServiceClient(); - HttpURLConnection conn = client.getHttpURLConnectionFactory() - .getHttpURLConnection(u); - Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); + Response resp = client.createClient().target(u.toURI()).request().get(); + Assert.assertEquals(HttpURLConnection.HTTP_OK, resp.getStatus()); WebServiceClient.destroy(); server.stop(); FileUtil.fullyDelete(new File(BASEDIR)); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/pom.xml index 91775989cebeb..9f26292bfbefe 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/pom.xml @@ -87,39 +87,8 @@ guice - com.sun.jersey.jersey-test-framework - jersey-test-framework-core - test - - - com.sun.jersey - jersey-core - - - com.sun.jersey - jersey-client - - - com.github.pjfanning - jersey-json - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - - - com.sun.jersey.contribs - jersey-guice + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 @@ -157,13 +126,6 @@ hadoop-yarn-server-common - - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test - - commons-collections commons-collections @@ -205,6 +167,22 @@ + + + javax.ws.rs + javax.ws.rs-api + + + org.glassfish.jersey.test-framework + jersey-test-framework-core + test + + + com.google.inject + guice + + + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/JAXBContextResolver.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/JAXBContextResolver.java index 5fd01244f73c3..acaac8cd2a6d4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/JAXBContextResolver.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/JAXBContextResolver.java @@ -34,8 +34,7 @@ import org.apache.hadoop.yarn.server.webapp.dao.ContainersInfo; import com.google.inject.Singleton; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; +import org.glassfish.jersey.jettison.JettisonJaxbContext; @Singleton @Provider @@ -51,10 +50,8 @@ public class JAXBContextResolver implements ContextResolver { ContainersInfo.class }; public JAXBContextResolver() throws Exception { - this.types = new HashSet(Arrays.asList(cTypes)); - this.context = - new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false) - .build(), cTypes); + this.types = new HashSet<>(Arrays.asList(cTypes)); + this.context = new JettisonJaxbContext(cTypes); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/TestAHSWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/TestAHSWebServices.java index 29890b2c7cd3f..d28f306d9afe2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/TestAHSWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/TestAHSWebServices.java @@ -40,7 +40,12 @@ import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; @@ -76,12 +81,15 @@ import org.apache.hadoop.yarn.util.timeline.TimelineUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; @@ -93,16 +101,9 @@ import com.google.inject.Guice; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.GenericType; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; @RunWith(Parameterized.class) -public class TestAHSWebServices extends JerseyTestBase { +public class TestAHSWebServices extends JerseyTest { private static ApplicationHistoryClientService historyClientService; private static AHSWebServices ahsWebservice; @@ -174,7 +175,7 @@ protected void configureServlets() { bind(AHSWebServices.class).toInstance(ahsWebservice); bind(GenericExceptionHandler.class); bind(ApplicationBaseProtocol.class).toInstance(historyClientService); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); filter("/*").through(TestSimpleAuthFilter.class); } } @@ -202,26 +203,22 @@ protected Properties getConfiguration(String configPrefix, private int round; public TestAHSWebServices(int round) { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.applicationhistoryservice.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); - this.round = round; } @Test public void testInvalidApp() { ApplicationId appId = ApplicationId.newInstance(0, MAX_APPS + 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = + target.path("ws").path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertResponseStatusCode("404 not found expected", - Status.NOT_FOUND, response.getStatusInfo()); + Response.Status.NOT_FOUND, response.getStatusInfo()); } @Test @@ -229,20 +226,23 @@ public void testInvalidAttempt() { ApplicationId appId = ApplicationId.newInstance(0, 1); ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, MAX_APPS + 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()).path("appattempts") - .path(appAttemptId.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = + target.path("ws").path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .path("appattempts") + .path(appAttemptId.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (round == 1) { - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); return; } assertResponseStatusCode("404 not found expected", - Status.NOT_FOUND, response.getStatusInfo()); + Response.Status.NOT_FOUND, response.getStatusInfo()); } @Test @@ -252,36 +252,44 @@ public void testInvalidContainer() throws Exception { ApplicationAttemptId.newInstance(appId, 1); ContainerId containerId = ContainerId.newContainerId(appAttemptId, MAX_APPS + 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()).path("appattempts") - .path(appAttemptId.toString()).path("containers") - .path(containerId.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = + target.path("ws").path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .path("appattempts") + .path(appAttemptId.toString()) + .path("containers") + .path(containerId.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (round == 1) { - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); return; } assertResponseStatusCode("404 not found expected", - Status.NOT_FOUND, response.getStatusInfo()); + Response.Status.NOT_FOUND, response.getStatusInfo()); } @Test - public void testInvalidUri() throws JSONException, Exception { - WebResource r = resource(); + public void testInvalidUri() { + WebTarget target = target(); String responseStr = ""; try { - responseStr = - r.path("ws").path("v1").path("applicationhistory").path("bogus") - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("bogus") + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo()); WebServicesTestUtils.checkStringMatch( "error string exists and shouldn't", "", responseStr); @@ -290,15 +298,17 @@ public void testInvalidUri() throws JSONException, Exception { @Test public void testInvalidUri2() throws JSONException, Exception { - WebResource r = resource(); + WebTarget target = target(); String responseStr = ""; try { - responseStr = r.queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = target + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo()); WebServicesTestUtils.checkStringMatch( "error string exists and shouldn't", "", responseStr); } @@ -306,17 +316,20 @@ public void testInvalidUri2() throws JSONException, Exception { @Test public void testInvalidAccept() throws JSONException, Exception { - WebResource r = resource(); + WebTarget target = target(); String responseStr = ""; try { - responseStr = - r.path("ws").path("v1").path("applicationhistory") - .queryParam("user.name", USERS[round]) - .accept(MediaType.TEXT_PLAIN).get(String.class); + responseStr = target + .path("ws") + .path("v1") + .path("applicationhistory") + .queryParam("user.name", USERS[round]) + .request(MediaType.TEXT_PLAIN) + .get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.INTERNAL_SERVER_ERROR, + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.INTERNAL_SERVER_ERROR, response.getStatusInfo()); WebServicesTestUtils.checkStringMatch( "error string exists and shouldn't", "", responseStr); @@ -325,14 +338,18 @@ public void testInvalidAccept() throws JSONException, Exception { @Test public void testAbout() throws Exception { - WebResource r = resource(); - ClientResponse response = r - .path("ws").path("v1").path("applicationhistory").path("about") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("about") .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineAbout actualAbout = response.getEntity(TimelineAbout.class); + response.getMediaType().toString()); + TimelineAbout actualAbout = response.readEntity(TimelineAbout.class); TimelineAbout expectedAbout = TimelineUtils.createTimelineAbout("Generic History Service API"); Assert.assertNotNull( @@ -354,15 +371,19 @@ public void testAbout() throws Exception { @Test public void testAppsQuery() throws Exception { - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .queryParam("state", YarnApplicationState.FINISHED.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .queryParam("state", YarnApplicationState.FINISHED.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -372,16 +393,20 @@ public void testAppsQuery() throws Exception { @Test public void testQueueQuery() throws Exception { - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .queryParam("queue", "test queue") - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .queryParam("queue", "test queue") + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -393,16 +418,19 @@ public void testQueueQuery() throws Exception { @Test public void testSingleApp() throws Exception { ApplicationId appId = ApplicationId.newInstance(0, 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject app = json.getJSONObject("app"); assertEquals(appId.toString(), app.getString("appId")); @@ -425,19 +453,24 @@ public void testSingleApp() throws Exception { @Test public void testMultipleAttempts() throws Exception { ApplicationId appId = ApplicationId.newInstance(0, 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()).path("appattempts") - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .path("appattempts") + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (round == 1) { - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); return; } assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject appAttempts = json.getJSONObject("appAttempts"); assertEquals("incorrect number of elements", 1, appAttempts.length()); @@ -450,21 +483,25 @@ public void testSingleAttempt() throws Exception { ApplicationId appId = ApplicationId.newInstance(0, 1); ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()).path("appattempts") - .path(appAttemptId.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .path("appattempts") + .path(appAttemptId.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (round == 1) { - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); return; } assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject appAttempt = json.getJSONObject("appAttempt"); assertEquals(appAttemptId.toString(), appAttempt.getString("appAttemptId")); @@ -481,20 +518,26 @@ public void testMultipleContainers() throws Exception { ApplicationId appId = ApplicationId.newInstance(0, 1); ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()).path("appattempts") - .path(appAttemptId.toString()).path("containers") - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .path("appattempts") + .path(appAttemptId.toString()) + .path("containers") + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (round == 1) { - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); return; } assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject containers = json.getJSONObject("containers"); assertEquals("incorrect number of elements", 1, containers.length()); @@ -508,22 +551,27 @@ public void testSingleContainer() throws Exception { ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1); ContainerId containerId = ContainerId.newContainerId(appAttemptId, 1); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("applicationhistory").path("apps") - .path(appId.toString()).path("appattempts") - .path(appAttemptId.toString()).path("containers") - .path(containerId.toString()) - .queryParam("user.name", USERS[round]) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("apps") + .path(appId.toString()) + .path("appattempts") + .path(appAttemptId.toString()) + .path("containers") + .path(containerId.toString()) + .queryParam("user.name", USERS[round]) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); if (round == 1) { - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); return; } assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject container = json.getJSONObject("container"); assertEquals(containerId.toString(), container.getString("containerId")); @@ -564,48 +612,66 @@ public void testContainerLogsForFinishedApps() throws Exception { nodeId2, fileName, user, false); // test whether we can find container log from remote diretory if // the containerInfo for this container could be fetched from AHS. - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1") - .path("applicationhistory").path("containerlogs") - .path(containerId1.toString()).path(fileName) + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containerlogs") + .path(containerId1.toString()) + .path(fileName) .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - String responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + String responseText = response.readEntity(String.class); assertTrue(responseText.contains("Hello." + containerId1)); // Do the same test with new API - r = resource(); - response = r.path("ws").path("v1") - .path("applicationhistory").path("containers") - .path(containerId1.toString()).path("logs").path(fileName) + target = target(); + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containers") + .path(containerId1.toString()) + .path("logs") + .path(fileName) .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains("Hello." + containerId1)); // test whether we can find container log from remote diretory if // the containerInfo for this container could not be fetched from AHS. - r = resource(); - response = r.path("ws").path("v1") - .path("applicationhistory").path("containerlogs") - .path(containerId100.toString()).path(fileName) + target = target(); + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containerlogs") + .path(containerId100.toString()) + .path(fileName) .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains("Hello." + containerId100)); // Do the same test with new API - r = resource(); - response = r.path("ws").path("v1") - .path("applicationhistory").path("containers") - .path(containerId100.toString()).path("logs").path(fileName) + target = target(); + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containers") + .path(containerId100.toString()) + .path("logs") + .path(fileName) .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains("Hello." + containerId100)); // create an application which can not be found from AHS @@ -620,14 +686,18 @@ public void testContainerLogsForFinishedApps() throws Exception { Collections.singletonMap(containerId1ForApp100, "Hello." + containerId1ForApp100), nodeId, fileName, user, true); - r = resource(); - response = r.path("ws").path("v1") - .path("applicationhistory").path("containerlogs") - .path(containerId1ForApp100.toString()).path(fileName) + target = target(); + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containerlogs") + .path(containerId1ForApp100.toString()) + .path(fileName) .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains("Hello." + containerId1ForApp100)); int fullTextSize = responseText.getBytes().length; String tailEndSeparator = StringUtils.repeat("*", @@ -640,15 +710,19 @@ public void testContainerLogsForFinishedApps() throws Exception { // specify how many bytes we should get from logs // if we specify a position number, it would get the first n bytes from // container log - r = resource(); - response = r.path("ws").path("v1") - .path("applicationhistory").path("containerlogs") - .path(containerId1ForApp100.toString()).path(fileName) + target = target(); + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containerlogs") + .path(containerId1ForApp100.toString()) + .path(fileName) .queryParam("user.name", user) .queryParam("size", "5") - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertEquals(responseText.getBytes().length, (fullTextSize - fileContentSize) + 5); assertTrue(fullTextSize >= responseText.getBytes().length); @@ -659,15 +733,19 @@ public void testContainerLogsForFinishedApps() throws Exception { // specify how many bytes we should get from logs // if we specify a negative number, it would get the last n bytes from // container log - r = resource(); - response = r.path("ws").path("v1") - .path("applicationhistory").path("containerlogs") - .path(containerId1ForApp100.toString()).path(fileName) + target = target(); + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containerlogs") + .path(containerId1ForApp100.toString()) + .path(fileName) .queryParam("user.name", user) .queryParam("size", "-5") - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertEquals(responseText.getBytes().length, (fullTextSize - fileContentSize) + 5); assertTrue(fullTextSize >= responseText.getBytes().length); @@ -677,26 +755,30 @@ public void testContainerLogsForFinishedApps() throws Exception { // specify the bytes which is larger than the actual file size, // we would get the full logs - r = resource(); - response = r.path("ws").path("v1") + target = target(); + response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containerlogs") .path(containerId1ForApp100.toString()).path(fileName) .queryParam("user.name", user) .queryParam("size", "10000") - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertThat(responseText.getBytes()).hasSize(fullTextSize); - r = resource(); - response = r.path("ws").path("v1") + target = target(); + response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containerlogs") .path(containerId1ForApp100.toString()).path(fileName) .queryParam("user.name", user) .queryParam("size", "-10000") - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertThat(responseText.getBytes()).hasSize(fullTextSize); } @@ -709,11 +791,14 @@ public void testContainerLogsForRunningApps() throws Exception { ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1); ContainerId containerId1 = ContainerId.newContainerId(appAttemptId, 1); - WebResource r = resource(); - URI requestURI = r.path("ws").path("v1") + WebTarget target = target(); + URI requestURI = target + .path("ws") + .path("v1") .path("applicationhistory").path("containerlogs") .path(containerId1.toString()).path(fileName) - .queryParam("user.name", user).getURI(); + .queryParam("user.name", user) + .getUri(); String redirectURL = getRedirectURL(requestURI.toString()); assertTrue(redirectURL != null); assertTrue(redirectURL.contains("test:1234")); @@ -724,12 +809,14 @@ public void testContainerLogsForRunningApps() throws Exception { // If we specify NM id, we would re-direct the request // to this NM's Web Address. - requestURI = r.path("ws").path("v1") + requestURI = target + .path("ws") + .path("v1") .path("applicationhistory").path("containerlogs") .path(containerId1.toString()).path(fileName) .queryParam("user.name", user) .queryParam(YarnWebServiceParams.NM_ID, NM_ID) - .getURI(); + .getUri(); redirectURL = getRedirectURL(requestURI.toString()); assertTrue(redirectURL != null); assertTrue(redirectURL.contains(NM_WEBADDRESS)); @@ -739,10 +826,13 @@ public void testContainerLogsForRunningApps() throws Exception { assertTrue(redirectURL.contains("user.name=" + user)); // Test with new API - requestURI = r.path("ws").path("v1") + requestURI = target + .path("ws") + .path("v1") .path("applicationhistory").path("containers") .path(containerId1.toString()).path("logs").path(fileName) - .queryParam("user.name", user).getURI(); + .queryParam("user.name", user) + .getUri(); redirectURL = getRedirectURL(requestURI.toString()); assertTrue(redirectURL != null); assertTrue(redirectURL.contains("test:1234")); @@ -751,12 +841,17 @@ public void testContainerLogsForRunningApps() throws Exception { assertTrue(redirectURL.contains("/logs/" + fileName)); assertTrue(redirectURL.contains("user.name=" + user)); - requestURI = r.path("ws").path("v1") - .path("applicationhistory").path("containers") - .path(containerId1.toString()).path("logs").path(fileName) + requestURI = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containers") + .path(containerId1.toString()) + .path("logs") + .path(fileName) .queryParam("user.name", user) .queryParam(YarnWebServiceParams.NM_ID, NM_ID) - .getURI(); + .getUri(); redirectURL = getRedirectURL(requestURI.toString()); assertTrue(redirectURL != null); assertTrue(redirectURL.contains(NM_WEBADDRESS)); @@ -774,14 +869,16 @@ public void testContainerLogsForRunningApps() throws Exception { TestContainerLogsUtils.createContainerLogFileInRemoteFS(conf, fs, rootLogDir, appId, Collections.singletonMap(containerId1000, content), nodeId, fileName, user, true); - r = resource(); - ClientResponse response = r.path("ws").path("v1") + target = target(); + Response response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containerlogs") .path(containerId1000.toString()).path(fileName) .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - String responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + String responseText = response.readEntity(String.class); assertTrue(responseText.contains(content)); // Also test whether we output the empty local container log, and give // the warning message. @@ -793,14 +890,16 @@ public void testContainerLogsForRunningApps() throws Exception { // If we can not container information from ATS, and we specify the NM id, // but we can not get nm web address, we would still try to // get aggregated log from remote FileSystem. - response = r.path("ws").path("v1") + response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containerlogs") .path(containerId1000.toString()).path(fileName) .queryParam(YarnWebServiceParams.NM_ID, "invalid-nm:1234") .queryParam("user.name", user) - .accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains(content)); assertTrue(responseText.contains("LogAggregationType: " + ContainerLogAggregationType.LOCAL)); @@ -814,13 +913,16 @@ public void testContainerLogsForRunningApps() throws Exception { TestContainerLogsUtils.createContainerLogFileInRemoteFS(conf, fs, rootLogDir, appId, Collections.singletonMap(containerId1, content1), nodeId1, fileName, user, true); - response = r.path("ws").path("v1") + response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containers") .path(containerId1.toString()).path("logs").path(fileName) .queryParam("user.name", user) .queryParam(YarnWebServiceParams.REDIRECTED_FROM_NODE, "true") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains(content1)); assertTrue(responseText.contains("LogAggregationType: " + ContainerLogAggregationType.AGGREGATED)); @@ -834,15 +936,17 @@ public void testContainerLogsMetaForRunningApps() throws Exception { ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 1); ContainerId containerId1 = ContainerId.newContainerId(appAttemptId, 1); - WebResource r = resource(); + WebTarget target = target(); // If we specify the NMID, we re-direct the request by using // the NM's web address - URI requestURI = r.path("ws").path("v1") + URI requestURI = target + .path("ws") + .path("v1") .path("applicationhistory").path("containers") .path(containerId1.toString()).path("logs") .queryParam("user.name", user) .queryParam(YarnWebServiceParams.NM_ID, NM_ID) - .getURI(); + .getUri(); String redirectURL = getRedirectURL(requestURI.toString()); assertTrue(redirectURL != null); assertTrue(redirectURL.contains(NM_WEBADDRESS)); @@ -853,10 +957,15 @@ public void testContainerLogsMetaForRunningApps() throws Exception { // If we do not specify the NodeId but can get Container information // from ATS, we re-direct the request to the node manager // who runs the container. - requestURI = r.path("ws").path("v1") - .path("applicationhistory").path("containers") - .path(containerId1.toString()).path("logs") - .queryParam("user.name", user).getURI(); + requestURI = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containers") + .path(containerId1.toString()) + .path("logs") + .queryParam("user.name", user) + .getUri(); redirectURL = getRedirectURL(requestURI.toString()); assertTrue(redirectURL != null); assertTrue(redirectURL.contains("test:1234")); @@ -875,15 +984,18 @@ public void testContainerLogsMetaForRunningApps() throws Exception { TestContainerLogsUtils.createContainerLogFileInRemoteFS(conf, fs, rootLogDir, appId, Collections.singletonMap(containerId1000, content), nodeId, fileName, user, true); - ClientResponse response = r.path("ws").path("v1") + Response response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containers") .path(containerId1000.toString()).path("logs") .queryParam("user.name", user) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); - List responseText = response.getEntity(new GenericType< - List>(){}); + List responseText = + response.readEntity(new GenericType>() { + }); assertTrue(responseText.size() == 2); for (ContainerLogsInfo logInfo : responseText) { if(logInfo.getLogType().equals( @@ -903,15 +1015,19 @@ public void testContainerLogsMetaForRunningApps() throws Exception { // If we can not container information from ATS, // and we specify NM id, but can not find NM WebAddress for this nodeId, // we would still try to get aggregated log meta from remote FileSystem. - response = r.path("ws").path("v1") - .path("applicationhistory").path("containers") - .path(containerId1000.toString()).path("logs") + response = target + .path("ws") + .path("v1") + .path("applicationhistory") + .path("containers") + .path(containerId1000.toString()) + .path("logs") .queryParam(YarnWebServiceParams.NM_ID, "invalid-nm:1234") .queryParam("user.name", user) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); - responseText = response.getEntity(new GenericType< - List>(){}); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); + responseText = response.readEntity(new GenericType>() { + }); assertTrue(responseText.size() == 2); for (ContainerLogsInfo logInfo : responseText) { if(logInfo.getLogType().equals( @@ -943,14 +1059,16 @@ public void testContainerLogsMetaForFinishedApps() throws Exception { rootLogDir, appId, Collections.singletonMap(containerId1, content), nodeId, fileName, user, true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") .path("applicationhistory").path("containers") .path(containerId1.toString()).path("logs") .queryParam("user.name", user) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); - List responseText = response.getEntity(new GenericType< + .request(MediaType.APPLICATION_JSON) + .get(Response.class); + List responseText = response.readEntity(new GenericType< List>(){}); assertTrue(responseText.size() == 1); assertEquals(responseText.get(0).getLogType(), diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServices.java index c8f9eb2d522cb..bebdbaf0cf56c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServices.java @@ -37,7 +37,10 @@ import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -68,21 +71,19 @@ import org.apache.hadoop.yarn.util.timeline.TimelineUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestTimelineWebServices extends JerseyTestBase { +public class TestTimelineWebServices extends JerseyTest { private static TimelineStore store; private static TimelineACLsManager timelineACLsManager; @@ -113,7 +114,7 @@ protected void configureServlets() { timelineDataManager.init(conf); timelineDataManager.start(); bind(TimelineDataManager.class).toInstance(timelineDataManager); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); TimelineAuthenticationFilter taFilter = new TimelineAuthenticationFilter(); FilterConfig filterConfig = mock(FilterConfig.class); @@ -174,27 +175,18 @@ private static TimelineStore mockTimelineStore() return store.getTimelineStore(); } - public TestTimelineWebServices() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.applicationhistoryservice.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter") - .servletPath("/") - .clientConfig( - new DefaultClientConfig(YarnJacksonJaxbJsonProvider.class)) - .build()); - } - @Test public void testAbout() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineAbout actualAbout = response.getEntity(TimelineAbout.class); + response.getMediaType().toString()); + TimelineAbout actualAbout = response.readEntity(TimelineAbout.class); TimelineAbout expectedAbout = TimelineUtils.createTimelineAbout("Timeline API"); Assert.assertNotNull( @@ -245,123 +237,158 @@ private static void verifyEntities(TimelineEntities entities) { @Test public void testGetEntities() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .path("type_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - verifyEntities(response.getEntity(TimelineEntities.class)); + response.getMediaType().toString()); + verifyEntities(response.readEntity(TimelineEntities.class)); } @Test public void testFromId() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").queryParam("fromId", "id_2") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .queryParam("fromId", "id_2") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(2, response.getEntity(TimelineEntities.class).getEntities() + response.getMediaType().toString()); + assertEquals(2, response.readEntity(TimelineEntities.class).getEntities() .size()); - response = r.path("ws").path("v1").path("timeline") - .path("type_1").queryParam("fromId", "id_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .queryParam("fromId", "id_1") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(3, response.getEntity(TimelineEntities.class).getEntities() + response.getMediaType().toString()); + assertEquals(3, response.readEntity(TimelineEntities.class).getEntities() .size()); } @Test public void testFromTs() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .path("type_1").queryParam("fromTs", Long.toString(beforeTime)) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(0, response.getEntity(TimelineEntities.class).getEntities() + response.getMediaType().toString()); + assertEquals(0, response.readEntity(TimelineEntities.class).getEntities() .size()); - response = r.path("ws").path("v1").path("timeline") + response = target + .path("ws") + .path("v1") + .path("timeline") .path("type_1").queryParam("fromTs", Long.toString( System.currentTimeMillis())) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(3, response.getEntity(TimelineEntities.class).getEntities() + response.getMediaType().toString()); + assertEquals(3, response.readEntity(TimelineEntities.class).getEntities() .size()); } @Test public void testPrimaryFilterString() { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").queryParam("primaryFilter", "user:username") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .queryParam("primaryFilter", "user:username") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - verifyEntities(response.getEntity(TimelineEntities.class)); + response.getMediaType().toString()); + verifyEntities(response.readEntity(TimelineEntities.class)); } @Test public void testPrimaryFilterInteger() { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").queryParam("primaryFilter", - "appname:" + Integer.toString(Integer.MAX_VALUE)) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .queryParam("primaryFilter", + "appname:" + Integer.MAX_VALUE) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - verifyEntities(response.getEntity(TimelineEntities.class)); + response.getMediaType().toString()); + verifyEntities(response.readEntity(TimelineEntities.class)); } @Test public void testPrimaryFilterLong() { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .path("type_1").queryParam("primaryFilter", - "long:" + Long.toString((long) Integer.MAX_VALUE + 1l)) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + "long:" + ((long) Integer.MAX_VALUE + 1L)) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - verifyEntities(response.getEntity(TimelineEntities.class)); + response.getMediaType().toString()); + verifyEntities(response.readEntity(TimelineEntities.class)); } @Test public void testSecondaryFilters() { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .path("type_1") .queryParam("secondaryFilter", - "user:username,appname:" + Integer.toString(Integer.MAX_VALUE)) - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + "user:username,appname:" + Integer.MAX_VALUE) + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - verifyEntities(response.getEntity(TimelineEntities.class)); + response.getMediaType().toString()); + verifyEntities(response.readEntity(TimelineEntities.class)); } @Test public void testGetEntity() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").path("id_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .path("id_1") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineEntity entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + TimelineEntity entity = response.readEntity(TimelineEntity.class); Assert.assertNotNull(entity); Assert.assertEquals("id_1", entity.getEntityId()); Assert.assertEquals("type_1", entity.getEntityType()); @@ -373,14 +400,19 @@ public void testGetEntity() throws Exception { @Test public void testGetEntityFields1() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").path("id_1").queryParam("fields", "events,otherinfo") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .path("id_1") + .queryParam("fields", "events,otherinfo") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineEntity entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + TimelineEntity entity = response.readEntity(TimelineEntity.class); Assert.assertNotNull(entity); Assert.assertEquals("id_1", entity.getEntityId()); Assert.assertEquals("type_1", entity.getEntityType()); @@ -392,15 +424,19 @@ public void testGetEntityFields1() throws Exception { @Test public void testGetEntityFields2() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").path("id_1").queryParam("fields", "lasteventonly," + - "primaryfilters,relatedentities") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .path("id_1") + .queryParam("fields", "lasteventonly,primaryfilters,relatedentities") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineEntity entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + TimelineEntity entity = response.readEntity(TimelineEntity.class); Assert.assertNotNull(entity); Assert.assertEquals("id_1", entity.getEntityId()); Assert.assertEquals("type_1", entity.getEntityType()); @@ -412,15 +448,19 @@ public void testGetEntityFields2() throws Exception { @Test public void testGetEvents() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("type_1").path("events") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("type_1") + .path("events") .queryParam("entityId", "id_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineEvents events = response.getEntity(TimelineEvents.class); + response.getMediaType().toString()); + TimelineEvents events = response.readEntity(TimelineEvents.class); Assert.assertNotNull(events); Assert.assertEquals(1, events.getAllEvents().size()); TimelineEvents.EventsOfOneEntity partEvents = events.getAllEvents().get(0); @@ -447,14 +487,17 @@ public void testPostEntitiesWithPrimaryFilter() throws Exception { entity.setEntityType("test type 6"); entity.setStartTime(System.currentTimeMillis()); entities.addEntity(entity); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "tester") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); TimelinePutResponse putResposne = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertEquals(0, putResposne.getErrors().size()); } @@ -467,35 +510,45 @@ public void testPostEntities() throws Exception { entity.setStartTime(System.currentTimeMillis()); entity.setDomainId("domain_id_1"); entities.addEntity(entity); - WebResource r = resource(); + WebTarget target = target(); // No owner, will be rejected - ClientResponse response = r.path("ws").path("v1").path("timeline") + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); - response = r.path("ws").path("v1").path("timeline") + response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "tester") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); TimelinePutResponse putResposne = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertNotNull(putResposne); Assert.assertEquals(0, putResposne.getErrors().size()); // verify the entity exists in the store - response = r.path("ws").path("v1").path("timeline") - .path("test type 1").path("test id 1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 1") + .path("test id 1") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + entity = response.readEntity(TimelineEntity.class); Assert.assertNotNull(entity); Assert.assertEquals("test id 1", entity.getEntityId()); Assert.assertEquals("test type 1", entity.getEntityType()); @@ -512,13 +565,18 @@ public void testPostIncompleteEntities() throws Exception { entities.addEntity(entity1); // Add an entity with no id or type. entities.addEntity(new TimelineEntity()); - WebResource r = resource(); + WebTarget target = target(); // One of the entities has no id or type. HTTP 400 will be returned - ClientResponse response = r.path("ws").path("v1").path("timeline") - .queryParam("user.name", "tester").accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON).post(ClientResponse.class, entities); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .queryParam("user.name", "tester") + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); } @@ -534,28 +592,34 @@ public void testPostEntitiesWithYarnACLsEnabled() throws Exception { entity.setStartTime(System.currentTimeMillis()); entity.setDomainId("domain_id_1"); entities.addEntity(entity); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_1") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); TimelinePutResponse putResponse = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertNotNull(putResponse); Assert.assertEquals(0, putResponse.getErrors().size()); // override/append timeline data in the same entity with different user - response = r.path("ws").path("v1").path("timeline") + response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_3") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - putResponse = response.getEntity(TimelinePutResponse.class); + response.getMediaType().toString()); + putResponse = response.readEntity(TimelinePutResponse.class); Assert.assertNotNull(putResponse); Assert.assertEquals(1, putResponse.getErrors().size()); Assert.assertEquals(TimelinePutResponse.TimelinePutError.ACCESS_DENIED, @@ -571,15 +635,18 @@ public void testPostEntitiesWithYarnACLsEnabled() throws Exception { entity.setRelatedEntities(Collections.singletonMap( "test type 2", Collections.singleton("test id 2"))); entities.addEntity(entity); - r = resource(); - response = r.path("ws").path("v1").path("timeline") + target = target(); + response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_3") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - putResponse = response.getEntity(TimelinePutResponse.class); + response.getMediaType().toString()); + putResponse = response.readEntity(TimelinePutResponse.class); Assert.assertNotNull(putResponse); Assert.assertEquals(1, putResponse.getErrors().size()); Assert.assertEquals(TimelinePutError.FORBIDDEN_RELATION, @@ -587,14 +654,18 @@ public void testPostEntitiesWithYarnACLsEnabled() throws Exception { // Make sure the entity has been added anyway even though the // relationship is been excluded - response = r.path("ws").path("v1").path("timeline") - .path("test type 2").path("test id 3") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 2") + .path("test id 3") .queryParam("user.name", "reader_user_3") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + entity = response.readEntity(TimelineEntity.class); Assert.assertNotNull(entity); Assert.assertEquals("test id 3", entity.getEntityId()); Assert.assertEquals("test type 2", entity.getEntityType()); @@ -614,27 +685,34 @@ public void testPostEntitiesToDefaultDomain() throws Exception { entity.setEntityType("test type 7"); entity.setStartTime(System.currentTimeMillis()); entities.addEntity(entity); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "anybody_1") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); TimelinePutResponse putResposne = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertNotNull(putResposne); Assert.assertEquals(0, putResposne.getErrors().size()); // verify the entity exists in the store - response = r.path("ws").path("v1").path("timeline") - .path("test type 7").path("test id 7") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 7") + .path("test id 7") .queryParam("user.name", "any_body_2") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + entity = response.readEntity(TimelineEntity.class); Assert.assertNotNull(entity); Assert.assertEquals("test id 7", entity.getEntityId()); Assert.assertEquals("test type 7", entity.getEntityType()); @@ -657,62 +735,81 @@ public void testGetEntityWithYarnACLsEnabled() throws Exception { entity.setStartTime(System.currentTimeMillis()); entity.setDomainId("domain_id_1"); entities.addEntity(entity); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_1") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); TimelinePutResponse putResponse = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertEquals(0, putResponse.getErrors().size()); // verify the system data will not be exposed // 1. No field specification - response = r.path("ws").path("v1").path("timeline") - .path("test type 3").path("test id 3") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 3") + .path("test id 3") .queryParam("user.name", "reader_user_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + entity = response.readEntity(TimelineEntity.class); Assert.assertNull(entity.getPrimaryFilters().get( TimelineStore.SystemFilter.ENTITY_OWNER.toString())); // 2. other field - response = r.path("ws").path("v1").path("timeline") - .path("test type 3").path("test id 3") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 3") + .path("test id 3") .queryParam("fields", "relatedentities") .queryParam("user.name", "reader_user_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + entity = response.readEntity(TimelineEntity.class); Assert.assertNull(entity.getPrimaryFilters().get( TimelineStore.SystemFilter.ENTITY_OWNER.toString())); // 3. primaryfilters field - response = r.path("ws").path("v1").path("timeline") - .path("test type 3").path("test id 3") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 3") + .path("test id 3") .queryParam("fields", "primaryfilters") .queryParam("user.name", "reader_user_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entity = response.getEntity(TimelineEntity.class); + response.getMediaType().toString()); + entity = response.readEntity(TimelineEntity.class); Assert.assertNull(entity.getPrimaryFilters().get( TimelineStore.SystemFilter.ENTITY_OWNER.toString())); // get entity with other user - response = r.path("ws").path("v1").path("timeline") - .path("test type 3").path("test id 3") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 3") + .path("test id 3") .queryParam("user.name", "reader_user_2") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); } finally { timelineACLsManager.setAdminACLsManager(oldAdminACLsManager); @@ -732,16 +829,19 @@ public void testGetEntitiesWithYarnACLsEnabled() { entity.setStartTime(System.currentTimeMillis()); entity.setDomainId("domain_id_1"); entities.addEntity(entity); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_1") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); TimelinePutResponse putResponse = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertEquals(0, putResponse.getErrors().size()); // Put entity [4, 5] in domain 2 @@ -752,26 +852,32 @@ public void testGetEntitiesWithYarnACLsEnabled() { entity.setStartTime(System.currentTimeMillis()); entity.setDomainId("domain_id_2"); entities.addEntity(entity); - r = resource(); - response = r.path("ws").path("v1").path("timeline") + target = target(); + response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_3") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - putResponse = response.getEntity(TimelinePutResponse.class); + response.getMediaType().toString()); + putResponse = response.readEntity(TimelinePutResponse.class); Assert.assertEquals(0, putResponse.getErrors().size()); // Query entities of type 4 - response = r.path("ws").path("v1").path("timeline") + response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "reader_user_1") .path("test type 4") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - entities = response.getEntity(TimelineEntities.class); + response.getMediaType().toString()); + entities = response.readEntity(TimelineEntities.class); // Reader 1 should just have the access to entity [4, 4] assertEquals(1, entities.getEntities().size()); assertEquals("test type 4", entities.getEntities().get(0).getEntityType()); @@ -798,16 +904,19 @@ public void testGetEventsWithYarnACLsEnabled() { event.setTimestamp(System.currentTimeMillis()); entity.addEvent(event); entities.addEntity(entity); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_1") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); TimelinePutResponse putResponse = - response.getEntity(TimelinePutResponse.class); + response.readEntity(TimelinePutResponse.class); Assert.assertEquals(0, putResponse.getErrors().size()); // Put entity [5, 6] in domain 2 @@ -822,27 +931,34 @@ public void testGetEventsWithYarnACLsEnabled() { event.setTimestamp(System.currentTimeMillis()); entity.addEvent(event); entities.addEntity(entity); - r = resource(); - response = r.path("ws").path("v1").path("timeline") + target = target(); + response = target + .path("ws") + .path("v1") + .path("timeline") .queryParam("user.name", "writer_user_3") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, entities); + .post(Entity.json(entities), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - putResponse = response.getEntity(TimelinePutResponse.class); + response.getMediaType().toString()); + putResponse = response.readEntity(TimelinePutResponse.class); Assert.assertEquals(0, putResponse.getErrors().size()); // Query events belonging to the entities of type 4 - response = r.path("ws").path("v1").path("timeline") - .path("test type 5").path("events") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("test type 5") + .path("events") .queryParam("user.name", "reader_user_1") .queryParam("entityId", "test id 5,test id 6") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineEvents events = response.getEntity(TimelineEvents.class); + response.getMediaType().toString()); + TimelineEvents events = response.readEntity(TimelineEvents.class); // Reader 1 should just have the access to the events of entity [5, 5] assertEquals(1, events.getAllEvents().size()); assertEquals("test id 5", events.getAllEvents().get(0).getEntityId()); @@ -853,14 +969,18 @@ public void testGetEventsWithYarnACLsEnabled() { @Test public void testGetDomain() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("domain").path("domain_id_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") + .path("domain_id_1") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineDomain domain = response.getEntity(TimelineDomain.class); + response.getMediaType().toString()); + TimelineDomain domain = response.readEntity(TimelineDomain.class); verifyDomain(domain, "domain_id_1"); } @@ -869,24 +989,32 @@ public void testGetDomainYarnACLsEnabled() { AdminACLsManager oldAdminACLsManager = timelineACLsManager.setAdminACLsManager(adminACLsManager); try { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") - .path("domain").path("domain_id_1") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") + .path("domain_id_1") .queryParam("user.name", "owner_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineDomain domain = response.getEntity(TimelineDomain.class); + response.getMediaType().toString()); + TimelineDomain domain = response.readEntity(TimelineDomain.class); verifyDomain(domain, "domain_id_1"); - response = r.path("ws").path("v1").path("timeline") - .path("domain").path("domain_id_1") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") + .path("domain_id_1") .queryParam("user.name", "tester") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); } finally { timelineACLsManager.setAdminACLsManager(oldAdminACLsManager); @@ -895,15 +1023,18 @@ public void testGetDomainYarnACLsEnabled() { @Test public void testGetDomains() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .path("domain") .queryParam("owner", "owner_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineDomains domains = response.getEntity(TimelineDomains.class); + response.getMediaType().toString()); + TimelineDomains domains = response.readEntity(TimelineDomains.class); Assert.assertEquals(2, domains.getDomains().size()); for (int i = 0; i < domains.getDomains().size(); ++i) { verifyDomain(domains.getDomains().get(i), @@ -916,30 +1047,36 @@ public void testGetDomainsYarnACLsEnabled() throws Exception { AdminACLsManager oldAdminACLsManager = timelineACLsManager.setAdminACLsManager(adminACLsManager); try { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("timeline") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") .path("domain") .queryParam("user.name", "owner_1") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - TimelineDomains domains = response.getEntity(TimelineDomains.class); + response.getMediaType().toString()); + TimelineDomains domains = response.readEntity(TimelineDomains.class); Assert.assertEquals(2, domains.getDomains().size()); for (int i = 0; i < domains.getDomains().size(); ++i) { verifyDomain(domains.getDomains().get(i), i == 0 ? "domain_id_4" : "domain_id_1"); } - response = r.path("ws").path("v1").path("timeline") + response = target + .path("ws") + .path("v1") + .path("timeline") .path("domain") .queryParam("owner", "owner_1") .queryParam("user.name", "tester") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - domains = response.getEntity(TimelineDomains.class); + response.getMediaType().toString()); + domains = response.readEntity(TimelineDomains.class); Assert.assertEquals(0, domains.getDomains().size()); } finally { timelineACLsManager.setAdminACLsManager(oldAdminACLsManager); @@ -950,33 +1087,43 @@ public void testGetDomainsYarnACLsEnabled() throws Exception { public void testPutDomain() throws Exception { TimelineDomain domain = new TimelineDomain(); domain.setId("test_domain_id"); - WebResource r = resource(); + WebTarget target = target(); // No owner, will be rejected - ClientResponse response = r.path("ws").path("v1") - .path("timeline").path("domain") + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, domain); + .put(Entity.json(domain), Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); - response = r.path("ws").path("v1") - .path("timeline").path("domain") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") .queryParam("user.name", "tester") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, domain); + .put(Entity.json(domain), Response.class); assertResponseStatusCode(Status.OK, response.getStatusInfo()); // Verify the domain exists - response = r.path("ws").path("v1").path("timeline") - .path("domain").path("test_domain_id") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") + .path("test_domain_id") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - domain = response.getEntity(TimelineDomain.class); + response.getMediaType().toString()); + domain = response.readEntity(TimelineDomain.class); Assert.assertNotNull(domain); Assert.assertEquals("test_domain_id", domain.getId()); Assert.assertEquals("tester", domain.getOwner()); @@ -984,22 +1131,29 @@ public void testPutDomain() throws Exception { // Update the domain domain.setDescription("test_description"); - response = r.path("ws").path("v1") - .path("timeline").path("domain") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") .queryParam("user.name", "tester") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, domain); + .put(Entity.json(domain), Response.class); assertResponseStatusCode(Status.OK, response.getStatusInfo()); // Verify the domain is updated - response = r.path("ws").path("v1").path("timeline") - .path("domain").path("test_domain_id") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") + .path("test_domain_id") + .request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - domain = response.getEntity(TimelineDomain.class); + response.getMediaType().toString()); + domain = response.readEntity(TimelineDomain.class); Assert.assertNotNull(domain); Assert.assertEquals("test_domain_id", domain.getId()); Assert.assertEquals("test_description", domain.getDescription()); @@ -1012,22 +1166,28 @@ public void testPutDomainYarnACLsEnabled() throws Exception { try { TimelineDomain domain = new TimelineDomain(); domain.setId("test_domain_id_acl"); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1") - .path("timeline").path("domain") + WebTarget target = target(); + Response response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") .queryParam("user.name", "tester") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, domain); + .put(Entity.json(domain), Response.class); assertResponseStatusCode(Status.OK, response.getStatusInfo()); // Update the domain by another user - response = r.path("ws").path("v1") - .path("timeline").path("domain") + response = target + .path("ws") + .path("v1") + .path("timeline") + .path("domain") .queryParam("user.name", "other") + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON) - .put(ClientResponse.class, domain); + .put(Entity.json(domain), Response.class); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); } finally { timelineACLsManager.setAdminACLsManager(oldAdminACLsManager); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServicesWithSSL.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServicesWithSSL.java index 36209a9be4bde..c6f0403f3d038 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServicesWithSSL.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestTimelineWebServicesWithSSL.java @@ -23,6 +23,9 @@ import java.net.URI; import java.util.EnumSet; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.security.UserGroupInformation; @@ -38,14 +41,13 @@ import org.apache.hadoop.yarn.server.timeline.MemoryTimelineStore; import org.apache.hadoop.yarn.server.timeline.TimelineReader.Field; import org.apache.hadoop.yarn.server.timeline.TimelineStore; + +import net.jodah.failsafe.RetryPolicy; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; - public class TestTimelineWebServicesWithSSL { private static final String BASEDIR = @@ -126,15 +128,14 @@ public void testPutEntities() throws Exception { private static class TestTimelineClient extends TimelineClientImpl { - private ClientResponse resp; + private Response resp; @Override - protected TimelineWriter createTimelineWriter(Configuration conf, - UserGroupInformation authUgi, Client client, URI resURI) - throws IOException { - return new DirectTimelineWriter(authUgi, client, resURI) { + protected TimelineWriter createTimelineWriter(Configuration conf, UserGroupInformation authUgi, + Client client, URI resURI, RetryPolicy retryPolicy) throws IOException { + return new DirectTimelineWriter(authUgi, client, resURI, retryPolicy) { @Override - public ClientResponse doPostingObject(Object obj, String path) { + public Response doPostingObject(Object obj, String path) { resp = super.doPostingObject(obj, path); return resp; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/pom.xml index 3bf70bf7560e5..a636e01d22314 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/pom.xml @@ -136,6 +136,10 @@ org.ehcache ehcache + + javax.ws.rs + javax.ws.rs-api + com.zaxxer HikariCP diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/dao/WeightedPolicyInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/dao/WeightedPolicyInfo.java index e7b8afe149058..16f78e8873bfd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/dao/WeightedPolicyInfo.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/dao/WeightedPolicyInfo.java @@ -17,34 +17,28 @@ package org.apache.hadoop.yarn.server.federation.policies.dao; -import java.io.StringReader; -import java.io.StringWriter; +import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; import org.apache.commons.collections.CollectionUtils; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.yarn.server.federation.policies.exceptions.FederationPolicyInitializationException; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterIdInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONMarshaller; -import com.sun.jersey.api.json.JSONUnmarshaller; /** - * This is a DAO class for the configuration of parameteres for federation + * This is a DAO class for the configuration of parameters for federation * policies. This generalizes several possible configurations as two lists of * {@link SubClusterIdInfo} and corresponding weights as a {@link Float}. The * interpretation of the weight is left to the logic in the policy. @@ -56,25 +50,16 @@ @XmlAccessorType(XmlAccessType.FIELD) public class WeightedPolicyInfo { - private static final Logger LOG = - LoggerFactory.getLogger(WeightedPolicyInfo.class); - private static JSONJAXBContext jsonjaxbContext = initContext(); + private static final ObjectWriter writer = new ObjectMapper() + .writerFor(WeightedPolicyInfo.class).withDefaultPrettyPrinter(); + private static final ObjectReader reader = + new ObjectMapper().readerFor(WeightedPolicyInfo.class); private Map routerPolicyWeights = new HashMap<>(); private Map amrmPolicyWeights = new HashMap<>(); private float headroomAlpha; public WeightedPolicyInfo() { - // JAXB needs this - } - - private static JSONJAXBContext initContext() { - try { - return new JSONJAXBContext(JSONConfiguration.DEFAULT, - WeightedPolicyInfo.class); - } catch (JAXBException e) { - LOG.error("Error parsing the policy.", e); - } - return null; + // Jackson requires this } /** @@ -91,22 +76,18 @@ private static JSONJAXBContext initContext() { public static WeightedPolicyInfo fromByteBuffer(ByteBuffer bb) throws FederationPolicyInitializationException { - if (jsonjaxbContext == null) { + if (reader == null || writer == null) { throw new FederationPolicyInitializationException( - "JSONJAXBContext should" + " not be null."); + "ObjectWriter and ObjectReader are not initialized."); } try { - JSONUnmarshaller unmarshaller = jsonjaxbContext.createJSONUnmarshaller(); final byte[] bytes = new byte[bb.remaining()]; bb.get(bytes); String params = new String(bytes, StandardCharsets.UTF_8); - - WeightedPolicyInfo weightedPolicyInfo = unmarshaller.unmarshalFromJSON( - new StringReader(params), WeightedPolicyInfo.class); - return weightedPolicyInfo; - } catch (JAXBException j) { - throw new FederationPolicyInitializationException(j); + return reader.readValue(params); + } catch (IOException e) { + throw new FederationPolicyInitializationException(e); } } @@ -158,24 +139,20 @@ public void setAMRMPolicyWeights(Map policyWeights) { */ public ByteBuffer toByteBuffer() throws FederationPolicyInitializationException { - if (jsonjaxbContext == null) { + if (reader == null || writer == null) { throw new FederationPolicyInitializationException( - "JSONJAXBContext should" + " not be null."); + "ObjectWriter and ObjectReader are not initialized."); } try { String s = toJSONString(); return ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)); - } catch (JAXBException j) { - throw new FederationPolicyInitializationException(j); + } catch (JsonProcessingException e) { + throw new FederationPolicyInitializationException(e); } } - private String toJSONString() throws JAXBException { - JSONMarshaller marshaller = jsonjaxbContext.createJSONMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - StringWriter sw = new StringWriter(256); - marshaller.marshallToJSON(this, sw); - return sw.toString(); + private String toJSONString() throws JsonProcessingException { + return writer.writeValueAsString(this); } @Override @@ -243,7 +220,7 @@ public void setHeadroomAlpha(float headroomAlpha) { public String toString() { try { return toJSONString(); - } catch (JAXBException e) { + } catch (JsonProcessingException e) { e.printStackTrace(); return "Error serializing to string."; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/store/records/SubClusterIdInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/store/records/SubClusterIdInfo.java index e2260a1f457fd..c722701adb703 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/store/records/SubClusterIdInfo.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/store/records/SubClusterIdInfo.java @@ -72,4 +72,8 @@ public int hashCode() { return id.hashCode(); } + @Override + public String toString() { + return id; + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogServlet.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogServlet.java index 86b8d55adc65f..3b6d59869f0fe 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogServlet.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogServlet.java @@ -20,8 +20,6 @@ import org.apache.hadoop.classification.VisibleForTesting; import org.apache.hadoop.thirdparty.com.google.common.base.Joiner; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.UniformInterfaceException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.security.UserGroupInformation; @@ -48,6 +46,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.GenericEntity; import javax.ws.rs.core.Response; @@ -90,7 +89,7 @@ private LogAggregationFileControllerFactory getOrCreateFactory() { @VisibleForTesting public String getNMWebAddressFromRM(String nodeId) - throws ClientHandlerException, UniformInterfaceException, JSONException { + throws ProcessingException, IllegalStateException, JSONException { return LogWebServiceUtils.getNMWebAddressFromRM(getConf(), nodeId); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebService.java index 1edfd5287ac41..3a92b508ebd98 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebService.java @@ -21,13 +21,7 @@ import org.apache.hadoop.classification.VisibleForTesting; import org.apache.hadoop.thirdparty.com.google.common.base.Joiner; import com.google.inject.Singleton; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; -import com.sun.jersey.core.util.MultivaluedMapImpl; + import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.conf.Configuration; @@ -44,6 +38,8 @@ import org.apache.hadoop.yarn.server.metrics.ContainerMetricsConstants; import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,13 +51,16 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import java.io.IOException; import java.net.HttpURLConnection; -import java.net.URL; import java.security.PrivilegedExceptionAction; /** @@ -104,25 +103,22 @@ public LogWebService() { } private Client createTimelineWebClient() { - ClientConfig cfg = new DefaultClientConfig(); - cfg.getClasses().add(YarnJacksonJaxbJsonProvider.class); - Client client = new Client( - new URLConnectionClientHandler(new HttpURLConnectionFactory() { - @Override public HttpURLConnection getHttpURLConnection(URL url) - throws IOException { - AuthenticatedURL.Token token = new AuthenticatedURL.Token(); - HttpURLConnection conn = null; - try { - conn = new AuthenticatedURL().openConnection(url, token); - LOG.info("LogWeService:Connecetion created."); - } catch (AuthenticationException e) { - throw new IOException(e); - } - return conn; + ClientConfig cfg = new ClientConfig(); + cfg.register(YarnJacksonJaxbJsonProvider.class); + cfg.connectorProvider(new HttpUrlConnectorProvider().connectionFactory( + url -> { + AuthenticatedURL.Token token = new AuthenticatedURL.Token(); + HttpURLConnection conn = null; + try { + conn = new AuthenticatedURL().openConnection(url, token); + LOG.info("LogWeService:Connecetion created."); + } catch (AuthenticationException e) { + throw new IOException(e); } - }), cfg); - - return client; + return conn; + } + )); + return ClientBuilder.newClient(cfg); } private void initForReadableEndpoints(HttpServletResponse response) { @@ -166,7 +162,7 @@ public String getNodeHttpAddress(HttpServletRequest req, String appId, String appAttemptId, String containerId, String clusterId) { UserGroupInformation callerUGI = LogWebServiceUtils.getUser(req); String cId = clusterId != null ? clusterId : defaultClusterid; - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", "INFO"); String path = JOINER.join("clusters/", cId, "/apps/", appId, "/entities/", TimelineEntityType.YARN_CONTAINER.toString(), "/", containerId); @@ -199,7 +195,7 @@ public BasicAppInfo getApp(HttpServletRequest req, String appId, UserGroupInformation callerUGI = LogWebServiceUtils.getUser(req); String cId = clusterId != null ? clusterId : defaultClusterid; - MultivaluedMap params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("fields", "INFO"); String path = JOINER.join("clusters/", cId, "/apps/", appId); TimelineEntity appEntity = null; @@ -292,22 +288,20 @@ public Response getLogs(@Context HttpServletRequest req, @VisibleForTesting protected TimelineEntity getEntity(String path, MultivaluedMap params) throws IOException { - ClientResponse resp = - getClient().resource(base).path(path).queryParams(params) - .accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); - if (resp == null - || resp.getStatusInfo().getStatusCode() != ClientResponse.Status.OK - .getStatusCode()) { + WebTarget t = getClient().target(base).path(path); + params.forEach(t::queryParam); + Response resp = t.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON) + .get(Response.class); + if (resp == null || resp.getStatus() != Response.Status.OK.getStatusCode()) { String msg = "Response from the timeline reader server is " + ((resp == null) ? "null" : "not successful," + " HTTP error code: " + resp.getStatus() - + ", Server response:\n" + resp.getEntity(String.class)); + + ", Server response:\n" + resp.readEntity(String.class)); LOG.error(msg); throw new IOException(msg); } - TimelineEntity entity = resp.getEntity(TimelineEntity.class); + TimelineEntity entity = resp.readEntity(TimelineEntity.class); return entity; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebServiceUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebServiceUtils.java index c4568be8011a7..e37e1cd00d9ef 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebServiceUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/webapp/LogWebServiceUtils.java @@ -19,8 +19,6 @@ package org.apache.hadoop.yarn.server.webapp; import org.apache.hadoop.thirdparty.com.google.common.base.Joiner; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.UniformInterfaceException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.conf.Configuration; @@ -40,6 +38,7 @@ import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; @@ -188,7 +187,7 @@ protected static UserGroupInformation getUser(HttpServletRequest req) { public static String getNMWebAddressFromRM(Configuration yarnConf, String nodeId) - throws ClientHandlerException, UniformInterfaceException, JSONException { + throws ProcessingException, IllegalStateException, JSONException { JSONObject nodeInfo = YarnWebServiceUtils.getNodeInfoFromRMWebService(yarnConf, nodeId) .getJSONObject("node"); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/pom.xml index 765ec0fcfe6bb..52c96a16956a2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/pom.xml @@ -73,11 +73,11 @@ commons-codec - com.sun.jersey - jersey-core + org.glassfish.jersey.core + jersey-common - com.sun.jersey + org.glassfish.jersey.core jersey-client @@ -147,31 +147,13 @@ guice - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 + org.glassfish.jersey.test-framework + jersey-test-framework-core test - com.github.pjfanning - jersey-json - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - - - com.sun.jersey.contribs - jersey-guice + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 @@ -200,6 +182,11 @@ test-jar test + + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/JAXBContextResolver.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/JAXBContextResolver.java index 89a5a7b12530f..b3ee689e21fc1 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/JAXBContextResolver.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/JAXBContextResolver.java @@ -22,8 +22,6 @@ import java.util.HashSet; import java.util.Arrays; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; import com.google.inject.Singleton; import javax.ws.rs.ext.ContextResolver; @@ -38,6 +36,7 @@ import org.apache.hadoop.yarn.server.nodemanager.webapp.dao.ContainersInfo; import org.apache.hadoop.yarn.server.nodemanager.webapp.dao.NodeInfo; import org.apache.hadoop.yarn.webapp.RemoteExceptionData; +import org.glassfish.jersey.jettison.JettisonJaxbContext; @Singleton @Provider @@ -53,11 +52,8 @@ public class JAXBContextResolver implements ContextResolver { RemoteExceptionData.class}; public JAXBContextResolver() throws Exception { - this.types = new HashSet(Arrays.asList(cTypes)); - // sets the json configuration so that the json output looks like - // the xml output - this.context = new JSONJAXBContext(JSONConfiguration.natural(). - rootUnwrapping(false).build(), cTypes); + this.types = new HashSet<>(Arrays.asList(cTypes)); + this.context = new JettisonJaxbContext(cTypes); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/NMWebAppFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/NMWebAppFilter.java index 961ea7d4c29b5..cd64562aa8a21 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/NMWebAppFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/NMWebAppFilter.java @@ -19,15 +19,19 @@ package org.apache.hadoop.yarn.server.nodemanager.webapp; import java.io.IOException; -import java.io.PrintWriter; import java.util.List; import javax.inject.Inject; import javax.inject.Singleton; +import javax.servlet.Filter; import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.google.inject.Injector; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.HtmlQuoting; import org.apache.hadoop.yarn.api.records.ApplicationId; @@ -36,13 +40,11 @@ import org.apache.hadoop.yarn.server.nodemanager.Context; import org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application; import org.apache.hadoop.yarn.webapp.Controller.RequestContext; -import com.google.inject.Injector; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; import org.apache.http.NameValuePair; @Singleton -public class NMWebAppFilter extends GuiceContainer{ +public class NMWebAppFilter implements Filter { private Injector injector; private Context nmContext; @@ -51,26 +53,26 @@ public class NMWebAppFilter extends GuiceContainer{ @Inject public NMWebAppFilter(Injector injector, Context nmContext) { - super(injector); this.injector = injector; this.nmContext = nmContext; } @Override - public void doFilter(HttpServletRequest request, - HttpServletResponse response, FilterChain chain) throws IOException, - ServletException { + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest servletRequest, + ServletResponse servletResponse, + FilterChain chain) throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + String redirectPath = containerLogPageRedirectPath(request); if (redirectPath != null) { - String redirectMsg = - "Redirecting to log server" + " : " + redirectPath; - PrintWriter out = response.getWriter(); - out.println(redirectMsg); - response.setHeader("Location", redirectPath); - response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); - return; + response.sendRedirect(redirectPath); } - super.doFilter(request, response, chain); + chain.doFilter(request, response); } private String containerLogPageRedirectPath(HttpServletRequest request) { @@ -89,7 +91,7 @@ private String containerLogPageRedirectPath(HttpServletRequest request) { try { containerId = ContainerId.fromString(containerIdStr); } catch (IllegalArgumentException ex) { - return redirectPath; + return null; } ApplicationId appId = containerId.getApplicationAttemptId().getApplicationId(); @@ -142,4 +144,8 @@ private String containerLogPageRedirectPath(HttpServletRequest request) { } return redirectPath; } + + @Override + public void destroy() { + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/WebServer.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/WebServer.java index eab20accf2c68..ab1055ccfd1bf 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/WebServer.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/webapp/WebServer.java @@ -39,7 +39,7 @@ import org.apache.hadoop.yarn.webapp.YarnWebParams; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; +import javax.servlet.Filter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -182,7 +182,7 @@ public void setup() { } @Override - protected Class getWebAppFilterClass() { + protected Class getWebAppFilterClass() { return NMWebAppFilter.class; } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java index 30b73c1acc7ab..f62e2b533fec8 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServices.java @@ -18,22 +18,22 @@ package org.apache.hadoop.yarn.server.nodemanager.webapp; -import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableMap; +import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; +import com.google.inject.servlet.GuiceServletContextListener; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.GenericType; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; + import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.Path; import org.apache.hadoop.http.JettyUtils; +import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableMap; import org.apache.hadoop.util.VersionInfo; import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; import org.apache.hadoop.yarn.api.records.ApplicationId; @@ -69,22 +69,37 @@ import org.apache.hadoop.yarn.util.YarnVersionInfo; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebApp; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.apache.hadoop.yarn.webapp.util.WebAppUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.hk2.api.ServiceLocatorFactory; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.DeploymentContext; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.ServletDeploymentContext; +import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; +import org.glassfish.jersey.test.spi.TestContainerFactory; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import org.jvnet.hk2.guice.bridge.api.GuiceBridge; +import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; @@ -95,6 +110,7 @@ import java.net.URL; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -111,7 +127,7 @@ /** * Test the nodemanager node info web services api's */ -public class TestNMWebServices extends JerseyTestBase { +public class TestNMWebServices extends JerseyTest { private static final long NM_RESOURCE_VALUE = 1000L; private static NodeManager.NMContext nmContext; @@ -129,6 +145,47 @@ public class TestNMWebServices extends JerseyTestBase { private static File testRemoteLogDir = new File("target", TestNMWebServices.class.getSimpleName() + "remote-log-dir"); + @Override + protected TestContainerFactory getTestContainerFactory() { + return new GrizzlyWebTestContainerFactory(); + } + + @Override + protected DeploymentContext configureDeployment() { + return ServletDeploymentContext + .forPackages("org.apache.hadoop.yarn.server.nodemanager.webapp") + .addListener(GuiceServletContextListener.class) + .filterClass(GuiceFilter.class) + .contextPath("jersey-guice-filter") + .servletPath("/") + .build(); + + } + + /* + @Override + protected Application configure() { + return new MyApplication(ServiceLocatorFactory.getInstance() + .create("test")).packages(); + }*/ + + static { + GuiceServletConfig.setInjector( + Guice.createInjector(new WebServletModule())); + } + + @ApplicationPath("/") + private class MyApplication extends ResourceConfig { + @Inject + public MyApplication(ServiceLocator serviceLocator) { + GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator); + GuiceIntoHK2Bridge guiceBridge = serviceLocator + .getService(GuiceIntoHK2Bridge.class); + guiceBridge.bridgeGuiceInjector(Guice.createInjector( + new WebServletModule())); + } + } + private static class WebServletModule extends ServletModule { @Override @@ -184,14 +241,8 @@ public boolean isPmemCheckEnabled() { bind(ResourceView.class).toInstance(resourceView); bind(ApplicationACLsManager.class).toInstance(aclsManager); bind(LocalDirsHandlerService.class).toInstance(dirsHandler); - - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } - }; - - static { - GuiceServletConfig.setInjector( - Guice.createInjector(new WebServletModule())); } private void setupMockPluginsWithNmResourceInfo() throws YarnException { @@ -250,42 +301,44 @@ private ResourcePluginManager createResourceManagerWithPlugins( return pluginManager; } - private void assertNMResourceInfoResponse(ClientResponse response, long value) + private void assertNMResourceInfoResponse(Response response, long value) throws JSONException { assertEquals("MediaType of the response is not the expected!", MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("Unexpected value in the json response!", (int) value, json.get("a")); } - private void assertEmptyNMResourceInfo(ClientResponse response) { + private void assertEmptyNMResourceInfo(Response response) { assertEquals("MediaType of the response is not the expected!", MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("Unexpected value in the json response!", 0, json.length()); } - private ClientResponse getNMResourceResponse(WebResource resource, - String resourceName) { - return resource.path("ws").path("v1").path("node").path("resources") - .path(resourceName).accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + private Response getNMResourceResponse(WebTarget target, + String resourceName) { + return target.path("ws").path("v1").path("node").path("resources") + .path(resourceName).request(MediaType.APPLICATION_JSON) + .get(Response.class); } - - @Before @Override public void setUp() throws Exception { super.setUp(); + GuiceServletConfig.setInjector( + Guice.createInjector(new WebServletModule())); + } + + @Before + public void createTesetDir() throws Exception { testRemoteLogDir.mkdir(); testRootDir.mkdirs(); testLogDir.mkdir(); - GuiceServletConfig.setInjector( - Guice.createInjector(new WebServletModule())); } @AfterClass @@ -295,41 +348,34 @@ static public void stop() { FileUtil.fullyDelete(testRemoteLogDir); } - public TestNMWebServices() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.nodemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); - } - @Test - public void testInvalidUri() throws JSONException, Exception { - WebResource r = resource(); + public void testInvalidUri() { + WebTarget t = target(); String responseStr = ""; try { - responseStr = r.path("ws").path("v1").path("node").path("bogus") - .accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = t.path("ws").path("v1").path("node").path("bogus") + .request(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, + response.getStatusInfo()); WebServicesTestUtils.checkStringMatch( "error string exists and shouldn't", "", responseStr); } } @Test - public void testInvalidAccept() throws JSONException, Exception { - WebResource r = resource(); + public void testInvalidAccept() { + WebTarget t = target(); String responseStr = ""; try { - responseStr = r.path("ws").path("v1").path("node") - .accept(MediaType.TEXT_PLAIN).get(String.class); + responseStr = t.path("ws").path("v1").path("node") + .request(MediaType.TEXT_PLAIN).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.INTERNAL_SERVER_ERROR, + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.INTERNAL_SERVER_ERROR, response.getStatusInfo()); WebServicesTestUtils.checkStringMatch( "error string exists and shouldn't", "", responseStr); @@ -337,101 +383,102 @@ public void testInvalidAccept() throws JSONException, Exception { } @Test - public void testInvalidUri2() throws JSONException, Exception { - WebResource r = resource(); + public void testInvalidUri2() { + WebTarget t = target(); String responseStr = ""; try { - responseStr = r.accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = t.request(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, + response.getStatusInfo()); WebServicesTestUtils.checkStringMatch( "error string exists and shouldn't", "", responseStr); } } @Test - public void testNode() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + public void testNode() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeInfo(json); } @Test - public void testNodeSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + public void testNodeSlash() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node/") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeInfo(json); } // make sure default is json output @Test - public void testNodeDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node") - .get(ClientResponse.class); + public void testNodeDefault() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node") + .request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeInfo(json); } @Test - public void testNodeInfo() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node").path("info") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + public void testNodeInfo() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node").path("info") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeInfo(json); } @Test - public void testNodeInfoSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node") - .path("info/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + public void testNodeInfoSlash() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node") + .path("info/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeInfo(json); } // make sure default is json output @Test - public void testNodeInfoDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node").path("info") - .get(ClientResponse.class); + public void testNodeInfoDefault() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node").path("info") + .request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeInfo(json); } @Test - public void testSingleNodesXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node") - .path("info/").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + public void testSingleNodesXML() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node") + .path("info/").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML+ "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -445,25 +492,25 @@ public void testSingleNodesXML() throws JSONException, Exception { @Test (timeout = 5000) public void testContainerLogsWithNewAPI() throws Exception { ContainerId containerId0 = BuilderUtils.newContainerId(0, 0, 0, 0); - WebResource r0 = resource(); - r0 = r0.path("ws").path("v1").path("node").path("containers") + WebTarget t0 = target(); + t0 = t0.path("ws").path("v1").path("node").path("containers") .path(containerId0.toString()).path("logs"); - testContainerLogs(r0, containerId0, LOG_MESSAGE); + testContainerLogs(t0, containerId0, LOG_MESSAGE); ContainerId containerId1 = BuilderUtils.newContainerId(0, 0, 0, 1); - WebResource r1 = resource(); - r1 = r1.path("ws").path("v1").path("node").path("containers") + WebTarget t1 = target(); + t1 = t1.path("ws").path("v1").path("node").path("containers") .path(containerId1.toString()).path("logs"); - testContainerLogs(r1, containerId1, ""); + testContainerLogs(t1, containerId1, ""); } @Test (timeout = 5000) public void testContainerLogsWithOldAPI() throws Exception { final ContainerId containerId2 = BuilderUtils.newContainerId(1, 1, 0, 2); - WebResource r = resource(); - r = r.path("ws").path("v1").path("node").path("containerlogs") + WebTarget t = target(); + t = t.path("ws").path("v1").path("node").path("containerlogs") .path(containerId2.toString()); - testContainerLogs(r, containerId2, LOG_MESSAGE); + testContainerLogs(t, containerId2, LOG_MESSAGE); } @Test (timeout = 10000) @@ -475,16 +522,16 @@ public void testNMRedirect() { ContainerId noExistContainerId = ContainerId.newContainerId( noExistAttemptId, 250); String fileName = "syslog"; - WebResource r = resource(); + WebTarget t = target(); // check the old api - URI requestURI = r.path("ws").path("v1").path("node") + URI requestURI = t.path("ws").path("v1").path("node") .path("containerlogs").path(noExistContainerId.toString()) .path(fileName).queryParam("user.name", "user") .queryParam(YarnWebServiceParams.NM_ID, "localhost:1111") - .getURI(); + .getUri(); String redirectURL = getRedirectURL(requestURI.toString()); - assertTrue(redirectURL != null); + assertThat(redirectURL).isNotNull(); assertTrue(redirectURL.contains(LOGSERVICEWSADDR)); assertTrue(redirectURL.contains(noExistContainerId.toString())); assertTrue(redirectURL.contains("/logs/" + fileName)); @@ -494,13 +541,13 @@ public void testNMRedirect() { assertFalse(redirectURL.contains(YarnWebServiceParams.NM_ID)); // check the new api - requestURI = r.path("ws").path("v1").path("node") + requestURI = t.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(); + .getUri(); redirectURL = getRedirectURL(requestURI.toString()); - assertTrue(redirectURL != null); + assertThat(redirectURL).isNotNull(); assertTrue(redirectURL.contains(LOGSERVICEWSADDR)); assertTrue(redirectURL.contains(noExistContainerId.toString())); assertTrue(redirectURL.contains("/logs/" + fileName)); @@ -509,13 +556,13 @@ public void testNMRedirect() { YarnWebServiceParams.REDIRECTED_FROM_NODE + "=true")); assertFalse(redirectURL.contains(YarnWebServiceParams.NM_ID)); - requestURI = r.path("ws").path("v1").path("node") + requestURI = t.path("ws").path("v1").path("node") .path("containers").path(noExistContainerId.toString()) .path("logs").queryParam("user.name", "user") .queryParam(YarnWebServiceParams.NM_ID, "localhost:1111") - .getURI(); + .getUri(); redirectURL = getRedirectURL(requestURI.toString()); - assertTrue(redirectURL != null); + assertThat(redirectURL).isNotNull(); assertTrue(redirectURL.contains(LOGSERVICEWSADDR)); assertTrue(redirectURL.contains(noExistContainerId.toString())); assertTrue(redirectURL.contains("user.name=" + "user")); @@ -529,8 +576,8 @@ public void testGetNMResourceInfoSuccessful() throws YarnException, JSONException { setupMockPluginsWithNmResourceInfo(); - WebResource r = resource(); - ClientResponse response = getNMResourceResponse(r, "resource-1"); + WebTarget t = target(); + Response response = getNMResourceResponse(t, "resource-1"); assertNMResourceInfoResponse(response, NM_RESOURCE_VALUE); } @@ -540,8 +587,8 @@ public void testGetNMResourceInfoEncodedIsSuccessful() setupMockPluginsWithNmResourceInfo(); //test encoded yarn.io/resource-1 path - WebResource r = resource(); - ClientResponse response = getNMResourceResponse(r, "yarn.io%2Fresource-1"); + WebTarget t = target(); + Response response = getNMResourceResponse(t, "yarn.io%2Fresource-1"); assertNMResourceInfoResponse(response, NM_RESOURCE_VALUE); } @@ -550,8 +597,8 @@ public void testGetNMResourceInfoFailBecauseOfEmptyResourceInfo() throws YarnException { setupMockPluginsWithNmResourceInfo(); - WebResource r = resource(); - ClientResponse response = getNMResourceResponse(r, "resource-2"); + WebTarget t = target(); + Response response = getNMResourceResponse(t, "resource-2"); assertEmptyNMResourceInfo(response); } @@ -560,8 +607,8 @@ public void testGetNMResourceInfoWhenPluginIsUnknown() throws YarnException { setupMockPluginsWithNmResourceInfo(); - WebResource r = resource(); - ClientResponse response = getNMResourceResponse(r, "resource-3"); + WebTarget t = target(); + Response response = getNMResourceResponse(t, "resource-3"); assertEmptyNMResourceInfo(response); } @@ -577,12 +624,12 @@ public void testGetYarnGpuResourceInfo() throws YarnException, JSONException { setupMockPluginsWithGpuResourceInfo(); - WebResource r = resource(); - ClientResponse response = getNMResourceResponse(r, "resource-1"); + WebTarget t = target(); + Response response = getNMResourceResponse(t, "resource-1"); assertEquals("MediaType of the response is not the expected!", MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("Unexpected driverVersion in the json response!", "1.2.3", json.getJSONObject("gpuDeviceInformation").get("driverVersion")); @@ -592,7 +639,7 @@ public void testGetYarnGpuResourceInfo() 2, json.getJSONArray("assignedGpuDevices").length()); } - private void testContainerLogs(WebResource r, ContainerId containerId, + private void testContainerLogs(WebTarget t, ContainerId containerId, String logMessage) throws Exception { final String containerIdStr = containerId.toString(); final ApplicationAttemptId appAttemptId = containerId @@ -624,9 +671,9 @@ private void testContainerLogs(WebResource r, ContainerId containerId, pw.close(); // ask for it - ClientResponse response = r.path(filename) - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - String responseText = response.getEntity(String.class); + Response response = t.path(filename) + .request(MediaType.TEXT_PLAIN).get(Response.class); + String responseText = response.readEntity(String.class); String responseLogMessage = getLogContext(responseText); assertEquals(logMessage, responseLogMessage); int fullTextSize = responseLogMessage.getBytes().length; @@ -634,10 +681,10 @@ private void testContainerLogs(WebResource r, ContainerId containerId, // specify how many bytes we should get from logs // specify a position number, it would get the first n bytes from // container log - response = r.path(filename) + response = t.path(filename) .queryParam("size", "5") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); responseLogMessage = getLogContext(responseText); int truncatedLength = Math.min(5, logMessage.getBytes().length); assertEquals(truncatedLength, responseLogMessage.getBytes().length); @@ -647,20 +694,20 @@ private void testContainerLogs(WebResource r, ContainerId containerId, // specify the bytes which is larger than the actual file size, // we would get the full logs - response = r.path(filename) + response = t.path(filename) .queryParam("size", "10000") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); responseLogMessage = getLogContext(responseText); assertEquals(fullTextSize, responseLogMessage.getBytes().length); assertEquals(logMessage, responseLogMessage); // specify a negative number, it would get the last n bytes from // container log - response = r.path(filename) + response = t.path(filename) .queryParam("size", "-5") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); responseLogMessage = getLogContext(responseText); assertEquals(truncatedLength, responseLogMessage.getBytes().length); assertEquals(new String(logMessage.getBytes(), @@ -668,57 +715,57 @@ private void testContainerLogs(WebResource r, ContainerId containerId, responseLogMessage); assertTrue(fullTextSize >= responseLogMessage.getBytes().length); - response = r.path(filename) + response = t.path(filename) .queryParam("size", "-10000") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); responseLogMessage = getLogContext(responseText); - assertEquals("text/plain; charset=utf-8", response.getType().toString()); + assertEquals("text/plain; charset=utf-8", response.getMediaType().toString()); assertEquals(fullTextSize, responseLogMessage.getBytes().length); assertEquals(logMessage, responseLogMessage); // ask and download it - response = r.path(filename) + response = t.path(filename) .queryParam("format", "octet-stream") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); responseLogMessage = getLogContext(responseText); assertEquals(logMessage, responseLogMessage); assertEquals(200, response.getStatus()); assertEquals("application/octet-stream; charset=utf-8", - response.getType().toString()); + response.getMediaType().toString()); // specify a invalid format value - response = r.path(filename) + response = t.path(filename) .queryParam("format", "123") - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); assertEquals("The valid values for the parameter : format are " + WebAppUtils.listSupportedLogContentType(), responseText); assertEquals(400, response.getStatus()); // ask for file that doesn't exist and it will re-direct to // the log server - URI requestURI = r.path("uhhh").getURI(); + URI requestURI = t.path("uhhh").getUri(); String redirectURL = getRedirectURL(requestURI.toString()); - assertTrue(redirectURL != null); + assertThat(redirectURL).isNotNull(); assertTrue(redirectURL.contains(LOGSERVICEWSADDR)); // Get container log files' name - WebResource r1 = resource(); - response = r1.path("ws").path("v1").path("node") + WebTarget t1 = target(); + response = t1.path("ws").path("v1").path("node") .path("containers").path(containerIdStr) - .path("logs").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .path("logs").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(200, response.getStatus()); - List responseList = response.getEntity(new GenericType< - List>(){}); - assertTrue(responseList.size() == 1); + List responseList = response.readEntity( + new GenericType>(){}); + assertThat(responseList).hasSize(1); assertEquals(responseList.get(0).getLogType(), ContainerLogAggregationType.LOCAL.toString()); List logMeta = responseList.get(0) .getContainerLogsInfo(); - assertTrue(logMeta.size() == 1); + assertThat(logMeta).hasSize(1); assertThat(logMeta.get(0).getFileName()).isEqualTo(filename); // now create an aggregated log in Remote File system @@ -732,26 +779,26 @@ private void testContainerLogs(WebResource r, ContainerId containerId, tempLogDir.getAbsolutePath(), appId, Collections.singletonMap(containerId, aggregatedLogMessage), nmContext.getNodeId(), aggregatedLogFile, "user", true); - r1 = resource(); - response = r1.path("ws").path("v1").path("node") + t1 = target(); + response = t1.path("ws").path("v1").path("node") .path("containers").path(containerIdStr) - .path("logs").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + .path("logs").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(200, response.getStatus()); - responseList = response.getEntity(new GenericType< + responseList = response.readEntity(new GenericType< List>(){}); assertThat(responseList).hasSize(2); for (ContainerLogsInfo logInfo : responseList) { if(logInfo.getLogType().equals( ContainerLogAggregationType.AGGREGATED.toString())) { List meta = logInfo.getContainerLogsInfo(); - assertTrue(meta.size() == 1); + assertThat(meta).hasSize(1); assertThat(meta.get(0).getFileName()).isEqualTo(aggregatedLogFile); } else { assertEquals(logInfo.getLogType(), ContainerLogAggregationType.LOCAL.toString()); List meta = logInfo.getContainerLogsInfo(); - assertTrue(meta.size() == 1); + assertThat(meta).hasSize(1); assertThat(meta.get(0).getFileName()).isEqualTo(filename); } } @@ -762,9 +809,9 @@ private void testContainerLogs(WebResource r, ContainerId containerId, tempLogDir.getAbsolutePath(), appId, Collections.singletonMap(containerId, aggregatedLogMessage), nmContext.getNodeId(), filename, "user", true); - response = r.path(filename) - .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); - responseText = response.getEntity(String.class); + response = t.path(filename) + .request(MediaType.TEXT_PLAIN).get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains("LogAggregationType: " + ContainerLogAggregationType.AGGREGATED)); assertTrue(responseText.contains(aggregatedLogMessage)); @@ -778,9 +825,9 @@ private void testContainerLogs(WebResource r, ContainerId containerId, nmContext.getContainers().remove(containerId); assertNull(nmContext.getContainers().get(containerId)); response = - r.path(filename).accept(MediaType.TEXT_PLAIN) - .get(ClientResponse.class); - responseText = response.getEntity(String.class); + t.path(filename).request(MediaType.TEXT_PLAIN) + .get(Response.class); + responseText = response.readEntity(String.class); assertTrue(responseText.contains(logMessage)); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesApps.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesApps.java index ab06c0f9f3396..42d8432dd1462 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesApps.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesApps.java @@ -33,7 +33,10 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -60,12 +63,12 @@ import org.apache.hadoop.yarn.server.utils.BuilderUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebApp; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -76,15 +79,11 @@ import org.xml.sax.InputSource; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestNMWebServicesApps extends JerseyTestBase { +public class TestNMWebServicesApps extends JerseyTest { private static Context nmContext; private static ResourceView resourceView; @@ -151,8 +150,7 @@ public boolean isPmemCheckEnabled() { bind(ResourceView.class).toInstance(resourceView); bind(ApplicationACLsManager.class).toInstance(aclsManager); bind(LocalDirsHandlerService.class).toInstance(dirsHandler); - - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -178,21 +176,23 @@ static public void cleanup() { } public TestNMWebServicesApps() { + /* TODO: Remove or fix this super(new WebAppDescriptor.Builder( "org.apache.hadoop.yarn.server.nodemanager.webapp") .contextListenerClass(GuiceServletConfig.class) .filterClass(com.google.inject.servlet.GuiceFilter.class) .contextPath("jersey-guice-filter").servletPath("/").build()); + */ } @Test - public void testNodeAppsNone() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + public void testNodeAppsNone() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node").path("apps") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("apps isn't empty", new JSONObject().toString(), json.get("apps").toString()); } @@ -225,25 +225,25 @@ private HashMap addAppContainers(Application app, } @Test - public void testNodeApps() throws JSONException, Exception { + public void testNodeApps() throws Exception { testNodeHelper("apps", MediaType.APPLICATION_JSON); } @Test - public void testNodeAppsSlash() throws JSONException, Exception { + public void testNodeAppsSlash() throws Exception { testNodeHelper("apps/", MediaType.APPLICATION_JSON); } // make sure default is json output @Test - public void testNodeAppsDefault() throws JSONException, Exception { + public void testNodeAppsDefault() throws Exception { testNodeHelper("apps/", ""); } public void testNodeHelper(String path, String media) throws JSONException, Exception { - WebResource r = resource(); + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); @@ -251,11 +251,11 @@ public void testNodeHelper(String path, String media) throws JSONException, nmContext.getApplications().put(app2.getAppId(), app2); HashMap hash2 = addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path(path) - .accept(media).get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node").path(path) + .request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject info = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, info.length()); JSONArray appInfo = info.getJSONArray("app"); @@ -271,8 +271,8 @@ public void testNodeHelper(String path, String media) throws JSONException, } @Test - public void testNodeAppsUser() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsUser() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); @@ -280,12 +280,12 @@ public void testNodeAppsUser() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") - .queryParam("user", "mockUser").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node").path("apps") + .queryParam("user", "mockUser").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject info = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, info.length()); @@ -295,8 +295,8 @@ public void testNodeAppsUser() throws JSONException, Exception { } @Test - public void testNodeAppsUserNone() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsUserNone() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -304,19 +304,19 @@ public void testNodeAppsUserNone() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") - .queryParam("user", "george").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node").path("apps") + .queryParam("user", "george").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("apps is not empty", new JSONObject().toString(), json.get("apps").toString()); } @Test - public void testNodeAppsUserEmpty() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsUserEmpty() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -325,16 +325,17 @@ public void testNodeAppsUserEmpty() throws JSONException, Exception { addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("apps").queryParam("user", "") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + t.path("ws").path("v1").path("node").path("apps").queryParam("user", "") + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException we) { + Response response = we.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -353,8 +354,8 @@ public void testNodeAppsUserEmpty() throws JSONException, Exception { } @Test - public void testNodeAppsState() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsState() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -363,13 +364,13 @@ public void testNodeAppsState() throws JSONException, Exception { HashMap hash2 = addAppContainers(app2); app2.setState(ApplicationState.RUNNING); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") + Response response = t.path("ws").path("v1").path("node").path("apps") .queryParam("state", ApplicationState.RUNNING.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject info = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, info.length()); @@ -380,8 +381,8 @@ public void testNodeAppsState() throws JSONException, Exception { } @Test - public void testNodeAppsStateNone() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsStateNone() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -389,20 +390,20 @@ public void testNodeAppsStateNone() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") + Response response = t.path("ws").path("v1").path("node").path("apps") .queryParam("state", ApplicationState.INITING.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("apps is not empty", new JSONObject().toString(), json.get("apps").toString()); } @Test - public void testNodeAppsStateInvalid() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsStateInvalid() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -411,17 +412,18 @@ public void testNodeAppsStateInvalid() throws JSONException, Exception { addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("apps") - .queryParam("state", "FOO_STATE").accept(MediaType.APPLICATION_JSON) + t.path("ws").path("v1").path("node").path("apps") + .queryParam("state", "FOO_STATE").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException we) { + Response response = we.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -433,8 +435,8 @@ public void testNodeAppsStateInvalid() throws JSONException, Exception { // verify the exception object default format is JSON @Test - public void testNodeAppsStateInvalidDefault() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsStateInvalidDefault() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -443,16 +445,18 @@ public void testNodeAppsStateInvalidDefault() throws JSONException, Exception { addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("apps") - .queryParam("state", "FOO_STATE").get(JSONObject.class); + t.path("ws").path("v1").path("node").path("apps") + .queryParam("state", "FOO_STATE") + .request().get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException we) { + Response response = we.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -464,8 +468,8 @@ public void testNodeAppsStateInvalidDefault() throws JSONException, Exception { // test that the exception output also returns XML @Test - public void testNodeAppsStateInvalidXML() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsStateInvalidXML() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -474,17 +478,18 @@ public void testNodeAppsStateInvalidXML() throws JSONException, Exception { addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("apps") - .queryParam("state", "FOO_STATE").accept(MediaType.APPLICATION_XML) + t.path("ws").path("v1").path("node").path("apps") + .queryParam("state", "FOO_STATE").request(MediaType.APPLICATION_XML) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException we) { + Response response = we.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String msg = response.getEntity(String.class); + response.getMediaType().toString()); + String msg = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); @@ -515,19 +520,18 @@ private void verifyStateInvalidException(String message, String type, } @Test - public void testNodeSingleApps() throws JSONException, Exception { + public void testNodeSingleApps() throws Exception { testNodeSingleAppHelper(MediaType.APPLICATION_JSON); } // make sure default is json output @Test - public void testNodeSingleAppsDefault() throws JSONException, Exception { + public void testNodeSingleAppsDefault() throws Exception { testNodeSingleAppHelper(""); } - public void testNodeSingleAppHelper(String media) throws JSONException, - Exception { - WebResource r = resource(); + public void testNodeSingleAppHelper(String media) throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); @@ -535,37 +539,37 @@ public void testNodeSingleAppHelper(String media) throws JSONException, nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") - .path(app.getAppId().toString()).accept(media) - .get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node").path("apps") + .path(app.getAppId().toString()).request(media) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeAppInfo(json.getJSONObject("app"), app, hash); } @Test - public void testNodeSingleAppsSlash() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeSingleAppsSlash() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") + Response response = t.path("ws").path("v1").path("node").path("apps") .path(app.getAppId().toString() + "/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeAppInfo(json.getJSONObject("app"), app, hash); } @Test - public void testNodeSingleAppsInvalid() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeSingleAppsInvalid() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -574,15 +578,16 @@ public void testNodeSingleAppsInvalid() throws JSONException, Exception { addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("apps").path("app_foo_0000") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + t.path("ws").path("v1").path("node").path("apps").path("app_foo_0000") + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -601,8 +606,8 @@ public void testNodeSingleAppsInvalid() throws JSONException, Exception { } @Test - public void testNodeSingleAppsMissing() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeSingleAppsMissing() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -611,16 +616,17 @@ public void testNodeSingleAppsMissing() throws JSONException, Exception { addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("apps") - .path("application_1234_0009").accept(MediaType.APPLICATION_JSON) + t.path("ws").path("v1").path("node").path("apps") + .path("application_1234_0009").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -637,8 +643,8 @@ public void testNodeSingleAppsMissing() throws JSONException, Exception { } @Test - public void testNodeAppsXML() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeAppsXML() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -646,11 +652,11 @@ public void testNodeAppsXML() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node").path("apps") + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -661,8 +667,8 @@ public void testNodeAppsXML() throws JSONException, Exception { } @Test - public void testNodeSingleAppsXML() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeSingleAppsXML() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); @@ -670,12 +676,12 @@ public void testNodeSingleAppsXML() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node").path("apps") + Response response = t.path("ws").path("v1").path("node").path("apps") .path(app.getAppId().toString() + "/") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -732,7 +738,7 @@ public void verifyNodeAppInfoGeneric(Application app, String id, } @Test - public void testNodeAppsUserFiltering() throws JSONException, Exception { + public void testNodeAppsUserFiltering() throws Exception { Configuration yarnConf = new Configuration(); yarnConf.setBoolean(YarnConfiguration.FILTER_ENTITY_LIST_BY_USER, true); yarnConf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, true); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesAuxServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesAuxServices.java index 7ec8fcd47d30b..d3ef5b2b66db2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesAuxServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesAuxServices.java @@ -30,13 +30,15 @@ import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; +import java.util.logging.Logger; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.filter.LoggingFilter; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.http.JettyUtils; @@ -53,12 +55,13 @@ import org.apache.hadoop.yarn.server.security.ApplicationACLsManager; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebApp; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.logging.LoggingFeature; +import org.glassfish.jersey.test.JerseyTest; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -68,17 +71,17 @@ import org.xml.sax.InputSource; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Basic sanity Tests for AuxServices. * */ -public class TestNMWebServicesAuxServices extends JerseyTestBase { +public class TestNMWebServicesAuxServices extends JerseyTest { + private static final Logger LOG = + Logger.getLogger(TestNMWebServicesAuxServices.class.getName()); private static final String AUX_SERVICES_PATH = "auxiliaryservices"; private static Context nmContext; private static Configuration conf = new Configuration(); @@ -148,8 +151,7 @@ public int getHttpPort() { bind(ResourceView.class).toInstance(resourceView); bind(ApplicationACLsManager.class).toInstance(aclsManager); bind(LocalDirsHandlerService.class).toInstance(dirsHandler); - - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -175,23 +177,26 @@ static public void cleanup() { } public TestNMWebServicesAuxServices() { + /* TODO: fix or remove this super(new WebAppDescriptor.Builder( "org.apache.hadoop.yarn.server.nodemanager.webapp") .contextListenerClass(GuiceServletConfig.class) .filterClass(com.google.inject.servlet.GuiceFilter.class) .contextPath("jersey-guice-filter").servletPath("/").build()); + + */ } @Test public void testNodeAuxServicesNone() throws Exception { addAuxServices(); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node") - .path(AUX_SERVICES_PATH).accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node") + .path(AUX_SERVICES_PATH).request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("aux services isn't empty", new JSONObject().toString(), json.get("services").toString()); } @@ -225,14 +230,14 @@ public void testNodeHelper(String path, String media) throws Exception { AuxServiceRecord r2 = new AuxServiceRecord().name("name2").launchTime(new Date(456L)); addAuxServices(r1, r2); - WebResource r = resource(); - client().addFilter(new LoggingFilter()); + WebTarget t = target(); + client().register(new LoggingFeature(LOG)); - ClientResponse response = r.path("ws").path("v1").path("node").path(path) - .accept(media).get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node").path(path) + .request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject info = json.getJSONObject("services"); assertEquals("incorrect number of elements", 1, info.length()); JSONArray auxInfo = info.getJSONArray("service"); @@ -249,14 +254,14 @@ public void testNodeAuxServicesXML() throws Exception { AuxServiceRecord r2 = new AuxServiceRecord().name("name2").launchTime(new Date(456L)); addAuxServices(r1, r2); - WebResource r = resource(); + WebTarget t = target(); - ClientResponse response = r.path("ws").path("v1").path("node") - .path(AUX_SERVICES_PATH).accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node") + .path(AUX_SERVICES_PATH).request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -272,18 +277,18 @@ public void testAuxServicesDisabled() throws JSONException, Exception { AuxServices auxServices = mock(AuxServices.class); when(auxServices.isManifestEnabled()).thenReturn(false); nmContext.setAuxServices(auxServices); - WebResource r = resource(); + WebTarget t = target(); try { - r.path("ws").path("v1").path("node").path(AUX_SERVICES_PATH) - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + t.path("ws").path("v1").path("node").path(AUX_SERVICES_PATH) + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(ClientResponse.Status.BAD_REQUEST, + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesContainers.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesContainers.java index 175a0b02470ba..dd93771a9f346 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesContainers.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesContainers.java @@ -30,12 +30,17 @@ import java.io.StringReader; import java.util.HashMap; import java.util.List; +import java.util.logging.Logger; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import com.sun.jersey.api.client.filter.LoggingFilter; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.http.JettyUtils; @@ -58,12 +63,13 @@ import org.apache.hadoop.yarn.server.utils.BuilderUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebApp; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.logging.LoggingFeature; +import org.glassfish.jersey.test.JerseyTest; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -74,15 +80,11 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestNMWebServicesContainers extends JerseyTestBase { +public class TestNMWebServicesContainers extends JerseyTest { + private static final Logger LOG = + Logger.getLogger(TestNMWebServicesContainers.class.getName()); private static Context nmContext; private static ResourceView resourceView; private static ApplicationACLsManager aclsManager; @@ -153,8 +155,7 @@ public int getHttpPort() { bind(ResourceView.class).toInstance(resourceView); bind(ApplicationACLsManager.class).toInstance(aclsManager); bind(LocalDirsHandlerService.class).toInstance(dirsHandler); - - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -180,22 +181,24 @@ static public void cleanup() { } public TestNMWebServicesContainers() { + /* TODO: Remove or fix this super(new WebAppDescriptor.Builder( "org.apache.hadoop.yarn.server.nodemanager.webapp") .contextListenerClass(GuiceServletConfig.class) .filterClass(com.google.inject.servlet.GuiceFilter.class) .contextPath("jersey-guice-filter").servletPath("/").build()); + */ } @Test - public void testNodeContainersNone() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("node") - .path("containers").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + public void testNodeContainersNone() throws Exception { + WebTarget t = target(); + Response response = t.path("ws").path("v1").path("node") + .path("containers").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("apps isn't empty", new JSONObject().toString(), json.get("containers").toString()); } @@ -240,38 +243,37 @@ private HashMap addAppContainers(Application app) } @Test - public void testNodeContainers() throws JSONException, Exception { + public void testNodeContainers() throws Exception { testNodeHelper("containers", MediaType.APPLICATION_JSON); } @Test - public void testNodeContainersSlash() throws JSONException, Exception { + public void testNodeContainersSlash() throws Exception { testNodeHelper("containers/", MediaType.APPLICATION_JSON); } // make sure default is json output @Test - public void testNodeContainersDefault() throws JSONException, Exception { + public void testNodeContainersDefault() throws Exception { testNodeHelper("containers/", ""); } - public void testNodeHelper(String path, String media) throws JSONException, - Exception { - WebResource r = resource(); + public void testNodeHelper(String path, String media) throws Exception { + WebTarget r = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - client().addFilter(new LoggingFilter()); + client().register(new LoggingFeature(LOG)); - ClientResponse response = r.path("ws").path("v1").path("node").path(path) - .accept(media).get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("node").path(path) + .request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject info = json.getJSONObject("containers"); assertEquals("incorrect number of elements", 1, info.length()); JSONArray conInfo = info.getJSONArray("container"); @@ -287,23 +289,23 @@ public void testNodeHelper(String path, String media) throws JSONException, } @Test - public void testNodeSingleContainers() throws JSONException, Exception { + public void testNodeSingleContainers() throws Exception { testNodeSingleContainersHelper(MediaType.APPLICATION_JSON); } @Test - public void testNodeSingleContainersSlash() throws JSONException, Exception { + public void testNodeSingleContainersSlash() throws Exception { testNodeSingleContainersHelper(MediaType.APPLICATION_JSON); } @Test - public void testNodeSingleContainersDefault() throws JSONException, Exception { + public void testNodeSingleContainersDefault() throws Exception { testNodeSingleContainersHelper(""); } public void testNodeSingleContainersHelper(String media) throws JSONException, Exception { - WebResource r = resource(); + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); @@ -312,19 +314,19 @@ public void testNodeSingleContainersHelper(String media) addAppContainers(app2); for (String id : hash.keySet()) { - ClientResponse response = r.path("ws").path("v1").path("node") - .path("containers").path(id).accept(media).get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node") + .path("containers").path(id).request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNodeContainerInfo(json.getJSONObject("container"), nmContext .getContainers().get(ContainerId.fromString(id))); } } @Test - public void testSingleContainerInvalid() throws JSONException, Exception { - WebResource r = resource(); + public void testSingleContainerInvalid() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -332,16 +334,17 @@ public void testSingleContainerInvalid() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("containers") - .path("container_foo_1234").accept(MediaType.APPLICATION_JSON) + t.path("ws").path("v1").path("node").path("containers") + .path("container_foo_1234").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -358,8 +361,8 @@ public void testSingleContainerInvalid() throws JSONException, Exception { } @Test - public void testSingleContainerInvalid2() throws JSONException, Exception { - WebResource r = resource(); + public void testSingleContainerInvalid2() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -367,16 +370,17 @@ public void testSingleContainerInvalid2() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("containers") - .path("container_1234_0001").accept(MediaType.APPLICATION_JSON) + t.path("ws").path("v1").path("node").path("containers") + .path("container_1234_0001").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.BAD_REQUEST, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -393,8 +397,8 @@ public void testSingleContainerInvalid2() throws JSONException, Exception { } @Test - public void testSingleContainerWrong() throws JSONException, Exception { - WebResource r = resource(); + public void testSingleContainerWrong() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -402,16 +406,17 @@ public void testSingleContainerWrong() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { - r.path("ws").path("v1").path("node").path("containers") + t.path("ws").path("v1").path("node").path("containers") .path("container_1234_0001_01_000005") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid user query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException we) { + Response response = we.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, + response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -430,23 +435,23 @@ public void testSingleContainerWrong() throws JSONException, Exception { } @Test - public void testNodeSingleContainerXML() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeSingleContainerXML() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); HashMap hash = addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - client().addFilter(new LoggingFilter(System.out)); + client().register(new LoggingFeature(LOG)); for (String id : hash.keySet()) { - ClientResponse response = r.path("ws").path("v1").path("node") - .path("containers").path(id).accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node") + .path("containers").path(id).request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -461,8 +466,8 @@ public void testNodeSingleContainerXML() throws JSONException, Exception { } @Test - public void testNodeContainerXML() throws JSONException, Exception { - WebResource r = resource(); + public void testNodeContainerXML() throws Exception { + WebTarget t = target(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); @@ -470,12 +475,12 @@ public void testNodeContainerXML() throws JSONException, Exception { nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); - ClientResponse response = r.path("ws").path("v1").path("node") - .path("containers").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = t.path("ws").path("v1").path("node") + .path("containers").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebTerminal.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebTerminal.java index d4180e4825186..a9c93f0db6836 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebTerminal.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebTerminal.java @@ -22,7 +22,10 @@ import java.io.File; import java.io.IOException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; @@ -37,10 +40,6 @@ import org.junit.Before; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource.Builder; - /** * Unit test for hosting web terminal servlet in node manager. */ @@ -110,11 +109,11 @@ public void tearDown() throws IOException { @Test public void testWebTerminal() { - Client client = Client.create(); - Builder builder = client.resource("http://127.0.0.1:" + port + - "/terminal/terminal.template").accept("text/html"); - ClientResponse response = builder.get(ClientResponse.class); + Client client = ClientBuilder.newClient(); + Response response = client.target("http://127.0.0.1:" + port + + "/terminal/terminal.template").request("text/html") + .get(Response.class); assertEquals(MediaType.TEXT_HTML + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/pom.xml index 9d096d20c5fdd..b5d32a9bc3ebb 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/pom.xml @@ -101,33 +101,6 @@ com.google.inject guice - - com.sun.jersey.jersey-test-framework - jersey-test-framework-core - test - - - com.github.pjfanning - jersey-json - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - - - com.sun.jersey.contribs - jersey-guice - org.apache.hadoop @@ -153,14 +126,6 @@ org.codehaus.jettison jettison - - com.sun.jersey - jersey-core - - - com.sun.jersey - jersey-client - org.eclipse.jetty jetty-util @@ -169,6 +134,24 @@ org.eclipse.jetty jetty-util-ajax + + jakarta.annotation + jakarta.annotation-api + + + org.glassfish.jersey.media + jersey-media-json-jackson + + + org.javassist + javassist + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-base + + + org.apache.hadoop.thirdparty hadoop-shaded-guava @@ -260,13 +243,6 @@ ${project.version} - - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test - - com.github.stefanbirkner system-rules @@ -334,6 +310,10 @@ com.fasterxml.jackson.dataformat jackson-dataformat-yaml + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceManager.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceManager.java index 8adcff42a695d..e2e7dbe5875bc 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceManager.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceManager.java @@ -20,10 +20,11 @@ import org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; import org.apache.hadoop.classification.VisibleForTesting; -import com.sun.jersey.spi.container.servlet.ServletContainer; import org.apache.hadoop.yarn.metrics.GenericEventTypeMetrics; import org.apache.hadoop.yarn.webapp.WebAppException; + +import org.glassfish.jersey.servlet.ServletContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/federation/FederationStateStoreHeartbeat.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/federation/FederationStateStoreHeartbeat.java index a4618a2fc3bb7..76ecb14820c41 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/federation/FederationStateStoreHeartbeat.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/federation/FederationStateStoreHeartbeat.java @@ -18,10 +18,6 @@ package org.apache.hadoop.yarn.server.resourcemanager.federation; -import java.io.StringWriter; - -import javax.xml.bind.JAXBException; - import org.apache.hadoop.yarn.server.federation.store.FederationStateStore; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterHeartbeatRequest; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterId; @@ -29,13 +25,12 @@ import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.ClusterMetricsInfo; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONMarshaller; - /** * Periodic heart beat from a ResourceManager participating in * federation to indicate liveliness. The heart beat publishes the current @@ -52,26 +47,16 @@ public class FederationStateStoreHeartbeat implements Runnable { private final ResourceScheduler rs; - private StringWriter currentClusterState; - private JSONJAXBContext jc; - private JSONMarshaller marshaller; private String capability; + private static final ObjectWriter WRITER = + new ObjectMapper().writerFor(ClusterMetricsInfo.class).withDefaultPrettyPrinter(); + public FederationStateStoreHeartbeat(SubClusterId subClusterId, FederationStateStore stateStoreClient, ResourceScheduler scheduler) { this.stateStoreService = stateStoreClient; this.subClusterId = subClusterId; this.rs = scheduler; - // Initialize the JAXB Marshaller - this.currentClusterState = new StringWriter(); - try { - this.jc = new JSONJAXBContext( - JSONConfiguration.mapped().rootUnwrapping(false).build(), - ClusterMetricsInfo.class); - marshaller = jc.createJSONMarshaller(); - } catch (JAXBException e) { - LOG.warn("Exception while trying to initialize JAXB context.", e); - } LOG.info("Initialized Federation membership for cluster with timestamp: " + ResourceManager.getClusterTimeStamp()); } @@ -83,10 +68,8 @@ public FederationStateStoreHeartbeat(SubClusterId subClusterId, private void updateClusterState() { try { // get the current state - currentClusterState.getBuffer().setLength(0); ClusterMetricsInfo clusterMetricsInfo = new ClusterMetricsInfo(rs); - marshaller.marshallToJSON(clusterMetricsInfo, currentClusterState); - capability = currentClusterState.toString(); + capability = WRITER.writeValueAsString(clusterMetricsInfo); } catch (Exception e) { LOG.warn("Exception while trying to generate cluster state," + " so reverting to last know state.", e); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/JAXBContextResolver.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/JAXBContextResolver.java index 24428b3bb83de..339ea0ecb1144 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/JAXBContextResolver.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/JAXBContextResolver.java @@ -20,8 +20,7 @@ import com.google.inject.Inject; import com.google.inject.Singleton; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; +import org.glassfish.jersey.jettison.JettisonJaxbContext; import java.util.*; @@ -116,13 +115,9 @@ public JAXBContextResolver(Configuration conf) throws Exception { final Class[] finalRootUnwrappedTypes = finalRootUnwrappedTypesList .toArray(new Class[finalRootUnwrappedTypesList.size()]); - this.typesContextMap = new HashMap(); - context = - new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false) - .build(), finalcTypes); - unWrappedRootContext = - new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(true) - .build(), finalRootUnwrappedTypes); + this.typesContextMap = new HashMap<>(); + context = new JettisonJaxbContext(finalcTypes); + unWrappedRootContext = new JettisonJaxbContext(finalRootUnwrappedTypes); for (Class type : finalcTypes) { typesContextMap.put(type, context); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebApp.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebApp.java index 3ed53f6dcc669..12c56867f9142 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebApp.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebApp.java @@ -22,6 +22,8 @@ import java.net.InetSocketAddress; +import javax.servlet.Filter; + import org.apache.hadoop.conf.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,8 +36,6 @@ import org.apache.hadoop.yarn.webapp.WebApp; import org.apache.hadoop.yarn.webapp.YarnWebParams; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; - /** * The RM webapp */ @@ -82,7 +82,7 @@ public void setup() { } @Override - protected Class getWebAppFilterClass() { + protected Class getWebAppFilterClass() { return RMWebAppFilter.class; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebAppFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebAppFilter.java index e70266f368a8b..789b5028f08e1 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebAppFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebAppFilter.java @@ -30,8 +30,12 @@ import javax.inject.Inject; import javax.inject.Singleton; +import javax.servlet.Filter; import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -53,10 +57,9 @@ import org.slf4j.LoggerFactory; import com.google.inject.Injector; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; @Singleton -public class RMWebAppFilter extends GuiceContainer { +public class RMWebAppFilter implements Filter { private static final Logger LOG = LoggerFactory.getLogger(RMWebAppFilter.class); @@ -79,7 +82,6 @@ public class RMWebAppFilter extends GuiceContainer { @Inject public RMWebAppFilter(Injector injector, Configuration conf) { - super(injector); this.injector=injector; InetSocketAddress sock = YarnConfiguration.useHttps(conf) ? conf.getSocketAddr(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS, @@ -103,9 +105,15 @@ public RMWebAppFilter(Injector injector, Configuration conf) { } @Override - public void doFilter(HttpServletRequest request, - HttpServletResponse response, FilterChain chain) throws IOException, - ServletException { + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, + FilterChain chain) throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; response.setCharacterEncoding("UTF-8"); String htmlEscapedUri = HtmlQuoting.quoteHtmlChars(request.getRequestURI()); @@ -176,7 +184,12 @@ && shouldRedirect(rmWebApp, htmlEscapedUri)) { } } - super.doFilter(request, response, chain); + chain.doFilter(request, response); + } + + @Override + public void destroy() { + } private String ahsRedirectPath(String uri, RMWebApp rmWebApp) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java index 26c921796b3c4..f1162dda080d9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java @@ -39,6 +39,8 @@ import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; @@ -221,8 +223,6 @@ import org.slf4j.LoggerFactory; import org.apache.hadoop.classification.VisibleForTesting; -import com.google.inject.Inject; -import com.google.inject.Singleton; @Singleton @Path(RMWSConsts.RM_WEB_SERVICE_PATH) @@ -235,7 +235,9 @@ public class RMWebServices extends WebServices implements RMWebServiceProtocol { private static RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null); private final Configuration conf; - private @Context HttpServletResponse response; + + @Context + private HttpServletResponse response; // -------Default values of QueryParams for RMWebServiceProtocol-------- diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMHA.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMHA.java index 83d81ec9ebb0d..1ccfc2c5efefd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMHA.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMHA.java @@ -31,8 +31,13 @@ import java.io.IOException; import java.net.InetSocketAddress; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.glassfish.jersey.client.ClientConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.conf.Configuration; @@ -67,11 +72,6 @@ import org.junit.Before; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.DefaultClientConfig; - public class TestRMHA { private static final Logger LOG = LoggerFactory.getLogger(TestRMHA.class); private Configuration configuration; @@ -157,21 +157,19 @@ private void checkActiveRMFunctionality() throws Exception { private void checkActiveRMWebServices() throws JSONException { // Validate web-service - Client webServiceClient = Client.create(new DefaultClientConfig()); + Client webServiceClient = ClientBuilder.newClient(new ClientConfig()); InetSocketAddress rmWebappAddr = NetUtils.getConnectAddress(rm.getWebapp().getListenerAddress()); String webappURL = "http://" + rmWebappAddr.getHostName() + ":" + rmWebappAddr.getPort(); - WebResource webResource = webServiceClient.resource(webappURL); + WebTarget webResource = webServiceClient.target(webappURL); String path = app.getApplicationId().toString(); - ClientResponse response = - webResource.path("ws").path("v1").path("cluster").path("apps") - .path(path).accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = webResource.path("ws").path("v1").path("cluster").path("apps").path(path) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject appJson = json.getJSONObject("app"); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/federation/TestFederationRMStateStoreService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/federation/TestFederationRMStateStoreService.java index e5e156dcf76c6..6b2111946afc9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/federation/TestFederationRMStateStoreService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/federation/TestFederationRMStateStoreService.java @@ -36,15 +36,15 @@ import org.apache.hadoop.yarn.server.federation.store.records.SubClusterState; import org.apache.hadoop.yarn.server.resourcemanager.MockRM; import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.ClusterMetricsInfo; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONUnmarshaller; - /** * Unit tests for FederationStateStoreService. */ @@ -60,22 +60,12 @@ public class TestFederationRMStateStoreService { private Configuration conf; private FederationStateStore stateStore; private long lastHearbeatTS = 0; - private JSONJAXBContext jc; - private JSONUnmarshaller unmarshaller; + + private static final ObjectReader READER = new ObjectMapper().readerFor(ClusterMetricsInfo.class); @Before public void setUp() throws IOException, YarnException, JAXBException { conf = new YarnConfiguration(); - jc = new JSONJAXBContext( - JSONConfiguration.mapped().rootUnwrapping(false).build(), - ClusterMetricsInfo.class); - unmarshaller = jc.createJSONUnmarshaller(); - } - - @After - public void tearDown() throws Exception { - unmarshaller = null; - jc = null; } @Test @@ -148,9 +138,8 @@ private void explicitFailover(MockRM rm) throws IOException { } private void checkClusterMetricsInfo(String capability, int numNodes) - throws JAXBException { - ClusterMetricsInfo clusterMetricsInfo = unmarshaller.unmarshalFromJSON( - new StringReader(capability), ClusterMetricsInfo.class); + throws JsonProcessingException { + ClusterMetricsInfo clusterMetricsInfo = READER.readValue(capability); Assert.assertEquals(numNodes, clusterMetricsInfo.getTotalNodes()); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/ActivitiesTestUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/ActivitiesTestUtils.java index dd08e4b129750..a60bae0daa4d3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/ActivitiesTestUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/ActivitiesTestUtils.java @@ -18,8 +18,6 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.util.Lists; import org.apache.hadoop.yarn.api.records.ExecutionType; @@ -35,12 +33,15 @@ import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -293,16 +294,17 @@ public static void verifyNumberOfAllocationAttempts(JSONObject allocation, } } - public static JSONObject requestWebResource(WebResource webResource, - MultivaluedMap params) { + public static JSONObject requestWebResource(WebTarget webTarget, + MultivaluedMap params) { if (params != null) { - webResource = webResource.queryParams(params); + for (Map.Entry> entry : params.entrySet()) { + webTarget = webTarget.queryParam(entry.getKey(), entry.getValue()); + } } - ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = webTarget.request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - return response.getEntity(JSONObject.class); + response.getMediaType().toString()); + return response.readEntity(JSONObject.class); } /** diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServiceAppsNodelabel.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServiceAppsNodelabel.java index 7397926732e95..a844dcda02b08 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServiceAppsNodelabel.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServiceAppsNodelabel.java @@ -26,7 +26,9 @@ import java.util.HashSet; import java.util.Set; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.hadoop.util.Sets; import org.apache.hadoop.yarn.api.records.ContainerId; @@ -46,10 +48,13 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -57,16 +62,12 @@ import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableMap; import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Tests partition resource usage per application. * */ -public class TestRMWebServiceAppsNodelabel extends JerseyTestBase { +public class TestRMWebServiceAppsNodelabel extends JerseyTest { private static final int AM_CONTAINER_MB = 1024; @@ -100,16 +101,11 @@ protected void configureServlets() { Assert.fail(); } bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } - }; + } public TestRMWebServiceAppsNodelabel() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } private static void setupQueueConfiguration( @@ -149,11 +145,11 @@ public void testAppsFinished() throws JSONException, Exception { amNodeManager.nodeHeartbeat(true); RMApp killedApp = MockRMAppSubmitter.submitWithMemory(AM_CONTAINER_MB, rm); rm.killApp(killedApp.getApplicationId()); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("cluster").path("apps") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); - JSONObject json = response.getEntity(JSONObject.class); + WebTarget target = target(); + Response response = + target.path("ws").path("v1").path("cluster").path("apps") + .request(MediaType.APPLICATION_JSON).get(Response.class); + JSONObject json = response.readEntity(JSONObject.class); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); try { @@ -191,12 +187,12 @@ public void testAppsRunning() throws JSONException, Exception { am1.allocate("*", 1024, 1, new ArrayList(), "X"); nm2.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response = - r.path("ws").path("v1").path("cluster").path("apps") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); - JSONObject json = response.getEntity(JSONObject.class); + Response response = + target.path("ws").path("v1").path("cluster").path("apps") + .request(MediaType.APPLICATION_JSON).get(Response.class); + JSONObject json = response.readEntity(JSONObject.class); // Verify apps resource JSONObject apps = json.getJSONObject("apps"); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java index ce9de643744dd..0c99d14446f4a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServices.java @@ -40,16 +40,22 @@ import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.logging.Level; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Application; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableSet; + import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; @@ -94,12 +100,16 @@ import org.apache.hadoop.yarn.webapp.ForbiddenException; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.apache.hadoop.yarn.webapp.dao.QueueConfigInfo; import org.apache.hadoop.yarn.webapp.dao.SchedConfUpdateInfo; + import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.hk2.utilities.binding.AbstractBinder; +import org.glassfish.jersey.logging.LoggingFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; @@ -113,16 +123,11 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestRMWebServices extends JerseyTestBase { - private static final Logger LOG = - LoggerFactory.getLogger(TestRMWebServices.class); +public class TestRMWebServices extends JerseyTest { + private static final Logger LOG = LoggerFactory.getLogger(TestRMWebServices.class); + private static final java.util.logging.Logger LOGGER = + java.util.logging.Logger.getLogger(RMWebServices.class.getName()); private static MockRM rm; @@ -133,33 +138,33 @@ protected void configureServlets() { bind(RMWebServices.class); bind(GenericExceptionHandler.class); Configuration conf = new Configuration(); - conf.setClass(YarnConfiguration.RM_SCHEDULER, FifoScheduler.class, - ResourceScheduler.class); + conf.setClass(YarnConfiguration.RM_SCHEDULER, FifoScheduler.class, ResourceScheduler.class); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); } } - static { - GuiceServletConfig.setInjector( - Guice.createInjector(new WebServletModule())); - } - @Before @Override public void setUp() throws Exception { super.setUp(); - GuiceServletConfig.setInjector( - Guice.createInjector(new WebServletModule())); + GuiceServletConfig.setInjector(Guice.createInjector(new WebServletModule())); } - public TestRMWebServices() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); + @Override + protected Application configure() { + ResourceConfig config = new ResourceConfig(); + config.register(new AbstractBinder() { + @Override + protected void configure() { + bind(ResourceManager.class).to(ResourceManager.class); + bind(Configuration.class).to(Configuration.class); + } + }); + config.register(new LoggingFeature(LOGGER, Level.INFO, LoggingFeature.Verbosity.PAYLOAD_ANY, + LoggingFeature.DEFAULT_MAX_ENTITY_SIZE)); + config.registerClasses(RMWebServices.class); + return config; } @BeforeClass @@ -174,138 +179,135 @@ public static void initClusterMetrics() { @Test public void testInfoXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").accept("application/xml").get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("info").request("application/xml") + .get(Response.class); assertEquals(MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyClusterInfoXML(xml); } @Test public void testInvalidUri() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); String responseStr = ""; try { - responseStr = r.path("ws").path("v1").path("cluster").path("bogus") - .accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = + r.path("ws").path("v1").path("cluster").path("bogus").request(MediaType.APPLICATION_JSON) + .get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); - WebServicesTestUtils.checkStringMatch( - "error string exists and shouldn't", "", responseStr); + WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } } @Test public void testInvalidUri2() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); String responseStr = ""; try { - responseStr = r.accept(MediaType.APPLICATION_JSON).get(String.class); + responseStr = r.request(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); - WebServicesTestUtils.checkStringMatch( - "error string exists and shouldn't", "", responseStr); + WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } } @Test public void testInvalidAccept() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); String responseStr = ""; try { - responseStr = r.path("ws").path("v1").path("cluster") - .accept(MediaType.TEXT_PLAIN).get(String.class); + responseStr = + r.path("ws").path("v1").path("cluster").request(MediaType.TEXT_PLAIN).get(String.class); fail("should have thrown exception on invalid uri"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.INTERNAL_SERVER_ERROR, - response.getStatusInfo()); - WebServicesTestUtils.checkStringMatch( - "error string exists and shouldn't", "", responseStr); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); + assertResponseStatusCode(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo()); + WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } } @Test public void testCluster() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterInfo(json); } @Test public void testClusterSlash() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); // test with trailing "/" to make sure acts same as without slash - ClientResponse response = r.path("ws").path("v1").path("cluster/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterInfo(json); } @Test public void testClusterDefault() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); // test with trailing "/" to make sure acts same as without slash - ClientResponse response = r.path("ws").path("v1").path("cluster") - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterInfo(json); } @Test public void testInfo() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("info").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterInfo(json); } @Test public void testInfoSlash() throws JSONException, Exception { // test with trailing "/" to make sure acts same as without slash - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("info/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterInfo(json); } @Test public void testInfoDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("info").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterInfo(json); } @@ -325,117 +327,103 @@ public void verifyClusterInfoXML(String xml) throws JSONException, Exception { WebServicesTestUtils.getXmlLong(element, "startedOn"), WebServicesTestUtils.getXmlString(element, "state"), WebServicesTestUtils.getXmlString(element, "haState"), - WebServicesTestUtils.getXmlString( - element, "haZooKeeperConnectionState"), + WebServicesTestUtils.getXmlString(element, "haZooKeeperConnectionState"), WebServicesTestUtils.getXmlString(element, "hadoopVersionBuiltOn"), WebServicesTestUtils.getXmlString(element, "hadoopBuildVersion"), WebServicesTestUtils.getXmlString(element, "hadoopVersion"), - WebServicesTestUtils.getXmlString(element, - "resourceManagerVersionBuiltOn"), - WebServicesTestUtils.getXmlString(element, - "resourceManagerBuildVersion"), + WebServicesTestUtils.getXmlString(element, "resourceManagerVersionBuiltOn"), + WebServicesTestUtils.getXmlString(element, "resourceManagerBuildVersion"), WebServicesTestUtils.getXmlString(element, "resourceManagerVersion")); } } - public void verifyClusterInfo(JSONObject json) throws JSONException, - Exception { + public void verifyClusterInfo(JSONObject json) throws JSONException, Exception { assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("clusterInfo"); assertEquals("incorrect number of elements", 12, info.length()); - verifyClusterGeneric(info.getLong("id"), info.getLong("startedOn"), - info.getString("state"), info.getString("haState"), - info.getString("haZooKeeperConnectionState"), - info.getString("hadoopVersionBuiltOn"), - info.getString("hadoopBuildVersion"), info.getString("hadoopVersion"), - info.getString("resourceManagerVersionBuiltOn"), - info.getString("resourceManagerBuildVersion"), - info.getString("resourceManagerVersion")); + verifyClusterGeneric(info.getLong("id"), info.getLong("startedOn"), info.getString("state"), + info.getString("haState"), info.getString("haZooKeeperConnectionState"), + info.getString("hadoopVersionBuiltOn"), info.getString("hadoopBuildVersion"), + info.getString("hadoopVersion"), info.getString("resourceManagerVersionBuiltOn"), + info.getString("resourceManagerBuildVersion"), info.getString("resourceManagerVersion")); } - public void verifyClusterGeneric(long clusterid, long startedon, - String state, String haState, String haZooKeeperConnectionState, - String hadoopVersionBuiltOn, - String hadoopBuildVersion, String hadoopVersion, - String resourceManagerVersionBuiltOn, String resourceManagerBuildVersion, - String resourceManagerVersion) { - - assertEquals("clusterId doesn't match: ", - ResourceManager.getClusterTimeStamp(), clusterid); - assertEquals("startedOn doesn't match: ", - ResourceManager.getClusterTimeStamp(), startedon); - assertTrue("stated doesn't match: " + state, - state.matches(STATE.INITED.toString())); - assertTrue("HA state doesn't match: " + haState, - haState.matches("INITIALIZING")); - - WebServicesTestUtils.checkStringMatch("hadoopVersionBuiltOn", - VersionInfo.getDate(), hadoopVersionBuiltOn); - WebServicesTestUtils.checkStringEqual("hadoopBuildVersion", - VersionInfo.getBuildVersion(), hadoopBuildVersion); - WebServicesTestUtils.checkStringMatch("hadoopVersion", - VersionInfo.getVersion(), hadoopVersion); + public void verifyClusterGeneric(long clusterid, long startedon, String state, String haState, + String haZooKeeperConnectionState, String hadoopVersionBuiltOn, String hadoopBuildVersion, + String hadoopVersion, String resourceManagerVersionBuiltOn, + String resourceManagerBuildVersion, String resourceManagerVersion) { + + assertEquals("clusterId doesn't match: ", ResourceManager.getClusterTimeStamp(), clusterid); + assertEquals("startedOn doesn't match: ", ResourceManager.getClusterTimeStamp(), startedon); + assertTrue("stated doesn't match: " + state, state.matches(STATE.INITED.toString())); + assertTrue("HA state doesn't match: " + haState, haState.matches("INITIALIZING")); + + WebServicesTestUtils.checkStringMatch("hadoopVersionBuiltOn", VersionInfo.getDate(), + hadoopVersionBuiltOn); + WebServicesTestUtils.checkStringEqual("hadoopBuildVersion", VersionInfo.getBuildVersion(), + hadoopBuildVersion); + WebServicesTestUtils.checkStringMatch("hadoopVersion", VersionInfo.getVersion(), hadoopVersion); WebServicesTestUtils.checkStringMatch("resourceManagerVersionBuiltOn", YarnVersionInfo.getDate(), resourceManagerVersionBuiltOn); WebServicesTestUtils.checkStringEqual("resourceManagerBuildVersion", YarnVersionInfo.getBuildVersion(), resourceManagerBuildVersion); - WebServicesTestUtils.checkStringMatch("resourceManagerVersion", - YarnVersionInfo.getVersion(), resourceManagerVersion); + WebServicesTestUtils.checkStringMatch("resourceManagerVersion", YarnVersionInfo.getVersion(), + resourceManagerVersion); } @Test public void testClusterMetrics() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("metrics").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("metrics").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterMetricsJSON(json); } @Test public void testClusterMetricsSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("metrics/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("metrics/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterMetricsJSON(json); } @Test public void testClusterMetricsDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("metrics").get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("metrics").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterMetricsJSON(json); } @Test public void testClusterMetricsXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("metrics").accept("application/xml").get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("metrics").request("application/xml") + .get(Response.class); assertEquals(MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyClusterMetricsXML(xml); } - public void verifyClusterMetricsXML(String xml) throws JSONException, - Exception { + public void verifyClusterMetricsXML(String xml) throws JSONException, Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -447,8 +435,7 @@ public void verifyClusterMetricsXML(String xml) throws JSONException, for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); - verifyClusterMetrics( - WebServicesTestUtils.getXmlInt(element, "appsSubmitted"), + verifyClusterMetrics(WebServicesTestUtils.getXmlInt(element, "appsSubmitted"), WebServicesTestUtils.getXmlInt(element, "appsCompleted"), WebServicesTestUtils.getXmlInt(element, "reservedMB"), WebServicesTestUtils.getXmlInt(element, "availableMB"), @@ -471,142 +458,117 @@ public void verifyClusterMetricsXML(String xml) throws JSONException, } } - public void verifyClusterMetricsJSON(JSONObject json) throws JSONException, - Exception { + public void verifyClusterMetricsJSON(JSONObject json) throws JSONException, Exception { assertEquals("incorrect number of elements", 1, json.length()); JSONObject clusterinfo = json.getJSONObject("clusterMetrics"); assertEquals("incorrect number of elements", 35, clusterinfo.length()); - verifyClusterMetrics( - clusterinfo.getInt("appsSubmitted"), clusterinfo.getInt("appsCompleted"), + verifyClusterMetrics(clusterinfo.getInt("appsSubmitted"), clusterinfo.getInt("appsCompleted"), clusterinfo.getInt("reservedMB"), clusterinfo.getInt("availableMB"), clusterinfo.getInt("allocatedMB"), clusterinfo.getInt("pendingMB"), - clusterinfo.getInt("reservedVirtualCores"), - clusterinfo.getInt("availableVirtualCores"), - clusterinfo.getInt("allocatedVirtualCores"), - clusterinfo.getInt("pendingVirtualCores"), - clusterinfo.getInt("totalVirtualCores"), - clusterinfo.getInt("containersAllocated"), + clusterinfo.getInt("reservedVirtualCores"), clusterinfo.getInt("availableVirtualCores"), + clusterinfo.getInt("allocatedVirtualCores"), clusterinfo.getInt("pendingVirtualCores"), + clusterinfo.getInt("totalVirtualCores"), clusterinfo.getInt("containersAllocated"), clusterinfo.getInt("totalMB"), clusterinfo.getInt("totalNodes"), clusterinfo.getInt("lostNodes"), clusterinfo.getInt("unhealthyNodes"), - clusterinfo.getInt("decommissionedNodes"), - clusterinfo.getInt("rebootedNodes"),clusterinfo.getInt("activeNodes"), - clusterinfo.getInt("shutdownNodes")); + clusterinfo.getInt("decommissionedNodes"), clusterinfo.getInt("rebootedNodes"), + clusterinfo.getInt("activeNodes"), clusterinfo.getInt("shutdownNodes")); } - public void verifyClusterMetrics(int submittedApps, int completedApps, - int reservedMB, int availableMB, int allocMB, int pendingMB, - int reservedVirtualCores, int availableVirtualCores, - int allocVirtualCores, int pendingVirtualCores, int totalVirtualCores, - int containersAlloc, int totalMB, int totalNodes, int lostNodes, - int unhealthyNodes, int decommissionedNodes, int rebootedNodes, - int activeNodes, int shutdownNodes) throws JSONException, Exception { + public void verifyClusterMetrics(int submittedApps, int completedApps, int reservedMB, + int availableMB, int allocMB, int pendingMB, int reservedVirtualCores, + int availableVirtualCores, int allocVirtualCores, int pendingVirtualCores, + int totalVirtualCores, int containersAlloc, int totalMB, int totalNodes, int lostNodes, + int unhealthyNodes, int decommissionedNodes, int rebootedNodes, int activeNodes, + int shutdownNodes) throws JSONException, Exception { ResourceScheduler rs = rm.getResourceScheduler(); QueueMetrics metrics = rs.getRootQueueMetrics(); ClusterMetrics clusterMetrics = ClusterMetrics.getMetrics(); - long totalMBExpect = - metrics.getAvailableMB() + metrics.getAllocatedMB(); + long totalMBExpect = metrics.getAvailableMB() + metrics.getAllocatedMB(); long totalVirtualCoresExpect = metrics.getAvailableVirtualCores() + metrics.getAllocatedVirtualCores(); - assertEquals("appsSubmitted doesn't match", - metrics.getAppsSubmitted(), submittedApps); - assertEquals("appsCompleted doesn't match", - metrics.getAppsCompleted(), completedApps); - assertEquals("reservedMB doesn't match", - metrics.getReservedMB(), reservedMB); - assertEquals("availableMB doesn't match", - metrics.getAvailableMB(), availableMB); - assertEquals("allocatedMB doesn't match", - metrics.getAllocatedMB(), allocMB); - assertEquals("pendingMB doesn't match", - metrics.getPendingMB(), pendingMB); - assertEquals("reservedVirtualCores doesn't match", - metrics.getReservedVirtualCores(), reservedVirtualCores); - assertEquals("availableVirtualCores doesn't match", - metrics.getAvailableVirtualCores(), availableVirtualCores); - assertEquals("pendingVirtualCores doesn't match", - metrics.getPendingVirtualCores(), pendingVirtualCores); - assertEquals("allocatedVirtualCores doesn't match", - metrics.getAllocatedVirtualCores(), allocVirtualCores); - assertEquals("totalVirtualCores doesn't match", - totalVirtualCoresExpect, totalVirtualCores); + assertEquals("appsSubmitted doesn't match", metrics.getAppsSubmitted(), submittedApps); + assertEquals("appsCompleted doesn't match", metrics.getAppsCompleted(), completedApps); + assertEquals("reservedMB doesn't match", metrics.getReservedMB(), reservedMB); + assertEquals("availableMB doesn't match", metrics.getAvailableMB(), availableMB); + assertEquals("allocatedMB doesn't match", metrics.getAllocatedMB(), allocMB); + assertEquals("pendingMB doesn't match", metrics.getPendingMB(), pendingMB); + assertEquals("reservedVirtualCores doesn't match", metrics.getReservedVirtualCores(), + reservedVirtualCores); + assertEquals("availableVirtualCores doesn't match", metrics.getAvailableVirtualCores(), + availableVirtualCores); + assertEquals("pendingVirtualCores doesn't match", metrics.getPendingVirtualCores(), + pendingVirtualCores); + assertEquals("allocatedVirtualCores doesn't match", metrics.getAllocatedVirtualCores(), + allocVirtualCores); + assertEquals("totalVirtualCores doesn't match", totalVirtualCoresExpect, totalVirtualCores); assertEquals("containersAllocated doesn't match", 0, containersAlloc); assertEquals("totalMB doesn't match", totalMBExpect, totalMB); - assertEquals( - "totalNodes doesn't match", + assertEquals("totalNodes doesn't match", clusterMetrics.getNumActiveNMs() + clusterMetrics.getNumLostNMs() - + clusterMetrics.getNumDecommisionedNMs() - + clusterMetrics.getNumRebootedNMs() + + clusterMetrics.getNumDecommisionedNMs() + clusterMetrics.getNumRebootedNMs() + clusterMetrics.getUnhealthyNMs(), totalNodes); - assertEquals("lostNodes doesn't match", clusterMetrics.getNumLostNMs(), - lostNodes); - assertEquals("unhealthyNodes doesn't match", - clusterMetrics.getUnhealthyNMs(), unhealthyNodes); - assertEquals("decommissionedNodes doesn't match", - clusterMetrics.getNumDecommisionedNMs(), decommissionedNodes); - assertEquals("rebootedNodes doesn't match", - clusterMetrics.getNumRebootedNMs(), rebootedNodes); - assertEquals("activeNodes doesn't match", clusterMetrics.getNumActiveNMs(), - activeNodes); - assertEquals("shutdownNodes doesn't match", - clusterMetrics.getNumShutdownNMs(), shutdownNodes); + assertEquals("lostNodes doesn't match", clusterMetrics.getNumLostNMs(), lostNodes); + assertEquals("unhealthyNodes doesn't match", clusterMetrics.getUnhealthyNMs(), unhealthyNodes); + assertEquals("decommissionedNodes doesn't match", clusterMetrics.getNumDecommisionedNMs(), + decommissionedNodes); + assertEquals("rebootedNodes doesn't match", clusterMetrics.getNumRebootedNMs(), rebootedNodes); + assertEquals("activeNodes doesn't match", clusterMetrics.getNumActiveNMs(), activeNodes); + assertEquals("shutdownNodes doesn't match", clusterMetrics.getNumShutdownNMs(), shutdownNodes); } @Test public void testClusterSchedulerFifo() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("scheduler").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterSchedulerFifo(json); } @Test public void testClusterSchedulerFifoSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("scheduler/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterSchedulerFifo(json); } @Test public void testClusterSchedulerFifoDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("scheduler").get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler") + .request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterSchedulerFifo(json); } @Test public void testClusterSchedulerFifoXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("scheduler").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = + r.path("ws").path("v1").path("cluster").path("scheduler").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifySchedulerFifoXML(xml); } - public void verifySchedulerFifoXML(String xml) throws JSONException, - Exception { + public void verifySchedulerFifoXML(String xml) throws JSONException, Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -620,8 +582,7 @@ public void verifySchedulerFifoXML(String xml) throws JSONException, for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); - verifyClusterSchedulerFifoGeneric( - WebServicesTestUtils.getXmlAttrString(element, "xsi:type"), + verifyClusterSchedulerFifoGeneric(WebServicesTestUtils.getXmlAttrString(element, "xsi:type"), WebServicesTestUtils.getXmlString(element, "qstate"), WebServicesTestUtils.getXmlFloat(element, "capacity"), WebServicesTestUtils.getXmlFloat(element, "usedCapacity"), @@ -635,8 +596,7 @@ public void verifySchedulerFifoXML(String xml) throws JSONException, } } - public void verifyClusterSchedulerFifo(JSONObject json) throws JSONException, - Exception { + public void verifyClusterSchedulerFifo(JSONObject json) throws JSONException, Exception { assertEquals("incorrect number of elements in: " + json, 1, json.length()); JSONObject info = json.getJSONObject("scheduler"); assertEquals("incorrect number of elements in: " + info, 1, info.length()); @@ -645,33 +605,27 @@ public void verifyClusterSchedulerFifo(JSONObject json) throws JSONException, LOG.debug("schedulerInfo: {}", info); assertEquals("incorrect number of elements in: " + info, 11, info.length()); - verifyClusterSchedulerFifoGeneric(info.getString("type"), - info.getString("qstate"), (float) info.getDouble("capacity"), - (float) info.getDouble("usedCapacity"), - info.getInt("minQueueMemoryCapacity"), - info.getInt("maxQueueMemoryCapacity"), info.getInt("numNodes"), - info.getInt("usedNodeCapacity"), info.getInt("availNodeCapacity"), + verifyClusterSchedulerFifoGeneric(info.getString("type"), info.getString("qstate"), + (float) info.getDouble("capacity"), (float) info.getDouble("usedCapacity"), + info.getInt("minQueueMemoryCapacity"), info.getInt("maxQueueMemoryCapacity"), + info.getInt("numNodes"), info.getInt("usedNodeCapacity"), info.getInt("availNodeCapacity"), info.getInt("totalNodeCapacity"), info.getInt("numContainers")); } - public void verifyClusterSchedulerFifoGeneric(String type, String state, - float capacity, float usedCapacity, int minQueueCapacity, - int maxQueueCapacity, int numNodes, int usedNodeCapacity, - int availNodeCapacity, int totalNodeCapacity, int numContainers) + public void verifyClusterSchedulerFifoGeneric(String type, String state, float capacity, + float usedCapacity, int minQueueCapacity, int maxQueueCapacity, int numNodes, + int usedNodeCapacity, int availNodeCapacity, int totalNodeCapacity, int numContainers) throws JSONException, Exception { assertEquals("type doesn't match", "fifoScheduler", type); assertEquals("qstate doesn't match", QueueState.RUNNING.toString(), state); assertEquals("capacity doesn't match", 1.0, capacity, 0.0); assertEquals("usedCapacity doesn't match", 0.0, usedCapacity, 0.0); - assertEquals( - "minQueueMemoryCapacity doesn't match", - YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB, - minQueueCapacity); + assertEquals("minQueueMemoryCapacity doesn't match", + YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB, minQueueCapacity); assertEquals("maxQueueMemoryCapacity doesn't match", - YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, - maxQueueCapacity); + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, maxQueueCapacity); assertEquals("numNodes doesn't match", 0, numNodes); assertEquals("usedNodeCapacity doesn't match", 0, usedNodeCapacity); assertEquals("availNodeCapacity doesn't match", 0, availNodeCapacity); @@ -689,37 +643,35 @@ public void testAppsRace() throws Exception { ApplicationId appId = ApplicationId.newInstance(1, 1); ApplicationReport mockReport = mock(ApplicationReport.class); when(mockReport.getApplicationId()).thenReturn(appId); - GetApplicationsResponse mockAppsResponse = - mock(GetApplicationsResponse.class); - when(mockAppsResponse.getApplicationList()) - .thenReturn(Arrays.asList(new ApplicationReport[] { mockReport })); + GetApplicationsResponse mockAppsResponse = mock(GetApplicationsResponse.class); + when(mockAppsResponse.getApplicationList()).thenReturn( + Arrays.asList(new ApplicationReport[] { mockReport })); ClientRMService mockClientSvc = mock(ClientRMService.class); - when(mockClientSvc.getApplications(isA(GetApplicationsRequest.class))) - .thenReturn(mockAppsResponse); + when(mockClientSvc.getApplications(isA(GetApplicationsRequest.class))).thenReturn( + mockAppsResponse); ResourceManager mockRM = mock(ResourceManager.class); - RMContextImpl rmContext = new RMContextImpl(null, null, null, null, null, - null, null, null, null, null); + RMContextImpl rmContext = + new RMContextImpl(null, null, null, null, null, null, null, null, null, null); when(mockRM.getRMContext()).thenReturn(rmContext); when(mockRM.getClientRMService()).thenReturn(mockClientSvc); rmContext.setNodeLabelManager(mock(RMNodeLabelsManager.class)); - RMWebServices webSvc = new RMWebServices(mockRM, new Configuration(), - mock(HttpServletResponse.class)); + RMWebServices webSvc = + new RMWebServices(mockRM, new Configuration(), mock(HttpServletResponse.class)); - final Set emptySet = - Collections.unmodifiableSet(Collections.emptySet()); + final Set emptySet = Collections.unmodifiableSet(Collections.emptySet()); // verify we don't get any apps when querying HttpServletRequest mockHsr = mock(HttpServletRequest.class); - AppsInfo appsInfo = webSvc.getApps(mockHsr, null, emptySet, null, - null, null, null, null, null, null, null, emptySet, emptySet, null, - null); + AppsInfo appsInfo = + webSvc.getApps(mockHsr, null, emptySet, null, null, null, null, null, null, null, null, + emptySet, emptySet, null, null); assertTrue(appsInfo.getApps().isEmpty()); // verify we don't get an NPE when specifying a final status query - appsInfo = webSvc.getApps(mockHsr, null, emptySet, "FAILED", - null, null, null, null, null, null, null, emptySet, emptySet, null, - null); + appsInfo = + webSvc.getApps(mockHsr, null, emptySet, "FAILED", null, null, null, null, null, null, null, + emptySet, emptySet, null, null); assertTrue(appsInfo.getApps().isEmpty()); } @@ -731,8 +683,7 @@ public void testDumpingSchedulerLogs() throws Exception { HttpServletRequest mockHsr = mockHttpServletRequestByUserName("non-admin"); ApplicationACLsManager aclsManager = new ApplicationACLsManager(conf); when(mockRM.getApplicationACLsManager()).thenReturn(aclsManager); - RMWebServices webSvc = - new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); + RMWebServices webSvc = new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); // nothing should happen webSvc.dumpSchedulerLogs("1", mockHsr); @@ -823,8 +774,8 @@ public void testCheckUserAccessToQueue() throws Exception { // For SUBMIT_APPLICATION ACL, both of admin/yarn user have acess ResourceScheduler mockScheduler = new FifoScheduler() { @Override - public synchronized boolean checkAccess(UserGroupInformation callerUGI, - QueueACL acl, String queueName) { + public synchronized boolean checkAccess(UserGroupInformation callerUGI, QueueACL acl, + String queueName) { if (acl == QueueACL.ADMINISTER_QUEUE) { if (callerUGI.getUserName().equals("admin")) { return true; @@ -840,16 +791,14 @@ public synchronized boolean checkAccess(UserGroupInformation callerUGI, when(mockRM.getResourceScheduler()).thenReturn(mockScheduler); - RMWebServices webSvc = - new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); + RMWebServices webSvc = new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); boolean caughtException = false; // Case 1: Only queue admin user can access other user's information HttpServletRequest mockHsr = mockHttpServletRequestByUserName("non-admin"); try { - webSvc.checkUserAccessToQueue("queue", "jack", - QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr); + webSvc.checkUserAccessToQueue("queue", "jack", QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr); } catch (ForbiddenException e) { caughtException = true; } @@ -867,24 +816,30 @@ public synchronized boolean checkAccess(UserGroupInformation callerUGI, // Case 3: get FORBIDDEN for rejected ACL mockHsr = mockHttpServletRequestByUserName("admin"); - Assert.assertFalse(webSvc.checkUserAccessToQueue("queue", "jack", - QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).isAllowed()); - Assert.assertFalse(webSvc.checkUserAccessToQueue("queue", "jack", - QueueACL.ADMINISTER_QUEUE.name(), mockHsr).isAllowed()); + Assert.assertFalse( + webSvc.checkUserAccessToQueue("queue", "jack", QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr) + .isAllowed()); + Assert.assertFalse( + webSvc.checkUserAccessToQueue("queue", "jack", QueueACL.ADMINISTER_QUEUE.name(), mockHsr) + .isAllowed()); // Case 4: get OK for listed ACLs mockHsr = mockHttpServletRequestByUserName("admin"); - Assert.assertTrue(webSvc.checkUserAccessToQueue("queue", "admin", - QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).isAllowed()); - Assert.assertTrue(webSvc.checkUserAccessToQueue("queue", "admin", - QueueACL.ADMINISTER_QUEUE.name(), mockHsr).isAllowed()); + Assert.assertTrue( + webSvc.checkUserAccessToQueue("queue", "admin", QueueACL.SUBMIT_APPLICATIONS.name(), + mockHsr).isAllowed()); + Assert.assertTrue( + webSvc.checkUserAccessToQueue("queue", "admin", QueueACL.ADMINISTER_QUEUE.name(), mockHsr) + .isAllowed()); // Case 5: get OK only for SUBMIT_APP acl for "yarn" user mockHsr = mockHttpServletRequestByUserName("admin"); - Assert.assertTrue(webSvc.checkUserAccessToQueue("queue", "yarn", - QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr).isAllowed()); - Assert.assertFalse(webSvc.checkUserAccessToQueue("queue", "yarn", - QueueACL.ADMINISTER_QUEUE.name(), mockHsr).isAllowed()); + Assert.assertTrue( + webSvc.checkUserAccessToQueue("queue", "yarn", QueueACL.SUBMIT_APPLICATIONS.name(), mockHsr) + .isAllowed()); + Assert.assertFalse( + webSvc.checkUserAccessToQueue("queue", "yarn", QueueACL.ADMINISTER_QUEUE.name(), mockHsr) + .isAllowed()); } @Test @@ -893,8 +848,7 @@ public void testClusterUserInfo() throws JSONException, Exception { Configuration conf = new YarnConfiguration(); HttpServletRequest mockHsr = mockHttpServletRequestByUserName("admin"); when(mockRM.getRMLoginUser()).thenReturn("yarn"); - RMWebServices webSvc = - new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); + RMWebServices webSvc = new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); ClusterUserInfo userInfo = webSvc.getClusterUserInfo(mockHsr); verifyClusterUserInfo(userInfo, "yarn", "admin"); } @@ -904,44 +858,39 @@ public void testInvalidXMLChars() throws Exception { ResourceManager mockRM = mock(ResourceManager.class); ApplicationId applicationId = ApplicationId.newInstance(1234, 5); - ApplicationReport appReport = ApplicationReport.newInstance( - applicationId, ApplicationAttemptId.newInstance(applicationId, 1), - "user", "queue", "appname", "host", 124, null, - YarnApplicationState.FAILED, "java.lang.Exception: \u0001", "url", - 0, 0, 0, FinalApplicationStatus.FAILED, null, "N/A", 0.53789f, "YARN", - null, null, false, Priority.newInstance(0), "high-mem", "high-mem"); + ApplicationReport appReport = ApplicationReport.newInstance(applicationId, + ApplicationAttemptId.newInstance(applicationId, 1), "user", "queue", "appname", "host", 124, + null, YarnApplicationState.FAILED, "java.lang.Exception: \u0001", "url", 0, 0, 0, + FinalApplicationStatus.FAILED, null, "N/A", 0.53789f, "YARN", null, null, false, + Priority.newInstance(0), "high-mem", "high-mem"); List appReports = new ArrayList(); appReports.add(appReport); GetApplicationsResponse response = mock(GetApplicationsResponse.class); when(response.getApplicationList()).thenReturn(appReports); ClientRMService clientRMService = mock(ClientRMService.class); - when(clientRMService.getApplications(any(GetApplicationsRequest.class))) - .thenReturn(response); + when(clientRMService.getApplications(any(GetApplicationsRequest.class))).thenReturn(response); when(mockRM.getClientRMService()).thenReturn(clientRMService); RMContext rmContext = mock(RMContext.class); when(rmContext.getDispatcher()).thenReturn(mock(Dispatcher.class)); - ApplicationSubmissionContext applicationSubmissionContext = mock( - ApplicationSubmissionContext.class); + ApplicationSubmissionContext applicationSubmissionContext = + mock(ApplicationSubmissionContext.class); when(applicationSubmissionContext.getUnmanagedAM()).thenReturn(true); RMApp app = mock(RMApp.class); - RMAppMetrics appMetrics = new RMAppMetrics(Resource.newInstance(0, 0), - 0, 0, new HashMap<>(), new HashMap<>(), 0); - when(app.getDiagnostics()).thenReturn( - new StringBuilder("java.lang.Exception: \u0001")); + RMAppMetrics appMetrics = + new RMAppMetrics(Resource.newInstance(0, 0), 0, 0, new HashMap<>(), new HashMap<>(), 0); + when(app.getDiagnostics()).thenReturn(new StringBuilder("java.lang.Exception: \u0001")); when(app.getApplicationId()).thenReturn(applicationId); when(app.getUser()).thenReturn("user"); when(app.getName()).thenReturn("appname"); when(app.getQueue()).thenReturn("queue"); when(app.getRMAppMetrics()).thenReturn(appMetrics); - when(app.getApplicationSubmissionContext()).thenReturn( - applicationSubmissionContext); + when(app.getApplicationSubmissionContext()).thenReturn(applicationSubmissionContext); - ConcurrentMap applications = - new ConcurrentHashMap<>(); + ConcurrentMap applications = new ConcurrentHashMap<>(); applications.put(applicationId, app); when(rmContext.getRMApps()).thenReturn(applications); @@ -949,70 +898,61 @@ public void testInvalidXMLChars() throws Exception { Configuration conf = new YarnConfiguration(); conf.setBoolean(YarnConfiguration.FILTER_INVALID_XML_CHARS, true); - RMWebServices webSvc = new RMWebServices(mockRM, conf, mock( - HttpServletResponse.class)); + RMWebServices webSvc = new RMWebServices(mockRM, conf, mock(HttpServletResponse.class)); HttpServletRequest mockHsr = mock(HttpServletRequest.class); - when(mockHsr.getHeader(HttpHeaders.ACCEPT)). - thenReturn(MediaType.APPLICATION_XML); + when(mockHsr.getHeader(HttpHeaders.ACCEPT)).thenReturn(MediaType.APPLICATION_XML); Set emptySet = Collections.unmodifiableSet(Collections.emptySet()); - AppsInfo appsInfo = webSvc.getApps(mockHsr, null, emptySet, null, - null, null, null, null, null, null, null, emptySet, emptySet, - null, null); + AppsInfo appsInfo = + webSvc.getApps(mockHsr, null, emptySet, null, null, null, null, null, null, null, null, + emptySet, emptySet, null, null); assertEquals("Incorrect Number of Apps", 1, appsInfo.getApps().size()); - assertEquals("Invalid XML Characters Present", - "java.lang.Exception: \uFFFD", appsInfo.getApps().get(0).getNote()); + assertEquals("Invalid XML Characters Present", "java.lang.Exception: \uFFFD", + appsInfo.getApps().get(0).getNote()); } @Test public void testDisableRestAppSubmission() throws Exception { Configuration conf = new YarnConfiguration(); conf.setBoolean(YarnConfiguration.ENABLE_REST_APP_SUBMISSIONS, false); - RMWebServices webSvc = new RMWebServices(mock(ResourceManager.class), conf, - mock(HttpServletResponse.class)); + RMWebServices webSvc = + new RMWebServices(mock(ResourceManager.class), conf, mock(HttpServletResponse.class)); HttpServletRequest request = mock(HttpServletRequest.class); Response response = webSvc.createNewApplication(request); assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus()); assertEquals("App submission via REST is disabled.", response.getEntity()); - response = webSvc.submitApplication( - mock(ApplicationSubmissionContextInfo.class), request); + response = webSvc.submitApplication(mock(ApplicationSubmissionContextInfo.class), request); assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus()); assertEquals("App submission via REST is disabled.", response.getEntity()); } - public void verifyClusterUserInfo(ClusterUserInfo userInfo, - String rmLoginUser, String requestedUser) { - assertEquals("rmLoginUser doesn't match: ", - rmLoginUser, userInfo.getRmLoginUser()); - assertEquals("requestedUser doesn't match: ", - requestedUser, userInfo.getRequestedUser()); + public void verifyClusterUserInfo(ClusterUserInfo userInfo, String rmLoginUser, + String requestedUser) { + assertEquals("rmLoginUser doesn't match: ", rmLoginUser, userInfo.getRmLoginUser()); + assertEquals("requestedUser doesn't match: ", requestedUser, userInfo.getRequestedUser()); } @Test public void testValidateAndGetSchedulerConfigurationInvalidScheduler() - throws AuthorizationException { + throws AuthorizationException { ResourceScheduler scheduler = new CapacityScheduler(); RMWebServices webService = prepareWebServiceForValidation(scheduler); SchedConfUpdateInfo mutationInfo = new SchedConfUpdateInfo(); HttpServletRequest mockHsr = prepareServletRequestForValidation(); - Response response = webService - .validateAndGetSchedulerConfiguration(mutationInfo, mockHsr); - Assert.assertEquals(Status.BAD_REQUEST - .getStatusCode(), response.getStatus()); - Assert.assertTrue(response.getEntity().toString() - .contains(String.format("Configuration change validation only supported by %s.", - MutableConfScheduler.class.getSimpleName()))); + Response response = webService.validateAndGetSchedulerConfiguration(mutationInfo, mockHsr); + Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + Assert.assertTrue(response.getEntity().toString().contains( + String.format("Configuration change validation only supported by %s.", + MutableConfScheduler.class.getSimpleName()))); } @Test - public void testValidateAndGetSchedulerConfigurationInvalidConfig() - throws IOException { - Configuration config = CapacitySchedulerConfigGeneratorForTest - .createBasicCSConfiguration(); + public void testValidateAndGetSchedulerConfigurationInvalidConfig() throws IOException { + Configuration config = CapacitySchedulerConfigGeneratorForTest.createBasicCSConfiguration(); ResourceScheduler scheduler = prepareCSForValidation(config); SchedConfUpdateInfo mutationInfo = new SchedConfUpdateInfo(); @@ -1023,22 +963,16 @@ public void testValidateAndGetSchedulerConfigurationInvalidConfig() RMWebServices webService = prepareWebServiceForValidation(scheduler); HttpServletRequest mockHsr = prepareServletRequestForValidation(); - Response response = webService - .validateAndGetSchedulerConfiguration(mutationInfo, mockHsr); - Assert.assertEquals(Status.BAD_REQUEST - .getStatusCode(), response.getStatus()); - Assert.assertTrue(response.getEntity().toString() - .contains("IOException")); + Response response = webService.validateAndGetSchedulerConfiguration(mutationInfo, mockHsr); + Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + Assert.assertTrue(response.getEntity().toString().contains("IOException")); } @Test - public void testValidateAndGetSchedulerConfigurationValidScheduler() - throws IOException { - Configuration config = CapacitySchedulerConfigGeneratorForTest - .createBasicCSConfiguration(); + public void testValidateAndGetSchedulerConfigurationValidScheduler() throws IOException { + Configuration config = CapacitySchedulerConfigGeneratorForTest.createBasicCSConfiguration(); config.set("yarn.scheduler.capacity.root.test1.state", "STOPPED"); - config.set("yarn.scheduler.capacity.queue-mappings", - "u:test2:test2"); + config.set("yarn.scheduler.capacity.queue-mappings", "u:test2:test2"); ResourceScheduler scheduler = prepareCSForValidation(config); SchedConfUpdateInfo mutationInfo = new SchedConfUpdateInfo(); @@ -1055,26 +989,20 @@ public void testValidateAndGetSchedulerConfigurationValidScheduler() RMWebServices webService = prepareWebServiceForValidation(scheduler); HttpServletRequest mockHsr = prepareServletRequestForValidation(); - Response response = webService - .validateAndGetSchedulerConfiguration(mutationInfo, mockHsr); - Assert.assertEquals(Status.OK - .getStatusCode(), response.getStatus()); + Response response = webService.validateAndGetSchedulerConfiguration(mutationInfo, mockHsr); + Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus()); } - private CapacityScheduler prepareCSForValidation(Configuration config) - throws IOException { + private CapacityScheduler prepareCSForValidation(Configuration config) throws IOException { CapacityScheduler scheduler = mock(CapacityScheduler.class); - when(scheduler.isConfigurationMutable()) - .thenReturn(true); + when(scheduler.isConfigurationMutable()).thenReturn(true); MutableCSConfigurationProvider configurationProvider = - mock(MutableCSConfigurationProvider.class); - when(scheduler.getMutableConfProvider()) - .thenReturn(configurationProvider); + mock(MutableCSConfigurationProvider.class); + when(scheduler.getMutableConfProvider()).thenReturn(configurationProvider); when(configurationProvider.getConfiguration()).thenReturn(config); when(scheduler.getConf()).thenReturn(config); - when(configurationProvider - .applyChanges(any(), any())).thenCallRealMethod(); + when(configurationProvider.applyChanges(any(), any())).thenCallRealMethod(); return scheduler; } @@ -1084,19 +1012,18 @@ private HttpServletRequest prepareServletRequestForValidation() { return mockHsr; } - private RMWebServices prepareWebServiceForValidation( - ResourceScheduler scheduler) { + private RMWebServices prepareWebServiceForValidation(ResourceScheduler scheduler) { ResourceManager mockRM = mock(ResourceManager.class); ApplicationACLsManager acLsManager = mock(ApplicationACLsManager.class); - RMWebServices webService = new RMWebServices(mockRM, new Configuration(), - mock(HttpServletResponse.class)); + RMWebServices webService = + new RMWebServices(mockRM, new Configuration(), mock(HttpServletResponse.class)); when(mockRM.getResourceScheduler()).thenReturn(scheduler); when(acLsManager.areACLsEnabled()).thenReturn(false); when(mockRM.getApplicationACLsManager()).thenReturn(acLsManager); RMContext context = TestCapacitySchedulerConfigValidator.prepareRMContext(); when(mockRM.getRMContext()).thenReturn(context); - return webService; + return webService; } } \ No newline at end of file diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppAttempts.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppAttempts.java index 102f13897fc79..f20d5d60c3820 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppAttempts.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppAttempts.java @@ -17,12 +17,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.yarn.api.records.ContainerState; @@ -41,10 +38,10 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -52,7 +49,10 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; @@ -65,7 +65,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -public class TestRMWebServicesAppAttempts extends JerseyTestBase { +public class TestRMWebServicesAppAttempts extends JerseyTest { private static MockRM rm; @@ -84,7 +84,7 @@ protected void configureServlets() { ResourceScheduler.class); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -102,11 +102,6 @@ public void setUp() throws Exception { } public TestRMWebServicesAppAttempts() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test @@ -146,12 +141,12 @@ public void testCompletedAppAttempt() throws Exception { rm.waitForState(am.getApplicationAttemptId(), RMAppAttemptState.FAILED); rm.waitForState(app1.getApplicationId(), RMAppState.FAILED); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").path(app1.getApplicationId().toString()) - .path("appattempts").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); - JSONObject json = response.getEntity(JSONObject.class); + .path("appattempts").request(MediaType.APPLICATION_JSON) + .get(Response.class); + JSONObject json = response.readEntity(JSONObject.class); JSONObject jsonAppAttempts = json.getJSONObject("appAttempts"); JSONArray jsonArray = jsonAppAttempts.getJSONArray("appAttempt"); JSONObject info = jsonArray.getJSONObject(0); @@ -228,22 +223,22 @@ public void testInvalidAppIdGetAttempts() throws Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); RMApp app = MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") .path("application_invalid_12").path("appattempts") - .accept(MediaType.APPLICATION_JSON) + .request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid appAttempt"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); - assertResponseStatusCode(ClientResponse.Status.BAD_REQUEST, + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -269,22 +264,22 @@ public void testInvalidAppAttemptId() throws Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); RMApp app = MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") .path(app.getApplicationId().toString()).path("appattempts") .path("appattempt_invalid_12_000001") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid appAttempt"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); - assertResponseStatusCode(ClientResponse.Status.BAD_REQUEST, + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -315,22 +310,22 @@ public void testNonexistAppAttempts() throws Exception { .build(); MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") - .path("application_00000_0099").accept(MediaType.APPLICATION_JSON) + .path("application_00000_0099").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid appid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); - assertResponseStatusCode(ClientResponse.Status.NOT_FOUND, + assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -350,13 +345,13 @@ public void testNonexistAppAttempts() throws Exception { private void testAppAttemptsHelper(String path, RMApp app, String media) throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").path(path).path("appattempts").accept(media) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("apps").path(path).path("appattempts").request(media) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject jsonAppAttempts = json.getJSONObject("appAttempts"); assertEquals("incorrect number of elements", 1, jsonAppAttempts.length()); @@ -386,14 +381,14 @@ public void testAppAttemptsXML() throws Exception { .build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").path(app1.getApplicationId().toString()) - .path("appattempts").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + .path("appattempts").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppCustomResourceTypes.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppCustomResourceTypes.java index 5dcae89e8ba0f..9d82afbe45f45 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppCustomResourceTypes.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppCustomResourceTypes.java @@ -17,11 +17,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.MockAM; @@ -33,21 +31,23 @@ import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler; -import org.apache.hadoop.yarn.util.resource.CustomResourceTypesConfigurationProvider; import org.apache.hadoop.yarn.server.resourcemanager.webapp.helper.BufferedClientResponse; +import org.apache.hadoop.yarn.util.resource.CustomResourceTypesConfigurationProvider; import org.apache.hadoop.yarn.server.resourcemanager.webapp.helper.JsonCustomResourceTypeTestcase; import org.apache.hadoop.yarn.server.resourcemanager.webapp.helper.XmlCustomResourceTypeTestCase; import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.NodeList; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.util.ArrayList; import static org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesCustomResourceTypesCommons.verifyAppInfoJson; @@ -58,7 +58,7 @@ * This test verifies that custom resource types are correctly serialized to XML * and JSON when HTTP GET request is sent to the resource: ws/v1/cluster/apps. */ -public class TestRMWebServicesAppCustomResourceTypes extends JerseyTestBase { +public class TestRMWebServicesAppCustomResourceTypes extends JerseyTest { private static MockRM rm; private static final int CONTAINER_MB = 1024; @@ -77,7 +77,7 @@ protected void configureServlets() { initResourceTypes(conf); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } private void initResourceTypes(Configuration conf) { @@ -100,16 +100,11 @@ private void createInjectorForWebServletModule() { } public TestRMWebServicesAppCustomResourceTypes() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } - private WebResource getWebResourcePathForApp(RMApp app1, WebResource r) { - return r.path("ws").path("v1").path("cluster").path("apps") - .path(String.valueOf(app1.getApplicationId().toString())); + private WebTarget getWebResourcePathForApp(RMApp app1, WebTarget target) { + return target.path("ws").path("v1").path("cluster").path("apps") + .path(String.valueOf(app1.getApplicationId().toString())); } @Test @@ -126,10 +121,9 @@ public void testRunningAppXml() throws Exception { am1.allocate("*", 2048, 1, new ArrayList<>()); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - WebResource path = getWebResourcePathForApp(app1, r); - ClientResponse response = - path.accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + WebTarget r = target(); + WebTarget path = getWebResourcePathForApp(app1, r); + Response response = path.request(MediaType.APPLICATION_XML).get(Response.class); XmlCustomResourceTypeTestCase testCase = new XmlCustomResourceTypeTestCase(path, @@ -159,10 +153,10 @@ public void testRunningAppJson() throws Exception { am1.allocate("*", 2048, 1, new ArrayList<>()); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - WebResource path = getWebResourcePathForApp(app1, r); - ClientResponse response = - path.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + WebTarget path = getWebResourcePathForApp(app1, r); + Response response = + path.request(MediaType.APPLICATION_JSON).get(Response.class); JsonCustomResourceTypeTestcase testCase = new JsonCustomResourceTypeTestcase(path, diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesApps.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesApps.java index 4c859baf78906..6f4bd7d46e1fe 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesApps.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesApps.java @@ -19,14 +19,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.security.UserGroupInformation; @@ -51,11 +46,11 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.AppInfo; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -63,7 +58,11 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; @@ -76,7 +75,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -public class TestRMWebServicesApps extends JerseyTestBase { +public class TestRMWebServicesApps extends JerseyTest { private static MockRM rm; @@ -105,7 +104,7 @@ protected void configureServlets() { ResourceScheduler.class); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -133,11 +132,6 @@ public void setUp() throws Exception { } public TestRMWebServicesApps() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test @@ -181,13 +175,13 @@ public void testAppsXML() throws JSONException, Exception { .build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("apps").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -215,13 +209,13 @@ public void testRunningApp() throws JSONException, Exception { am1.allocate("*", 4096, 1, new ArrayList<>()); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("apps").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -255,14 +249,14 @@ public void testAppsXMLMulti() throws JSONException, Exception { MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path("apps").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -282,13 +276,13 @@ public void testAppsHelper(String path, RMApp app, String media) public void testAppsHelper(String path, RMApp app, String media, boolean hasResourceReq) throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path(path).accept(media).get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path(path).request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -304,15 +298,15 @@ public void testAppsQueryState() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); RMApp app1 = MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("state", YarnApplicationState.ACCEPTED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -332,15 +326,13 @@ public void testAppsQueryStates() throws JSONException, Exception { amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("states", YarnApplicationState.ACCEPTED.toString()); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("states", YarnApplicationState.ACCEPTED.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -349,16 +341,14 @@ public void testAppsQueryStates() throws JSONException, Exception { assertEquals("state not equal to ACCEPTED", "ACCEPTED", array .getJSONObject(0).getString("state")); - r = resource(); - params = new MultivaluedMapImpl(); - params.add("states", YarnApplicationState.ACCEPTED.toString()); - params.add("states", YarnApplicationState.KILLED.toString()); - response = r.path("ws").path("v1").path("cluster") - .path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + r = target(); + response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("states", YarnApplicationState.ACCEPTED.toString()) + .queryParam("states", YarnApplicationState.KILLED.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -383,15 +373,13 @@ public void testAppsQueryStatesComma() throws JSONException, Exception { amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("states", YarnApplicationState.ACCEPTED.toString()); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("states", YarnApplicationState.ACCEPTED.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -400,16 +388,13 @@ public void testAppsQueryStatesComma() throws JSONException, Exception { assertEquals("state not equal to ACCEPTED", "ACCEPTED", array .getJSONObject(0).getString("state")); - r = resource(); - params = new MultivaluedMapImpl(); - params.add("states", YarnApplicationState.ACCEPTED.toString() + "," - + YarnApplicationState.KILLED.toString()); - response = r.path("ws").path("v1").path("cluster") - .path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + r = target(); + response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("states", YarnApplicationState.ACCEPTED + "," + YarnApplicationState.KILLED) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -430,15 +415,15 @@ public void testAppsQueryStatesNone() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("states", YarnApplicationState.RUNNING.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("apps is not empty", new JSONObject().toString(), json.get("apps").toString()); @@ -451,15 +436,15 @@ public void testAppsQueryStateNone() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("state", YarnApplicationState.RUNNING.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("apps is not empty", new JSONObject().toString(), json.get("apps").toString()); @@ -472,19 +457,19 @@ public void testAppsQueryStatesInvalid() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") .queryParam("states", "INVALID_test") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid state query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -510,19 +495,19 @@ public void testAppsQueryStateInvalid() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") .queryParam("state", "INVALID_test") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid state query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -548,15 +533,15 @@ public void testAppsQueryFinalStatus() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); RMApp app1 = MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("finalStatus", FinalApplicationStatus.UNDEFINED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); System.out.println(json.toString()); JSONObject apps = json.getJSONObject("apps"); @@ -573,14 +558,14 @@ public void testAppsQueryFinalStatusNone() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("finalStatus", FinalApplicationStatus.KILLED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("apps is not null", new JSONObject().toString(), json.get("apps").toString()); @@ -593,19 +578,19 @@ public void testAppsQueryFinalStatusInvalid() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") .queryParam("finalStatus", "INVALID_test") - .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + .request(MediaType.APPLICATION_JSON).get(JSONObject.class); fail("should have thrown exception on invalid state query"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -634,18 +619,18 @@ public void testAppsQueryUser() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r + WebTarget r = target(); + Response response = r .path("ws") .path("v1") .path("cluster") .path("apps") .queryParam("user", UserGroupInformation.getCurrentUser().getShortUserName()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); @@ -663,14 +648,14 @@ public void testAppsQueryQueue() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("queue", "default") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -690,16 +675,16 @@ public void testAppsQueryQueueAndStateTwoFinishedApps() throws Exception { finishApp(amNodeManager, app1); finishApp(amNodeManager, app2); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("queue", "default") .queryParam("state", YarnApplicationState.FINISHED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -725,16 +710,16 @@ public void testAppsQueryQueueAndStateOneFinishedApp() throws Exception { finishApp(amNodeManager, finishedApp); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("queue", "default") .queryParam("state", YarnApplicationState.FINISHED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -761,15 +746,15 @@ public void testAppsQueryQueueOneFinishedApp() throws Exception { finishApp(amNodeManager, finishedApp); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("queue", "default") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -793,13 +778,13 @@ public void testAppsQueryLimit() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("limit", "2") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -817,13 +802,13 @@ public void testAppsQueryStartBegin() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("startedTimeBegin", String.valueOf(start)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -841,13 +826,13 @@ public void testAppsQueryStartBeginSome() throws JSONException, Exception { long start = System.currentTimeMillis(); Thread.sleep(1); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("startedTimeBegin", String.valueOf(start)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -865,13 +850,13 @@ public void testAppsQueryStartEnd() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("startedTimeEnd", String.valueOf(end)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("apps is not empty", new JSONObject().toString(), json.get("apps").toString()); @@ -889,14 +874,14 @@ public void testAppsQueryStartBeginEnd() throws JSONException, Exception { long end = System.currentTimeMillis(); Thread.sleep(1); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("startedTimeBegin", String.valueOf(start)) .queryParam("startedTimeEnd", String.valueOf(end)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -917,13 +902,13 @@ public void testAppsQueryFinishBegin() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("finishedTimeBegin", String.valueOf(start)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -955,13 +940,13 @@ public void testAppsQueryFinishEnd() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); long end = System.currentTimeMillis(); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("finishedTimeEnd", String.valueOf(end)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -985,14 +970,14 @@ public void testAppsQueryFinishBeginEnd() throws JSONException, Exception { MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); long end = System.currentTimeMillis(); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("finishedTimeBegin", String.valueOf(start)) .queryParam("finishedTimeEnd", String.valueOf(end)) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1036,13 +1021,13 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { .withAppType("NON-YARN") .build()); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("applicationTypes", "MAPREDUCE") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1051,15 +1036,15 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { assertEquals("MAPREDUCE", array.getJSONObject(0).getString("applicationType")); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", "YARN") .queryParam("applicationTypes", "MAPREDUCE") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1072,14 +1057,14 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { && array.getJSONObject(0).getString("applicationType") .equals("MAPREDUCE"))); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", "YARN,NON-YARN") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1092,43 +1077,43 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { && array.getJSONObject(0).getString("applicationType") .equals("NON-YARN"))); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("applicationTypes", "") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); array = apps.getJSONArray("app"); assertEquals("incorrect number of elements", 3, array.length()); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", "YARN,NON-YARN") .queryParam("applicationTypes", "MAPREDUCE") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); array = apps.getJSONArray("app"); assertEquals("incorrect number of elements", 3, array.length()); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", "YARN") .queryParam("applicationTypes", "") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1137,14 +1122,14 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { assertEquals("YARN", array.getJSONObject(0).getString("applicationType")); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", ",,, ,, YARN ,, ,") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1153,28 +1138,28 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { assertEquals("YARN", array.getJSONObject(0).getString("applicationType")); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", ",,, ,, ,, ,") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); array = apps.getJSONArray("app"); assertEquals("incorrect number of elements", 3, array.length()); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", "YARN, ,NON-YARN, ,,") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1187,15 +1172,15 @@ public void testAppsQueryAppTypes() throws JSONException, Exception { && array.getJSONObject(0).getString("applicationType") .equals("NON-YARN"))); - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster").path("apps") .queryParam("applicationTypes", " YARN, , ,,,") .queryParam("applicationTypes", "MAPREDUCE , ,, ,") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1219,14 +1204,14 @@ public void testAppsQueryWithInvalidDeselects() MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("deSelects", "INVALIED_deSelectsParam") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -1252,18 +1237,15 @@ public void testAppsQueryWithDeselects() MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("deSelects", - DeSelectFields.DeSelectType.RESOURCE_REQUESTS.toString()); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + + Response response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("deSelects", DeSelectFields.DeSelectType.RESOURCE_REQUESTS.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1273,16 +1255,13 @@ public void testAppsQueryWithDeselects() assertTrue("resource requests shouldn't exist", !app.has("resourceRequests")); - params.clear(); - params.add("deSelects", - DeSelectFields.DeSelectType.AM_NODE_LABEL_EXPRESSION.toString()); - response = - r.path("ws").path("v1").path("cluster").path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("deSelects", DeSelectFields.DeSelectType.AM_NODE_LABEL_EXPRESSION.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - json = response.getEntity(JSONObject.class); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1292,15 +1271,13 @@ public void testAppsQueryWithDeselects() assertTrue("AMNodeLabelExpression shouldn't exist", !app.has("amNodeLabelExpression")); - params.clear(); - params.add("deSelects", DeSelectFields.DeSelectType.TIMEOUTS.toString()); - response = - r.path("ws").path("v1").path("cluster").path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("deSelects", DeSelectFields.DeSelectType.TIMEOUTS.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - json = response.getEntity(JSONObject.class); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1310,16 +1287,13 @@ public void testAppsQueryWithDeselects() assertTrue("Timeouts shouldn't exist", !app.has("timeouts")); rm.stop(); - params.clear(); - params.add("deSelects", - DeSelectFields.DeSelectType.APP_NODE_LABEL_EXPRESSION.toString()); - response = - r.path("ws").path("v1").path("cluster").path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("deSelects", DeSelectFields.DeSelectType.APP_NODE_LABEL_EXPRESSION.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - json = response.getEntity(JSONObject.class); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1330,16 +1304,13 @@ public void testAppsQueryWithDeselects() !app.has("appNodeLabelExpression")); rm.stop(); - params.clear(); - params - .add("deSelects", DeSelectFields.DeSelectType.RESOURCE_INFO.toString()); - response = - r.path("ws").path("v1").path("cluster").path("apps").queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("apps") + .queryParam("deSelects", DeSelectFields.DeSelectType.RESOURCE_INFO.toString()) + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - json = response.getEntity(JSONObject.class); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -1397,13 +1368,13 @@ public void testAppStatistics() throws JSONException, Exception { .build()); // zero type, zero state - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("appstatistics") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject appsStatInfo = json.getJSONObject("appStatInfo"); assertEquals("incorrect number of elements", 1, appsStatInfo.length()); @@ -1423,14 +1394,14 @@ public void testAppStatistics() throws JSONException, Exception { } // zero type, one state - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster") .path("appstatistics") .queryParam("states", YarnApplicationState.ACCEPTED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); appsStatInfo = json.getJSONObject("appStatInfo"); assertEquals("incorrect number of elements", 1, appsStatInfo.length()); @@ -1441,14 +1412,14 @@ public void testAppStatistics() throws JSONException, Exception { assertEquals("2", statItems.getJSONObject(0).getString("count")); // one type, zero state - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster") .path("appstatistics") .queryParam("applicationTypes", "MAPREDUCE") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); appsStatInfo = json.getJSONObject("appStatInfo"); assertEquals("incorrect number of elements", 1, appsStatInfo.length()); @@ -1468,15 +1439,15 @@ public void testAppStatistics() throws JSONException, Exception { } // two types, zero state - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster") .path("appstatistics") .queryParam("applicationTypes", "MAPREDUCE,OTHER") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject exception = json.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); @@ -1491,16 +1462,16 @@ public void testAppStatistics() throws JSONException, Exception { "org.apache.hadoop.yarn.webapp.BadRequestException", className); // one type, two states - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster") .path("appstatistics") .queryParam("states", YarnApplicationState.FINISHED.toString() + "," + YarnApplicationState.ACCEPTED.toString()) .queryParam("applicationTypes", "MAPREDUCE") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); appsStatInfo = json.getJSONObject("appStatInfo"); assertEquals("incorrect number of elements", 1, appsStatInfo.length()); @@ -1518,14 +1489,14 @@ public void testAppStatistics() throws JSONException, Exception { assertEquals("1", statItem2.getString("count")); // invalid state - r = resource(); + r = target(); response = r.path("ws").path("v1").path("cluster") .path("appstatistics").queryParam("states", "wrong_state") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); exception = json.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); @@ -1571,12 +1542,12 @@ public void testUnmarshalAppInfo() throws JSONException, Exception { RMApp app1 = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").path(app1.getApplicationId().toString()) - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); - AppInfo appInfo = response.getEntity(AppInfo.class); + AppInfo appInfo = response.readEntity(AppInfo.class); // Check only a few values; all are validated in testSingleApp. assertEquals(app1.getApplicationId().toString(), appInfo.getAppId()); @@ -1615,20 +1586,20 @@ public void testInvalidApp() throws JSONException, Exception { MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); MockRMAppSubmitter.submitWithMemory(CONTAINER_MB, rm); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") - .path("application_invalid_12").accept(MediaType.APPLICATION_JSON) + .path("application_invalid_12").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid appid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -1659,21 +1630,21 @@ public void testNonexistApp() throws JSONException, Exception { .build(); MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("apps") - .path("application_00000_0099").accept(MediaType.APPLICATION_JSON) + .path("application_00000_0099").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid appid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -1693,12 +1664,12 @@ public void testNonexistApp() throws JSONException, Exception { public void testSingleAppsHelper(String path, RMApp app, String media) throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("apps").path(path).accept(media).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("apps").path(path).request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); verifyAppInfo(json.getJSONObject("app"), app, false); @@ -1715,13 +1686,13 @@ public void testSingleAppsXML() throws JSONException, Exception { .build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") .path("apps").path(app1.getApplicationId().toString()) - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); @@ -2018,15 +1989,15 @@ public void testAppsQueryByQueueShortname() throws Exception { amNodeManager.nodeHeartbeat(true); finishApp(amNodeManager, finishedApp2); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("queue", "default") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); @@ -2087,15 +2058,15 @@ public void testAppsQueryByQueueFullname() throws Exception { amNodeManager.nodeHeartbeat(true); finishApp(amNodeManager, finishedApp2); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("apps") .queryParam("queue", "root.default") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsCustomResourceTypes.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsCustomResourceTypes.java index 3c6853faf4f1f..aefe8d1380489 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsCustomResourceTypes.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsCustomResourceTypes.java @@ -19,11 +19,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.MockAM; @@ -43,16 +41,18 @@ import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.util.ArrayList; import static org.apache.hadoop.yarn.server.resourcemanager.webapp @@ -65,7 +65,7 @@ * This test verifies that custom resource types are correctly serialized to XML * and JSON when HTTP GET request is sent to the resource: ws/v1/cluster/apps. */ -public class TestRMWebServicesAppsCustomResourceTypes extends JerseyTestBase { +public class TestRMWebServicesAppsCustomResourceTypes extends JerseyTest { private static MockRM rm; private static final int CONTAINER_MB = 1024; @@ -84,7 +84,7 @@ protected void configureServlets() { initResourceTypes(conf); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } private void initResourceTypes(Configuration conf) { @@ -107,11 +107,6 @@ private void createInjectorForWebServletModule() { } public TestRMWebServicesAppsCustomResourceTypes() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test @@ -128,10 +123,9 @@ public void testRunningAppsXml() throws Exception { am1.allocate("*", 2048, 1, new ArrayList<>()); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - WebResource path = r.path("ws").path("v1").path("cluster").path("apps"); - ClientResponse response = - path.accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + WebTarget r = target(); + WebTarget path = r.path("ws").path("v1").path("cluster").path("apps"); + Response response = path.request(MediaType.APPLICATION_XML).get(Response.class); XmlCustomResourceTypeTestCase testCase = new XmlCustomResourceTypeTestCase(path, @@ -164,10 +158,9 @@ public void testRunningAppsJson() throws Exception { am1.allocate("*", 2048, 1, new ArrayList<>()); amNodeManager.nodeHeartbeat(true); - WebResource r = resource(); - WebResource path = r.path("ws").path("v1").path("cluster").path("apps"); - ClientResponse response = - path.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + WebTarget path = r.path("ws").path("v1").path("cluster").path("apps"); + Response response = path.request(MediaType.APPLICATION_JSON).get(Response.class); JsonCustomResourceTypeTestcase testCase = new JsonCustomResourceTypeTestcase(path, diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsModification.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsModification.java index f5f21aac24945..c253bc1fade38 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsModification.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesAppsModification.java @@ -28,7 +28,6 @@ import java.io.File; import java.io.IOException; import java.io.StringReader; -import java.io.StringWriter; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; @@ -43,8 +42,13 @@ import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -97,11 +101,17 @@ import org.apache.hadoop.yarn.util.Times; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -119,20 +129,9 @@ import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.api.client.filter.LoggingFilter; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONMarshaller; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; @RunWith(Parameterized.class) -public class TestRMWebServicesAppsModification extends JerseyTestBase { +public class TestRMWebServicesAppsModification extends JerseyTest { private static MockRM rm; private static final int CONTAINER_MB = 1024; @@ -146,6 +145,25 @@ public class TestRMWebServicesAppsModification extends JerseyTestBase { private static final String FS_ALLOC_FILE = new File(TEST_DIR, "test-fs-queues.xml").getAbsolutePath(); + private static final ObjectWriter APP_STATE_WRITER = + new ObjectMapper().writerFor(AppState.class).withDefaultPrettyPrinter(); + private static final ObjectReader APP_STATE_READER = new ObjectMapper().readerFor(AppState.class); + + private static final ObjectWriter APP_PRIORITY_WRITER = + new ObjectMapper().writerFor(AppPriority.class).withDefaultPrettyPrinter(); + private static final ObjectReader APP_PRIORITY_READER = + new ObjectMapper().readerFor(AppPriority.class); + + private static final ObjectWriter APP_QUEUE_WRITER = + new ObjectMapper().writerFor(AppQueue.class).withDefaultPrettyPrinter(); + private static final ObjectReader APP_QUEUE_READER = + new ObjectMapper().readerFor(AppQueue.class); + + private static final ObjectWriter APP_TIMEOUT_WRITER = + new ObjectMapper().writerFor(AppTimeoutInfo.class).withDefaultPrettyPrinter(); + private static final ObjectReader APP_TIMEOUT_READER = + new ObjectMapper().readerFor(AppTimeoutInfo.class); + /* * Helper class to allow testing of RM web services which require * authorization Add this class as a filter in the Guice injector for the @@ -192,7 +210,7 @@ protected void configureServlets() { if (setAuthFilter) { filter("/*").through(TestRMCustomAuthFilter.class); } - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -281,12 +299,6 @@ public void setUp() throws Exception { } public TestRMWebServicesAppsModification(int run) { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .clientConfig(new DefaultClientConfig(JAXBContextResolver.class)) - .contextPath("jersey-guice-filter").servletPath("/").build()); switch (run) { case 0: default: @@ -312,21 +324,19 @@ private boolean isAuthenticationEnabled() { return setAuthFilter; } - private WebResource constructWebResource(WebResource r, String... paths) { - WebResource rt = r; + private WebTarget constructWebResource(WebTarget target, String... paths) { for (String path : paths) { - rt = rt.path(path); + target = target.path(path); } if (isAuthenticationEnabled()) { - rt = rt.queryParam("user.name", webserviceUserName); + target = target.queryParam("user.name", webserviceUserName); } - return rt; + return target; } - private WebResource constructWebResource(String... paths) { - WebResource r = resource(); - WebResource ws = r.path("ws").path("v1").path("cluster"); - return this.constructWebResource(ws, paths); + private WebTarget constructWebResource(String... paths) { + WebTarget target = target().path("ws").path("v1").path("cluster"); + return this.constructWebResource(target, paths); } @Test @@ -343,11 +353,11 @@ public void testSingleAppState() throws Exception { .build(); RMApp app = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - ClientResponse response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "state").accept(mediaType).get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "state") + .request(mediaType) + .get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); if (mediaType.contains(MediaType.APPLICATION_JSON)) { verifyAppStateJson(response, RMAppState.ACCEPTED); } else if (mediaType.contains(MediaType.APPLICATION_XML)) { @@ -386,18 +396,18 @@ public void testSingleAppKill() throws Exception { } else { entity = targetState; } - ClientResponse response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "state").entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "state") + .request(contentType) + .accept(mediaType) + .put(Entity.json(entity), Response.class); if (!isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); continue; } - assertResponseStatusCode(Status.ACCEPTED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.ACCEPTED, response.getStatusInfo()); if (mediaType.contains(MediaType.APPLICATION_JSON)) { verifyAppStateJson(response, RMAppState.FINAL_SAVING, RMAppState.KILLED, RMAppState.KILLING, RMAppState.ACCEPTED); @@ -406,32 +416,28 @@ public void testSingleAppKill() throws Exception { RMAppState.KILLED, RMAppState.KILLING, RMAppState.ACCEPTED); } - String locationHeaderValue = - response.getHeaders().getFirst(HttpHeaders.LOCATION); - Client c = Client.create(); - WebResource tmp = c.resource(locationHeaderValue); + String locationHeaderValue = (String) response.getHeaders().getFirst(HttpHeaders.LOCATION); + Client c = ClientBuilder.newClient(); + WebTarget tmp = c.target(locationHeaderValue); if (isAuthenticationEnabled()) { tmp = tmp.queryParam("user.name", webserviceUserName); } - response = tmp.get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response = tmp.request().get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); assertTrue(locationHeaderValue.endsWith("/ws/v1/cluster/apps/" + app.getApplicationId().toString() + "/state")); while (true) { Thread.sleep(100); - response = - this - .constructWebResource("apps", - app.getApplicationId().toString(), "state").accept(mediaType) - .entity(entity, contentType).put(ClientResponse.class); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "state") + .request(mediaType) + .accept(contentType) + .put(Entity.json(entity), Response.class); assertTrue( - (response.getStatusInfo().getStatusCode() - == Status.ACCEPTED.getStatusCode()) - || (response.getStatusInfo().getStatusCode() - == Status.OK.getStatusCode())); - if (response.getStatusInfo().getStatusCode() - == Status.OK.getStatusCode()) { + (response.getStatusInfo().getStatusCode() == Response.Status.ACCEPTED.getStatusCode()) + || (response.getStatusInfo().getStatusCode() + == Response.Status.OK.getStatusCode())); + if (response.getStatusInfo().getStatusCode() == Response.Status.OK.getStatusCode()) { assertEquals(RMAppState.KILLED, app.getState()); if (mediaType.equals(MediaType.APPLICATION_JSON)) { verifyAppStateJson(response, RMAppState.KILLED); @@ -471,7 +477,7 @@ public void testSingleAppKillInvalidState() throws Exception { .build(); RMApp app = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - ClientResponse response; + Response response; AppState targetState = new AppState(targetStateString); Object entity; if (contentType.equals(MediaType.APPLICATION_JSON_TYPE)) { @@ -479,19 +485,17 @@ public void testSingleAppKillInvalidState() throws Exception { } else { entity = targetState; } - response = - this - .constructWebResource("apps", - app.getApplicationId().toString(), "state") - .entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "state") + .request(contentType) + .accept(mediaType) + .put(Entity.json(entity), Response.class); if (!isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); continue; } - assertResponseStatusCode(Status.BAD_REQUEST, + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); } } @@ -501,19 +505,15 @@ public void testSingleAppKillInvalidState() throws Exception { } private static String appStateToJSON(AppState state) throws Exception { - StringWriter sw = new StringWriter(); - JSONJAXBContext ctx = new JSONJAXBContext(AppState.class); - JSONMarshaller jm = ctx.createJSONMarshaller(); - jm.marshallToJSON(state, sw); - return sw.toString(); + return APP_STATE_WRITER.writeValueAsString(state); } - protected static void verifyAppStateJson(ClientResponse response, + protected static void verifyAppStateJson(Response response, RMAppState... states) throws JSONException { assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); String responseState = json.getString("state"); boolean valid = false; @@ -526,12 +526,12 @@ protected static void verifyAppStateJson(ClientResponse response, assertTrue(msg, valid); } - protected static void verifyAppStateXML(ClientResponse response, + protected static void verifyAppStateXML(Response response, RMAppState... appStates) throws ParserConfigurationException, IOException, SAXException { assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -583,19 +583,17 @@ public void testSingleAppKillUnauthorized() throws Exception { .build(); RMApp app = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - ClientResponse response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "state").accept(mediaType).get(ClientResponse.class); - AppState info = response.getEntity(AppState.class); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "state") + .request(mediaType) + .get(Response.class); + AppState info = response.readEntity(AppState.class); info.setState(YarnApplicationState.KILLED.toString()); - response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "state").accept(mediaType) - .entity(info, MediaType.APPLICATION_XML).put(ClientResponse.class); - validateResponseStatus(response, Status.FORBIDDEN); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "state") + .request(mediaType) + .put(Entity.json(info), Response.class); + validateResponseStatus(response, Response.Status.FORBIDDEN); } rm.stop(); } @@ -608,20 +606,19 @@ public void testSingleAppKillInvalidId() throws Exception { String[] testAppIds = { "application_1391705042196_0001", "random_string" }; for (int i = 0; i < testAppIds.length; i++) { AppState info = new AppState("KILLED"); - ClientResponse response = - this.constructWebResource("apps", testAppIds[i], "state") - .accept(MediaType.APPLICATION_XML) - .entity(info, MediaType.APPLICATION_XML).put(ClientResponse.class); + Response response = this.constructWebResource("apps", testAppIds[i], "state") + .request(MediaType.APPLICATION_XML) + .put(Entity.json(info), Response.class); if (!isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); continue; } if (i == 0) { - assertResponseStatusCode(Status.NOT_FOUND, + assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo()); } else { - assertResponseStatusCode(Status.BAD_REQUEST, + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); } } @@ -643,13 +640,13 @@ public void tearDown() throws Exception { * off or the param passed if we are running with authorization turned on. * * @param response - * the ClientResponse object to be checked + * the Response object to be checked * @param expectedAuthorizedMode * the expected Status in authorized mode. */ - public void validateResponseStatus(ClientResponse response, - Status expectedAuthorizedMode) { - validateResponseStatus(response, Status.UNAUTHORIZED, + public void validateResponseStatus(Response response, + Response.Status expectedAuthorizedMode) { + validateResponseStatus(response, Response.Status.UNAUTHORIZED, expectedAuthorizedMode); } @@ -660,14 +657,14 @@ public void validateResponseStatus(ClientResponse response, * we are running with authorization turned on. * * @param response - * the ClientResponse object to be checked + * the Response object to be checked * @param expectedUnauthorizedMode * the expected Status in unauthorized mode. * @param expectedAuthorizedMode * the expected Status in authorized mode. */ - public void validateResponseStatus(ClientResponse response, - Status expectedUnauthorizedMode, Status expectedAuthorizedMode) { + public void validateResponseStatus(Response response, + Response.Status expectedUnauthorizedMode, Response.Status expectedAuthorizedMode) { if (!isAuthenticationEnabled()) { assertResponseStatusCode(expectedUnauthorizedMode, response.getStatusInfo()); @@ -680,7 +677,6 @@ public void validateResponseStatus(ClientResponse response, // Simple test - just post to /apps/new-application and validate the response @Test public void testGetNewApplication() throws Exception { - client().addFilter(new LoggingFilter(System.out)); rm.start(); String mediaTypes[] = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }; @@ -692,25 +688,26 @@ public void testGetNewApplication() throws Exception { protected String testGetNewApplication(String mediaType) throws JSONException, ParserConfigurationException, IOException, SAXException { - ClientResponse response = - this.constructWebResource("apps", "new-application").accept(mediaType) - .post(ClientResponse.class); - validateResponseStatus(response, Status.OK); + Response response = + this.constructWebResource("apps", "new-application") + .request(mediaType) + .get(Response.class); + validateResponseStatus(response, Response.Status.OK); if (!isAuthenticationEnabled()) { return ""; } return validateGetNewApplicationResponse(response); } - protected String validateGetNewApplicationResponse(ClientResponse resp) + protected String validateGetNewApplicationResponse(Response resp) throws JSONException, ParserConfigurationException, IOException, SAXException { String ret = ""; - if (resp.getType().toString().contains(MediaType.APPLICATION_JSON)) { - JSONObject json = resp.getEntity(JSONObject.class); + if (resp.getMediaType().toString().contains(MediaType.APPLICATION_JSON)) { + JSONObject json = resp.readEntity(JSONObject.class); ret = validateGetNewApplicationJsonResponse(json); - } else if (resp.getType().toString().contains(MediaType.APPLICATION_XML)) { - String xml = resp.getEntity(String.class); + } else if (resp.getMediaType().toString().contains(MediaType.APPLICATION_XML)) { + String xml = resp.readEntity(String.class); ret = validateGetNewApplicationXMLResponse(xml); } else { // we should not be here @@ -780,7 +777,6 @@ public void testAppSubmit(String acceptMedia, String contentMedia) // create a test app and submit it via rest(after getting an app-id) then // get the app details from the rmcontext and check that everything matches - client().addFilter(new LoggingFilter(System.out)); String lrKey = "example"; String queueName = "testqueue"; @@ -870,24 +866,23 @@ public void testAppSubmit(String acceptMedia, String contentMedia) System.currentTimeMillis(), 1).toString(); appInfo.setReservationId(reservationId); - ClientResponse response = - this.constructWebResource(urlPath).accept(acceptMedia) - .entity(appInfo, contentMedia).post(ClientResponse.class); + Response response = this.constructWebResource(urlPath) + .request(acceptMedia) + .post(Entity.json(appInfo), Response.class); if (!this.isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); return; } - assertResponseStatusCode(Status.ACCEPTED, response.getStatusInfo()); - assertTrue(!response.getHeaders().getFirst(HttpHeaders.LOCATION).isEmpty()); - String locURL = response.getHeaders().getFirst(HttpHeaders.LOCATION); + assertResponseStatusCode(Response.Status.ACCEPTED, response.getStatusInfo()); + String locURL = (String) response.getHeaders().getFirst(HttpHeaders.LOCATION); assertTrue(locURL.contains("/apps/application")); appId = locURL.substring(locURL.indexOf("/apps/") + "/apps/".length()); - WebResource res = resource().uri(new URI(locURL)); - res = res.queryParam("user.name", webserviceUserName); - response = res.get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + WebTarget target = target(locURL); + target = target.queryParam("user.name", webserviceUserName); + response = target.request().get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); RMApp app = rm.getRMContext().getRMApps() @@ -943,10 +938,10 @@ public void testAppSubmit(String acceptMedia, String contentMedia) // Check ReservationId assertEquals(reservationId, app.getReservationId().toString()); - response = - this.constructWebResource("apps", appId).accept(acceptMedia) - .get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response = this.constructWebResource("apps", appId) + .request(acceptMedia) + .get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); } public void testAppSubmitErrors(String acceptMedia, String contentMedia) @@ -957,24 +952,24 @@ public void testAppSubmitErrors(String acceptMedia, String contentMedia) String urlPath = "apps"; ApplicationSubmissionContextInfo appInfo = new ApplicationSubmissionContextInfo(); - ClientResponse response = - this.constructWebResource(urlPath).accept(acceptMedia) - .entity(appInfo, contentMedia).post(ClientResponse.class); - validateResponseStatus(response, Status.BAD_REQUEST); + Response response = this.constructWebResource(urlPath) + .request(acceptMedia) + .post(Entity.json(appInfo), Response.class); + validateResponseStatus(response, Response.Status.BAD_REQUEST); String appId = "random"; appInfo.setApplicationId(appId); - response = - this.constructWebResource(urlPath).accept(acceptMedia) - .entity(appInfo, contentMedia).post(ClientResponse.class); - validateResponseStatus(response, Status.BAD_REQUEST); + response = this.constructWebResource(urlPath) + .request(acceptMedia) + .post(Entity.json(appInfo), Response.class); + validateResponseStatus(response, Response.Status.BAD_REQUEST); appId = "random_junk"; appInfo.setApplicationId(appId); - response = - this.constructWebResource(urlPath).accept(acceptMedia) - .entity(appInfo, contentMedia).post(ClientResponse.class); - validateResponseStatus(response, Status.BAD_REQUEST); + response = this.constructWebResource(urlPath) + .request(acceptMedia) + .post(Entity.json(appInfo), Response.class); + validateResponseStatus(response, Response.Status.BAD_REQUEST); // bad resource info appInfo.getResource().setMemory( @@ -982,21 +977,21 @@ public void testAppSubmitErrors(String acceptMedia, String contentMedia) YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB) + 1); appInfo.getResource().setvCores(1); - response = - this.constructWebResource(urlPath).accept(acceptMedia) - .entity(appInfo, contentMedia).post(ClientResponse.class); + response = this.constructWebResource(urlPath) + .request(acceptMedia) + .post(Entity.json(appInfo), Response.class); - validateResponseStatus(response, Status.BAD_REQUEST); + validateResponseStatus(response, Response.Status.BAD_REQUEST); appInfo.getResource().setvCores( rm.getConfig().getInt( YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES) + 1); appInfo.getResource().setMemory(CONTAINER_MB); - response = - this.constructWebResource(urlPath).accept(acceptMedia) - .entity(appInfo, contentMedia).post(ClientResponse.class); - validateResponseStatus(response, Status.BAD_REQUEST); + response = this.constructWebResource(urlPath) + .request(acceptMedia) + .post(Entity.json(appInfo), Response.class); + validateResponseStatus(response, Response.Status.BAD_REQUEST); } @Test @@ -1031,21 +1026,20 @@ public void testAppSubmitBadJsonAndXML() throws Exception { String body = ""; - ClientResponse response = - this.constructWebResource(urlPath).accept(MediaType.APPLICATION_XML) - .entity(body, MediaType.APPLICATION_XML).post(ClientResponse.class); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + Response response = this.constructWebResource(urlPath) + .request(MediaType.APPLICATION_XML) + .post(Entity.json(body), Response.class); + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); body = "{\"a\" : \"b\"}"; - response = - this.constructWebResource(urlPath).accept(MediaType.APPLICATION_XML) - .entity(body, MediaType.APPLICATION_JSON).post(ClientResponse.class); - validateResponseStatus(response, Status.BAD_REQUEST); + response = this.constructWebResource(urlPath) + .request(MediaType.APPLICATION_XML) + .post(Entity.json(body), Response.class); + validateResponseStatus(response, Response.Status.BAD_REQUEST); rm.stop(); } @Test public void testGetAppQueue() throws Exception { - client().addFilter(new LoggingFilter(System.out)); boolean isCapacityScheduler = rm.getResourceScheduler() instanceof CapacityScheduler; rm.start(); @@ -1060,11 +1054,11 @@ public void testGetAppQueue() throws Exception { .build(); RMApp app = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - ClientResponse response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "queue").accept(contentType).get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "queue") + .request(contentType) + .get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); String expectedQueue = "default"; if(!isCapacityScheduler) { expectedQueue = "root." + webserviceUserName; @@ -1080,7 +1074,6 @@ public void testGetAppQueue() throws Exception { @Test(timeout = 90000) public void testUpdateAppPriority() throws Exception { - client().addFilter(new LoggingFilter(System.out)); if (!(rm.getResourceScheduler() instanceof CapacityScheduler)) { // till the fair scheduler modifications for priority is completed @@ -1127,29 +1120,27 @@ public void testUpdateAppPriority() throws Exception { } else { entity = priority; } - ClientResponse response = this - .constructWebResource("apps", app.getApplicationId().toString(), - "priority") - .entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "priority") + .request(mediaType) + .put(Entity.json(entity), Response.class); if (!isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); continue; } - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); if (mediaType.contains(MediaType.APPLICATION_JSON)) { verifyAppPriorityJson(response, modifiedPriority); } else { verifyAppPriorityXML(response, modifiedPriority); } - response = this - .constructWebResource("apps", app.getApplicationId().toString(), - "priority") - .accept(mediaType).get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "priority") + .request(mediaType) + .get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); if (mediaType.contains(MediaType.APPLICATION_JSON)) { verifyAppPriorityJson(response, modifiedPriority); } else { @@ -1164,12 +1155,10 @@ public void testUpdateAppPriority() throws Exception { .build(); app = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - response = this - .constructWebResource("apps", app.getApplicationId().toString(), - "priority") - .entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "priority") + .request(mediaType) + .put(Entity.json(entity), Response.class); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); } } rm.stop(); @@ -1178,8 +1167,6 @@ public void testUpdateAppPriority() throws Exception { @Test(timeout = 90000) public void testAppMove() throws Exception { - client().addFilter(new LoggingFilter(System.out)); - boolean isCapacityScheduler = rm.getResourceScheduler() instanceof CapacityScheduler; @@ -1217,18 +1204,17 @@ public void testAppMove() throws Exception { } else { entity = targetQueue; } - ClientResponse response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "queue").entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "queue") + .request(mediaType) + .put(Entity.json(entity), Response.class); if (!isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); continue; } - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); String expectedQueue = "test"; if(!isCapacityScheduler) { expectedQueue = "root.test"; @@ -1248,12 +1234,10 @@ public void testAppMove() throws Exception { .build(); app = MockRMAppSubmitter.submit(rm, data); amNodeManager.nodeHeartbeat(true); - response = - this - .constructWebResource("apps", app.getApplicationId().toString(), - "queue").entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); - assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "queue") + .request(mediaType) + .put(Entity.entity(entity, contentType), Response.class); + assertResponseStatusCode(Response.Status.FORBIDDEN, response.getStatusInfo()); if(isCapacityScheduler) { Assert.assertEquals("default", app.getQueue()); } @@ -1268,37 +1252,29 @@ public void testAppMove() throws Exception { protected static String appPriorityToJSON(AppPriority targetPriority) throws Exception { - StringWriter sw = new StringWriter(); - JSONJAXBContext ctx = new JSONJAXBContext(AppPriority.class); - JSONMarshaller jm = ctx.createJSONMarshaller(); - jm.marshallToJSON(targetPriority, sw); - return sw.toString(); + return APP_PRIORITY_WRITER.writeValueAsString(targetPriority); } protected static String appQueueToJSON(AppQueue targetQueue) throws Exception { - StringWriter sw = new StringWriter(); - JSONJAXBContext ctx = new JSONJAXBContext(AppQueue.class); - JSONMarshaller jm = ctx.createJSONMarshaller(); - jm.marshallToJSON(targetQueue, sw); - return sw.toString(); + return APP_QUEUE_WRITER.writeValueAsString(targetQueue); } - protected static void verifyAppPriorityJson(ClientResponse response, - int expectedPriority) throws JSONException { + protected static void verifyAppPriorityJson(Response response, int expectedPriority) + throws JSONException { assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); int responsePriority = json.getInt("priority"); assertEquals(expectedPriority, responsePriority); } - protected static void verifyAppPriorityXML(ClientResponse response, + protected static void verifyAppPriorityXML(Response response, int expectedPriority) throws ParserConfigurationException, IOException, SAXException { assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -1311,24 +1287,21 @@ protected static void verifyAppPriorityXML(ClientResponse response, assertEquals(expectedPriority, responsePriority); } - protected static void - verifyAppQueueJson(ClientResponse response, String queue) - throws JSONException { + protected static void verifyAppQueueJson(Response response, String queue) throws JSONException { assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); String responseQueue = json.getString("queue"); assertEquals(queue, responseQueue); } - protected static void - verifyAppQueueXML(ClientResponse response, String queue) - throws ParserConfigurationException, IOException, SAXException { + protected static void verifyAppQueueXML(Response response, String queue) + throws ParserConfigurationException, IOException, SAXException { assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -1343,7 +1316,6 @@ protected static void verifyAppPriorityXML(ClientResponse response, @Test(timeout = 90000) public void testUpdateAppTimeout() throws Exception { - client().addFilter(new LoggingFilter(System.out)); rm.start(); rm.registerNode("127.0.0.1:1234", 2048); @@ -1361,15 +1333,15 @@ public void testUpdateAppTimeout() throws Exception { .build(); RMApp app = MockRMAppSubmitter.submit(rm, data); - ClientResponse response = - this.constructWebResource("apps", app.getApplicationId().toString(), - "timeouts").accept(mediaType).get(ClientResponse.class); + Response response = + this.constructWebResource("apps", app.getApplicationId().toString(), "timeouts") + .request(mediaType).get(Response.class); if (mediaType.contains(MediaType.APPLICATION_JSON)) { assertEquals( MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); JSONObject js = - response.getEntity(JSONObject.class).getJSONObject("timeouts"); + response.readEntity(JSONObject.class).getJSONObject("timeouts"); JSONArray entity = js.getJSONArray("timeout"); verifyAppTimeoutJson(entity.getJSONObject(0), ApplicationTimeoutType.LIFETIME, "UNLIMITED", -1); @@ -1380,18 +1352,15 @@ public void testUpdateAppTimeout() throws Exception { .formatISO8601(System.currentTimeMillis() + timeOutFromNow * 1000); Object entity = getAppTimeoutInfoEntity(ApplicationTimeoutType.LIFETIME, contentType, expireTime); - response = this - .constructWebResource("apps", app.getApplicationId().toString(), - "timeout") - .entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "timeout") + .request(mediaType).put(Entity.entity(entity, contentType), Response.class); if (!isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); continue; } - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); if (mediaType.contains(MediaType.APPLICATION_JSON)) { verifyAppTimeoutJson(response, ApplicationTimeoutType.LIFETIME, expireTime, timeOutFromNow); @@ -1403,19 +1372,14 @@ public void testUpdateAppTimeout() throws Exception { // verify for negative cases entity = getAppTimeoutInfoEntity(null, contentType, null); - response = this - .constructWebResource("apps", app.getApplicationId().toString(), - "timeout") - .entity(entity, contentType).accept(mediaType) - .put(ClientResponse.class); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "timeout") + .request(mediaType).put(Entity.entity(entity, contentType), Response.class); + assertResponseStatusCode(Response.Status.BAD_REQUEST, response.getStatusInfo()); // invoke get - response = - this.constructWebResource("apps", app.getApplicationId().toString(), - "timeouts", ApplicationTimeoutType.LIFETIME.toString()) - .accept(mediaType).get(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response = this.constructWebResource("apps", app.getApplicationId().toString(), "timeouts", + ApplicationTimeoutType.LIFETIME.toString()).request(mediaType).get(Response.class); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); if (mediaType.contains(MediaType.APPLICATION_JSON)) { verifyAppTimeoutJson(response, ApplicationTimeoutType.LIFETIME, expireTime, timeOutFromNow); @@ -1440,12 +1404,12 @@ private Object getAppTimeoutInfoEntity(ApplicationTimeoutType type, return entity; } - protected static void verifyAppTimeoutJson(ClientResponse response, + protected static void verifyAppTimeoutJson(Response response, ApplicationTimeoutType type, String expireTime, long timeOutFromNow) throws JSONException { assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject jsonTimeout = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject jsonTimeout = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, jsonTimeout.length()); JSONObject json = jsonTimeout.getJSONObject("timeout"); verifyAppTimeoutJson(json, type, expireTime, timeOutFromNow); @@ -1460,12 +1424,12 @@ protected static void verifyAppTimeoutJson(JSONObject json, assertTrue(json.getLong("remainingTimeInSeconds") <= timeOutFromNow); } - protected static void verifyAppTimeoutXML(ClientResponse response, + protected static void verifyAppTimeoutXML(Response response, ApplicationTimeoutType type, String expireTime, long timeOutFromNow) throws ParserConfigurationException, IOException, SAXException { assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -1484,14 +1448,7 @@ protected static void verifyAppTimeoutXML(ClientResponse response, protected static String appTimeoutToJSON(AppTimeoutInfo timeout) throws Exception { - StringWriter sw = new StringWriter(); - JSONJAXBContext ctx = new JSONJAXBContext( - JSONConfiguration.natural().rootUnwrapping(false).build(), - AppTimeoutInfo.class); - JSONMarshaller jm = ctx.createJSONMarshaller(); - jm.marshallToJSON(timeout, sw); - jm.marshallToJSON(timeout, System.out); - return sw.toString(); + return APP_TIMEOUT_WRITER.writeValueAsString(timeout); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySched.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySched.java index b9ce10aaeddcb..dee4f987a3e78 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySched.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySched.java @@ -19,10 +19,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import java.io.BufferedReader; import java.io.FileWriter; @@ -36,6 +35,7 @@ import java.util.stream.Collectors; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; @@ -59,9 +59,9 @@ import org.apache.hadoop.yarn.util.resource.Resources; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -75,7 +75,7 @@ import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.PREFIX; import static org.junit.Assert.assertEquals; -public class TestRMWebServicesCapacitySched extends JerseyTestBase { +public class TestRMWebServicesCapacitySched extends JerseyTest { private MockRM rm; @@ -92,12 +92,11 @@ protected void configureServlets() { bind(RMWebServices.class); bind(GenericExceptionHandler.class); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } public TestRMWebServicesCapacitySched() { - super(createWebAppDescriptor()); } @Before @@ -167,32 +166,31 @@ public static void setupQueueConfiguration( @Test public void testClusterScheduler() throws Exception { - ClientResponse response = resource().path("ws").path("v1").path("cluster") - .path("scheduler").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster").path("scheduler") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertJsonResponse(response, "webapp/scheduler-response.json"); } @Test public void testClusterSchedulerSlash() throws Exception { - ClientResponse response = resource().path("ws").path("v1").path("cluster") - .path("scheduler/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster") + .path("scheduler/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertJsonResponse(response, "webapp/scheduler-response.json"); } @Test public void testClusterSchedulerDefault() throws Exception { - ClientResponse response = resource().path("ws").path("v1").path("cluster") - .path("scheduler").get(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster") + .path("scheduler").request().get(Response.class); assertJsonResponse(response, "webapp/scheduler-response.json"); } @Test public void testClusterSchedulerXML() throws Exception { - ClientResponse response = resource().path("ws").path("v1").path("cluster") - .path("scheduler/").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster") + .path("scheduler/").request(MediaType.APPLICATION_XML) + .get(Response.class); assertXmlResponse(response, "webapp/scheduler-response.xml"); } @@ -221,8 +219,8 @@ public void testPerUserResourcesXML() throws Exception { MockRMAppSubmitter.submit(rm, data); //Get the XML from ws/v1/cluster/scheduler - ClientResponse response = resource().path("ws/v1/cluster/scheduler") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + Response response = target().path("ws/v1/cluster/scheduler") + .request(MediaType.APPLICATION_XML).get(Response.class); assertXmlResponse(response, "webapp/scheduler-response-PerUserResources.xml"); } finally { rm.stop(); @@ -242,8 +240,8 @@ public void testNodeLabelDefaultAPI() throws Exception { rm.start(); try { //Get the XML from ws/v1/cluster/scheduler - ClientResponse response = resource().path("ws/v1/cluster/scheduler") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + Response response = target().path("ws/v1/cluster/scheduler") + .request(MediaType.APPLICATION_XML).get(Response.class); assertXmlResponse(response, "webapp/scheduler-response-NodeLabelDefaultAPI.xml"); } finally { rm.stop(); @@ -275,9 +273,9 @@ public void testPerUserResourcesJSON() throws Exception { MockRMAppSubmitter.submit(rm, data); //Get JSON - ClientResponse response = resource().path("ws").path("v1").path("cluster") - .path("scheduler/").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster") + .path("scheduler/").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertJsonResponse(response, "webapp/scheduler-response-PerUserResources.json"); } finally { rm.stop(); @@ -293,16 +291,16 @@ public void testResourceInfo() { assertEquals("", res.toString()); } - public static void assertXmlType(ClientResponse response) { + public static void assertXmlType(Response response) { assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); } - public static void assertXmlResponse(ClientResponse response, + public static void assertXmlResponse(Response response, String expectedResourceFilename) throws Exception { assertXmlType(response); - Document document = loadDocument(response.getEntity(String.class)); + Document document = loadDocument(response.readEntity(String.class)); String actual = serializeDocument(document).trim(); updateTestDataAutomatically(expectedResourceFilename, actual); assertEquals(getResourceAsString(expectedResourceFilename), actual); @@ -327,19 +325,19 @@ public static Document loadDocument(String xml) throws Exception { return builder.parse(is); } - public static void assertJsonResponse(ClientResponse response, + public static void assertJsonResponse(Response response, String expectedResourceFilename) throws JSONException, IOException { assertJsonType(response); - JSONObject json = response.getEntity(JSONObject.class); + JSONObject json = response.readEntity(JSONObject.class); String actual = json.toString(2); updateTestDataAutomatically(expectedResourceFilename, actual); assertEquals(getResourceAsString(expectedResourceFilename), actual); } - public static void assertJsonType(ClientResponse response) { + public static void assertJsonType(Response response) { assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); } public static InputStream getResourceAsStream(String configFilename) { @@ -385,14 +383,6 @@ public static void updateTestDataAutomatically(String configFilename, String act } } - public static WebAppDescriptor createWebAppDescriptor() { - return new WebAppDescriptor.Builder( - TestRMWebServicesCapacitySched.class.getPackage().getName()) - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build(); - } - public static MockRM createMockRM(CapacitySchedulerConfiguration csConf) { setupQueueConfiguration(csConf); YarnConfiguration conf = new YarnConfiguration(csConf); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySchedDynamicConfig.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySchedDynamicConfig.java index f2336e79d2ace..0ed3ecdc9042b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySchedDynamicConfig.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesCapacitySchedDynamicConfig.java @@ -19,13 +19,13 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; -import com.sun.jersey.api.client.ClientResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.yarn.conf.YarnConfiguration; @@ -37,22 +37,20 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerQueueManager; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePath; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; + +import org.glassfish.jersey.test.JerseyTest; import org.junit.Test; import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerTestUtilities.GB; import static org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesCapacitySched.assertJsonResponse; import static org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesCapacitySched.createMockRM; -import static org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesCapacitySched.createWebAppDescriptor; -public class TestRMWebServicesCapacitySchedDynamicConfig extends - JerseyTestBase { +public class TestRMWebServicesCapacitySchedDynamicConfig extends JerseyTest { private MockRM rm; private CapacitySchedulerQueueManager autoQueueHandler; public TestRMWebServicesCapacitySchedDynamicConfig() { - super(createWebAppDescriptor()); } @Test @@ -180,10 +178,10 @@ private void createQueue(String queuePath) throws YarnException, autoQueueHandler.createQueue(new QueuePath(queuePath)); } - private ClientResponse sendRequest() { - return resource().path("ws").path("v1").path("cluster") - .path("scheduler").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + private Response sendRequest() { + return target().path("ws").path("v1").path("cluster") + .path("scheduler").request(MediaType.APPLICATION_JSON) + .get(Response.class); } private static class CSConfigGenerator { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesConfigurationMutation.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesConfigurationMutation.java index 43b5ee7127c2e..43f1119c37613 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesConfigurationMutation.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesConfigurationMutation.java @@ -19,12 +19,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.security.UserGroupInformation; @@ -41,7 +38,6 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodeLabelsInfo; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.dao.QueueConfigInfo; import org.apache.hadoop.yarn.webapp.dao.SchedConfUpdateInfo; import org.apache.hadoop.yarn.webapp.util.YarnWebServiceUtils; @@ -49,13 +45,19 @@ import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import java.io.File; @@ -77,7 +79,7 @@ /** * Test scheduler configuration mutation via REST API. */ -public class TestRMWebServicesConfigurationMutation extends JerseyTestBase { +public class TestRMWebServicesConfigurationMutation extends JerseyTest { private static final Logger LOG = LoggerFactory .getLogger(TestRMWebServicesConfigurationMutation.class); @@ -130,7 +132,7 @@ protected void configureServlets() { } rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); filter("/*").through(TestRMWebServicesAppsModification .TestRMCustomAuthFilter.class); } @@ -176,23 +178,16 @@ private static void setupQueueConfiguration( } public TestRMWebServicesConfigurationMutation() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } private CapacitySchedulerConfiguration getSchedulerConf() throws JSONException { - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("cluster") - .queryParam("user.name", userName).path("scheduler-conf") - .accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + WebTarget target = target(); + Response response = + target.path("ws").path("v1").path("cluster").queryParam("user.name", userName) + .path("scheduler-conf").request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - JSONObject json = response.getEntity(JSONObject.class); + JSONObject json = response.readEntity(JSONObject.class); JSONArray items = (JSONArray) json.get("property"); CapacitySchedulerConfiguration parsedConf = new CapacitySchedulerConfiguration(); @@ -232,36 +227,33 @@ public void testFormatSchedulerConf() throws Exception { updateInfo.getUpdateQueueInfo().add(stoppedInfo); // Add a queue root.formattest to the existing three queues - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + WebTarget target = target(); + Response response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); newConf = getSchedulerConf(); assertNotNull(newConf); assertEquals(5, newConf.getQueues("root").length); // Format the scheduler config and validate root.formattest is not present - response = r.path("ws").path("v1").path("cluster") - .queryParam("user.name", userName) - .path(RMWSConsts.FORMAT_SCHEDULER_CONF) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").queryParam("user.name", userName) + .path(RMWSConsts.FORMAT_SCHEDULER_CONF).request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); newConf = getSchedulerConf(); assertEquals(4, newConf.getQueues("root").length); } private long getConfigVersion() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .queryParam("user.name", userName) - .path(RMWSConsts.SCHEDULER_CONF_VERSION) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget target = target(); + Response response = + target.path("ws").path("v1").path("cluster").queryParam("user.name", userName) + .path(RMWSConsts.SCHEDULER_CONF_VERSION).request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - JSONObject json = response.getEntity(JSONObject.class); + JSONObject json = response.readEntity(JSONObject.class); return Long.parseLong(json.get("versionID").toString()); } @@ -278,9 +270,9 @@ public void testAddNestedQueue() throws Exception { assertNotNull(orgConf); assertEquals(4, orgConf.getQueues("root").length); - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Add parent queue root.d with two children d1 and d2. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -300,13 +292,10 @@ public void testAddNestedQueue() throws Exception { updateInfo.getAddQueueInfo().add(d1); updateInfo.getAddQueueInfo().add(d2); updateInfo.getAddQueueInfo().add(d); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -325,9 +314,9 @@ public void testAddNestedQueue() throws Exception { @Test public void testAddWithUpdate() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Add root.d with capacity 25, reducing root.b capacity from 75 to 50. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -339,13 +328,10 @@ public void testAddWithUpdate() throws Exception { QueueConfigInfo b = new QueueConfigInfo("root.b", bCapacity); updateInfo.getAddQueueInfo().add(d); updateInfo.getUpdateQueueInfo().add(b); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -357,8 +343,8 @@ public void testAddWithUpdate() throws Exception { @Test public void testUnsetParentQueueOrderingPolicy() throws Exception { - WebResource r = resource(); - ClientResponse response; + WebTarget target = target(); + Response response; // Update ordering policy of Leaf Queue root.b to fair SchedConfUpdateInfo updateInfo1 = new SchedConfUpdateInfo(); @@ -367,12 +353,10 @@ public void testUnsetParentQueueOrderingPolicy() throws Exception { "fair"); QueueConfigInfo aUpdateInfo = new QueueConfigInfo("root.b", updateParam); updateInfo1.getUpdateQueueInfo().add(aUpdateInfo); - response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo1, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo1, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = ((CapacityScheduler) rm.getResourceScheduler()).getConfiguration(); @@ -388,12 +372,10 @@ public void testUnsetParentQueueOrderingPolicy() throws Exception { capacity.put(CapacitySchedulerConfiguration.CAPACITY, "100"); QueueConfigInfo b1 = new QueueConfigInfo("root.b.b1", capacity); updateInfo2.getAddQueueInfo().add(b1); - response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo2, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo2, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); // Validate unset ordering policy of root.b after converted to // Parent Queue @@ -408,8 +390,8 @@ public void testUnsetParentQueueOrderingPolicy() throws Exception { @Test public void testUnsetLeafQueueOrderingPolicy() throws Exception { - WebResource r = resource(); - ClientResponse response; + WebTarget target = target(); + Response response; // Update ordering policy of Parent Queue root.c to priority-utilization SchedConfUpdateInfo updateInfo1 = new SchedConfUpdateInfo(); @@ -418,12 +400,10 @@ public void testUnsetLeafQueueOrderingPolicy() throws Exception { "priority-utilization"); QueueConfigInfo aUpdateInfo = new QueueConfigInfo("root.c", updateParam); updateInfo1.getUpdateQueueInfo().add(aUpdateInfo); - response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo1, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo1, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = ((CapacityScheduler) rm.getResourceScheduler()).getConfiguration(); @@ -436,12 +416,10 @@ public void testUnsetLeafQueueOrderingPolicy() throws Exception { // Remove root.c.c1 which makes root.c a Leaf Queue SchedConfUpdateInfo updateInfo2 = new SchedConfUpdateInfo(); updateInfo2.getRemoveQueueInfo().add("root.c.c1"); - response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo2, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo2, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); // Validate unset ordering policy of root.c after converted to @@ -456,21 +434,18 @@ public void testUnsetLeafQueueOrderingPolicy() throws Exception { @Test public void testRemoveQueue() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; stopQueue("root.a.a2"); // Remove root.a.a2 SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); updateInfo.getRemoveQueueInfo().add("root.a.a2"); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -483,9 +458,9 @@ public void testRemoveQueue() throws Exception { @Test public void testStopWithRemoveQueue() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Set state of queues to STOPPED. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -497,12 +472,10 @@ public void testStopWithRemoveQueue() throws Exception { updateInfo.getUpdateQueueInfo().add(stoppedInfo); updateInfo.getRemoveQueueInfo().add("root.a.a2"); - response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -513,9 +486,9 @@ public void testStopWithRemoveQueue() throws Exception { @Test public void testRemoveQueueWhichHasQueueMapping() throws Exception { - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response; + Response response; CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); // Validate Queue 'mappedqueue' exists before deletion @@ -532,10 +505,10 @@ public void testRemoveQueueWhichHasQueueMapping() throws Exception { // Remove queue 'mappedqueue' using update scheduler-conf updateInfo.getRemoveQueueInfo().add("root.mappedqueue"); response = r.path("ws").path("v1").path("cluster").path("scheduler-conf") - .queryParam("user.name", userName).accept(MediaType.APPLICATION_JSON) - .entity(YarnWebServiceUtils.toJson(updateInfo, SchedConfUpdateInfo.class), - MediaType.APPLICATION_JSON).put(ClientResponse.class); - String responseText = response.getEntity(String.class); + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(YarnWebServiceUtils.toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); + String responseText = response.readEntity(String.class); // Queue 'mappedqueue' deletion will fail as there is queue mapping present assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); @@ -553,8 +526,8 @@ public void testRemoveQueueWhichHasQueueMapping() throws Exception { @Test public void testStopWithConvertLeafToParentQueue() throws Exception { - WebResource r = resource(); - ClientResponse response; + WebTarget target = target(); + Response response; // Set state of queues to STOPPED. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -570,12 +543,10 @@ public void testStopWithConvertLeafToParentQueue() throws Exception { QueueConfigInfo b1 = new QueueConfigInfo("root.b.b1", b1Capacity); updateInfo.getAddQueueInfo().add(b1); - response = r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -586,21 +557,18 @@ public void testStopWithConvertLeafToParentQueue() throws Exception { @Test public void testRemoveParentQueue() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; stopQueue("root.c", "root.c.c1"); // Remove root.c (parent queue) SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); updateInfo.getRemoveQueueInfo().add("root.c"); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -611,9 +579,9 @@ public void testRemoveParentQueue() throws Exception { @Test public void testRemoveParentQueueWithCapacity() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; stopQueue("root.a", "root.a.a1", "root.a.a2"); // Remove root.a (parent queue) with capacity 25 @@ -625,13 +593,10 @@ public void testRemoveParentQueueWithCapacity() throws Exception { bCapacity.put(CapacitySchedulerConfiguration.CAPACITY, "100"); QueueConfigInfo b = new QueueConfigInfo("root.b", bCapacity); updateInfo.getUpdateQueueInfo().add(b); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -643,9 +608,9 @@ public void testRemoveParentQueueWithCapacity() throws Exception { @Test public void testRemoveMultipleQueues() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; stopQueue("root.b", "root.c", "root.c.c1"); // Remove root.b and root.c @@ -657,13 +622,10 @@ public void testRemoveMultipleQueues() throws Exception { aCapacity.put(CapacitySchedulerConfiguration.MAXIMUM_CAPACITY, "100"); QueueConfigInfo configInfo = new QueueConfigInfo("root.a", aCapacity); updateInfo.getUpdateQueueInfo().add(configInfo); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = @@ -672,9 +634,9 @@ public void testRemoveMultipleQueues() throws Exception { } private void stopQueue(String... queuePaths) throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Set state of queues to STOPPED. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -685,13 +647,10 @@ private void stopQueue(String... queuePaths) throws Exception { QueueConfigInfo stoppedInfo = new QueueConfigInfo(queue, stoppedParam); updateInfo.getUpdateQueueInfo().add(stoppedInfo); } - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = ((CapacityScheduler) rm.getResourceScheduler()).getConfiguration(); @@ -702,9 +661,9 @@ private void stopQueue(String... queuePaths) throws Exception { @Test public void testUpdateQueue() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Update config value. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -720,13 +679,10 @@ public void testUpdateQueue() throws Exception { cs.getConfiguration() .getMaximumApplicationMasterResourcePerQueuePercent("root.a"), 0.001f); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); LOG.debug("Response headers: " + response.getHeaders()); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = cs.getConfiguration(); @@ -739,13 +695,10 @@ public void testUpdateQueue() throws Exception { aUpdateInfo = new QueueConfigInfo("root.a", updateParam); updateInfo.getUpdateQueueInfo().clear(); updateInfo.getUpdateQueueInfo().add(aUpdateInfo); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); newCSConf = cs.getConfiguration(); assertEquals(CapacitySchedulerConfiguration @@ -756,9 +709,9 @@ public void testUpdateQueue() throws Exception { @Test public void testUpdateQueueCapacity() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Update root.a and root.b capacity to 50. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); @@ -769,13 +722,10 @@ public void testUpdateQueueCapacity() throws Exception { updateInfo.getUpdateQueueInfo().add(aUpdateInfo); updateInfo.getUpdateQueueInfo().add(bUpdateInfo); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = ((CapacityScheduler) rm.getResourceScheduler()).getConfiguration(); @@ -785,22 +735,19 @@ public void testUpdateQueueCapacity() throws Exception { @Test public void testGlobalConfChange() throws Exception { - WebResource r = resource(); + WebTarget target = target(); - ClientResponse response; + Response response; // Set maximum-applications to 30000. SchedConfUpdateInfo updateInfo = new SchedConfUpdateInfo(); updateInfo.getGlobalParams().put(CapacitySchedulerConfiguration.PREFIX + "maximum-applications", "30000"); - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacitySchedulerConfiguration newCSConf = ((CapacityScheduler) rm.getResourceScheduler()).getConfiguration(); @@ -809,13 +756,10 @@ public void testGlobalConfChange() throws Exception { updateInfo.getGlobalParams().put(CapacitySchedulerConfiguration.PREFIX + "maximum-applications", null); // Unset maximum-applications. Should be set to default. - response = - r.path("ws").path("v1").path("cluster") - .path("scheduler-conf").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("scheduler-conf") + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).put( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); newCSConf = ((CapacityScheduler) rm.getResourceScheduler()).getConfiguration(); @@ -826,35 +770,33 @@ public void testGlobalConfChange() throws Exception { @Test public void testNodeLabelRemovalResidualConfigsAreCleared() throws Exception { - WebResource r = resource(); - ClientResponse response; + WebTarget target = target(); + Response response; // 1. Create Node Label: label1 NodeLabelsInfo nodeLabelsInfo = new NodeLabelsInfo(); nodeLabelsInfo.getNodeLabelsInfo().add(new NodeLabelInfo(LABEL_1)); - WebResource addNodeLabelsResource = r.path("ws").path("v1").path("cluster") - .path("add-node-labels"); - WebResource getNodeLabelsResource = r.path("ws").path("v1").path("cluster") - .path("get-node-labels"); - WebResource removeNodeLabelsResource = r.path("ws").path("v1").path("cluster") - .path("remove-node-labels"); - WebResource schedulerConfResource = r.path("ws").path("v1").path("cluster") - .path(RMWSConsts.SCHEDULER_CONF); + WebTarget addNodeLabelsResource = + target.path("ws").path("v1").path("cluster").path("add-node-labels"); + WebTarget getNodeLabelsResource = + target.path("ws").path("v1").path("cluster").path("get-node-labels"); + WebTarget removeNodeLabelsResource = + target.path("ws").path("v1").path("cluster").path("remove-node-labels"); + WebTarget schedulerConfResource = + target.path("ws").path("v1").path("cluster").path(RMWSConsts.SCHEDULER_CONF); response = - addNodeLabelsResource.queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(logAndReturnJson(addNodeLabelsResource, - toJson(nodeLabelsInfo, NodeLabelsInfo.class)), - MediaType.APPLICATION_JSON) - .post(ClientResponse.class); + addNodeLabelsResource.queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .post(Entity.entity(logAndReturnJson(addNodeLabelsResource, + toJson(nodeLabelsInfo, NodeLabelsInfo.class)), MediaType.APPLICATION_JSON), + Response.class); // 2. Verify new Node Label response = - getNodeLabelsResource.queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + getNodeLabelsResource.queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - nodeLabelsInfo = response.getEntity(NodeLabelsInfo.class); + response.getMediaType().toString()); + nodeLabelsInfo = response.readEntity(NodeLabelsInfo.class); assertEquals(1, nodeLabelsInfo.getNodeLabels().size()); for (NodeLabelInfo nl : nodeLabelsInfo.getNodeLabelsInfo()) { assertEquals(LABEL_1, nl.getName()); @@ -875,12 +817,10 @@ public void testNodeLabelRemovalResidualConfigsAreCleared() throws Exception { updateInfo.getUpdateQueueInfo().add(rootAUpdateInfo); response = - schedulerConfResource - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(logAndReturnJson(schedulerConfResource, toJson(updateInfo, - SchedConfUpdateInfo.class)), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + schedulerConfResource.queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .put(Entity.entity(logAndReturnJson(schedulerConfResource, + toJson(updateInfo, SchedConfUpdateInfo.class)), MediaType.APPLICATION_JSON), + Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); @@ -924,12 +864,10 @@ public void testNodeLabelRemovalResidualConfigsAreCleared() throws Exception { updateInfo.getUpdateQueueInfo().add(rootA_A2UpdateInfo); response = - schedulerConfResource - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(logAndReturnJson(schedulerConfResource, toJson(updateInfo, - SchedConfUpdateInfo.class)), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + schedulerConfResource.queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .put(Entity.entity(logAndReturnJson(schedulerConfResource, + toJson(updateInfo, SchedConfUpdateInfo.class)), MediaType.APPLICATION_JSON), + Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertEquals(100.0, cs.getConfiguration().getLabeledQueueCapacity(ROOT, LABEL_1), 0.001f); @@ -977,34 +915,27 @@ public void testNodeLabelRemovalResidualConfigsAreCleared() throws Exception { updateInfo.getUpdateQueueInfo().add(rootA_A2UpdateInfo); response = - schedulerConfResource - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(logAndReturnJson(schedulerConfResource, toJson(updateInfo, - SchedConfUpdateInfo.class)), MediaType.APPLICATION_JSON) - .put(ClientResponse.class); + schedulerConfResource.queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .put(Entity.entity(logAndReturnJson(schedulerConfResource, + toJson(updateInfo, SchedConfUpdateInfo.class)), MediaType.APPLICATION_JSON), + Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertEquals(Sets.newHashSet("*"), cs.getConfiguration().getAccessibleNodeLabels(ROOT.getFullPath())); assertNull(cs.getConfiguration().getAccessibleNodeLabels(ROOT_A.getFullPath())); //6. Remove node label 'label1' - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("labels", LABEL_1); response = - removeNodeLabelsResource - .queryParam("user.name", userName) - .queryParams(params) - .accept(MediaType.APPLICATION_JSON) - .post(ClientResponse.class); + removeNodeLabelsResource.queryParam("user.name", userName).queryParam("labels", LABEL_1) + .request(MediaType.APPLICATION_JSON).post(null, Response.class); // Verify response = - getNodeLabelsResource.queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + getNodeLabelsResource.queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - nodeLabelsInfo = response.getEntity(NodeLabelsInfo.class); + response.getMediaType().toString()); + nodeLabelsInfo = response.readEntity(NodeLabelsInfo.class); assertEquals(0, nodeLabelsInfo.getNodeLabels().size()); //6. Check residual configs @@ -1025,7 +956,7 @@ private String getConfValueForQueueAndLabelAndType(CapacityScheduler cs, queuePath.getFullPath(), label) + type); } - private Object logAndReturnJson(WebResource ws, String json) { + private Object logAndReturnJson(WebTarget ws, String json) { LOG.info("Sending to web resource: {}, json: {}", ws, json); return json; } @@ -1040,7 +971,7 @@ private String getAccessibleNodeLabelsMaxCapacityPropertyName(String label) { @Test public void testValidateWithClusterMaxAllocation() throws Exception { - WebResource r = resource(); + WebTarget target = target(); int clusterMax = YarnConfiguration. DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB * 2; conf.setInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, @@ -1053,14 +984,11 @@ public void testValidateWithClusterMaxAllocation() throws Exception { QueueConfigInfo aUpdateInfo = new QueueConfigInfo("root.a", updateParam); updateInfo.getUpdateQueueInfo().add(aUpdateInfo); - ClientResponse response = - r.path("ws").path("v1").path("cluster") - .path(RMWSConsts.SCHEDULER_CONF_VALIDATE) - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(updateInfo, - SchedConfUpdateInfo.class), MediaType.APPLICATION_JSON) - .post(ClientResponse.class); + Response response = + target.path("ws").path("v1").path("cluster").path(RMWSConsts.SCHEDULER_CONF_VALIDATE) + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON).post( + Entity.entity(toJson(updateInfo, SchedConfUpdateInfo.class), + MediaType.APPLICATION_JSON), Response.class); assertEquals(Status.OK.getStatusCode(), response.getStatus()); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesContainers.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesContainers.java index e94180073e5ca..c216c9160bff2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesContainers.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesContainers.java @@ -22,7 +22,9 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; @@ -40,22 +42,20 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; -import org.eclipse.jetty.server.Response; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; /** * Testing containers REST API. */ -public class TestRMWebServicesContainers extends JerseyTestBase { +public class TestRMWebServicesContainers extends JerseyTest { private static MockRM rm; private static String userName; @@ -69,40 +69,30 @@ protected void configureServlets() { try { userName = UserGroupInformation.getCurrentUser().getShortUserName(); } catch (IOException ioe) { - throw new RuntimeException("Unable to get current user name " - + ioe.getMessage(), ioe); + throw new RuntimeException("Unable to get current user name " + ioe.getMessage(), ioe); } Configuration conf = new Configuration(); - conf.setClass(YarnConfiguration.RM_SCHEDULER, FifoScheduler.class, - ResourceScheduler.class); + conf.setClass(YarnConfiguration.RM_SCHEDULER, FifoScheduler.class, ResourceScheduler.class); conf.set(YarnConfiguration.YARN_ADMIN_ACL, "admin"); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); - filter("/*").through(TestRMWebServicesAppsModification - .TestRMCustomAuthFilter.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); + filter("/*").through(TestRMWebServicesAppsModification.TestRMCustomAuthFilter.class); } } static { - GuiceServletConfig.setInjector( - Guice.createInjector(new WebServletModule())); + GuiceServletConfig.setInjector(Guice.createInjector(new WebServletModule())); } @Before @Override public void setUp() throws Exception { super.setUp(); - GuiceServletConfig.setInjector( - Guice.createInjector(new WebServletModule())); + GuiceServletConfig.setInjector(Guice.createInjector(new WebServletModule())); } public TestRMWebServicesContainers() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test @@ -112,61 +102,48 @@ public void testSignalContainer() throws Exception { RMApp app = MockRMAppSubmitter.submit(rm, MockRMAppSubmissionData.Builder.createWithMemory(1024, rm).build()); nm.nodeHeartbeat(true); - MockRM - .waitForState(app.getCurrentAppAttempt(), RMAppAttemptState.ALLOCATED); + MockRM.waitForState(app.getCurrentAppAttempt(), RMAppAttemptState.ALLOCATED); rm.sendAMLaunched(app.getCurrentAppAttempt().getAppAttemptId()); - WebResource r = resource(); + WebTarget target = target(); // test error command - ClientResponse response = - r.path("ws").path("v1").path("cluster").path("containers").path( - app.getCurrentAppAttempt().getMasterContainer().getId().toString()) - .path("signal") - .path("not-exist-signal") - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); + Response response = target.path("ws").path("v1").path("cluster").path("containers") + .path(app.getCurrentAppAttempt().getMasterContainer().getId().toString()).path("signal") + .path("not-exist-signal").queryParam("user.name", userName) + .request(MediaType.APPLICATION_JSON).post(null, Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(Response.SC_BAD_REQUEST, response.getStatus()); - assertTrue(response.getEntity(String.class) - .contains("Invalid command: NOT-EXIST-SIGNAL")); + response.getMediaType().toString()); + assertEquals(Response.Status.BAD_REQUEST, response.getStatusInfo()); + assertTrue(response.readEntity(String.class).contains("Invalid command: NOT-EXIST-SIGNAL")); // test error containerId response = - r.path("ws").path("v1").path("cluster").path("containers").path("XXX") - .path("signal") + target.path("ws").path("v1").path("cluster").path("containers").path("XXX").path("signal") .path(SignalContainerCommand.OUTPUT_THREAD_DUMP.name()) - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); + .queryParam("user.name", userName).request(MediaType.APPLICATION_JSON) + .post(null, Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(Response.SC_INTERNAL_SERVER_ERROR, response.getStatus()); - assertTrue( - response.getEntity(String.class).contains("Invalid ContainerId")); + response.getMediaType().toString()); + assertEquals(Response.Status.INTERNAL_SERVER_ERROR, response.getStatusInfo()); + assertTrue(response.readEntity(String.class).contains("Invalid ContainerId")); // test correct signal by owner - response = - r.path("ws").path("v1").path("cluster").path("containers").path( - app.getCurrentAppAttempt().getMasterContainer().getId().toString()) - .path("signal") - .path(SignalContainerCommand.OUTPUT_THREAD_DUMP.name()) - .queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("containers") + .path(app.getCurrentAppAttempt().getMasterContainer().getId().toString()).path("signal") + .path(SignalContainerCommand.OUTPUT_THREAD_DUMP.name()).queryParam("user.name", userName) + .request(MediaType.APPLICATION_JSON).post(null, Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(Response.SC_OK, response.getStatus()); + response.getMediaType().toString()); + assertEquals(Response.Status.OK, response.getStatusInfo()); // test correct signal by admin - response = - r.path("ws").path("v1").path("cluster").path("containers").path( - app.getCurrentAppAttempt().getMasterContainer().getId().toString()) - .path("signal") - .path(SignalContainerCommand.OUTPUT_THREAD_DUMP.name()) - .queryParam("user.name", "admin") - .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); + response = target.path("ws").path("v1").path("cluster").path("containers") + .path(app.getCurrentAppAttempt().getMasterContainer().getId().toString()).path("signal") + .path(SignalContainerCommand.OUTPUT_THREAD_DUMP.name()).queryParam("user.name", "admin") + .request(MediaType.APPLICATION_JSON).post(null, Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertEquals(Response.SC_OK, response.getStatus()); + response.getMediaType().toString()); + assertEquals(Response.Status.OK, response.getStatusInfo()); rm.stop(); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokenAuthentication.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokenAuthentication.java index ea286568340c2..7308ee0c07dea 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokenAuthentication.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokenAuthentication.java @@ -36,6 +36,7 @@ import java.util.concurrent.Callable; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; @@ -66,7 +67,6 @@ import org.junit.BeforeClass; import org.junit.Test; -import com.sun.jersey.api.client.ClientResponse.Status; import static org.apache.hadoop.yarn.conf.YarnConfiguration.RM_PROXY_USER_PREFIX; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -235,7 +235,7 @@ public void testDelegationTokenAuth() throws Exception { conn.getInputStream(); fail("we should not be here"); } catch (IOException e) { - assertEquals(Status.UNAUTHORIZED.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), conn.getResponseCode()); } conn = (HttpURLConnection) url.openConnection(); @@ -293,7 +293,7 @@ public void testCancelledDelegationToken() throws Exception { conn.getInputStream(); fail("Authentication should fail with expired delegation tokens"); } catch (IOException e) { - assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); } } @@ -317,7 +317,7 @@ public void testDelegationTokenOps() throws Exception { fail("Creation/Renewing delegation tokens should not be " + "allowed with token auth"); } catch (IOException e) { - assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); } } @@ -331,7 +331,7 @@ public void testDelegationTokenOps() throws Exception { conn.getInputStream(); fail("Cancelling delegation tokens should not be allowed with token auth"); } catch (IOException e) { - assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); } } @@ -353,7 +353,7 @@ public Void call() throws Exception { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); setupConn(conn, "POST", MediaType.APPLICATION_JSON, body); InputStream response = conn.getInputStream(); - assertEquals(Status.OK.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.OK.getStatusCode(), conn.getResponseCode()); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(response, "UTF8")); @@ -392,7 +392,7 @@ public Void call() throws Exception { fail("Client should not be allowed to impersonate using delegation tokens"); } catch(IOException ie) { - assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); } // this should also fail due to client2 not being a super user @@ -411,7 +411,7 @@ public Void call() throws Exception { fail("Non superuser client should not be allowed to carry out doAs"); } catch (IOException ie) { - assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); } return null; } @@ -430,7 +430,7 @@ public String call() throws Exception { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); setupConn(conn, "POST", MediaType.APPLICATION_JSON, body); InputStream response = conn.getInputStream(); - assertEquals(Status.OK.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.OK.getStatusCode(), conn.getResponseCode()); try (BufferedReader reader = new BufferedReader(new InputStreamReader( response, "UTF8"))) { String line; @@ -463,7 +463,7 @@ public Void call() throws Exception { tokenString); setupConn(conn, "DELETE", null, null); InputStream response = conn.getInputStream(); - assertEquals(Status.OK.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.OK.getStatusCode(), conn.getResponseCode()); response.close(); return null; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokens.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokens.java index 095b076df73a1..7346bf665e71e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokens.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesDelegationTokens.java @@ -29,10 +29,14 @@ import java.util.Map; import java.util.Properties; import java.util.concurrent.Callable; +import java.util.logging.Logger; import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -57,10 +61,14 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.DelegationToken; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.logging.LoggingFeature; +import org.glassfish.jersey.test.JerseyTest; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -69,6 +77,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -83,14 +92,9 @@ import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.filter.LoggingFilter; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; @RunWith(Parameterized.class) -public class TestRMWebServicesDelegationTokens extends JerseyTestBase { +public class TestRMWebServicesDelegationTokens extends JerseyTest { private static File testRootDir; private static File httpSpnegoKeytabFile = new File( @@ -104,6 +108,9 @@ public class TestRMWebServicesDelegationTokens extends JerseyTestBase { // Make sure the test uses the published header string final String yarnTokenHeader = "Hadoop-YARN-RM-Delegation-Token"; + private static final Logger LOG = + Logger.getLogger(TestRMWebServicesDelegationTokens.class.getName()); + @Singleton public static class TestKerberosAuthFilter extends AuthenticationFilter { @Override @@ -162,7 +169,7 @@ protected void configureServlets() { } else { filter("/*").through(TestSimpleAuthFilter.class); } - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -207,11 +214,6 @@ public static Collection guiceConfigs() { } public TestRMWebServicesDelegationTokens(int run) throws Exception { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); switch (run) { case 0: default: @@ -265,7 +267,7 @@ public void tearDown() throws Exception { @Test public void testCreateDelegationToken() throws Exception { rm.start(); - this.client().addFilter(new LoggingFilter(System.out)); + this.client().register(new LoggingFeature(LOG)); final String renewer = "test-renewer"; String jsonBody = "{ \"renewer\" : \"" + renewer + "\" }"; String xmlBody = @@ -293,11 +295,9 @@ public void testCreateDelegationToken() throws Exception { private void verifySimpleAuthCreate(String mediaType, String contentType, String body) { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").queryParam("user.name", "testuser") - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .queryParam("user.name", "testuser").request(contentType) + .post(Entity.entity(body, mediaType), Response.class); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); } @@ -310,23 +310,18 @@ private void verifyKerberosAuthCreate(String mType, String cType, KerberosTestUtils.doAsClient(new Callable() { @Override public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(body, mediaType).post(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(body, mediaType), Response.class); assertResponseStatusCode(Status.OK, response.getStatusInfo()); DelegationToken tok = getDelegationTokenFromResponse(response); assertFalse(tok.getToken().isEmpty()); - Token token = - new Token(); + Token token = new Token(); token.decodeFromUrlString(tok.getToken()); assertEquals(renewer, token.decodeIdentifier().getRenewer().toString()); assertValidRMToken(tok.getToken()); DelegationToken dtoken = new DelegationToken(); - response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(dtoken, mediaType).post(ClientResponse.class); + response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(dtoken, mediaType), Response.class); assertResponseStatusCode(Status.OK, response.getStatusInfo()); tok = getDelegationTokenFromResponse(response); assertFalse(tok.getToken().isEmpty()); @@ -343,10 +338,10 @@ public Void call() throws Exception { // it. The renewer should succeed; owner and third user should fail @Test public void testRenewDelegationToken() throws Exception { - client().addFilter(new LoggingFilter(System.out)); + this.client().register(new LoggingFeature(LOG)); rm.start(); final String renewer = "client2"; - this.client().addFilter(new LoggingFilter(System.out)); + this.client().register(new LoggingFeature(LOG)); final DelegationToken dummyToken = new DelegationToken(); dummyToken.setRenewer(renewer); String[] mediaTypes = @@ -360,113 +355,83 @@ public void testRenewDelegationToken() throws Exception { } // test "client" and client2" trying to renew "client" token - final DelegationToken responseToken = - KerberosTestUtils.doAsClient(new Callable() { - @Override - public DelegationToken call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(dummyToken, mediaType).post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - DelegationToken tok = getDelegationTokenFromResponse(response); - assertFalse(tok.getToken().isEmpty()); - String body = generateRenewTokenBody(mediaType, tok.getToken()); - response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").path("expiration") - .header(yarnTokenHeader, tok.getToken()) - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); - assertResponseStatusCode(Status.FORBIDDEN, - response.getStatusInfo()); - return tok; - } - }); - - KerberosTestUtils.doAs(renewer, new Callable() { - @Override - public DelegationToken call() throws Exception { - // renew twice so that we can confirm that the - // expiration time actually changes - long oldExpirationTime = Time.now(); - assertValidRMToken(responseToken.getToken()); - String body = - generateRenewTokenBody(mediaType, responseToken.getToken()); - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").path("expiration") - .header(yarnTokenHeader, responseToken.getToken()) - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - DelegationToken tok = getDelegationTokenFromResponse(response); - String message = - "Expiration time not as expected: old = " + oldExpirationTime - + "; new = " + tok.getNextExpirationTime(); - assertTrue(message, tok.getNextExpirationTime() > oldExpirationTime); - oldExpirationTime = tok.getNextExpirationTime(); - // artificial sleep to ensure we get a different expiration time - Thread.sleep(1000); - response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").path("expiration") + final DelegationToken responseToken = KerberosTestUtils.doAsClient(() -> { + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(dummyToken, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + DelegationToken tok = getDelegationTokenFromResponse(response); + assertFalse(tok.getToken().isEmpty()); + String body = generateRenewTokenBody(mediaType, tok.getToken()); + response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .path("expiration").request(contentType).header(yarnTokenHeader, tok.getToken()) + .post(Entity.entity(body, mediaType), Response.class); + assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + return tok; + }); + + KerberosTestUtils.doAs(renewer, () -> { + // renew twice so that we can confirm that the + // expiration time actually changes + long oldExpirationTime = Time.now(); + assertValidRMToken(responseToken.getToken()); + String body = generateRenewTokenBody(mediaType, responseToken.getToken()); + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .path("expiration").request(contentType) .header(yarnTokenHeader, responseToken.getToken()) - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - tok = getDelegationTokenFromResponse(response); - message = - "Expiration time not as expected: old = " + oldExpirationTime - + "; new = " + tok.getNextExpirationTime(); - assertTrue(message, tok.getNextExpirationTime() > oldExpirationTime); - return tok; - } + .post(Entity.entity(body, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + DelegationToken tok = getDelegationTokenFromResponse(response); + String message = + "Expiration time not as expected: old = " + oldExpirationTime + "; new = " + + tok.getNextExpirationTime(); + assertTrue(message, tok.getNextExpirationTime() > oldExpirationTime); + oldExpirationTime = tok.getNextExpirationTime(); + // artificial sleep to ensure we get a different expiration time + Thread.sleep(1000); + response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .path("expiration").request(contentType) + .header(yarnTokenHeader, responseToken.getToken()) + .post(Entity.entity(body, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + tok = getDelegationTokenFromResponse(response); + message = "Expiration time not as expected: old = " + oldExpirationTime + "; new = " + + tok.getNextExpirationTime(); + assertTrue(message, tok.getNextExpirationTime() > oldExpirationTime); + return tok; }); // test unauthorized user renew attempt - KerberosTestUtils.doAs("client3", new Callable() { - @Override - public DelegationToken call() throws Exception { - String body = - generateRenewTokenBody(mediaType, responseToken.getToken()); - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").path("expiration") + KerberosTestUtils.doAs("client3", (Callable) () -> { + String body = generateRenewTokenBody(mediaType, responseToken.getToken()); + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .path("expiration").request(contentType) .header(yarnTokenHeader, responseToken.getToken()) - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); - assertResponseStatusCode(Status.FORBIDDEN, - response.getStatusInfo()); - return null; - } + .post(Entity.entity(body, mediaType), Response.class); + assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + return null; }); // test bad request - incorrect format, empty token string and random // token string - KerberosTestUtils.doAsClient(new Callable() { - @Override - public Void call() throws Exception { - String token = "TEST_TOKEN_STRING"; - String body = ""; - if (mediaType.equals(MediaType.APPLICATION_JSON)) { - body = "{\"token\": \"" + token + "\" }"; - } else { - body = - "" + token - + ""; - } - - // missing token header - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").path("expiration") - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); - assertResponseStatusCode(Status.BAD_REQUEST, - response.getStatusInfo()); - return null; + KerberosTestUtils.doAsClient((Callable) () -> { + String token = "TEST_TOKEN_STRING"; + String body = ""; + if (mediaType.equals(MediaType.APPLICATION_JSON)) { + body = "{\"token\": \"" + token + "\" }"; + } else { + body = "" + token + ""; } + + // missing token header + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .path("expiration").request(contentType) + .post(Entity.entity(body, mediaType), Response.class); + assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + return null; }); } } @@ -488,11 +453,9 @@ private void verifySimpleAuthRenew(String mediaType, String contentType) { "" + token + ""; body = "abcd"; } - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").queryParam("user.name", "testuser") - .accept(contentType).entity(body, mediaType) - .post(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .queryParam("user.name", "testuser").request(contentType) + .post(Entity.entity(body, mediaType), Response.class); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); } @@ -501,7 +464,7 @@ private void verifySimpleAuthRenew(String mediaType, String contentType) { @Test public void testCancelDelegationToken() throws Exception { rm.start(); - this.client().addFilter(new LoggingFilter(System.out)); + this.client().register(new LoggingFeature(LOG)); if (isKerberosAuth == false) { verifySimpleAuthCancel(); return; @@ -516,83 +479,57 @@ public void testCancelDelegationToken() throws Exception { for (final String contentType : mediaTypes) { // owner should be able to cancel delegation token - KerberosTestUtils.doAsClient(new Callable() { - @Override - public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(dtoken, mediaType).post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - DelegationToken tok = getDelegationTokenFromResponse(response); - response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token") - .header(yarnTokenHeader, tok.getToken()).accept(contentType) - .delete(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - assertTokenCancelled(tok.getToken()); - return null; - } + KerberosTestUtils.doAsClient((Callable) () -> { + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(dtoken, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + DelegationToken tok = getDelegationTokenFromResponse(response); + response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).header(yarnTokenHeader, tok.getToken()).delete(Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + assertTokenCancelled(tok.getToken()); + return null; }); // renewer should be able to cancel token - final DelegationToken tmpToken = - KerberosTestUtils.doAsClient(new Callable() { - @Override - public DelegationToken call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(dtoken, mediaType).post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - DelegationToken tok = getDelegationTokenFromResponse(response); - return tok; - } - }); - - KerberosTestUtils.doAs(renewer, new Callable() { - @Override - public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token") - .header(yarnTokenHeader, tmpToken.getToken()) - .accept(contentType).delete(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - assertTokenCancelled(tmpToken.getToken()); - return null; - } + final DelegationToken tmpToken = KerberosTestUtils.doAsClient(() -> { + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(dtoken, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + DelegationToken tok = getDelegationTokenFromResponse(response); + return tok; + }); + + KerberosTestUtils.doAs(renewer, (Callable) () -> { + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).header(yarnTokenHeader, tmpToken.getToken()) + .delete(Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + assertTokenCancelled(tmpToken.getToken()); + return null; }); // third user should not be able to cancel token - final DelegationToken tmpToken2 = - KerberosTestUtils.doAsClient(new Callable() { - @Override - public DelegationToken call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(dtoken, mediaType).post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - DelegationToken tok = getDelegationTokenFromResponse(response); - return tok; - } - }); - - KerberosTestUtils.doAs("client3", new Callable() { - @Override - public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token") - .header(yarnTokenHeader, tmpToken2.getToken()) - .accept(contentType).delete(ClientResponse.class); - assertResponseStatusCode(Status.FORBIDDEN, - response.getStatusInfo()); - assertValidRMToken(tmpToken2.getToken()); - return null; - } + final DelegationToken tmpToken2 = KerberosTestUtils.doAsClient(() -> { + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(dtoken, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + DelegationToken tok = getDelegationTokenFromResponse(response); + return tok; + }); + + KerberosTestUtils.doAs("client3", (Callable) () -> { + Response response = + target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).header(yarnTokenHeader, tmpToken2.getToken()) + .delete(Response.class); + assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); + assertValidRMToken(tmpToken2.getToken()); + return null; }); testCancelTokenBadRequests(mediaType, contentType); @@ -613,86 +550,56 @@ private void testCancelTokenBadRequests(String mType, String cType) dtoken.setRenewer(renewer); // bad request(invalid header value) - KerberosTestUtils.doAsClient(new Callable() { - @Override - public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token") - .header(yarnTokenHeader, "random-string").accept(contentType) - .delete(ClientResponse.class); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); - return null; - } + KerberosTestUtils.doAsClient((Callable) () -> { + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).header(yarnTokenHeader, "random-string").delete(Response.class); + assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + return null; }); // bad request(missing header) - KerberosTestUtils.doAsClient(new Callable() { - @Override - public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .delete(ClientResponse.class); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + KerberosTestUtils.doAsClient((Callable) () -> { + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).delete(Response.class); + assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); - return null; - } + return null; }); // bad request(cancelled token) - final DelegationToken tmpToken = - KerberosTestUtils.doAsClient(new Callable() { - @Override - public DelegationToken call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").accept(contentType) - .entity(dtoken, mediaType).post(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - DelegationToken tok = getDelegationTokenFromResponse(response); - return tok; - } - }); + final DelegationToken tmpToken = KerberosTestUtils.doAsClient(() -> { + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).post(Entity.entity(dtoken, mediaType), Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + DelegationToken tok = getDelegationTokenFromResponse(response); + return tok; + }); - KerberosTestUtils.doAs(renewer, new Callable() { - @Override - public Void call() throws Exception { - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token") - .header(yarnTokenHeader, tmpToken.getToken()).accept(contentType) - .delete(ClientResponse.class); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); - response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token") - .header(yarnTokenHeader, tmpToken.getToken()).accept(contentType) - .delete(ClientResponse.class); - assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); - return null; - } + KerberosTestUtils.doAs(renewer, (Callable) () -> { + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).header(yarnTokenHeader, tmpToken.getToken()).delete(Response.class); + assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .request(contentType).header(yarnTokenHeader, tmpToken.getToken()).delete(Response.class); + assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); + return null; }); } private void verifySimpleAuthCancel() { // contents of header don't matter; request should never get that far - ClientResponse response = - resource().path("ws").path("v1").path("cluster") - .path("delegation-token").queryParam("user.name", "testuser") - .header(RMWebServices.DELEGATION_TOKEN_HEADER, "random") - .delete(ClientResponse.class); + Response response = target().path("ws").path("v1").path("cluster").path("delegation-token") + .queryParam("user.name", "testuser").request() + .header(RMWebServices.DELEGATION_TOKEN_HEADER, "random").delete(Response.class); assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo()); } - private DelegationToken - getDelegationTokenFromResponse(ClientResponse response) - throws IOException, ParserConfigurationException, SAXException, - JSONException { - if (response.getType().toString().contains(MediaType.APPLICATION_JSON)) { - return getDelegationTokenFromJson(response.getEntity(JSONObject.class)); + private DelegationToken getDelegationTokenFromResponse(Response response) + throws IOException, ParserConfigurationException, SAXException, JSONException { + if (response.getMediaType().toString().contains(MediaType.APPLICATION_JSON)) { + return getDelegationTokenFromJson(response.readEntity(JSONObject.class)); } - return getDelegationTokenFromXML(response.getEntity(String.class)); + return getDelegationTokenFromXML(response.readEntity(String.class)); } public static DelegationToken getDelegationTokenFromXML(String tokenXML) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesForCSWithPartitions.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesForCSWithPartitions.java index 0697ad0321952..9f73601279327 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesForCSWithPartitions.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesForCSWithPartitions.java @@ -35,7 +35,9 @@ import java.util.Set; import java.util.function.Predicate; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -63,11 +65,14 @@ import org.apache.hadoop.yarn.util.resource.Resources; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -79,12 +84,8 @@ import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableSet; import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestRMWebServicesForCSWithPartitions extends JerseyTestBase { +public class TestRMWebServicesForCSWithPartitions extends JerseyTest { private static final String DEFAULT_PARTITION = ""; private static final String CAPACITIES = "capacities"; private static final String RESOURCE_USAGES_BY_PARTITION = @@ -128,9 +129,9 @@ protected void configureServlets() { Assert.fail(); } bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } - }; + } static { GuiceServletConfig.setInjector( @@ -206,58 +207,53 @@ public void setUp() throws Exception { } public TestRMWebServicesForCSWithPartitions() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testSchedulerPartitions() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifySchedulerInfoJson(json); } @Test public void testSchedulerPartitionsSlash() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifySchedulerInfoJson(json); } @Test public void testSchedulerPartitionsDefault() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("scheduler").get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("scheduler").request().get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifySchedulerInfoJson(json); } @Test public void testSchedulerPartitionsXML() throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler") - .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + .request(MediaType.APPLICATION_XML).get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -295,7 +291,7 @@ public void testPartitionInSchedulerActivities() throws Exception { .capability(Resources.createResource(2048)).numContainers(1) .build()), null); - WebResource sr = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) + WebTarget sr = target().path(RMWSConsts.RM_WEB_SERVICE_PATH) .path(RMWSConsts.SCHEDULER_ACTIVITIES); ActivitiesTestUtils.requestWebResource(sr, null); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesHttpStaticUserPermissions.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesHttpStaticUserPermissions.java index cef32f4c2bfe6..05ebd4b8cd0b6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesHttpStaticUserPermissions.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesHttpStaticUserPermissions.java @@ -31,6 +31,8 @@ import java.util.Map; import java.util.HashMap; +import javax.ws.rs.core.Response; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; import org.apache.hadoop.minikdc.MiniKdc; @@ -46,8 +48,6 @@ import org.junit.BeforeClass; import org.junit.Test; -import com.sun.jersey.api.client.ClientResponse.Status; - public class TestRMWebServicesHttpStaticUserPermissions { private static final File testRootDir = new File("target", @@ -143,7 +143,7 @@ public void testWebServiceAccess() throws Exception { ""); try { conn.getInputStream(); - assertEquals(Status.OK.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.OK.getStatusCode(), conn.getResponseCode()); } catch (IOException e) { fail("Got " + conn.getResponseCode() + " instead of 200 accessing " + url.toString()); @@ -177,7 +177,7 @@ public void testWebServiceAccess() throws Exception { conn.getInputStream(); fail("Request " + entry.getKey() + "succeeded but should have failed"); } catch (IOException e) { - assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), conn.getResponseCode()); InputStream errorStream = conn.getErrorStream(); String error = ""; BufferedReader reader = new BufferedReader( diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java index 503b4a8b2bbdd..6dcd435461472 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java @@ -29,12 +29,24 @@ import java.util.List; import java.util.Map; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; import org.apache.commons.lang3.tuple.Pair; import org.apache.hadoop.util.Lists; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; +import org.glassfish.jersey.jettison.JettisonJaxbContext; +import org.glassfish.jersey.test.JerseyTest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.http.JettyUtils; @@ -50,7 +62,6 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodeToLabelsEntryList; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; @@ -60,15 +71,8 @@ import com.google.inject.Guice; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONMarshaller; -import com.sun.jersey.core.util.MultivaluedMapImpl; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestRMWebServicesNodeLabels extends JerseyTestBase { +public class TestRMWebServicesNodeLabels extends JerseyTest { private static final int BAD_REQUEST_CODE = 400; @@ -103,7 +107,7 @@ public class TestRMWebServicesNodeLabels extends JerseyTestBase { private static String userName; private static String notUserName; private static RMWebServices rmWebService; - private WebResource resource; + private WebTarget target; private static class WebServletModule extends ServletModule { @@ -127,7 +131,7 @@ protected void configureServlets() { bind(ResourceManager.class).toInstance(rm); filter("/*").through( TestRMWebServicesAppsModification.TestRMCustomAuthFilter.class); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -137,64 +141,57 @@ public void setUp() throws Exception { super.setUp(); GuiceServletConfig.setInjector( Guice.createInjector(new WebServletModule())); - resource = resource(); - } - - public TestRMWebServicesNodeLabels() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); + target = target(); } - private WebResource getClusterWebResource() { - return resource.path(PATH_WS).path(PATH_V1).path(PATH_CLUSTER); + private WebTarget getClusterWebResource() { + return target.path(PATH_WS).path(PATH_V1).path(PATH_CLUSTER); } - private ClientResponse get(String path) { - return getClusterWebResource() - .path(path) - .queryParam(QUERY_USER_NAME, userName) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + private Response get(String path) { + return getClusterWebResource().path(path).queryParam(QUERY_USER_NAME, userName) + .request(MediaType.APPLICATION_JSON).get(Response.class); } - private ClientResponse get(String path, MultivaluedMapImpl queryParams) { - return getClusterWebResource() - .path(path) - .queryParam(QUERY_USER_NAME, userName) - .queryParams(queryParams) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + private Response get(String path, MultivaluedMap queryParams) { + WebTarget target = getClusterWebResource().path(path).queryParam(QUERY_USER_NAME, userName); + for (Map.Entry> set : queryParams.entrySet()) { + target.queryParam(set.getKey(), set.getValue()); + } + return target.request(MediaType.APPLICATION_JSON).get(Response.class); } - private ClientResponse post(String path, String userName, Object payload, + private Response post(String path, String userName, Object payload, Class payloadClass) throws Exception { return getClusterWebResource() .path(path) .queryParam(QUERY_USER_NAME, userName) - .accept(MediaType.APPLICATION_JSON) - .entity(toJson(payload, payloadClass), - MediaType.APPLICATION_JSON) - .post(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .post(Entity.entity(toJson(payload, payloadClass), + MediaType.APPLICATION_JSON), Response.class); } - private ClientResponse post(String path, String userName, Object payload, - Class payloadClass, MultivaluedMapImpl queryParams) throws Exception { - WebResource.Builder builder = getClusterWebResource() - .path(path) - .queryParam(QUERY_USER_NAME, userName) - .queryParams(queryParams) - .accept(MediaType.APPLICATION_JSON); + private Response post(String path, String userName, Object payload, Class payloadClass, + MultivaluedMap queryParams) throws Exception { + WebTarget target = getClusterWebResource().path(path).queryParam(QUERY_USER_NAME, userName); + + for (Map.Entry> queryParam : queryParams.entrySet()) { + for (String value : queryParam.getValue()) { + target.queryParam(queryParam.getKey(), value); + } + } if (payload != null && payloadClass != null) { - builder.entity(toJson(payload, payloadClass), MediaType.APPLICATION_JSON); + return target.request(MediaType.APPLICATION_JSON) + .post(Entity.entity(toJson(payload, payloadClass), MediaType.APPLICATION_JSON), + Response.class); } - return builder.post(ClientResponse.class); + return target.request(MediaType.APPLICATION_JSON).post(null); } @Test public void testNodeLabels() throws Exception { - ClientResponse response; + Response response; // Add a label response = addNodeLabels(Lists.newArrayList(Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY))); @@ -203,7 +200,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfo(response.getEntity(NodeLabelsInfo.class), Lists.newArrayList( + assertNodeLabelsInfo(response.readEntity(NodeLabelsInfo.class), Lists.newArrayList( Pair.of(LABEL_A, true))); // Add another @@ -214,7 +211,7 @@ public void testNodeLabels() throws Exception { response = getNodeLabels(); assertApplicationJsonUtf8Response(response); // Verify exclusivity for 'b' as false - assertNodeLabelsInfo(response.getEntity(NodeLabelsInfo.class), + assertNodeLabelsInfo(response.readEntity(NodeLabelsInfo.class), Lists.newArrayList( Pair.of(LABEL_A, true), Pair.of(LABEL_B, false))); @@ -234,7 +231,7 @@ public void testNodeLabels() throws Exception { // Verify all, using get-labels-to-Nodes response = getNodeLabelMappings(); assertApplicationJsonUtf8Response(response); - LabelsToNodesInfo labelsToNodesInfo = response.getEntity(LabelsToNodesInfo.class); + LabelsToNodesInfo labelsToNodesInfo = response.readEntity(LabelsToNodesInfo.class); assertLabelsToNodesInfo(labelsToNodesInfo, 2, Lists.newArrayList( Pair.of(Pair.of(LABEL_B, false), Lists.newArrayList(NODE_1, NODE_2)), Pair.of(Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY), Lists.newArrayList(NODE_0)) @@ -243,7 +240,7 @@ public void testNodeLabels() throws Exception { // Verify, using get-labels-to-Nodes for specified set of labels response = getNodeLabelMappingsByLabels(LABEL_A); assertApplicationJsonUtf8Response(response); - labelsToNodesInfo = response.getEntity(LabelsToNodesInfo.class); + labelsToNodesInfo = response.readEntity(LabelsToNodesInfo.class); assertLabelsToNodesInfo(labelsToNodesInfo, 1, Lists.newArrayList( Pair.of(Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY), Lists.newArrayList(NODE_0)) )); @@ -251,7 +248,7 @@ public void testNodeLabels() throws Exception { // Verify response = getLabelsOfNode(NODE_0); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfoContains(response.getEntity(NodeLabelsInfo.class), + assertNodeLabelsInfoContains(response.readEntity(NodeLabelsInfo.class), Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY)); // Replace @@ -261,7 +258,7 @@ public void testNodeLabels() throws Exception { // Verify response = getLabelsOfNode(NODE_0); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfoContains(response.getEntity(NodeLabelsInfo.class), Pair.of(LABEL_B, false)); + assertNodeLabelsInfoContains(response.readEntity(NodeLabelsInfo.class), Pair.of(LABEL_B, false)); // Replace labels using node-to-labels response = replaceNodeToLabels(Lists.newArrayList(Pair.of(NODE_0, @@ -271,7 +268,7 @@ public void testNodeLabels() throws Exception { // Verify, using node-to-labels response = getNodeToLabels(); assertApplicationJsonUtf8Response(response); - NodeToLabelsInfo nodeToLabelsInfo = response.getEntity(NodeToLabelsInfo.class); + NodeToLabelsInfo nodeToLabelsInfo = response.readEntity(NodeToLabelsInfo.class); NodeLabelsInfo nodeLabelsInfo = nodeToLabelsInfo.getNodeToLabels().get(NODE_0); assertNodeLabelsSize(nodeLabelsInfo, 1); assertNodeLabelsInfoContains(nodeLabelsInfo, Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY)); @@ -282,7 +279,7 @@ public void testNodeLabels() throws Exception { // Verify response = getLabelsOfNode(NODE_0); assertApplicationJsonUtf8Response(response); - assertNodeLabelsSize(response.getEntity(NodeLabelsInfo.class), 0); + assertNodeLabelsSize(response.readEntity(NodeLabelsInfo.class), 0); // Add a label back for auth tests response = replaceLabelsOnNode(NODE_0, LABEL_A); @@ -291,7 +288,7 @@ public void testNodeLabels() throws Exception { // Verify response = getLabelsOfNode(NODE_0); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfoContains(response.getEntity(NodeLabelsInfo.class), + assertNodeLabelsInfoContains(response.readEntity(NodeLabelsInfo.class), Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY)); // Auth fail replace labels on node @@ -300,7 +297,7 @@ public void testNodeLabels() throws Exception { // Verify response = getLabelsOfNode(NODE_0); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfoContains(response.getEntity(NodeLabelsInfo.class), + assertNodeLabelsInfoContains(response.readEntity(NodeLabelsInfo.class), Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY)); // Fail to add a label with wrong user @@ -311,7 +308,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - assertNodeLabelsSize(response.getEntity(NodeLabelsInfo.class), 2); + assertNodeLabelsSize(response.readEntity(NodeLabelsInfo.class), 2); // Remove cluster label (succeed, we no longer need it) response = removeNodeLabel(LABEL_B); @@ -319,7 +316,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfo(response.getEntity(NodeLabelsInfo.class), + assertNodeLabelsInfo(response.readEntity(NodeLabelsInfo.class), Lists.newArrayList(Pair.of(LABEL_A, true))); // Remove cluster label with post @@ -328,7 +325,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - nodeLabelsInfo = response.getEntity(NodeLabelsInfo.class); + nodeLabelsInfo = response.readEntity(NodeLabelsInfo.class); assertEquals(0, nodeLabelsInfo.getNodeLabels().size()); // Following test cases are to test replace when distributed node label @@ -352,7 +349,7 @@ public void testNodeLabels() throws Exception { // Verify, using node-to-labels that previous operation has failed response = getNodeToLabels(); assertApplicationJsonUtf8Response(response); - nodeToLabelsInfo = response.getEntity(NodeToLabelsInfo.class); + nodeToLabelsInfo = response.readEntity(NodeToLabelsInfo.class); nodeLabelsInfo = nodeToLabelsInfo.getNodeToLabels().get(NODE_0); assertNodeLabelsSize(nodeLabelsInfo, 1); assertNodeLabelsInfoDoesNotContain(nodeLabelsInfo, Pair.of(LABEL_X, false)); @@ -364,7 +361,7 @@ public void testNodeLabels() throws Exception { // Verify, using node-to-labels that previous operation has failed response = getNodeToLabels(); assertApplicationJsonUtf8Response(response); - nodeToLabelsInfo = response.getEntity(NodeToLabelsInfo.class); + nodeToLabelsInfo = response.readEntity(NodeToLabelsInfo.class); nodeLabelsInfo = nodeToLabelsInfo.getNodeToLabels().get(NODE_0); assertNodeLabelsSize(nodeLabelsInfo, 1); assertNodeLabelsInfoDoesNotContain(nodeLabelsInfo, Pair.of(LABEL_X, false)); @@ -375,7 +372,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfoAtPosition(response.getEntity(NodeLabelsInfo.class), Pair.of(LABEL_Y, + assertNodeLabelsInfoAtPosition(response.readEntity(NodeLabelsInfo.class), Pair.of(LABEL_Y, false), 0); // Remove y @@ -385,7 +382,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - assertNodeLabelsSize(response.getEntity(NodeLabelsInfo.class), 0); + assertNodeLabelsSize(response.readEntity(NodeLabelsInfo.class), 0); // add a new nodelabel with exclusivity=false response = addNodeLabels(Lists.newArrayList(Pair.of(LABEL_Z, false))); @@ -393,7 +390,7 @@ public void testNodeLabels() throws Exception { // Verify response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - assertNodeLabelsInfoAtPosition(response.getEntity(NodeLabelsInfo.class), + assertNodeLabelsInfoAtPosition(response.readEntity(NodeLabelsInfo.class), Pair.of(LABEL_Z, false), 0); assertNodeLabelsSize(nodeLabelsInfo, 1); } @@ -460,7 +457,7 @@ private void assertNodeLabelsSize(NodeLabelsInfo nodeLabelsInfo, int expectedSiz assertEquals(expectedSize, nodeLabelsInfo.getNodeLabelsInfo().size()); } - private ClientResponse replaceNodeToLabels(List>> nodeToLabelInfos) throws Exception { + private Response replaceNodeToLabels(List>> nodeToLabelInfos) throws Exception { NodeToLabelsEntryList nodeToLabelsEntries = new NodeToLabelsEntryList(); for (Pair> nodeToLabelInfo : nodeToLabelInfos) { @@ -472,65 +469,65 @@ private ClientResponse replaceNodeToLabels(List>> node return post(PATH_REPLACE_NODE_TO_LABELS, userName, nodeToLabelsEntries, NodeToLabelsEntryList.class); } - private ClientResponse getNodeLabelMappings() { + private Response getNodeLabelMappings() { return get(PATH_LABEL_MAPPINGS); } - private ClientResponse getNodeLabelMappingsByLabels(String... labelNames) { - MultivaluedMapImpl params = createMultiValuedMap(labelNames); + private Response getNodeLabelMappingsByLabels(String... labelNames) { + MultivaluedMap params = createMultiValuedMap(labelNames); return get(PATH_LABEL_MAPPINGS, params); } - private ClientResponse replaceLabelsOnNode(String node, String... labelNames) throws Exception { + private Response replaceLabelsOnNode(String node, String... labelNames) throws Exception { return replaceLabelsOnNodeWithUserName(node, userName, labelNames); } - private ClientResponse replaceLabelsOnNodeWithUserName(String node, + private Response replaceLabelsOnNodeWithUserName(String node, String userName, String... labelNames) throws Exception { LOG.info("Replacing labels on node '{}', label(s): {}", node, labelNames); - MultivaluedMapImpl params = createMultiValuedMap(labelNames); + MultivaluedMap params = createMultiValuedMap(labelNames); String path = UriBuilder.fromPath(PATH_NODES).path(node) .path(PATH_REPLACE_LABELS).build().toString(); return post(path, userName, null, null, params); } - private static MultivaluedMapImpl createMultiValuedMap(String[] labelNames) { - MultivaluedMapImpl params = new MultivaluedMapImpl(); + private static MultivaluedMap createMultiValuedMap(String[] labelNames) { + MultivaluedMap params = new MultivaluedHashMap<>(); for (String labelName : labelNames) { params.add("labels", labelName); } return params; } - private ClientResponse removeNodeLabel(String... labelNames) throws Exception { - MultivaluedMapImpl params = createMultiValuedMap(labelNames); + private Response removeNodeLabel(String... labelNames) throws Exception { + MultivaluedMap params = createMultiValuedMap(labelNames); return post(PATH_REMOVE_LABELS, userName, null, null, params); } - private ClientResponse getLabelsOfNode(String node) { + private Response getLabelsOfNode(String node) { String path = UriBuilder.fromPath(PATH_NODES).path(node) .path(PATH_GET_LABELS).build().toString(); return get(path); } - private ClientResponse getNodeLabels() { + private Response getNodeLabels() { return get(PATH_GET_NODE_LABELS); } - private ClientResponse getNodeToLabels() { + private Response getNodeToLabels() { return get(PATH_GET_NODE_TO_LABELS); } - private ClientResponse addNodeLabels(List> nlInfos) throws Exception { + private Response addNodeLabels(List> nlInfos) throws Exception { return addNodeLabelsInternal(nlInfos, userName); } - private ClientResponse addNodeLabelsWithUser(List> nlInfos, + private Response addNodeLabelsWithUser(List> nlInfos, String userName) throws Exception { return addNodeLabelsInternal(nlInfos, userName); } - private ClientResponse addNodeLabelsInternal(List> nlInfos, + private Response addNodeLabelsInternal(List> nlInfos, String userName) throws Exception { NodeLabelsInfo nodeLabelsInfo = new NodeLabelsInfo(); for (Pair nlInfo : nlInfos) { @@ -540,20 +537,20 @@ private ClientResponse addNodeLabelsInternal(List> nlInfos return post(PATH_ADD_NODE_LABELS, userName, nodeLabelsInfo, NodeLabelsInfo.class); } - private void assertApplicationJsonUtf8Response(ClientResponse response) { + private void assertApplicationJsonUtf8Response(Response response) { assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); } - private void assertHttp200(ClientResponse response) { + private void assertHttp200(Response response) { assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); } - private void assertHttp401(ClientResponse response) { + private void assertHttp401(Response response) { assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); } - private void assertHttp404(ClientResponse response) { + private void assertHttp404(Response response) { assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); } @@ -561,7 +558,7 @@ private void assertHttp404(ClientResponse response) { public void testLabelInvalidAddition() throws Exception { // Add a invalid label - ClientResponse response = addNodeLabels(Lists.newArrayList(Pair.of("a&", + Response response = addNodeLabels(Lists.newArrayList(Pair.of("a&", DEFAULT_NL_EXCLUSIVITY))); String expectedMessage = "java.io.IOException: label name should only contains" @@ -573,7 +570,7 @@ public void testLabelInvalidAddition() @Test public void testLabelChangeExclusivity() throws Exception { - ClientResponse response; + Response response; response = addNodeLabels(Lists.newArrayList(Pair.of("newLabel", DEFAULT_NL_EXCLUSIVITY))); assertHttp200(response); // new info and change exclusivity @@ -584,11 +581,11 @@ public void testLabelChangeExclusivity() validateJsonExceptionContent(response, expectedMessage); } - private void validateJsonExceptionContent(ClientResponse response, + private void validateJsonExceptionContent(Response response, String expectedMessage) throws JSONException { Assert.assertEquals(BAD_REQUEST_CODE, response.getStatus()); - JSONObject msg = response.getEntity(JSONObject.class); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); String message = exception.getString("message"); assertEquals("incorrect number of elements", 3, exception.length()); @@ -605,7 +602,7 @@ private void validateJsonExceptionContent(ClientResponse response, @Test public void testLabelInvalidReplace() throws Exception { - ClientResponse response; + Response response; // replace label which doesn't exist response = replaceLabelsOnNode(NODE_0, "idontexist"); @@ -618,7 +615,7 @@ public void testLabelInvalidReplace() @Test public void testLabelInvalidRemove() throws Exception { - ClientResponse response; + Response response; response = removeNodeLabel("ireallydontexist"); String expectedMessage = "java.io.IOException: Node label=ireallydontexist to be" @@ -629,7 +626,7 @@ public void testLabelInvalidRemove() @Test public void testNodeLabelPartitionInfo() throws Exception { - ClientResponse response; + Response response; // Add a node label response = addNodeLabels(Lists.newArrayList(Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY))); @@ -638,7 +635,7 @@ public void testNodeLabelPartitionInfo() throws Exception { // Verify partition info in get-node-labels response = getNodeLabels(); assertApplicationJsonUtf8Response(response); - NodeLabelsInfo nodeLabelsInfo = response.getEntity(NodeLabelsInfo.class); + NodeLabelsInfo nodeLabelsInfo = response.readEntity(NodeLabelsInfo.class); assertNodeLabelsSize(nodeLabelsInfo, 1); for (NodeLabelInfo nl : nodeLabelsInfo.getNodeLabelsInfo()) { assertEquals(LABEL_A, nl.getName()); @@ -654,7 +651,7 @@ public void testNodeLabelPartitionInfo() throws Exception { // Verify partition info in label-mappings response = getNodeLabelMappings(); assertApplicationJsonUtf8Response(response); - LabelsToNodesInfo labelsToNodesInfo = response.getEntity(LabelsToNodesInfo.class); + LabelsToNodesInfo labelsToNodesInfo = response.readEntity(LabelsToNodesInfo.class); assertLabelsToNodesInfo(labelsToNodesInfo, 1, Lists.newArrayList( Pair.of(Pair.of(LABEL_A, DEFAULT_NL_EXCLUSIVITY), Lists.newArrayList("nodeId:0")) )); @@ -666,9 +663,9 @@ public void testNodeLabelPartitionInfo() throws Exception { @SuppressWarnings("rawtypes") private String toJson(Object obj, Class klass) throws Exception { StringWriter sw = new StringWriter(); - JSONJAXBContext ctx = new JSONJAXBContext(klass); - JSONMarshaller jm = ctx.createJSONMarshaller(); - jm.marshallToJSON(obj, sw); + JAXBContext ctx = new JettisonJaxbContext(klass); + Marshaller jm = ctx.createMarshaller(); + jm.marshal(obj, sw); return sw.toString(); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodes.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodes.java index c67c49a361006..cf4f797ce627d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodes.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodes.java @@ -41,7 +41,12 @@ import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -85,11 +90,14 @@ import org.apache.hadoop.yarn.util.YarnVersionInfo; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.apache.hadoop.yarn.webapp.WebServicesTestUtils; + +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -101,14 +109,8 @@ import com.google.inject.Guice; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; -public class TestRMWebServicesNodes extends JerseyTestBase { +public class TestRMWebServicesNodes extends JerseyTest { private static MockRM rm; private static YarnConfiguration conf; @@ -135,7 +137,7 @@ protected void configureServlets() { rm.disableDrainEventsImplicitly(); bind(ResourceManager.class).toInstance(rm); filter("/*").through(TestRMCustomAuthFilter.class); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -177,11 +179,6 @@ public void setUp() throws Exception { } public TestRMWebServicesNodes() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test @@ -203,7 +200,7 @@ public void testNodesDefault() throws JSONException, Exception { public void testNodesDefaultWithUnHealthyNode() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getRunningRMNode("h1", 1234, 5120); // h2 will be in NEW state getNewRMNode("h2", 1235, 5121); @@ -220,13 +217,13 @@ public void testNodesDefaultWithUnHealthyNode() throws JSONException, .handle(new RMNodeStatusEvent(nodeId3, nodeStatus, null)); rm.waitForState(nodeId3, NodeState.UNHEALTHY); - ClientResponse response = - r.path("ws").path("v1").path("cluster").path("nodes") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("cluster").path("nodes").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); @@ -265,18 +262,18 @@ private RMNodeImpl getNewRMNode(String host, int port, int memory) { @Test public void testNodesQueryNew() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getRunningRMNode("h1", 1234, 5120); // h2 will be in NEW state RMNode rmnode2 = getNewRMNode("h2", 1235, 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("nodes").queryParam("states", NodeState.NEW.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); @@ -289,17 +286,17 @@ public void testNodesQueryNew() throws JSONException, Exception { @Test public void testNodesQueryStateNone() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getNewRMNode("h1", 1234, 5120); getNewRMNode("h2", 1235, 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("nodes") .queryParam("states", NodeState.DECOMMISSIONED.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("nodes is not empty", new JSONObject().toString(), json.get("nodes").toString()); @@ -307,24 +304,24 @@ public void testNodesQueryStateNone() throws JSONException, Exception { @Test public void testNodesQueryStateInvalid() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getNewRMNode("h1", 1234, 5120); getNewRMNode("h2", 1235, 5121); try { r.path("ws").path("v1").path("cluster").path("nodes") - .queryParam("states", "BOGUSSTATE").accept(MediaType.APPLICATION_JSON) + .queryParam("states", "BOGUSSTATE").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception querying invalid state"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -345,19 +342,19 @@ public void testNodesQueryStateInvalid() throws JSONException, Exception { @Test public void testNodesQueryStateLost() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); RMNode rmnode1 = getRunningRMNode("h1", 1234, 5120); sendLostEvent(rmnode1); RMNode rmnode2 = getRunningRMNode("h2", 1235, 5121); sendLostEvent(rmnode2); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("nodes").queryParam("states", NodeState.LOST.toString()) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); JSONArray nodeArray = nodes.getJSONArray("node"); @@ -378,18 +375,18 @@ public void testNodesQueryStateLost() throws JSONException, Exception { @Test public void testSingleNodeQueryStateLost() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getRunningRMNode("h1", 1234, 5120); RMNode rmnode2 = getRunningRMNode("h2", 1234, 5121); sendLostEvent(rmnode2); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").path("h2:1234").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").path("h2:1234").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject info = json.getJSONObject("node"); String id = info.get("id").toString(); @@ -407,16 +404,16 @@ public void testSingleNodeQueryStateLost() throws JSONException, Exception { @Test public void testNodesQueryRunning() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getRunningRMNode("h1", 1234, 5120); // h2 will be in NEW state getNewRMNode("h2", 1235, 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("nodes").queryParam("states", "running") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); @@ -426,16 +423,16 @@ public void testNodesQueryRunning() throws JSONException, Exception { @Test public void testNodesQueryHealthyFalse() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getRunningRMNode("h1", 1234, 5120); // h2 will be in NEW state getNewRMNode("h2", 1235, 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("nodes").queryParam("states", "UNHEALTHY") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("nodes is not empty", new JSONObject().toString(), json.get("nodes").toString()); @@ -443,15 +440,15 @@ public void testNodesQueryHealthyFalse() throws JSONException, Exception { public void testNodesHelper(String path, String media) throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); RMNode rmnode1 = getRunningRMNode("h1", 1234, 5120); RMNode rmnode2 = getRunningRMNode("h2", 1235, 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path(path).accept(media).get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path(path).request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); @@ -492,13 +489,13 @@ public void testSingleNodeDefault() throws JSONException, Exception { public void testSingleNodeHelper(String nodeid, RMNode nm, String media) throws JSONException, Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").path(nodeid).accept(media).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").path(nodeid).request(media).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("node"); verifyNodeInfo(info, nm); @@ -510,19 +507,19 @@ public void testNonexistNode() throws JSONException, Exception { getNewRMNode("h1", 1234, 5120); // add h2 node in NEW state getNewRMNode("h2", 1235, 5121); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("nodes") - .path("node_invalid:99").accept(MediaType.APPLICATION_JSON) + .path("node_invalid:99").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on non-existent nodeid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); - assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); + assertResponseStatusCode(Response.Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -538,18 +535,18 @@ public void testNonexistNode() throws JSONException, Exception { public void testNonexistNodeDefault() throws JSONException, Exception { getNewRMNode("h1", 1234, 5120); getNewRMNode("h2", 1235, 5121); - WebResource r = resource(); + WebTarget r = target(); try { - r.path("ws").path("v1").path("cluster").path("nodes") - .path("node_invalid:99").get(JSONObject.class); + r.path("ws").path("v1").path("cluster").path("nodes").path("node_invalid:99").request() + .get(JSONObject.class); fail("should have thrown exception on non-existent nodeid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -564,19 +561,19 @@ public void testNonexistNodeDefault() throws JSONException, Exception { public void testNonexistNodeXML() throws JSONException, Exception { getNewRMNode("h1", 1234, 5120); getNewRMNode("h2", 1235, 5121); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("nodes") - .path("node_invalid:99").accept(MediaType.APPLICATION_XML) + .path("node_invalid:99").request(MediaType.APPLICATION_XML) .get(JSONObject.class); fail("should have thrown exception on non-existent nodeid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.NOT_FOUND, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String msg = response.getEntity(String.class); + response.getMediaType().toString()); + String msg = response.readEntity(String.class); System.out.println(msg); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); @@ -607,20 +604,20 @@ public void testInvalidNode() throws JSONException, Exception { getNewRMNode("h1", 1234, 5120); getNewRMNode("h2", 1235, 5121); - WebResource r = resource(); + WebTarget r = target(); try { r.path("ws").path("v1").path("cluster").path("nodes") - .path("node_invalid_foo").accept(MediaType.APPLICATION_JSON) + .path("node_invalid_foo").request(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on non-existent nodeid"); - } catch (UniformInterfaceException ue) { - ClientResponse response = ue.getResponse(); + } catch (WebApplicationException ue) { + Response response = ue.getResponse(); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject msg = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject msg = response.readEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); @@ -637,15 +634,15 @@ public void testInvalidNode() throws JSONException, Exception { @Test public void testNodesXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); RMNodeImpl rmnode1 = getNewRMNode("h1", 1234, 5120); // MockNM nm2 = rm.registerNode("h2:1235", 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); @@ -660,17 +657,17 @@ public void testNodesXML() throws JSONException, Exception { @Test public void testSingleNodesXML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); // add h2 node in NEW state RMNodeImpl rmnode1 = getNewRMNode("h1", 1234, 5120); // MockNM nm2 = rm.registerNode("h2:1235", 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").path("h1:1234").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").path("h1:1234").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); @@ -684,15 +681,15 @@ public void testSingleNodesXML() throws JSONException, Exception { @Test public void testNodes2XML() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); getNewRMNode("h1", 1234, 5120); getNewRMNode("h2", 1235, 5121); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").request(MediaType.APPLICATION_XML) + .get(Response.class); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); @@ -707,7 +704,7 @@ public void testNodes2XML() throws JSONException, Exception { @Test public void testQueryAll() throws Exception { - WebResource r = resource(); + WebTarget r = target(); getRunningRMNode("h1", 1234, 5120); // add h2 node in NEW state getNewRMNode("h2", 1235, 5121); @@ -715,14 +712,14 @@ public void testQueryAll() throws Exception { RMNode nm3 = getRunningRMNode("h3", 1236, 5122); sendLostEvent(nm3); - ClientResponse response = r.path("ws").path("v1").path("cluster") + Response response = r.path("ws").path("v1").path("cluster") .path("nodes") .queryParam("states", Joiner.on(',').join(EnumSet.allOf(NodeState.class))) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); JSONArray nodeArray = nodes.getJSONArray("node"); @@ -731,7 +728,7 @@ public void testQueryAll() throws Exception { @Test public void testNodesResourceUtilization() throws JSONException, Exception { - WebResource r = resource(); + WebTarget r = target(); RMNode rmnode1 = getRunningRMNode("h1", 1234, 5120); NodeId nodeId1 = rmnode1.getNodeID(); @@ -749,13 +746,13 @@ public void testNodesResourceUtilization() throws JSONException, Exception { node.handle(new RMNodeStatusEvent(nodeId1, nodeStatus, null)); rm.waitForState(nodeId1, NodeState.RUNNING); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").accept(MediaType.APPLICATION_JSON) - .get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); @@ -769,7 +766,7 @@ public void testNodesResourceUtilization() throws JSONException, Exception { @Test public void testUpdateNodeResource() throws Exception { - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH); + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH); r = r.queryParam("user.name", userName); RMNode rmnode = getRunningRMNode("h1", 1234, 5120); @@ -777,10 +774,10 @@ public void testUpdateNodeResource() throws Exception { assertEquals("h1:1234", rmnodeId); // assert memory and default vcores - ClientResponse response = r.path(RMWSConsts.NODES).path(rmnodeId) - .accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); - NodeInfo nodeInfo0 = response.getEntity(NodeInfo.class); + Response response = r.path(RMWSConsts.NODES).path(rmnodeId) + .request(MediaType.APPLICATION_XML) + .get(Response.class); + NodeInfo nodeInfo0 = response.readEntity(NodeInfo.class); ResourceInfo nodeResource0 = nodeInfo0.getTotalResource(); assertEquals(5120, nodeResource0.getMemorySize()); assertEquals(4, nodeResource0.getvCores()); @@ -792,31 +789,29 @@ public void testUpdateNodeResource() throws Exception { Resource resource = Resource.newInstance(8192, 5); ResourceOptionInfo resourceOption = new ResourceOptionInfo( ResourceOption.newInstance(resource, 1000)); - response = r.path(RMWSConsts.NODES).path(rmnodeId).path("resource") - .entity(resourceOption, MediaType.APPLICATION_XML_TYPE) - .accept(MediaType.APPLICATION_XML) - .post(ClientResponse.class); + response = + r.path(RMWSConsts.NODES).path(rmnodeId).path("resource").request(MediaType.APPLICATION_XML) + .post(Entity.entity(resourceOption, MediaType.APPLICATION_XML_TYPE), Response.class); assertResponseStatusCode(Status.OK, response.getStatusInfo()); - ResourceInfo updatedResource = response.getEntity(ResourceInfo.class); + ResourceInfo updatedResource = response.readEntity(ResourceInfo.class); assertEquals(8192, updatedResource.getMemorySize()); assertEquals(5, updatedResource.getvCores()); // assert updated memory and cores response = r.path(RMWSConsts.NODES).path(rmnodeId) - .accept(MediaType.APPLICATION_XML) - .get(ClientResponse.class); - NodeInfo nodeInfo1 = response.getEntity(NodeInfo.class); + .request(MediaType.APPLICATION_XML) + .get(Response.class); + NodeInfo nodeInfo1 = response.readEntity(NodeInfo.class); ResourceInfo nodeResource1 = nodeInfo1.getTotalResource(); assertEquals(8192, nodeResource1.getMemorySize()); assertEquals(5, nodeResource1.getvCores()); // test non existing node response = r.path(RMWSConsts.NODES).path("badnode").path("resource") - .entity(resourceOption, MediaType.APPLICATION_XML_TYPE) - .accept(MediaType.APPLICATION_JSON) - .post(ClientResponse.class); + .request(MediaType.APPLICATION_JSON) + .post(Entity.entity(resourceOption, MediaType.APPLICATION_XML_TYPE), Response.class); assertResponseStatusCode(Status.BAD_REQUEST, response.getStatusInfo()); - JSONObject json = response.getEntity(JSONObject.class); + JSONObject json = response.readEntity(JSONObject.class); JSONObject exception = json.getJSONObject("RemoteException"); assertEquals("IllegalArgumentException", exception.getString("exception")); String msg = exception.getString("message"); @@ -989,12 +984,12 @@ public void testNodesAllocationTags() throws Exception { rm.registerNode(nm1.toString(), 1024); rm.registerNode(nm2.toString(), 1024); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").accept("application/json").get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").request("application/json").get(Response.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject nodesInfoJson = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject nodesInfoJson = response.readEntity(JSONObject.class); verifyNodeAllocationTag(nodesInfoJson, expectedAllocationTags); rm.stop(); @@ -1036,11 +1031,11 @@ public void testNodeAttributesInfo() throws Exception { heartbeatReq.setNodeAttributes(nodeAttributes); resourceTrackerService.nodeHeartbeat(heartbeatReq); - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("nodes").accept("application/json").get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster") + .path("nodes").request("application/json").get(Response.class); - JSONObject nodesInfoJson = response.getEntity(JSONObject.class); + JSONObject nodesInfoJson = response.readEntity(JSONObject.class); JSONArray nodes = nodesInfoJson.getJSONObject("nodes") .getJSONArray("node"); JSONObject nodeJson = nodes.getJSONObject(0); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesReservation.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesReservation.java index a0481283f0f05..574baec0c39e4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesReservation.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesReservation.java @@ -34,7 +34,10 @@ import javax.servlet.FilterConfig; import javax.servlet.ServletException; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; @@ -63,10 +66,16 @@ import org.apache.hadoop.yarn.util.UTCClock; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -78,18 +87,9 @@ import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONUnmarshaller; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; @RunWith(Parameterized.class) -public class TestRMWebServicesReservation extends JerseyTestBase { +public class TestRMWebServicesReservation extends JerseyTest { private String webserviceUserName = "testuser"; private static boolean setAuthFilter = false; @@ -111,6 +111,11 @@ public class TestRMWebServicesReservation extends JerseyTestBase { private static final String GET_NEW_RESERVATION_PATH = "reservation/new-reservation"; + private static final ObjectWriter writer = new ObjectMapper() + .writerFor(ReservationSubmissionRequestInfo.class).withDefaultPrettyPrinter(); + private static final ObjectReader reader = + new ObjectMapper().readerFor(ReservationSubmissionRequestInfo.class); + /* * Helper class to allow testing of RM web services which require * authorization Add this class as a filter in the Guice injector for the @@ -159,7 +164,7 @@ protected void configureServlets() { if (setAuthFilter) { filter("/*").through(TestRMCustomAuthFilter.class); } - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -274,13 +279,6 @@ public void setUp() throws Exception { } public TestRMWebServicesReservation(int run, boolean recurrence) { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .clientConfig(new DefaultClientConfig(JAXBContextResolver.class)) - .contextPath("jersey-guice-filter").servletPath("/").build()); - enableRecurrence = recurrence; switch (run) { case 0: @@ -307,21 +305,19 @@ private boolean isAuthenticationEnabled() { return setAuthFilter; } - private WebResource constructWebResource(WebResource r, String... paths) { - WebResource rt = r; + private WebTarget constructWebResource(WebTarget target, String... paths) { for (String path : paths) { - rt = rt.path(path); + target = target.path(path); } if (isAuthenticationEnabled()) { - rt = rt.queryParam("user.name", webserviceUserName); + target = target.queryParam("user.name", webserviceUserName); } - return rt; + return target; } - private WebResource constructWebResource(String... paths) { - WebResource r = resource(); - WebResource ws = r.path("ws").path("v1").path("cluster"); - return this.constructWebResource(ws, paths); + private WebTarget constructWebResource(String... paths) { + WebTarget target = target().path("ws").path("v1").path("cluster"); + return this.constructWebResource(target, paths); } @After @@ -339,7 +335,7 @@ public void testSubmitReservation() throws Exception { setupCluster(100); ReservationId rid = getReservationIdTestHelper(1); - ClientResponse response = reservationSubmissionTestHelper( + Response response = reservationSubmissionTestHelper( "reservation/submit", MediaType.APPLICATION_JSON, rid); if (this.isAuthenticationEnabled()) { assertTrue(isHttpSuccessResponse(response)); @@ -355,7 +351,7 @@ public void testSubmitDuplicateReservation() throws Exception { ReservationId rid = getReservationIdTestHelper(1); long currentTimestamp = clock.getTime() + MINIMUM_RESOURCE_DURATION; - ClientResponse response = reservationSubmissionTestHelper( + Response response = reservationSubmissionTestHelper( "reservation/submit", MediaType.APPLICATION_JSON, currentTimestamp, "", rid); @@ -384,9 +380,9 @@ public void testSubmitDifferentReservationWithSameId() throws Exception { ReservationId rid = getReservationIdTestHelper(1); long currentTimestamp = clock.getTime() + MINIMUM_RESOURCE_DURATION; - ClientResponse response = reservationSubmissionTestHelper( - "reservation/submit", MediaType.APPLICATION_JSON, currentTimestamp, - "res1", rid); + Response response = + reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, + currentTimestamp, "res1", rid); // Make sure that the first submission is successful if (this.isAuthenticationEnabled()) { @@ -414,8 +410,8 @@ public void testFailedSubmitReservation() throws Exception { setupCluster(1); ReservationId rid = getReservationIdTestHelper(1); - ClientResponse response = reservationSubmissionTestHelper( - "reservation/submit", MediaType.APPLICATION_JSON, rid); + Response response = + reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, rid); assertTrue(!isHttpSuccessResponse(response)); @@ -428,8 +424,8 @@ public void testUpdateReservation() throws JSONException, Exception { setupCluster(100); ReservationId rid = getReservationIdTestHelper(1); - ClientResponse response = reservationSubmissionTestHelper( - "reservation/submit", MediaType.APPLICATION_JSON, rid); + Response response = + reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, rid); if (this.isAuthenticationEnabled()) { assertTrue(isHttpSuccessResponse(response)); } @@ -455,14 +451,13 @@ public void testTimeIntervalRequestListReservation() throws Exception { MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) - .queryParam("start-time", Long.toString((long) (time * 0.9))) - .queryParam("end-time", Long.toString(time + (long) (0.9 * - MINIMUM_RESOURCE_DURATION))) - .queryParam("include-resource-allocations", "true") - .queryParam("queue", DEFAULT_QUEUE); + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) + .queryParam("start-time", Long.toString((long) (time * 0.9))) + .queryParam("end-time", Long.toString(time + (long) (0.9 * MINIMUM_RESOURCE_DURATION))) + .queryParam("include-resource-allocations", "true") + .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -501,13 +496,13 @@ public void testSameTimeIntervalRequestListReservation() throws Exception { "res_2", id2); String timeParam = Long.toString(time + MINIMUM_RESOURCE_DURATION / 2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) - .queryParam("start-time", timeParam) - .queryParam("end-time", timeParam) - .queryParam("include-resource-allocations", "true") - .queryParam("queue", DEFAULT_QUEUE); + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) + .queryParam("start-time", timeParam) + .queryParam("end-time", timeParam) + .queryParam("include-resource-allocations", "true") + .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -540,14 +535,13 @@ public void testInvalidTimeIntervalRequestListReservation() throws MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2); - WebResource resource; - resource = constructWebResource(LIST_RESERVATION_PATH) - .queryParam("start-time", "-100") - .queryParam("end-time", "-100") - .queryParam("include-resource-allocations", "true") - .queryParam("queue", DEFAULT_QUEUE); + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) + .queryParam("start-time", "-100") + .queryParam("end-time", "-100") + .queryParam("include-resource-allocations", "true") + .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -579,14 +573,14 @@ public void testInvalidEndTimeRequestListReservation() throws Exception { MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("start-time", Long.toString((long) (time + MINIMUM_RESOURCE_DURATION * 1.3))) .queryParam("end-time", "-1") .queryParam("include-resource-allocations", "true") .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -628,13 +622,13 @@ public void testEmptyEndTimeRequestListReservation() throws Exception { MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("start-time", new Long((long) (time + MINIMUM_RESOURCE_DURATION * 1.3)).toString()) .queryParam("include-resource-allocations", "true") .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -676,14 +670,14 @@ public void testInvalidStartTimeRequestListReservation() throws Exception { MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("start-time", "-1") .queryParam("end-time", new Long((long)(time + MINIMUM_RESOURCE_DURATION * 0.9)).toString()) .queryParam("include-resource-allocations", "true") .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -718,13 +712,13 @@ public void testEmptyStartTimeRequestListReservation() throws Exception { MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("end-time", new Long((long)(time + MINIMUM_RESOURCE_DURATION * 0.9)).toString()) .queryParam("include-resource-allocations", "true") .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -755,10 +749,10 @@ public void testQueueOnlyRequestListReservation() throws Exception { reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -785,9 +779,9 @@ public void testEmptyQueueRequestListReservation() throws Exception { reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH); + WebTarget target = constructWebResource(LIST_RESERVATION_PATH); - testListReservationHelper(resource, Status.BAD_REQUEST); + testListReservationHelper(target, Response.Status.BAD_REQUEST); rm.stop(); } @@ -805,10 +799,10 @@ public void testNonExistentQueueRequestListReservation() throws Exception { reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("queue", DEFAULT_QUEUE + "_invalid"); - testListReservationHelper(resource, Status.BAD_REQUEST); + testListReservationHelper(target, Response.Status.BAD_REQUEST); rm.stop(); } @@ -828,15 +822,15 @@ public void testReservationIdRequestListReservation() throws Exception { reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_2", id2); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("include-resource-allocations", "true") .queryParam("queue", DEFAULT_QUEUE); if (id1 != null) { - resource = resource.queryParam("reservation-id", id1.toString()); + target = target.queryParam("reservation-id", id1.toString()); } - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -863,15 +857,15 @@ public void testInvalidReservationIdRequestListReservation() throws reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_1", id1); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("queue", DEFAULT_QUEUE); if (id1 != null) { - resource = resource.queryParam("reservation-id", + target = target.queryParam("reservation-id", "invalid" + id1.toString()); } - JSONObject response = testListReservationHelper(resource, Status.NOT_FOUND); + JSONObject response = testListReservationHelper(target, Response.Status.NOT_FOUND); rm.stop(); } @@ -885,15 +879,15 @@ public void testIncludeResourceAllocations() throws Exception { reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_1", id1); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("include-resource-allocations", "true") .queryParam("queue", DEFAULT_QUEUE); if (id1 != null) { - resource = resource.queryParam("reservation-id", id1.toString()); + target = target.queryParam("reservation-id", id1.toString()); } - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -921,15 +915,15 @@ public void testExcludeResourceAllocations() throws Exception { reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, clock.getTime(), "res_1", id1); - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("include-resource-allocations", "false") .queryParam("queue", DEFAULT_QUEUE); if (id1 != null) { - resource = resource.queryParam("reservation-id", id1.toString()); + target = target.queryParam("reservation-id", id1.toString()); } - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (!this.isAuthenticationEnabled() && json == null) { return; @@ -979,20 +973,20 @@ public void testDeleteReservation() throws JSONException, Exception { private ReservationId getReservationIdTestHelper(int fallbackReservationId) throws Exception { Thread.sleep(1000); - ClientResponse response = constructWebResource(GET_NEW_RESERVATION_PATH) - .type(MediaType.APPLICATION_JSON) + Response response = constructWebResource(GET_NEW_RESERVATION_PATH) + .request(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) - .post(ClientResponse.class); + .get(Response.class); if (!this.isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); return ReservationId.newInstance(clock.getTime(), fallbackReservationId); } System.out.println("RESPONSE:" + response); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); ReservationId rid = null; @@ -1004,7 +998,7 @@ private ReservationId getReservationIdTestHelper(int fallbackReservationId) return rid; } - private ClientResponse reservationSubmissionTestHelper(String path, + private Response reservationSubmissionTestHelper(String path, String media, ReservationId reservationId) throws Exception { long arrival = clock.getTime() + MINIMUM_RESOURCE_DURATION; @@ -1012,7 +1006,7 @@ private ClientResponse reservationSubmissionTestHelper(String path, reservationId); } - private ClientResponse reservationSubmissionTestHelper(String path, + private Response reservationSubmissionTestHelper(String path, String media, Long arrival, String reservationName, ReservationId reservationId) throws Exception { String reservationJson = loadJsonFile("submit-reservation.json"); @@ -1030,23 +1024,18 @@ private ClientResponse reservationSubmissionTestHelper(String path, return submitAndVerifyReservation(path, media, reservationJsonRequest); } - private ClientResponse submitAndVerifyReservation(String path, String media, + private Response submitAndVerifyReservation(String path, String media, String reservationJson) throws Exception { - JSONJAXBContext jc = - new JSONJAXBContext(JSONConfiguration.mapped() - .build(), ReservationSubmissionRequestInfo.class); - JSONUnmarshaller unmarshaller = jc.createJSONUnmarshaller(); - ReservationSubmissionRequestInfo rsci = - unmarshaller.unmarshalFromJSON(new StringReader(reservationJson), - ReservationSubmissionRequestInfo.class); + ReservationSubmissionRequestInfo rsci = reader.readValue(reservationJson); Thread.sleep(1000); - ClientResponse response = - constructWebResource(path).entity(rsci, MediaType.APPLICATION_JSON) - .accept(media).post(ClientResponse.class); + Response response = constructWebResource(path) + .request(MediaType.APPLICATION_JSON) + .accept(media) + .post(Entity.json(rsci), Response.class); if (!this.isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); } return response; @@ -1057,13 +1046,7 @@ private void updateReservationTestHelper(String path, String reservationJson = loadJsonFile("update-reservation.json"); - JSONJAXBContext jc = - new JSONJAXBContext(JSONConfiguration.mapped() - .build(), ReservationUpdateRequestInfo.class); - JSONUnmarshaller unmarshaller = jc.createJSONUnmarshaller(); - ReservationUpdateRequestInfo rsci = - unmarshaller.unmarshalFromJSON(new StringReader(reservationJson), - ReservationUpdateRequestInfo.class); + ReservationUpdateRequestInfo rsci = reader.readValue(reservationJson); if (this.isAuthenticationEnabled()) { // only works when previous submit worked if(rsci.getReservationId() == null) { @@ -1073,19 +1056,20 @@ private void updateReservationTestHelper(String path, } Thread.sleep(1000); - ClientResponse response = - constructWebResource(path).entity(rsci, MediaType.APPLICATION_JSON) - .accept(media).post(ClientResponse.class); + Response response = constructWebResource(path) + .request(MediaType.APPLICATION_JSON) + .accept(media) + .post(Entity.json(rsci), Response.class); if (!this.isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); return; } System.out.println("RESPONSE:" + response); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response.getMediaType().toString()); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); } @@ -1107,13 +1091,7 @@ private void testDeleteReservationHelper(String path, String reservationJson = loadJsonFile("delete-reservation.json"); - JSONJAXBContext jc = - new JSONJAXBContext(JSONConfiguration.mapped() - .build(), ReservationDeleteRequestInfo.class); - JSONUnmarshaller unmarshaller = jc.createJSONUnmarshaller(); - ReservationDeleteRequestInfo rsci = - unmarshaller.unmarshalFromJSON(new StringReader(reservationJson), - ReservationDeleteRequestInfo.class); + ReservationDeleteRequestInfo rsci = reader.readValue(reservationJson); if (this.isAuthenticationEnabled()) { // only works when previous submit worked if(rsci.getReservationId() == null) { @@ -1123,19 +1101,20 @@ private void testDeleteReservationHelper(String path, } Thread.sleep(1000); - ClientResponse response = - constructWebResource(path).entity(rsci, MediaType.APPLICATION_JSON) - .accept(media).post(ClientResponse.class); + Response response = constructWebResource(path) + .request(MediaType.APPLICATION_JSON) + .accept(media) + .post(Entity.json(rsci), Response.class); if (!this.isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); return; } System.out.println("RESPONSE:" + response); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - assertResponseStatusCode(Status.OK, response.getStatusInfo()); + response.getMediaType().toString()); + assertResponseStatusCode(Response.Status.OK, response.getStatusInfo()); } private void testRDLHelper(JSONObject json) throws JSONException { @@ -1149,33 +1128,32 @@ private void testRDLHelper(JSONObject json) throws JSONException { .getJSONObject(0).getInt("duration")); } - private JSONObject testListReservationHelper(WebResource resource) throws - Exception { - return testListReservationHelper(resource, Status.OK); + private JSONObject testListReservationHelper(WebTarget target) throws Exception { + return testListReservationHelper(target, Response.Status.OK); } - private JSONObject testListReservationHelper(WebResource resource, Status + private JSONObject testListReservationHelper(WebTarget target, Response.Status status) throws Exception { Thread.sleep(1000); - ClientResponse response = resource.get(ClientResponse.class); + Response response = target.request().get(Response.class); if (!this.isAuthenticationEnabled()) { - assertResponseStatusCode(Status.UNAUTHORIZED, response.getStatusInfo()); + assertResponseStatusCode(Response.Status.UNAUTHORIZED, response.getStatusInfo()); return null; } assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); assertResponseStatusCode(status, response.getStatusInfo()); - return response.getEntity(JSONObject.class); + return response.readEntity(JSONObject.class); } private void verifyReservationCount(int count) throws Exception { - WebResource resource = constructWebResource(LIST_RESERVATION_PATH) + WebTarget target = constructWebResource(LIST_RESERVATION_PATH) .queryParam("queue", DEFAULT_QUEUE); - JSONObject json = testListReservationHelper(resource); + JSONObject json = testListReservationHelper(target); if (count == 1) { // If there are any number other than one reservation, this will throw. @@ -1186,7 +1164,7 @@ private void verifyReservationCount(int count) throws Exception { } } - private boolean isHttpSuccessResponse(ClientResponse response) { + private boolean isHttpSuccessResponse(Response response) { return (response.getStatusInfo().getStatusCode() / 100) == 2; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivities.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivities.java index ee472dafcb940..f78f94d93d691 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivities.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivities.java @@ -19,9 +19,6 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.test.GenericTestUtils; @@ -30,8 +27,10 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.activities.ActivityState; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; + +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; + import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.yarn.api.records.ContainerId; import org.apache.hadoop.yarn.api.records.ContainerState; @@ -51,15 +50,21 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.event.NodeUpdateSchedulerEvent; import org.apache.hadoop.yarn.util.resource.Resources; + import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import org.junit.Test; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.function.Predicate; import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.NODE; @@ -89,7 +94,6 @@ import static org.apache.hadoop.yarn.server.resourcemanager.webapp.ActivitiesTestUtils.verifyQueueOrder; import static org.apache.hadoop.yarn.server.resourcemanager.webapp.ActivitiesTestUtils.verifyStateOfAllocations; import static org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesCapacitySched.createMockRM; -import static org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesCapacitySched.createWebAppDescriptor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -98,43 +102,34 @@ /** * Tests for scheduler/app activities. */ -public class TestRMWebServicesSchedulerActivities extends JerseyTestBase { +public class TestRMWebServicesSchedulerActivities extends JerseyTest { private MockRM rm; public TestRMWebServicesSchedulerActivities() { - super(createWebAppDescriptor()); } @Before @Override public void setUp() throws Exception { super.setUp(); - rm = createMockRM(new CapacitySchedulerConfiguration( - new Configuration(false))); + rm = createMockRM(new CapacitySchedulerConfiguration(new Configuration(false))); GuiceServletConfig.setInjector( Guice.createInjector(new TestRMWebServicesCapacitySched.WebServletModule(rm))); } @Test - public void testAssignMultipleContainersPerNodeHeartbeat() - throws Exception { + public void testAssignMultipleContainersPerNodeHeartbeat() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm); am1.allocate(Arrays.asList(ResourceRequest @@ -146,59 +141,49 @@ public void testAssignMultipleContainersPerNodeHeartbeat() 10)), null); //Get JSON - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.1:1234"); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1:1234").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); nm.nodeHeartbeat(true); Thread.sleep(1000); //Get JSON - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1:1234").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); // Collection logic of scheduler activities changed after YARN-9313, // only one allocation should be recorded for all scenarios. verifyNumberOfAllocations(json, 1); - JSONObject allocation = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); - verifyStateOfAllocations(allocation, - FN_ACT_FINAL_ALLOCATION_STATE, "ALLOCATED"); - verifyQueueOrder(allocation, - "root-root.a-root.c-root.b-root.b.b2-root.b.b3-root.b.b1"); + JSONObject allocation = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + verifyStateOfAllocations(allocation, FN_ACT_FINAL_ALLOCATION_STATE, "ALLOCATED"); + verifyQueueOrder(allocation, "root-root.a-root.c-root.b-root.b.b2-root.b.b3-root.b.b1"); } finally { rm.stop(); } } @Test - public void testAssignWithoutAvailableResource() throws Exception { + public void testAssignWithoutAvailabletarget() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 1 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 1 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(1024, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(1024, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm); am1.allocate(Arrays.asList(ResourceRequest @@ -210,35 +195,32 @@ public void testAssignWithoutAvailableResource() throws Exception { 10)), null); //Get JSON - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.1"); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); nm.nodeHeartbeat(true); Thread.sleep(1000); //Get JSON - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1:1234").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); // verify scheduler activities verifyNumberOfAllocations(json, 1); - JSONObject rootObj = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS) - .getJSONObject(FN_SCHEDULER_ACT_ALLOCATIONS_ROOT); - assertTrue(rootObj.optString(FN_ACT_DIAGNOSTIC).startsWith( - ActivityDiagnosticConstant. - INIT_CHECK_SINGLE_NODE_RESOURCE_INSUFFICIENT)); + JSONObject rootObj = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS).getJSONObject( + FN_SCHEDULER_ACT_ALLOCATIONS_ROOT); + assertTrue(rootObj.optString(FN_ACT_DIAGNOSTIC) + .startsWith(ActivityDiagnosticConstant.INIT_CHECK_SINGLE_NODE_RESOURCE_INSUFFICIENT)); } finally { rm.stop(); } @@ -251,25 +233,23 @@ public void testNoNM() throws Exception { try { //Get JSON - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.1:1234"); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1:1234").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); Thread.sleep(1000); //Get JSON - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1:1234").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 0); } finally { @@ -282,19 +262,13 @@ public void testWrongNodeId() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(1024, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(1024, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm); am1.allocate(Arrays.asList(ResourceRequest @@ -306,26 +280,25 @@ public void testWrongNodeId() throws Exception { 10)), null); //Get JSON - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.0"); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.0").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); nm.nodeHeartbeat(true); Thread.sleep(1000); //Get JSON - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.0").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 0); } finally { @@ -338,34 +311,22 @@ public void testReserveNewContainer() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, - rm.getResourceTrackerService()); - MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, - rm.getResourceTrackerService()); + MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, rm.getResourceTrackerService()); + MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, rm.getResourceTrackerService()); nm1.registerNode(); nm2.registerNode(); try { MockRMAppSubmissionData data1 = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data1); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app2") - .withUser("user1") - .withAcls(null) - .withQueue("b2") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app2") + .withUser("user1").withAcls(null).withQueue("b2").withUnmanagedAM(false).build(); RMApp app2 = MockRMAppSubmitter.submit(rm, data); MockAM am2 = MockRM.launchAndRegisterAM(app2, rm, nm2); @@ -374,97 +335,86 @@ public void testReserveNewContainer() throws Exception { 10)), null); // Reserve new container - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.2"); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.2").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); nm2.nodeHeartbeat(true); Thread.sleep(1000); - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.2").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); - JSONObject allocations = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); - verifyQueueOrder(allocations, - "root-root.c-root.a-root.b-root.b.b3-root.b.b1"); - verifyStateOfAllocations(allocations, FN_ACT_FINAL_ALLOCATION_STATE, - "RESERVED"); + JSONObject allocations = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + verifyQueueOrder(allocations, "root-root.c-root.a-root.b-root.b.b3-root.b.b1"); + verifyStateOfAllocations(allocations, FN_ACT_FINAL_ALLOCATION_STATE, "RESERVED"); // Do a node heartbeat again without releasing container from app2 - r = resource(); - params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.2"); - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + r = target(); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.2").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); nm2.nodeHeartbeat(true); Thread.sleep(1000); - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.2").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); - JSONObject allocation = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + JSONObject allocation = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); verifyQueueOrder(allocation, "root.b.b1"); - verifyStateOfAllocations(allocation, FN_ACT_FINAL_ALLOCATION_STATE, - "RESERVED"); + verifyStateOfAllocations(allocation, FN_ACT_FINAL_ALLOCATION_STATE, "RESERVED"); // Finish application 2 CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); - ContainerId containerId = ContainerId.newContainerId( - am2.getApplicationAttemptId(), 1); - cs.completedContainer(cs.getRMContainer(containerId), ContainerStatus - .newInstance(containerId, ContainerState.COMPLETE, "", 0), + ContainerId containerId = ContainerId.newContainerId(am2.getApplicationAttemptId(), 1); + cs.completedContainer(cs.getRMContainer(containerId), + ContainerStatus.newInstance(containerId, ContainerState.COMPLETE, "", 0), RMContainerEventType.FINISHED); // Do a node heartbeat again - r = resource(); - params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.2"); - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + r = target(); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.2").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); nm2.nodeHeartbeat(true); Thread.sleep(1000); - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.2").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); - allocations = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + allocations = getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); verifyQueueOrder(allocations, "root.b.b1"); verifyStateOfAllocations(allocations, FN_ACT_FINAL_ALLOCATION_STATE, "ALLOCATED_FROM_RESERVED"); @@ -478,49 +428,41 @@ public void testActivityJSON() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); //Get JSON - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add("nodeId", "127.0.0.1"); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); nm.nodeHeartbeat(true); Thread.sleep(1000); //Get JSON - response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").queryParams(params).accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .queryParam("nodeId", "127.0.0.1").request(MediaType.APPLICATION_JSON) + .get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); - JSONObject allocation = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); - verifyStateOfAllocations(allocation, FN_ACT_FINAL_ALLOCATION_STATE, - "ALLOCATED"); + JSONObject allocation = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + verifyStateOfAllocations(allocation, FN_ACT_FINAL_ALLOCATION_STATE, "ALLOCATED"); // Increase number of nodes to 6 since request node has been added verifyNumberOfNodes(allocation, 6); @@ -536,26 +478,20 @@ public void testAppActivityJSON() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); //Get JSON - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); ActivitiesTestUtils.requestWebResource(r, params); nm.nodeHeartbeat(true); @@ -566,26 +502,19 @@ public void testAppActivityJSON() throws Exception { //Check app activities verifyNumberOfAllocations(json, 1); - JSONObject allocation = getFirstSubNodeFromJson(json, - FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); - verifyStateOfAllocations(allocation, FN_ACT_ALLOCATION_STATE, - "ALLOCATED"); + JSONObject allocation = getFirstSubNodeFromJson(json, FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); + verifyStateOfAllocations(allocation, FN_ACT_ALLOCATION_STATE, "ALLOCATED"); //Check request allocation - JSONObject requestAllocationObj = - getFirstSubNodeFromJson(allocation, FN_APP_ACT_CHILDREN); - verifyStateOfAllocations(requestAllocationObj, FN_ACT_ALLOCATION_STATE, - "ALLOCATED"); - assertEquals(0, - requestAllocationObj.optInt(FN_ACT_REQUEST_PRIORITY)); - assertEquals(-1, - requestAllocationObj.optLong(FN_ACT_ALLOCATION_REQUEST_ID)); + JSONObject requestAllocationObj = getFirstSubNodeFromJson(allocation, FN_APP_ACT_CHILDREN); + verifyStateOfAllocations(requestAllocationObj, FN_ACT_ALLOCATION_STATE, "ALLOCATED"); + assertEquals(0, requestAllocationObj.optInt(FN_ACT_REQUEST_PRIORITY)); + assertEquals(-1, requestAllocationObj.optLong(FN_ACT_ALLOCATION_REQUEST_ID)); //Check allocation attempts verifyNumberOfAllocationAttempts(requestAllocationObj, 1); List allocationAttempts = getSubNodesFromJson(requestAllocationObj, FN_APP_ACT_CHILDREN); assertEquals(1, allocationAttempts.size()); - verifyStateOfAllocations(allocationAttempts.get(0), - FN_ACT_ALLOCATION_STATE, "ALLOCATED"); + verifyStateOfAllocations(allocationAttempts.get(0), FN_ACT_ALLOCATION_STATE, "ALLOCATED"); assertNotNull(allocationAttempts.get(0).get(FN_ACT_NODE_ID)); } finally { rm.stop(); @@ -593,24 +522,17 @@ public void testAppActivityJSON() throws Exception { } @Test - public void testAppAssignMultipleContainersPerNodeHeartbeat() - throws Exception { + public void testAppAssignMultipleContainersPerNodeHeartbeat() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 24 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(1024, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(1024, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm); am1.allocate(Arrays.asList(ResourceRequest @@ -622,10 +544,10 @@ public void testAppAssignMultipleContainersPerNodeHeartbeat() 10)), null); //Get JSON - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); ActivitiesTestUtils.requestWebResource(r, params); nm.nodeHeartbeat(true); @@ -648,23 +570,17 @@ public void testAppAssignMultipleContainersPerNodeHeartbeat() } @Test - public void testAppAssignWithoutAvailableResource() throws Exception { + public void testAppAssignWithoutAvailabletarget() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm = new MockNM("127.0.0.1:1234", 1 * 1024, - rm.getResourceTrackerService()); + MockNM nm = new MockNM("127.0.0.1:1234", 1 * 1024, rm.getResourceTrackerService()); nm.registerNode(); try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(1024, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(1024, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm); am1.allocate(Arrays.asList(ResourceRequest @@ -676,10 +592,10 @@ public void testAppAssignWithoutAvailableResource() throws Exception { 10)), null); //Get JSON - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); ActivitiesTestUtils.requestWebResource(r, params); nm.nodeHeartbeat(true); @@ -700,20 +616,15 @@ public void testAppNoNM() throws Exception { try { MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(1024, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(1024, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data); //Get JSON - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); ActivitiesTestUtils.requestWebResource(r, params); //Get JSON @@ -729,34 +640,22 @@ public void testAppReserveNewContainer() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, - rm.getResourceTrackerService()); - MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, - rm.getResourceTrackerService()); + MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, rm.getResourceTrackerService()); + MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, rm.getResourceTrackerService()); nm1.registerNode(); nm2.registerNode(); try { MockRMAppSubmissionData data1 = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").withUnmanagedAM(false).build(); RMApp app1 = MockRMAppSubmitter.submit(rm, data1); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); MockRMAppSubmissionData data = - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app2") - .withUser("user1") - .withAcls(null) - .withQueue("b2") - .withUnmanagedAM(false) - .build(); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app2") + .withUser("user1").withAcls(null).withQueue("b2").withUnmanagedAM(false).build(); RMApp app2 = MockRMAppSubmitter.submit(rm, data); MockAM am2 = MockRM.launchAndRegisterAM(app2, rm, nm2); @@ -765,10 +664,10 @@ public void testAppReserveNewContainer() throws Exception { 10)), null); // Reserve new container - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); ActivitiesTestUtils.requestWebResource(r, params); nm2.nodeHeartbeat(true); @@ -786,10 +685,9 @@ public void testAppReserveNewContainer() throws Exception { // Finish application 2 CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); - ContainerId containerId = ContainerId.newContainerId( - am2.getApplicationAttemptId(), 1); - cs.completedContainer(cs.getRMContainer(containerId), ContainerStatus - .newInstance(containerId, ContainerState.COMPLETE, "", 0), + ContainerId containerId = ContainerId.newContainerId(am2.getApplicationAttemptId(), 1); + cs.completedContainer(cs.getRMContainer(containerId), + ContainerStatus.newInstance(containerId, ContainerState.COMPLETE, "", 0), RMContainerEventType.FINISHED); // Do a node heartbeat again @@ -803,7 +701,7 @@ public void testAppReserveNewContainer() throws Exception { } } - @Test (timeout=30000) + @Test(timeout = 30000) public void testInsufficientResourceDiagnostic() throws Exception { rm.start(); CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); @@ -813,44 +711,36 @@ public void testInsufficientResourceDiagnostic() throws Exception { try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource(); + WebTarget r = target(); - ClientResponse response = - r.path("ws").path("v1").path("cluster").path("scheduler/activities") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("waiting for next allocation", - getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT) - .optString(FN_ACT_DIAGNOSTIC)); + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT).optString(FN_ACT_DIAGNOSTIC)); - am1.allocate(Arrays.asList(ResourceRequest - .newInstance(Priority.UNDEFINED, "*", - Resources.createResource(5 * 1024), 1)), null); + am1.allocate(Arrays.asList( + ResourceRequest.newInstance(Priority.UNDEFINED, "*", Resources.createResource(5 * 1024), + 1)), null); //will reserve a container on nm1 - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); - response = - r.path("ws").path("v1").path("cluster").path("scheduler/activities") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); - JSONObject allocationObj = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + JSONObject allocationObj = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); // check diagnostics Predicate findReqPred = (obj) -> obj.optString(FN_SCHEDULER_ACT_NAME).equals("request_-1_-1"); @@ -867,67 +757,56 @@ public void testInsufficientResourceDiagnostic() throws Exception { } } - @Test (timeout=30000) + @Test(timeout = 30000) public void testPlacementConstraintDiagnostic() throws Exception { rm.start(); - CapacityScheduler cs = (CapacityScheduler)rm.getResourceScheduler(); + CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); MockNM nm1 = rm.registerNode("127.0.0.1:1234", 4 * 1024); try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); // init scheduling request - PlacementConstraint pcExpression = PlacementConstraints - .build(PlacementConstraints.targetIn(NODE, allocationTag("foo"))); + PlacementConstraint pcExpression = + PlacementConstraints.build(PlacementConstraints.targetIn(NODE, allocationTag("foo"))); List schedulingRequests = new ArrayList<>(); - schedulingRequests.add(ActivitiesTestUtils - .schedulingRequest(5, 1, 1, 1, 512, pcExpression, "foo")); + schedulingRequests.add( + ActivitiesTestUtils.schedulingRequest(5, 1, 1, 1, 512, pcExpression, "foo")); AllocateRequest allocateReq = - AllocateRequest.newBuilder().schedulingRequests(schedulingRequests) - .build(); + AllocateRequest.newBuilder().schedulingRequests(schedulingRequests).build(); am1.allocate(allocateReq); - WebResource r = resource(); - ClientResponse response = - r.path("ws").path("v1").path("cluster").path("scheduler/activities") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("waiting for next allocation", - getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT) - .optString(FN_ACT_DIAGNOSTIC)); + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT).optString(FN_ACT_DIAGNOSTIC)); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); - response = - r.path("ws").path("v1").path("cluster").path("scheduler/activities") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); - JSONObject allocationObj = getFirstSubNodeFromJson(json, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); + JSONObject allocationObj = + getFirstSubNodeFromJson(json, FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS); // check diagnostics Predicate findReqPred = (obj) -> obj.optString(FN_SCHEDULER_ACT_NAME).equals("request_1_1"); - List reqObjs = - ActivitiesTestUtils.findInAllocations(allocationObj, findReqPred); + List reqObjs = ActivitiesTestUtils.findInAllocations(allocationObj, findReqPred); assertEquals(1, reqObjs.size()); - JSONObject reqChild = - getFirstSubNodeFromJson(reqObjs.get(0), FN_SCHEDULER_ACT_CHILDREN); + JSONObject reqChild = getFirstSubNodeFromJson(reqObjs.get(0), FN_SCHEDULER_ACT_CHILDREN); assertTrue(reqChild.getString(FN_ACT_DIAGNOSTIC) .contains(UNMATCHED_PARTITION_OR_PC_DIAGNOSTIC_PREFIX)); } finally { @@ -935,7 +814,7 @@ public void testPlacementConstraintDiagnostic() throws Exception { } } - @Test (timeout=30000) + @Test(timeout = 30000) public void testAppInsufficientResourceDiagnostic() throws Exception { rm.start(); CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); @@ -945,29 +824,24 @@ public void testAppInsufficientResourceDiagnostic() throws Exception { try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); // am1 asks for 1 * 5GB container - am1.allocate(Arrays.asList(ResourceRequest - .newInstance(Priority.UNDEFINED, "*", - Resources.createResource(5 * 1024), 1)), null); + am1.allocate(Arrays.asList( + ResourceRequest.newInstance(Priority.UNDEFINED, "*", Resources.createResource(5 * 1024), + 1)), null); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); json = ActivitiesTestUtils.requestWebResource(r, params); verifyNumberOfAllocations(json, 1); @@ -987,7 +861,7 @@ public void testAppInsufficientResourceDiagnostic() throws Exception { } } - @Test(timeout=30000) + @Test(timeout = 30000) public void testAppPlacementConstraintDiagnostic() throws Exception { rm.start(); CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); @@ -997,47 +871,38 @@ public void testAppPlacementConstraintDiagnostic() throws Exception { try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); // am1 asks for 1 * 5GB container with PC expression: in,node,foo - PlacementConstraint pcExpression = PlacementConstraints - .build(PlacementConstraints.targetIn(NODE, allocationTag("foo"))); + PlacementConstraint pcExpression = + PlacementConstraints.build(PlacementConstraints.targetIn(NODE, allocationTag("foo"))); List schedulingRequests = new ArrayList<>(); - schedulingRequests.add(ActivitiesTestUtils - .schedulingRequest(5, 1, 1, 1, 512, pcExpression, "foo")); + schedulingRequests.add( + ActivitiesTestUtils.schedulingRequest(5, 1, 1, 1, 512, pcExpression, "foo")); AllocateRequest allocateReq = - AllocateRequest.newBuilder().schedulingRequests(schedulingRequests) - .build(); + AllocateRequest.newBuilder().schedulingRequests(schedulingRequests).build(); am1.allocate(allocateReq); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); json = ActivitiesTestUtils.requestWebResource(r, params); verifyNumberOfAllocations(json, 1); - JSONObject allocationObj = getFirstSubNodeFromJson(json, - FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); - JSONObject requestAllocationObj = - getFirstSubNodeFromJson(allocationObj, FN_APP_ACT_CHILDREN); + JSONObject allocationObj = getFirstSubNodeFromJson(json, FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); + JSONObject requestAllocationObj = getFirstSubNodeFromJson(allocationObj, FN_APP_ACT_CHILDREN); verifyNumberOfAllocationAttempts(requestAllocationObj, 1); - JSONObject allocationAttemptObj = getFirstSubNodeFromJson( - requestAllocationObj, FN_APP_ACT_CHILDREN); - verifyStateOfAllocations(allocationAttemptObj, FN_ACT_ALLOCATION_STATE, - "SKIPPED"); + JSONObject allocationAttemptObj = + getFirstSubNodeFromJson(requestAllocationObj, FN_APP_ACT_CHILDREN); + verifyStateOfAllocations(allocationAttemptObj, FN_ACT_ALLOCATION_STATE, "SKIPPED"); assertTrue(allocationAttemptObj.optString(FN_ACT_DIAGNOSTIC) .contains(UNMATCHED_PARTITION_OR_PC_DIAGNOSTIC_PREFIX)); } finally { @@ -1045,9 +910,8 @@ public void testAppPlacementConstraintDiagnostic() throws Exception { } } - @Test (timeout=30000) - public void testAppFilterByRequestPrioritiesAndAllocationRequestIds() - throws Exception { + @Test(timeout = 30000) + public void testAppFilterByRequestPrioritiesAndAllocationRequestIds() throws Exception { rm.start(); CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); @@ -1055,18 +919,14 @@ public void testAppFilterByRequestPrioritiesAndAllocationRequestIds() try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); @@ -1074,107 +934,103 @@ public void testAppFilterByRequestPrioritiesAndAllocationRequestIds() // am1 asks for 1 * 1GB container with requestPriority=-1 // and allocationRequestId=1 am1.allocate(Arrays.asList( - ResourceRequest.newBuilder().priority(Priority.UNDEFINED) - .allocationRequestId(1).resourceName("*") - .capability(Resources.createResource(1 * 1024)).numContainers(1) + ResourceRequest.newBuilder().priority(Priority.UNDEFINED).allocationRequestId(1) + .resourceName("*").capability(Resources.createResource(1 * 1024)).numContainers(1) .build()), null); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); // am1 asks for 1 * 1GB container with requestPriority=-1 // and allocationRequestId=2 am1.allocate(Arrays.asList( - ResourceRequest.newBuilder().priority(Priority.UNDEFINED) - .allocationRequestId(2).resourceName("*") - .capability(Resources.createResource(1 * 1024)).numContainers(1) + ResourceRequest.newBuilder().priority(Priority.UNDEFINED).allocationRequestId(2) + .resourceName("*").capability(Resources.createResource(1 * 1024)).numContainers(1) .build()), null); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); // am1 asks for 1 * 1GB container with requestPriority=0 // and allocationRequestId=1 am1.allocate(Arrays.asList( - ResourceRequest.newBuilder().priority(Priority.newInstance(0)) - .allocationRequestId(1).resourceName("*") - .capability(Resources.createResource(1 * 1024)).numContainers(1) + ResourceRequest.newBuilder().priority(Priority.newInstance(0)).allocationRequestId(1) + .resourceName("*").capability(Resources.createResource(1 * 1024)).numContainers(1) .build()), null); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); // am1 asks for 1 * 1GB container with requestPriority=0 // and allocationRequestId=3 am1.allocate(Arrays.asList( - ResourceRequest.newBuilder().priority(Priority.newInstance(0)) - .allocationRequestId(3).resourceName("*") - .capability(Resources.createResource(1 * 1024)).numContainers(1) + ResourceRequest.newBuilder().priority(Priority.newInstance(0)).allocationRequestId(3) + .resourceName("*").capability(Resources.createResource(1 * 1024)).numContainers(1) .build()), null); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); // query app activities with requestPriorities={0,-1} - MultivaluedMapImpl filterParams1 = new MultivaluedMapImpl(params); + MultivaluedMap filterParams1 = + new MultivaluedHashMap<>((Map) params); filterParams1.add(RMWSConsts.REQUEST_PRIORITIES, "0,-1"); json = ActivitiesTestUtils.requestWebResource(r, filterParams1); verifyNumberOfAllocations(json, 4); // query app activities with requestPriorities=-1 - MultivaluedMapImpl filterParams2 = new MultivaluedMapImpl(params); + MultivaluedMap filterParams2 = + new MultivaluedHashMap<>((Map) params); filterParams2.add(RMWSConsts.REQUEST_PRIORITIES, "-1"); json = ActivitiesTestUtils.requestWebResource(r, filterParams2); verifyNumberOfAllocations(json, 2); - JSONArray allocations = - json.getJSONObject(FN_APP_ACT_ROOT).getJSONArray(FN_ACT_ALLOCATIONS); - for (int i=0; i filterParams3 = + new MultivaluedHashMap<>((Map) params); filterParams3.add(RMWSConsts.ALLOCATION_REQUEST_IDS, "1"); json = ActivitiesTestUtils.requestWebResource(r, filterParams3); verifyNumberOfAllocations(json, 2); - allocations = - json.getJSONObject(FN_APP_ACT_ROOT).getJSONArray(FN_ACT_ALLOCATIONS); + allocations = json.getJSONObject(FN_APP_ACT_ROOT).getJSONArray(FN_ACT_ALLOCATIONS); for (int i = 0; i < allocations.length(); i++) { - assertEquals("1", getFirstSubNodeFromJson(allocations.getJSONObject(i), - FN_APP_ACT_CHILDREN).optString(FN_ACT_ALLOCATION_REQUEST_ID)); + assertEquals("1", + getFirstSubNodeFromJson(allocations.getJSONObject(i), FN_APP_ACT_CHILDREN).optString( + FN_ACT_ALLOCATION_REQUEST_ID)); } // query app activities with requestPriorities=0 and allocationRequestId=1 - MultivaluedMapImpl filterParams4 = new MultivaluedMapImpl(params); + MultivaluedMap filterParams4 = + new MultivaluedHashMap<>((Map) params); filterParams4.add(RMWSConsts.REQUEST_PRIORITIES, "0"); filterParams4.add(RMWSConsts.ALLOCATION_REQUEST_IDS, "1"); json = ActivitiesTestUtils.requestWebResource(r, filterParams4); verifyNumberOfAllocations(json, 1); - JSONObject allocation = getFirstSubNodeFromJson(json, - FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); - JSONObject request = - getFirstSubNodeFromJson(allocation, FN_APP_ACT_CHILDREN); + JSONObject allocation = getFirstSubNodeFromJson(json, FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); + JSONObject request = getFirstSubNodeFromJson(allocation, FN_APP_ACT_CHILDREN); assertEquals("0", request.optString(FN_ACT_REQUEST_PRIORITY)); assertEquals("1", request.optString(FN_ACT_ALLOCATION_REQUEST_ID)); // query app activities with requestPriorities=-1 // and allocationRequestId={1,2} - MultivaluedMapImpl filterParams5 = new MultivaluedMapImpl(params); + MultivaluedMap filterParams5 = + new MultivaluedHashMap<>((Map) params); filterParams5.add(RMWSConsts.REQUEST_PRIORITIES, "-1"); filterParams5.add(RMWSConsts.ALLOCATION_REQUEST_IDS, "1,2"); json = ActivitiesTestUtils.requestWebResource(r, filterParams5); verifyNumberOfAllocations(json, 2); - allocations = - json.getJSONObject(FN_APP_ACT_ROOT).getJSONArray(FN_ACT_ALLOCATIONS); + allocations = json.getJSONObject(FN_APP_ACT_ROOT).getJSONArray(FN_ACT_ALLOCATIONS); for (int i = 0; i < allocations.length(); i++) { - assertEquals("-1", getFirstSubNodeFromJson(allocations.getJSONObject(i), - FN_APP_ACT_CHILDREN).optString(FN_ACT_REQUEST_PRIORITY)); + assertEquals("-1", + getFirstSubNodeFromJson(allocations.getJSONObject(i), FN_APP_ACT_CHILDREN).optString( + FN_ACT_REQUEST_PRIORITY)); } // query app activities with requestPriorities=-1 // and allocationRequestId={-1,1} - MultivaluedMapImpl filterParams6 = new MultivaluedMapImpl(params); + MultivaluedMap filterParams6 = + new MultivaluedHashMap<>((Map) params); filterParams6.add(RMWSConsts.REQUEST_PRIORITIES, "-1"); filterParams6.add(RMWSConsts.ALLOCATION_REQUEST_IDS, "-1,1"); json = ActivitiesTestUtils.requestWebResource(r, filterParams6); @@ -1192,18 +1048,14 @@ public void testAppLimit() throws Exception { MockNM nm2 = rm.registerNode("127.0.0.2:1234", 8 * 1024); try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); @@ -1211,12 +1063,9 @@ public void testAppLimit() throws Exception { // am1 asks for 1 * 5GB container am1.allocate("*", 5120, 1, new ArrayList<>()); // trigger scheduling triple, there will be 3 app activities in cache - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); // query all app activities without limit json = ActivitiesTestUtils.requestWebResource(r, params); @@ -1266,27 +1115,23 @@ public void testAppActions() throws Exception { MockNM nm1 = rm.registerNode("127.0.0.1:1234", 8 * 1024); try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(512, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(512, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); // am1 asks for 10 * 512MB container am1.allocate("*", 512, 10, new ArrayList<>()); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); params.add("maxTime", 1); //only last for 1 second // testing invalid action params.add(RMWSConsts.ACTIONS, "get,invalid-action"); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); - assertTrue(json.getJSONObject(FN_APP_ACT_ROOT) - .getString(FN_ACT_DIAGNOSTIC).startsWith("Got invalid action")); + assertTrue(json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC) + .startsWith("Got invalid action")); /* * testing get action @@ -1297,8 +1142,7 @@ public void testAppActions() throws Exception { json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); // app activities won't be recorded params.putSingle(RMWSConsts.ACTIONS, "get"); @@ -1307,8 +1151,7 @@ public void testAppActions() throws Exception { json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); /* * testing update action @@ -1319,8 +1162,7 @@ public void testAppActions() throws Exception { json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); Thread.sleep(1000); // app activities should be recorded @@ -1329,8 +1171,7 @@ public void testAppActions() throws Exception { verifyNumberOfAllocations(json, 1); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); Thread.sleep(1000); /* @@ -1342,8 +1183,7 @@ public void testAppActions() throws Exception { verifyNumberOfAllocations(json, 1); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); Thread.sleep(1000); // more app activities should be recorded @@ -1351,8 +1191,7 @@ public void testAppActions() throws Exception { verifyNumberOfAllocations(json, 2); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); Thread.sleep(1000); // more app activities should be recorded @@ -1363,7 +1202,7 @@ public void testAppActions() throws Exception { } } - @Test(timeout=30000) + @Test(timeout = 30000) public void testAppSummary() throws Exception { rm.start(); CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); @@ -1374,33 +1213,25 @@ public void testAppSummary() throws Exception { try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(5120, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); - - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + MockRMAppSubmissionData.Builder.createWithMemory(5120, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); + + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); // am1 asks for 1 * 5GB container - am1.allocate(Arrays.asList(ResourceRequest - .newInstance(Priority.newInstance(0), "*", - Resources.createResource(5 * 1024), 1)), null); + am1.allocate(Arrays.asList(ResourceRequest.newInstance(Priority.newInstance(0), "*", + Resources.createResource(5 * 1024), 1)), null); // trigger scheduling - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm2.getNodeId()))); - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm3.getNodeId()))); - cs.handle(new NodeUpdateSchedulerEvent( - rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm2.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm3.getNodeId()))); + cs.handle(new NodeUpdateSchedulerEvent(rm.getRMContext().getRMNodes().get(nm1.getNodeId()))); params.add(RMWSConsts.SUMMARIZE, "true"); params.add(RMWSConsts.GROUP_BY, RMWSConsts.ActivitiesGroupBy.DIAGNOSTIC); @@ -1408,16 +1239,13 @@ public void testAppSummary() throws Exception { // verify that response contains an allocation summary for all nodes verifyNumberOfAllocations(json, 1); - JSONObject allocation = getFirstSubNodeFromJson(json, - FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); - JSONObject reqestAllocation = - getFirstSubNodeFromJson(allocation, FN_APP_ACT_CHILDREN); + JSONObject allocation = getFirstSubNodeFromJson(json, FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS); + JSONObject reqestAllocation = getFirstSubNodeFromJson(allocation, FN_APP_ACT_CHILDREN); JSONArray attempts = reqestAllocation.getJSONArray(FN_APP_ACT_CHILDREN); assertEquals(2, attempts.length()); for (int i = 0; i < attempts.length(); i++) { JSONObject attempt = attempts.getJSONObject(i); - if (attempt.getString(FN_ACT_ALLOCATION_STATE) - .equals(ActivityState.SKIPPED.name())) { + if (attempt.getString(FN_ACT_ALLOCATION_STATE).equals(ActivityState.SKIPPED.name())) { assertEquals(2, attempt.getJSONArray(FN_ACT_NODE_IDS).length()); } else if (attempt.getString(FN_ACT_ALLOCATION_STATE) .equals(ActivityState.RESERVED.name())) { @@ -1436,64 +1264,48 @@ public void testNodeSkippedBecauseOfRelaxLocality() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, - rm.getResourceTrackerService()); - MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, - rm.getResourceTrackerService()); + MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, rm.getResourceTrackerService()); + MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, rm.getResourceTrackerService()); nm1.registerNode(); nm2.registerNode(); try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("b1") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("b1").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); am1.allocate(Arrays.asList( - ResourceRequest.newBuilder().priority(Priority.UNDEFINED) - .resourceName("127.0.0.2") - .capability(Resources.createResource(1024)).numContainers(1) - .build(), - ResourceRequest.newBuilder().priority(Priority.UNDEFINED) - .resourceName("/default-rack") - .capability(Resources.createResource(1024)).numContainers(1) - .relaxLocality(false) - .build(), - ResourceRequest.newBuilder().priority(Priority.UNDEFINED) - .resourceName("*") - .capability(Resources.createResource(1024)).numContainers(1) - .relaxLocality(false) + ResourceRequest.newBuilder().priority(Priority.UNDEFINED).resourceName("127.0.0.2") + .capability(Resources.createResource(1024)).numContainers(1).build(), + ResourceRequest.newBuilder().priority(Priority.UNDEFINED).resourceName("/default-rack") + .capability(Resources.createResource(1024)).numContainers(1).relaxLocality(false) + .build(), ResourceRequest.newBuilder().priority(Priority.UNDEFINED).resourceName("*") + .capability(Resources.createResource(1024)).numContainers(1).relaxLocality(false) .build()), null); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); ActivitiesTestUtils.requestWebResource(r, null); - WebResource sr = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(RMWSConsts.SCHEDULER_ACTIVITIES); + WebTarget sr = + target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path(RMWSConsts.SCHEDULER_ACTIVITIES); ActivitiesTestUtils.requestWebResource(sr, null); nm1.nodeHeartbeat(true); Thread.sleep(1000); - JSONObject appActivitiesJson = - ActivitiesTestUtils.requestWebResource(r, null); - JSONObject schedulerActivitiesJson = - ActivitiesTestUtils.requestWebResource(sr, null); + JSONObject appActivitiesJson = ActivitiesTestUtils.requestWebResource(r, null); + JSONObject schedulerActivitiesJson = ActivitiesTestUtils.requestWebResource(sr, null); // verify app activities verifyNumberOfAllocations(appActivitiesJson, 1); - List allocationAttempts = ActivitiesTestUtils - .getSubNodesFromJson(appActivitiesJson, FN_APP_ACT_ROOT, + List allocationAttempts = + ActivitiesTestUtils.getSubNodesFromJson(appActivitiesJson, FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS, FN_APP_ACT_CHILDREN, FN_APP_ACT_CHILDREN); assertEquals(1, allocationAttempts.size()); - assertEquals( - ActivityDiagnosticConstant.NODE_SKIPPED_BECAUSE_OF_RELAX_LOCALITY, + assertEquals(ActivityDiagnosticConstant.NODE_SKIPPED_BECAUSE_OF_RELAX_LOCALITY, allocationAttempts.get(0).optString(FN_ACT_DIAGNOSTIC)); /* @@ -1504,19 +1316,14 @@ public void testNodeSkippedBecauseOfRelaxLocality() throws Exception { Predicate findA1AQueuePred = (obj) -> obj.optString(FN_SCHEDULER_ACT_NAME).equals("request_-1_-1"); List reqObjs = ActivitiesTestUtils.findInAllocations( - getFirstSubNodeFromJson(schedulerActivitiesJson, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS), - findA1AQueuePred); + getFirstSubNodeFromJson(schedulerActivitiesJson, FN_SCHEDULER_ACT_ROOT, + FN_ACT_ALLOCATIONS), findA1AQueuePred); assertEquals(1, reqObjs.size()); - assertEquals(ActivityState.SKIPPED.name(), - reqObjs.get(0).optString(FN_ACT_ALLOCATION_STATE)); + assertEquals(ActivityState.SKIPPED.name(), reqObjs.get(0).optString(FN_ACT_ALLOCATION_STATE)); // verify node activity - JSONObject nodeObj = - getFirstSubNodeFromJson(reqObjs.get(0), FN_SCHEDULER_ACT_CHILDREN); - assertEquals(nm1.getNodeId().toString(), - nodeObj.optString(FN_ACT_NODE_ID)); - assertEquals( - ActivityDiagnosticConstant.NODE_SKIPPED_BECAUSE_OF_RELAX_LOCALITY, + JSONObject nodeObj = getFirstSubNodeFromJson(reqObjs.get(0), FN_SCHEDULER_ACT_CHILDREN); + assertEquals(nm1.getNodeId().toString(), nodeObj.optString(FN_ACT_NODE_ID)); + assertEquals(ActivityDiagnosticConstant.NODE_SKIPPED_BECAUSE_OF_RELAX_LOCALITY, nodeObj.optString(FN_ACT_DIAGNOSTIC)); } finally { rm.stop(); @@ -1528,50 +1335,41 @@ public void testQueueSkippedBecauseOfHeadroom() throws Exception { //Start RM so that it accepts app submissions rm.start(); - MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, - rm.getResourceTrackerService()); - MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, - rm.getResourceTrackerService()); + MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, rm.getResourceTrackerService()); + MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, rm.getResourceTrackerService()); nm1.registerNode(); nm2.registerNode(); try { RMApp app1 = MockRMAppSubmitter.submit(rm, - MockRMAppSubmissionData.Builder.createWithMemory(10, rm) - .withAppName("app1") - .withUser("user1") - .withAcls(null) - .withQueue("a1a") - .build()); + MockRMAppSubmissionData.Builder.createWithMemory(10, rm).withAppName("app1") + .withUser("user1").withAcls(null).withQueue("a1a").build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); am1.allocate(Arrays.asList( - ResourceRequest.newBuilder().priority(Priority.UNDEFINED) - .resourceName("*").capability(Resources.createResource(3072)) - .numContainers(1).relaxLocality(false).build()), null); + ResourceRequest.newBuilder().priority(Priority.UNDEFINED).resourceName("*") + .capability(Resources.createResource(3072)).numContainers(1).relaxLocality(false) + .build()), null); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path( + ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); ActivitiesTestUtils.requestWebResource(r, null); - WebResource sr = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) - .path(RMWSConsts.SCHEDULER_ACTIVITIES); + WebTarget sr = + target().path(RMWSConsts.RM_WEB_SERVICE_PATH).path(RMWSConsts.SCHEDULER_ACTIVITIES); ActivitiesTestUtils.requestWebResource(sr, null); - nm1.nodeHeartbeat(true); Thread.sleep(1000); - JSONObject appActivitiesJson = - ActivitiesTestUtils.requestWebResource(r, null); - JSONObject schedulerActivitiesJson = - ActivitiesTestUtils.requestWebResource(sr, null); + JSONObject appActivitiesJson = ActivitiesTestUtils.requestWebResource(r, null); + JSONObject schedulerActivitiesJson = ActivitiesTestUtils.requestWebResource(sr, null); // verify app activities: diagnostic should be attached at request level // and there should be no allocation attempts at node level verifyNumberOfAllocations(appActivitiesJson, 1); - List requestAllocations = ActivitiesTestUtils - .getSubNodesFromJson(appActivitiesJson, FN_APP_ACT_ROOT, + List requestAllocations = + ActivitiesTestUtils.getSubNodesFromJson(appActivitiesJson, FN_APP_ACT_ROOT, FN_ACT_ALLOCATIONS, FN_APP_ACT_CHILDREN); assertEquals(1, requestAllocations.size()); assertEquals(ActivityDiagnosticConstant.QUEUE_DO_NOT_HAVE_ENOUGH_HEADROOM, @@ -1585,42 +1383,39 @@ public void testQueueSkippedBecauseOfHeadroom() throws Exception { Predicate findA1AQueuePred = (obj) -> obj.optString(FN_SCHEDULER_ACT_NAME).equals("root.a.a1.a1a"); List a1aQueueObj = ActivitiesTestUtils.findInAllocations( - getFirstSubNodeFromJson(schedulerActivitiesJson, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS), findA1AQueuePred); + getFirstSubNodeFromJson(schedulerActivitiesJson, FN_SCHEDULER_ACT_ROOT, + FN_ACT_ALLOCATIONS), findA1AQueuePred); assertEquals(1, a1aQueueObj.size()); assertEquals(ActivityState.REJECTED.name(), a1aQueueObj.get(0).optString(FN_ACT_ALLOCATION_STATE)); - assertTrue(a1aQueueObj.get(0).optString(FN_ACT_DIAGNOSTIC).startsWith( - ActivityDiagnosticConstant.QUEUE_DO_NOT_HAVE_ENOUGH_HEADROOM)); + assertTrue(a1aQueueObj.get(0).optString(FN_ACT_DIAGNOSTIC) + .startsWith(ActivityDiagnosticConstant.QUEUE_DO_NOT_HAVE_ENOUGH_HEADROOM)); // verify at request level Predicate findReqPred = (obj) -> obj.optString(FN_SCHEDULER_ACT_NAME).equals("request_-1_-1"); List reqObj = ActivitiesTestUtils.findInAllocations( - getFirstSubNodeFromJson(schedulerActivitiesJson, - FN_SCHEDULER_ACT_ROOT, FN_ACT_ALLOCATIONS), findReqPred); + getFirstSubNodeFromJson(schedulerActivitiesJson, FN_SCHEDULER_ACT_ROOT, + FN_ACT_ALLOCATIONS), findReqPred); assertEquals(1, reqObj.size()); - assertEquals(ActivityState.REJECTED.name(), - reqObj.get(0).optString(FN_ACT_ALLOCATION_STATE)); - assertTrue(reqObj.get(0).optString(FN_ACT_DIAGNOSTIC).startsWith( - ActivityDiagnosticConstant.QUEUE_DO_NOT_HAVE_ENOUGH_HEADROOM)); + assertEquals(ActivityState.REJECTED.name(), reqObj.get(0).optString(FN_ACT_ALLOCATION_STATE)); + assertTrue(reqObj.get(0).optString(FN_ACT_DIAGNOSTIC) + .startsWith(ActivityDiagnosticConstant.QUEUE_DO_NOT_HAVE_ENOUGH_HEADROOM)); } finally { rm.stop(); } } - @Test(timeout=30000) + @Test(timeout = 30000) public void testSchedulerBulkActivities() throws Exception { rm.start(); - MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, - rm.getResourceTrackerService()); - MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, - rm.getResourceTrackerService()); + MockNM nm1 = new MockNM("127.0.0.1:1234", 4 * 1024, rm.getResourceTrackerService()); + MockNM nm2 = new MockNM("127.0.0.2:1234", 4 * 1024, rm.getResourceTrackerService()); nm1.registerNode(); nm2.registerNode(); - MockNM[] nms = new MockNM[] {nm1, nm2}; + MockNM[] nms = new MockNM[] { nm1, nm2 }; try { @@ -1631,13 +1426,11 @@ public void testSchedulerBulkActivities() throws Exception { sendHeartbeat(restClient, nms); - JSONObject activitiesJson = restClient.getOutput().getJSONObject( - FN_SCHEDULER_BULK_ACT_ROOT); + JSONObject activitiesJson = restClient.getOutput().getJSONObject(FN_SCHEDULER_BULK_ACT_ROOT); Object activities = activitiesJson.get(FN_SCHEDULER_ACT_ROOT); assertEquals("Number of activities is wrong", expectedCount, ((JSONArray) activities).length()); - // Validate if response does not exceed max 500 expectedCount = 1000; restClient = new RESTClient(expectedCount); @@ -1645,11 +1438,9 @@ public void testSchedulerBulkActivities() throws Exception { sendHeartbeat(restClient, nms); - activitiesJson = restClient.getOutput().getJSONObject( - FN_SCHEDULER_BULK_ACT_ROOT); + activitiesJson = restClient.getOutput().getJSONObject(FN_SCHEDULER_BULK_ACT_ROOT); activities = activitiesJson.get(FN_SCHEDULER_ACT_ROOT); - assertEquals("Max Activities Limit does not work", - RMWebServices.MAX_ACTIVITIES_COUNT, + assertEquals("Max Activities Limit does not work", RMWebServices.MAX_ACTIVITIES_COUNT, ((JSONArray) activities).length()); } finally { @@ -1677,23 +1468,21 @@ JSONObject getOutput() { @Override public void run() { - WebResource r = resource(); - MultivaluedMapImpl params = new MultivaluedMapImpl(); - params.add(RMWSConsts.ACTIVITIES_COUNT, expectedCount); + WebTarget r = target(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path(RMWSConsts.SCHEDULER_BULK_ACTIVITIES).queryParams(params) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response response = + r.path("ws").path("v1").path("cluster").path(RMWSConsts.SCHEDULER_BULK_ACTIVITIES) + .queryParam(RMWSConsts.ACTIVITIES_COUNT, expectedCount) + .request(MediaType.APPLICATION_JSON).get(Response.class); - assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " - + JettyUtils.UTF_8, response.getType().toString()); - json = response.getEntity(JSONObject.class); + assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); done = true; } } - private void sendHeartbeat(RESTClient restClient, MockNM[] nms) - throws Exception { + private void sendHeartbeat(RESTClient restClient, MockNM[] nms) throws Exception { GenericTestUtils.waitFor(() -> { try { for (MockNM nm : nms) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivitiesWithMultiNodesEnabled.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivitiesWithMultiNodesEnabled.java index e37a8d83c0560..30e5841a3f2c1 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivitiesWithMultiNodesEnabled.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesSchedulerActivitiesWithMultiNodesEnabled.java @@ -18,12 +18,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.yarn.api.records.Priority; import org.apache.hadoop.yarn.api.records.ResourceRequest; @@ -46,14 +43,18 @@ import org.apache.hadoop.yarn.util.resource.Resources; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -85,18 +86,13 @@ * Tests for scheduler/app activities when multi-nodes enabled. */ public class TestRMWebServicesSchedulerActivitiesWithMultiNodesEnabled - extends JerseyTestBase { + extends JerseyTest { private static MockRM rm; private static CapacitySchedulerConfiguration csConf; private static YarnConfiguration conf; public TestRMWebServicesSchedulerActivitiesWithMultiNodesEnabled() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } private static class WebServletModule extends ServletModule { @@ -130,7 +126,7 @@ protected void configureServlets() { conf.setLong(YarnConfiguration.RM_NM_HEARTBEAT_INTERVAL_MS, 30000); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -184,12 +180,12 @@ public void testAssignContainer() throws Exception { 1)), null); //Trigger recording for multi-nodes without params - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path( + "scheduler/activities").request( + MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); //Trigger scheduling for this app CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); RMNode rmNode = rm.getRMContext().getRMNodes().get(nm.getNodeId()); @@ -198,11 +194,11 @@ public void testAssignContainer() throws Exception { //Check scheduler activities, it should contain one allocation and // final allocation state is ALLOCATED response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + "scheduler/activities").request( + MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); @@ -227,12 +223,12 @@ public void testSchedulingWithoutPendingRequests() try { //Trigger recording for multi-nodes without params - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path( + "scheduler/activities").request( + MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); + response.getMediaType().toString()); //Trigger scheduling for this app CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler(); RMNode rmNode = rm.getRMContext().getRMNodes().get(nm.getNodeId()); @@ -241,11 +237,11 @@ public void testSchedulingWithoutPendingRequests() //Check scheduler activities, it should contain one allocation and // final allocation state is SKIPPED response = r.path("ws").path("v1").path("cluster").path( - "scheduler/activities").accept( - MediaType.APPLICATION_JSON).get(ClientResponse.class); + "scheduler/activities").request( + MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyNumberOfAllocations(json, 1); JSONObject allocation = getFirstSubNodeFromJson(json, @@ -279,10 +275,10 @@ public void testAppAssignContainer() throws Exception { 1)), null); //Trigger recording for this app - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH) .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); @@ -351,13 +347,13 @@ public void testInsufficientResourceDiagnostic() throws Exception { .build()); MockAM am2 = MockRM.launchAndRegisterAM(app2, rm, nm1); - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); assertEquals("waiting for next allocation", json.getJSONObject(FN_SCHEDULER_ACT_ROOT).getString("diagnostic")); @@ -368,10 +364,10 @@ public void testInsufficientResourceDiagnostic() throws Exception { response = r.path("ws").path("v1").path("cluster").path("scheduler/activities") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + json = response.readEntity(JSONObject.class); //Check app activities verifyNumberOfAllocations(json, 1); @@ -428,10 +424,10 @@ public void testAppInsufficientResourceDiagnostic() throws Exception { .build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH) .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); JSONObject json = ActivitiesTestUtils.requestWebResource(r, params); assertEquals("waiting for display", json.getJSONObject(FN_APP_ACT_ROOT).getString(FN_ACT_DIAGNOSTIC)); @@ -507,9 +503,9 @@ public void testGroupByDiagnostics() throws Exception { .build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH) .path(RMWSConsts.SCHEDULER_ACTIVITIES); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); /* * test non-exist groupBy @@ -593,10 +589,10 @@ public void testAppGroupByDiagnostics() throws Exception { .build()); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm1); - WebResource r = resource().path(RMWSConsts.RM_WEB_SERVICE_PATH) + WebTarget r = target().path(RMWSConsts.RM_WEB_SERVICE_PATH) .path(ActivitiesTestUtils.format(RMWSConsts.SCHEDULER_APP_ACTIVITIES, app1.getApplicationId().toString())); - MultivaluedMapImpl params = new MultivaluedMapImpl(); + MultivaluedMap params = new MultivaluedHashMap<>(); /* * test non-exist groupBy diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebappAuthentication.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebappAuthentication.java index 1b4ff9ce0fd1e..0eddb5ff3c6eb 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebappAuthentication.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebappAuthentication.java @@ -30,6 +30,7 @@ import java.util.Collection; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response.Status; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeys; @@ -52,8 +53,6 @@ import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import com.sun.jersey.api.client.ClientResponse.Status; - /* Just a simple test class to ensure that the RM handles the static web user * correctly for secure and un-secure modes * diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairScheduler.java index bf605e9f5f669..3836bfb876b51 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairScheduler.java @@ -19,11 +19,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp.fairscheduler; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.MockRM; @@ -36,13 +34,16 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.RMWebServices; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; + +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -50,7 +51,7 @@ /** * Tests RM Webservices fair scheduler resources. */ -public class TestRMWebServicesFairScheduler extends JerseyTestBase { +public class TestRMWebServicesFairScheduler extends JerseyTest { private static MockRM rm; private static YarnConfiguration conf; @@ -65,7 +66,7 @@ protected void configureServlets() { ResourceScheduler.class); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } } @@ -83,34 +84,29 @@ public void setUp() throws Exception { } public TestRMWebServicesFairScheduler() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testClusterScheduler() throws JSONException { - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterScheduler(json); } @Test public void testClusterSchedulerSlash() throws JSONException { - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler/") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); verifyClusterScheduler(json); } @@ -123,13 +119,13 @@ public void testClusterSchedulerWithSubQueues() queueManager.getLeafQueue("root.q.subqueue1", true); queueManager.getLeafQueue("root.q.subqueue2", true); - WebResource r = resource(); - ClientResponse response = + WebTarget r = target(); + Response response = r.path("ws").path("v1").path("cluster").path("scheduler") - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .request(MediaType.APPLICATION_JSON).get(Response.class); assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - JSONObject json = response.getEntity(JSONObject.class); + response.getMediaType().toString()); + JSONObject json = response.readEntity(JSONObject.class); JSONArray subQueueInfo = json.getJSONObject("scheduler") .getJSONObject("schedulerInfo").getJSONObject("rootQueue") .getJSONObject("childQueues").getJSONArray("queue").getJSONObject(0) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairSchedulerCustomResourceTypes.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairSchedulerCustomResourceTypes.java index 7e9d11cc7bed8..00f5c54f408c7 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairSchedulerCustomResourceTypes.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/fairscheduler/TestRMWebServicesFairSchedulerCustomResourceTypes.java @@ -19,11 +19,9 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp.fairscheduler; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.conf.YarnConfiguration; @@ -40,15 +38,18 @@ import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.GuiceServletConfig; -import org.apache.hadoop.yarn.webapp.JerseyTestBase; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.glassfish.jersey.test.JerseyTest; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Element; + +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.lang.reflect.Method; import java.util.List; import java.util.Map; @@ -63,7 +64,7 @@ * {@link CustomResourceTypesConfigurationProvider} */ public class TestRMWebServicesFairSchedulerCustomResourceTypes - extends JerseyTestBase { + extends JerseyTest { private static MockRM rm; private static YarnConfiguration conf; @@ -79,7 +80,7 @@ protected void configureServlets() { initResourceTypes(conf); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); } private void initResourceTypes(YarnConfiguration conf) { @@ -112,11 +113,6 @@ public void teardown() { } public TestRMWebServicesFairSchedulerCustomResourceTypes() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test @@ -131,10 +127,10 @@ public void testClusterSchedulerWithCustomResourceTypesJson() { queueManager.getLeafQueue("root.q.subqueue1", false); incrementUsedResourcesOnQueue(subqueue1, 33L); - WebResource path = - resource().path("ws").path("v1").path("cluster").path("scheduler"); - ClientResponse response = - path.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget path = + target().path("ws").path("v1").path("cluster").path("scheduler"); + Response response = + path.request(MediaType.APPLICATION_JSON).get(Response.class); verifyJsonResponse(path, response, CustomResourceTypesConfigurationProvider.getCustomResourceTypes()); @@ -152,10 +148,10 @@ public void testClusterSchedulerWithCustomResourceTypesXml() { queueManager.getLeafQueue("root.q.subqueue1", false); incrementUsedResourcesOnQueue(subqueue1, 33L); - WebResource path = - resource().path("ws").path("v1").path("cluster").path("scheduler"); - ClientResponse response = - path.accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + WebTarget path = + target().path("ws").path("v1").path("cluster").path("scheduler"); + Response response = + path.request(MediaType.APPLICATION_XML).get(Response.class); verifyXmlResponse(path, response, CustomResourceTypesConfigurationProvider.getCustomResourceTypes()); @@ -176,10 +172,10 @@ public void testClusterSchedulerWithElevenCustomResourceTypesXml() { queueManager.getLeafQueue("root.q.subqueue1", false); incrementUsedResourcesOnQueue(subqueue1, 33L); - WebResource path = - resource().path("ws").path("v1").path("cluster").path("scheduler"); - ClientResponse response = - path.accept(MediaType.APPLICATION_XML).get(ClientResponse.class); + WebTarget path = + target().path("ws").path("v1").path("cluster").path("scheduler"); + Response response = + path.request(MediaType.APPLICATION_XML).get(Response.class); verifyXmlResponse(path, response, CustomResourceTypesConfigurationProvider.getCustomResourceTypes()); @@ -200,16 +196,16 @@ public void testClusterSchedulerElevenWithCustomResourceTypesJson() { queueManager.getLeafQueue("root.q.subqueue1", false); incrementUsedResourcesOnQueue(subqueue1, 33L); - WebResource path = - resource().path("ws").path("v1").path("cluster").path("scheduler"); - ClientResponse response = - path.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + WebTarget path = + target().path("ws").path("v1").path("cluster").path("scheduler"); + Response response = + path.request(MediaType.APPLICATION_JSON).get(Response.class); verifyJsonResponse(path, response, CustomResourceTypesConfigurationProvider.getCustomResourceTypes()); } - private void verifyJsonResponse(WebResource path, ClientResponse response, + private void verifyJsonResponse(WebTarget path, Response response, List customResourceTypes) { JsonCustomResourceTypeTestcase testCase = new JsonCustomResourceTypeTestcase(path, @@ -232,7 +228,7 @@ private void verifyJsonResponse(WebResource path, ClientResponse response, }); } - private void verifyXmlResponse(WebResource path, ClientResponse response, + private void verifyXmlResponse(WebTarget path, Response response, List customResourceTypes) { XmlCustomResourceTypeTestCase testCase = new XmlCustomResourceTypeTestCase( path, new BufferedClientResponse(response)); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/BufferedClientResponse.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/BufferedClientResponse.java index 8953293bdc988..a496503f19ee2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/BufferedClientResponse.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/BufferedClientResponse.java @@ -19,41 +19,37 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp.helper; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; - import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; +import java.io.InputStream; /** - * This class is merely a wrapper for {@link ClientResponse}. Given that the - * entity input stream of {@link ClientResponse} can be read only once by + * This class is merely a wrapper for {@link Response}. Given that the + * entity input stream of {@link Response} can be read only once by * default and for some tests it is convenient to read the input stream many * times, this class hides the details of how to do that and prevents * unnecessary code duplication in tests. */ public class BufferedClientResponse { - private ClientResponse response; + private Response response; - public BufferedClientResponse(ClientResponse response) { + public BufferedClientResponse(Response response) { response.bufferEntity(); this.response = response; } - public T getEntity(Class clazz) - throws ClientHandlerException, UniformInterfaceException { + public T getEntity(Class clazz) { try { - response.getEntityInputStream().reset(); + response.readEntity(InputStream.class).reset(); } catch (IOException e) { throw new RuntimeException(e); } - return response.getEntity(clazz); + return response.readEntity(clazz); } public MediaType getType() { - return response.getType(); + return response.getMediaType(); } public int getStatus() { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/JsonCustomResourceTypeTestcase.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/JsonCustomResourceTypeTestcase.java index 2b8d08d398c90..0a320c258d0cb 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/JsonCustomResourceTypeTestcase.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/JsonCustomResourceTypeTestcase.java @@ -18,12 +18,12 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp.helper; -import com.sun.jersey.api.client.WebResource; import org.apache.hadoop.http.JettyUtils; import org.codehaus.jettison.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import java.util.function.Consumer; @@ -33,7 +33,7 @@ /** * This class hides the implementation details of how to verify the structure of * JSON responses. Tests should only provide the path of the - * {@link WebResource}, the response from the resource and + * {@link WebTarget}, the response from the resource and * the verifier Consumer to * {@link JsonCustomResourceTypeTestcase#verify(Consumer)}. An instance of * {@link JSONObject} will be passed to that consumer to be able to @@ -43,11 +43,11 @@ public class JsonCustomResourceTypeTestcase { private static final Logger LOG = LoggerFactory.getLogger(JsonCustomResourceTypeTestcase.class); - private final WebResource path; + private final WebTarget path; private final BufferedClientResponse response; private final JSONObject parsedResponse; - public JsonCustomResourceTypeTestcase(WebResource path, + public JsonCustomResourceTypeTestcase(WebTarget path, BufferedClientResponse response) { this.path = path; verifyStatus(response); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/XmlCustomResourceTypeTestCase.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/XmlCustomResourceTypeTestCase.java index 0ad92d20bb5fa..44adaf9fee34a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/XmlCustomResourceTypeTestCase.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/helper/XmlCustomResourceTypeTestCase.java @@ -18,7 +18,6 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp.helper; -import com.sun.jersey.api.client.WebResource; import org.apache.hadoop.http.JettyUtils; import org.codehaus.jettison.json.JSONObject; import org.slf4j.Logger; @@ -27,6 +26,7 @@ import org.w3c.dom.Node; import org.xml.sax.InputSource; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -42,7 +42,7 @@ /** * This class hides the implementation details of how to verify the structure of * XML responses. Tests should only provide the path of the - * {@link WebResource}, the response from the resource and + * {@link WebTarget}, the response from the resource and * the verifier Consumer to * {@link XmlCustomResourceTypeTestCase#verify(Consumer)}. An instance of * {@link JSONObject} will be passed to that consumer to be able to @@ -52,11 +52,11 @@ public class XmlCustomResourceTypeTestCase { private static final Logger LOG = LoggerFactory.getLogger(XmlCustomResourceTypeTestCase.class); - private WebResource path; + private WebTarget path; private BufferedClientResponse response; private Document parsedResponse; - public XmlCustomResourceTypeTestCase(WebResource path, + public XmlCustomResourceTypeTestCase(WebTarget path, BufferedClientResponse response) { this.path = path; verifyStatus(response); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/webapp/TestRMWithCSRFFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/webapp/TestRMWithCSRFFilter.java index 2925e841120bb..fcd06ffe674d8 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/webapp/TestRMWithCSRFFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/webapp/TestRMWithCSRFFilter.java @@ -19,12 +19,9 @@ package org.apache.hadoop.yarn.webapp; import com.google.inject.Guice; +import com.google.inject.Scopes; +import com.google.inject.servlet.GuiceFilter; import com.google.inject.servlet.ServletModule; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; -import com.sun.jersey.test.framework.WebAppDescriptor; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; import org.apache.hadoop.security.http.RestCsrfPreventionFilter; @@ -38,6 +35,8 @@ import org.apache.hadoop.yarn.server.resourcemanager.webapp.JAXBContextResolver; import org.apache.hadoop.yarn.server.resourcemanager.webapp.RMWebServices; import org.apache.hadoop.yarn.util.YarnVersionInfo; + +import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -45,7 +44,10 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; @@ -59,7 +61,7 @@ * Used TestRMWebServices as an example of web invocations of RM and added * test for CSRF Filter. */ -public class TestRMWithCSRFFilter extends JerseyTestBase { +public class TestRMWithCSRFFilter extends JerseyTest { private static MockRM rm; @@ -74,7 +76,7 @@ protected void configureServlets() { ResourceScheduler.class); rm = new MockRM(conf); bind(ResourceManager.class).toInstance(rm); - serve("/*").with(GuiceContainer.class); + bind(GuiceFilter.class).in(Scopes.SINGLETON); RestCsrfPreventionFilter csrfFilter = new RestCsrfPreventionFilter(); Map initParams = new HashMap<>(); // adding GET as protected method to make things a little easier... @@ -93,45 +95,40 @@ public void setUp() throws Exception { } public TestRMWithCSRFFilter() { - super(new WebAppDescriptor.Builder( - "org.apache.hadoop.yarn.server.resourcemanager.webapp") - .contextListenerClass(GuiceServletConfig.class) - .filterClass(com.google.inject.servlet.GuiceFilter.class) - .contextPath("jersey-guice-filter").servletPath("/").build()); } @Test public void testNoCustomHeaderFromBrowser() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").accept("application/xml") + WebTarget target = target(); + Response response = target.path("ws").path("v1").path("cluster") + .path("info").request("application/xml") .header(RestCsrfPreventionFilter.HEADER_USER_AGENT,"Mozilla/5.0") - .get(ClientResponse.class); + .get(Response.class); assertTrue("Should have been rejected", response.getStatus() == Status.BAD_REQUEST.getStatusCode()); } @Test public void testIncludeCustomHeaderFromBrowser() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").accept("application/xml") + WebTarget target = target(); + Response response = target.path("ws").path("v1").path("cluster") + .path("info").request("application/xml") .header(RestCsrfPreventionFilter.HEADER_USER_AGENT,"Mozilla/5.0") .header("X-XSRF-HEADER", "") - .get(ClientResponse.class); + .get(Response.class); assertTrue("Should have been accepted", response.getStatus() == Status.OK.getStatusCode()); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyClusterInfoXML(xml); } @Test public void testAllowedMethod() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").accept("application/xml") + WebTarget target = target(); + Response response = target.path("ws").path("v1").path("cluster") + .path("info").request("application/xml") .header(RestCsrfPreventionFilter.HEADER_USER_AGENT,"Mozilla/5.0") .head(); assertTrue("Should have been allowed", response.getStatus() == @@ -140,15 +137,15 @@ public void testAllowedMethod() throws Exception { @Test public void testAllowNonBrowserInteractionWithoutHeader() throws Exception { - WebResource r = resource(); - ClientResponse response = r.path("ws").path("v1").path("cluster") - .path("info").accept("application/xml") - .get(ClientResponse.class); + WebTarget target = target(); + Response response = target.path("ws").path("v1").path("cluster") + .path("info").request("application/xml") + .get(Response.class); assertTrue("Should have been accepted", response.getStatus() == Status.OK.getStatusCode()); assertEquals(MediaType.APPLICATION_XML_TYPE + "; " + JettyUtils.UTF_8, - response.getType().toString()); - String xml = response.getEntity(String.class); + response.getMediaType().toString()); + String xml = response.readEntity(String.class); verifyClusterInfoXML(xml); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/pom.xml index d29929047af6f..8ef7b62f24f4f 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/pom.xml @@ -116,6 +116,11 @@ guice + + javax.ws.rs + javax.ws.rs-api + + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/FederationBlock.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/FederationBlock.java index d84c705fec057..15eb7f5083b9b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/FederationBlock.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/FederationBlock.java @@ -18,7 +18,6 @@ package org.apache.hadoop.yarn.server.router.webapp; -import java.io.StringReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -39,10 +38,9 @@ import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet.TBODY; import org.apache.hadoop.yarn.webapp.view.HtmlBlock; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; import com.google.inject.Inject; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.api.json.JSONJAXBContext; -import com.sun.jersey.api.json.JSONUnmarshaller; class FederationBlock extends HtmlBlock { @@ -50,6 +48,8 @@ class FederationBlock extends HtmlBlock { private final Router router; + private static final ObjectReader MAPPER = new ObjectMapper().readerFor(ClusterMetricsInfo.class); + @Inject FederationBlock(ViewContext ctx, Router router) { super(ctx); @@ -162,12 +162,7 @@ public int compare(SubClusterInfo o1, SubClusterInfo o2) { private static ClusterMetricsInfo getClusterMetricsInfo(String capability) { ClusterMetricsInfo clusterMetrics = null; try { - JSONJAXBContext jc = new JSONJAXBContext( - JSONConfiguration.mapped().rootUnwrapping(false).build(), - ClusterMetricsInfo.class); - JSONUnmarshaller unmarshaller = jc.createJSONUnmarshaller(); - clusterMetrics = unmarshaller.unmarshalFromJSON( - new StringReader(capability), ClusterMetricsInfo.class); + clusterMetrics = MAPPER.readValue(capability); } catch (Exception e) { LOG.error("Cannot parse SubCluster info", e); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/pom.xml index 2de2c13f16b51..f6f56f818a236 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/pom.xml @@ -132,6 +132,10 @@ test test-jar + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timeline-pluginstorage/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timeline-pluginstorage/pom.xml index 835f2fed09555..0954c566427c4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timeline-pluginstorage/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timeline-pluginstorage/pom.xml @@ -133,6 +133,10 @@ com.fasterxml.jackson.core jackson-databind + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/pom.xml index a323eaff8558c..3e05772405871 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/pom.xml @@ -198,18 +198,6 @@ runtime - - com.sun.jersey - jersey-client - test - - - - javax.ws.rs - jsr311-api - 1.1.1 - - org.apache.hbase hbase-common @@ -438,6 +426,10 @@ commons-lang3 test + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/AbstractTimelineReaderHBaseTestBase.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/AbstractTimelineReaderHBaseTestBase.java index 471fb6c36f3dc..86bfe18e19c04 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/AbstractTimelineReaderHBaseTestBase.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase-tests/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/AbstractTimelineReaderHBaseTestBase.java @@ -29,7 +29,10 @@ import java.net.URL; import java.util.List; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseTestingUtility; @@ -37,16 +40,10 @@ import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.server.timelineservice.storage.DataGeneratorForTest; import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; -import org.junit.Assert; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.GenericType; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; +import org.junit.Assert; /** * Test Base for TimelineReaderServer HBase tests. @@ -109,19 +106,21 @@ protected void addFilters(Configuration conf) { } protected Client createClient() { - ClientConfig cfg = new DefaultClientConfig(); - cfg.getClasses().add(YarnJacksonJaxbJsonProvider.class); - return new Client( - new URLConnectionClientHandler(new DummyURLConnectionFactory()), cfg); + ClientConfig cfg = new ClientConfig(); + cfg.register(YarnJacksonJaxbJsonProvider.class); + cfg.connectorProvider( + new HttpUrlConnectorProvider().connectionFactory(new DummyURLConnectionFactory())); + return ClientBuilder.newClient(cfg); } - protected ClientResponse getResponse(Client client, URI uri) + protected Response getResponse(Client client, URI uri) throws Exception { - ClientResponse resp = - client.resource(uri).accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON).get(ClientResponse.class); - if (resp == null || resp.getStatusInfo() - .getStatusCode() != ClientResponse.Status.OK.getStatusCode()) { + Response resp = client.target(uri) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .get(Response.class); + if (resp == null || resp.getStatus() + .getStatusCode() != Response.Status.OK.getStatusCode()) { String msg = ""; if (resp != null) { msg = String.valueOf(resp.getStatusInfo().getStatusCode()); @@ -154,10 +153,10 @@ protected List verifyFlowEntites(Client client, URI uri, } protected static class DummyURLConnectionFactory - implements HttpURLConnectionFactory { + implements HttpUrlConnectorProvider.ConnectionFactory { @Override - public HttpURLConnection getHttpURLConnection(final URL url) + public HttpURLConnection getConnection(final URL url) throws IOException { try { return (HttpURLConnection) url.openConnection(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase/hadoop-yarn-server-timelineservice-hbase-common/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase/hadoop-yarn-server-timelineservice-hbase-common/pom.xml index 02fc357226552..39706647b7a23 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase/hadoop-yarn-server-timelineservice-hbase-common/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice-hbase/hadoop-yarn-server-timelineservice-hbase-common/pom.xml @@ -145,6 +145,11 @@ junit test + + + javax.ws.rs + javax.ws.rs-api + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/pom.xml index 65af3afadda62..30bfe6ec15ac2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/pom.xml @@ -101,11 +101,6 @@ jackson-databind - - com.sun.jersey - jersey-client - - org.apache.commons commons-csv @@ -113,8 +108,7 @@ javax.ws.rs - jsr311-api - 1.1.1 + javax.ws.rs-api diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServices.java index ef74716b56401..09fc797d40fc0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServices.java @@ -31,7 +31,11 @@ import java.net.URL; import java.util.Set; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; @@ -45,6 +49,9 @@ import org.apache.hadoop.yarn.server.timelineservice.storage.TestFileSystemTimelineReaderImpl; import org.apache.hadoop.yarn.server.timelineservice.storage.TimelineReader; import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -52,15 +59,6 @@ import org.junit.BeforeClass; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.GenericType; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; - public class TestTimelineReaderWebServices { private static final String ROOT_DIR = new File("target", @@ -116,30 +114,33 @@ private static TimelineEntity newEntity(String type, String id) { } private static void verifyHttpResponse(Client client, URI uri, - Status expectedStatus) { - ClientResponse resp = - client.resource(uri).accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON).get(ClientResponse.class); + Response.Status expectedStatus) { + Response resp = client.target(uri) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .get(Response.class); assertNotNull(resp); assertEquals(resp.getStatusInfo().getStatusCode(), expectedStatus.getStatusCode()); } private static Client createClient() { - ClientConfig cfg = new DefaultClientConfig(); - cfg.getClasses().add(YarnJacksonJaxbJsonProvider.class); - return new Client(new URLConnectionClientHandler( - new DummyURLConnectionFactory()), cfg); + ClientConfig cfg = new ClientConfig(); + cfg.register(YarnJacksonJaxbJsonProvider.class); + cfg.connectorProvider( + new HttpUrlConnectorProvider().connectionFactory(new DummyURLConnectionFactory())); + return ClientBuilder.newClient(cfg); } - private static ClientResponse getResponse(Client client, URI uri) + private static Response getResponse(Client client, URI uri) throws Exception { - ClientResponse resp = - client.resource(uri).accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON).get(ClientResponse.class); - if (resp == null || - resp.getStatusInfo().getStatusCode() != - ClientResponse.Status.OK.getStatusCode()) { + Response resp = + client.target(uri) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .get(Response.class); + if (resp == null + || resp.getStatusInfo().getStatusCode() != Response.Status.OK.getStatusCode()) { String msg = new String(); if (resp != null) { msg = String.valueOf(resp.getStatusInfo().getStatusCode()); @@ -151,11 +152,10 @@ private static ClientResponse getResponse(Client client, URI uri) } private static class DummyURLConnectionFactory - implements HttpURLConnectionFactory { + implements HttpUrlConnectorProvider.ConnectionFactory { @Override - public HttpURLConnection getHttpURLConnection(final URL url) - throws IOException { + public HttpURLConnection getConnection(URL url) throws IOException { try { return (HttpURLConnection)url.openConnection(); } catch (UndeclaredThrowableException e) { @@ -169,12 +169,12 @@ public void testAbout() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/timeline/"); Client client = createClient(); try { - ClientResponse resp = getResponse(client, uri); - TimelineAbout about = resp.getEntity(TimelineAbout.class); + Response resp = getResponse(client, uri); + TimelineAbout about = resp.readEntity(TimelineAbout.class); Assert.assertNotNull(about); Assert.assertEquals("Timeline Reader API", about.getAbout()); } finally { - client.destroy(); + client.close(); } } @@ -184,10 +184,10 @@ public void testGetEntityDefaultView() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app/id_1"); - ClientResponse resp = getResponse(client, uri); - TimelineEntity entity = resp.getEntity(TimelineEntity.class); + Response resp = getResponse(client, uri); + TimelineEntity entity = resp.readEntity(TimelineEntity.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entity); assertEquals("id_1", entity.getId()); assertEquals("app", entity.getType()); @@ -197,7 +197,7 @@ public void testGetEntityDefaultView() throws Exception { assertEquals(0, entity.getConfigs().size()); assertEquals(0, entity.getMetrics().size()); } finally { - client.destroy(); + client.close(); } } @@ -208,16 +208,16 @@ public void testGetEntityWithUserAndFlowInfo() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app/id_1?" + "userid=user1&flowname=flow1&flowrunid=1"); - ClientResponse resp = getResponse(client, uri); - TimelineEntity entity = resp.getEntity(TimelineEntity.class); + Response resp = getResponse(client, uri); + TimelineEntity entity = resp.readEntity(TimelineEntity.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entity); assertEquals("id_1", entity.getId()); assertEquals("app", entity.getType()); assertEquals((Long)1425016502000L, entity.getCreatedTime()); } finally { - client.destroy(); + client.close(); } } @@ -229,10 +229,10 @@ public void testGetEntityCustomFields() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app/id_1?" + "fields=CONFIGS,Metrics,info"); - ClientResponse resp = getResponse(client, uri); - TimelineEntity entity = resp.getEntity(TimelineEntity.class); + Response resp = getResponse(client, uri); + TimelineEntity entity = resp.readEntity(TimelineEntity.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entity); assertEquals("id_1", entity.getId()); assertEquals("app", entity.getType()); @@ -245,7 +245,7 @@ public void testGetEntityCustomFields() throws Exception { // No events will be returned as events are not part of fields. assertEquals(0, entity.getEvents().size()); } finally { - client.destroy(); + client.close(); } } @@ -256,10 +256,10 @@ public void testGetEntityAllFields() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app/id_1?" + "fields=ALL"); - ClientResponse resp = getResponse(client, uri); - TimelineEntity entity = resp.getEntity(TimelineEntity.class); + Response resp = getResponse(client, uri); + TimelineEntity entity = resp.readEntity(TimelineEntity.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entity); assertEquals("id_1", entity.getId()); assertEquals("app", entity.getType()); @@ -271,7 +271,7 @@ public void testGetEntityAllFields() throws Exception { assertEquals(3, entity.getInfo().size()); assertEquals(2, entity.getEvents().size()); } finally { - client.destroy(); + client.close(); } } @@ -281,9 +281,9 @@ public void testGetEntityNotPresent() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app/id_10"); - verifyHttpResponse(client, uri, Status.NOT_FOUND); + verifyHttpResponse(client, uri, Response.Status.NOT_FOUND); } finally { - client.destroy(); + client.close(); } } @@ -293,10 +293,10 @@ public void testQueryWithoutCluster() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/apps/app1/entities/app/id_1"); - ClientResponse resp = getResponse(client, uri); - TimelineEntity entity = resp.getEntity(TimelineEntity.class); + Response resp = getResponse(client, uri); + TimelineEntity entity = resp.readEntity(TimelineEntity.class); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entity); assertEquals("id_1", entity.getId()); assertEquals("app", entity.getType()); @@ -305,13 +305,13 @@ public void testQueryWithoutCluster() throws Exception { "timeline/apps/app1/entities/app"); resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(4, entities.size()); } finally { - client.destroy(); + client.close(); } } @@ -321,11 +321,11 @@ public void testGetEntities() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(4, entities.size()); assertTrue("Entities id_1, id_2, id_3 and id_4 should have been" + @@ -335,7 +335,7 @@ public void testGetEntities() throws Exception { entities.contains(newEntity("app", "id_3")) && entities.contains(newEntity("app", "id_4"))); } finally { - client.destroy(); + client.close(); } } @@ -345,11 +345,11 @@ public void testGetEntitiesWithLimit() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?limit=2"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(2, entities.size()); // Entities returned are based on most recent created time. @@ -361,15 +361,15 @@ public void testGetEntitiesWithLimit() throws Exception { uri = URI.create("http://localhost:" + serverPort + "/ws/v2/timeline/" + "clusters/cluster1/apps/app1/entities/app?limit=3"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>(){}); + entities = resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); // Even though 2 entities out of 4 have same created time, one entity // is left out due to limit assertEquals(3, entities.size()); } finally { - client.destroy(); + client.close(); } } @@ -380,11 +380,11 @@ public void testGetEntitiesBasedOnCreatedTime() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?" + "createdtimestart=1425016502030&createdtimeend=1425016502060"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_4 should have been present in response.", @@ -394,9 +394,9 @@ public void testGetEntitiesBasedOnCreatedTime() throws Exception { "clusters/cluster1/apps/app1/entities/app?createdtimeend" + "=1425016502010"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>(){}); + entities = resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(3, entities.size()); assertFalse("Entity with id_4 should not have been present in response.", @@ -406,15 +406,15 @@ public void testGetEntitiesBasedOnCreatedTime() throws Exception { "clusters/cluster1/apps/app1/entities/app?createdtimestart=" + "1425016502010"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>(){}); + entities = resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_4 should have been present in response.", entities.contains(newEntity("app", "id_4"))); } finally { - client.destroy(); + client.close(); } } @@ -425,11 +425,11 @@ public void testGetEntitiesByRelations() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?relatesto=" + "flow:flow1"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_1 should have been present in response.", @@ -439,9 +439,9 @@ public void testGetEntitiesByRelations() throws Exception { "clusters/cluster1/apps/app1/entities/app?isrelatedto=" + "type1:tid1_2,type2:tid2_1%60"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>(){}); + entities = resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_1 should have been present in response.", @@ -451,15 +451,15 @@ public void testGetEntitiesByRelations() throws Exception { "clusters/cluster1/apps/app1/entities/app?isrelatedto=" + "type1:tid1_1:tid1_2,type2:tid2_1%60"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>(){}); + entities = resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_1 should have been present in response.", entities.contains(newEntity("app", "id_1"))); } finally { - client.destroy(); + client.close(); } } @@ -470,17 +470,17 @@ public void testGetEntitiesByConfigFilters() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?" + "conffilters=config_1%20eq%20123%20AND%20config_3%20eq%20abc"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_3 should have been present in response.", entities.contains(newEntity("app", "id_3"))); } finally { - client.destroy(); + client.close(); } } @@ -491,17 +491,17 @@ public void testGetEntitiesByInfoFilters() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?" + "infofilters=info2%20eq%203.5"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_3 should have been present in response.", entities.contains(newEntity("app", "id_3"))); } finally { - client.destroy(); + client.close(); } } @@ -512,11 +512,11 @@ public void testGetEntitiesByMetricFilters() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?" + "metricfilters=metric3%20ge%200"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(2, entities.size()); assertTrue("Entities with id_1 and id_2 should have been present" + @@ -524,7 +524,7 @@ public void testGetEntitiesByMetricFilters() throws Exception { entities.contains(newEntity("app", "id_1")) && entities.contains(newEntity("app", "id_2"))); } finally { - client.destroy(); + client.close(); } } @@ -535,17 +535,17 @@ public void testGetEntitiesByEventFilters() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?" + "eventfilters=event_2,event_4"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(1, entities.size()); assertTrue("Entity with id_3 should have been present in response.", entities.contains(newEntity("app", "id_3"))); } finally { - client.destroy(); + client.close(); } } @@ -559,15 +559,15 @@ public void testGetEntitiesNoMatch() throws Exception { "type2:tid2_1%60&relatesto=flow:flow1&eventfilters=event_2,event_4" + "&infofilters=info2%20eq%203.5&createdtimestart=1425016502030&" + "createdtimeend=1425016502060"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>(){}); + resp.readEntity(new GenericType>(){}); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); assertEquals(0, entities.size()); } finally { - client.destroy(); + client.close(); } } @@ -577,17 +577,17 @@ public void testInvalidValuesHandling() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/app?flowrunid=a23b"); - verifyHttpResponse(client, uri, Status.BAD_REQUEST); + verifyHttpResponse(client, uri, Response.Status.BAD_REQUEST); uri = URI.create("http://localhost:" + serverPort + "/ws/v2/timeline/" + "clusters/cluster1/apps/app1/entities/app/id_1?flowrunid=2ab15"); - verifyHttpResponse(client, uri, Status.BAD_REQUEST); + verifyHttpResponse(client, uri, Response.Status.BAD_REQUEST); uri = URI.create("http://localhost:" + serverPort + "/ws/v2/timeline/" + "clusters/cluster1/apps/app1/entities/app?limit=#$561av"); - verifyHttpResponse(client, uri, Status.BAD_REQUEST); + verifyHttpResponse(client, uri, Response.Status.BAD_REQUEST); } finally { - client.destroy(); + client.close(); } } @@ -598,12 +598,12 @@ public void testGetAppAttempts() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/" + "entities/YARN_APPLICATION_ATTEMPT"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>() { + resp.readEntity(new GenericType>() { }); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); int totalEntities = entities.size(); assertEquals(2, totalEntities); @@ -621,9 +621,9 @@ public void testGetAppAttempts() throws Exception { uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/appattempts"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>() { + entities = resp.readEntity(new GenericType>() { }); - assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getType()); + assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType()); assertNotNull(entities); int retrievedEntity = entities.size(); assertEquals(2, retrievedEntity); @@ -641,7 +641,7 @@ public void testGetAppAttempts() throws Exception { assertEquals(totalEntities, retrievedEntity); } finally { - client.destroy(); + client.close(); } } @@ -652,27 +652,27 @@ public void testGetAppAttempt() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/" + "YARN_APPLICATION_ATTEMPT/app-attempt-1"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); TimelineEntity entities1 = - resp.getEntity(new GenericType() { + resp.readEntity(new GenericType() { }); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities1); uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/appattempts/app-attempt-1"); resp = getResponse(client, uri); TimelineEntity entities2 = - resp.getEntity(new GenericType() { + resp.readEntity(new GenericType() { }); - assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getType()); + assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType()); assertNotNull(entities2); assertEquals(entities1, entities2); } finally { - client.destroy(); + client.close(); } } @@ -683,12 +683,12 @@ public void testGetContainers() throws Exception { // total 3 containers in a application. URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/entities/YARN_CONTAINER"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); Set entities = - resp.getEntity(new GenericType>() { + resp.readEntity(new GenericType>() { }); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities); int totalEntities = entities.size(); assertEquals(3, totalEntities); @@ -710,9 +710,9 @@ public void testGetContainers() throws Exception { + "timeline/clusters/cluster1/apps/app1/" + "appattempts/app-attempt-1/containers"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>() { + entities = resp.readEntity(new GenericType>() { }); - assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getType()); + assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType()); assertNotNull(entities); int retrievedEntity = entities.size(); assertEquals(1, retrievedEntity); @@ -726,9 +726,9 @@ public void testGetContainers() throws Exception { + "timeline/clusters/cluster1/apps/app1/" + "appattempts/app-attempt-2/containers"); resp = getResponse(client, uri); - entities = resp.getEntity(new GenericType>() { + entities = resp.readEntity(new GenericType>() { }); - assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getType()); + assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType()); assertNotNull(entities); retrievedEntity += entities.size(); assertEquals(2, entities.size()); @@ -744,7 +744,7 @@ public void testGetContainers() throws Exception { assertEquals(totalEntities, retrievedEntity); } finally { - client.destroy(); + client.close(); } } @@ -755,27 +755,27 @@ public void testGetContainer() throws Exception { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/" + "entities/YARN_CONTAINER/container_2_2"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); TimelineEntity entities1 = - resp.getEntity(new GenericType() { + resp.readEntity(new GenericType() { }); assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, - resp.getType().toString()); + resp.getMediaType().toString()); assertNotNull(entities1); uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/clusters/cluster1/apps/app1/containers/container_2_2"); resp = getResponse(client, uri); TimelineEntity entities2 = - resp.getEntity(new GenericType() { + resp.readEntity(new GenericType() { }); - assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getType()); + assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType()); assertNotNull(entities2); assertEquals(entities1, entities2); } finally { - client.destroy(); + client.close(); } } @@ -785,15 +785,15 @@ public void testHealthCheck() throws Exception { try { URI uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/health"); - ClientResponse resp = getResponse(client, uri); + Response resp = getResponse(client, uri); TimelineHealth timelineHealth = - resp.getEntity(new GenericType() { + resp.readEntity(new GenericType() { }); assertEquals(200, resp.getStatus()); assertEquals(TimelineHealth.TimelineHealthStatus.RUNNING, timelineHealth.getHealthStatus()); } finally { - client.destroy(); + client.close(); } } } \ No newline at end of file diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServicesACL.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServicesACL.java index fbd042bd01537..1b657cb26db78 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServicesACL.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/reader/TestTimelineReaderWebServicesACL.java @@ -29,7 +29,10 @@ import java.net.URI; import java.net.URL; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; @@ -38,6 +41,9 @@ import org.apache.hadoop.yarn.server.timelineservice.storage.TestFileSystemTimelineReaderImpl; import org.apache.hadoop.yarn.server.timelineservice.storage.TimelineReader; import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -45,14 +51,6 @@ import org.junit.BeforeClass; import org.junit.Test; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; -import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; - /** * Tests ACL check while retrieving entity-types per application. */ @@ -109,11 +107,12 @@ public void stop() throws Exception { } } - private static ClientResponse verifyHttpResponse(Client client, URI uri, - Status expectedStatus) { - ClientResponse resp = - client.resource(uri).accept(MediaType.APPLICATION_JSON) - .type(MediaType.APPLICATION_JSON).get(ClientResponse.class); + private static Response verifyHttpResponse(Client client, URI uri, + Response.Status expectedStatus) { + Response resp = client.target(uri) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .get(Response.class); assertNotNull(resp); assertEquals(resp.getStatusInfo().getStatusCode(), expectedStatus.getStatusCode()); @@ -121,16 +120,18 @@ private static ClientResponse verifyHttpResponse(Client client, URI uri, } private static Client createClient() { - ClientConfig cfg = new DefaultClientConfig(); - cfg.getClasses().add(YarnJacksonJaxbJsonProvider.class); - return new Client(new URLConnectionClientHandler( - new DummyURLConnectionFactory()), cfg); + ClientConfig cfg = new ClientConfig(); + cfg.register(YarnJacksonJaxbJsonProvider.class); + cfg.connectorProvider( + new HttpUrlConnectorProvider().connectionFactory(new DummyURLConnectionFactory())); + return ClientBuilder.newClient(cfg); } private static class DummyURLConnectionFactory - implements HttpURLConnectionFactory { + implements HttpUrlConnectorProvider.ConnectionFactory { + @Override - public HttpURLConnection getHttpURLConnection(final URL url) + public HttpURLConnection getConnection(final URL url) throws IOException { try { return (HttpURLConnection)url.openConnection(); @@ -149,30 +150,30 @@ public void testGetEntityTypes() throws Exception { "timeline/apps/app1/entity-types?user.name="+unAuthorizedUser); String msg = "User " + unAuthorizedUser + " is not allowed to read TimelineService V2 data."; - ClientResponse resp = verifyHttpResponse(client, uri, Status.FORBIDDEN); - assertTrue(resp.getEntity(String.class).contains(msg)); + Response resp = verifyHttpResponse(client, uri, Response.Status.FORBIDDEN); + assertTrue(resp.readEntity(String.class).contains(msg)); String authorizedUser ="user1"; uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/apps/app1/entity-types?user.name="+authorizedUser); - verifyHttpResponse(client, uri, Status.OK); + verifyHttpResponse(client, uri, Response.Status.OK); uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/apps/app1/entity-types?user.name="+ADMIN); - verifyHttpResponse(client, uri, Status.OK); + verifyHttpResponse(client, uri, Response.Status.OK); // Verify with Query Parameter userid uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/apps/app1/entity-types?user.name="+authorizedUser + "&userid="+authorizedUser); - verifyHttpResponse(client, uri, Status.OK); + verifyHttpResponse(client, uri, Response.Status.OK); uri = URI.create("http://localhost:" + serverPort + "/ws/v2/" + "timeline/apps/app1/entity-types?user.name="+authorizedUser + "&userid="+unAuthorizedUser); - verifyHttpResponse(client, uri, Status.FORBIDDEN); + verifyHttpResponse(client, uri, Response.Status.FORBIDDEN); } finally { - client.destroy(); + client.close(); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/pom.xml index 1e0e9ae57ad8d..22044fa5ce3af 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/pom.xml @@ -105,17 +105,16 @@ - org.glassfish.grizzly - grizzly-http-servlet + org.glassfish.jersey.test-framework + jersey-test-framework-core test - - - com.sun.jersey.jersey-test-framework - jersey-test-framework-grizzly2 - test + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 + + org.bouncycastle bcprov-jdk15on