View Javadoc

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