1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.apache.hadoop.hbase.client;
16
17 import org.apache.hadoop.hbase.classification.InterfaceAudience;
18 import org.apache.hadoop.hbase.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
27
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
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
65
66
67
68
69
70 public RetriesExhaustedException(final String callableVitals, int numTries,
71 List<Throwable> exceptions) {
72 super(getMessage(callableVitals, numTries, exceptions));
73 }
74
75
76
77
78
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 }