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