ResourceManager中setDefaultUncaughtExceptionHandler做了那些事情

总结一下:

Thread.setDefaultUncaughtExceptionHandler(new YarnUncaughtExceptionHandler());

方法主要记录,异常日志信息,并且记录完成后退出。

 

public static void main(String argv[]) {
  Thread.setDefaultUncaughtExceptionHandler(new YarnUncaughtExceptionHandler());
  StringUtils.startupShutdownMessage(ResourceManager.class, argv, LOG);
… 

}

YarnUncaughtExceptionHandler类主要结构,其实就是记录LOG

public class YarnUncaughtExceptionHandler implements UncaughtExceptionHandler {
  private static final Log LOG = LogFactory.getLog(YarnUncaughtExceptionHandler.class);
 
  @Override
  public void uncaughtException(Thread t, Throwable e) {
    if(ShutdownHookManager.get().isShutdownInProgress()) {   //如果RM在关闭过程中…
      LOG.error("Thread " + t + " threw an Throwable, but we are shutting " +
              "down, so ignoring this", e);
    } else if(e instanceof Error) {     // log不同的日志
      try {
        LOG.fatal("Thread " + t + " threw an Error.  Shutting down now...", e);
      } catch (Throwable err) {
        //We don't want to not exit because of an issue with logging
      }
     if(e instanceof OutOfMemoryError) {    // OOM
        //After catching an OOM java says it is undefined behavior, so don't
        //even try to clean up or we can get stuck on shutdown.
        try {
          System.err.println("Halting due to Out Of Memory Error...");
        } catch (Throwable err) {
          //Again we done want to exit because of logging issues.
        }
        ExitUtil.halt(-1);    // 如果是OOM异常那么就runtime halt退出
      } else {
       ExitUtil.terminate(-1);   // 如果不是OOM异常那么System exit退出
      }
    } else {
      LOG.error("Thread " + t + " threw an Exception.", e);
    }
  }
}

 

ExitUtil退出工具,主要的方法就是两个:

这里用到了,两种退出方法,具体有什么区别呢,单写一篇blog来看吧。

Runtime.getRuntime().halt(status);
System.exit(status);

 

终止守护进程方法

 

/**
* 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 {@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);
  if (systemExitDisabled) {
    ExitException ee = new ExitException(status, msg);
    LOG.fatal("Terminate called", ee);
    if (null == firstExitException) {
      firstExitException = ee;
    }
    throw ee;
  }
  System.exit(status);
}

 

终止当前的JVM

/**
* 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);
}

posted @ 2015-08-18 11:44  闫昆  阅读(496)  评论(0编辑  收藏  举报