From c4ebaa80dbb5134d3238985ae85272b85011e28a Mon Sep 17 00:00:00 2001 From: chenheng Date: Fri, 14 Aug 2015 23:06:59 +0800 Subject: [PATCH] HBASE-14203 remove duplicate code getTableDescriptor in HTable --- .../org/apache/hadoop/hbase/client/HBaseAdmin.java | 44 ++++++++++++------- .../org/apache/hadoop/hbase/client/HTable.java | 23 +--------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java index 1df3ffa..7ed303d 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java @@ -446,25 +446,32 @@ public class HBaseAdmin implements Admin { @Override public HTableDescriptor getTableDescriptor(final TableName tableName) throws TableNotFoundException, IOException { - if (tableName == null) return null; - HTableDescriptor htd = executeCallable(new MasterCallable(getConnection()) { - @Override - public HTableDescriptor call(int callTimeout) throws ServiceException { - GetTableDescriptorsResponse htds; - GetTableDescriptorsRequest req = - RequestConverter.buildGetTableDescriptorsRequest(tableName); - htds = master.getTableDescriptors(null, req); + return getTableDescriptor(tableName, getConnection(), rpcCallerFactory, operationTimeout); + } - if (!htds.getTableSchemaList().isEmpty()) { - return HTableDescriptor.convert(htds.getTableSchemaList().get(0)); + static HTableDescriptor getTableDescriptor(final TableName tableName, + HConnection connection, RpcRetryingCallerFactory rpcCallerFactory, + int operationTimeout) throws TableNotFoundException, IOException { + + if (tableName == null) return null; + HTableDescriptor htd = executeCallable(new MasterCallable(connection) { + @Override + public HTableDescriptor call(int callTimeout) throws ServiceException { + GetTableDescriptorsResponse htds; + GetTableDescriptorsRequest req = + RequestConverter.buildGetTableDescriptorsRequest(tableName); + htds = master.getTableDescriptors(null, req); + + if (!htds.getTableSchemaList().isEmpty()) { + return HTableDescriptor.convert(htds.getTableSchemaList().get(0)); + } + return null; } - return null; + }, rpcCallerFactory, operationTimeout); + if (htd != null) { + return htd; } - }); - if (htd != null) { - return htd; - } - throw new TableNotFoundException(tableName.getNameAsString()); + throw new TableNotFoundException(tableName.getNameAsString()); } public HTableDescriptor getTableDescriptor(final byte[] tableName) @@ -3986,6 +3993,11 @@ public class HBaseAdmin implements Admin { private & Closeable, V> V executeCallable(C callable) throws IOException { + return executeCallable(callable, rpcCallerFactory, operationTimeout); + } + + private static & Closeable, V> V executeCallable(C callable, + RpcRetryingCallerFactory rpcCallerFactory, int operationTimeout) throws IOException { RpcRetryingCaller caller = rpcCallerFactory.newCaller(); try { return caller.callWithRetries(callable, operationTimeout); diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java index 89c7893..46f8ec0 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java @@ -265,30 +265,11 @@ public class HTable implements HTableInterface { */ @Override public HTableDescriptor getTableDescriptor() throws IOException { - // TODO: This is the same as HBaseAdmin.getTableDescriptor(). Only keep one. - if (tableName == null) return null; - if (tableName.equals(TableName.META_TABLE_NAME)) { - return HTableDescriptor.META_TABLEDESC; - } - HTableDescriptor htd = executeMasterCallable( - new MasterCallable(getConnection()) { - @Override - public HTableDescriptor call(int callTimeout) throws ServiceException { - GetTableDescriptorsResponse htds; - GetTableDescriptorsRequest req = - RequestConverter.buildGetTableDescriptorsRequest(tableName); - htds = master.getTableDescriptors(null, req); - - if (!htds.getTableSchemaList().isEmpty()) { - return HTableDescriptor.convert(htds.getTableSchemaList().get(0)); - } - return null; - } - }); + HTableDescriptor htd = HBaseAdmin.getTableDescriptor(tableName, connection, rpcCallerFactory, operationTimeout); if (htd != null) { return new UnmodifyableHTableDescriptor(htd); } - throw new TableNotFoundException(tableName.getNameAsString()); + return null; } private V executeMasterCallable(MasterCallable callable) throws IOException { -- 1.7.9