diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/YarnCriticalThreadUncaughtExceptionHandler.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/YarnCriticalThreadUncaughtExceptionHandler.java new file mode 100644 index 0000000..20941af --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/YarnCriticalThreadUncaughtExceptionHandler.java @@ -0,0 +1,47 @@ +/** + * 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; + +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; +import org.apache.hadoop.util.ExitUtil; + +/** + * This class is intended to be installed by calling + * {@code setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)} + * in the thread entry point or before the thread starts. It is intended to + * shut down the program while any uncaught exception causes the thread crash. + */ +@Public +@Evolving +public class YarnCriticalThreadUncaughtExceptionHandler + implements UncaughtExceptionHandler { + private static final Log LOG = LogFactory.getLog( + YarnCriticalThreadUncaughtExceptionHandler.class); + + @Override + public void uncaughtException(Thread t, Throwable e) { + LOG.fatal("Critical Thread " + t + " crashed. Terminate the program.", e); + ExitUtil.terminate(-1); + } +} \ No newline at end of file diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnCriticalThreadUncaughtExceptionHandler.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnCriticalThreadUncaughtExceptionHandler.java new file mode 100644 index 0000000..a003106 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestYarnCriticalThreadUncaughtExceptionHandler.java @@ -0,0 +1,65 @@ +/** + * 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; + +import org.apache.hadoop.util.ExitUtil; +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 YarnCriticalThreadUncaughtExceptionHandler}. + */ +public class TestYarnCriticalThreadUncaughtExceptionHandler { + + private static final YarnCriticalThreadUncaughtExceptionHandler exHandler = + new YarnCriticalThreadUncaughtExceptionHandler(); + + /** + * Throw {@link RuntimeException} inside thread and + * check {@link YarnCriticalThreadUncaughtExceptionHandler} 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(); + final YarnCriticalThreadUncaughtExceptionHandler spyRTEHandler = + spy(exHandler); + final RuntimeException rte= new RuntimeException("test-RuntimeException"); + final Thread testThread = new Thread(new Runnable() { + @Override + public void run() { + throw rte; + } + }); + + testThread.setUncaughtExceptionHandler(spyRTEHandler); + assertSame(spyRTEHandler, testThread.getUncaughtExceptionHandler()); + testThread.start(); + testThread.join(); + verify(spyRTEHandler).uncaughtException(testThread, rte); + } +}