1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.classification.InterfaceStability;
23
24 import java.io.IOException;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.concurrent.Callable;
28
29
30
31
32
33 @InterfaceAudience.Public
34 @InterfaceStability.Stable
35 public class RetriesExhaustedException extends IOException {
36 private static final long serialVersionUID = 1876775844L;
37
38 public RetriesExhaustedException(final String msg) {
39 super(msg);
40 }
41
42 public RetriesExhaustedException(final String msg, final IOException e) {
43 super(msg, e);
44 }
45
46
47
48
49 public static class ThrowableWithExtraContext {
50 private final Throwable t;
51 private final long when;
52 private final String extras;
53
54 public ThrowableWithExtraContext(final Throwable t, final long when,
55 final String extras) {
56 this.t = t;
57 this.when = when;
58 this.extras = extras;
59 }
60
61 @Override
62 public String toString() {
63 return new Date(this.when).toString() + ", " + extras + ", " + t.toString();
64 }
65 }
66
67
68
69
70
71
72
73
74 public RetriesExhaustedException(final String callableVitals, int numTries,
75 List<Throwable> exceptions) {
76 super(getMessage(callableVitals, numTries, exceptions));
77 }
78
79
80
81
82
83
84 public RetriesExhaustedException(final int numTries,
85 final List<ThrowableWithExtraContext> exceptions) {
86 super(getMessage(numTries, exceptions),
87 (exceptions != null && !exceptions.isEmpty() ?
88 exceptions.get(exceptions.size() - 1).t : null));
89 }
90
91 private static String getMessage(String callableVitals, int numTries,
92 List<Throwable> exceptions) {
93 StringBuilder buffer = new StringBuilder("Failed contacting ");
94 buffer.append(callableVitals);
95 buffer.append(" after ");
96 buffer.append(numTries + 1);
97 buffer.append(" attempts.\nExceptions:\n");
98 for (Throwable t : exceptions) {
99 buffer.append(t.toString());
100 buffer.append("\n");
101 }
102 return buffer.toString();
103 }
104
105 private static String getMessage(final int numTries,
106 final List<ThrowableWithExtraContext> exceptions) {
107 StringBuilder buffer = new StringBuilder("Failed after attempts=");
108 buffer.append(numTries + 1);
109 buffer.append(", exceptions:\n");
110 for (ThrowableWithExtraContext t : exceptions) {
111 buffer.append(t.toString());
112 buffer.append("\n");
113 }
114 return buffer.toString();
115 }
116 }