diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMCriticalThreadUncaughtExceptionHandler.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMCriticalThreadUncaughtExceptionHandler.java new file mode 100644 index 0000000..fbc8086 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMCriticalThreadUncaughtExceptionHandler.java @@ -0,0 +1,54 @@ +/** + * 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.resourcemanager; + +import java.lang.Thread.UncaughtExceptionHandler; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.classification.InterfaceAudience.Public; +import org.apache.hadoop.classification.InterfaceStability.Evolving; + +/** + * This class sends a {@link RMFatalEvent} to {@link ResourceManager} if any + * uncaught exception causes critical threads crash. It is intended to be + * installed by calling + * {@code setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)} + * in the thread entry point or after creation of threads. + */ +@Public +@Evolving +public class RMCriticalThreadUncaughtExceptionHandler + implements UncaughtExceptionHandler { + private static final Log LOG = LogFactory.getLog( + RMCriticalThreadUncaughtExceptionHandler.class); + private RMContext rmContext; + + public RMCriticalThreadUncaughtExceptionHandler(RMContext rmContext) { + this.rmContext = rmContext; + } + + @Override + public void uncaughtException(Thread t, Throwable e) { + LOG.fatal("Critical thread " + t + " crashed!", e); + rmContext.getDispatcher().getEventHandler().handle( + new RMFatalEvent(RMFatalEventType.CRITICAL_THREAD_CRASH, + new Exception(e))); + } +} \ No newline at end of file diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMFatalEventType.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMFatalEventType.java index 87cc496..b6f6b3c 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMFatalEventType.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMFatalEventType.java @@ -29,5 +29,8 @@ EMBEDDED_ELECTOR_FAILED, // Source <- Admin Service - TRANSITION_TO_ACTIVE_FAILED + TRANSITION_TO_ACTIVE_FAILED, + + // Source <- Critical Thread Crash + CRITICAL_THREAD_CRASH } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMCriticalThreadUncaughtExceptionHandler.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMCriticalThreadUncaughtExceptionHandler.java new file mode 100644 index 0000000..cd8688f --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMCriticalThreadUncaughtExceptionHandler.java @@ -0,0 +1,75 @@ +/** + * 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.resourcemanager; + +import org.apache.hadoop.util.ExitUtil; +import org.apache.hadoop.yarn.event.AsyncDispatcher; +import org.junit.Test; + +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +/** + * This class is to test {@link RMCriticalThreadUncaughtExceptionHandler}. + */ +public class TestRMCriticalThreadUncaughtExceptionHandler { + /** + * Throw {@link RuntimeException} inside thread and + * check {@link RMCriticalThreadUncaughtExceptionHandler} instance. + * + * Used {@link ExitUtil} class to avoid jvm exit through + * {@code System.exit(-1)}. + * + * @throws InterruptedException if any + */ + @Test + public void testUncaughtExceptionHandlerWithError() + throws InterruptedException { + ExitUtil.disableSystemExit(); + + // Create a MockRM and start it + ResourceManager resourceManager = new MockRM(); + ((AsyncDispatcher)resourceManager.getRMContext().getDispatcher()).start(); + resourceManager.getRMContext().getStateStore().start(); + resourceManager.getRMContext().getContainerTokenSecretManager().rollMasterKey(); + + final RMCriticalThreadUncaughtExceptionHandler exHandler = + new RMCriticalThreadUncaughtExceptionHandler( + resourceManager.getRMContext()); + final RMCriticalThreadUncaughtExceptionHandler spyRTEHandler = + spy(exHandler); + + // Create a thread and throw a RTE inside it + final RuntimeException rte = new RuntimeException("TestRuntimeException"); + final Thread testThread = new Thread(new Runnable() { + @Override + public void run() { + throw rte; + } + }); + testThread.setName("TestThread"); + testThread.setUncaughtExceptionHandler(spyRTEHandler); + assertSame(spyRTEHandler, testThread.getUncaughtExceptionHandler()); + testThread.start(); + testThread.join(); + + verify(spyRTEHandler).uncaughtException(testThread, rte); + } +} \ No newline at end of file