From 718c8a5868680115c0f5f8253a8c9f72e0069082 Mon Sep 17 00:00:00 2001 From: Ayush Saxena Date: Sun, 24 May 2020 18:03:41 +0530 Subject: [PATCH] HDFS-15369. Refactor method VolumeScanner#runLoop(). Contributed by Yang Yun. --- .../hdfs/server/datanode/VolumeScanner.java | 81 +++++++++++-------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/VolumeScanner.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/VolumeScanner.java index 5f1a1e0248..5e3d523cde 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/VolumeScanner.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/VolumeScanner.java @@ -483,6 +483,50 @@ static boolean calculateShouldScan(String storageId, long targetBytesPerSec, return shouldScan; } + /** + * Get next block and check if it's needed to scan. + * + * @return the candidate block. + */ + ExtendedBlock getNextBlockToScan() { + ExtendedBlock block; + try { + block = curBlockIter.nextBlock(); + } catch (IOException e) { + // There was an error listing the next block in the volume. This is a + // serious issue. + LOG.warn("{}: nextBlock error on {}", this, curBlockIter); + // On the next loop iteration, curBlockIter#eof will be set to true, and + // we will pick a different block iterator. + return null; + } + if (block == null) { + // The BlockIterator is at EOF. + LOG.info("{}: finished scanning block pool {}", + this, curBlockIter.getBlockPoolId()); + saveBlockIterator(curBlockIter); + return null; + } else if (conf.skipRecentAccessed) { + // Check the access time of block file to avoid scanning recently + // changed blocks, reducing disk IO. + try { + BlockLocalPathInfo blockLocalPathInfo = + volume.getDataset().getBlockLocalPathInfo(block); + BasicFileAttributes attr = Files.readAttributes( + new File(blockLocalPathInfo.getBlockPath()).toPath(), + BasicFileAttributes.class); + if (System.currentTimeMillis() - attr.lastAccessTime(). + to(TimeUnit.MILLISECONDS) < conf.scanPeriodMs) { + return null; + } + } catch (IOException ioe) { + LOG.debug("Failed to get access time of block {}", + block, ioe); + } + } + return block; + } + /** * Run an iteration of the VolumeScanner loop. * @@ -507,10 +551,10 @@ private long runLoop(ExtendedBlock suspectBlock) { return 30000L; } - // Find a usable block pool to scan. if (suspectBlock != null) { block = suspectBlock; } else { + // Find a usable block pool to scan. if ((curBlockIter == null) || curBlockIter.atEnd()) { long timeout = findNextUsableBlockIter(); if (timeout > 0) { @@ -528,40 +572,9 @@ private long runLoop(ExtendedBlock suspectBlock) { } return 0L; } - try { - block = curBlockIter.nextBlock(); - } catch (IOException e) { - // There was an error listing the next block in the volume. This is a - // serious issue. - LOG.warn("{}: nextBlock error on {}", this, curBlockIter); - // On the next loop iteration, curBlockIter#eof will be set to true, and - // we will pick a different block iterator. - return 0L; - } + block = getNextBlockToScan(); if (block == null) { - // The BlockIterator is at EOF. - LOG.info("{}: finished scanning block pool {}", - this, curBlockIter.getBlockPoolId()); - saveBlockIterator(curBlockIter); - return 0; - } else if (conf.skipRecentAccessed) { - // Check the access time of block file to avoid scanning recently - // changed blocks, reducing disk IO. - try { - BlockLocalPathInfo blockLocalPathInfo = - volume.getDataset().getBlockLocalPathInfo(block); - BasicFileAttributes attr = Files.readAttributes( - new File(blockLocalPathInfo.getBlockPath()).toPath(), - BasicFileAttributes.class); - if (System.currentTimeMillis() - attr.lastAccessTime(). - to(TimeUnit.MILLISECONDS) < conf.scanPeriodMs) { - return 0; - } - - } catch (IOException ioe) { - LOG.debug("Failed to get access time of block {}", - block, ioe); - } + return 0L; } } if (curBlockIter != null) {