Skip to content

Commit f43a152

Browse files
committed
HDFS-15369. Refactor method VolumeScanner#runLoop(). Contributed by Yang Yun.
1 parent f4901d0 commit f43a152

File tree

1 file changed

+47
-34
lines changed
  • hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode

1 file changed

+47
-34
lines changed

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

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,50 @@ static boolean calculateShouldScan(String storageId, long targetBytesPerSec,
483483
return shouldScan;
484484
}
485485

486+
/**
487+
* Get next block and check if it's needed to scan.
488+
*
489+
* @return the candidate block.
490+
*/
491+
ExtendedBlock getNextBlockToScan() {
492+
ExtendedBlock block;
493+
try {
494+
block = curBlockIter.nextBlock();
495+
} catch (IOException e) {
496+
// There was an error listing the next block in the volume. This is a
497+
// serious issue.
498+
LOG.warn("{}: nextBlock error on {}", this, curBlockIter);
499+
// On the next loop iteration, curBlockIter#eof will be set to true, and
500+
// we will pick a different block iterator.
501+
return null;
502+
}
503+
if (block == null) {
504+
// The BlockIterator is at EOF.
505+
LOG.info("{}: finished scanning block pool {}",
506+
this, curBlockIter.getBlockPoolId());
507+
saveBlockIterator(curBlockIter);
508+
return null;
509+
} else if (conf.skipRecentAccessed) {
510+
// Check the access time of block file to avoid scanning recently
511+
// changed blocks, reducing disk IO.
512+
try {
513+
BlockLocalPathInfo blockLocalPathInfo =
514+
volume.getDataset().getBlockLocalPathInfo(block);
515+
BasicFileAttributes attr = Files.readAttributes(
516+
new File(blockLocalPathInfo.getBlockPath()).toPath(),
517+
BasicFileAttributes.class);
518+
if (System.currentTimeMillis() - attr.lastAccessTime().
519+
to(TimeUnit.MILLISECONDS) < conf.scanPeriodMs) {
520+
return null;
521+
}
522+
} catch (IOException ioe) {
523+
LOG.debug("Failed to get access time of block {}",
524+
block, ioe);
525+
}
526+
}
527+
return block;
528+
}
529+
486530
/**
487531
* Run an iteration of the VolumeScanner loop.
488532
*
@@ -507,10 +551,10 @@ private long runLoop(ExtendedBlock suspectBlock) {
507551
return 30000L;
508552
}
509553

510-
// Find a usable block pool to scan.
511554
if (suspectBlock != null) {
512555
block = suspectBlock;
513556
} else {
557+
// Find a usable block pool to scan.
514558
if ((curBlockIter == null) || curBlockIter.atEnd()) {
515559
long timeout = findNextUsableBlockIter();
516560
if (timeout > 0) {
@@ -528,40 +572,9 @@ private long runLoop(ExtendedBlock suspectBlock) {
528572
}
529573
return 0L;
530574
}
531-
try {
532-
block = curBlockIter.nextBlock();
533-
} catch (IOException e) {
534-
// There was an error listing the next block in the volume. This is a
535-
// serious issue.
536-
LOG.warn("{}: nextBlock error on {}", this, curBlockIter);
537-
// On the next loop iteration, curBlockIter#eof will be set to true, and
538-
// we will pick a different block iterator.
539-
return 0L;
540-
}
575+
block = getNextBlockToScan();
541576
if (block == null) {
542-
// The BlockIterator is at EOF.
543-
LOG.info("{}: finished scanning block pool {}",
544-
this, curBlockIter.getBlockPoolId());
545-
saveBlockIterator(curBlockIter);
546-
return 0;
547-
} else if (conf.skipRecentAccessed) {
548-
// Check the access time of block file to avoid scanning recently
549-
// changed blocks, reducing disk IO.
550-
try {
551-
BlockLocalPathInfo blockLocalPathInfo =
552-
volume.getDataset().getBlockLocalPathInfo(block);
553-
BasicFileAttributes attr = Files.readAttributes(
554-
new File(blockLocalPathInfo.getBlockPath()).toPath(),
555-
BasicFileAttributes.class);
556-
if (System.currentTimeMillis() - attr.lastAccessTime().
557-
to(TimeUnit.MILLISECONDS) < conf.scanPeriodMs) {
558-
return 0;
559-
}
560-
561-
} catch (IOException ioe) {
562-
LOG.debug("Failed to get access time of block {}",
563-
block, ioe);
564-
}
577+
return 0L;
565578
}
566579
}
567580
if (curBlockIter != null) {

0 commit comments

Comments
 (0)