diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/InMemoryAliasMapProtocolClientSideTranslatorPB.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/InMemoryAliasMapProtocolClientSideTranslatorPB.java index 2025c16d1c..d9e984b45c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/InMemoryAliasMapProtocolClientSideTranslatorPB.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/InMemoryAliasMapProtocolClientSideTranslatorPB.java @@ -167,6 +167,9 @@ public InMemoryAliasMap.IterationResult list(Optional marker) public Optional read(@Nonnull Block block) throws IOException { + if (block == null) { + throw new IOException("Block cannot be null"); + } ReadRequestProto request = ReadRequestProto .newBuilder() @@ -191,6 +194,9 @@ public Optional read(@Nonnull Block block) public void write(@Nonnull Block block, @Nonnull ProvidedStorageLocation providedStorageLocation) throws IOException { + if (block == null || providedStorageLocation == null) { + throw new IOException("Provided block and location cannot be null"); + } WriteRequestProto request = WriteRequestProto .newBuilder() diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/aliasmap/InMemoryLevelDBAliasMapServer.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/aliasmap/InMemoryLevelDBAliasMapServer.java index f201bfd784..5c56736be4 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/aliasmap/InMemoryLevelDBAliasMapServer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/aliasmap/InMemoryLevelDBAliasMapServer.java @@ -150,11 +150,15 @@ public Configuration getConf() { public void close() { LOG.info("Stopping InMemoryLevelDBAliasMapServer"); try { - aliasMap.close(); + if (aliasMap != null) { + aliasMap.close(); + } } catch (IOException e) { LOG.error(e.getMessage()); } - aliasMapServer.stop(); + if (aliasMapServer != null) { + aliasMapServer.stop(); + } } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/common/blockaliasmap/impl/TestInMemoryLevelDBAliasMapClient.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/common/blockaliasmap/impl/TestInMemoryLevelDBAliasMapClient.java index f0626335bb..fccb6f2ab3 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/common/blockaliasmap/impl/TestInMemoryLevelDBAliasMapClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/common/blockaliasmap/impl/TestInMemoryLevelDBAliasMapClient.java @@ -28,14 +28,19 @@ import org.apache.hadoop.hdfs.server.aliasmap.InMemoryLevelDBAliasMapServer; import org.apache.hadoop.hdfs.server.common.blockaliasmap.BlockAliasMap; import org.apache.hadoop.hdfs.server.common.FileRegion; +import org.apache.hadoop.test.LambdaTestUtils; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_BIND_HOST_KEY; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -59,6 +64,9 @@ public class TestInMemoryLevelDBAliasMapClient { private Configuration conf; private final static String BPID = "BPID-0"; + @Rule + public final ExpectedException exception = ExpectedException.none(); + @Before public void setUp() throws IOException { conf = new Configuration(); @@ -348,4 +356,35 @@ public void testServerBindHost() throws Exception { conf.set(DFS_NAMENODE_SERVICE_RPC_BIND_HOST_KEY, "0.0.0.0"); writeRead(); } + + @Test + public void testNonExistentFile() throws Exception { + // delete alias map location + FileUtils.deleteDirectory(tempDir); + // expect a RuntimeException when the aliasmap is started. + exception.expect(RuntimeException.class); + levelDBAliasMapServer.setConf(conf); + } + + @Test + public void testNonExistentBlock() throws Exception { + inMemoryLevelDBAliasMapClient.setConf(conf); + levelDBAliasMapServer.setConf(conf); + levelDBAliasMapServer.start(); + Block block1 = new Block(100, 43, 44); + ProvidedStorageLocation providedStorageLocation1 = null; + BlockAliasMap.Writer writer1 = + inMemoryLevelDBAliasMapClient.getWriter(null, BPID); + try { + writer1.store(new FileRegion(block1, providedStorageLocation1)); + fail("Should fail on writing a region with null ProvidedLocation"); + } catch (IOException | IllegalArgumentException e) { + assertTrue(e.getMessage().contains("not be null")); + } + + BlockAliasMap.Reader reader = + inMemoryLevelDBAliasMapClient.getReader(null, BPID); + LambdaTestUtils.assertOptionalUnset("Expected empty BlockAlias", + reader.resolve(block1)); + } } \ No newline at end of file