You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by sz...@apache.org on 2008/10/29 22:56:14 UTC

svn commit: r709025 - in /hadoop/core/branches/branch-0.18: CHANGES.txt src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java

Author: szetszwo
Date: Wed Oct 29 14:56:13 2008
New Revision: 709025

URL: http://svn.apache.org/viewvc?rev=709025&view=rev
Log:
HADOOP-4483 Honor the max parameter in DatanodeDescriptor.getBlockArray(...).  (Ahad Rana and Hairong Kuang via szetszwo)

Added:
    hadoop/core/branches/branch-0.18/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java
Modified:
    hadoop/core/branches/branch-0.18/CHANGES.txt
    hadoop/core/branches/branch-0.18/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java

Modified: hadoop/core/branches/branch-0.18/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.18/CHANGES.txt?rev=709025&r1=709024&r2=709025&view=diff
==============================================================================
--- hadoop/core/branches/branch-0.18/CHANGES.txt (original)
+++ hadoop/core/branches/branch-0.18/CHANGES.txt Wed Oct 29 14:56:13 2008
@@ -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

Modified: hadoop/core/branches/branch-0.18/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java
URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.18/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java?rev=709025&r1=709024&r2=709025&view=diff
==============================================================================
--- hadoop/core/branches/branch-0.18/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java (original)
+++ hadoop/core/branches/branch-0.18/src/hdfs/org/apache/hadoop/dfs/DatanodeDescriptor.java Wed Oct 29 14:56:13 2008
@@ -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;

Added: hadoop/core/branches/branch-0.18/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java
URL: http://svn.apache.org/viewvc/hadoop/core/branches/branch-0.18/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java?rev=709025&view=auto
==============================================================================
--- hadoop/core/branches/branch-0.18/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java (added)
+++ hadoop/core/branches/branch-0.18/src/test/org/apache/hadoop/dfs/TestDatanodeDescriptor.java Wed Oct 29 14:56:13 2008
@@ -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);
+  }
+}