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.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
24
25
26
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
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
64
65
66
67
68
69 public RetriesExhaustedException(final String callableVitals, int numTries,
70 List<Throwable> exceptions) {
71 super(getMessage(callableVitals, numTries, exceptions));
72 }
73
74
75
76
77
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 }