From e4e18b17b5232816e150eb2e18159f807d80b178 Mon Sep 17 00:00:00 2001 From: Bingkun Guo Date: Fri, 26 Jun 2015 10:40:56 -0500 Subject: [PATCH] fix npe thrown from getEntry() and getStatus() SQLMetadataStorageActionHandler due to a non-exist entryId --- .../SQLMetadataStorageActionHandler.java | 34 +++++++++---------- .../SQLMetadataStorageActionHandlerTest.java | 14 ++++++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java b/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java index c375b192433..271495790c6 100644 --- a/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java +++ b/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java @@ -165,16 +165,15 @@ public class SQLMetadataStorageActionHandler withHandle(Handle handle) throws Exception { + byte[] res = handle.createQuery( + String.format("SELECT payload FROM %s WHERE id = :id", entryTable) + ) + .bind("id", entryId) + .map(ByteArrayMapper.FIRST) + .first(); + return Optional.fromNullable( - jsonMapper.readValue( - handle.createQuery( - String.format("SELECT payload FROM %s WHERE id = :id", entryTable) - ) - .bind("id", entryId) - .map(ByteArrayMapper.FIRST) - .first(), - entryType - ) + res == null ? null : jsonMapper.readValue(res, entryType) ); } } @@ -190,16 +189,15 @@ public class SQLMetadataStorageActionHandler withHandle(Handle handle) throws Exception { + byte[] res = handle.createQuery( + String.format("SELECT status_payload FROM %s WHERE id = :id", entryTable) + ) + .bind("id", entryId) + .map(ByteArrayMapper.FIRST) + .first(); + return Optional.fromNullable( - jsonMapper.readValue( - handle.createQuery( - String.format("SELECT status_payload FROM %s WHERE id = :id", entryTable) - ) - .bind("id", entryId) - .map(ByteArrayMapper.FIRST) - .first(), - statusType - ) + res == null ? null : jsonMapper.readValue(res, statusType) ); } } diff --git a/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java b/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java index 342a0a25e7a..e9f07881849 100644 --- a/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java +++ b/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java @@ -118,8 +118,12 @@ public class SQLMetadataStorageActionHandlerTest handler.getEntry(entryId) ); + Assert.assertEquals(Optional.absent(), handler.getEntry("non_exist_entry")); + Assert.assertEquals(Optional.absent(), handler.getStatus(entryId)); + Assert.assertEquals(Optional.absent(), handler.getStatus("non_exist_entry")); + Assert.assertTrue(handler.setStatus(entryId, true, status1)); Assert.assertEquals( @@ -179,6 +183,11 @@ public class SQLMetadataStorageActionHandlerTest handler.insert(entryId, new DateTime("2014-01-01"), "test", entry, true, status); + Assert.assertEquals( + ImmutableList.of(), + handler.getLogs("non_exist_entry") + ); + Assert.assertEquals( ImmutableMap.of(), handler.getLocks(entryId) @@ -206,6 +215,11 @@ public class SQLMetadataStorageActionHandlerTest handler.insert(entryId, new DateTime("2014-01-01"), "test", entry, true, status); + Assert.assertEquals( + ImmutableMap.>of(), + handler.getLocks("non_exist_entry") + ); + Assert.assertEquals( ImmutableMap.>of(), handler.getLocks(entryId)