View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.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   * This subclass of {@link org.apache.hadoop.hbase.client.RetriesExhaustedException}
38   * is thrown when we have more information about which rows were causing which
39   * exceptions on what servers.  You can call {@link #mayHaveClusterIssues()}
40   * and if the result is false, you have input error problems, otherwise you
41   * may have cluster issues.  You can iterate over the causes, rows and last
42   * known server addresses via {@link #getNumExceptions()} and
43   * {@link #getCause(int)}, {@link #getRow(int)} and {@link #getHostnamePort(int)}.
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      // If all of the exceptions are DNRIOE not exception
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<? extends 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("exception");
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         pw.println();
137         t.printStackTrace(pw);
138       }
139     }
140     pw.flush();
141     return errorWriter.toString();
142   }
143 
144 
145   public static Map<String, Integer> classifyExs(List<Throwable> ths) {
146     Map<String, Integer> cls = new HashMap<String, Integer>();
147     for (Throwable t : ths) {
148       if (t == null) continue;
149       String name = "";
150       if (t instanceof DoNotRetryIOException) {
151         name = t.getMessage();
152       } else {
153         name = t.getClass().getSimpleName();
154       }
155       Integer i = cls.get(name);
156       if (i == null) {
157         i = 0;
158       }
159       i += 1;
160       cls.put(name, i);
161     }
162     return cls;
163   }
164 
165   public static String getDesc(Map<String,Integer> classificaton) {
166     StringBuilder classificatons =new StringBuilder(11);
167     for (Map.Entry<String, Integer> e : classificaton.entrySet()) {
168       classificatons.append(e.getKey());
169       classificatons.append(": ");
170       classificatons.append(e.getValue());
171       classificatons.append(" time");
172       classificatons.append(pluralize(e.getValue()));
173       classificatons.append(", ");
174     }
175     return classificatons.toString();
176   }
177 
178 }