Return-Path: X-Original-To: apmail-hadoop-common-commits-archive@www.apache.org Delivered-To: apmail-hadoop-common-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 96745D3DC for ; Thu, 23 May 2013 22:22:21 +0000 (UTC) Received: (qmail 89027 invoked by uid 500); 23 May 2013 22:22:21 -0000 Delivered-To: apmail-hadoop-common-commits-archive@hadoop.apache.org Received: (qmail 88976 invoked by uid 500); 23 May 2013 22:22:21 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: common-dev@hadoop.apache.org Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 88969 invoked by uid 99); 23 May 2013 22:22:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 May 2013 22:22:21 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 May 2013 22:22:18 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 535CE2388906; Thu, 23 May 2013 22:21:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1485877 - /hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ExitUtil.java Date: Thu, 23 May 2013 22:21:57 -0000 To: common-commits@hadoop.apache.org From: jeagles@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130523222157.535CE2388906@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jeagles Date: Thu May 23 22:21:56 2013 New Revision: 1485877 URL: http://svn.apache.org/r1485877 Log: YARN-548. Add tests for YarnUncaughtExceptionHandler (Vadim Bondarev via jeagles) Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ExitUtil.java Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ExitUtil.java URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ExitUtil.java?rev=1485877&r1=1485876&r2=1485877&view=diff ============================================================================== --- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ExitUtil.java (original) +++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ExitUtil.java Thu May 23 22:21:56 2013 @@ -30,7 +30,9 @@ import org.apache.hadoop.classification. public final class ExitUtil { private final static Log LOG = LogFactory.getLog(ExitUtil.class.getName()); private static volatile boolean systemExitDisabled = false; + private static volatile boolean systemHaltDisabled = false; private static volatile ExitException firstExitException; + private static volatile HaltException firstHaltException; public static class ExitException extends RuntimeException { private static final long serialVersionUID = 1L; @@ -42,6 +44,16 @@ public final class ExitUtil { } } + public static class HaltException extends RuntimeException { + private static final long serialVersionUID = 1L; + public final int status; + + public HaltException(int status, String msg) { + super(msg); + this.status = status; + } + } + /** * Disable the use of System.exit for testing. */ @@ -50,6 +62,13 @@ public final class ExitUtil { } /** + * Disable the use of {@code Runtime.getRuntime().halt() } for testing. + */ + public static void disableSystemHalt() { + systemHaltDisabled = true; + } + + /** * @return true if terminate has been called */ public static boolean terminateCalled() { @@ -58,6 +77,13 @@ public final class ExitUtil { } /** + * @return true if halt has been called + */ + public static boolean haltCalled() { + return firstHaltException != null; + } + + /** * @return the first ExitException thrown, null if none thrown yet */ public static ExitException getFirstExitException() { @@ -65,20 +91,34 @@ public final class ExitUtil { } /** - * Reset the tracking of process termination. This is for use - * in unit tests where one test in the suite expects an exit - * but others do not. + * @return the first {@code HaltException} thrown, null if none thrown yet + */ + public static HaltException getFirstHaltException() { + return firstHaltException; + } + + /** + * Reset the tracking of process termination. This is for use in unit tests + * where one test in the suite expects an exit but others do not. */ public static void resetFirstExitException() { firstExitException = null; } + public static void resetFirstHaltException() { + firstHaltException = null; + } + /** * Terminate the current process. Note that terminate is the *only* method * that should be used to terminate the daemon processes. - * @param status exit code - * @param msg message used to create the ExitException - * @throws ExitException if System.exit is disabled for test purposes + * + * @param status + * exit code + * @param msg + * message used to create the {@code ExitException} + * @throws ExitException + * if System.exit is disabled for test purposes */ public static void terminate(int status, String msg) throws ExitException { LOG.info("Exiting with status " + status); @@ -94,22 +134,70 @@ public final class ExitUtil { } /** + * Forcibly terminates the currently running Java virtual machine. + * + * @param status + * exit code + * @param msg + * message used to create the {@code HaltException} + * @throws HaltException + * if Runtime.getRuntime().halt() is disabled for test purposes + */ + public static void halt(int status, String msg) throws HaltException { + LOG.info("Halt with status " + status + " Message: " + msg); + if (systemHaltDisabled) { + HaltException ee = new HaltException(status, msg); + LOG.fatal("Halt called", ee); + if (null == firstHaltException) { + firstHaltException = ee; + } + throw ee; + } + Runtime.getRuntime().halt(status); + } + + /** * Like {@link terminate(int, String)} but uses the given throwable to * initialize the ExitException. + * * @param status - * @param t throwable used to create the ExitException - * @throws ExitException if System.exit is disabled for test purposes + * @param t + * throwable used to create the ExitException + * @throws ExitException + * if System.exit is disabled for test purposes */ public static void terminate(int status, Throwable t) throws ExitException { terminate(status, StringUtils.stringifyException(t)); } /** + * Forcibly terminates the currently running Java virtual machine. + * + * @param status + * @param t + * @throws ExitException + */ + public static void halt(int status, Throwable t) throws HaltException { + halt(status, StringUtils.stringifyException(t)); + } + + /** * Like {@link terminate(int, String)} without a message. + * * @param status - * @throws ExitException if System.exit is disabled for test purposes + * @throws ExitException + * if System.exit is disabled for test purposes */ public static void terminate(int status) throws ExitException { terminate(status, "ExitException"); } + + /** + * Forcibly terminates the currently running Java virtual machine. + * @param status + * @throws ExitException + */ + public static void halt(int status) throws HaltException { + halt(status, "HaltException"); + } }