From 5a5c6c84db3c62d991f393e836e8391c494c488c Mon Sep 17 00:00:00 2001 From: chenheng Date: Wed, 12 Aug 2015 10:38:01 +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 | 22 ++--------- 2 files changed, 31 insertions(+), 35 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 ac9db75..0f2597d 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 @@ -444,25 +444,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)); + public 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) @@ -3984,6 +3991,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..257ec9a 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,14 @@ 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)) { + if (tableName != null && 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.9.3 (Apple Git-50)