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 java.io.IOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.HRegionInfo;
28 import org.apache.hadoop.hbase.HRegionLocation;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.TableNotEnabledException;
31 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
32 import org.apache.hadoop.hbase.util.Bytes;
33
34
35
36
37
38
39
40
41
42
43
44 @InterfaceAudience.Private
45 public abstract class RegionServerCallable<T> implements RetryingCallable<T> {
46
47 private static final Log LOG = LogFactory.getLog(RegionServerCallable.class);
48 protected final Connection connection;
49 protected final TableName tableName;
50 protected final byte[] row;
51 protected HRegionLocation location;
52 private ClientService.BlockingInterface stub;
53
54 protected final static int MIN_WAIT_DEAD_SERVER = 10000;
55
56
57
58
59
60
61 public RegionServerCallable(Connection connection, TableName tableName, byte [] row) {
62 this.connection = connection;
63 this.tableName = tableName;
64 this.row = row;
65 }
66
67
68
69
70
71
72
73 @Override
74 public void prepare(final boolean reload) throws IOException {
75
76 if (reload &&
77 !tableName.equals(TableName.META_TABLE_NAME) &&
78 getConnection().isTableDisabled(tableName)) {
79 throw new TableNotEnabledException(tableName.getNameAsString() + " is disabled.");
80 }
81 try (RegionLocator regionLocator = connection.getRegionLocator(tableName)) {
82 this.location = regionLocator.getRegionLocation(row);
83 }
84 if (this.location == null) {
85 throw new IOException("Failed to find location, tableName=" + tableName +
86 ", row=" + Bytes.toString(row) + ", reload=" + reload);
87 }
88 setStub(getConnection().getClient(this.location.getServerName()));
89 }
90
91
92
93
94 HConnection getConnection() {
95 return (HConnection) this.connection;
96 }
97
98 protected ClientService.BlockingInterface getStub() {
99 return this.stub;
100 }
101
102 void setStub(final ClientService.BlockingInterface stub) {
103 this.stub = stub;
104 }
105
106 protected HRegionLocation getLocation() {
107 return this.location;
108 }
109
110 protected void setLocation(final HRegionLocation location) {
111 this.location = location;
112 }
113
114 public TableName getTableName() {
115 return this.tableName;
116 }
117
118 public byte [] getRow() {
119 return this.row;
120 }
121
122 @Override
123 public void throwable(Throwable t, boolean retrying) {
124 if (location != null) {
125 getConnection().updateCachedLocations(tableName, location.getRegionInfo().getRegionName(),
126 row, t, location.getServerName());
127 }
128 }
129
130 @Override
131 public String getExceptionMessageAdditionalDetail() {
132 return "row '" + Bytes.toString(row) + "' on table '" + tableName + "' at " + location;
133 }
134
135 @Override
136 public long sleep(long pause, int tries) {
137 long sleep = ConnectionUtils.getPauseTime(pause, tries);
138 if (sleep < MIN_WAIT_DEAD_SERVER
139 && (location == null || getConnection().isDeadServer(location.getServerName()))) {
140 sleep = ConnectionUtils.addJitter(MIN_WAIT_DEAD_SERVER, 0.10f);
141 }
142 return sleep;
143 }
144
145
146
147
148 public HRegionInfo getHRegionInfo() {
149 if (this.location == null) {
150 return null;
151 }
152 return this.location.getRegionInfo();
153 }
154 }