From 34d62ecfae4cf79ae2882b91f2ca2a22f68281e0 Mon Sep 17 00:00:00 2001 From: tedyu Date: Mon, 27 Jun 2016 14:47:57 -0700 Subject: [PATCH] HBASE-14007 Writing to table through MR should fail upfront if table does not exist/is disabled (Thiruvel Thirumoolan) --- .../hbase/mapreduce/TableOutputFormat.java | 18 +++++++++++-- .../hbase/mapreduce/TestTableMapReduce.java | 26 +++++++++++++++++++ .../mapreduce/TestTableMapReduceBase.java | 3 +++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableOutputFormat.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableOutputFormat.java index 5904f9cec8a..9d4031ee6f1 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableOutputFormat.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/TableOutputFormat.java @@ -29,6 +29,9 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotEnabledException; +import org.apache.hadoop.hbase.TableNotFoundException; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.BufferedMutator; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; @@ -154,7 +157,7 @@ implements Configurable { } /** - * Checks if the output target exists. + * Checks if the output table exists and is enabled. * * @param context The current context. * @throws IOException When the check fails. @@ -164,8 +167,19 @@ implements Configurable { @Override public void checkOutputSpecs(JobContext context) throws IOException, InterruptedException { - // TODO Check if the table exists? + try (Admin admin = ConnectionFactory.createConnection(getConf()).getAdmin()) { + TableName tableName = TableName.valueOf(this.conf.get(OUTPUT_TABLE)); + if (!admin.tableExists(tableName)) { + throw new TableNotFoundException("Can't write, table does not exist:" + + tableName.getNameAsString()); + } + + if (!admin.isTableEnabled(tableName)) { + throw new TableNotEnabledException("Can't write, table is not enabled: " + + tableName.getNameAsString()); + } + } } /** diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduce.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduce.java index 6fb94600552..6c19a99a619 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduce.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduce.java @@ -31,6 +31,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotEnabledException; +import org.apache.hadoop.hbase.TableNotFoundException; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.testclassification.LargeTests; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; @@ -40,6 +44,7 @@ import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; +import org.junit.Test; import org.junit.experimental.categories.Category; /** @@ -124,4 +129,25 @@ public class TestTableMapReduce extends TestTableMapReduceBase { } } } + + @Test(expected = TableNotEnabledException.class) + public void testWritingToDisabledTable() throws IOException { + + try (Admin admin = UTIL.getConnection().getAdmin(); + HTable table = new HTable(UTIL.getConfiguration(), TABLE_FOR_NEGATIVE_TESTS)) { + admin.disableTable(table.getName()); + runTestOnTable(table); + fail("Should not have reached here, should have thrown an exception"); + } + } + + @Test(expected = TableNotFoundException.class) + public void testWritingToNonExistentTable() throws IOException { + + try (HTable table = new HTable(UTIL.getConfiguration(), + TableName.valueOf("table-does-not-exist"))) { + runTestOnTable(table); + fail("Should not have reached here, should have thrown an exception"); + } + } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduceBase.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduceBase.java index 022d4c9351b..cbb4fb2752a 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduceBase.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableMapReduceBase.java @@ -59,6 +59,7 @@ public abstract class TestTableMapReduceBase { withTimeout(this.getClass()).withLookingForStuckThread(true).build(); protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); protected static final TableName MULTI_REGION_TABLE_NAME = TableName.valueOf("mrtest"); + protected static final TableName TABLE_FOR_NEGATIVE_TESTS = TableName.valueOf("testfailuretable"); protected static final byte[] INPUT_FAMILY = Bytes.toBytes("contents"); protected static final byte[] OUTPUT_FAMILY = Bytes.toBytes("text"); @@ -85,10 +86,12 @@ public abstract class TestTableMapReduceBase { UTIL.createMultiRegionTable(MULTI_REGION_TABLE_NAME, new byte[][] { INPUT_FAMILY, OUTPUT_FAMILY }); UTIL.loadTable(table, INPUT_FAMILY, false); + UTIL.createTable(TABLE_FOR_NEGATIVE_TESTS, new byte[][] { INPUT_FAMILY, OUTPUT_FAMILY }); } @AfterClass public static void afterClass() throws Exception { + UTIL.deleteTable(TABLE_FOR_NEGATIVE_TESTS); UTIL.shutdownMiniCluster(); }