HADOOP-4483 Honor the max parameter in DatanodeDescriptor.getBlockArray(...). (Ahad Rana and Hairong Kuang via szetszwo)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.18@709025 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 49ae60f..f75ff58 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -50,6 +50,9 @@
0.18.2 incompatibility problem and fixed the balancing semaphore
contention problem without changing the datanode protocol. (hairong)
+ HADOOP-4483 Honor the max parameter in DatanodeDescriptor.getBlockArray(..)
+ (Ahad Rana and Hairong Kuang via szetszwo)
+
NEW FEATURES
HADOOP-2421. Add jdiff output to documentation, listing all API
diff --git a/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java b/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java
index 208b220..df68314 100644
--- a/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java
+++ b/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java
@@ -339,14 +339,36 @@
static private Block[] getBlockArray(Collection<Block> blocks, int max) {
Block[] blockarray = null;
synchronized(blocks) {
- int n = blocks.size();
+ int available = blocks.size();
+ int n = available;
if (max > 0 && n > 0) {
if (max < n) {
n = max;
}
- blockarray = blocks.toArray(new Block[n]);
- blocks.clear();
- assert(blockarray.length > 0);
+ // allocate the properly sized block array ...
+ blockarray = new Block[n];
+
+ // iterate tree collecting n blocks...
+ Iterator<Block> e = blocks.iterator();
+ int blockCount = 0;
+
+ while (blockCount < n && e.hasNext()) {
+ // insert into array ...
+ blockarray[blockCount++] = e.next();
+
+ // remove from tree via iterator, if we are removing
+ // less than total available blocks
+ if (n < available){
+ e.remove();
+ }
+ }
+ assert(blockarray.length == n);
+
+ // now if the number of blocks removed equals available blocks,
+ // them remove all blocks in one fell swoop via clear
+ if (n == available) {
+ blocks.clear();
+ }
}
}
return blockarray;
diff --git a/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java b/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java
new file mode 100644
index 0000000..a08e4dc
--- /dev/null
+++ b/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.dfs;
+
+import java.util.ArrayList;
+
+import org.apache.hadoop.dfs.Block;
+import org.apache.hadoop.dfs.GenerationStamp;
+import org.apache.hadoop.dfs.BlockCommand;
+
+import junit.framework.TestCase;
+
+/**
+ * This class tests that methods in DatanodeDescriptor
+ */
+public class TestDatanodeDescriptor extends TestCase {
+ /**
+ * Test that getInvalidateBlocks observes the maxlimit.
+ */
+ public void testGetInvalidateBlocks() throws Exception {
+ final int MAX_BLOCKS = 10;
+ final int REMAINING_BLOCKS = 2;
+ final int MAX_LIMIT = MAX_BLOCKS - REMAINING_BLOCKS;
+
+ DatanodeDescriptor dd = new DatanodeDescriptor();
+ ArrayList<Block> blockList = new ArrayList<Block>(MAX_BLOCKS);
+ for (int i=0; i<MAX_BLOCKS; i++) {
+ blockList.add(new Block(i, 0, GenerationStamp.FIRST_VALID_STAMP));
+ }
+ dd.addBlocksToBeInvalidated(blockList);
+ BlockCommand bc = dd.getInvalidateBlocks(MAX_LIMIT);
+ assertEquals(bc.getBlocks().length, MAX_LIMIT);
+ bc = dd.getInvalidateBlocks(MAX_LIMIT);
+ assertEquals(bc.getBlocks().length, REMAINING_BLOCKS);
+ }
+}