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
55
56
57
58
59 public RegionServerCallable(Connection connection, TableName tableName, byte [] row) {
60 this.connection = connection;
61 this.tableName = tableName;
62 this.row = row;
63 }
64
65
66
67
68
69
70
71 @Override
72 public void prepare(final boolean reload) throws IOException {
73
74 if (reload &&
75 !tableName.equals(TableName.META_TABLE_NAME) &&
76 getConnection().isTableDisabled(tableName)) {
77 throw new TableNotEnabledException(tableName.getNameAsString() + " is disabled.");
78 }
79 try (RegionLocator regionLocator = connection.getRegionLocator(tableName)) {
80 this.location = regionLocator.getRegionLocation(row);
81 }
82 if (this.location == null) {
83 throw new IOException("Failed to find location, tableName=" + tableName +
84 ", row=" + Bytes.toString(row) + ", reload=" + reload);
85 }
86 setStub(getConnection().getClient(this.location.getServerName()));
87 }
88
89
90
91
92 HConnection getConnection() {
93 return (HConnection) this.connection;
94 }
95
96 protected ClientService.BlockingInterface getStub() {
97 return this.stub;
98 }
99
100 void setStub(final ClientService.BlockingInterface stub) {
101 this.stub = stub;
102 }
103
104 protected HRegionLocation getLocation() {
105 return this.location;
106 }
107
108 protected void setLocation(final HRegionLocation location) {
109 this.location = location;
110 }
111
112 public TableName getTableName() {
113 return this.tableName;
114 }
115
116 public byte [] getRow() {
117 return this.row;
118 }
119
120 @Override
121 public void throwable(Throwable t, boolean retrying) {
122 if (location != null) {
123 getConnection().updateCachedLocations(tableName, location.getRegionInfo().getRegionName(),
124 row, t, location.getServerName());
125 }
126 }
127
128 @Override
129 public String getExceptionMessageAdditionalDetail() {
130 return "row '" + Bytes.toString(row) + "' on table '" + tableName + "' at " + location;
131 }
132
133 @Override
134 public long sleep(long pause, int tries) {
135 return ConnectionUtils.getPauseTime(pause, tries);
136 }
137
138
139
140
141 public HRegionInfo getHRegionInfo() {
142 if (this.location == null) {
143 return null;
144 }
145 return this.location.getRegionInfo();
146 }
147 }