1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.master;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.RemoteExceptionHandler;
26 import org.apache.hadoop.hbase.TableNotDisabledException;
27 import org.apache.hadoop.hbase.TableNotFoundException;
28 import org.apache.hadoop.hbase.ipc.HRegionInterface;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.apache.hadoop.hbase.util.Sleeper;
31 import org.apache.hadoop.ipc.RemoteException;
32
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.Callable;
37
38
39
40
41
42 abstract class RetryableMetaOperation<T> implements Callable<T> {
43 protected final Log LOG = LogFactory.getLog(this.getClass());
44 protected final Sleeper sleeper;
45 protected final MetaRegion m;
46 protected final HMaster master;
47
48 protected HRegionInterface server;
49
50 protected RetryableMetaOperation(MetaRegion m, HMaster master) {
51 this.m = m;
52 this.master = master;
53 this.sleeper = new Sleeper(this.master.getThreadWakeFrequency(),
54 this.master.getClosed());
55 }
56
57 protected T doWithRetries()
58 throws IOException, RuntimeException {
59 List<IOException> exceptions = new ArrayList<IOException>();
60 for (int tries = 0; tries < this.master.getNumRetries(); tries++) {
61 if (this.master.isClosed()) {
62 return null;
63 }
64 try {
65 this.server =
66 this.master.getServerConnection().getHRegionConnection(m.getServer());
67 return this.call();
68 } catch (IOException e) {
69 if (e instanceof TableNotFoundException ||
70 e instanceof TableNotDisabledException ||
71 e instanceof InvalidColumnNameException) {
72 throw e;
73 }
74 if (e instanceof RemoteException) {
75 e = RemoteExceptionHandler.decodeRemoteException((RemoteException) e);
76 }
77 if (tries == this.master.getNumRetries() - 1) {
78 if (LOG.isDebugEnabled()) {
79 StringBuilder message = new StringBuilder(
80 "Trying to contact region server for regionName '" +
81 Bytes.toString(m.getRegionName()) + "', but failed after " +
82 (tries + 1) + " attempts.\n");
83 int i = 1;
84 for (IOException e2 : exceptions) {
85 message.append("Exception " + i + ":\n" + e2);
86 }
87 LOG.debug(message);
88 }
89 this.master.checkFileSystem();
90 throw e;
91 }
92 if (LOG.isDebugEnabled()) {
93 exceptions.add(e);
94 }
95 } catch (Exception e) {
96 LOG.debug("Exception in RetryableMetaOperation: ", e);
97 throw new RuntimeException(e);
98 }
99 this.sleeper.sleep();
100 }
101 return null;
102 }
103 }