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.CallQueueTooBigException;
23  import org.apache.hadoop.hbase.MultiActionResultTooLarge;
24  import org.apache.hadoop.hbase.RegionTooBusyException;
25  import org.apache.hadoop.hbase.RetryImmediatelyException;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.quotas.ThrottlingException;
29  import org.apache.hadoop.ipc.RemoteException;
30  
31  @InterfaceAudience.Private
32  @InterfaceStability.Evolving
33  public final class ClientExceptionsUtil {
34  
35    private ClientExceptionsUtil() {}
36  
37    public static boolean isMetaClearingException(Throwable cur) {
38      cur = findException(cur);
39  
40      if (cur == null) {
41        return true;
42      }
43      return !isSpecialException(cur) || (cur instanceof RegionMovedException);
44    }
45  
46    public static boolean isSpecialException(Throwable cur) {
47      return (cur instanceof RegionMovedException || cur instanceof RegionOpeningException
48          || cur instanceof RegionTooBusyException || cur instanceof ThrottlingException
49          || cur instanceof MultiActionResultTooLarge || cur instanceof RetryImmediatelyException
50          || cur instanceof CallQueueTooBigException);
51    }
52  
53  
54    /**
55     * Look for an exception we know in the remote exception:
56     * - hadoop.ipc wrapped exceptions
57     * - nested exceptions
58     *
59     * Looks for: RegionMovedException / RegionOpeningException / RegionTooBusyException /
60     *            ThrottlingException
61     * @return null if we didn't find the exception, the exception otherwise.
62     */
63    public static Throwable findException(Object exception) {
64      if (exception == null || !(exception instanceof Throwable)) {
65        return null;
66      }
67      Throwable cur = (Throwable) exception;
68      while (cur != null) {
69        if (isSpecialException(cur)) {
70          return cur;
71        }
72        if (cur instanceof RemoteException) {
73          RemoteException re = (RemoteException) cur;
74          cur = re.unwrapRemoteException(
75              RegionOpeningException.class, RegionMovedException.class,
76              RegionTooBusyException.class);
77          if (cur == null) {
78            cur = re.unwrapRemoteException();
79          }
80          // unwrapRemoteException can return the exception given as a parameter when it cannot
81          //  unwrap it. In this case, there is no need to look further
82          // noinspection ObjectEquality
83          if (cur == re) {
84            return cur;
85          }
86        } else if (cur.getCause() != null) {
87          cur = cur.getCause();
88        } else {
89          return cur;
90        }
91      }
92  
93      return null;
94    }
95  }