diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestDatanodeManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestDatanodeManager.java new file mode 100644 index 00000000000..75034932acd --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestDatanodeManager.java @@ -0,0 +1,148 @@ +/** + * 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.hdfs.server.blockmanagement; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; +import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration; +import org.junit.Test; +import org.mockito.Mockito; +import org.mortbay.log.Log; + +import static org.junit.Assert.*; + +public class TestDatanodeManager { + + //The number of times the registration / removal of nodes should happen + final int NUM_ITERATIONS = 500; + + /** + * This test sends a random sequence of node registrations and node removals + * to the DatanodeManager (of nodes with different IDs and versions), and + * checks that the DatanodeManager keeps a correct count of different software + * versions at all times. + */ + @Test + public void testNumVersionsReportedCorrect() throws IOException { + //Create the DatanodeManager which will be tested + FSNamesystem fsn = Mockito.mock(FSNamesystem.class); + Mockito.when(fsn.hasWriteLock()).thenReturn(true); + DatanodeManager dm = new DatanodeManager(Mockito.mock(BlockManager.class), + fsn, new Configuration()); + + //Seed the RNG with a known value so test failures are easier to reproduce + Random rng = new Random(); + int seed = rng.nextInt(); + rng = new Random(seed); + Log.info("Using seed " + seed + " for testing"); + + //A map of the Storage IDs to the DN registration it was registered with + HashMap sIdToDnReg = + new HashMap(); + + for(int i=0; i> it = + sIdToDnReg.entrySet().iterator(); + for(int j=0; j mapToCheck = dm.getDatanodesSoftwareVersions(); + + //Remove counts from versions and make sure that after removing all nodes + //mapToCheck is empty + for(Entry it: sIdToDnReg.entrySet()) { + String ver = it.getValue().getSoftwareVersion(); + if(!mapToCheck.containsKey(ver)) { + throw new AssertionError("The correct number of datanodes of a " + + "version was not found on iteration " + i); + } + mapToCheck.put(ver, mapToCheck.get(ver) - 1); + if(mapToCheck.get(ver) == 0) { + mapToCheck.remove(ver); + } + } + for(Entry entry: mapToCheck.entrySet()) { + Log.info("Still in map: " + entry.getKey() + " has " + + entry.getValue()); + } + assertEquals("The map of version counts returned by DatanodeManager was" + + " not what it was expected to be on iteration " + i, 0, + mapToCheck.size()); + } + } + +}