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.client;
22
23 import java.io.IOException;
24 import java.net.SocketTimeoutException;
25 import java.util.concurrent.Callable;
26
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.HRegionLocation;
29 import org.apache.hadoop.hbase.ipc.HBaseRPC;
30 import org.apache.hadoop.hbase.ipc.HRegionInterface;
31 import org.apache.hadoop.hbase.util.Bytes;
32
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class ServerCallable<T> implements Callable<T> {
45 protected final HConnection connection;
46 protected final byte [] tableName;
47 protected final byte [] row;
48 protected HRegionLocation location;
49 protected HRegionInterface server;
50 protected int callTimeout;
51 protected long startTime, endTime;
52
53
54
55
56
57
58 public ServerCallable(HConnection connection, byte [] tableName, byte [] row) {
59 this(connection, tableName, row, HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
60 }
61
62 public ServerCallable(HConnection connection, byte [] tableName, byte [] row, int callTimeout) {
63 this.connection = connection;
64 this.tableName = tableName;
65 this.row = row;
66 this.callTimeout = callTimeout;
67 }
68
69
70
71
72
73
74 public void connect(final boolean reload) throws IOException {
75 this.location = connection.getRegionLocation(tableName, row, reload);
76 this.server = connection.getHRegionConnection(location.getHostname(),
77 location.getPort());
78 }
79
80
81
82
83 public String getServerName() {
84 if (location == null) return null;
85 return location.getHostnamePort();
86 }
87
88
89
90
91 public byte[] getRegionName() {
92 if (location == null) return null;
93 return location.getRegionInfo().getRegionName();
94 }
95
96
97
98
99 public byte [] getRow() {
100 return row;
101 }
102
103 public void beforeCall() {
104 HBaseRPC.setRpcTimeout(this.callTimeout);
105 this.startTime = System.currentTimeMillis();
106 }
107
108 public void afterCall() {
109 HBaseRPC.resetRpcTimeout();
110 this.endTime = System.currentTimeMillis();
111 }
112
113 public void shouldRetry(Throwable throwable) throws IOException {
114 if (this.callTimeout != HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT)
115 if (throwable instanceof SocketTimeoutException
116 || (this.endTime - this.startTime > this.callTimeout)) {
117 throw (SocketTimeoutException) (SocketTimeoutException) new SocketTimeoutException(
118 "Call to access row '" + Bytes.toString(row) + "' on table '"
119 + Bytes.toString(tableName)
120 + "' failed on socket timeout exception: " + throwable)
121 .initCause(throwable);
122 } else {
123 this.callTimeout = ((int) (this.endTime - this.startTime));
124 }
125 }
126 }