Skip to content

Commit 71ecd2e

Browse files
committed
HDFS-14303. check block directory logic not correct when there is only meta file, print no meaning warn log. Contributed by qiang Liu.
1 parent f5ecc0b commit 71ecd2e

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ private void compileReport(File bpFinalizedDir, File dir,
13741374
if (!Block.isBlockFilename(file)) {
13751375
if (isBlockMetaFile(Block.BLOCK_FILE_PREFIX, file.getName())) {
13761376
long blockId = Block.getBlockId(file.getName());
1377-
verifyFileLocation(file.getParentFile(), bpFinalizedDir,
1377+
verifyFileLocation(file, bpFinalizedDir,
13781378
blockId);
13791379
report.add(new ScanInfo(blockId, null, file, this));
13801380
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.Assert.assertNull;
2727
import static org.junit.Assert.assertTrue;
2828

29+
import java.io.ByteArrayOutputStream;
2930
import java.io.File;
3031
import java.io.FileOutputStream;
3132
import java.io.IOException;
@@ -72,6 +73,9 @@
7273
import org.apache.hadoop.test.GenericTestUtils;
7374
import org.apache.hadoop.util.AutoCloseableLock;
7475
import org.apache.hadoop.util.Time;
76+
import org.apache.log4j.Level;
77+
import org.apache.log4j.SimpleLayout;
78+
import org.apache.log4j.WriterAppender;
7579
import org.junit.Before;
7680
import org.junit.Test;
7781
import org.mockito.Mockito;
@@ -394,6 +398,70 @@ public void testRetainBlockOnPersistentStorage() throws Exception {
394398
}
395399
}
396400

401+
/**
402+
* test scan only meta file NOT generate wrong folder structure warn log.
403+
*/
404+
@Test(timeout=600000)
405+
public void testScanDirectoryStructureWarn() throws Exception {
406+
407+
//add a logger stream to check what has printed to log
408+
ByteArrayOutputStream loggerStream = new ByteArrayOutputStream();
409+
org.apache.log4j.Logger rootLogger =
410+
org.apache.log4j.Logger.getRootLogger();
411+
rootLogger.setLevel(Level.INFO);
412+
WriterAppender writerAppender =
413+
new WriterAppender(new SimpleLayout(), loggerStream);
414+
rootLogger.addAppender(writerAppender);
415+
416+
cluster = new MiniDFSCluster
417+
.Builder(CONF)
418+
.storageTypes(new StorageType[] {
419+
StorageType.RAM_DISK, StorageType.DEFAULT })
420+
.numDataNodes(1)
421+
.build();
422+
try {
423+
cluster.waitActive();
424+
bpid = cluster.getNamesystem().getBlockPoolId();
425+
fds = DataNodeTestUtils.getFSDataset(cluster.getDataNodes().get(0));
426+
client = cluster.getFileSystem().getClient();
427+
scanner = new DirectoryScanner(fds, CONF);
428+
scanner.setRetainDiffs(true);
429+
FsDatasetTestUtil.stopLazyWriter(cluster.getDataNodes().get(0));
430+
431+
// Create a file file on RAM_DISK
432+
createFile(GenericTestUtils.getMethodName(), BLOCK_LENGTH, true);
433+
434+
// Ensure no difference between volumeMap and disk.
435+
scan(1, 0, 0, 0, 0, 0);
436+
437+
//delete thre block file , left the meta file alone
438+
deleteBlockFile();
439+
440+
//scan to ensure log warn not printed
441+
scan(1, 1, 0, 1, 0, 0, 0);
442+
443+
//ensure the warn log not appear and missing block log do appear
444+
String logContent = new String(loggerStream.toByteArray());
445+
String missingBlockWarn = "Deleted a metadata file" +
446+
" for the deleted block";
447+
String dirStructureWarnLog = " found in invalid directory." +
448+
" Expected directory: ";
449+
assertFalse("directory check print meaningless warning message",
450+
logContent.contains(dirStructureWarnLog));
451+
assertTrue("missing block warn log not appear",
452+
logContent.contains(missingBlockWarn));
453+
LOG.info("check pass");
454+
455+
} finally {
456+
if (scanner != null) {
457+
scanner.shutdown();
458+
scanner = null;
459+
}
460+
cluster.shutdown();
461+
cluster = null;
462+
}
463+
}
464+
397465
@Test(timeout = 300000)
398466
public void testDeleteBlockOnTransientStorage() throws Exception {
399467
cluster = new MiniDFSCluster.Builder(CONF)

0 commit comments

Comments
 (0)