This is an automated email from the ASF dual-hosted git repository. eyang pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/hadoop.git
The following commit(s) were added to refs/heads/trunk by this push: new fab5b80 YARN-9554. Fixed TimelineEntity DAO serialization handling. Contributed by Prabhu Joseph fab5b80 is described below commit fab5b80a36bad90e03f7e5e37ded47d67d6e2e81 Author: Eric Yang <ey...@apache.org> AuthorDate: Thu May 16 16:35:54 2019 -0400 YARN-9554. Fixed TimelineEntity DAO serialization handling. Contributed by Prabhu Joseph --- .../webapp/ContextFactory.java | 91 +++++++++++++++++++--- .../webapp/TestAHSWebServices.java | 11 --- .../timeline/webapp/TestTimelineWebServices.java | 21 +++++ 3 files changed, 102 insertions(+), 21 deletions(-) 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/ContextFactory.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/ContextFactory.java index 67668a9..ff52324 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/ContextFactory.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/ContextFactory.java @@ -1,3 +1,4 @@ + /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,34 +19,104 @@ package org.apache.hadoop.yarn.server.applicationhistoryservice.webapp; +import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity; +import org.apache.hadoop.yarn.api.records.timeline.TimelineEntities; +import org.apache.hadoop.yarn.api.records.timeline.TimelineDomain; +import org.apache.hadoop.yarn.api.records.timeline.TimelineDomains; +import org.apache.hadoop.yarn.api.records.timeline.TimelineEvents; +import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse; +import org.apache.hadoop.yarn.server.webapp.dao.AppInfo; +import org.apache.hadoop.yarn.server.webapp.dao.AppsInfo; +import org.apache.hadoop.yarn.server.webapp.dao.AppAttemptInfo; +import org.apache.hadoop.yarn.server.webapp.dao.AppAttemptsInfo; +import org.apache.hadoop.yarn.server.webapp.dao.ContainerInfo; +import org.apache.hadoop.yarn.server.webapp.dao.ContainersInfo; +import org.apache.hadoop.yarn.webapp.RemoteExceptionData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Set; +import java.util.HashSet; +import java.util.Arrays; import java.util.Map; import java.lang.reflect.Method; import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; /** * ContextFactory to reuse JAXBContextImpl for DAO Classes. */ public final class ContextFactory { - private static JAXBContext jaxbContext; + private static final Logger LOG = + LoggerFactory.getLogger(ContextFactory.class); + + private static JAXBContext cacheContext; + + // All the dao classes from TimelineWebService and AHSWebService + // added except TimelineEntity and TimelineEntities + private static final Class[] CTYPES = {AppInfo.class, AppsInfo.class, + AppAttemptInfo.class, AppAttemptsInfo.class, ContainerInfo.class, + ContainersInfo.class, RemoteExceptionData.class, TimelineDomain.class, + TimelineDomains.class, TimelineEvents.class, TimelinePutResponse.class}; + private static final Set<Class> CLASS_SET = + new HashSet<>(Arrays.asList(CTYPES)); + + // TimelineEntity has java.util.Set interface which JAXB + // can't handle and throws IllegalAnnotationExceptions + private static final Class[] IGNORE_TYPES = {TimelineEntity.class, + TimelineEntities.class}; + private static final Set<Class> IGNORE_SET = + new HashSet<>(Arrays.asList(IGNORE_TYPES)); + + private static JAXBException je = + new JAXBException("TimelineEntity and TimelineEntities has " + + "IllegalAnnotation"); + + private static StackTraceElement[] stackTrace = new StackTraceElement[]{ + new StackTraceElement(ContextFactory.class.getName(), + "createContext", "ContextFactory.java", -1)}; private ContextFactory() { } + public static JAXBContext newContext(Class[] classes, + Map<String, Object> properties) throws Exception { + Class spFactory = Class.forName( + "com.sun.xml.internal.bind.v2.ContextFactory"); + Method m = spFactory.getMethod("createContext", Class[].class, Map.class); + return (JAXBContext) m.invoke((Object) null, classes, properties); + } + // Called from WebComponent.service public static JAXBContext createContext(Class[] classes, Map<String, Object> properties) throws Exception { - synchronized (ContextFactory.class) { - if (jaxbContext == null) { - Class spFactory = Class.forName( - "com.sun.xml.internal.bind.v2.ContextFactory"); - Method m = spFactory.getMethod("createContext", Class[].class, - Map.class); - jaxbContext = (JAXBContext) m.invoke((Object) null, classes, - properties); + for (Class c : classes) { + if (IGNORE_SET.contains(c)) { + je.setStackTrace(stackTrace); + throw je; + } + if (!CLASS_SET.contains(c)) { + try { + return newContext(classes, properties); + } catch (Exception e) { + LOG.warn("Error while Creating JAXBContext", e); + throw e; + } + } + } + + try { + synchronized (ContextFactory.class) { + if (cacheContext == null) { + cacheContext = newContext(CTYPES, properties); + } } + } catch(Exception e) { + LOG.warn("Error while Creating JAXBContext", e); + throw e; } - return jaxbContext; + return cacheContext; } // Called from WebComponent.init 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 65bd005..492e453 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 @@ -29,14 +29,12 @@ import java.net.URI; import java.net.URL; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Properties; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; -import javax.xml.bind.JAXBContext; import javax.ws.rs.core.MediaType; import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.conf.Configuration; @@ -952,15 +950,6 @@ public class TestAHSWebServices extends JerseyTestBase { String.valueOf(content.length())); } - @Test - public void testContextFactory() throws Exception { - JAXBContext jaxbContext1 = ContextFactory.createContext( - new Class[]{}, Collections.EMPTY_MAP); - JAXBContext jaxbContext2 = ContextFactory.createContext( - new Class[]{}, Collections.EMPTY_MAP); - assertEquals(jaxbContext1, jaxbContext2); - } - private static String getRedirectURL(String url) { String redirectUrl = null; try { 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 d8161bb..c8f9eb2 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 @@ -19,6 +19,7 @@ package org.apache.hadoop.yarn.server.timeline.webapp; import static org.apache.hadoop.yarn.webapp.WebServicesTestUtils.assertResponseStatusCode; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; @@ -38,6 +39,8 @@ import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response.Status; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.JettyUtils; @@ -55,6 +58,7 @@ import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse.TimelineP import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.security.AdminACLsManager; import org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier; +import org.apache.hadoop.yarn.server.applicationhistoryservice.webapp.ContextFactory; import org.apache.hadoop.yarn.server.timeline.TestMemoryTimelineStore; import org.apache.hadoop.yarn.server.timeline.TimelineDataManager; import org.apache.hadoop.yarn.server.timeline.TimelineStore; @@ -1030,6 +1034,23 @@ public class TestTimelineWebServices extends JerseyTestBase { } } + @Test + public void testContextFactory() throws Exception { + JAXBContext jaxbContext1 = ContextFactory.createContext( + new Class[]{TimelineDomain.class}, Collections.EMPTY_MAP); + JAXBContext jaxbContext2 = ContextFactory.createContext( + new Class[]{TimelineDomain.class}, Collections.EMPTY_MAP); + assertEquals(jaxbContext1, jaxbContext2); + + try { + ContextFactory.createContext(new Class[]{TimelineEntity.class}, + Collections.EMPTY_MAP); + Assert.fail("Expected JAXBException"); + } catch(Exception e) { + assertThat(e).isExactlyInstanceOf(JAXBException.class); + } + } + private static void verifyDomain(TimelineDomain domain, String domainId) { Assert.assertNotNull(domain); Assert.assertEquals(domainId, domain.getId()); --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org For additional commands, e-mail: common-commits-h...@hadoop.apache.org