From 49c000638c3405b0ac956ecec4881fd86a01529a Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Fri, 27 Jul 2018 13:20:15 -0400 Subject: [PATCH 1/3] HbaseClient.mutate should return the number of mutations --- .../java/org/apache/metron/hbase/client/HBaseClient.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/metron-platform/metron-hbase/src/main/java/org/apache/metron/hbase/client/HBaseClient.java b/metron-platform/metron-hbase/src/main/java/org/apache/metron/hbase/client/HBaseClient.java index c027c5c6f9..f0a0a106f8 100644 --- a/metron-platform/metron-hbase/src/main/java/org/apache/metron/hbase/client/HBaseClient.java +++ b/metron-platform/metron-hbase/src/main/java/org/apache/metron/hbase/client/HBaseClient.java @@ -133,9 +133,11 @@ public void clearMutations() { /** * Submits all queued Mutations. + * @return The number of mutation submitted. */ - public void mutate() { - Object[] result = new Object[mutations.size()]; + public int mutate() { + int mutationCount = mutations.size(); + Object[] result = new Object[mutationCount]; try { table.batch(mutations, result); mutations.clear(); @@ -144,6 +146,8 @@ public void mutate() { LOG.warn("Error performing a mutation to HBase.", e); throw new RuntimeException(e); } + + return mutationCount; } /** From 2c7fbc01b5016d758a848deef791043a64facfff Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Mon, 20 Aug 2018 09:55:47 -0400 Subject: [PATCH 2/3] Added unit tests based on feedback from @merrimanr --- .../metron/hbase/client/HBaseClientTest.java | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java b/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java index 1849745f7d..29a0a552b2 100644 --- a/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java +++ b/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java @@ -85,8 +85,6 @@ public static void startHBase() throws Exception { // create the table table = util.createTable(Bytes.toBytes(tableName), WidgetMapper.CF); util.waitTableEnabled(table.getName()); - // setup the client - client = new HBaseClient((c,t) -> table, table.getConfiguration(), tableName); } @AfterClass @@ -108,6 +106,9 @@ public void clearTable() throws Exception { @Before public void setupTuples() throws Exception { + // setup the client + client = new HBaseClient((c,t) -> table, table.getConfiguration(), tableName); + // create a mapper mapper = new WidgetMapper(); @@ -160,7 +161,10 @@ public void testBatchWrite() throws Exception { // add two mutations to the queue client.addMutation(rowKey1, cols1, Durability.SYNC_WAL); client.addMutation(rowKey2, cols2, Durability.SYNC_WAL); - client.mutate(); + int count = client.mutate(); + + // there were two mutations + Assert.assertEquals(2, count); HBaseProjectionCriteria criteria = new HBaseProjectionCriteria(); criteria.addColumnFamily(WidgetMapper.CF_STRING); @@ -179,6 +183,31 @@ public void testBatchWrite() throws Exception { } } + /** + * What happens when there is nothing in the batch to write? + */ + @Test + public void testEmptyBatch() throws Exception { + + // do not add any mutations before attempting to write + int count = client.mutate(); + Assert.assertEquals(0, count); + + HBaseProjectionCriteria criteria = new HBaseProjectionCriteria(); + criteria.addColumnFamily(WidgetMapper.CF_STRING); + + // read back both + client.addGet(rowKey1, criteria); + client.addGet(rowKey2, criteria); + Result[] results = client.getAll(); + + // validate - there should be nothing to find + assertEquals(2, results.length); + for(Result result : results) { + Assert.assertTrue(result.isEmpty()); + } + } + /** * Should be able to read back widgets that were written with a TTL 30 days out. */ From 19e603845beb429602a5be2815b7473d55893071 Mon Sep 17 00:00:00 2001 From: Nick Allen Date: Mon, 20 Aug 2018 09:59:54 -0400 Subject: [PATCH 3/3] No need to change this --- .../java/org/apache/metron/hbase/client/HBaseClientTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java b/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java index 29a0a552b2..ca1f5a686e 100644 --- a/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java +++ b/metron-platform/metron-hbase/src/test/java/org/apache/metron/hbase/client/HBaseClientTest.java @@ -85,6 +85,8 @@ public static void startHBase() throws Exception { // create the table table = util.createTable(Bytes.toBytes(tableName), WidgetMapper.CF); util.waitTableEnabled(table.getName()); + // setup the client + client = new HBaseClient((c,t) -> table, table.getConfiguration(), tableName); } @AfterClass @@ -106,9 +108,6 @@ public void clearTable() throws Exception { @Before public void setupTuples() throws Exception { - // setup the client - client = new HBaseClient((c,t) -> table, table.getConfiguration(), tableName); - // create a mapper mapper = new WidgetMapper();