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.util.Collection;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.client.coprocessor.Batch;
33  import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback;
34  import org.apache.hadoop.hbase.filter.BinaryComparator;
35  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
36  import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.PoolMap;
39  import org.apache.hadoop.hbase.util.PoolMap.PoolType;
40  
41  import com.google.protobuf.Descriptors;
42  import com.google.protobuf.Message;
43  import com.google.protobuf.Service;
44  import com.google.protobuf.ServiceException;
45  
46  /**
47   * A simple pool of HTable instances.
48   *
49   * Each HTablePool acts as a pool for all tables. To use, instantiate an
50   * HTablePool and use {@link #getTable(String)} to get an HTable from the pool.
51   *
52     * This method is not needed anymore, clients should call
53     * HTableInterface.close() rather than returning the tables to the pool
54     *
55   * Once you are done with it, close your instance of {@link HTableInterface}
56   * by calling {@link HTableInterface#close()} rather than returning the tables
57   * to the pool with (deprecated) {@link #putTable(HTableInterface)}.
58   *
59   * <p>
60   * A pool can be created with a <i>maxSize</i> which defines the most HTable
61   * references that will ever be retained for each table. Otherwise the default
62   * is {@link Integer#MAX_VALUE}.
63   *
64   * <p>
65   * Pool will manage its own connections to the cluster. See
66   * {@link HConnectionManager}.
67   * @deprecated as of 0.98.1. See {@link HConnection#getTable(String)}.
68   */
69  @InterfaceAudience.Private
70  @Deprecated
71  public class HTablePool implements Closeable {
72    private final PoolMap<String, HTableInterface> tables;
73    private final int maxSize;
74    private final PoolType poolType;
75    private final Configuration config;
76    private final HTableInterfaceFactory tableFactory;
77  
78    /**
79     * Default Constructor. Default HBaseConfiguration and no limit on pool size.
80     */
81    public HTablePool() {
82      this(HBaseConfiguration.create(), Integer.MAX_VALUE);
83    }
84  
85    /**
86     * Constructor to set maximum versions and use the specified configuration.
87     *
88     * @param config
89     *          configuration
90     * @param maxSize
91     *          maximum number of references to keep for each table
92     */
93    public HTablePool(final Configuration config, final int maxSize) {
94      this(config, maxSize, null, null);
95    }
96  
97    /**
98     * Constructor to set maximum versions and use the specified configuration and
99     * table factory.
100    *
101    * @param config
102    *          configuration
103    * @param maxSize
104    *          maximum number of references to keep for each table
105    * @param tableFactory
106    *          table factory
107    */
108   public HTablePool(final Configuration config, final int maxSize,
109       final HTableInterfaceFactory tableFactory) {
110     this(config, maxSize, tableFactory, PoolType.Reusable);
111   }
112 
113   /**
114    * Constructor to set maximum versions and use the specified configuration and
115    * pool type.
116    *
117    * @param config
118    *          configuration
119    * @param maxSize
120    *          maximum number of references to keep for each table
121    * @param poolType
122    *          pool type which is one of {@link PoolType#Reusable} or
123    *          {@link PoolType#ThreadLocal}
124    */
125   public HTablePool(final Configuration config, final int maxSize,
126       final PoolType poolType) {
127     this(config, maxSize, null, poolType);
128   }
129 
130   /**
131    * Constructor to set maximum versions and use the specified configuration,
132    * table factory and pool type. The HTablePool supports the
133    * {@link PoolType#Reusable} and {@link PoolType#ThreadLocal}. If the pool
134    * type is null or not one of those two values, then it will default to
135    * {@link PoolType#Reusable}.
136    *
137    * @param config
138    *          configuration
139    * @param maxSize
140    *          maximum number of references to keep for each table
141    * @param tableFactory
142    *          table factory
143    * @param poolType
144    *          pool type which is one of {@link PoolType#Reusable} or
145    *          {@link PoolType#ThreadLocal}
146    */
147   public HTablePool(final Configuration config, final int maxSize,
148       final HTableInterfaceFactory tableFactory, PoolType poolType) {
149     // Make a new configuration instance so I can safely cleanup when
150     // done with the pool.
151     this.config = config == null ? HBaseConfiguration.create() : config;
152     this.maxSize = maxSize;
153     this.tableFactory = tableFactory == null ? new HTableFactory()
154         : tableFactory;
155     if (poolType == null) {
156       this.poolType = PoolType.Reusable;
157     } else {
158       switch (poolType) {
159       case Reusable:
160       case ThreadLocal:
161         this.poolType = poolType;
162         break;
163       default:
164         this.poolType = PoolType.Reusable;
165         break;
166       }
167     }
168     this.tables = new PoolMap<String, HTableInterface>(this.poolType,
169         this.maxSize);
170   }
171 
172   /**
173    * Get a reference to the specified table from the pool.
174    * <p>
175    * <p/>
176    *
177    * @param tableName
178    *          table name
179    * @return a reference to the specified table
180    * @throws RuntimeException
181    *           if there is a problem instantiating the HTable
182    */
183   public HTableInterface getTable(String tableName) {
184     // call the old getTable implementation renamed to findOrCreateTable
185     HTableInterface table = findOrCreateTable(tableName);
186     // return a proxy table so when user closes the proxy, the actual table
187     // will be returned to the pool
188     return new PooledHTable(table);
189   }
190 
191   /**
192    * Get a reference to the specified table from the pool.
193    * <p>
194    *
195    * Create a new one if one is not available.
196    *
197    * @param tableName
198    *          table name
199    * @return a reference to the specified table
200    * @throws RuntimeException
201    *           if there is a problem instantiating the HTable
202    */
203   private HTableInterface findOrCreateTable(String tableName) {
204     HTableInterface table = tables.get(tableName);
205     if (table == null) {
206       table = createHTable(tableName);
207     }
208     return table;
209   }
210 
211   /**
212    * Get a reference to the specified table from the pool.
213    * <p>
214    *
215    * Create a new one if one is not available.
216    *
217    * @param tableName
218    *          table name
219    * @return a reference to the specified table
220    * @throws RuntimeException
221    *           if there is a problem instantiating the HTable
222    */
223   public HTableInterface getTable(byte[] tableName) {
224     return getTable(Bytes.toString(tableName));
225   }
226 
227   /**
228    * This method is not needed anymore, clients should call
229    * HTableInterface.close() rather than returning the tables to the pool
230    *
231    * @param table
232    *          the proxy table user got from pool
233    * @deprecated
234    */
235   public void putTable(HTableInterface table) throws IOException {
236     // we need to be sure nobody puts a proxy implementation in the pool
237     // but if the client code is not updated
238     // and it will continue to call putTable() instead of calling close()
239     // then we need to return the wrapped table to the pool instead of the
240     // proxy
241     // table
242     if (table instanceof PooledHTable) {
243       returnTable(((PooledHTable) table).getWrappedTable());
244     } else {
245       // normally this should not happen if clients pass back the same
246       // table
247       // object they got from the pool
248       // but if it happens then it's better to reject it
249       throw new IllegalArgumentException("not a pooled table: " + table);
250     }
251   }
252 
253   /**
254    * Puts the specified HTable back into the pool.
255    * <p>
256    *
257    * If the pool already contains <i>maxSize</i> references to the table, then
258    * the table instance gets closed after flushing buffered edits.
259    *
260    * @param table
261    *          table
262    */
263   private void returnTable(HTableInterface table) throws IOException {
264     // this is the old putTable method renamed and made private
265     String tableName = Bytes.toString(table.getTableName());
266     if (tables.size(tableName) >= maxSize) {
267       // release table instance since we're not reusing it
268       this.tables.removeValue(tableName, table);
269       this.tableFactory.releaseHTableInterface(table);
270       return;
271     }
272     tables.put(tableName, table);
273   }
274 
275   protected HTableInterface createHTable(String tableName) {
276     return this.tableFactory.createHTableInterface(config,
277         Bytes.toBytes(tableName));
278   }
279 
280   /**
281    * Closes all the HTable instances , belonging to the given table, in the
282    * table pool.
283    * <p>
284    * Note: this is a 'shutdown' of the given table pool and different from
285    * {@link #putTable(HTableInterface)}, that is used to return the table
286    * instance to the pool for future re-use.
287    *
288    * @param tableName
289    */
290   public void closeTablePool(final String tableName) throws IOException {
291     Collection<HTableInterface> tables = this.tables.values(tableName);
292     if (tables != null) {
293       for (HTableInterface table : tables) {
294         this.tableFactory.releaseHTableInterface(table);
295       }
296     }
297     this.tables.remove(tableName);
298   }
299 
300   /**
301    * See {@link #closeTablePool(String)}.
302    *
303    * @param tableName
304    */
305   public void closeTablePool(final byte[] tableName) throws IOException {
306     closeTablePool(Bytes.toString(tableName));
307   }
308 
309   /**
310    * Closes all the HTable instances , belonging to all tables in the table
311    * pool.
312    * <p>
313    * Note: this is a 'shutdown' of all the table pools.
314    */
315   public void close() throws IOException {
316     for (String tableName : tables.keySet()) {
317       closeTablePool(tableName);
318     }
319     this.tables.clear();
320   }
321 
322   public int getCurrentPoolSize(String tableName) {
323     return tables.size(tableName);
324   }
325 
326   /**
327    * A proxy class that implements HTableInterface.close method to return the
328    * wrapped table back to the table pool
329    *
330    */
331   class PooledHTable implements HTableInterface {
332 
333     private boolean open = false;
334 
335     private HTableInterface table; // actual table implementation
336 
337     public PooledHTable(HTableInterface table) {
338       this.table = table;
339       this.open = true;
340     }
341 
342     @Override
343     public byte[] getTableName() {
344       checkState();
345       return table.getTableName();
346     }
347 
348     @Override
349     public TableName getName() {
350       return table.getName();
351     }
352 
353     @Override
354     public Configuration getConfiguration() {
355       checkState();
356       return table.getConfiguration();
357     }
358 
359     @Override
360     public HTableDescriptor getTableDescriptor() throws IOException {
361       checkState();
362       return table.getTableDescriptor();
363     }
364 
365     @Override
366     public boolean exists(Get get) throws IOException {
367       checkState();
368       return table.exists(get);
369     }
370 
371     @Override
372     public Boolean[] exists(List<Get> gets) throws IOException {
373       checkState();
374       return table.exists(gets);
375     }
376 
377     @Override
378     public void batch(List<? extends Row> actions, Object[] results) throws IOException,
379         InterruptedException {
380       checkState();
381       table.batch(actions, results);
382     }
383 
384     /**
385      * {@inheritDoc}
386      * @deprecated If any exception is thrown by one of the actions, there is no way to
387      * retrieve the partially executed results. Use {@link #batch(List, Object[])} instead.
388      */
389     @Override
390     public Object[] batch(List<? extends Row> actions) throws IOException,
391         InterruptedException {
392       checkState();
393       return table.batch(actions);
394     }
395 
396     @Override
397     public Result get(Get get) throws IOException {
398       checkState();
399       return table.get(get);
400     }
401 
402     @Override
403     public Result[] get(List<Get> gets) throws IOException {
404       checkState();
405       return table.get(gets);
406     }
407 
408     @Override
409     @SuppressWarnings("deprecation")
410     public Result getRowOrBefore(byte[] row, byte[] family) throws IOException {
411       checkState();
412       return table.getRowOrBefore(row, family);
413     }
414 
415     @Override
416     public ResultScanner getScanner(Scan scan) throws IOException {
417       checkState();
418       return table.getScanner(scan);
419     }
420 
421     @Override
422     public ResultScanner getScanner(byte[] family) throws IOException {
423       checkState();
424       return table.getScanner(family);
425     }
426 
427     @Override
428     public ResultScanner getScanner(byte[] family, byte[] qualifier)
429         throws IOException {
430       checkState();
431       return table.getScanner(family, qualifier);
432     }
433 
434     @Override
435     public void put(Put put) throws IOException {
436       checkState();
437       table.put(put);
438     }
439 
440     @Override
441     public void put(List<Put> puts) throws IOException {
442       checkState();
443       table.put(puts);
444     }
445 
446     @Override
447     public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier,
448         byte[] value, Put put) throws IOException {
449       checkState();
450       return table.checkAndPut(row, family, qualifier, value, put);
451     }
452 
453     @Override
454     public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier,
455         CompareOp compareOp, byte[] value, Put put) throws IOException {
456       checkState();
457       return table.checkAndPut(row, family, qualifier, compareOp, value, put);
458     }
459 
460     @Override
461     public void delete(Delete delete) throws IOException {
462       checkState();
463       table.delete(delete);
464     }
465 
466     @Override
467     public void delete(List<Delete> deletes) throws IOException {
468       checkState();
469       table.delete(deletes);
470     }
471 
472     @Override
473     public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier,
474         byte[] value, Delete delete) throws IOException {
475       checkState();
476       return table.checkAndDelete(row, family, qualifier, value, delete);
477     }
478 
479     @Override
480     public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier,
481         CompareOp compareOp, byte[] value, Delete delete) throws IOException {
482       checkState();
483       return table.checkAndDelete(row, family, qualifier, compareOp, value, delete);
484     }
485 
486     @Override
487     public Result increment(Increment increment) throws IOException {
488       checkState();
489       return table.increment(increment);
490     }
491 
492     @Override
493     public long incrementColumnValue(byte[] row, byte[] family,
494         byte[] qualifier, long amount) throws IOException {
495       checkState();
496       return table.incrementColumnValue(row, family, qualifier, amount);
497     }
498 
499     @Override
500     public long incrementColumnValue(byte[] row, byte[] family,
501         byte[] qualifier, long amount, Durability durability) throws IOException {
502       checkState();
503       return table.incrementColumnValue(row, family, qualifier, amount,
504           durability);
505     }
506 
507     @Override
508     public boolean isAutoFlush() {
509       checkState();
510       return table.isAutoFlush();
511     }
512 
513     @Override
514     public void flushCommits() throws IOException {
515       checkState();
516       table.flushCommits();
517     }
518 
519     /**
520      * Returns the actual table back to the pool
521      *
522      * @throws IOException
523      */
524     public void close() throws IOException {
525       checkState();
526       open = false;
527       returnTable(table);
528     }
529 
530     @Override
531     public CoprocessorRpcChannel coprocessorService(byte[] row) {
532       checkState();
533       return table.coprocessorService(row);
534     }
535 
536     @Override
537     public <T extends Service, R> Map<byte[], R> coprocessorService(Class<T> service,
538         byte[] startKey, byte[] endKey, Batch.Call<T, R> callable)
539         throws ServiceException, Throwable {
540       checkState();
541       return table.coprocessorService(service, startKey, endKey, callable);
542     }
543 
544     @Override
545     public <T extends Service, R> void coprocessorService(Class<T> service,
546         byte[] startKey, byte[] endKey, Batch.Call<T, R> callable, Callback<R> callback)
547         throws ServiceException, Throwable {
548       checkState();
549       table.coprocessorService(service, startKey, endKey, callable, callback);
550     }
551 
552     @Override
553     public String toString() {
554       return "PooledHTable{" + ", table=" + table + '}';
555     }
556 
557     /**
558      * Expose the wrapped HTable to tests in the same package
559      *
560      * @return wrapped htable
561      */
562     HTableInterface getWrappedTable() {
563       return table;
564     }
565 
566     @Override
567     public <R> void batchCallback(List<? extends Row> actions,
568         Object[] results, Callback<R> callback) throws IOException,
569         InterruptedException {
570       checkState();
571       table.batchCallback(actions, results, callback);
572     }
573 
574     /**
575      * {@inheritDoc}
576      * @deprecated If any exception is thrown by one of the actions, there is no way to
577      * retrieve the partially executed results. Use
578      * {@link #batchCallback(List, Object[], org.apache.hadoop.hbase.client.coprocessor.Batch.Callback)}
579      * instead.
580      */
581     @Override
582     public <R> Object[] batchCallback(List<? extends Row> actions,
583         Callback<R> callback) throws IOException, InterruptedException {
584       checkState();
585       return table.batchCallback(actions,  callback);
586     }
587 
588     @Override
589     public void mutateRow(RowMutations rm) throws IOException {
590       checkState();
591       table.mutateRow(rm);
592     }
593 
594     @Override
595     public Result append(Append append) throws IOException {
596       checkState();
597       return table.append(append);
598     }
599 
600     @Override
601     public void setAutoFlush(boolean autoFlush) {
602       checkState();
603       table.setAutoFlush(autoFlush, autoFlush);
604     }
605 
606     @Override
607     public void setAutoFlush(boolean autoFlush, boolean clearBufferOnFail) {
608       checkState();
609       table.setAutoFlush(autoFlush, clearBufferOnFail);
610     }
611 
612     @Override
613     public void setAutoFlushTo(boolean autoFlush) {
614       table.setAutoFlushTo(autoFlush);
615     }
616 
617     @Override
618     public long getWriteBufferSize() {
619       checkState();
620       return table.getWriteBufferSize();
621     }
622 
623     @Override
624     public void setWriteBufferSize(long writeBufferSize) throws IOException {
625       checkState();
626       table.setWriteBufferSize(writeBufferSize);
627     }
628 
629     boolean isOpen() {
630       return open;
631     }
632 
633     private void checkState() {
634       if (!isOpen()) {
635         throw new IllegalStateException("Table=" + new String(table.getTableName()) + " already closed");
636       }
637     }
638 
639     @Override
640     public long incrementColumnValue(byte[] row, byte[] family,
641         byte[] qualifier, long amount, boolean writeToWAL) throws IOException {
642       return table.incrementColumnValue(row, family, qualifier, amount, writeToWAL);
643     }
644 
645     @Override
646     public <R extends Message> Map<byte[], R> batchCoprocessorService(
647         Descriptors.MethodDescriptor method, Message request,
648         byte[] startKey, byte[] endKey, R responsePrototype) throws ServiceException, Throwable {
649       checkState();
650       return table.batchCoprocessorService(method, request, startKey, endKey,
651           responsePrototype);
652     }
653 
654     @Override
655     public <R extends Message> void batchCoprocessorService(
656         Descriptors.MethodDescriptor method, Message request,
657         byte[] startKey, byte[] endKey, R responsePrototype, Callback<R> callback)
658         throws ServiceException, Throwable {
659       checkState();
660       table.batchCoprocessorService(method, request, startKey, endKey, responsePrototype, callback);
661     }
662   }
663 }