YARN-9554. Fixed TimelineEntity DAO serialization handling.

Contributed by Prabhu Joseph
This commit is contained in:
Eric Yang 2019-05-16 16:35:54 -04:00
parent 03ea8ea92e
commit fab5b80a36
3 changed files with 102 additions and 21 deletions

View File

@ -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;
}
}
}
return jaxbContext;
try {
synchronized (ContextFactory.class) {
if (cacheContext == null) {
cacheContext = newContext(CTYPES, properties);
}
}
} catch(Exception e) {
LOG.warn("Error while Creating JAXBContext", e);
throw e;
}
return cacheContext;
}
// Called from WebComponent.init

View File

@ -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 {

View File

@ -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());