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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.errorhandling;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * The dispatcher acts as the state holding entity for foreign error handling.  The first
029 * exception received by the dispatcher get passed directly to the listeners.  Subsequent
030 * exceptions are dropped.
031 * <p>
032 * If there are multiple dispatchers that are all in the same foreign exception monitoring group,
033 * ideally all these monitors are "peers" -- any error on one dispatcher should get propagated to
034 * all others (via rpc, or some other mechanism).  Due to racing error conditions the exact reason
035 * for failure may be different on different peers, but the fact that they are in error state
036 * should eventually hold on all.
037 * <p>
038 * This is thread-safe and must be because this is expected to be used to propagate exceptions
039 * from foreign threads.
040 */
041@InterfaceAudience.Private
042public class ForeignExceptionDispatcher implements ForeignExceptionListener, ForeignExceptionSnare {
043  private static final Logger LOG = LoggerFactory.getLogger(ForeignExceptionDispatcher.class);
044  protected final String name;
045  protected final List<ForeignExceptionListener> listeners = new ArrayList<>();
046  private ForeignException exception;
047
048  public ForeignExceptionDispatcher(String name) {
049    this.name = name;
050  }
051
052  public ForeignExceptionDispatcher() {
053    this("");
054  }
055
056  public String getName() {
057    return name;
058  }
059
060  @Override
061  public synchronized void receive(ForeignException e) {
062    // if we already have an exception, then ignore it
063    if (exception != null) return;
064
065    LOG.debug(name + " accepting received exception" , e);
066    // mark that we got the error
067    if (e != null) {
068      exception = e;
069    } else {
070      exception = new ForeignException(name, "");
071    }
072
073    // notify all the listeners
074    dispatch(e);
075  }
076
077  @Override
078  public synchronized void rethrowException() throws ForeignException {
079    if (exception != null) {
080      // This gets the stack where this is caused, (instead of where it was deserialized).
081      // This is much more useful for debugging
082      throw new ForeignException(exception.getSource(), exception.getCause());
083    }
084  }
085
086  @Override
087  public synchronized boolean hasException() {
088    return exception != null;
089  }
090
091  @Override
092  synchronized public ForeignException getException() {
093    return exception;
094  }
095
096  /**
097   * Sends an exception to all listeners.
098   * @param e {@link ForeignException} containing the cause.  Can be null.
099   */
100  private void dispatch(ForeignException e) {
101    // update all the listeners with the passed error
102    for (ForeignExceptionListener l: listeners) {
103      l.receive(e);
104    }
105  }
106
107  /**
108   * Listen for failures to a given process.  This method should only be used during
109   * initialization and not added to after exceptions are accepted.
110   * @param errorable listener for the errors.  may be null.
111   */
112  public synchronized void addListener(ForeignExceptionListener errorable) {
113    this.listeners.add(errorable);
114  }
115}