From 64beecb7cb51d0404149b7bb12561ca8cad2a7d2 Mon Sep 17 00:00:00 2001 From: slfan1989 <55643692+slfan1989@users.noreply.github.com> Date: Sat, 6 Jan 2024 17:50:20 +0800 Subject: [PATCH] YARN-11631. [GPG] Add GPGWebServices. (#6354) Contributed by Shilun Fan. Reviewed-by: Inigo Goiri Signed-off-by: Shilun Fan --- .../pom.xml | 11 +++ .../GlobalPolicyGenerator.java | 3 +- .../webapp/GPGWebApp.java | 1 + .../webapp/GPGWebServices.java | 62 ++++++++++++ .../webapp/dao/GpgInfo.java | 81 ++++++++++++++++ .../webapp/dao/package-info.java | 19 ++++ .../webapp/TestGPGWebServices.java | 96 +++++++++++++++++++ 7 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebServices.java create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/GpgInfo.java create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/package-info.java create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/test/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/TestGPGWebServices.java diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/pom.xml index f7c7c59139..b56c60e817 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/pom.xml @@ -118,6 +118,17 @@ test-jar + + com.sun.jersey.jersey-test-framework + jersey-test-framework-core + test + + + com.sun.jersey.jersey-test-framework + jersey-test-framework-grizzly2 + test + + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/GlobalPolicyGenerator.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/GlobalPolicyGenerator.java index 5822bd48c9..a3e231da1a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/GlobalPolicyGenerator.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/GlobalPolicyGenerator.java @@ -299,7 +299,8 @@ public void startWepApp() { } LOG.info("Instantiating GPGWebApp at {}.", webAppAddress); GPGWebApp gpgWebApp = new GPGWebApp(this); - webApp = WebApps.$for("gpg").at(webAppAddress).start(gpgWebApp); + webApp = WebApps.$for("gpg", GPGContext.class, this.gpgContext, + "ws").at(webAppAddress).start(gpgWebApp); } @SuppressWarnings("resource") diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebApp.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebApp.java index b3d63634ca..0bbe49db25 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebApp.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebApp.java @@ -34,6 +34,7 @@ public GPGWebApp(GlobalPolicyGenerator gpg) { @Override public void setup() { + bind(GPGWebServices.class); bind(JAXBContextResolver.class); bind(GPGWebApp.class).toInstance(this); bind(GenericExceptionHandler.class); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebServices.java new file mode 100644 index 0000000000..ef6b50d83a --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/GPGWebServices.java @@ -0,0 +1,62 @@ +/** * 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 applicable 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.server.globalpolicygenerator.webapp; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.hadoop.http.JettyUtils; +import org.apache.hadoop.yarn.server.globalpolicygenerator.GlobalPolicyGenerator; +import org.apache.hadoop.yarn.server.globalpolicygenerator.webapp.dao.GpgInfo; +import org.apache.hadoop.yarn.webapp.WebApp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Singleton +@Path("/ws/v1/gpg") +public class GPGWebServices { + + private static final Logger LOG = LoggerFactory.getLogger(GPGWebServices.class); + + private GlobalPolicyGenerator gpgGenerator; + private WebApp webapp; + + @Inject + public GPGWebServices(final GlobalPolicyGenerator gpg, final WebApp webapp) { + this.gpgGenerator = gpg; + this.webapp = webapp; + } + + @GET + @Produces({ MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8 }) + public GpgInfo get() { + return new GpgInfo(this.gpgGenerator.getGPGContext()); + } + + @GET + @Path("/info") + @Produces({ MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, + MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8 }) + public GpgInfo getGPGInfo() { + return new GpgInfo(this.gpgGenerator.getGPGContext()); + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/GpgInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/GpgInfo.java new file mode 100644 index 0000000000..a5c6c29e9f --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/GpgInfo.java @@ -0,0 +1,81 @@ +/** + * 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 applicable 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.server.globalpolicygenerator.webapp.dao; + +import org.apache.hadoop.util.VersionInfo; +import org.apache.hadoop.yarn.server.globalpolicygenerator.GPGContext; +import org.apache.hadoop.yarn.server.globalpolicygenerator.GlobalPolicyGenerator; +import org.apache.hadoop.yarn.util.YarnVersionInfo; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class GpgInfo { + private String gpgVersion; + private String gpgBuildVersion; + private String gpgVersionBuiltOn; + private String hadoopVersion; + private String hadoopBuildVersion; + private String hadoopVersionBuiltOn; + private long gpgStartupTime; + + public GpgInfo() { + } // JAXB needs this + + public GpgInfo(final GPGContext context) { + this.gpgVersion = YarnVersionInfo.getVersion(); + this.gpgBuildVersion = YarnVersionInfo.getBuildVersion(); + this.gpgVersionBuiltOn = YarnVersionInfo.getDate(); + this.hadoopVersion = VersionInfo.getVersion(); + this.hadoopBuildVersion = VersionInfo.getBuildVersion(); + this.hadoopVersionBuiltOn = VersionInfo.getDate(); + this.gpgStartupTime = GlobalPolicyGenerator.getGPGStartupTime(); + } + + public String getGpgVersion() { + return gpgVersion; + } + + public String getGpgBuildVersion() { + return gpgBuildVersion; + } + + public String getGpgVersionBuiltOn() { + return gpgVersionBuiltOn; + } + + public String getHadoopVersion() { + return hadoopVersion; + } + + public String getHadoopBuildVersion() { + return hadoopBuildVersion; + } + + public String getHadoopVersionBuiltOn() { + return hadoopVersionBuiltOn; + } + + public long getGpgStartupTime() { + return gpgStartupTime; + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/package-info.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/package-info.java new file mode 100644 index 0000000000..b687ba2890 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/dao/package-info.java @@ -0,0 +1,19 @@ +/** + * 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 applicable 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.server.globalpolicygenerator.webapp.dao; \ No newline at end of file diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/test/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/TestGPGWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/test/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/TestGPGWebServices.java new file mode 100644 index 0000000000..9e665e0c8b --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/test/java/org/apache/hadoop/yarn/server/globalpolicygenerator/webapp/TestGPGWebServices.java @@ -0,0 +1,96 @@ +/** * 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 applicable 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.server.globalpolicygenerator.webapp; + +import com.google.inject.Guice; +import com.google.inject.servlet.GuiceFilter; +import com.google.inject.servlet.ServletModule; +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.yarn.server.globalpolicygenerator.GPGContext; +import org.apache.hadoop.yarn.server.globalpolicygenerator.GlobalPolicyGenerator; +import org.apache.hadoop.yarn.server.resourcemanager.webapp.JAXBContextResolver; +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.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; +import org.junit.Before; +import org.junit.Test; + +import javax.ws.rs.core.MediaType; + +import static org.junit.Assert.assertNotNull; + +public class TestGPGWebServices extends JerseyTestBase { + private static GlobalPolicyGenerator gpg; + private static GPGWebApp webApp; + + private static class XWebServletModule extends ServletModule { + @Override + protected void configureServlets() { + bind(JAXBContextResolver.class); + bind(GPGWebServices.class); + bind(GenericExceptionHandler.class); + gpg = new GlobalPolicyGenerator(); + webApp = new GPGWebApp(gpg); + bind(WebApp.class).toInstance(webApp); + bind(GlobalPolicyGenerator.class).toInstance(gpg); + bind(GPGContext.class).toInstance(gpg.getGPGContext()); + serve("/*").with(GuiceContainer.class); + } + } + + static { + GuiceServletConfig.setInjector( + Guice.createInjector(new XWebServletModule())); + } + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + GuiceServletConfig.setInjector( + Guice.createInjector(new XWebServletModule())); + } + + public TestGPGWebServices() { + super(new WebAppDescriptor.Builder( + "org.apache.hadoop.yarn.server.globalpolicygenerator.webapp") + .contextListenerClass(GuiceServletConfig.class) + .filterClass(GuiceFilter.class) + .contextPath("jersey-guice-filter").servletPath("/").build()); + } + + @Test + public void testGetGPG() throws JSONException, Exception { + WebResource r = resource(); + JSONObject json = r.path("ws").path("v1").path("gpg") + .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + assertNotNull(json); + } + + @Test + public void testGetGPGInfo() throws JSONException, Exception { + WebResource r = resource(); + JSONObject json = r.path("ws").path("v1").path("gpg").path("info") + .accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + assertNotNull(json); + } +}