浏览代码

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
Tsz-wo Sze 16 年之前
父节点
当前提交
74aa99d474

+ 3 - 0
CHANGES.txt

@@ -50,6 +50,9 @@ Release 0.18.2 - Unreleased
     0.18.2 incompatibility problem and fixed the balancing semaphore 
     0.18.2 incompatibility problem and fixed the balancing semaphore 
     contention problem without changing the datanode protocol. (hairong)
     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
   NEW FEATURES
 
 
     HADOOP-2421.  Add jdiff output to documentation, listing all API
     HADOOP-2421.  Add jdiff output to documentation, listing all API

+ 26 - 4
src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java

@@ -339,14 +339,36 @@ public class DatanodeDescriptor extends DatanodeInfo {
   static private Block[] getBlockArray(Collection<Block> blocks, int max) {
   static private Block[] getBlockArray(Collection<Block> blocks, int max) {
     Block[] blockarray = null;
     Block[] blockarray = null;
     synchronized(blocks) {
     synchronized(blocks) {
-      int n = blocks.size();
+      int available = blocks.size();
+      int n = available;
       if (max > 0 && n > 0) {
       if (max > 0 && n > 0) {
         if (max < n) {
         if (max < n) {
           n = max;
           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;
     return blockarray;

+ 51 - 0
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);
+  }
+}