View Javadoc

1   /**
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.apache.hadoop.hbase.client;
16  
17  import java.io.IOException;
18  import java.util.Date;
19  import java.util.List;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  
24  /**
25   * Exception thrown by HTable methods when an attempt to do something (like
26   * commit changes) fails after a bunch of retries.
27   */
28  @InterfaceAudience.Public
29  @InterfaceStability.Stable
30  public class RetriesExhaustedException extends IOException {
31    private static final long serialVersionUID = 1876775844L;
32  
33    public RetriesExhaustedException(final String msg) {
34      super(msg);
35    }
36  
37    public RetriesExhaustedException(final String msg, final IOException e) {
38      super(msg, e);
39    }
40  
41    /**
42     * Datastructure that allows adding more info around Throwable incident.
43     */
44    @InterfaceAudience.Private
45    public static class ThrowableWithExtraContext {
46      private final Throwable t;
47      private final long when;
48      private final String extras;
49  
50      public ThrowableWithExtraContext(final Throwable t, final long when,
51          final String extras) {
52        this.t = t;
53        this.when = when;
54        this.extras = extras;
55      }
56  
57      @Override
58      public String toString() {
59        return new Date(this.when).toString() + ", " + extras + ", " + t.toString();
60      }
61    }
62  
63    /**
64     * Create a new RetriesExhaustedException from the list of prior failures.
65     * @param callableVitals Details from the Callable we were using
66     * when we got this exception.
67     * @param numTries The number of tries we made
68     * @param exceptions List of exceptions that failed before giving up
69     */
70    public RetriesExhaustedException(final String callableVitals, int numTries,
71        List<Throwable> exceptions) {
72      super(getMessage(callableVitals, numTries, exceptions));
73    }
74  
75    /**
76     * Create a new RetriesExhaustedException from the list of prior failures.
77     * @param numTries
78     * @param exceptions List of exceptions that failed before giving up
79     */
80    @InterfaceAudience.Private
81    public RetriesExhaustedException(final int numTries,
82                                     final List<ThrowableWithExtraContext> exceptions) {
83      super(getMessage(numTries, exceptions),
84          (exceptions != null && !exceptions.isEmpty() ?
85              exceptions.get(exceptions.size() - 1).t : null));
86    }
87  
88    private static String getMessage(String callableVitals, int numTries,
89        List<Throwable> exceptions) {
90      StringBuilder buffer = new StringBuilder("Failed contacting ");
91      buffer.append(callableVitals);
92      buffer.append(" after ");
93      buffer.append(numTries + 1);
94      buffer.append(" attempts.\nExceptions:\n");
95      for (Throwable t : exceptions) {
96        buffer.append(t.toString());
97        buffer.append("\n");
98      }
99      return buffer.toString();
100   }
101 
102   private static String getMessage(final int numTries,
103       final List<ThrowableWithExtraContext> exceptions) {
104     StringBuilder buffer = new StringBuilder("Failed after attempts=");
105     buffer.append(numTries + 1);
106     buffer.append(", exceptions:\n");
107     for (ThrowableWithExtraContext t : exceptions) {
108       buffer.append(t.toString());
109       buffer.append("\n");
110     }
111     return buffer.toString();
112   }
113 }