View Javadoc

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.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  
28  /**
29   * The dispatcher acts as the state holding entity for foreign error handling.  The first
30   * exception received by the dispatcher get passed directly to the listeners.  Subsequent
31   * exceptions are dropped.
32   * <p>
33   * If there are multiple dispatchers that are all in the same foreign exception monitoring group,
34   * ideally all these monitors are "peers" -- any error on one dispatcher should get propagated to
35   * all others (via rpc, or some other mechanism).  Due to racing error conditions the exact reason
36   * for failure may be different on different peers, but the fact that they are in error state
37   * should eventually hold on all.
38   * <p>
39   * This is thread-safe and must be because this is expected to be used to propagate exceptions
40   * from foreign threads.
41   */
42  @InterfaceAudience.Public
43  @InterfaceStability.Evolving
44  public class ForeignExceptionDispatcher implements ForeignExceptionListener, ForeignExceptionSnare {
45    public static final Log LOG = LogFactory.getLog(ForeignExceptionDispatcher.class);
46    protected final String name;
47    protected final List<ForeignExceptionListener> listeners =
48        new ArrayList<ForeignExceptionListener>();
49    private ForeignException exception;
50  
51    public ForeignExceptionDispatcher(String name) {
52      this.name = name;
53    }
54  
55    public ForeignExceptionDispatcher() {
56      this("");
57    }
58  
59    public String getName() {
60      return name;
61    }
62  
63    @Override
64    public synchronized void receive(ForeignException e) {
65      // if we already have an exception, then ignore it
66      if (exception != null) return;
67  
68      LOG.debug(name + " accepting received exception" , e);
69      // mark that we got the error
70      if (e != null) {
71        exception = e;
72      } else {
73        exception = new ForeignException(name, "");
74      }
75  
76      // notify all the listeners
77      dispatch(e);
78    }
79  
80    @Override
81    public synchronized void rethrowException() throws ForeignException {
82      if (exception != null) {
83        // This gets the stack where this is caused, (instead of where it was deserialized).
84        // This is much more useful for debugging
85        throw new ForeignException(exception.getSource(), exception.getCause());
86      }
87    }
88  
89    @Override
90    public synchronized boolean hasException() {
91      return exception != null;
92    }
93  
94    @Override
95    synchronized public ForeignException getException() {
96      return exception;
97    }
98  
99    /**
100    * Sends an exception to all listeners.
101    * @param message human readable message passed to the listener
102    * @param e {@link ForeignException} containing the cause.  Can be null.
103    */
104   private void dispatch(ForeignException e) {
105     // update all the listeners with the passed error
106     for (ForeignExceptionListener l: listeners) {
107       l.receive(e);
108     }
109   }
110 
111   /**
112    * Listen for failures to a given process.  This method should only be used during
113    * initialization and not added to after exceptions are accepted.
114    * @param errorable listener for the errors.  may be null.
115    */
116   public synchronized void addListener(ForeignExceptionListener errorable) {
117     this.listeners.add(errorable);
118   }
119 }