From 2d1903437caf1894fb8ad0d5494ee35b2bb20e76 Mon Sep 17 00:00:00 2001 From: Sakthi Date: Sat, 23 Mar 2019 09:27:59 -0700 Subject: [PATCH] HBASE-22094: Throw TableNotFoundException if table not exists in AsyncAdmin.compact --- .../hadoop/hbase/client/AsyncAdmin.java | 8 ++ .../hbase/client/RawAsyncHBaseAdmin.java | 91 ++++++++++--------- .../hbase/client/TestAsyncRegionAdminApi.java | 21 +++++ 3 files changed, 78 insertions(+), 42 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java index f7adc165102147bd87333dbc28e0ed1518915d46..f20dfac0e7eaba4a3a9b106a32adb034b6975f44 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java @@ -317,6 +317,7 @@ public interface AsyncAdmin { /** * Compact a table. When the returned CompletableFuture is done, it only means the compact request * was sent to HBase and may need some time to finish the compact operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to compact */ default CompletableFuture compact(TableName tableName) { @@ -327,6 +328,7 @@ public interface AsyncAdmin { * Compact a column family within a table. When the returned CompletableFuture is done, it only * means the compact request was sent to HBase and may need some time to finish the compact * operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to compact * @param columnFamily column family within a table. If not present, compact the table's all * column families. @@ -338,6 +340,7 @@ public interface AsyncAdmin { /** * Compact a table. When the returned CompletableFuture is done, it only means the compact request * was sent to HBase and may need some time to finish the compact operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to compact * @param compactType {@link org.apache.hadoop.hbase.client.CompactType} */ @@ -347,6 +350,7 @@ public interface AsyncAdmin { * Compact a column family within a table. When the returned CompletableFuture is done, it only * means the compact request was sent to HBase and may need some time to finish the compact * operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to compact * @param columnFamily column family within a table * @param compactType {@link org.apache.hadoop.hbase.client.CompactType} @@ -374,6 +378,7 @@ public interface AsyncAdmin { /** * Major compact a table. When the returned CompletableFuture is done, it only means the compact * request was sent to HBase and may need some time to finish the compact operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to major compact */ default CompletableFuture majorCompact(TableName tableName) { @@ -384,6 +389,7 @@ public interface AsyncAdmin { * Major compact a column family within a table. When the returned CompletableFuture is done, it * only means the compact request was sent to HBase and may need some time to finish the compact * operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to major compact * @param columnFamily column family within a table. If not present, major compact the table's all * column families. @@ -395,6 +401,7 @@ public interface AsyncAdmin { /** * Major compact a table. When the returned CompletableFuture is done, it only means the compact * request was sent to HBase and may need some time to finish the compact operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to major compact * @param compactType {@link org.apache.hadoop.hbase.client.CompactType} */ @@ -404,6 +411,7 @@ public interface AsyncAdmin { * Major compact a column family within a table. When the returned CompletableFuture is done, it * only means the compact request was sent to HBase and may need some time to finish the compact * operation. + * Throws {@link org.apache.hadoop.hbase.TableNotFoundException} if table not found. * @param tableName table to major compact * @param columnFamily column family within a table. If not present, major compact the table's all * column families. diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java index 587c6e213507f4df75d60fd00688cc65a3873741..54e89ea9d5f1ac03368c7f8c4ebfe4e908a15f73 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java @@ -1079,48 +1079,55 @@ class RawAsyncHBaseAdmin implements AsyncAdmin { private CompletableFuture compact(TableName tableName, byte[] columnFamily, boolean major, CompactType compactType) { CompletableFuture future = new CompletableFuture<>(); - - switch (compactType) { - case MOB: - addListener(connection.registry.getMasterAddress(), (serverName, err) -> { - if (err != null) { - future.completeExceptionally(err); - return; - } - RegionInfo regionInfo = RegionInfo.createMobRegionInfo(tableName); - addListener(compact(serverName, regionInfo, major, columnFamily), (ret, err2) -> { - if (err2 != null) { - future.completeExceptionally(err2); - } else { - future.complete(ret); - } - }); - }); - break; - case NORMAL: - addListener(getTableHRegionLocations(tableName), (locations, err) -> { - if (err != null) { - future.completeExceptionally(err); - return; - } - CompletableFuture[] compactFutures = - locations.stream().filter(l -> l.getRegion() != null) - .filter(l -> !l.getRegion().isOffline()).filter(l -> l.getServerName() != null) - .map(l -> compact(l.getServerName(), l.getRegion(), major, columnFamily)) - .toArray(CompletableFuture[]::new); - // future complete unless all of the compact futures are completed. - addListener(CompletableFuture.allOf(compactFutures), (ret, err2) -> { - if (err2 != null) { - future.completeExceptionally(err2); - } else { - future.complete(ret); - } - }); - }); - break; - default: - throw new IllegalArgumentException("Unknown compactType: " + compactType); - } + addListener(tableExists(tableName), (exists, err) -> { + if (err != null) { + future.completeExceptionally(err); + } else if (!exists) { + future.completeExceptionally(new TableNotFoundException(tableName)); + } else { + switch (compactType) { + case MOB: + addListener(connection.registry.getMasterAddress(), (serverName, err2) -> { + if (err2 != null) { + future.completeExceptionally(err2); + return; + } + RegionInfo regionInfo = RegionInfo.createMobRegionInfo(tableName); + addListener(compact(serverName, regionInfo, major, columnFamily), (ret, err3) -> { + if (err3 != null) { + future.completeExceptionally(err3); + } else { + future.complete(ret); + } + }); + }); + break; + case NORMAL: + addListener(getTableHRegionLocations(tableName), (locations, err2) -> { + if (err2 != null) { + future.completeExceptionally(err2); + return; + } + CompletableFuture[] compactFutures = + locations.stream().filter(l -> l.getRegion() != null) + .filter(l -> !l.getRegion().isOffline()).filter(l -> l.getServerName() != null) + .map(l -> compact(l.getServerName(), l.getRegion(), major, columnFamily)) + .toArray(CompletableFuture[]::new); + // future complete unless all of the compact futures are completed. + addListener(CompletableFuture.allOf(compactFutures), (ret, err3) -> { + if (err3 != null) { + future.completeExceptionally(err3); + } else { + future.complete(ret); + } + }); + }); + break; + default: + throw new IllegalArgumentException("Unknown compactType: " + compactType); + } + } + }); return future; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApi.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApi.java index 6d30faf6248e80434089c5c8933cb50d98340022..3a8673cfba3dfc066d583236dcad0ce670f4015e 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApi.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApi.java @@ -36,6 +36,7 @@ import java.util.stream.Collectors; import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.master.HMaster; import org.apache.hadoop.hbase.master.RegionState; import org.apache.hadoop.hbase.master.ServerManager; @@ -427,6 +428,26 @@ public class TestAsyncRegionAdminApi extends TestAsyncAdminBase { } } + @Test + public void testNonExistentTableCompaction() { + testNonExistentTableCompaction(CompactionState.MINOR); + testNonExistentTableCompaction(CompactionState.MAJOR); + } + + private void testNonExistentTableCompaction(CompactionState compactionState) { + try { + if (compactionState == CompactionState.MINOR) { + admin.compact(TableName.valueOf("NonExistentTable")).get(); + } else { + admin.majorCompact(TableName.valueOf("NonExistentTable")).get(); + } + fail("Expected TableNotFoundException when table doesn't exist"); + } catch (Exception e) { + // expected. + assertTrue(e.getCause() instanceof TableNotFoundException); + } + } + private static int countStoreFilesInFamily(List regions, final byte[] family) { return countStoreFilesInFamilies(regions, new byte[][] { family }); } -- 2.17.2 (Apple Git-113)