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