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 org.apache.hadoop.classification.InterfaceAudience;
18  import org.apache.hadoop.classification.InterfaceStability;
19  
20  import java.io.IOException;
21  import java.util.Date;
22  import java.util.List;
23  import java.util.concurrent.Callable;
24  
25  /**
26   * Exception thrown by HTable methods when an attempt to do something (like
27   * commit changes) fails after a bunch of retries.
28   */
29  @InterfaceAudience.Public
30  @InterfaceStability.Stable
31  public class RetriesExhaustedException extends IOException {
32    private static final long serialVersionUID = 1876775844L;
33  
34    public RetriesExhaustedException(final String msg) {
35      super(msg);
36    }
37  
38    public RetriesExhaustedException(final String msg, final IOException e) {
39      super(msg, e);
40    }
41  
42    /**
43     * Datastructure that allows adding more info around Throwable incident.
44     */
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    public RetriesExhaustedException(final int numTries,
81                                     final List<ThrowableWithExtraContext> exceptions) {
82      super(getMessage(numTries, exceptions),
83          (exceptions != null && !exceptions.isEmpty() ?
84              exceptions.get(exceptions.size() - 1).t : null));
85    }
86  
87    private static String getMessage(String callableVitals, int numTries,
88        List<Throwable> exceptions) {
89      StringBuilder buffer = new StringBuilder("Failed contacting ");
90      buffer.append(callableVitals);
91      buffer.append(" after ");
92      buffer.append(numTries + 1);
93      buffer.append(" attempts.\nExceptions:\n");
94      for (Throwable t : exceptions) {
95        buffer.append(t.toString());
96        buffer.append("\n");
97      }
98      return buffer.toString();
99    }
100 
101   private static String getMessage(final int numTries,
102       final List<ThrowableWithExtraContext> exceptions) {
103     StringBuilder buffer = new StringBuilder("Failed after attempts=");
104     buffer.append(numTries + 1);
105     buffer.append(", exceptions:\n");
106     for (ThrowableWithExtraContext t : exceptions) {
107       buffer.append(t.toString());
108       buffer.append("\n");
109     }
110     return buffer.toString();
111   }
112 }