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