View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.Closeable;
23  import java.io.DataInput;
24  import java.io.DataOutput;
25  import java.io.IOException;
26  import java.lang.reflect.Proxy;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Collections;
32  import java.util.NavigableMap;
33  import java.util.TreeMap;
34  import java.util.concurrent.ExecutorService;
35  import java.util.concurrent.SynchronousQueue;
36  import java.util.concurrent.ThreadFactory;
37  import java.util.concurrent.ThreadPoolExecutor;
38  import java.util.concurrent.TimeUnit;
39  import java.util.concurrent.atomic.AtomicInteger;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  import org.apache.hadoop.conf.Configuration;
44  import org.apache.hadoop.hbase.HBaseConfiguration;
45  import org.apache.hadoop.hbase.HConstants;
46  import org.apache.hadoop.hbase.HRegionInfo;
47  import org.apache.hadoop.hbase.HRegionLocation;
48  import org.apache.hadoop.hbase.HServerAddress;
49  import org.apache.hadoop.hbase.HTableDescriptor;
50  import org.apache.hadoop.hbase.KeyValue;
51  import org.apache.hadoop.hbase.ServerName;
52  import org.apache.hadoop.hbase.client.HConnectionManager.HConnectable;
53  import org.apache.hadoop.hbase.client.coprocessor.Batch;
54  import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;
55  import org.apache.hadoop.hbase.ipc.ExecRPCInvoker;
56  import org.apache.hadoop.hbase.util.Addressing;
57  import org.apache.hadoop.hbase.util.Bytes;
58  import org.apache.hadoop.hbase.util.Pair;
59  import org.apache.hadoop.hbase.util.Threads;
60  
61  /**
62   * <p>Used to communicate with a single HBase table.
63   *
64   * <p>This class is not thread safe for reads nor write.
65   * 
66   * <p>In case of writes (Put, Delete), the underlying write buffer can
67   * be corrupted if multiple threads contend over a single HTable instance.
68   * 
69   * <p>In case of reads, some fields used by a Scan are shared among all threads.
70   * The HTable implementation can either not contract to be safe in case of a Get
71   *
72   * <p>To access a table in a multi threaded environment, please consider
73   * using the {@link HTablePool} class to create your HTable instances.
74   *
75   * <p>Instances of HTable passed the same {@link Configuration} instance will
76   * share connections to servers out on the cluster and to the zookeeper ensemble
77   * as well as caches of region locations.  This is usually a *good* thing and it
78   * is recommended to reuse the same configuration object for all your tables.
79   * This happens because they will all share the same underlying
80   * {@link HConnection} instance. See {@link HConnectionManager} for more on
81   * how this mechanism works.
82   *
83   * <p>{@link HConnection} will read most of the
84   * configuration it needs from the passed {@link Configuration} on initial
85   * construction.  Thereafter, for settings such as
86   * <code>hbase.client.pause</code>, <code>hbase.client.retries.number</code>,
87   * and <code>hbase.client.rpc.maxattempts</code> updating their values in the
88   * passed {@link Configuration} subsequent to {@link HConnection} construction
89   * will go unnoticed.  To run with changed values, make a new
90   * {@link HTable} passing a new {@link Configuration} instance that has the
91   * new configuration.
92   *
93   * <p>Note that this class implements the {@link Closeable} interface. When a
94   * HTable instance is no longer required, it *should* be closed in order to ensure
95   * that the underlying resources are promptly released. Please note that the close 
96   * method can throw java.io.IOException that must be handled.
97   *
98   * @see HBaseAdmin for create, drop, list, enable and disable of tables.
99   * @see HConnection
100  * @see HConnectionManager
101  */
102 public class HTable implements HTableInterface {
103   private static final Log LOG = LogFactory.getLog(HTable.class);
104   private HConnection connection;
105   private final byte [] tableName;
106   private volatile Configuration configuration;
107   private final ArrayList<Put> writeBuffer = new ArrayList<Put>();
108   private long writeBufferSize;
109   private boolean clearBufferOnFail;
110   private boolean autoFlush;
111   private long currentWriteBufferSize;
112   protected int scannerCaching;
113   private int maxKeyValueSize;
114   private ExecutorService pool;  // For Multi
115   private boolean closed;
116   private int operationTimeout;
117   private final boolean cleanupPoolOnClose; // shutdown the pool in close()
118   private final boolean cleanupConnectionOnClose; // close the connection in close()
119 
120   /**
121    * Creates an object to access a HBase table.
122    * Shares zookeeper connection and other resources with other HTable instances
123    * created with the same <code>conf</code> instance.  Uses already-populated
124    * region cache if one is available, populated by any other HTable instances
125    * sharing this <code>conf</code> instance.  Recommended.
126    * @param conf Configuration object to use.
127    * @param tableName Name of the table.
128    * @throws IOException if a remote or network exception occurs
129    */
130   public HTable(Configuration conf, final String tableName)
131   throws IOException {
132     this(conf, Bytes.toBytes(tableName));
133   }
134 
135 
136   /**
137    * Creates an object to access a HBase table.
138    * Shares zookeeper connection and other resources with other HTable instances
139    * created with the same <code>conf</code> instance.  Uses already-populated
140    * region cache if one is available, populated by any other HTable instances
141    * sharing this <code>conf</code> instance.  Recommended.
142    * @param conf Configuration object to use.
143    * @param tableName Name of the table.
144    * @throws IOException if a remote or network exception occurs
145    */
146   public HTable(Configuration conf, final byte [] tableName)
147   throws IOException {
148     this.tableName = tableName;
149     this.cleanupPoolOnClose = this.cleanupConnectionOnClose = true;
150     if (conf == null) {
151       this.connection = null;
152       return;
153     }
154     this.connection = HConnectionManager.getConnection(conf);
155     this.configuration = conf;
156 
157     int maxThreads = conf.getInt("hbase.htable.threads.max", Integer.MAX_VALUE);
158     if (maxThreads == 0) {
159       maxThreads = 1; // is there a better default?
160     }
161     long keepAliveTime = conf.getLong("hbase.htable.threads.keepalivetime", 60);
162 
163     // Using the "direct handoff" approach, new threads will only be created
164     // if it is necessary and will grow unbounded. This could be bad but in HCM
165     // we only create as many Runnables as there are region servers. It means
166     // it also scales when new region servers are added.
167     this.pool = new ThreadPoolExecutor(1, maxThreads,
168         keepAliveTime, TimeUnit.SECONDS,
169         new SynchronousQueue<Runnable>(),
170         Threads.newDaemonThreadFactory("hbase-table"));
171     ((ThreadPoolExecutor)this.pool).allowCoreThreadTimeOut(true);
172 
173     this.finishSetup();
174   }
175 
176   /**
177    * Creates an object to access a HBase table.
178    * Shares zookeeper connection and other resources with other HTable instances
179    * created with the same <code>conf</code> instance.  Uses already-populated
180    * region cache if one is available, populated by any other HTable instances
181    * sharing this <code>conf</code> instance.
182    * Use this constructor when the ExecutorService is externally managed.
183    * @param conf Configuration object to use.
184    * @param tableName Name of the table.
185    * @param pool ExecutorService to be used.
186    * @throws IOException if a remote or network exception occurs
187    */
188   public HTable(Configuration conf, final byte[] tableName, final ExecutorService pool)
189       throws IOException {
190     this.connection = HConnectionManager.getConnection(conf);
191     this.configuration = conf;
192     this.pool = pool;
193     this.tableName = tableName;
194     this.cleanupPoolOnClose = false;
195     this.cleanupConnectionOnClose = true;
196 
197     this.finishSetup();
198   }
199 
200   /**
201    * Creates an object to access a HBase table.
202    * Shares zookeeper connection and other resources with other HTable instances
203    * created with the same <code>connection</code> instance.
204    * Use this constructor when the ExecutorService and HConnection instance are
205    * externally managed.
206    * @param tableName Name of the table.
207    * @param connection HConnection to be used.
208    * @param pool ExecutorService to be used.
209    * @throws IOException if a remote or network exception occurs
210    */
211   public HTable(final byte[] tableName, final HConnection connection, 
212       final ExecutorService pool) throws IOException {
213     if (pool == null || pool.isShutdown()) {
214       throw new IllegalArgumentException("Pool is null or shut down.");
215     }
216     if (connection == null || connection.isClosed()) {
217       throw new IllegalArgumentException("Connection is null or closed.");
218     }
219     this.tableName = tableName;
220     this.cleanupPoolOnClose = this.cleanupConnectionOnClose = false;
221     this.connection = connection;
222     this.configuration = connection.getConfiguration();
223     this.pool = pool;
224 
225     this.finishSetup();
226   }
227 
228   /**
229    * setup this HTable's parameter based on the passed configuration
230    * @param conf
231    */
232   private void finishSetup() throws IOException {
233     this.connection.locateRegion(tableName, HConstants.EMPTY_START_ROW);
234     this.operationTimeout = HTableDescriptor.isMetaTable(tableName) ? HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT
235         : this.configuration.getInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
236             HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
237     this.writeBufferSize = this.configuration.getLong(
238         "hbase.client.write.buffer", 2097152);
239     this.clearBufferOnFail = true;
240     this.autoFlush = true;
241     this.currentWriteBufferSize = 0;
242     this.scannerCaching = this.configuration.getInt(
243         "hbase.client.scanner.caching", 1);
244 
245     this.maxKeyValueSize = this.configuration.getInt(
246         "hbase.client.keyvalue.maxsize", -1);
247     this.closed = false;
248   }
249 
250   /**
251    * {@inheritDoc}
252    */
253   @Override
254   public Configuration getConfiguration() {
255     return configuration;
256   }
257 
258   /**
259    * Tells whether or not a table is enabled or not. This method creates a
260    * new HBase configuration, so it might make your unit tests fail due to
261    * incorrect ZK client port.
262    * @param tableName Name of table to check.
263    * @return {@code true} if table is online.
264    * @throws IOException if a remote or network exception occurs
265 	* @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
266    */
267   @Deprecated
268   public static boolean isTableEnabled(String tableName) throws IOException {
269     return isTableEnabled(Bytes.toBytes(tableName));
270   }
271 
272   /**
273    * Tells whether or not a table is enabled or not. This method creates a
274    * new HBase configuration, so it might make your unit tests fail due to
275    * incorrect ZK client port.
276    * @param tableName Name of table to check.
277    * @return {@code true} if table is online.
278    * @throws IOException if a remote or network exception occurs
279    * @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
280    */
281   @Deprecated
282   public static boolean isTableEnabled(byte[] tableName) throws IOException {
283     return isTableEnabled(HBaseConfiguration.create(), tableName);
284   }
285 
286   /**
287    * Tells whether or not a table is enabled or not.
288    * @param conf The Configuration object to use.
289    * @param tableName Name of table to check.
290    * @return {@code true} if table is online.
291    * @throws IOException if a remote or network exception occurs
292 	* @deprecated use {@link HBaseAdmin#isTableEnabled(byte[])}
293    */
294   @Deprecated
295   public static boolean isTableEnabled(Configuration conf, String tableName)
296   throws IOException {
297     return isTableEnabled(conf, Bytes.toBytes(tableName));
298   }
299 
300   /**
301    * Tells whether or not a table is enabled or not.
302    * @param conf The Configuration object to use.
303    * @param tableName Name of table to check.
304    * @return {@code true} if table is online.
305    * @throws IOException if a remote or network exception occurs
306    */
307   public static boolean isTableEnabled(Configuration conf,
308       final byte[] tableName) throws IOException {
309     return HConnectionManager.execute(new HConnectable<Boolean>(conf) {
310       @Override
311       public Boolean connect(HConnection connection) throws IOException {
312         return connection.isTableEnabled(tableName);
313       }
314     });
315   }
316 
317   /**
318    * Find region location hosting passed row using cached info
319    * @param row Row to find.
320    * @return The location of the given row.
321    * @throws IOException if a remote or network exception occurs
322    */
323   public HRegionLocation getRegionLocation(final String row)
324   throws IOException {
325     return connection.getRegionLocation(tableName, Bytes.toBytes(row), false);
326   }
327 
328   /**
329    * Finds the region on which the given row is being served.
330    * @param row Row to find.
331    * @return Location of the row.
332    * @throws IOException if a remote or network exception occurs
333    * @deprecated use {@link #getRegionLocation(byte [], boolean)} instead
334    */
335   public HRegionLocation getRegionLocation(final byte [] row)
336   throws IOException {
337     return connection.getRegionLocation(tableName, row, false);
338   }
339 
340   /**
341    * Finds the region on which the given row is being served.
342    * @param row Row to find.
343    * @param reload whether or not to reload information or just use cached
344    * information
345    * @return Location of the row.
346    * @throws IOException if a remote or network exception occurs
347    */
348   public HRegionLocation getRegionLocation(final byte [] row, boolean reload)
349   throws IOException {
350     return connection.getRegionLocation(tableName, row, reload);
351   }
352      
353   /**
354    * {@inheritDoc}
355    */
356   @Override
357   public byte [] getTableName() {
358     return this.tableName;
359   }
360 
361   /**
362    * <em>INTERNAL</em> Used by unit tests and tools to do low-level
363    * manipulations.
364    * @return An HConnection instance.
365    * @deprecated This method will be changed from public to package protected.
366    */
367   // TODO(tsuna): Remove this.  Unit tests shouldn't require public helpers.
368   public HConnection getConnection() {
369     return this.connection;
370   }
371 
372   /**
373    * Gets the number of rows that a scanner will fetch at once.
374    * <p>
375    * The default value comes from {@code hbase.client.scanner.caching}.
376    * @deprecated Use {@link Scan#setCaching(int)} and {@link Scan#getCaching()}
377    */
378   public int getScannerCaching() {
379     return scannerCaching;
380   }
381 
382   /**
383    * Sets the number of rows that a scanner will fetch at once.
384    * <p>
385    * This will override the value specified by
386    * {@code hbase.client.scanner.caching}.
387    * Increasing this value will reduce the amount of work needed each time
388    * {@code next()} is called on a scanner, at the expense of memory use
389    * (since more rows will need to be maintained in memory by the scanners).
390    * @param scannerCaching the number of rows a scanner will fetch at once.
391    * @deprecated Use {@link Scan#setCaching(int)}
392    */
393   public void setScannerCaching(int scannerCaching) {
394     this.scannerCaching = scannerCaching;
395   }
396 
397   /**
398    * {@inheritDoc}
399    */
400   @Override
401   public HTableDescriptor getTableDescriptor() throws IOException {
402     return new UnmodifyableHTableDescriptor(
403       this.connection.getHTableDescriptor(this.tableName));
404   }
405 
406   /**
407    * Gets the starting row key for every region in the currently open table.
408    * <p>
409    * This is mainly useful for the MapReduce integration.
410    * @return Array of region starting row keys
411    * @throws IOException if a remote or network exception occurs
412    */
413   public byte [][] getStartKeys() throws IOException {
414     return getStartEndKeys().getFirst();
415   }
416 
417   /**
418    * Gets the ending row key for every region in the currently open table.
419    * <p>
420    * This is mainly useful for the MapReduce integration.
421    * @return Array of region ending row keys
422    * @throws IOException if a remote or network exception occurs
423    */
424   public byte[][] getEndKeys() throws IOException {
425     return getStartEndKeys().getSecond();
426   }
427 
428   /**
429    * Gets the starting and ending row keys for every region in the currently
430    * open table.
431    * <p>
432    * This is mainly useful for the MapReduce integration.
433    * @return Pair of arrays of region starting and ending row keys
434    * @throws IOException if a remote or network exception occurs
435    */
436   public Pair<byte[][],byte[][]> getStartEndKeys() throws IOException {
437     NavigableMap<HRegionInfo, ServerName> regions = getRegionLocations();
438     final List<byte[]> startKeyList = new ArrayList<byte[]>(regions.size());
439     final List<byte[]> endKeyList = new ArrayList<byte[]>(regions.size());
440 
441     for (HRegionInfo region : regions.keySet()) {
442       startKeyList.add(region.getStartKey());
443       endKeyList.add(region.getEndKey());
444     }
445 
446     return new Pair<byte [][], byte [][]>(
447       startKeyList.toArray(new byte[startKeyList.size()][]),
448       endKeyList.toArray(new byte[endKeyList.size()][]));
449   }
450 
451   /**
452    * Gets all the regions and their address for this table.
453    * @return A map of HRegionInfo with it's server address
454    * @throws IOException if a remote or network exception occurs
455    * @deprecated Use {@link #getRegionLocations()} or {@link #getStartEndKeys()}
456    */
457   public Map<HRegionInfo, HServerAddress> getRegionsInfo() throws IOException {
458     final Map<HRegionInfo, HServerAddress> regionMap =
459       new TreeMap<HRegionInfo, HServerAddress>();
460 
461     final Map<HRegionInfo, ServerName> regionLocations = getRegionLocations();
462 
463     for (Map.Entry<HRegionInfo, ServerName> entry : regionLocations.entrySet()) {
464       HServerAddress server = new HServerAddress();
465       ServerName serverName = entry.getValue();
466       if (serverName != null && serverName.getHostAndPort() != null) {
467         server = new HServerAddress(Addressing.createInetSocketAddressFromHostAndPortStr(
468             serverName.getHostAndPort()));
469       }
470       regionMap.put(entry.getKey(), server);
471     }
472 
473     return regionMap;
474   }
475 
476   /**
477    * Gets all the regions and their address for this table.
478    * <p>
479    * This is mainly useful for the MapReduce integration.
480    * @return A map of HRegionInfo with it's server address
481    * @throws IOException if a remote or network exception occurs
482    */
483   public NavigableMap<HRegionInfo, ServerName> getRegionLocations() throws IOException {
484     return MetaScanner.allTableRegions(getConfiguration(), getTableName(), false);
485   }
486 
487   /**
488    * Get the corresponding regions for an arbitrary range of keys.
489    * <p>
490    * @param startRow Starting row in range, inclusive
491    * @param endRow Ending row in range, exclusive
492    * @return A list of HRegionLocations corresponding to the regions that
493    * contain the specified range
494    * @throws IOException if a remote or network exception occurs
495    */
496   public List<HRegionLocation> getRegionsInRange(final byte [] startKey,
497     final byte [] endKey) throws IOException {
498     final boolean endKeyIsEndOfTable = Bytes.equals(endKey,
499                                                     HConstants.EMPTY_END_ROW);
500     if ((Bytes.compareTo(startKey, endKey) > 0) && !endKeyIsEndOfTable) {
501       throw new IllegalArgumentException(
502         "Invalid range: " + Bytes.toStringBinary(startKey) +
503         " > " + Bytes.toStringBinary(endKey));
504     }
505     final List<HRegionLocation> regionList = new ArrayList<HRegionLocation>();
506     byte [] currentKey = startKey;
507     do {
508       HRegionLocation regionLocation = getRegionLocation(currentKey, false);
509       regionList.add(regionLocation);
510       currentKey = regionLocation.getRegionInfo().getEndKey();
511     } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW) &&
512              (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0));
513     return regionList;
514   }
515 
516   /**
517    * Save the passed region information and the table's regions
518    * cache.
519    * <p>
520    * This is mainly useful for the MapReduce integration. You can call
521    * {@link #deserializeRegionInfo deserializeRegionInfo}
522    * to deserialize regions information from a
523    * {@link DataInput}, then call this method to load them to cache.
524    *
525    * <pre>
526    * {@code
527    * HTable t1 = new HTable("foo");
528    * FileInputStream fis = new FileInputStream("regions.dat");
529    * DataInputStream dis = new DataInputStream(fis);
530    *
531    * Map<HRegionInfo, HServerAddress> hm = t1.deserializeRegionInfo(dis);
532    * t1.prewarmRegionCache(hm);
533    * }
534    * </pre>
535    * @param regionMap This piece of regions information will be loaded
536    * to region cache.
537    */
538   public void prewarmRegionCache(Map<HRegionInfo, HServerAddress> regionMap) {
539     this.connection.prewarmRegionCache(this.getTableName(), regionMap);
540   }
541 
542   /**
543    * Serialize the regions information of this table and output
544    * to <code>out</code>.
545    * <p>
546    * This is mainly useful for the MapReduce integration. A client could
547    * perform a large scan for all the regions for the table, serialize the
548    * region info to a file. MR job can ship a copy of the meta for the table in
549    * the DistributedCache.
550    * <pre>
551    * {@code
552    * FileOutputStream fos = new FileOutputStream("regions.dat");
553    * DataOutputStream dos = new DataOutputStream(fos);
554    * table.serializeRegionInfo(dos);
555    * dos.flush();
556    * dos.close();
557    * }
558    * </pre>
559    * @param out {@link DataOutput} to serialize this object into.
560    * @throws IOException if a remote or network exception occurs
561    */
562   public void serializeRegionInfo(DataOutput out) throws IOException {
563     Map<HRegionInfo, HServerAddress> allRegions = this.getRegionsInfo();
564     // first, write number of regions
565     out.writeInt(allRegions.size());
566     for (Map.Entry<HRegionInfo, HServerAddress> es : allRegions.entrySet()) {
567       es.getKey().write(out);
568       es.getValue().write(out);
569     }
570   }
571 
572   /**
573    * Read from <code>in</code> and deserialize the regions information.
574    *
575    * <p>It behaves similarly as {@link #getRegionsInfo getRegionsInfo}, except
576    * that it loads the region map from a {@link DataInput} object.
577    *
578    * <p>It is supposed to be followed immediately by  {@link
579    * #prewarmRegionCache prewarmRegionCache}.
580    *
581    * <p>
582    * Please refer to {@link #prewarmRegionCache prewarmRegionCache} for usage.
583    *
584    * @param in {@link DataInput} object.
585    * @return A map of HRegionInfo with its server address.
586    * @throws IOException if an I/O exception occurs.
587    */
588   public Map<HRegionInfo, HServerAddress> deserializeRegionInfo(DataInput in)
589   throws IOException {
590     final Map<HRegionInfo, HServerAddress> allRegions =
591       new TreeMap<HRegionInfo, HServerAddress>();
592 
593     // the first integer is expected to be the size of records
594     int regionsCount = in.readInt();
595     for (int i = 0; i < regionsCount; ++i) {
596       HRegionInfo hri = new HRegionInfo();
597       hri.readFields(in);
598       HServerAddress hsa = new HServerAddress();
599       hsa.readFields(in);
600       allRegions.put(hri, hsa);
601     }
602     return allRegions;
603   }
604 
605   /**
606    * {@inheritDoc}
607    */
608    @Override
609    public Result getRowOrBefore(final byte[] row, final byte[] family)
610    throws IOException {
611      return new ServerCallable<Result>(connection, tableName, row, operationTimeout) {
612        public Result call() throws IOException {
613          return server.getClosestRowBefore(location.getRegionInfo().getRegionName(),
614            row, family);
615        }
616      }.withRetries();
617    }
618 
619    /**
620     * {@inheritDoc}
621     */
622   @Override
623   public ResultScanner getScanner(final Scan scan) throws IOException {
624     if (scan.getCaching() <= 0) {
625       scan.setCaching(getScannerCaching());
626     }
627     return new ClientScanner(getConfiguration(), scan, getTableName(),
628         this.connection);
629   }
630 
631   /**
632    * {@inheritDoc}
633    */
634   @Override
635   public ResultScanner getScanner(byte [] family) throws IOException {
636     Scan scan = new Scan();
637     scan.addFamily(family);
638     return getScanner(scan);
639   }
640 
641   /**
642    * {@inheritDoc}
643    */
644   @Override
645   public ResultScanner getScanner(byte [] family, byte [] qualifier)
646   throws IOException {
647     Scan scan = new Scan();
648     scan.addColumn(family, qualifier);
649     return getScanner(scan);
650   }
651 
652   /**
653    * {@inheritDoc}
654    */
655   @Override
656   public Result get(final Get get) throws IOException {
657     return new ServerCallable<Result>(connection, tableName, get.getRow(), operationTimeout) {
658           public Result call() throws IOException {
659             return server.get(location.getRegionInfo().getRegionName(), get);
660           }
661         }.withRetries();
662   }
663 
664   /**
665    * {@inheritDoc}
666    */
667   @Override
668   public Result[] get(List<Get> gets) throws IOException {
669     try {
670       Object [] r1 = batch((List)gets);
671 
672       // translate.
673       Result [] results = new Result[r1.length];
674       int i=0;
675       for (Object o : r1) {
676         // batch ensures if there is a failure we get an exception instead
677         results[i++] = (Result) o;
678       }
679 
680       return results;
681     } catch (InterruptedException e) {
682       throw new IOException(e);
683     }
684   }
685 
686   /**
687    * {@inheritDoc}
688    */
689   @Override
690   public void batch(final List<?extends Row> actions, final Object[] results)
691       throws InterruptedException, IOException {
692     connection.processBatch(actions, tableName, pool, results);
693   }
694 
695   /**
696    * {@inheritDoc}
697    */
698   @Override
699   public Object[] batch(final List<? extends Row> actions) throws InterruptedException, IOException {
700     Object[] results = new Object[actions.size()];
701     connection.processBatch(actions, tableName, pool, results);
702     return results;
703   }
704 
705   /**
706    * {@inheritDoc}
707    */
708   @Override
709   public void delete(final Delete delete)
710   throws IOException {
711     new ServerCallable<Boolean>(connection, tableName, delete.getRow(), operationTimeout) {
712           public Boolean call() throws IOException {
713             server.delete(location.getRegionInfo().getRegionName(), delete);
714             return null; // FindBugs NP_BOOLEAN_RETURN_NULL
715           }
716         }.withRetries();
717   }
718 
719   /**
720    * {@inheritDoc}
721    */
722   @Override
723   public void delete(final List<Delete> deletes)
724   throws IOException {
725     Object[] results = new Object[deletes.size()];
726     try {
727       connection.processBatch((List) deletes, tableName, pool, results);
728     } catch (InterruptedException e) {
729       throw new IOException(e);
730     } finally {
731       // mutate list so that it is empty for complete success, or contains only failed records
732       // results are returned in the same order as the requests in list
733       // walk the list backwards, so we can remove from list without impacting the indexes of earlier members
734       for (int i = results.length - 1; i>=0; i--) {
735         // if result is not null, it succeeded
736         if (results[i] instanceof Result) {
737           deletes.remove(i);
738         }
739       }
740     }
741   }
742 
743   /**
744    * {@inheritDoc}
745    */
746   @Override
747   public void put(final Put put) throws IOException {
748     doPut(put);
749     if (autoFlush) {
750       flushCommits();
751     }
752   }
753 
754   /**
755    * {@inheritDoc}
756    */
757   @Override
758   public void put(final List<Put> puts) throws IOException {
759     for (Put put : puts) {
760       doPut(put);
761     }
762     if (autoFlush) {
763       flushCommits();
764     }
765   }
766 
767   private void doPut(Put put) throws IOException{
768     validatePut(put);
769     writeBuffer.add(put);
770     currentWriteBufferSize += put.heapSize();
771     if (currentWriteBufferSize > writeBufferSize) {
772       flushCommits();
773     }
774   }
775 
776   /**
777    * {@inheritDoc}
778    */
779   @Override
780   public void mutateRow(final RowMutations rm) throws IOException {
781     new ServerCallable<Void>(connection, tableName, rm.getRow(),
782         operationTimeout) {
783       public Void call() throws IOException {
784         server.mutateRow(location.getRegionInfo().getRegionName(), rm);
785         return null;
786       }
787     }.withRetries();
788   }
789 
790   /**
791    * {@inheritDoc}
792    */
793   @Override
794   public Result append(final Append append) throws IOException {
795     if (append.numFamilies() == 0) {
796       throw new IOException(
797           "Invalid arguments to append, no columns specified");
798     }
799     return new ServerCallable<Result>(connection, tableName, append.getRow(), operationTimeout) {
800           public Result call() throws IOException {
801             return server.append(
802                 location.getRegionInfo().getRegionName(), append);
803           }
804         }.withRetries();
805   }
806 
807   /**
808    * {@inheritDoc}
809    */
810   @Override
811   public Result increment(final Increment increment) throws IOException {
812     if (!increment.hasFamilies()) {
813       throw new IOException(
814           "Invalid arguments to increment, no columns specified");
815     }
816     return new ServerCallable<Result>(connection, tableName, increment.getRow(), operationTimeout) {
817           public Result call() throws IOException {
818             return server.increment(
819                 location.getRegionInfo().getRegionName(), increment);
820           }
821         }.withRetries();
822   }
823 
824   /**
825    * {@inheritDoc}
826    */
827   @Override
828   public long incrementColumnValue(final byte [] row, final byte [] family,
829       final byte [] qualifier, final long amount)
830   throws IOException {
831     return incrementColumnValue(row, family, qualifier, amount, true);
832   }
833 
834   /**
835    * {@inheritDoc}
836    */
837   @Override
838   public long incrementColumnValue(final byte [] row, final byte [] family,
839       final byte [] qualifier, final long amount, final boolean writeToWAL)
840   throws IOException {
841     NullPointerException npe = null;
842     if (row == null) {
843       npe = new NullPointerException("row is null");
844     } else if (family == null) {
845       npe = new NullPointerException("column is null");
846     }
847     if (npe != null) {
848       throw new IOException(
849           "Invalid arguments to incrementColumnValue", npe);
850     }
851     return new ServerCallable<Long>(connection, tableName, row, operationTimeout) {
852           public Long call() throws IOException {
853             return server.incrementColumnValue(
854                 location.getRegionInfo().getRegionName(), row, family,
855                 qualifier, amount, writeToWAL);
856           }
857         }.withRetries();
858   }
859 
860   /**
861    * {@inheritDoc}
862    */
863   @Override
864   public boolean checkAndPut(final byte [] row,
865       final byte [] family, final byte [] qualifier, final byte [] value,
866       final Put put)
867   throws IOException {
868     return new ServerCallable<Boolean>(connection, tableName, row, operationTimeout) {
869           public Boolean call() throws IOException {
870             return server.checkAndPut(location.getRegionInfo().getRegionName(),
871                 row, family, qualifier, value, put) ? Boolean.TRUE : Boolean.FALSE;
872           }
873         }.withRetries();
874   }
875 
876 
877   /**
878    * {@inheritDoc}
879    */
880   @Override
881   public boolean checkAndDelete(final byte [] row,
882       final byte [] family, final byte [] qualifier, final byte [] value,
883       final Delete delete)
884   throws IOException {
885     return new ServerCallable<Boolean>(connection, tableName, row, operationTimeout) {
886           public Boolean call() throws IOException {
887             return server.checkAndDelete(
888                 location.getRegionInfo().getRegionName(),
889                 row, family, qualifier, value, delete)
890             ? Boolean.TRUE : Boolean.FALSE;
891           }
892         }.withRetries();
893   }
894 
895   /**
896    * {@inheritDoc}
897    */
898   @Override
899   public boolean exists(final Get get) throws IOException {
900     return new ServerCallable<Boolean>(connection, tableName, get.getRow(), operationTimeout) {
901           public Boolean call() throws IOException {
902             return server.
903                 exists(location.getRegionInfo().getRegionName(), get);
904           }
905         }.withRetries();
906   }
907 
908   /**
909    * {@inheritDoc}
910    */
911   @Override
912   public void flushCommits() throws IOException {
913     try {
914       Object[] results = new Object[writeBuffer.size()];
915       try {
916         this.connection.processBatch(writeBuffer, tableName, pool, results);
917       } catch (InterruptedException e) {
918         throw new IOException(e);
919       } finally {
920         // mutate list so that it is empty for complete success, or contains
921         // only failed records results are returned in the same order as the
922         // requests in list walk the list backwards, so we can remove from list
923         // without impacting the indexes of earlier members
924         for (int i = results.length - 1; i>=0; i--) {
925           if (results[i] instanceof Result) {
926             // successful Puts are removed from the list here.
927             writeBuffer.remove(i);
928           }
929         }
930       }
931     } finally {
932       if (clearBufferOnFail) {
933         writeBuffer.clear();
934         currentWriteBufferSize = 0;
935       } else {
936         // the write buffer was adjusted by processBatchOfPuts
937         currentWriteBufferSize = 0;
938         for (Put aPut : writeBuffer) {
939           currentWriteBufferSize += aPut.heapSize();
940         }
941       }
942     }
943   }
944 
945   /**
946    * {@inheritDoc}
947    */
948   @Override
949   public void close() throws IOException {
950     if (this.closed) {
951       return;
952     }
953     flushCommits();
954     if (cleanupPoolOnClose) {
955       this.pool.shutdown();
956     }
957     if (cleanupConnectionOnClose) {
958       if (this.connection != null) {
959         this.connection.close();
960       }
961     }
962     this.closed = true;
963   }
964 
965   // validate for well-formedness
966   private void validatePut(final Put put) throws IllegalArgumentException{
967     if (put.isEmpty()) {
968       throw new IllegalArgumentException("No columns to insert");
969     }
970     if (maxKeyValueSize > 0) {
971       for (List<KeyValue> list : put.getFamilyMap().values()) {
972         for (KeyValue kv : list) {
973           if (kv.getLength() > maxKeyValueSize) {
974             throw new IllegalArgumentException("KeyValue size too large");
975           }
976         }
977       }
978     }
979   }
980 
981   /**
982    * {@inheritDoc}
983    */
984   @Override
985   public RowLock lockRow(final byte [] row)
986   throws IOException {
987     return new ServerCallable<RowLock>(connection, tableName, row, operationTimeout) {
988         public RowLock call() throws IOException {
989           long lockId =
990               server.lockRow(location.getRegionInfo().getRegionName(), row);
991           return new RowLock(row,lockId);
992         }
993       }.withRetries();
994   }
995 
996   /**
997    * {@inheritDoc}
998    */
999   @Override
1000   public void unlockRow(final RowLock rl)
1001   throws IOException {
1002     new ServerCallable<Boolean>(connection, tableName, rl.getRow(), operationTimeout) {
1003         public Boolean call() throws IOException {
1004           server.unlockRow(location.getRegionInfo().getRegionName(),
1005               rl.getLockId());
1006           return null; // FindBugs NP_BOOLEAN_RETURN_NULL
1007         }
1008       }.withRetries();
1009   }
1010 
1011   /**
1012    * {@inheritDoc}
1013    */
1014   @Override
1015   public boolean isAutoFlush() {
1016     return autoFlush;
1017   }
1018 
1019   /**
1020    * See {@link #setAutoFlush(boolean, boolean)}
1021    *
1022    * @param autoFlush
1023    *          Whether or not to enable 'auto-flush'.
1024    */
1025   public void setAutoFlush(boolean autoFlush) {
1026     setAutoFlush(autoFlush, autoFlush);
1027   }
1028 
1029   /**
1030    * Turns 'auto-flush' on or off.
1031    * <p>
1032    * When enabled (default), {@link Put} operations don't get buffered/delayed
1033    * and are immediately executed. Failed operations are not retried. This is
1034    * slower but safer.
1035    * <p>
1036    * Turning off {@link #autoFlush} means that multiple {@link Put}s will be
1037    * accepted before any RPC is actually sent to do the write operations. If the
1038    * application dies before pending writes get flushed to HBase, data will be
1039    * lost.
1040    * <p>
1041    * When you turn {@link #autoFlush} off, you should also consider the
1042    * {@link #clearBufferOnFail} option. By default, asynchronous {@link Put}
1043    * requests will be retried on failure until successful. However, this can
1044    * pollute the writeBuffer and slow down batching performance. Additionally,
1045    * you may want to issue a number of Put requests and call
1046    * {@link #flushCommits()} as a barrier. In both use cases, consider setting
1047    * clearBufferOnFail to true to erase the buffer after {@link #flushCommits()}
1048    * has been called, regardless of success.
1049    *
1050    * @param autoFlush
1051    *          Whether or not to enable 'auto-flush'.
1052    * @param clearBufferOnFail
1053    *          Whether to keep Put failures in the writeBuffer
1054    * @see #flushCommits
1055    */
1056   public void setAutoFlush(boolean autoFlush, boolean clearBufferOnFail) {
1057     this.autoFlush = autoFlush;
1058     this.clearBufferOnFail = autoFlush || clearBufferOnFail;
1059   }
1060 
1061   /**
1062    * Returns the maximum size in bytes of the write buffer for this HTable.
1063    * <p>
1064    * The default value comes from the configuration parameter
1065    * {@code hbase.client.write.buffer}.
1066    * @return The size of the write buffer in bytes.
1067    */
1068   public long getWriteBufferSize() {
1069     return writeBufferSize;
1070   }
1071 
1072   /**
1073    * Sets the size of the buffer in bytes.
1074    * <p>
1075    * If the new size is less than the current amount of data in the
1076    * write buffer, the buffer gets flushed.
1077    * @param writeBufferSize The new write buffer size, in bytes.
1078    * @throws IOException if a remote or network exception occurs.
1079    */
1080   public void setWriteBufferSize(long writeBufferSize) throws IOException {
1081     this.writeBufferSize = writeBufferSize;
1082     if(currentWriteBufferSize > writeBufferSize) {
1083       flushCommits();
1084     }
1085   }
1086 
1087   /**
1088    * Returns the write buffer.
1089    * @return The current write buffer.
1090    */
1091   public ArrayList<Put> getWriteBuffer() {
1092     return writeBuffer;
1093   }
1094 
1095   /**
1096    * The pool is used for mutli requests for this HTable
1097    * @return the pool used for mutli
1098    */
1099   ExecutorService getPool() {
1100     return this.pool;
1101   }
1102 
1103   /**
1104    * Enable or disable region cache prefetch for the table. It will be
1105    * applied for the given table's all HTable instances who share the same
1106    * connection. By default, the cache prefetch is enabled.
1107    * @param tableName name of table to configure.
1108    * @param enable Set to true to enable region cache prefetch. Or set to
1109    * false to disable it.
1110    * @throws IOException
1111    */
1112   public static void setRegionCachePrefetch(final byte[] tableName,
1113       final boolean enable) throws IOException {
1114     HConnectionManager.execute(new HConnectable<Void>(HBaseConfiguration
1115         .create()) {
1116       @Override
1117       public Void connect(HConnection connection) throws IOException {
1118         connection.setRegionCachePrefetch(tableName, enable);
1119         return null;
1120       }
1121     });
1122   }
1123 
1124   /**
1125    * Enable or disable region cache prefetch for the table. It will be
1126    * applied for the given table's all HTable instances who share the same
1127    * connection. By default, the cache prefetch is enabled.
1128    * @param conf The Configuration object to use.
1129    * @param tableName name of table to configure.
1130    * @param enable Set to true to enable region cache prefetch. Or set to
1131    * false to disable it.
1132    * @throws IOException
1133    */
1134   public static void setRegionCachePrefetch(final Configuration conf,
1135       final byte[] tableName, final boolean enable) throws IOException {
1136     HConnectionManager.execute(new HConnectable<Void>(conf) {
1137       @Override
1138       public Void connect(HConnection connection) throws IOException {
1139         connection.setRegionCachePrefetch(tableName, enable);
1140         return null;
1141       }
1142     });
1143   }
1144 
1145   /**
1146    * Check whether region cache prefetch is enabled or not for the table.
1147    * @param conf The Configuration object to use.
1148    * @param tableName name of table to check
1149    * @return true if table's region cache prefecth is enabled. Otherwise
1150    * it is disabled.
1151    * @throws IOException
1152    */
1153   public static boolean getRegionCachePrefetch(final Configuration conf,
1154       final byte[] tableName) throws IOException {
1155     return HConnectionManager.execute(new HConnectable<Boolean>(conf) {
1156       @Override
1157       public Boolean connect(HConnection connection) throws IOException {
1158         return connection.getRegionCachePrefetch(tableName);
1159       }
1160     });
1161   }
1162 
1163   /**
1164    * Check whether region cache prefetch is enabled or not for the table.
1165    * @param tableName name of table to check
1166    * @return true if table's region cache prefecth is enabled. Otherwise
1167    * it is disabled.
1168    * @throws IOException
1169    */
1170   public static boolean getRegionCachePrefetch(final byte[] tableName) throws IOException {
1171     return HConnectionManager.execute(new HConnectable<Boolean>(
1172         HBaseConfiguration.create()) {
1173       @Override
1174       public Boolean connect(HConnection connection) throws IOException {
1175         return connection.getRegionCachePrefetch(tableName);
1176       }
1177     });
1178  }
1179 
1180   /**
1181    * Explicitly clears the region cache to fetch the latest value from META.
1182    * This is a power user function: avoid unless you know the ramifications.
1183    */
1184   public void clearRegionCache() {
1185     this.connection.clearRegionCache();
1186   }
1187 
1188   /**
1189    * {@inheritDoc}
1190    */
1191   @Override
1192   public <T extends CoprocessorProtocol> T coprocessorProxy(
1193       Class<T> protocol, byte[] row) {
1194     return (T)Proxy.newProxyInstance(this.getClass().getClassLoader(),
1195         new Class[]{protocol},
1196         new ExecRPCInvoker(configuration,
1197             connection,
1198             protocol,
1199             tableName,
1200             row));
1201   }
1202 
1203   /**
1204    * {@inheritDoc}
1205    */
1206   @Override
1207   public <T extends CoprocessorProtocol, R> Map<byte[],R> coprocessorExec(
1208       Class<T> protocol, byte[] startKey, byte[] endKey,
1209       Batch.Call<T,R> callable)
1210       throws IOException, Throwable {
1211 
1212     final Map<byte[],R> results =  Collections.synchronizedMap(new TreeMap<byte[],R>(
1213         Bytes.BYTES_COMPARATOR));
1214     coprocessorExec(protocol, startKey, endKey, callable,
1215         new Batch.Callback<R>(){
1216       public void update(byte[] region, byte[] row, R value) {
1217         results.put(region, value);
1218       }
1219     });
1220     return results;
1221   }
1222 
1223   /**
1224    * {@inheritDoc}
1225    */
1226   @Override
1227   public <T extends CoprocessorProtocol, R> void coprocessorExec(
1228       Class<T> protocol, byte[] startKey, byte[] endKey,
1229       Batch.Call<T,R> callable, Batch.Callback<R> callback)
1230       throws IOException, Throwable {
1231 
1232     // get regions covered by the row range
1233     List<byte[]> keys = getStartKeysInRange(startKey, endKey);
1234     connection.processExecs(protocol, keys, tableName, pool, callable,
1235         callback);
1236   }
1237 
1238   private List<byte[]> getStartKeysInRange(byte[] start, byte[] end)
1239   throws IOException {
1240     Pair<byte[][],byte[][]> startEndKeys = getStartEndKeys();
1241     byte[][] startKeys = startEndKeys.getFirst();
1242     byte[][] endKeys = startEndKeys.getSecond();
1243 
1244     if (start == null) {
1245       start = HConstants.EMPTY_START_ROW;
1246     }
1247     if (end == null) {
1248       end = HConstants.EMPTY_END_ROW;
1249     }
1250 
1251     List<byte[]> rangeKeys = new ArrayList<byte[]>();
1252     for (int i=0; i<startKeys.length; i++) {
1253       if (Bytes.compareTo(start, startKeys[i]) >= 0 ) {
1254         if (Bytes.equals(endKeys[i], HConstants.EMPTY_END_ROW) ||
1255             Bytes.compareTo(start, endKeys[i]) < 0) {
1256           rangeKeys.add(start);
1257         }
1258       } else if (Bytes.equals(end, HConstants.EMPTY_END_ROW) ||
1259           Bytes.compareTo(startKeys[i], end) <= 0) {
1260         rangeKeys.add(startKeys[i]);
1261       } else {
1262         break; // past stop
1263       }
1264     }
1265 
1266     return rangeKeys;
1267   }
1268 
1269   public void setOperationTimeout(int operationTimeout) {
1270     this.operationTimeout = operationTimeout;
1271   }
1272 
1273   public int getOperationTimeout() {
1274     return operationTimeout;
1275   }
1276 
1277 }