HDFS-9412. getBlocks occupies FSLock and takes too long to complete. Contributed by He Tianyi.

(cherry picked from commit 67523ffcf4)

 Conflicts:
	hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestGetBlocks.java
This commit is contained in:
Walter Su 2016-04-18 09:28:02 +08:00
parent 41cafeb5a1
commit a6d0e9e56a
2 changed files with 25 additions and 18 deletions

View File

@ -279,6 +279,14 @@ public class BlockManager implements BlockStatsMXBean {
* processed again after aquiring lock again. * processed again after aquiring lock again.
*/ */
private int numBlocksPerIteration; 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. * Progress of the Replication queues initialisation.
*/ */
@ -373,6 +381,9 @@ public class BlockManager implements BlockStatsMXBean {
this.numBlocksPerIteration = conf.getInt( this.numBlocksPerIteration = conf.getInt(
DFSConfigKeys.DFS_BLOCK_MISREPLICATION_PROCESSING_LIMIT, DFSConfigKeys.DFS_BLOCK_MISREPLICATION_PROCESSING_LIMIT,
DFSConfigKeys.DFS_BLOCK_MISREPLICATION_PROCESSING_LIMIT_DEFAULT); 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); this.blockReportLeaseManager = new BlockReportLeaseManager(conf);
bmSafeMode = new BlockManagerSafeMode(this, namesystem, haEnabled, conf); bmSafeMode = new BlockManagerSafeMode(this, namesystem, haEnabled, conf);
@ -1068,6 +1079,9 @@ public class BlockManager implements BlockStatsMXBean {
while(totalSize<size && iter.hasNext()) { while(totalSize<size && iter.hasNext()) {
curBlock = iter.next(); curBlock = iter.next();
if(!curBlock.isComplete()) continue; if(!curBlock.isComplete()) continue;
if (curBlock.getNumBytes() < getBlocksMinBlockSize) {
continue;
}
totalSize += addBlock(curBlock, results); totalSize += addBlock(curBlock, results);
} }
if(totalSize<size) { if(totalSize<size) {
@ -1075,6 +1089,9 @@ public class BlockManager implements BlockStatsMXBean {
for(int i=0; i<startBlock&&totalSize<size; i++) { for(int i=0; i<startBlock&&totalSize<size; i++) {
curBlock = iter.next(); curBlock = iter.next();
if(!curBlock.isComplete()) continue; if(!curBlock.isComplete()) continue;
if (curBlock.getNumBytes() < getBlocksMinBlockSize) {
continue;
}
totalSize += addBlock(curBlock, results); totalSize += addBlock(curBlock, results);
} }
} }

View File

@ -179,29 +179,19 @@ public class TestGetBlocks {
final short REPLICATION_FACTOR = (short) 2; final short REPLICATION_FACTOR = (short) 2;
final int DEFAULT_BLOCK_SIZE = 1024; final int DEFAULT_BLOCK_SIZE = 1024;
final Random r = new Random();
CONF.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, DEFAULT_BLOCK_SIZE); CONF.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, DEFAULT_BLOCK_SIZE);
CONF.setLong(DFSConfigKeys.DFS_BALANCER_GETBLOCKS_MIN_BLOCK_SIZE_KEY,
DEFAULT_BLOCK_SIZE);
MiniDFSCluster cluster = new MiniDFSCluster.Builder(CONF).numDataNodes( MiniDFSCluster cluster = new MiniDFSCluster.Builder(CONF).numDataNodes(
REPLICATION_FACTOR).build(); REPLICATION_FACTOR).build();
try { try {
cluster.waitActive(); cluster.waitActive();
// the third block will not be visible to getBlocks
// create a file with two blocks long fileLen = 2 * DEFAULT_BLOCK_SIZE + 1;
FileSystem fs = cluster.getFileSystem(); DFSTestUtil.createFile(cluster.getFileSystem(), new Path("/tmp.txt"),
FSDataOutputStream out = fs.create(new Path("/tmp.txt"), fileLen, REPLICATION_FACTOR, 0L);
REPLICATION_FACTOR);
byte[] data = new byte[1024];
long fileLen = 2 * DEFAULT_BLOCK_SIZE;
long bytesToWrite = fileLen;
while (bytesToWrite > 0) {
r.nextBytes(data);
int bytesToWriteNext = (1024 < bytesToWrite) ? 1024
: (int) bytesToWrite;
out.write(data, 0, bytesToWriteNext);
bytesToWrite -= bytesToWriteNext;
}
out.close();
// get blocks & data nodes // get blocks & data nodes
List<LocatedBlock> locatedBlocks; List<LocatedBlock> locatedBlocks;
@ -212,7 +202,7 @@ public class TestGetBlocks {
DFSUtilClient.getNNAddress(CONF), CONF); DFSUtilClient.getNNAddress(CONF), CONF);
locatedBlocks = dfsclient.getNamenode() locatedBlocks = dfsclient.getNamenode()
.getBlockLocations("/tmp.txt", 0, fileLen).getLocatedBlocks(); .getBlockLocations("/tmp.txt", 0, fileLen).getLocatedBlocks();
assertEquals(2, locatedBlocks.size()); assertEquals(3, locatedBlocks.size());
notWritten = false; notWritten = false;
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
dataNodes = locatedBlocks.get(i).getLocations(); dataNodes = locatedBlocks.get(i).getLocations();