View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.client;
20  
21  import java.io.IOException;
22  import java.net.UnknownHostException;
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.conf.Configuration;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellScanner;
30  import org.apache.hadoop.hbase.DoNotRetryIOException;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HRegionLocation;
33  import org.apache.hadoop.hbase.KeyValueUtil;
34  import org.apache.hadoop.hbase.NotServingRegionException;
35  import org.apache.hadoop.hbase.RemoteExceptionHandler;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.UnknownScannerException;
38  import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
39  import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
40  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
41  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
42  import org.apache.hadoop.hbase.protobuf.RequestConverter;
43  import org.apache.hadoop.hbase.protobuf.ResponseConverter;
44  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
45  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
46  import org.apache.hadoop.hbase.regionserver.RegionServerStoppedException;
47  import org.apache.hadoop.ipc.RemoteException;
48  import org.apache.hadoop.net.DNS;
49  
50  import com.google.protobuf.ServiceException;
51  import com.google.protobuf.TextFormat;
52  
53  /**
54   * Scanner operations such as create, next, etc.
55   * Used by {@link ResultScanner}s made by {@link HTable}. Passed to a retrying caller such as
56   * {@link RpcRetryingCaller} so fails are retried.
57   */
58  @InterfaceAudience.Private
59  public class ScannerCallable extends RegionServerCallable<Result[]> {
60    public static final String LOG_SCANNER_LATENCY_CUTOFF
61      = "hbase.client.log.scanner.latency.cutoff";
62    public static final String LOG_SCANNER_ACTIVITY = "hbase.client.log.scanner.activity";
63  
64    public static final Log LOG = LogFactory.getLog(ScannerCallable.class);
65    private long scannerId = -1L;
66    protected boolean instantiated = false;
67    protected boolean closed = false;
68    protected boolean renew = false;
69    private Scan scan;
70    private int caching = 1;
71    protected ScanMetrics scanMetrics;
72    private boolean logScannerActivity = false;
73    private int logCutOffLatency = 1000;
74    private static String myAddress;
75    static {
76      try {
77        myAddress = DNS.getDefaultHost("default", "default");
78      } catch (UnknownHostException uhe) {
79        LOG.error("cannot determine my address", uhe);
80      }
81    }
82  
83    // indicate if it is a remote server call
84    protected boolean isRegionServerRemote = true;
85    private long nextCallSeq = 0;
86    protected PayloadCarryingRpcController controller;
87    
88    /**
89     * @param connection which connection
90     * @param tableName table callable is on
91     * @param scan the scan to execute
92     * @param scanMetrics the ScanMetrics to used, if it is null, ScannerCallable won't collect
93     *          metrics
94     * @param controller to use when writing the rpc
95     */
96    public ScannerCallable (HConnection connection, TableName tableName, Scan scan,
97        ScanMetrics scanMetrics, PayloadCarryingRpcController controller) {
98      super(connection, tableName, scan.getStartRow());
99      this.scan = scan;
100     this.scanMetrics = scanMetrics;
101     Configuration conf = connection.getConfiguration();
102     logScannerActivity = conf.getBoolean(LOG_SCANNER_ACTIVITY, false);
103     logCutOffLatency = conf.getInt(LOG_SCANNER_LATENCY_CUTOFF, 1000);
104     this.controller = controller;
105   }
106 
107   /**
108    * @deprecated Use {@link #ScannerCallable(HConnection, TableName, Scan, 
109    *  ScanMetrics, PayloadCarryingRpcController)}
110    */
111   @Deprecated
112   public ScannerCallable (HConnection connection, final byte [] tableName, Scan scan,
113       ScanMetrics scanMetrics) {
114     this(connection, TableName.valueOf(tableName), scan, scanMetrics, RpcControllerFactory
115         .instantiate(connection.getConfiguration()).newController());
116   }
117 
118   /**
119    * @param reload force reload of server location
120    * @throws IOException
121    */
122   @Override
123   public void prepare(boolean reload) throws IOException {
124     if (!instantiated || reload) {
125       super.prepare(reload);
126       checkIfRegionServerIsRemote();
127       instantiated = true;
128     }
129 
130     // check how often we retry.
131     // HConnectionManager will call instantiateServer with reload==true
132     // if and only if for retries.
133     if (reload && this.scanMetrics != null) {
134       this.scanMetrics.countOfRPCRetries.incrementAndGet();
135       if (isRegionServerRemote) {
136         this.scanMetrics.countOfRemoteRPCRetries.incrementAndGet();
137       }
138     }
139   }
140 
141   /**
142    * compare the local machine hostname with region server's hostname
143    * to decide if hbase client connects to a remote region server
144    */
145   protected void checkIfRegionServerIsRemote() {
146     if (getLocation().getHostname().equalsIgnoreCase(myAddress)) {
147       isRegionServerRemote = false;
148     } else {
149       isRegionServerRemote = true;
150     }
151   }
152 
153   /**
154    * @see java.util.concurrent.Callable#call()
155    */
156   @SuppressWarnings("deprecation")
157   public Result [] call() throws IOException {
158     if (controller == null) {
159       controller = RpcControllerFactory.instantiate(connection.getConfiguration())
160           .newController();
161     }
162     if (closed) {
163       if (scannerId != -1) {
164         close();
165       }
166     } else {
167       if (scannerId == -1L) {
168         this.scannerId = openScanner();
169       } else {
170         Result [] rrs = null;
171         ScanRequest request = null;
172         try {
173           incRPCcallsMetrics();
174           request =
175               RequestConverter.buildScanRequest(scannerId, caching, false, nextCallSeq, renew);
176           ScanResponse response = null;
177           try {
178             controller.setPriority(getTableName());
179             response = getStub().scan(controller, request);
180             // Client and RS maintain a nextCallSeq number during the scan. Every next() call
181             // from client to server will increment this number in both sides. Client passes this
182             // number along with the request and at RS side both the incoming nextCallSeq and its
183             // nextCallSeq will be matched. In case of a timeout this increment at the client side
184             // should not happen. If at the server side fetching of next batch of data was over,
185             // there will be mismatch in the nextCallSeq number. Server will throw
186             // OutOfOrderScannerNextException and then client will reopen the scanner with startrow
187             // as the last successfully retrieved row.
188             // See HBASE-5974
189             nextCallSeq++;
190             long timestamp = System.currentTimeMillis();
191             // Results are returned via controller
192             CellScanner cellScanner = controller.cellScanner();
193             rrs = ResponseConverter.getResults(cellScanner, response);
194             if (logScannerActivity) {
195               long now = System.currentTimeMillis();
196               if (now - timestamp > logCutOffLatency) {
197                 int rows = rrs == null ? 0 : rrs.length;
198                 LOG.info("Took " + (now-timestamp) + "ms to fetch "
199                   + rows + " rows from scanner=" + scannerId);
200               }
201             }
202             // moreResults is only used for the case where a filter exhausts all elements
203             if (response.hasMoreResults() && !response.getMoreResults()) {
204               scannerId = -1L;
205               closed = true;
206               // Implied that no results were returned back, either.
207               return null;
208             }
209             // moreResultsInRegion explicitly defines when a RS may choose to terminate a batch due
210             // to size or quantity of results in the response.
211             if (response.hasMoreResultsInRegion()) {
212               // Set what the RS said
213               setHasMoreResultsContext(true);
214               setServerHasMoreResults(response.getMoreResultsInRegion());
215             } else {
216               // Server didn't respond whether it has more results or not.
217               setHasMoreResultsContext(false);
218             }
219           } catch (ServiceException se) {
220             throw ProtobufUtil.getRemoteException(se);
221           }
222           updateResultsMetrics(rrs);
223         } catch (IOException e) {
224           if (logScannerActivity) {
225             LOG.info("Got exception making request " + TextFormat.shortDebugString(request)
226               + " to " + getLocation(), e);
227           }
228           IOException ioe = e;
229           if (e instanceof RemoteException) {
230             ioe = RemoteExceptionHandler.decodeRemoteException((RemoteException)e);
231           }
232           if (logScannerActivity && (ioe instanceof UnknownScannerException)) {
233             try {
234               HRegionLocation location =
235                 getConnection().relocateRegion(getTableName(), scan.getStartRow());
236               LOG.info("Scanner=" + scannerId
237                 + " expired, current region location is " + location.toString());
238             } catch (Throwable t) {
239               LOG.info("Failed to relocate region", t);
240             }
241           }
242           // The below convertion of exceptions into DoNotRetryExceptions is a little strange.
243           // Why not just have these exceptions implment DNRIOE you ask?  Well, usually we want
244           // ServerCallable#withRetries to just retry when it gets these exceptions.  In here in
245           // a scan when doing a next in particular, we want to break out and get the scanner to
246           // reset itself up again.  Throwing a DNRIOE is how we signal this to happen (its ugly,
247           // yeah and hard to follow and in need of a refactor).
248           if (ioe instanceof NotServingRegionException) {
249             // Throw a DNRE so that we break out of cycle of calling NSRE
250             // when what we need is to open scanner against new location.
251             // Attach NSRE to signal client that it needs to re-setup scanner.
252             if (this.scanMetrics != null) {
253               this.scanMetrics.countOfNSRE.incrementAndGet();
254             }
255             throw new DoNotRetryIOException("Resetting the scanner -- see exception cause", ioe);
256           } else if (ioe instanceof RegionServerStoppedException) {
257             // Throw a DNRE so that we break out of cycle of the retries and instead go and
258             // open scanner against new location.
259             throw new DoNotRetryIOException("Resetting the scanner -- see exception cause", ioe);
260           } else {
261             // The outer layers will retry
262             throw ioe;
263           }
264         }
265         return rrs;
266       }
267     }
268     return null;
269   }
270 
271   private void incRPCcallsMetrics() {
272     if (this.scanMetrics == null) {
273       return;
274     }
275     this.scanMetrics.countOfRPCcalls.incrementAndGet();
276     if (isRegionServerRemote) {
277       this.scanMetrics.countOfRemoteRPCcalls.incrementAndGet();
278     }
279   }
280 
281   protected void updateResultsMetrics(Result[] rrs) {
282     if (this.scanMetrics == null || rrs == null || rrs.length == 0) {
283       return;
284     }
285     long resultSize = 0;
286     for (Result rr : rrs) {
287       for (Cell kv : rr.rawCells()) {
288         // TODO add getLength to Cell/use CellUtil#estimatedSizeOf
289         resultSize += KeyValueUtil.ensureKeyValue(kv).getLength();
290       }
291     }
292     this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
293     if (isRegionServerRemote) {
294       this.scanMetrics.countOfBytesInRemoteResults.addAndGet(resultSize);
295     }
296   }
297 
298   private void close() {
299     if (this.scannerId == -1L) {
300       return;
301     }
302     try {
303       incRPCcallsMetrics();
304       ScanRequest request =
305         RequestConverter.buildScanRequest(this.scannerId, 0, true);
306       try {
307         getStub().scan(controller, request);
308       } catch (ServiceException se) {
309         throw ProtobufUtil.getRemoteException(se);
310       }
311     } catch (IOException e) {
312       LOG.warn("Ignore, probably already closed", e);
313     }
314     this.scannerId = -1L;
315   }
316 
317   protected long openScanner() throws IOException {
318     incRPCcallsMetrics();
319     ScanRequest request =
320       RequestConverter.buildScanRequest(
321         getLocation().getRegionInfo().getRegionName(),
322         this.scan, 0, false);
323     try {
324       ScanResponse response = getStub().scan(controller, request);
325       long id = response.getScannerId();
326       if (logScannerActivity) {
327         LOG.info("Open scanner=" + id + " for scan=" + scan.toString()
328           + " on region " + getLocation().toString());
329       }
330       return id;
331     } catch (ServiceException se) {
332       throw ProtobufUtil.getRemoteException(se);
333     }
334   }
335 
336   protected Scan getScan() {
337     return scan;
338   }
339 
340   /**
341    * Call this when the next invocation of call should close the scanner
342    */
343   public void setClose() {
344     this.closed = true;
345   }
346 
347   public void setRenew(boolean val) {
348     this.renew = val;
349   }
350 
351   /**
352    * @return the HRegionInfo for the current region
353    */
354   public HRegionInfo getHRegionInfo() {
355     if (!instantiated) {
356       return null;
357     }
358     return getLocation().getRegionInfo();
359   }
360 
361   /**
362    * Get the number of rows that will be fetched on next
363    * @return the number of rows for caching
364    */
365   public int getCaching() {
366     return caching;
367   }
368 
369   /**
370    * Set the number of rows that will be fetched on next
371    * @param caching the number of rows for caching
372    */
373   public void setCaching(int caching) {
374     this.caching = caching;
375   }
376 }