From 29a0db368c65e3aad31f55554d6a1a53ba2aa64d Mon Sep 17 00:00:00 2001 From: Prabhu Joseph Date: Wed, 15 May 2019 17:03:53 +0530 Subject: [PATCH] YARN-9554 --- .../webapp/ContextFactory.java | 86 +++++++++++++++++++--- .../webapp/TestAHSWebServices.java | 11 --- .../timeline/webapp/TestTimelineWebServices.java | 21 ++++++ 3 files changed, 97 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..69deb42 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,99 @@ 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 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 JAXBContext cacheContext; private ContextFactory() { } + // 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_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 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)}; + + public static JAXBContext newContext(Class[] classes, + Map 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 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) { + e.printStackTrace(); + throw e; + } + } + } + + try { + synchronized (ContextFactory.class) { + if (cacheContext == null) { + cacheContext = newContext(CTYPES, properties); + } } + } catch(Exception e) { + e.printStackTrace(); + 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.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 void testContainerLogsMetaForFinishedApps() throws Exception { 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.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.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 void testPutDomainYarnACLsEnabled() throws Exception { } } + @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()); -- 2.7.4 (Apple Git-66)