From a6d0e9e56a7eef226c8a891cf741ab22d8205e1c Mon Sep 17 00:00:00 2001 From: Walter Su Date: Mon, 18 Apr 2016 09:28:02 +0800 Subject: [PATCH] HDFS-9412. getBlocks occupies FSLock and takes too long to complete. Contributed by He Tianyi. (cherry picked from commit 67523ffcf491f4f2db5335899c00a174d0caaa9b) Conflicts: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestGetBlocks.java --- .../server/blockmanagement/BlockManager.java | 17 ++++++++++++ .../org/apache/hadoop/hdfs/TestGetBlocks.java | 26 ++++++------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java index 4f68c28cc8b..ee9ca98d3ed 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java @@ -279,6 +279,14 @@ public int getPendingDataNodeMessageCount() { * processed again after aquiring lock again. */ private int numBlocksPerIteration; + + /** + * Minimum size that a block can be sent to Balancer through getBlocks. + * And after HDFS-8824, the small blocks are unused anyway, so there's no + * point to send them to balancer. + */ + private long getBlocksMinBlockSize = -1; + /** * Progress of the Replication queues initialisation. */ @@ -373,6 +381,9 @@ public BlockManager(final Namesystem namesystem, boolean haEnabled, this.numBlocksPerIteration = conf.getInt( DFSConfigKeys.DFS_BLOCK_MISREPLICATION_PROCESSING_LIMIT, DFSConfigKeys.DFS_BLOCK_MISREPLICATION_PROCESSING_LIMIT_DEFAULT); + this.getBlocksMinBlockSize = conf.getLongBytes( + DFSConfigKeys.DFS_BALANCER_GETBLOCKS_MIN_BLOCK_SIZE_KEY, + DFSConfigKeys.DFS_BALANCER_GETBLOCKS_MIN_BLOCK_SIZE_DEFAULT); this.blockReportLeaseManager = new BlockReportLeaseManager(conf); bmSafeMode = new BlockManagerSafeMode(this, namesystem, haEnabled, conf); @@ -1068,6 +1079,9 @@ public BlocksWithLocations getBlocksWithLocations(final DatanodeID datanode, while(totalSize 0) { - r.nextBytes(data); - int bytesToWriteNext = (1024 < bytesToWrite) ? 1024 - : (int) bytesToWrite; - out.write(data, 0, bytesToWriteNext); - bytesToWrite -= bytesToWriteNext; - } - out.close(); + // the third block will not be visible to getBlocks + long fileLen = 2 * DEFAULT_BLOCK_SIZE + 1; + DFSTestUtil.createFile(cluster.getFileSystem(), new Path("/tmp.txt"), + fileLen, REPLICATION_FACTOR, 0L); // get blocks & data nodes List locatedBlocks; @@ -212,7 +202,7 @@ public void testGetBlocks() throws Exception { DFSUtilClient.getNNAddress(CONF), CONF); locatedBlocks = dfsclient.getNamenode() .getBlockLocations("/tmp.txt", 0, fileLen).getLocatedBlocks(); - assertEquals(2, locatedBlocks.size()); + assertEquals(3, locatedBlocks.size()); notWritten = false; for (int i = 0; i < 2; i++) { dataNodes = locatedBlocks.get(i).getLocations();