View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import java.io.Closeable;
22  import java.io.IOException;
23  import java.io.InterruptedIOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.LinkedList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.NavigableMap;
30  import java.util.TreeMap;
31  import java.util.concurrent.Callable;
32  import java.util.concurrent.ExecutionException;
33  import java.util.concurrent.ExecutorService;
34  import java.util.concurrent.Future;
35  import java.util.concurrent.SynchronousQueue;
36  import java.util.concurrent.ThreadPoolExecutor;
37  import java.util.concurrent.TimeUnit;
38  
39  import com.google.protobuf.InvalidProtocolBufferException;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  import org.apache.hadoop.hbase.classification.InterfaceAudience;
44  import org.apache.hadoop.hbase.classification.InterfaceStability;
45  import org.apache.hadoop.conf.Configuration;
46  import org.apache.hadoop.hbase.Cell;
47  import org.apache.hadoop.hbase.DoNotRetryIOException;
48  import org.apache.hadoop.hbase.HBaseConfiguration;
49  import org.apache.hadoop.hbase.HConstants;
50  import org.apache.hadoop.hbase.HRegionInfo;
51  import org.apache.hadoop.hbase.HRegionLocation;
52  import org.apache.hadoop.hbase.HTableDescriptor;
53  import org.apache.hadoop.hbase.KeyValue;
54  import org.apache.hadoop.hbase.KeyValueUtil;
55  import org.apache.hadoop.hbase.ServerName;
56  import org.apache.hadoop.hbase.TableName;
57  import org.apache.hadoop.hbase.client.coprocessor.Batch;
58  import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback;
59  import org.apache.hadoop.hbase.filter.BinaryComparator;
60  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
61  import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
62  import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
63  import org.apache.hadoop.hbase.ipc.RegionCoprocessorRpcChannel;
64  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
65  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
66  import org.apache.hadoop.hbase.protobuf.RequestConverter;
67  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
68  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
69  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateRequest;
70  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateResponse;
71  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionAction;
72  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.CompareType;
73  import org.apache.hadoop.hbase.util.Bytes;
74  import org.apache.hadoop.hbase.util.Pair;
75  import org.apache.hadoop.hbase.util.Threads;
76  
77  import com.google.protobuf.Descriptors;
78  import com.google.protobuf.Message;
79  import com.google.protobuf.Service;
80  import com.google.protobuf.ServiceException;
81  
82  /**
83   * <p>Used to communicate with a single HBase table.  An implementation of
84   * {@link HTableInterface}.  Instances of this class can be constructed directly but it is
85   * encouraged that users get instances via {@link HConnection} and {@link HConnectionManager}.
86   * See {@link HConnectionManager} class comment for an example.
87   *
88   * <p>This class is not thread safe for reads nor write.
89   *
90   * <p>In case of writes (Put, Delete), the underlying write buffer can
91   * be corrupted if multiple threads contend over a single HTable instance.
92   *
93   * <p>In case of reads, some fields used by a Scan are shared among all threads.
94   * The HTable implementation can either not contract to be safe in case of a Get
95   *
96   * <p>Instances of HTable passed the same {@link Configuration} instance will
97   * share connections to servers out on the cluster and to the zookeeper ensemble
98   * as well as caches of region locations.  This is usually a *good* thing and it
99   * is recommended to reuse the same configuration object for all your tables.
100  * This happens because they will all share the same underlying
101  * {@link HConnection} instance. See {@link HConnectionManager} for more on
102  * how this mechanism works.
103  *
104  * <p>{@link HConnection} will read most of the
105  * configuration it needs from the passed {@link Configuration} on initial
106  * construction.  Thereafter, for settings such as
107  * <code>hbase.client.pause</code>, <code>hbase.client.retries.number</code>,
108  * and <code>hbase.client.rpc.maxattempts</code> updating their values in the
109  * passed {@link Configuration} subsequent to {@link HConnection} construction
110  * will go unnoticed.  To run with changed values, make a new
111  * {@link HTable} passing a new {@link Configuration} instance that has the
112  * new configuration.
113  *
114  * <p>Note that this class implements the {@link Closeable} interface. When a
115  * HTable instance is no longer required, it *should* be closed in order to ensure
116  * that the underlying resources are promptly released. Please note that the close
117  * method can throw java.io.IOException that must be handled.
118  *
119  * @see HBaseAdmin for create, drop, list, enable and disable of tables.
120  * @see HConnection
121  * @see HConnectionManager
122  */
123 @InterfaceAudience.Public
124 @InterfaceStability.Stable
125 public class HTable implements HTableInterface {
126   private static final Log LOG = LogFactory.getLog(HTable.class);
127   protected HConnection connection;
128   private final TableName tableName;
129   private volatile Configuration configuration;
130   private TableConfiguration tableConfiguration;
131   protected List<Row> writeAsyncBuffer = new LinkedList<Row>();
132   private long writeBufferSize;
133   private boolean clearBufferOnFail;
134   private boolean autoFlush;
135   protected long currentWriteBufferSize;
136   protected int scannerCaching;
137   private ExecutorService pool;  // For Multi
138   private boolean closed;
139   private int operationTimeout;
140   private final boolean cleanupPoolOnClose; // shutdown the pool in close()
141   private final boolean cleanupConnectionOnClose; // close the connection in close()
142 
143   /** The Async process for puts with autoflush set to false or multiputs */
144   protected AsyncProcess<Object> ap;
145   private RpcRetryingCallerFactory rpcCallerFactory;
146   private RpcControllerFactory rpcControllerFactory;
147 
148   /**
149    * Creates an object to access a HBase table.
150    * Shares zookeeper connection and other resources with other HTable instances
151    * created with the same <code>conf</code> instance.  Uses already-populated
152    * region cache if one is available, populated by any other HTable instances
153    * sharing this <code>conf</code> instance.  Recommended.
154    * @param conf Configuration object to use.
155    * @param tableName Name of the table.
156    * @throws IOException if a remote or network exception occurs
157    */
158   public HTable(Configuration conf, final String tableName)
159   throws IOException {
160     this(conf, TableName.valueOf(tableName));
161   }
162 
163   /**
164    * Creates an object to access a HBase table.
165    * Shares zookeeper connection and other resources with other HTable instances
166    * created with the same <code>conf</code> instance.  Uses already-populated
167    * region cache if one is available, populated by any other HTable instances
168    * sharing this <code>conf</code> instance.  Recommended.
169    * @param conf Configuration object to use.
170    * @param tableName Name of the table.
171    * @throws IOException if a remote or network exception occurs
172    */
173   public HTable(Configuration conf, final byte[] tableName)
174   throws IOException {
175     this(conf, TableName.valueOf(tableName));
176   }
177 
178 
179 
180   /**
181    * Creates an object to access a HBase table.
182    * Shares zookeeper connection and other resources with other HTable instances
183    * created with the same <code>conf</code> instance.  Uses already-populated
184    * region cache if one is available, populated by any other HTable instances
185    * sharing this <code>conf</code> instance.  Recommended.
186    * @param conf Configuration object to use.
187    * @param tableName table name pojo
188    * @throws IOException if a remote or network exception occurs
189    */
190   public HTable(Configuration conf, final TableName tableName)
191   throws IOException {
192     this.tableName = tableName;
193     this.cleanupPoolOnClose = this.cleanupConnectionOnClose = true;
194     if (conf == null) {
195       this.connection = null;
196       return;
197     }
198     this.connection = HConnectionManager.getConnection(conf);
199     this.configuration = conf;
200 
201     this.pool = getDefaultExecutor(conf);
202     this.finishSetup();
203   }
204 
205   /**
206    * Creates an object to access a HBase table. Shares zookeeper connection and other resources with
207    * other HTable instances created with the same <code>connection</code> instance. Use this
208    * constructor when the HConnection instance is externally managed.
209    * @param tableName Name of the table.
210    * @param connection HConnection to be used.
211    * @throws IOException if a remote or network exception occurs
212    */
213   public HTable(TableName tableName, HConnection connection) throws IOException {
214     this.tableName = tableName;
215     this.cleanupPoolOnClose = true;
216     this.cleanupConnectionOnClose = false;
217     this.connection = connection;
218     this.configuration = connection.getConfiguration();
219 
220     this.pool = getDefaultExecutor(this.configuration);
221     this.finishSetup();
222   }
223    
224   public static ThreadPoolExecutor getDefaultExecutor(Configuration conf) {
225     int maxThreads = conf.getInt("hbase.htable.threads.max", Integer.MAX_VALUE);
226     if (maxThreads == 0) {
227       maxThreads = 1; // is there a better default?
228     }
229     long keepAliveTime = conf.getLong("hbase.htable.threads.keepalivetime", 60);
230 
231     // Using the "direct handoff" approach, new threads will only be created
232     // if it is necessary and will grow unbounded. This could be bad but in HCM
233     // we only create as many Runnables as there are region servers. It means
234     // it also scales when new region servers are added.
235     ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS,
236         new SynchronousQueue<Runnable>(), Threads.newDaemonThreadFactory("htable"));
237     ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
238     return pool;
239   }
240 
241   /**
242    * Creates an object to access a HBase table.
243    * Shares zookeeper connection and other resources with other HTable instances
244    * created with the same <code>conf</code> instance.  Uses already-populated
245    * region cache if one is available, populated by any other HTable instances
246    * sharing this <code>conf</code> instance.
247    * Use this constructor when the ExecutorService is externally managed.
248    * @param conf Configuration object to use.
249    * @param tableName Name of the table.
250    * @param pool ExecutorService to be used.
251    * @throws IOException if a remote or network exception occurs
252    */
253   public HTable(Configuration conf, final byte[] tableName, final ExecutorService pool)
254       throws IOException {
255     this(conf, TableName.valueOf(tableName), pool);
256   }
257 
258   /**
259    * Creates an object to access a HBase table.
260    * Shares zookeeper connection and other resources with other HTable instances
261    * created with the same <code>conf</code> instance.  Uses already-populated
262    * region cache if one is available, populated by any other HTable instances
263    * sharing this <code>conf</code> instance.
264    * Use this constructor when the ExecutorService is externally managed.
265    * @param conf Configuration object to use.
266    * @param tableName Name of the table.
267    * @param pool ExecutorService to be used.
268    * @throws IOException if a remote or network exception occurs
269    */
270   public HTable(Configuration conf, final TableName tableName, final ExecutorService pool)
271       throws IOException {
272     this.connection = HConnectionManager.getConnection(conf);
273     this.configuration = conf;
274     this.pool = pool;
275     this.tableName = tableName;
276     this.cleanupPoolOnClose = false;
277     this.cleanupConnectionOnClose = true;
278 
279     this.finishSetup();
280   }
281 
282   /**
283    * Creates an object to access a HBase table.
284    * Shares zookeeper connection and other resources with other HTable instances
285    * created with the same <code>connection</code> instance.
286    * Use this constructor when the ExecutorService and HConnection instance are
287    * externally managed.
288    * @param tableName Name of the table.
289    * @param connection HConnection to be used.
290    * @param pool ExecutorService to be used.
291    * @throws IOException if a remote or network exception occurs
292    */
293   public HTable(final byte[] tableName, final HConnection connection,
294       final ExecutorService pool) throws IOException {
295     this(TableName.valueOf(tableName), connection, pool);
296   }
297 
298   /**
299    * Creates an object to access a HBase table.
300    * Shares zookeeper connection and other resources with other HTable instances
301    * created with the same <code>connection</code> instance.
302    * Use this constructor when the ExecutorService and HConnection instance are
303    * externally managed.
304    * @param tableName Name of the table.
305    * @param connection HConnection to be used.
306    * @param pool ExecutorService to be used.
307    * @throws IOException if a remote or network exception occurs
308    */
309   public HTable(TableName tableName, final HConnection connection,
310       final ExecutorService pool) throws IOException {
311     this(tableName, connection, null, null, null, pool);
312   }
313 
314   /**
315    * Creates an object to access a HBase table.
316    * Shares zookeeper connection and other resources with other HTable instances
317    * created with the same <code>connection</code> instance.
318    * Use this constructor when the ExecutorService and HConnection instance are
319    * externally managed.
320    * @param tableName Name of the table.
321    * @param connection HConnection to be used.
322    * @param tableConfig table configuration
323    * @param rpcCallerFactory RPC caller factory
324    * @param rpcControllerFactory RPC controller factory
325    * @param pool ExecutorService to be used.
326    * @throws IOException if a remote or network exception occurs
327    */
328   public HTable(TableName tableName, final HConnection connection,
329       final TableConfiguration tableConfig,
330       final RpcRetryingCallerFactory rpcCallerFactory,
331       final RpcControllerFactory rpcControllerFactory,
332       final ExecutorService pool) throws IOException {
333     if (connection == null || connection.isClosed()) {
334       throw new IllegalArgumentException("Connection is null or closed.");
335     }
336     this.tableName = tableName;
337     this.connection = connection;
338     this.configuration = connection.getConfiguration();
339     this.tableConfiguration = tableConfig;
340     this.cleanupPoolOnClose = this.cleanupConnectionOnClose = false;
341     this.pool = pool;
342 
343     this.rpcCallerFactory = rpcCallerFactory;
344     this.rpcControllerFactory = rpcControllerFactory;
345 
346     this.finishSetup();
347   }
348 
349   /**
350    * For internal testing.
351    */
352   protected HTable(){
353     tableName = null;
354     tableConfiguration = new TableConfiguration();
355     cleanupPoolOnClose = false;
356     cleanupConnectionOnClose = false;
357   }
358 
359   /**
360    * @return maxKeyValueSize from configuration.
361    */
362   public static int getMaxKeyValueSize(Configuration conf) {
363     return conf.getInt("hbase.client.keyvalue.maxsize", -1);
364   }
365 
366   /**
367    * setup this HTable's parameter based on the passed configuration
368    */
369   private void finishSetup() throws IOException {
370     if (tableConfiguration == null) {
371       tableConfiguration = new TableConfiguration(configuration);
372     }
373     this.operationTimeout = tableName.isSystemTable() ?
374       tableConfiguration.getMetaOperationTimeout() : tableConfiguration.getOperationTimeout();
375     this.writeBufferSize = tableConfiguration.getWriteBufferSize();
376     this.clearBufferOnFail = true;
377     this.autoFlush = true;
378     this.currentWriteBufferSize = 0;
379     this.scannerCaching = tableConfiguration.getScannerCaching();
380 
381     if (this.rpcCallerFactory == null) {
382       this.rpcCallerFactory = RpcRetryingCallerFactory.instantiate(configuration,
383         this.connection.getStatisticsTracker());
384     }
385     if (this.rpcControllerFactory == null) {
386       this.rpcControllerFactory = RpcControllerFactory.instantiate(configuration);
387     }
388 
389     ap = new AsyncProcess<Object>(connection, tableName, pool, null, configuration,
390       rpcCallerFactory, rpcControllerFactory);
391 
392     this.closed = false;
393   }
394 
395   /**
396    * {@inheritDoc}
397    */
398   @Override
399   public Configuration getConfiguration() {
400     return configuration;
401   }
402 
403   /**
404    * Tells whether or not a table is enabled or not. This method creates a
405    * new HBase configuration, so it might make your unit tests fail due to
406    * incorrect ZK client port.
407    * @param tableName Name of table to check.
408    * @return {@code true} if table is online.
409    * @throws IOException if a remote or network exception occurs
410 	* @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
411    */
412   @Deprecated
413   public static boolean isTableEnabled(String tableName) throws IOException {
414     return isTableEnabled(TableName.valueOf(tableName));
415   }
416 
417   /**
418    * Tells whether or not a table is enabled or not. This method creates a
419    * new HBase configuration, so it might make your unit tests fail due to
420    * incorrect ZK client port.
421    * @param tableName Name of table to check.
422    * @return {@code true} if table is online.
423    * @throws IOException if a remote or network exception occurs
424 	* @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
425    */
426   @Deprecated
427   public static boolean isTableEnabled(byte[] tableName) throws IOException {
428     return isTableEnabled(TableName.valueOf(tableName));
429   }
430 
431   /**
432    * Tells whether or not a table is enabled or not. This method creates a
433    * new HBase configuration, so it might make your unit tests fail due to
434    * incorrect ZK client port.
435    * @param tableName Name of table to check.
436    * @return {@code true} if table is online.
437    * @throws IOException if a remote or network exception occurs
438    * @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
439    */
440   @Deprecated
441   public static boolean isTableEnabled(TableName tableName) throws IOException {
442     return isTableEnabled(HBaseConfiguration.create(), tableName);
443   }
444 
445   /**
446    * Tells whether or not a table is enabled or not.
447    * @param conf The Configuration object to use.
448    * @param tableName Name of table to check.
449    * @return {@code true} if table is online.
450    * @throws IOException if a remote or network exception occurs
451 	 * @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
452    */
453   @Deprecated
454   public static boolean isTableEnabled(Configuration conf, String tableName)
455   throws IOException {
456     return isTableEnabled(conf, TableName.valueOf(tableName));
457   }
458 
459   /**
460    * Tells whether or not a table is enabled or not.
461    * @param conf The Configuration object to use.
462    * @param tableName Name of table to check.
463    * @return {@code true} if table is online.
464    * @throws IOException if a remote or network exception occurs
465 	 * @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
466    */
467   @Deprecated
468   public static boolean isTableEnabled(Configuration conf, byte[] tableName)
469   throws IOException {
470     return isTableEnabled(conf, TableName.valueOf(tableName));
471   }
472 
473   /**
474    * Tells whether or not a table is enabled or not.
475    * @param conf The Configuration object to use.
476    * @param tableName Name of table to check.
477    * @return {@code true} if table is online.
478    * @throws IOException if a remote or network exception occurs
479    * @deprecated use {@link HBaseAdmin#isTableEnabled(org.apache.hadoop.hbase.TableName tableName)}
480    */
481   @Deprecated
482   public static boolean isTableEnabled(Configuration conf,
483       final TableName tableName) throws IOException {
484     return HConnectionManager.execute(new HConnectable<Boolean>(conf) {
485       @Override
486       public Boolean connect(HConnection connection) throws IOException {
487         return connection.isTableEnabled(tableName);
488       }
489     });
490   }
491 
492   /**
493    * Find region location hosting passed row using cached info
494    * @param row Row to find.
495    * @return The location of the given row.
496    * @throws IOException if a remote or network exception occurs
497    */
498   public HRegionLocation getRegionLocation(final String row)
499   throws IOException {
500     return connection.getRegionLocation(tableName, Bytes.toBytes(row), false);
501   }
502 
503   /**
504    * Finds the region on which the given row is being served. Does not reload the cache.
505    * @param row Row to find.
506    * @return Location of the row.
507    * @throws IOException if a remote or network exception occurs
508    */
509   public HRegionLocation getRegionLocation(final byte [] row)
510   throws IOException {
511     return connection.getRegionLocation(tableName, row, false);
512   }
513 
514   /**
515    * Finds the region on which the given row is being served.
516    * @param row Row to find.
517    * @param reload true to reload information or false to use cached information
518    * @return Location of the row.
519    * @throws IOException if a remote or network exception occurs
520    */
521   public HRegionLocation getRegionLocation(final byte [] row, boolean reload)
522   throws IOException {
523     return connection.getRegionLocation(tableName, row, reload);
524   }
525 
526   /**
527    * {@inheritDoc}
528    */
529   @Override
530   public byte [] getTableName() {
531     return this.tableName.getName();
532   }
533 
534   @Override
535   public TableName getName() {
536     return tableName;
537   }
538 
539   /**
540    * <em>INTERNAL</em> Used by unit tests and tools to do low-level
541    * manipulations.
542    * @return An HConnection instance.
543    * @deprecated This method will be changed from public to package protected.
544    */
545   // TODO(tsuna): Remove this.  Unit tests shouldn't require public helpers.
546   @Deprecated
547   public HConnection getConnection() {
548     return this.connection;
549   }
550 
551   /**
552    * Gets the number of rows that a scanner will fetch at once.
553    * <p>
554    * The default value comes from {@code hbase.client.scanner.caching}.
555    * @deprecated Use {@link Scan#setCaching(int)} and {@link Scan#getCaching()}
556    */
557   @Deprecated
558   public int getScannerCaching() {
559     return scannerCaching;
560   }
561 
562   /**
563    * Kept in 0.96 for backward compatibility
564    * @deprecated  since 0.96. This is an internal buffer that should not be read nor write.
565    */
566   @Deprecated
567   public List<Row> getWriteBuffer() {
568     return writeAsyncBuffer;
569   }
570 
571   /**
572    * Sets the number of rows that a scanner will fetch at once.
573    * <p>
574    * This will override the value specified by
575    * {@code hbase.client.scanner.caching}.
576    * Increasing this value will reduce the amount of work needed each time
577    * {@code next()} is called on a scanner, at the expense of memory use
578    * (since more rows will need to be maintained in memory by the scanners).
579    * @param scannerCaching the number of rows a scanner will fetch at once.
580    * @deprecated Use {@link Scan#setCaching(int)}
581    */
582   @Deprecated
583   public void setScannerCaching(int scannerCaching) {
584     this.scannerCaching = scannerCaching;
585   }
586 
587   /**
588    * {@inheritDoc}
589    */
590   @Override
591   public HTableDescriptor getTableDescriptor() throws IOException {
592     return new UnmodifyableHTableDescriptor(
593       this.connection.getHTableDescriptor(this.tableName));
594   }
595 
596   /**
597    * Gets the starting row key for every region in the currently open table.
598    * <p>
599    * This is mainly useful for the MapReduce integration.
600    * @return Array of region starting row keys
601    * @throws IOException if a remote or network exception occurs
602    */
603   public byte [][] getStartKeys() throws IOException {
604     return getStartEndKeys().getFirst();
605   }
606 
607   /**
608    * Gets the ending row key for every region in the currently open table.
609    * <p>
610    * This is mainly useful for the MapReduce integration.
611    * @return Array of region ending row keys
612    * @throws IOException if a remote or network exception occurs
613    */
614   public byte[][] getEndKeys() throws IOException {
615     return getStartEndKeys().getSecond();
616   }
617 
618   /**
619    * Gets the starting and ending row keys for every region in the currently
620    * open table.
621    * <p>
622    * This is mainly useful for the MapReduce integration.
623    * @return Pair of arrays of region starting and ending row keys
624    * @throws IOException if a remote or network exception occurs
625    */
626   public Pair<byte[][],byte[][]> getStartEndKeys() throws IOException {
627     NavigableMap<HRegionInfo, ServerName> regions = getRegionLocations();
628     final List<byte[]> startKeyList = new ArrayList<byte[]>(regions.size());
629     final List<byte[]> endKeyList = new ArrayList<byte[]>(regions.size());
630 
631     for (HRegionInfo region : regions.keySet()) {
632       startKeyList.add(region.getStartKey());
633       endKeyList.add(region.getEndKey());
634     }
635 
636     return new Pair<byte [][], byte [][]>(
637       startKeyList.toArray(new byte[startKeyList.size()][]),
638       endKeyList.toArray(new byte[endKeyList.size()][]));
639   }
640 
641   /**
642    * Gets all the regions and their address for this table.
643    * <p>
644    * This is mainly useful for the MapReduce integration.
645    * @return A map of HRegionInfo with it's server address
646    * @throws IOException if a remote or network exception occurs
647    */
648   public NavigableMap<HRegionInfo, ServerName> getRegionLocations() throws IOException {
649     // TODO: Odd that this returns a Map of HRI to SN whereas getRegionLocation, singular, returns an HRegionLocation.
650     return MetaScanner.allTableRegions(getConfiguration(), this.connection, getName(), false);
651   }
652 
653   /**
654    * Get the corresponding regions for an arbitrary range of keys.
655    * <p>
656    * @param startKey Starting row in range, inclusive
657    * @param endKey Ending row in range, exclusive
658    * @return A list of HRegionLocations corresponding to the regions that
659    * contain the specified range
660    * @throws IOException if a remote or network exception occurs
661    */
662   public List<HRegionLocation> getRegionsInRange(final byte [] startKey,
663     final byte [] endKey) throws IOException {
664     return getRegionsInRange(startKey, endKey, false);
665   }
666 
667   /**
668    * Get the corresponding regions for an arbitrary range of keys.
669    * <p>
670    * @param startKey Starting row in range, inclusive
671    * @param endKey Ending row in range, exclusive
672    * @param reload true to reload information or false to use cached information
673    * @return A list of HRegionLocations corresponding to the regions that
674    * contain the specified range
675    * @throws IOException if a remote or network exception occurs
676    */
677   public List<HRegionLocation> getRegionsInRange(final byte [] startKey,
678       final byte [] endKey, final boolean reload) throws IOException {
679     return getKeysAndRegionsInRange(startKey, endKey, false, reload).getSecond();
680   }
681 
682   /**
683    * Get the corresponding start keys and regions for an arbitrary range of
684    * keys.
685    * <p>
686    * @param startKey Starting row in range, inclusive
687    * @param endKey Ending row in range
688    * @param includeEndKey true if endRow is inclusive, false if exclusive
689    * @return A pair of list of start keys and list of HRegionLocations that
690    *         contain the specified range
691    * @throws IOException if a remote or network exception occurs
692    */
693   private Pair<List<byte[]>, List<HRegionLocation>> getKeysAndRegionsInRange(
694       final byte[] startKey, final byte[] endKey, final boolean includeEndKey)
695       throws IOException {
696     return getKeysAndRegionsInRange(startKey, endKey, includeEndKey, false);
697   }
698 
699   /**
700    * Get the corresponding start keys and regions for an arbitrary range of
701    * keys.
702    * <p>
703    * @param startKey Starting row in range, inclusive
704    * @param endKey Ending row in range
705    * @param includeEndKey true if endRow is inclusive, false if exclusive
706    * @param reload true to reload information or false to use cached information
707    * @return A pair of list of start keys and list of HRegionLocations that
708    *         contain the specified range
709    * @throws IOException if a remote or network exception occurs
710    */
711   private Pair<List<byte[]>, List<HRegionLocation>> getKeysAndRegionsInRange(
712       final byte[] startKey, final byte[] endKey, final boolean includeEndKey,
713       final boolean reload) throws IOException {
714     final boolean endKeyIsEndOfTable = Bytes.equals(endKey,HConstants.EMPTY_END_ROW);
715     if ((Bytes.compareTo(startKey, endKey) > 0) && !endKeyIsEndOfTable) {
716       throw new IllegalArgumentException(
717         "Invalid range: " + Bytes.toStringBinary(startKey) +
718         " > " + Bytes.toStringBinary(endKey));
719     }
720     List<byte[]> keysInRange = new ArrayList<byte[]>();
721     List<HRegionLocation> regionsInRange = new ArrayList<HRegionLocation>();
722     byte[] currentKey = startKey;
723     do {
724       HRegionLocation regionLocation = getRegionLocation(currentKey, reload);
725       keysInRange.add(currentKey);
726       regionsInRange.add(regionLocation);
727       currentKey = regionLocation.getRegionInfo().getEndKey();
728     } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW)
729         && (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0
730             || (includeEndKey && Bytes.compareTo(currentKey, endKey) == 0)));
731     return new Pair<List<byte[]>, List<HRegionLocation>>(keysInRange,
732         regionsInRange);
733   }
734 
735   /**
736    * {@inheritDoc}
737    */
738    @Override
739    public Result getRowOrBefore(final byte[] row, final byte[] family)
740    throws IOException {
741      RegionServerCallable<Result> callable = new RegionServerCallable<Result>(this.connection,
742          tableName, row) {
743        public Result call() throws IOException {
744             return ProtobufUtil.getRowOrBefore(getStub(), getLocation().getRegionInfo()
745                 .getRegionName(), row, family, rpcControllerFactory.newController());
746           }
747      };
748     return rpcCallerFactory.<Result> newCaller().callWithRetries(callable, this.operationTimeout);
749   }
750 
751    /**
752     * {@inheritDoc}
753     */
754   @Override
755   public ResultScanner getScanner(final Scan scan) throws IOException {
756     if (scan.getBatch() > 0 && scan.isSmall()) {
757       throw new IllegalArgumentException("Small scan should not be used with batching");
758     }
759     if (scan.getCaching() <= 0) {
760       scan.setCaching(getScannerCaching());
761     }
762 
763     if (scan.isReversed()) {
764       if (scan.isSmall()) {
765         return new ClientSmallReversedScanner(getConfiguration(), scan, getName(),
766             this.connection);
767       } else {
768         return new ReversedClientScanner(getConfiguration(), scan, getName(), this.connection);
769       }
770     }
771 
772     if (scan.isSmall()) {
773       return new ClientSmallScanner(getConfiguration(), scan, getName(), this.connection);
774     } else {
775       return new ClientScanner(getConfiguration(), scan, getName(), this.connection);
776     }
777   }
778 
779   /**
780    * {@inheritDoc}
781    */
782   @Override
783   public ResultScanner getScanner(byte [] family) throws IOException {
784     Scan scan = new Scan();
785     scan.addFamily(family);
786     return getScanner(scan);
787   }
788 
789   /**
790    * {@inheritDoc}
791    */
792   @Override
793   public ResultScanner getScanner(byte [] family, byte [] qualifier)
794   throws IOException {
795     Scan scan = new Scan();
796     scan.addColumn(family, qualifier);
797     return getScanner(scan);
798   }
799 
800   /**
801    * {@inheritDoc}
802    */
803   @Override
804   public Result get(final Get get) throws IOException {
805     // have to instanatiate this and set the priority here since in protobuf util we don't pass in
806     // the tablename... an unfortunate side-effect of public interfaces :-/ In 0.99+ we put all the
807     // logic back into HTable
808     final PayloadCarryingRpcController controller = rpcControllerFactory.newController();
809     controller.setPriority(tableName);
810     RegionServerCallable<Result> callable =
811         new RegionServerCallable<Result>(this.connection, getName(), get.getRow()) {
812           public Result call() throws IOException {
813             return ProtobufUtil.get(getStub(), getLocation().getRegionInfo().getRegionName(), get,
814               controller);
815           }
816         };
817     return rpcCallerFactory.<Result> newCaller().callWithRetries(callable, this.operationTimeout);
818   }
819 
820   /**
821    * {@inheritDoc}
822    */
823   @Override
824   public Result[] get(List<Get> gets) throws IOException {
825     if (gets.size() == 1) {
826       return new Result[]{get(gets.get(0))};
827     }
828     try {
829       Object [] r1 = batch((List)gets);
830 
831       // translate.
832       Result [] results = new Result[r1.length];
833       int i=0;
834       for (Object o : r1) {
835         // batch ensures if there is a failure we get an exception instead
836         results[i++] = (Result) o;
837       }
838 
839       return results;
840     } catch (InterruptedException e) {
841       throw (InterruptedIOException)new InterruptedIOException().initCause(e);
842     }
843   }
844 
845   /**
846    * {@inheritDoc}
847    */
848   @Override
849   public void batch(final List<?extends Row> actions, final Object[] results)
850       throws InterruptedException, IOException {
851     batchCallback(actions, results, null);
852   }
853 
854   /**
855    * {@inheritDoc}
856    * @deprecated If any exception is thrown by one of the actions, there is no way to
857    * retrieve the partially executed results. Use {@link #batch(List, Object[])} instead.
858    */
859   @Override
860   public Object[] batch(final List<? extends Row> actions)
861      throws InterruptedException, IOException {
862     return batchCallback(actions, null);
863   }
864 
865   /**
866    * {@inheritDoc}
867    */
868   @Override
869   public <R> void batchCallback(
870       final List<? extends Row> actions, final Object[] results, final Batch.Callback<R> callback)
871       throws IOException, InterruptedException {
872     connection.processBatchCallback(actions, tableName, pool, results, callback);
873   }
874 
875   /**
876    * {@inheritDoc}
877    * @deprecated If any exception is thrown by one of the actions, there is no way to
878    * retrieve the partially executed results. Use
879    * {@link #batchCallback(List, Object[], org.apache.hadoop.hbase.client.coprocessor.Batch.Callback)}
880    * instead.
881    */
882   @Override
883   public <R> Object[] batchCallback(
884     final List<? extends Row> actions, final Batch.Callback<R> callback) throws IOException,
885       InterruptedException {
886     Object[] results = new Object[actions.size()];
887     batchCallback(actions, results, callback);
888     return results;
889   }
890 
891   /**
892    * {@inheritDoc}
893    */
894   @Override
895   public void delete(final Delete delete)
896   throws IOException {
897     RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection,
898         tableName, delete.getRow()) {
899       public Boolean call() throws IOException {
900         try {
901           MutateRequest request = RequestConverter.buildMutateRequest(
902             getLocation().getRegionInfo().getRegionName(), delete);
903               PayloadCarryingRpcController controller = rpcControllerFactory.newController();
904               controller.setPriority(tableName);
905               MutateResponse response = getStub().mutate(controller, request);
906           return Boolean.valueOf(response.getProcessed());
907         } catch (ServiceException se) {
908           throw ProtobufUtil.getRemoteException(se);
909         }
910       }
911     };
912     rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout);
913   }
914 
915   /**
916    * {@inheritDoc}
917    */
918   @Override
919   public void delete(final List<Delete> deletes)
920   throws IOException {
921     Object[] results = new Object[deletes.size()];
922     try {
923       batch(deletes, results);
924     } catch (InterruptedException e) {
925       throw (InterruptedIOException)new InterruptedIOException().initCause(e);
926     } finally {
927       // mutate list so that it is empty for complete success, or contains only failed records
928       // results are returned in the same order as the requests in list
929       // walk the list backwards, so we can remove from list without impacting the indexes of earlier members
930       for (int i = results.length - 1; i>=0; i--) {
931         // if result is not null, it succeeded
932         if (results[i] instanceof Result) {
933           deletes.remove(i);
934         }
935       }
936     }
937   }
938 
939   /**
940    * {@inheritDoc}
941    */
942   @Override
943   public void put(final Put put)
944       throws InterruptedIOException, RetriesExhaustedWithDetailsException {
945     doPut(put);
946     if (autoFlush) {
947       flushCommits();
948     }
949   }
950 
951   /**
952    * {@inheritDoc}
953    */
954   @Override
955   public void put(final List<Put> puts)
956       throws InterruptedIOException, RetriesExhaustedWithDetailsException {
957     for (Put put : puts) {
958       doPut(put);
959     }
960     if (autoFlush) {
961       flushCommits();
962     }
963   }
964 
965 
966   /**
967    * Add the put to the buffer. If the buffer is already too large, sends the buffer to the
968    *  cluster.
969    * @throws RetriesExhaustedWithDetailsException if there is an error on the cluster.
970    * @throws InterruptedIOException if we were interrupted.
971    */
972   private void doPut(Put put) throws InterruptedIOException, RetriesExhaustedWithDetailsException {
973     if (ap.hasError()){
974       writeAsyncBuffer.add(put);
975       backgroundFlushCommits(true);
976     }
977 
978     validatePut(put);
979 
980     currentWriteBufferSize += put.heapSize();
981     writeAsyncBuffer.add(put);
982 
983     while (currentWriteBufferSize > writeBufferSize) {
984       backgroundFlushCommits(false);
985     }
986   }
987 
988 
989   /**
990    * Send the operations in the buffer to the servers. Does not wait for the server's answer.
991    * If the is an error (max retried reach from a previous flush or bad operation), it tries to
992    * send all operations in the buffer and sends an exception.
993    * @param synchronous - if true, sends all the writes and wait for all of them to finish before
994    *                     returning.
995    */
996   private void backgroundFlushCommits(boolean synchronous) throws
997       InterruptedIOException, RetriesExhaustedWithDetailsException {
998 
999     try {
1000       do {
1001         ap.submit(writeAsyncBuffer, true);
1002       } while (synchronous && !writeAsyncBuffer.isEmpty());
1003 
1004       if (synchronous) {
1005         ap.waitUntilDone();
1006       }
1007 
1008       if (ap.hasError()) {
1009         LOG.debug(tableName + ": One or more of the operations have failed -" +
1010             " waiting for all operation in progress to finish (successfully or not)");
1011         while (!writeAsyncBuffer.isEmpty()) {
1012           ap.submit(writeAsyncBuffer, true);
1013         }
1014         ap.waitUntilDone();
1015 
1016         if (!clearBufferOnFail) {
1017           // if clearBufferOnFailed is not set, we're supposed to keep the failed operation in the
1018           //  write buffer. This is a questionable feature kept here for backward compatibility
1019           writeAsyncBuffer.addAll(ap.getFailedOperations());
1020         }
1021         RetriesExhaustedWithDetailsException e = ap.getErrors();
1022         ap.clearErrors();
1023         throw e;
1024       }
1025     } finally {
1026       currentWriteBufferSize = 0;
1027       for (Row mut : writeAsyncBuffer) {
1028         if (mut instanceof Mutation) {
1029           currentWriteBufferSize += ((Mutation) mut).heapSize();
1030         }
1031       }
1032     }
1033   }
1034 
1035   /**
1036    * {@inheritDoc}
1037    */
1038   @Override
1039   public void mutateRow(final RowMutations rm) throws IOException {
1040     RegionServerCallable<Void> callable =
1041         new RegionServerCallable<Void>(connection, getName(), rm.getRow()) {
1042       public Void call() throws IOException {
1043         try {
1044           RegionAction.Builder regionMutationBuilder = RequestConverter.buildRegionAction(
1045             getLocation().getRegionInfo().getRegionName(), rm);
1046           regionMutationBuilder.setAtomic(true);
1047           MultiRequest request =
1048             MultiRequest.newBuilder().addRegionAction(regionMutationBuilder.build()).build();
1049           PayloadCarryingRpcController pcrc = rpcControllerFactory.newController();
1050           pcrc.setPriority(tableName);
1051           getStub().multi(pcrc, request);
1052         } catch (ServiceException se) {
1053           throw ProtobufUtil.getRemoteException(se);
1054         }
1055         return null;
1056       }
1057     };
1058     rpcCallerFactory.<Void> newCaller().callWithRetries(callable, this.operationTimeout);
1059   }
1060 
1061   /**
1062    * {@inheritDoc}
1063    */
1064   @Override
1065   public Result append(final Append append) throws IOException {
1066     if (append.numFamilies() == 0) {
1067       throw new IOException(
1068           "Invalid arguments to append, no columns specified");
1069     }
1070 
1071     NonceGenerator ng = this.connection.getNonceGenerator();
1072     final long nonceGroup = ng.getNonceGroup(), nonce = ng.newNonce();
1073     RegionServerCallable<Result> callable =
1074       new RegionServerCallable<Result>(this.connection, getName(), append.getRow()) {
1075         public Result call() throws IOException {
1076           try {
1077             MutateRequest request = RequestConverter.buildMutateRequest(
1078               getLocation().getRegionInfo().getRegionName(), append, nonceGroup, nonce);
1079             PayloadCarryingRpcController rpcController = rpcControllerFactory.newController();
1080             rpcController.setPriority(getTableName());
1081             MutateResponse response = getStub().mutate(rpcController, request);
1082             if (!response.hasResult()) return null;
1083             return ProtobufUtil.toResult(response.getResult(), rpcController.cellScanner());
1084           } catch (ServiceException se) {
1085             throw ProtobufUtil.getRemoteException(se);
1086           }
1087         }
1088       };
1089     return rpcCallerFactory.<Result> newCaller().callWithRetries(callable, this.operationTimeout);
1090   }
1091 
1092   /**
1093    * {@inheritDoc}
1094    */
1095   @Override
1096   public Result increment(final Increment increment) throws IOException {
1097     if (!increment.hasFamilies()) {
1098       throw new IOException(
1099           "Invalid arguments to increment, no columns specified");
1100     }
1101     NonceGenerator ng = this.connection.getNonceGenerator();
1102     final long nonceGroup = ng.getNonceGroup(), nonce = ng.newNonce();
1103     RegionServerCallable<Result> callable = new RegionServerCallable<Result>(this.connection,
1104         getName(), increment.getRow()) {
1105       public Result call() throws IOException {
1106         try {
1107           MutateRequest request = RequestConverter.buildMutateRequest(
1108             getLocation().getRegionInfo().getRegionName(), increment, nonceGroup, nonce);
1109           PayloadCarryingRpcController rpcController = rpcControllerFactory.newController();
1110           rpcController.setPriority(getTableName());
1111           MutateResponse response = getStub().mutate(rpcController, request);
1112           return ProtobufUtil.toResult(response.getResult(), rpcController.cellScanner());
1113         } catch (ServiceException se) {
1114           throw ProtobufUtil.getRemoteException(se);
1115         }
1116       }
1117     };
1118     return rpcCallerFactory.<Result> newCaller().callWithRetries(callable, this.operationTimeout);
1119   }
1120 
1121   /**
1122    * {@inheritDoc}
1123    */
1124   @Override
1125   public long incrementColumnValue(final byte [] row, final byte [] family,
1126       final byte [] qualifier, final long amount)
1127   throws IOException {
1128     return incrementColumnValue(row, family, qualifier, amount, Durability.SYNC_WAL);
1129   }
1130 
1131   /**
1132    * @deprecated Use {@link #incrementColumnValue(byte[], byte[], byte[], long, Durability)}
1133    */
1134   @Deprecated
1135   @Override
1136   public long incrementColumnValue(final byte [] row, final byte [] family,
1137       final byte [] qualifier, final long amount, final boolean writeToWAL)
1138   throws IOException {
1139     return incrementColumnValue(row, family, qualifier, amount,
1140       writeToWAL? Durability.SKIP_WAL: Durability.USE_DEFAULT);
1141   }
1142 
1143   /**
1144    * {@inheritDoc}
1145    */
1146   @Override
1147   public long incrementColumnValue(final byte [] row, final byte [] family,
1148       final byte [] qualifier, final long amount, final Durability durability)
1149   throws IOException {
1150     NullPointerException npe = null;
1151     if (row == null) {
1152       npe = new NullPointerException("row is null");
1153     } else if (family == null) {
1154       npe = new NullPointerException("family is null");
1155     } else if (qualifier == null) {
1156       npe = new NullPointerException("qualifier is null");
1157     }
1158     if (npe != null) {
1159       throw new IOException(
1160           "Invalid arguments to incrementColumnValue", npe);
1161     }
1162 
1163     NonceGenerator ng = this.connection.getNonceGenerator();
1164     final long nonceGroup = ng.getNonceGroup(), nonce = ng.newNonce();
1165     RegionServerCallable<Long> callable =
1166       new RegionServerCallable<Long>(connection, getName(), row) {
1167         public Long call() throws IOException {
1168           try {
1169             MutateRequest request = RequestConverter.buildIncrementRequest(
1170               getLocation().getRegionInfo().getRegionName(), row, family,
1171               qualifier, amount, durability, nonceGroup, nonce);
1172             PayloadCarryingRpcController rpcController = rpcControllerFactory.newController();
1173             rpcController.setPriority(getTableName());
1174             MutateResponse response = getStub().mutate(rpcController, request);
1175             Result result =
1176               ProtobufUtil.toResult(response.getResult(), rpcController.cellScanner());
1177             return Long.valueOf(Bytes.toLong(result.getValue(family, qualifier)));
1178           } catch (ServiceException se) {
1179             throw ProtobufUtil.getRemoteException(se);
1180           }
1181         }
1182       };
1183     return rpcCallerFactory.<Long> newCaller().callWithRetries(callable, this.operationTimeout);
1184   }
1185 
1186   /**
1187    * {@inheritDoc}
1188    */
1189   @Override
1190   public boolean checkAndPut(final byte [] row,
1191       final byte [] family, final byte [] qualifier, final byte [] value,
1192       final Put put)
1193   throws IOException {
1194     RegionServerCallable<Boolean> callable =
1195       new RegionServerCallable<Boolean>(connection, getName(), row) {
1196         public Boolean call() throws IOException {
1197           try {
1198             MutateRequest request = RequestConverter.buildMutateRequest(
1199               getLocation().getRegionInfo().getRegionName(), row, family, qualifier,
1200                 new BinaryComparator(value), CompareType.EQUAL, put);
1201             PayloadCarryingRpcController rpcController = rpcControllerFactory.newController();
1202             rpcController.setPriority(getTableName());
1203             MutateResponse response = getStub().mutate(rpcController, request);
1204             return Boolean.valueOf(response.getProcessed());
1205           } catch (ServiceException se) {
1206             throw ProtobufUtil.getRemoteException(se);
1207           }
1208         }
1209       };
1210     return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout);
1211   }
1212 
1213 
1214   /**
1215    * {@inheritDoc}
1216    */
1217   @Override
1218   public boolean checkAndDelete(final byte [] row,
1219       final byte [] family, final byte [] qualifier, final byte [] value,
1220       final Delete delete)
1221   throws IOException {
1222     RegionServerCallable<Boolean> callable =
1223       new RegionServerCallable<Boolean>(connection, getName(), row) {
1224         public Boolean call() throws IOException {
1225           try {
1226             MutateRequest request = RequestConverter.buildMutateRequest(
1227               getLocation().getRegionInfo().getRegionName(), row, family, qualifier,
1228                 new BinaryComparator(value), CompareType.EQUAL, delete);
1229             PayloadCarryingRpcController rpcController = rpcControllerFactory.newController();
1230             rpcController.setPriority(getTableName());
1231             MutateResponse response = getStub().mutate(rpcController, request);
1232             return Boolean.valueOf(response.getProcessed());
1233           } catch (ServiceException se) {
1234             throw ProtobufUtil.getRemoteException(se);
1235           }
1236         }
1237       };
1238     return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout);
1239   }
1240 
1241   /**
1242    * {@inheritDoc}
1243    */
1244   @Override
1245   public boolean checkAndMutate(final byte [] row, final byte [] family, final byte [] qualifier,
1246       final CompareOp compareOp, final byte [] value, final RowMutations rm)
1247   throws IOException {
1248     RegionServerCallable<Boolean> callable =
1249         new RegionServerCallable<Boolean>(connection, getName(), row) {
1250           @Override
1251           public Boolean call() throws IOException {
1252             PayloadCarryingRpcController controller = rpcControllerFactory.newController();
1253             controller.setPriority(tableName);
1254             try {
1255               CompareType compareType = CompareType.valueOf(compareOp.name());
1256               MultiRequest request = RequestConverter.buildMutateRequest(
1257                   getLocation().getRegionInfo().getRegionName(), row, family, qualifier,
1258                   new BinaryComparator(value), compareType, rm);
1259               ClientProtos.MultiResponse response = getStub().multi(controller, request);
1260               return Boolean.valueOf(response.getProcessed());
1261             } catch (ServiceException se) {
1262               throw ProtobufUtil.getRemoteException(se);
1263             }
1264           }
1265         };
1266     return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout);
1267   }
1268 
1269   /**
1270    * {@inheritDoc}
1271    */
1272   @Override
1273   public boolean exists(final Get get) throws IOException {
1274     get.setCheckExistenceOnly(true);
1275     Result r = get(get);
1276     assert r.getExists() != null;
1277     return r.getExists();
1278   }
1279 
1280   /**
1281    * {@inheritDoc}
1282    */
1283   @Override
1284   public Boolean[] exists(final List<Get> gets) throws IOException {
1285     if (gets.isEmpty()) return new Boolean[]{};
1286     if (gets.size() == 1) return new Boolean[]{exists(gets.get(0))};
1287 
1288     for (Get g: gets){
1289       g.setCheckExistenceOnly(true);
1290     }
1291 
1292     Object[] r1;
1293     try {
1294       r1 = batch(gets);
1295     } catch (InterruptedException e) {
1296       throw (InterruptedIOException)new InterruptedIOException().initCause(e);
1297     }
1298 
1299     // translate.
1300     Boolean[] results = new Boolean[r1.length];
1301     int i = 0;
1302     for (Object o : r1) {
1303       // batch ensures if there is a failure we get an exception instead
1304       results[i++] = ((Result)o).getExists();
1305     }
1306 
1307     return results;
1308   }
1309 
1310   /**
1311    * {@inheritDoc}
1312    */
1313   @Override
1314   public void flushCommits() throws InterruptedIOException, RetriesExhaustedWithDetailsException {
1315     // As we can have an operation in progress even if the buffer is empty, we call
1316     //  backgroundFlushCommits at least one time.
1317     backgroundFlushCommits(true);
1318   }
1319 
1320   /**
1321    * Process a mixed batch of Get, Put and Delete actions. All actions for a
1322    * RegionServer are forwarded in one RPC call. Queries are executed in parallel.
1323    *
1324    * @param list The collection of actions.
1325    * @param results An empty array, same size as list. If an exception is thrown,
1326    * you can test here for partial results, and to determine which actions
1327    * processed successfully.
1328    * @throws IOException if there are problems talking to META. Per-item
1329    * exceptions are stored in the results array.
1330    */
1331   public <R> void processBatchCallback(
1332     final List<? extends Row> list, final Object[] results, final Batch.Callback<R> callback)
1333     throws IOException, InterruptedException {
1334     this.batchCallback(list, results, callback);
1335   }
1336 
1337 
1338   /**
1339    * Parameterized batch processing, allowing varying return types for different
1340    * {@link Row} implementations.
1341    */
1342   public void processBatch(final List<? extends Row> list, final Object[] results)
1343     throws IOException, InterruptedException {
1344 
1345     this.processBatchCallback(list, results, null);
1346   }
1347 
1348 
1349   @Override
1350   public void close() throws IOException {
1351     if (this.closed) {
1352       return;
1353     }
1354     flushCommits();
1355     if (cleanupPoolOnClose) {
1356       this.pool.shutdown();
1357     }
1358     if (cleanupConnectionOnClose) {
1359       if (this.connection != null) {
1360         this.connection.close();
1361       }
1362     }
1363     this.closed = true;
1364   }
1365 
1366   // validate for well-formedness
1367   public void validatePut(final Put put) throws IllegalArgumentException {
1368     validatePut(put, tableConfiguration.getMaxKeyValueSize());
1369   }
1370 
1371   // validate for well-formedness
1372   public static void validatePut(Put put, int maxKeyValueSize) throws IllegalArgumentException {
1373     if (put.isEmpty()) {
1374       throw new IllegalArgumentException("No columns to insert");
1375     }
1376     if (maxKeyValueSize > 0) {
1377       for (List<Cell> list : put.getFamilyCellMap().values()) {
1378         for (Cell cell : list) {
1379           // KeyValue v1 expectation.  Cast for now.
1380           KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
1381           if (kv.getLength() > maxKeyValueSize) {
1382             throw new IllegalArgumentException("KeyValue size too large");
1383           }
1384         }
1385       }
1386     }
1387   }
1388 
1389   /**
1390    * {@inheritDoc}
1391    */
1392   @Override
1393   public boolean isAutoFlush() {
1394     return autoFlush;
1395   }
1396 
1397   /**
1398    * {@inheritDoc}
1399    */
1400   @Deprecated
1401   @Override
1402   public void setAutoFlush(boolean autoFlush) {
1403     setAutoFlush(autoFlush, autoFlush);
1404   }
1405 
1406   /**
1407    * {@inheritDoc}
1408    */
1409   @Override
1410   public void setAutoFlushTo(boolean autoFlush) {
1411     setAutoFlush(autoFlush, clearBufferOnFail);
1412   }
1413 
1414   /**
1415    * {@inheritDoc}
1416    */
1417   @Override
1418   public void setAutoFlush(boolean autoFlush, boolean clearBufferOnFail) {
1419     this.autoFlush = autoFlush;
1420     this.clearBufferOnFail = autoFlush || clearBufferOnFail;
1421   }
1422 
1423   /**
1424    * Returns the maximum size in bytes of the write buffer for this HTable.
1425    * <p>
1426    * The default value comes from the configuration parameter
1427    * {@code hbase.client.write.buffer}.
1428    * @return The size of the write buffer in bytes.
1429    */
1430   @Override
1431   public long getWriteBufferSize() {
1432     return writeBufferSize;
1433   }
1434 
1435   /**
1436    * Sets the size of the buffer in bytes.
1437    * <p>
1438    * If the new size is less than the current amount of data in the
1439    * write buffer, the buffer gets flushed.
1440    * @param writeBufferSize The new write buffer size, in bytes.
1441    * @throws IOException if a remote or network exception occurs.
1442    */
1443   public void setWriteBufferSize(long writeBufferSize) throws IOException {
1444     this.writeBufferSize = writeBufferSize;
1445     if(currentWriteBufferSize > writeBufferSize) {
1446       flushCommits();
1447     }
1448   }
1449 
1450   /**
1451    * The pool is used for mutli requests for this HTable
1452    * @return the pool used for mutli
1453    */
1454   ExecutorService getPool() {
1455     return this.pool;
1456   }
1457 
1458   /**
1459    * Enable or disable region cache prefetch for the table. It will be
1460    * applied for the given table's all HTable instances who share the same
1461    * connection. By default, the cache prefetch is enabled.
1462    * @param tableName name of table to configure.
1463    * @param enable Set to true to enable region cache prefetch. Or set to
1464    * false to disable it.
1465    * @throws IOException
1466    */
1467   public static void setRegionCachePrefetch(final byte[] tableName,
1468       final boolean enable) throws IOException {
1469     setRegionCachePrefetch(TableName.valueOf(tableName), enable);
1470   }
1471 
1472   public static void setRegionCachePrefetch(
1473       final TableName tableName,
1474       final boolean enable) throws IOException {
1475     HConnectionManager.execute(new HConnectable<Void>(HBaseConfiguration.create()) {
1476       @Override
1477       public Void connect(HConnection connection) throws IOException {
1478         connection.setRegionCachePrefetch(tableName, enable);
1479         return null;
1480       }
1481     });
1482   }
1483 
1484   /**
1485    * Enable or disable region cache prefetch for the table. It will be
1486    * applied for the given table's all HTable instances who share the same
1487    * connection. By default, the cache prefetch is enabled.
1488    * @param conf The Configuration object to use.
1489    * @param tableName name of table to configure.
1490    * @param enable Set to true to enable region cache prefetch. Or set to
1491    * false to disable it.
1492    * @throws IOException
1493    */
1494   public static void setRegionCachePrefetch(final Configuration conf,
1495       final byte[] tableName, final boolean enable) throws IOException {
1496     setRegionCachePrefetch(conf, TableName.valueOf(tableName), enable);
1497   }
1498 
1499   public static void setRegionCachePrefetch(final Configuration conf,
1500       final TableName tableName,
1501       final boolean enable) throws IOException {
1502     HConnectionManager.execute(new HConnectable<Void>(conf) {
1503       @Override
1504       public Void connect(HConnection connection) throws IOException {
1505         connection.setRegionCachePrefetch(tableName, enable);
1506         return null;
1507       }
1508     });
1509   }
1510 
1511   /**
1512    * Check whether region cache prefetch is enabled or not for the table.
1513    * @param conf The Configuration object to use.
1514    * @param tableName name of table to check
1515    * @return true if table's region cache prefecth is enabled. Otherwise
1516    * it is disabled.
1517    * @throws IOException
1518    */
1519   public static boolean getRegionCachePrefetch(final Configuration conf,
1520       final byte[] tableName) throws IOException {
1521     return getRegionCachePrefetch(conf, TableName.valueOf(tableName));
1522   }
1523 
1524   public static boolean getRegionCachePrefetch(final Configuration conf,
1525       final TableName tableName) throws IOException {
1526     return HConnectionManager.execute(new HConnectable<Boolean>(conf) {
1527       @Override
1528       public Boolean connect(HConnection connection) throws IOException {
1529         return connection.getRegionCachePrefetch(tableName);
1530       }
1531     });
1532   }
1533 
1534   /**
1535    * Check whether region cache prefetch is enabled or not for the table.
1536    * @param tableName name of table to check
1537    * @return true if table's region cache prefecth is enabled. Otherwise
1538    * it is disabled.
1539    * @throws IOException
1540    */
1541   public static boolean getRegionCachePrefetch(final byte[] tableName) throws IOException {
1542     return getRegionCachePrefetch(TableName.valueOf(tableName));
1543   }
1544 
1545   public static boolean getRegionCachePrefetch(
1546       final TableName tableName) throws IOException {
1547     return HConnectionManager.execute(new HConnectable<Boolean>(
1548         HBaseConfiguration.create()) {
1549       @Override
1550       public Boolean connect(HConnection connection) throws IOException {
1551         return connection.getRegionCachePrefetch(tableName);
1552       }
1553     });
1554   }
1555 
1556   /**
1557    * Explicitly clears the region cache to fetch the latest value from META.
1558    * This is a power user function: avoid unless you know the ramifications.
1559    */
1560   public void clearRegionCache() {
1561     this.connection.clearRegionCache();
1562   }
1563 
1564   /**
1565    * {@inheritDoc}
1566    */
1567   public CoprocessorRpcChannel coprocessorService(byte[] row) {
1568     return new RegionCoprocessorRpcChannel(connection, tableName, row, rpcCallerFactory,
1569         rpcControllerFactory);
1570   }
1571 
1572   /**
1573    * {@inheritDoc}
1574    */
1575   @Override
1576   public <T extends Service, R> Map<byte[],R> coprocessorService(final Class<T> service,
1577       byte[] startKey, byte[] endKey, final Batch.Call<T,R> callable)
1578       throws ServiceException, Throwable {
1579     final Map<byte[],R> results =  Collections.synchronizedMap(
1580         new TreeMap<byte[], R>(Bytes.BYTES_COMPARATOR));
1581     coprocessorService(service, startKey, endKey, callable, new Batch.Callback<R>() {
1582       public void update(byte[] region, byte[] row, R value) {
1583         if (region != null) {
1584           results.put(region, value);
1585         }
1586       }
1587     });
1588     return results;
1589   }
1590 
1591   /**
1592    * {@inheritDoc}
1593    */
1594   @Override
1595   public <T extends Service, R> void coprocessorService(final Class<T> service,
1596       byte[] startKey, byte[] endKey, final Batch.Call<T,R> callable,
1597       final Batch.Callback<R> callback) throws ServiceException, Throwable {
1598 
1599     // get regions covered by the row range
1600     List<byte[]> keys = getStartKeysInRange(startKey, endKey);
1601 
1602     Map<byte[],Future<R>> futures =
1603         new TreeMap<byte[],Future<R>>(Bytes.BYTES_COMPARATOR);
1604     for (final byte[] r : keys) {
1605       final RegionCoprocessorRpcChannel channel =
1606           new RegionCoprocessorRpcChannel(connection, tableName, r, rpcCallerFactory,
1607               rpcControllerFactory);
1608       Future<R> future = pool.submit(
1609           new Callable<R>() {
1610             public R call() throws Exception {
1611               T instance = ProtobufUtil.newServiceStub(service, channel);
1612               R result = callable.call(instance);
1613               byte[] region = channel.getLastRegion();
1614               if (callback != null) {
1615                 callback.update(region, r, result);
1616               }
1617               return result;
1618             }
1619           });
1620       futures.put(r, future);
1621     }
1622     for (Map.Entry<byte[],Future<R>> e : futures.entrySet()) {
1623       try {
1624         e.getValue().get();
1625       } catch (ExecutionException ee) {
1626         LOG.warn("Error calling coprocessor service " + service.getName() + " for row "
1627             + Bytes.toStringBinary(e.getKey()), ee);
1628         throw ee.getCause();
1629       } catch (InterruptedException ie) {
1630         throw new InterruptedIOException("Interrupted calling coprocessor service " + service.getName()
1631             + " for row " + Bytes.toStringBinary(e.getKey()))
1632             .initCause(ie);
1633       }
1634     }
1635   }
1636 
1637   private List<byte[]> getStartKeysInRange(byte[] start, byte[] end)
1638   throws IOException {
1639     if (start == null) {
1640       start = HConstants.EMPTY_START_ROW;
1641     }
1642     if (end == null) {
1643       end = HConstants.EMPTY_END_ROW;
1644     }
1645     return getKeysAndRegionsInRange(start, end, true).getFirst();
1646   }
1647 
1648   public void setOperationTimeout(int operationTimeout) {
1649     this.operationTimeout = operationTimeout;
1650   }
1651 
1652   public int getOperationTimeout() {
1653     return operationTimeout;
1654   }
1655 
1656   @Override
1657   public String toString() {
1658     return tableName + ";" + connection;
1659   }
1660 
1661   /**
1662    * Run basic test.
1663    * @param args Pass table name and row and will get the content.
1664    * @throws IOException
1665    */
1666   public static void main(String[] args) throws IOException {
1667     HTable t = new HTable(HBaseConfiguration.create(), args[0]);
1668     try {
1669       System.out.println(t.get(new Get(Bytes.toBytes(args[1]))));
1670     } finally {
1671       t.close();
1672     }
1673   }
1674 
1675   /**
1676    * {@inheritDoc}
1677    */
1678   @Override
1679   public <R extends Message> Map<byte[], R> batchCoprocessorService(
1680       Descriptors.MethodDescriptor methodDescriptor, Message request,
1681       byte[] startKey, byte[] endKey, R responsePrototype) throws ServiceException, Throwable {
1682     final Map<byte[], R> results = Collections.synchronizedMap(new TreeMap<byte[], R>(
1683         Bytes.BYTES_COMPARATOR));
1684     batchCoprocessorService(methodDescriptor, request, startKey, endKey, responsePrototype,
1685         new Callback<R>() {
1686 
1687           @Override
1688           public void update(byte[] region, byte[] row, R result) {
1689             if (region != null) {
1690               results.put(region, result);
1691             }
1692           }
1693         });
1694     return results;
1695   }
1696 
1697   /**
1698    * {@inheritDoc}
1699    */
1700   @Override
1701   public <R extends Message> void batchCoprocessorService(
1702       final Descriptors.MethodDescriptor methodDescriptor, final Message request,
1703       byte[] startKey, byte[] endKey, final R responsePrototype, final Callback<R> callback)
1704       throws ServiceException, Throwable {
1705 
1706     // get regions covered by the row range
1707     Pair<List<byte[]>, List<HRegionLocation>> keysAndRegions =
1708         getKeysAndRegionsInRange(startKey, endKey, true);
1709     List<byte[]> keys = keysAndRegions.getFirst();
1710     List<HRegionLocation> regions = keysAndRegions.getSecond();
1711 
1712     // check if we have any calls to make
1713     if (keys.isEmpty()) {
1714       LOG.info("No regions were selected by key range start=" + Bytes.toStringBinary(startKey) +
1715           ", end=" + Bytes.toStringBinary(endKey));
1716       return;
1717     }
1718 
1719     List<RegionCoprocessorServiceExec> execs = new ArrayList<RegionCoprocessorServiceExec>();
1720     final Map<byte[], RegionCoprocessorServiceExec> execsByRow =
1721         new TreeMap<byte[], RegionCoprocessorServiceExec>(Bytes.BYTES_COMPARATOR);
1722     for (int i = 0; i < keys.size(); i++) {
1723       final byte[] rowKey = keys.get(i);
1724       final byte[] region = regions.get(i).getRegionInfo().getRegionName();
1725       RegionCoprocessorServiceExec exec =
1726           new RegionCoprocessorServiceExec(region, rowKey, methodDescriptor, request);
1727       execs.add(exec);
1728       execsByRow.put(rowKey, exec);
1729     }
1730 
1731     // tracking for any possible deserialization errors on success callback
1732     // TODO: it would be better to be able to reuse AsyncProcess.BatchErrors here
1733     final List<Throwable> callbackErrorExceptions = new ArrayList<Throwable>();
1734     final List<Row> callbackErrorActions = new ArrayList<Row>();
1735     final List<String> callbackErrorServers = new ArrayList<String>();
1736 
1737     AsyncProcess<ClientProtos.CoprocessorServiceResult> asyncProcess =
1738         new AsyncProcess<ClientProtos.CoprocessorServiceResult>(connection, tableName, pool,
1739             new AsyncProcess.AsyncProcessCallback<ClientProtos.CoprocessorServiceResult>() {
1740           @SuppressWarnings("unchecked")
1741           @Override
1742           public void success(int originalIndex, byte[] region, Row row,
1743               ClientProtos.CoprocessorServiceResult serviceResult) {
1744             if (LOG.isTraceEnabled()) {
1745               LOG.trace("Received result for endpoint " + methodDescriptor.getFullName() +
1746                 " call #" + originalIndex + ": region=" + Bytes.toStringBinary(region) +
1747                 ", row=" + Bytes.toStringBinary(row.getRow()) +
1748                 ", value=" + serviceResult.getValue().getValue());
1749             }
1750             try {
1751               callback.update(region, row.getRow(),
1752                 (R) responsePrototype.newBuilderForType().mergeFrom(
1753                   serviceResult.getValue().getValue()).build());
1754             } catch (InvalidProtocolBufferException e) {
1755               LOG.error("Unexpected response type from endpoint " + methodDescriptor.getFullName(),
1756                 e);
1757               callbackErrorExceptions.add(e);
1758               callbackErrorActions.add(row);
1759               callbackErrorServers.add("null");
1760             }
1761           }
1762 
1763           @Override
1764           public boolean failure(int originalIndex, byte[] region, Row row, Throwable t) {
1765             RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) row;
1766             LOG.error("Failed calling endpoint " + methodDescriptor.getFullName() + ": region="
1767                 + Bytes.toStringBinary(exec.getRegion()), t);
1768             return true;
1769           }
1770 
1771           @Override
1772           public boolean retriableFailure(int originalIndex, Row row, byte[] region,
1773               Throwable exception) {
1774             RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) row;
1775             LOG.error("Failed calling endpoint " + methodDescriptor.getFullName() + ": region="
1776                 + Bytes.toStringBinary(exec.getRegion()), exception);
1777             return !(exception instanceof DoNotRetryIOException);
1778           }
1779         },
1780         configuration, rpcCallerFactory, rpcControllerFactory);
1781 
1782     asyncProcess.submitAll(execs);
1783     asyncProcess.waitUntilDone();
1784 
1785     if (asyncProcess.hasError()) {
1786       throw asyncProcess.getErrors();
1787     } else if (!callbackErrorExceptions.isEmpty()) {
1788       throw new RetriesExhaustedWithDetailsException(callbackErrorExceptions, callbackErrorActions,
1789         callbackErrorServers);
1790     }
1791   }
1792 }