1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import org.apache.hadoop.classification.InterfaceAudience;
23 import org.apache.hadoop.classification.InterfaceStability;
24 import org.apache.hadoop.hbase.exceptions.DoNotRetryIOException;
25 import org.apache.hadoop.hbase.util.Bytes;
26
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35
36
37
38
39
40
41
42
43
44
45 @SuppressWarnings("serial")
46 @InterfaceAudience.Public
47 @InterfaceStability.Stable
48 public class RetriesExhaustedWithDetailsException
49 extends RetriesExhaustedException {
50 List<Throwable> exceptions;
51 List<Row> actions;
52 List<String> hostnameAndPort;
53
54 public RetriesExhaustedWithDetailsException(List<Throwable> exceptions,
55 List<Row> actions,
56 List<String> hostnameAndPort) {
57 super("Failed " + exceptions.size() + " action" +
58 pluralize(exceptions) + ": " +
59 getDesc(exceptions, actions, hostnameAndPort));
60
61 this.exceptions = exceptions;
62 this.actions = actions;
63 this.hostnameAndPort = hostnameAndPort;
64 }
65
66 public List<Throwable> getCauses() {
67 return exceptions;
68 }
69
70 public int getNumExceptions() {
71 return exceptions.size();
72 }
73
74 public Throwable getCause(int i) {
75 return exceptions.get(i);
76 }
77
78 public Row getRow(int i) {
79 return actions.get(i);
80 }
81
82 public String getHostnamePort(final int i) {
83 return this.hostnameAndPort.get(i);
84 }
85
86 public boolean mayHaveClusterIssues() {
87 boolean res = false;
88
89
90 for (Throwable t : exceptions) {
91 if ( !(t instanceof DoNotRetryIOException)) {
92 res = true;
93 }
94 }
95 return res;
96 }
97
98
99 public static String pluralize(Collection<?> c) {
100 return pluralize(c.size());
101 }
102
103 public static String pluralize(int c) {
104 return c > 1 ? "s" : "";
105 }
106
107 public static String getDesc(List<Throwable> exceptions,
108 List<Row> actions,
109 List<String> hostnamePort) {
110 String s = getDesc(classifyExs(exceptions));
111 StringBuilder addrs = new StringBuilder(s);
112 addrs.append("servers with issues: ");
113 Set<String> uniqAddr = new HashSet<String>();
114 uniqAddr.addAll(hostnamePort);
115
116 for(String addr : uniqAddr) {
117 addrs.append(addr).append(", ");
118 }
119 return s;
120 }
121
122 public String getExhaustiveDescription() {
123 StringWriter errorWriter = new StringWriter();
124 PrintWriter pw = new PrintWriter(errorWriter);
125 for (int i = 0; i < this.exceptions.size(); ++i) {
126 Throwable t = this.exceptions.get(i);
127 Row action = this.actions.get(i);
128 String server = this.hostnameAndPort.get(i);
129 pw.append("Error");
130 if (this.exceptions.size() > 1) {
131 pw.append(" #" + i);
132 }
133 pw.append(" from [" + server + "] for ["
134 + ((action == null) ? "unknown key" : Bytes.toStringBinary(action.getRow())) + "]");
135 if (t != null) {
136 t.printStackTrace(pw);
137 }
138 }
139 pw.flush();
140 return errorWriter.toString();
141 }
142
143
144 public static Map<String, Integer> classifyExs(List<Throwable> ths) {
145 Map<String, Integer> cls = new HashMap<String, Integer>();
146 for (Throwable t : ths) {
147 if (t == null) continue;
148 String name = "";
149 if (t instanceof DoNotRetryIOException) {
150 name = t.getMessage();
151 } else {
152 name = t.getClass().getSimpleName();
153 }
154 Integer i = cls.get(name);
155 if (i == null) {
156 i = 0;
157 }
158 i += 1;
159 cls.put(name, i);
160 }
161 return cls;
162 }
163
164 public static String getDesc(Map<String,Integer> classificaton) {
165 StringBuilder classificatons =new StringBuilder(11);
166 for (Map.Entry<String, Integer> e : classificaton.entrySet()) {
167 classificatons.append(e.getKey());
168 classificatons.append(": ");
169 classificatons.append(e.getValue());
170 classificatons.append(" time");
171 classificatons.append(pluralize(e.getValue()));
172 classificatons.append(", ");
173 }
174 return classificatons.toString();
175 }
176
177 }