Skip to content

Commit 01717a2

Browse files
zhujianghuazhujianghua
authored andcommitted
HDFS-15416. DataStorage#addStorageLocations() should add more reasonable information verification.
1 parent 6dfe00c commit 01717a2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ synchronized List<StorageDirectory> addStorageLocations(DataNode datanode,
388388
try {
389389
final List<StorageLocation> successLocations = loadDataStorage(
390390
datanode, nsInfo, dataDirs, startOpt, executor);
391+
392+
if (successLocations.isEmpty()) {
393+
return Lists.newArrayList();
394+
}
395+
391396
return loadBlockPoolSliceStorage(
392397
datanode, nsInfo, successLocations, startOpt, executor);
393398
} finally {

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataStorage.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
public class TestDataStorage {
4545
private final static String DEFAULT_BPID = "bp-0";
4646
private final static String CLUSTER_ID = "cluster0";
47+
private final static String CLUSTER_ID2 = "cluster1";
4748
private final static String BUILD_VERSION = "2.0";
4849
private final static String SOFTWARE_VERSION = "2.0";
4950
private final static long CTIME = 1;
@@ -165,6 +166,35 @@ public void testAddStorageDirectories() throws IOException,
165166
assertEquals(6, storage.getNumStorageDirs());
166167
}
167168

169+
@Test
170+
public void testAddStorageDirectoriesFailure() throws IOException {
171+
final int numLocations = 1;
172+
List<StorageLocation> locations = createStorageLocations(numLocations);
173+
assertEquals(numLocations, locations.size());
174+
175+
NamespaceInfo namespaceInfo = new NamespaceInfo(0, CLUSTER_ID,
176+
DEFAULT_BPID, CTIME, BUILD_VERSION, SOFTWARE_VERSION);
177+
List<StorageDirectory> successLocations = storage.addStorageLocations(
178+
mockDN, namespaceInfo, locations, START_OPT);
179+
assertEquals(1, successLocations.size());
180+
181+
/**
182+
* After the DataNode restarts, the value of the clusterId is different
183+
* from the value before the restart.
184+
*/
185+
storage.unlockAll();
186+
DataNode newMockDN = Mockito.mock(DataNode.class);
187+
Mockito.when(newMockDN.getConf()).thenReturn(new HdfsConfiguration());
188+
DataStorage newStorage = new DataStorage();
189+
NamespaceInfo newNamespaceInfo = new NamespaceInfo(0, CLUSTER_ID2,
190+
DEFAULT_BPID, CTIME, BUILD_VERSION, SOFTWARE_VERSION);
191+
successLocations = newStorage.addStorageLocations(
192+
newMockDN, newNamespaceInfo, locations, START_OPT);
193+
assertEquals(0, successLocations.size());
194+
newStorage.unlockAll();
195+
newMockDN.shutdown();
196+
}
197+
168198
@Test
169199
public void testMissingVersion() throws IOException,
170200
URISyntaxException {

0 commit comments

Comments
 (0)