From 6d5cbbdc25562fca16dae7e76ec62fe3beb52f90 Mon Sep 17 00:00:00 2001 From: Andrew Kyle Purtell Date: Tue, 5 May 2009 09:13:05 +0000 Subject: [PATCH] HBASE-1341 HTable pooler git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@771633 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 1 + .../hadoop/hbase/client/HTablePool.java | 127 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100755 src/java/org/apache/hadoop/hbase/client/HTablePool.java diff --git a/CHANGES.txt b/CHANGES.txt index 33148801ec6..825fba16e07 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -198,6 +198,7 @@ Release 0.20.0 - Unreleased HBASE-889 The current Thrift API does not allow a new scanner to be created without supplying a column list unlike the other APIs. (Tim Sell via Stack) + HBASE-1341 HTable pooler Release 0.19.0 - 01/21/2009 INCOMPATIBLE CHANGES diff --git a/src/java/org/apache/hadoop/hbase/client/HTablePool.java b/src/java/org/apache/hadoop/hbase/client/HTablePool.java new file mode 100755 index 00000000000..301982e43f1 --- /dev/null +++ b/src/java/org/apache/hadoop/hbase/client/HTablePool.java @@ -0,0 +1,127 @@ +/** + * Copyright 2009 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.client; + +import java.io.IOException; +import java.util.ArrayDeque; +/* using a stack instead of a FIFO might have some small positive performance + impact wrt. cache */ +import java.util.Deque; +import java.util.Map; +import java.util.TreeMap; + +import org.apache.hadoop.hbase.util.Bytes; + +/** + * A simple pool of HTable instances. + *

+ * The default pool size is 10. + */ +public class HTablePool { + private static final Map poolMap = + new TreeMap(Bytes.BYTES_COMPARATOR); + + private final byte[] tableName; + private final Deque pool; + private final int maxSize; + + /** + * Get a shared table pool. + * @param tableName the table name + * @return the table pool + */ + public static HTablePool getPool(byte[] tableName) { + return getPool(tableName, 10); + } + + /** + * Get a shared table pool. + *

+ * NOTE: maxSize is advisory. If the pool does not yet exist, a new + * shared pool will be allocated with maxSize as the size limit. + * However, if the shared pool already exists, and was created with a + * different (or default) value for maxSize, it will not be changed. + * @param tableName the table name + * @param maxSize the maximum size of the pool + * @return the table pool + */ + public static HTablePool getPool(byte[] tableName, int maxSize) { + synchronized (poolMap) { + HTablePool pool = poolMap.get(tableName); + if (pool == null) { + pool = new HTablePool(tableName, maxSize); + poolMap.put(tableName, pool); + } + return pool; + } + } + + /** + * Constructor + * @param tableName the table name + */ + public HTablePool(byte[] tableName) { + this.tableName = tableName; + this.maxSize = 10; + this.pool = new ArrayDeque(this.maxSize); + } + + /** + * Constructor + * @param tableName the table name + * @param maxSize maximum pool size + */ + public HTablePool(byte[] tableName, int maxSize) { + this.tableName = tableName; + this.maxSize = maxSize; + this.pool = new ArrayDeque(this.maxSize); + } + + /** + * Get a HTable instance, possibly from the pool, if one is available. + * @return HTable a HTable instance + * @throws IOException + */ + public HTable get() throws IOException { + synchronized (pool) { + // peek then pop inside a synchronized block avoids the overhead of a + // NoSuchElementException + HTable table = pool.peek(); + if (table != null) { + return pool.pop(); + } + } + return new HTable(tableName); + } + + /** + * Return a HTable instance to the pool. + * @param table a HTable instance + */ + public void put(HTable table) { + synchronized (pool) { + if (pool.size() < maxSize) { + pool.push(table); + } + } + } + +}