1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.io.IOException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.KeyValueUtil;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
33 import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
34 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
35 import org.apache.hadoop.hbase.protobuf.RequestConverter;
36 import org.apache.hadoop.hbase.protobuf.ResponseConverter;
37 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
38 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
39 import org.apache.hadoop.hbase.util.Bytes;
40
41 import com.google.protobuf.ServiceException;
42
43
44
45
46
47
48
49
50 @InterfaceAudience.Public
51 @InterfaceStability.Evolving
52 public class ClientSmallScanner extends ClientScanner {
53 private final Log LOG = LogFactory.getLog(this.getClass());
54 private RegionServerCallable<Result[]> smallScanCallable = null;
55
56
57 private byte[] skipRowOfFirstResult = null;
58
59
60
61
62
63
64
65
66
67
68
69 public ClientSmallScanner(final Configuration conf, final Scan scan,
70 final TableName tableName) throws IOException {
71 this(conf, scan, tableName, HConnectionManager.getConnection(conf));
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public ClientSmallScanner(final Configuration conf, final Scan scan,
85 final TableName tableName, HConnection connection) throws IOException {
86 this(conf, scan, tableName, connection, RpcRetryingCallerFactory.instantiate(conf),
87 RpcControllerFactory.instantiate(conf));
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public ClientSmallScanner(final Configuration conf, final Scan scan, final TableName tableName,
103 HConnection connection, RpcRetryingCallerFactory rpcFactory,
104 RpcControllerFactory controllerFactory) throws IOException {
105 super(conf, scan, tableName, connection, rpcFactory, controllerFactory);
106 }
107
108 @Override
109 protected void initializeScannerInConstruction() throws IOException {
110
111
112 }
113
114
115
116
117
118
119
120
121
122
123 private boolean nextScanner(int nbRows, final boolean done,
124 boolean currentRegionDone) throws IOException {
125
126 byte[] localStartKey;
127 int cacheNum = nbRows;
128 skipRowOfFirstResult = null;
129
130 if (this.currentRegion != null && currentRegionDone) {
131 byte[] endKey = this.currentRegion.getEndKey();
132 if (endKey == null || Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY)
133 || checkScanStopRow(endKey) || done) {
134 close();
135 if (LOG.isDebugEnabled()) {
136 LOG.debug("Finished with small scan at " + this.currentRegion);
137 }
138 return false;
139 }
140 localStartKey = endKey;
141 if (LOG.isDebugEnabled()) {
142 LOG.debug("Finished with region " + this.currentRegion);
143 }
144 } else if (this.lastResult != null) {
145 localStartKey = this.lastResult.getRow();
146 skipRowOfFirstResult = this.lastResult.getRow();
147 cacheNum++;
148 } else {
149 localStartKey = this.scan.getStartRow();
150 }
151
152 if (LOG.isTraceEnabled()) {
153 LOG.trace("Advancing internal small scanner to startKey at '"
154 + Bytes.toStringBinary(localStartKey) + "'");
155 }
156 smallScanCallable = getSmallScanCallable(
157 scan, getConnection(), getTable(), localStartKey, cacheNum, rpcControllerFactory);
158 if (this.scanMetrics != null && skipRowOfFirstResult == null) {
159 this.scanMetrics.countOfRegions.incrementAndGet();
160 }
161 return true;
162 }
163
164 static RegionServerCallable<Result[]> getSmallScanCallable(
165 final Scan sc, HConnection connection, TableName table, byte[] localStartKey,
166 final int cacheNum, final RpcControllerFactory rpcControllerFactory) throws IOException {
167 sc.setStartRow(localStartKey);
168 RegionServerCallable<Result[]> callable = new RegionServerCallable<Result[]>(
169 connection, table, sc.getStartRow()) {
170 public Result[] call() throws IOException {
171 ScanRequest request = RequestConverter.buildScanRequest(getLocation()
172 .getRegionInfo().getRegionName(), sc, cacheNum, true);
173 ScanResponse response = null;
174 PayloadCarryingRpcController controller = rpcControllerFactory.newController();
175 try {
176 controller.setPriority(getTableName());
177 response = getStub().scan(controller, request);
178 return ResponseConverter.getResults(controller.cellScanner(),
179 response);
180 } catch (ServiceException se) {
181 throw ProtobufUtil.getRemoteException(se);
182 }
183 }
184 };
185 return callable;
186 }
187
188 @Override
189 public Result next() throws IOException {
190
191
192 if (cache.size() == 0 && this.closed) {
193 return null;
194 }
195 if (cache.size() == 0) {
196 Result[] values = null;
197 long remainingResultSize = maxScannerResultSize;
198 int countdown = this.caching;
199 boolean currentRegionDone = false;
200
201 while (remainingResultSize > 0 && countdown > 0
202 && nextScanner(countdown, values == null, currentRegionDone)) {
203
204
205
206 values = this.caller.callWithRetries(smallScanCallable);
207 this.currentRegion = smallScanCallable.getHRegionInfo();
208 long currentTime = System.currentTimeMillis();
209 if (this.scanMetrics != null) {
210 this.scanMetrics.sumOfMillisSecBetweenNexts.addAndGet(currentTime
211 - lastNext);
212 }
213 lastNext = currentTime;
214 if (values != null && values.length > 0) {
215 for (int i = 0; i < values.length; i++) {
216 Result rs = values[i];
217 if (i == 0 && this.skipRowOfFirstResult != null
218 && Bytes.equals(skipRowOfFirstResult, rs.getRow())) {
219
220 continue;
221 }
222 cache.add(rs);
223 for (Cell kv : rs.rawCells()) {
224 remainingResultSize -= KeyValueUtil.ensureKeyValue(kv).heapSize();
225 }
226 countdown--;
227 this.lastResult = rs;
228 }
229 }
230 currentRegionDone = countdown > 0;
231 }
232 }
233
234 if (cache.size() > 0) {
235 return cache.poll();
236 }
237
238
239 writeScanMetrics();
240 return null;
241 }
242
243 @Override
244 public void close() {
245 if (!scanMetricsPublished) writeScanMetrics();
246 closed = true;
247 }
248 }