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    public static class ThrowableWithExtraContext {
45      private final Throwable t;
46      private final long when;
47      private final String extras;
48  
49      public ThrowableWithExtraContext(final Throwable t, final long when,
50          final String extras) {
51        this.t = t;
52        this.when = when;
53        this.extras = extras;
54      }
55   
56      @Override
57      public String toString() {
58        return new Date(this.when).toString() + ", " + extras + ", " + t.toString();
59      }
60    }
61  
62    /**
63     * Create a new RetriesExhaustedException from the list of prior failures.
64     * @param callableVitals Details from the Callable we were using
65     * when we got this exception.
66     * @param numTries The number of tries we made
67     * @param exceptions List of exceptions that failed before giving up
68     */
69    public RetriesExhaustedException(final String callableVitals, int numTries,
70        List<Throwable> exceptions) {
71      super(getMessage(callableVitals, numTries, exceptions));
72    }
73  
74    /**
75     * Create a new RetriesExhaustedException from the list of prior failures.
76     * @param numTries
77     * @param exceptions List of exceptions that failed before giving up
78     */
79    public RetriesExhaustedException(final int numTries,
80                                     final List<ThrowableWithExtraContext> exceptions) {
81      super(getMessage(numTries, exceptions),
82          (exceptions != null && !exceptions.isEmpty() ?
83              exceptions.get(exceptions.size() - 1).t : null));
84    }
85  
86    private static String getMessage(String callableVitals, int numTries,
87        List<Throwable> exceptions) {
88      StringBuilder buffer = new StringBuilder("Failed contacting ");
89      buffer.append(callableVitals);
90      buffer.append(" after ");
91      buffer.append(numTries + 1);
92      buffer.append(" attempts.\nExceptions:\n");
93      for (Throwable t : exceptions) {
94        buffer.append(t.toString());
95        buffer.append("\n");
96      }
97      return buffer.toString();
98    }
99  
100   private static String getMessage(final int numTries,
101       final List<ThrowableWithExtraContext> exceptions) {
102     StringBuilder buffer = new StringBuilder("Failed after attempts=");
103     buffer.append(numTries + 1);
104     buffer.append(", exceptions:\n");
105     for (ThrowableWithExtraContext t : exceptions) {
106       buffer.append(t.toString());
107       buffer.append("\n");
108     }
109     return buffer.toString();
110   }
111 }