HBASE-13327 Use Admin in ConnectionCache (Solomon Duskis)

This commit is contained in:
tedyu 2015-03-24 18:13:51 -07:00
parent 8cb4f89c01
commit 227ace9100
2 changed files with 35 additions and 22 deletions

View File

@ -29,9 +29,9 @@ import org.apache.hadoop.hbase.ScheduledChore;
import org.apache.hadoop.hbase.Stoppable; import org.apache.hadoop.hbase.Stoppable;
import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.classification.InterfaceAudience; 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.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory; 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.client.Table;
import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.security.UserProvider; import org.apache.hadoop.hbase.security.UserProvider;
@ -128,14 +128,13 @@ public class ConnectionCache {
* Caller doesn't close the admin afterwards. * Caller doesn't close the admin afterwards.
* We need to manage it and close it properly. * We need to manage it and close it properly.
*/ */
@SuppressWarnings("deprecation") public Admin getAdmin() throws IOException {
public HBaseAdmin getAdmin() throws IOException {
ConnectionInfo connInfo = getCurrentConnection(); ConnectionInfo connInfo = getCurrentConnection();
if (connInfo.admin == null) { if (connInfo.admin == null) {
Lock lock = locker.acquireLock(getEffectiveUser()); Lock lock = locker.acquireLock(getEffectiveUser());
try { try {
if (connInfo.admin == null) { if (connInfo.admin == null) {
connInfo.admin = new HBaseAdmin(connInfo.connection); connInfo.admin = connInfo.connection.getAdmin();
} }
} finally { } finally {
lock.unlock(); lock.unlock();
@ -184,7 +183,7 @@ public class ConnectionCache {
final Connection connection; final Connection connection;
final String userName; final String userName;
volatile HBaseAdmin admin; volatile Admin admin;
private long lastAccessTime; private long lastAccessTime;
private boolean closed; private boolean closed;

View File

@ -61,11 +61,11 @@ import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.classification.InterfaceAudience; 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.Append;
import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Durability; import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.Get; 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.HTable;
import org.apache.hadoop.hbase.client.Increment; import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.OperationWithAttributes; 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. * Obtain HBaseAdmin. Creates the instance if it is not already created.
*/ */
private HBaseAdmin getHBaseAdmin() throws IOException { private Admin getAdmin() throws IOException {
return connectionCache.getAdmin(); return connectionCache.getAdmin();
} }
@ -749,7 +749,7 @@ public class ThriftServerRunner implements Runnable {
@Override @Override
public void enableTable(ByteBuffer tableName) throws IOError { public void enableTable(ByteBuffer tableName) throws IOError {
try{ try{
getHBaseAdmin().enableTable(getBytes(tableName)); getAdmin().enableTable(getTableName(tableName));
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage(), e); LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage()); throw new IOError(e.getMessage());
@ -759,7 +759,7 @@ public class ThriftServerRunner implements Runnable {
@Override @Override
public void disableTable(ByteBuffer tableName) throws IOError{ public void disableTable(ByteBuffer tableName) throws IOError{
try{ try{
getHBaseAdmin().disableTable(getBytes(tableName)); getAdmin().disableTable(getTableName(tableName));
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage(), e); LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage()); throw new IOError(e.getMessage());
@ -778,8 +778,13 @@ public class ThriftServerRunner implements Runnable {
@Override @Override
public void compact(ByteBuffer tableNameOrRegionName) throws IOError { public void compact(ByteBuffer tableNameOrRegionName) throws IOError {
byte[] tableNameOrRegionNameArray = getBytes(tableNameOrRegionName);
try { try {
getHBaseAdmin().compact(getBytes(tableNameOrRegionName)); try {
getAdmin().compactRegion(tableNameOrRegionNameArray);
} catch (IllegalArgumentException e) {
getAdmin().compact(TableName.valueOf(tableNameOrRegionNameArray));
}
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage(), e); LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage()); throw new IOError(e.getMessage());
@ -788,8 +793,13 @@ public class ThriftServerRunner implements Runnable {
@Override @Override
public void majorCompact(ByteBuffer tableNameOrRegionName) throws IOError { public void majorCompact(ByteBuffer tableNameOrRegionName) throws IOError {
byte[] tableNameOrRegionNameArray = getBytes(tableNameOrRegionName);
try { try {
getHBaseAdmin().majorCompact(getBytes(tableNameOrRegionName)); try {
getAdmin().majorCompactRegion(tableNameOrRegionNameArray);
} catch (IllegalArgumentException e) {
getAdmin().majorCompact(TableName.valueOf(tableNameOrRegionNameArray));
}
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage(), e); LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage()); throw new IOError(e.getMessage());
@ -799,7 +809,7 @@ public class ThriftServerRunner implements Runnable {
@Override @Override
public List<ByteBuffer> getTableNames() throws IOError { public List<ByteBuffer> getTableNames() throws IOError {
try { try {
TableName[] tableNames = this.getHBaseAdmin().listTableNames(); TableName[] tableNames = this.getAdmin().listTableNames();
ArrayList<ByteBuffer> list = new ArrayList<ByteBuffer>(tableNames.length); ArrayList<ByteBuffer> list = new ArrayList<ByteBuffer>(tableNames.length);
for (int i = 0; i < tableNames.length; i++) { for (int i = 0; i < tableNames.length; i++) {
list.add(ByteBuffer.wrap(tableNames[i].getName())); list.add(ByteBuffer.wrap(tableNames[i].getName()));
@ -1164,17 +1174,17 @@ public class ThriftServerRunner implements Runnable {
public void createTable(ByteBuffer in_tableName, public void createTable(ByteBuffer in_tableName,
List<ColumnDescriptor> columnFamilies) throws IOError, List<ColumnDescriptor> columnFamilies) throws IOError,
IllegalArgument, AlreadyExists { IllegalArgument, AlreadyExists {
byte [] tableName = getBytes(in_tableName); TableName tableName = getTableName(in_tableName);
try { try {
if (getHBaseAdmin().tableExists(tableName)) { if (getAdmin().tableExists(tableName)) {
throw new AlreadyExists("table name already in use"); throw new AlreadyExists("table name already in use");
} }
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName)); HTableDescriptor desc = new HTableDescriptor(tableName);
for (ColumnDescriptor col : columnFamilies) { for (ColumnDescriptor col : columnFamilies) {
HColumnDescriptor colDesc = ThriftUtilities.colDescFromThrift(col); HColumnDescriptor colDesc = ThriftUtilities.colDescFromThrift(col);
desc.addFamily(colDesc); desc.addFamily(colDesc);
} }
getHBaseAdmin().createTable(desc); getAdmin().createTable(desc);
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage(), e); LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage()); 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 @Override
public void deleteTable(ByteBuffer in_tableName) throws IOError { public void deleteTable(ByteBuffer in_tableName) throws IOError {
byte [] tableName = getBytes(in_tableName); TableName tableName = getTableName(in_tableName);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("deleteTable: table=" + Bytes.toString(tableName)); LOG.debug("deleteTable: table=" + tableName);
} }
try { try {
if (!getHBaseAdmin().tableExists(tableName)) { if (!getAdmin().tableExists(tableName)) {
throw new IOException("table does not exist"); throw new IOException("table does not exist");
} }
getHBaseAdmin().deleteTable(tableName); getAdmin().deleteTable(tableName);
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage(), e); LOG.warn(e.getMessage(), e);
throw new IOError(e.getMessage()); throw new IOError(e.getMessage());