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