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