From 9ac498e30057de1291c3e3128bceaa1af9547c67 Mon Sep 17 00:00:00 2001 From: He Xiaoqiao Date: Wed, 1 Jul 2020 12:30:10 +0800 Subject: [PATCH] HDFS-15416. Improve DataStorage#addStorageLocations() for empty locations. Contibuted by jianghua zhu. --- .../hdfs/server/datanode/DataStorage.java | 5 ++++ .../hdfs/server/datanode/TestDataStorage.java | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataStorage.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataStorage.java index 2447fd71372..b7faecb1ad5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataStorage.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataStorage.java @@ -388,6 +388,11 @@ public class DataStorage extends Storage { try { final List successLocations = loadDataStorage( datanode, nsInfo, dataDirs, startOpt, executor); + + if (successLocations.isEmpty()) { + return Lists.newArrayList(); + } + return loadBlockPoolSliceStorage( datanode, nsInfo, successLocations, startOpt, executor); } finally { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataStorage.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataStorage.java index 6c494519672..f82462a384f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataStorage.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataStorage.java @@ -44,6 +44,7 @@ import static org.junit.Assert.fail; public class TestDataStorage { private final static String DEFAULT_BPID = "bp-0"; private final static String CLUSTER_ID = "cluster0"; + private final static String CLUSTER_ID2 = "cluster1"; private final static String BUILD_VERSION = "2.0"; private final static String SOFTWARE_VERSION = "2.0"; private final static long CTIME = 1; @@ -165,6 +166,33 @@ public class TestDataStorage { assertEquals(6, storage.getNumStorageDirs()); } + @Test + public void testAddStorageDirectoriesFailure() throws IOException { + final int numLocations = 1; + List locations = createStorageLocations(numLocations); + assertEquals(numLocations, locations.size()); + + NamespaceInfo namespaceInfo = new NamespaceInfo(0, CLUSTER_ID, + DEFAULT_BPID, CTIME, BUILD_VERSION, SOFTWARE_VERSION); + List successLocations = storage.addStorageLocations( + mockDN, namespaceInfo, locations, START_OPT); + assertEquals(1, successLocations.size()); + + // After the DataNode restarts, the value of the clusterId is different + // from the value before the restart. + storage.unlockAll(); + DataNode newMockDN = Mockito.mock(DataNode.class); + Mockito.when(newMockDN.getConf()).thenReturn(new HdfsConfiguration()); + DataStorage newStorage = new DataStorage(); + NamespaceInfo newNamespaceInfo = new NamespaceInfo(0, CLUSTER_ID2, + DEFAULT_BPID, CTIME, BUILD_VERSION, SOFTWARE_VERSION); + successLocations = newStorage.addStorageLocations( + newMockDN, newNamespaceInfo, locations, START_OPT); + assertEquals(0, successLocations.size()); + newStorage.unlockAll(); + newMockDN.shutdown(); + } + @Test public void testMissingVersion() throws IOException, URISyntaxException {