1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.hbase.errorhandling; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.hadoop.hbase.classification.InterfaceAudience; 26 27 /** 28 * The dispatcher acts as the state holding entity for foreign error handling. The first 29 * exception received by the dispatcher get passed directly to the listeners. Subsequent 30 * exceptions are dropped. 31 * <p> 32 * If there are multiple dispatchers that are all in the same foreign exception monitoring group, 33 * ideally all these monitors are "peers" -- any error on one dispatcher should get propagated to 34 * all others (via rpc, or some other mechanism). Due to racing error conditions the exact reason 35 * for failure may be different on different peers, but the fact that they are in error state 36 * should eventually hold on all. 37 * <p> 38 * This is thread-safe and must be because this is expected to be used to propagate exceptions 39 * from foreign threads. 40 */ 41 @InterfaceAudience.Private 42 public class ForeignExceptionDispatcher implements ForeignExceptionListener, ForeignExceptionSnare { 43 private static final Log LOG = LogFactory.getLog(ForeignExceptionDispatcher.class); 44 protected final String name; 45 protected final List<ForeignExceptionListener> listeners = 46 new ArrayList<ForeignExceptionListener>(); 47 private ForeignException exception; 48 49 public ForeignExceptionDispatcher(String name) { 50 this.name = name; 51 } 52 53 public ForeignExceptionDispatcher() { 54 this(""); 55 } 56 57 public String getName() { 58 return name; 59 } 60 61 @Override 62 public synchronized void receive(ForeignException e) { 63 // if we already have an exception, then ignore it 64 if (exception != null) return; 65 66 LOG.debug(name + " accepting received exception" , e); 67 // mark that we got the error 68 if (e != null) { 69 exception = e; 70 } else { 71 exception = new ForeignException(name, ""); 72 } 73 74 // notify all the listeners 75 dispatch(e); 76 } 77 78 @Override 79 public synchronized void rethrowException() throws ForeignException { 80 if (exception != null) { 81 // This gets the stack where this is caused, (instead of where it was deserialized). 82 // This is much more useful for debugging 83 throw new ForeignException(exception.getSource(), exception.getCause()); 84 } 85 } 86 87 @Override 88 public synchronized boolean hasException() { 89 return exception != null; 90 } 91 92 @Override 93 synchronized public ForeignException getException() { 94 return exception; 95 } 96 97 /** 98 * Sends an exception to all listeners. 99 * @param message human readable message passed to the listener 100 * @param e {@link ForeignException} containing the cause. Can be null. 101 */ 102 private void dispatch(ForeignException e) { 103 // update all the listeners with the passed error 104 for (ForeignExceptionListener l: listeners) { 105 l.receive(e); 106 } 107 } 108 109 /** 110 * Listen for failures to a given process. This method should only be used during 111 * initialization and not added to after exceptions are accepted. 112 * @param errorable listener for the errors. may be null. 113 */ 114 public synchronized void addListener(ForeignExceptionListener errorable) { 115 this.listeners.add(errorable); 116 } 117 }