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