From c35b3be700ab61ef86520265d2fd9ba03ba32ea3 Mon Sep 17 00:00:00 2001 From: Eric Yang Date: Tue, 5 Mar 2019 12:17:01 -0500 Subject: [PATCH] YARN-7266. Fixed deadlock in Timeline Server thread initialization. Contributed by Prabhu Joseph (cherry picked from commit 7b42e0e32ac7dfb60f25fa656a9bef69c2a62501) --- .../hadoop-yarn/hadoop-yarn-api/pom.xml | 6 ++ .../yarn/api/records/timeline/jaxb.properties | 13 ++++ .../webapp/ContextFactory.java | 62 +++++++++++++++++++ .../webapp/TestAHSWebServices.java | 10 +++ 4 files changed, 91 insertions(+) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/resources/org/apache/hadoop/yarn/api/records/timeline/jaxb.properties create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/ContextFactory.java 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 a070cafe83f..f17a13d9510 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml @@ -91,6 +91,12 @@ false + + src/main/resources + + **/jaxb.properties + + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/resources/org/apache/hadoop/yarn/api/records/timeline/jaxb.properties b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/resources/org/apache/hadoop/yarn/api/records/timeline/jaxb.properties new file mode 100644 index 00000000000..8e545b31cc3 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/resources/org/apache/hadoop/yarn/api/records/timeline/jaxb.properties @@ -0,0 +1,13 @@ +# Licensed 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. + +javax.xml.bind.context.factory=org.apache.hadoop.yarn.server.applicationhistoryservice.webapp.ContextFactory 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 new file mode 100644 index 00000000000..67668a9b367 --- /dev/null +++ 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 @@ -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.applicationhistoryservice.webapp; + +import java.util.Map; +import java.lang.reflect.Method; +import javax.xml.bind.JAXBContext; + +/** + * ContextFactory to reuse JAXBContextImpl for DAO Classes. + */ +public final class ContextFactory { + + private static JAXBContext jaxbContext; + + private ContextFactory() { + } + + // 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); + } + } + return jaxbContext; + } + + // Called from WebComponent.init + public static JAXBContext createContext(String contextPath, ClassLoader + classLoader, Map properties) throws Exception { + Class spFactory = Class.forName( + "com.sun.xml.internal.bind.v2.ContextFactory"); + Method m = spFactory.getMethod("createContext", String.class, + ClassLoader.class, Map.class); + return (JAXBContext) m.invoke(null, contextPath, classLoader, + properties); + } + +} 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 495e87a95f7..a461103b10f 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 @@ -39,6 +39,7 @@ 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; @@ -962,6 +963,15 @@ 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 {