View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.hadoop.hbase.client;
17  
18  import org.apache.hadoop.hbase.util.Bytes;
19  
20  import java.io.IOException;
21  import java.util.List;
22  
23  /**
24   * Exception thrown by HTable methods when an attempt to do something (like
25   * commit changes) fails after a bunch of retries.
26   */
27  public class RetriesExhaustedException extends IOException {
28    private static final long serialVersionUID = 1876775844L;
29  
30    public RetriesExhaustedException(final String msg) {
31      super(msg);
32    }
33  
34    public RetriesExhaustedException(final String msg, final IOException e) {
35      super(msg, e);
36    }
37  
38    /**
39     * Create a new RetriesExhaustedException from the list of prior failures.
40     * @param serverName name of HRegionServer
41     * @param regionName name of region
42     * @param row The row we were pursuing when we ran out of retries
43     * @param numTries The number of tries we made
44     * @param exceptions List of exceptions that failed before giving up
45     */
46    public RetriesExhaustedException(String serverName, final byte [] regionName,
47        final byte []  row, int numTries, List<Throwable> exceptions) {
48      super(getMessage(serverName, regionName, row, numTries, exceptions));
49    }
50  
51    private static String getMessage(String serverName, final byte [] regionName,
52        final byte [] row,
53        int numTries, List<Throwable> exceptions) {
54      StringBuilder buffer = new StringBuilder("Trying to contact region server ");
55      buffer.append(serverName);
56      buffer.append(" for region ");
57      buffer.append(regionName == null? "": Bytes.toStringBinary(regionName));
58      buffer.append(", row '");
59      buffer.append(row == null? "": Bytes.toStringBinary(row));
60      buffer.append("', but failed after ");
61      buffer.append(numTries + 1);
62      buffer.append(" attempts.\nExceptions:\n");
63      for (Throwable t : exceptions) {
64        buffer.append(t.toString());
65        buffer.append("\n");
66      }
67      return buffer.toString();
68    }
69  }