From 034a00a6a1fbcc5ec9a991d82e2291c8635e1827 Mon Sep 17 00:00:00 2001 From: Shrijeet Paliwal Date: Tue, 3 Jan 2012 14:34:24 -0800 Subject: [PATCH] HBASE-5041: Throw error if table does not exist Admin operations on non existing tables should throw error. The three operations that have been changed under this are; split, compact and flush. A side-effect change is, the way we determine if the given name argument is a regioname. Now we make a call to MetaReader, a non null location for region name confirms validity. --- .../org/apache/hadoop/hbase/client/HBaseAdmin.java | 46 +++++++++++++++----- .../org/apache/hadoop/hbase/client/TestAdmin.java | 28 ++++++++++++ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java index 8cd9bd0..88c381f 100644 --- src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java +++ src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java @@ -1140,8 +1140,8 @@ public class HBaseAdmin implements Abortable, Closeable { */ public void flush(final byte [] tableNameOrRegionName) throws IOException, InterruptedException { - boolean isRegionName = isRegionName(tableNameOrRegionName); CatalogTracker ct = getCatalogTracker(); + boolean isRegionName = isRegionName(tableNameOrRegionName, ct); try { if (isRegionName) { Pair pair = @@ -1153,9 +1153,10 @@ public class HBaseAdmin implements Abortable, Closeable { flush(pair.getSecond(), pair.getFirst()); } } else { + final String tableName = tableNameString(tableNameOrRegionName, ct); List> pairs = MetaReader.getTableRegionsAndLocations(ct, - Bytes.toString(tableNameOrRegionName)); + tableName); for (Pair pair: pairs) { if (pair.getFirst().isOffline()) continue; if (pair.getSecond() == null) continue; @@ -1246,7 +1247,7 @@ public class HBaseAdmin implements Abortable, Closeable { throws IOException, InterruptedException { CatalogTracker ct = getCatalogTracker(); try { - if (isRegionName(tableNameOrRegionName)) { + if (isRegionName(tableNameOrRegionName, ct)) { Pair pair = MetaReader.getRegion(ct, tableNameOrRegionName); if (pair == null || pair.getSecond() == null) { @@ -1256,9 +1257,10 @@ public class HBaseAdmin implements Abortable, Closeable { compact(pair.getSecond(), pair.getFirst(), major); } } else { + final String tableName = tableNameString(tableNameOrRegionName, ct); List> pairs = MetaReader.getTableRegionsAndLocations(ct, - Bytes.toString(tableNameOrRegionName)); + tableName); for (Pair pair: pairs) { if (pair.getFirst().isOffline()) continue; if (pair.getSecond() == null) continue; @@ -1402,7 +1404,7 @@ public class HBaseAdmin implements Abortable, Closeable { final byte [] splitPoint) throws IOException, InterruptedException { CatalogTracker ct = getCatalogTracker(); try { - if (isRegionName(tableNameOrRegionName)) { + if (isRegionName(tableNameOrRegionName, ct)) { // Its a possible region name. Pair pair = MetaReader.getRegion(ct, tableNameOrRegionName); @@ -1413,9 +1415,10 @@ public class HBaseAdmin implements Abortable, Closeable { split(pair.getSecond(), pair.getFirst(), splitPoint); } } else { + final String tableName = tableNameString(tableNameOrRegionName, ct); List> pairs = MetaReader.getTableRegionsAndLocations(ct, - Bytes.toString(tableNameOrRegionName)); + tableName); for (Pair pair: pairs) { // May not be a server for a particular row if (pair.getSecond() == null) continue; @@ -1463,17 +1466,38 @@ public class HBaseAdmin implements Abortable, Closeable { /** * @param tableNameOrRegionName Name of a table or name of a region. - * @return True if tableNameOrRegionName is *possibly* a region - * name else false if a verified tablename (we call {@link #tableExists(byte[])}; - * else we throw an exception. + * @param ct A {@link #CatalogTracker} instance (caller of this method usually has one). + * @return True if tableNameOrRegionName is a verified region + * name (we call {@link #MetaReader.getRegion(CatalogTracker catalogTracker, + * byte [] regionName)};) else false. + * Throw an exception if tableNameOrRegionName is null. * @throws IOException */ - private boolean isRegionName(final byte [] tableNameOrRegionName) + private boolean isRegionName(final byte[] tableNameOrRegionName, + CatalogTracker ct) throws IOException { if (tableNameOrRegionName == null) { throw new IllegalArgumentException("Pass a table name or region name"); } - return !tableExists(tableNameOrRegionName); + return (MetaReader.getRegion(ct, tableNameOrRegionName) != null); + } + + /** + * Convert the table name byte array into a table name string and check if table + * exists or not. + * @param tableNameBytes Name of a table. + * @param ct A {@link #CatalogTracker} instance (caller of this method usually has one). + * @return tableName in string form. + * @throws IOException if a remote or network exception occurs. + * @throws TableNotFoundException if table does not exist. + */ + private String tableNameString(final byte[] tableNameBytes, CatalogTracker ct) + throws IOException { + String tableNameString = Bytes.toString(tableNameBytes); + if (!MetaReader.tableExists(ct, tableNameString)) { + throw new TableNotFoundException(tableNameString); + } + return tableNameString; } /** diff --git src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java index bb077d0..d300c17 100644 --- src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java +++ src/test/java/org/apache/hadoop/hbase/client/TestAdmin.java @@ -90,6 +90,34 @@ public class TestAdmin { } @Test + public void testSplitFlushCompactUnknownTable() throws InterruptedException { + final String unknowntable = "fubar"; + Exception exception = null; + try { + this.admin.compact(unknowntable); + } catch (IOException e) { + exception = e; + } + assertTrue(exception instanceof TableNotFoundException); + + exception = null; + try { + this.admin.flush(unknowntable); + } catch (IOException e) { + exception = e; + } + assertTrue(exception instanceof TableNotFoundException); + + exception = null; + try { + this.admin.split(unknowntable); + } catch (IOException e) { + exception = e; + } + assertTrue(exception instanceof TableNotFoundException); + } + + @Test public void testDeleteEditUnknownColumnFamilyAndOrTable() throws IOException { // Test we get exception if we try to final String nonexistent = "nonexistent"; -- 1.7.5.4