From 227ace9100043613a014d897dc78e97cc214ffac Mon Sep 17 00:00:00 2001 From: tedyu Date: Tue, 24 Mar 2015 18:13:51 -0700 Subject: [PATCH] HBASE-13327 Use Admin in ConnectionCache (Solomon Duskis) --- .../hadoop/hbase/util/ConnectionCache.java | 9 ++-- .../hbase/thrift/ThriftServerRunner.java | 48 ++++++++++++------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/ConnectionCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/ConnectionCache.java index bbfed4cf6f2..ab1948b7b2e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/ConnectionCache.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/ConnectionCache.java @@ -29,9 +29,9 @@ import org.apache.hadoop.hbase.ScheduledChore; import org.apache.hadoop.hbase.Stoppable; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; -import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.security.UserProvider; @@ -128,14 +128,13 @@ public class ConnectionCache { * Caller doesn't close the admin afterwards. * We need to manage it and close it properly. */ - @SuppressWarnings("deprecation") - public HBaseAdmin getAdmin() throws IOException { + public Admin getAdmin() throws IOException { ConnectionInfo connInfo = getCurrentConnection(); if (connInfo.admin == null) { Lock lock = locker.acquireLock(getEffectiveUser()); try { if (connInfo.admin == null) { - connInfo.admin = new HBaseAdmin(connInfo.connection); + connInfo.admin = connInfo.connection.getAdmin(); } } finally { lock.unlock(); @@ -184,7 +183,7 @@ public class ConnectionCache { final Connection connection; final String userName; - volatile HBaseAdmin admin; + volatile Admin admin; private long lastAccessTime; private boolean closed; diff --git a/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java b/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java index 9f23c09aea3..285d3695795 100644 --- a/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java +++ b/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServerRunner.java @@ -61,11 +61,11 @@ import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Append; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Durability; import org.apache.hadoop.hbase.client.Get; -import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Increment; import org.apache.hadoop.hbase.client.OperationWithAttributes; @@ -738,7 +738,7 @@ public class ThriftServerRunner implements Runnable { /** * Obtain HBaseAdmin. Creates the instance if it is not already created. */ - private HBaseAdmin getHBaseAdmin() throws IOException { + private Admin getAdmin() throws IOException { return connectionCache.getAdmin(); } @@ -749,7 +749,7 @@ public class ThriftServerRunner implements Runnable { @Override public void enableTable(ByteBuffer tableName) throws IOError { try{ - getHBaseAdmin().enableTable(getBytes(tableName)); + getAdmin().enableTable(getTableName(tableName)); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw new IOError(e.getMessage()); @@ -759,7 +759,7 @@ public class ThriftServerRunner implements Runnable { @Override public void disableTable(ByteBuffer tableName) throws IOError{ try{ - getHBaseAdmin().disableTable(getBytes(tableName)); + getAdmin().disableTable(getTableName(tableName)); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw new IOError(e.getMessage()); @@ -778,8 +778,13 @@ public class ThriftServerRunner implements Runnable { @Override public void compact(ByteBuffer tableNameOrRegionName) throws IOError { - try{ - getHBaseAdmin().compact(getBytes(tableNameOrRegionName)); + byte[] tableNameOrRegionNameArray = getBytes(tableNameOrRegionName); + try { + try { + getAdmin().compactRegion(tableNameOrRegionNameArray); + } catch (IllegalArgumentException e) { + getAdmin().compact(TableName.valueOf(tableNameOrRegionNameArray)); + } } catch (IOException e) { LOG.warn(e.getMessage(), e); throw new IOError(e.getMessage()); @@ -788,8 +793,13 @@ public class ThriftServerRunner implements Runnable { @Override public void majorCompact(ByteBuffer tableNameOrRegionName) throws IOError { - try{ - getHBaseAdmin().majorCompact(getBytes(tableNameOrRegionName)); + byte[] tableNameOrRegionNameArray = getBytes(tableNameOrRegionName); + try { + try { + getAdmin().majorCompactRegion(tableNameOrRegionNameArray); + } catch (IllegalArgumentException e) { + getAdmin().majorCompact(TableName.valueOf(tableNameOrRegionNameArray)); + } } catch (IOException e) { LOG.warn(e.getMessage(), e); throw new IOError(e.getMessage()); @@ -799,7 +809,7 @@ public class ThriftServerRunner implements Runnable { @Override public List getTableNames() throws IOError { try { - TableName[] tableNames = this.getHBaseAdmin().listTableNames(); + TableName[] tableNames = this.getAdmin().listTableNames(); ArrayList list = new ArrayList(tableNames.length); for (int i = 0; i < tableNames.length; i++) { list.add(ByteBuffer.wrap(tableNames[i].getName())); @@ -1164,17 +1174,17 @@ public class ThriftServerRunner implements Runnable { public void createTable(ByteBuffer in_tableName, List columnFamilies) throws IOError, IllegalArgument, AlreadyExists { - byte [] tableName = getBytes(in_tableName); + TableName tableName = getTableName(in_tableName); try { - if (getHBaseAdmin().tableExists(tableName)) { + if (getAdmin().tableExists(tableName)) { throw new AlreadyExists("table name already in use"); } - HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName)); + HTableDescriptor desc = new HTableDescriptor(tableName); for (ColumnDescriptor col : columnFamilies) { HColumnDescriptor colDesc = ThriftUtilities.colDescFromThrift(col); desc.addFamily(colDesc); } - getHBaseAdmin().createTable(desc); + getAdmin().createTable(desc); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw new IOError(e.getMessage()); @@ -1184,17 +1194,21 @@ public class ThriftServerRunner implements Runnable { } } + private static TableName getTableName(ByteBuffer buffer) { + return TableName.valueOf(getBytes(buffer)); + } + @Override public void deleteTable(ByteBuffer in_tableName) throws IOError { - byte [] tableName = getBytes(in_tableName); + TableName tableName = getTableName(in_tableName); if (LOG.isDebugEnabled()) { - LOG.debug("deleteTable: table=" + Bytes.toString(tableName)); + LOG.debug("deleteTable: table=" + tableName); } try { - if (!getHBaseAdmin().tableExists(tableName)) { + if (!getAdmin().tableExists(tableName)) { throw new IOException("table does not exist"); } - getHBaseAdmin().deleteTable(tableName); + getAdmin().deleteTable(tableName); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw new IOError(e.getMessage());