View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.exceptions;
21  
22  import org.apache.hadoop.hbase.CallDroppedException;
23  import org.apache.hadoop.hbase.CallQueueTooBigException;
24  import org.apache.hadoop.hbase.RegionTooBusyException;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.quotas.ThrottlingException;
28  import org.apache.hadoop.ipc.RemoteException;
29  
30  @InterfaceAudience.Private
31  @InterfaceStability.Evolving
32  public final class ClientExceptionsUtil {
33  
34    private ClientExceptionsUtil() {}
35  
36    public static boolean isMetaClearingException(Throwable cur) {
37      cur = findException(cur);
38  
39      if (cur == null) {
40        return true;
41      }
42      return !isSpecialException(cur) || (cur instanceof RegionMovedException);
43    }
44  
45    public static boolean isSpecialException(Throwable cur) {
46      return (cur instanceof RegionMovedException || cur instanceof RegionOpeningException
47          || cur instanceof RegionTooBusyException || cur instanceof ThrottlingException
48          || cur instanceof CallQueueTooBigException || cur instanceof CallDroppedException);
49    }
50  
51    /**
52     * Checks if the exception is CallQueueTooBig exception (maybe wrapped
53     * into some RemoteException).
54     * @param t exception to check
55     * @return true if it's a CQTBE, false otherwise
56     */
57    public static boolean isCallQueueTooBigException(Throwable t) {
58      t = findException(t);
59      return (t instanceof CallQueueTooBigException);
60    }
61  
62    /**
63     * Checks if the exception is CallDroppedException (maybe wrapped
64     * into some RemoteException).
65     * @param t exception to check
66     * @return true if it's a CQTBE, false otherwise
67     */
68    public static boolean isCallDroppedException(Throwable t) {
69      t = findException(t);
70      return (t instanceof CallDroppedException);
71    }
72  
73    /**
74     * Look for an exception we know in the remote exception:
75     * - hadoop.ipc wrapped exceptions
76     * - nested exceptions
77     *
78     * Looks for: RegionMovedException / RegionOpeningException / RegionTooBusyException /
79     *            ThrottlingException
80     * @return null if we didn't find the exception, the exception otherwise.
81     */
82    public static Throwable findException(Object exception) {
83      if (exception == null || !(exception instanceof Throwable)) {
84        return null;
85      }
86      Throwable cur = (Throwable) exception;
87      while (cur != null) {
88        if (isSpecialException(cur)) {
89          return cur;
90        }
91        if (cur instanceof RemoteException) {
92          RemoteException re = (RemoteException) cur;
93          cur = re.unwrapRemoteException(
94              RegionOpeningException.class, RegionMovedException.class,
95              RegionTooBusyException.class);
96          if (cur == null) {
97            cur = re.unwrapRemoteException();
98          }
99          // unwrapRemoteException can return the exception given as a parameter when it cannot
100         //  unwrap it. In this case, there is no need to look further
101         // noinspection ObjectEquality
102         if (cur == re) {
103           return cur;
104         }
105       } else if (cur.getCause() != null) {
106         cur = cur.getCause();
107       } else {
108         return cur;
109       }
110     }
111 
112     return null;
113   }
114 }