001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.common.launch; 020 021import org.apache.reef.tang.Configuration; 022import org.apache.reef.tang.Tang; 023import org.apache.reef.tang.exceptions.InjectionException; 024 025import javax.inject.Inject; 026import java.util.logging.Level; 027import java.util.logging.Logger; 028 029/** 030 * This is used as the Exception handler for REEF client processes (Driver, Evaluator). 031 * <p> 032 * It catches all exceptions and sends them to the controlling process. 033 * For Evaluators, that is the Driver. For the Driver, that is the Client. 034 * <p> 035 * After sending the exception, this shuts down the JVM, as this JVM is then officially dead. 036 */ 037public final class REEFUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { 038 private static final Logger LOG = Logger.getLogger(REEFUncaughtExceptionHandler.class.getName()); 039 private final Configuration errorHandlerConfig; 040 041 private REEFErrorHandler errorHandler; 042 043 /** 044 * @param errorHandlerConfig 045 */ 046 @Inject 047 public REEFUncaughtExceptionHandler(final Configuration errorHandlerConfig) { 048 this.errorHandlerConfig = errorHandlerConfig; 049 this.errorHandler = null; 050 } 051 052 @Override 053 public synchronized void uncaughtException(final Thread thread, final Throwable throwable) { 054 055 final String msg = "Thread " + thread.getName() + " threw an uncaught exception."; 056 LOG.log(Level.SEVERE, msg, throwable); 057 058 if (this.errorHandler == null) { 059 try { 060 this.errorHandler = Tang.Factory.getTang() 061 .newInjector(this.errorHandlerConfig).getInstance(REEFErrorHandler.class); 062 } catch (final InjectionException ie) { 063 LOG.log(Level.WARNING, "Unable to inject error handler."); 064 } 065 } 066 067 if (this.errorHandler != null) { 068 this.errorHandler.onNext(new Exception(msg, throwable)); 069 try { 070 this.wait(100); 071 } catch (final InterruptedException expected) { 072 // try-catch block used to wait and give process a chance to setup communication with its parent 073 } 074 this.errorHandler.close(); 075 } 076 077 LOG.log(Level.SEVERE, msg + " System.exit(1)"); 078 079 System.exit(1); 080 } 081 082 @Override 083 public String toString() { 084 return "REEFUncaughtExceptionHandler{errorHandler=" + this.errorHandler + '}'; 085 } 086}