HDFS-13795. Fix potential NPE in InMemoryLevelDBAliasMapServer.

This commit is contained in:
Virajith Jalaparti 2018-08-10 09:38:40 -07:00
parent 0a71bf1452
commit 15241c6349
3 changed files with 51 additions and 2 deletions

View File

@ -167,6 +167,9 @@ public InMemoryAliasMap.IterationResult list(Optional<Block> marker)
public Optional<ProvidedStorageLocation> 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<ProvidedStorageLocation> 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()

View File

@ -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();
}
}
}

View File

@ -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<FileRegion> 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<FileRegion> reader =
inMemoryLevelDBAliasMapClient.getReader(null, BPID);
LambdaTestUtils.assertOptionalUnset("Expected empty BlockAlias",
reader.resolve(block1));
}
}