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.classification.InterfaceAudience;
29 import org.apache.hadoop.classification.InterfaceStability;
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 @InterfaceAudience.Public
43 @InterfaceStability.Stable
44 public abstract class RegionServerCallable<T> implements RetryingCallable<T> {
45
46 static final Log LOG = LogFactory.getLog(RegionServerCallable.class);
47 private final HConnection connection;
48 private final TableName tableName;
49 private final byte [] row;
50 private 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(HConnection 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 public void prepare(final boolean reload) throws IOException {
73 this.location = connection.getRegionLocation(tableName, row, reload);
74 if (this.location == null) {
75 throw new IOException("Failed to find location, tableName=" + tableName +
76 ", row=" + Bytes.toString(row) + ", reload=" + reload);
77 }
78 setStub(getConnection().getClient(getLocation().getServerName()));
79 }
80
81
82
83
84 HConnection getConnection() {
85 return this.connection;
86 }
87
88 protected ClientService.BlockingInterface getStub() {
89 return this.stub;
90 }
91
92 void setStub(final ClientService.BlockingInterface stub) {
93 this.stub = stub;
94 }
95
96 protected HRegionLocation getLocation() {
97 return this.location;
98 }
99
100 protected void setLocation(final HRegionLocation location) {
101 this.location = location;
102 }
103
104 public TableName getTableName() {
105 return this.tableName;
106 }
107
108 public byte [] getRow() {
109 return this.row;
110 }
111
112 @Override
113 public void throwable(Throwable t, boolean retrying) {
114 if (t instanceof SocketTimeoutException ||
115 t instanceof ConnectException ||
116 t instanceof RetriesExhaustedException ||
117 (location != null && getConnection().isDeadServer(location.getServerName()))) {
118
119
120
121 getConnection().clearCaches(location.getServerName());
122 } else if (t instanceof RegionMovedException) {
123 getConnection().updateCachedLocations(tableName, row, t, location);
124 } else if (t instanceof NotServingRegionException && !retrying) {
125
126
127 getConnection().deleteCachedRegionLocation(location);
128 }
129 }
130
131 @Override
132 public String getExceptionMessageAdditionalDetail() {
133 return "row '" + Bytes.toString(row) + "' on table '" + tableName;
134 }
135
136 @Override
137 public long sleep(long pause, int tries) {
138
139 long sleep = ConnectionUtils.getPauseTime(pause, tries + 1);
140 if (sleep < MIN_WAIT_DEAD_SERVER
141 && (location == null || getConnection().isDeadServer(location.getServerName()))) {
142 sleep = ConnectionUtils.addJitter(MIN_WAIT_DEAD_SERVER, 0.10f);
143 }
144 return sleep;
145 }
146 }