1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.hdfs; 19 20 import static org.junit.Assert.assertEquals; 21 22 import java.io.IOException; 23 import java.util.ArrayList; 24 25 import org.apache.hadoop.fs.FSDataOutputStream; 26 import org.apache.hadoop.fs.FileSystem; 27 import org.apache.hadoop.fs.Path; 28 import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor; 29 import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeManager; 30 import org.junit.After; 31 import org.junit.Test; 32 33 /** 34 * This class tests DatanodeDescriptor.getBlocksScheduled() at the 35 * NameNode. This counter is supposed to keep track of blocks currently 36 * scheduled to a datanode. 37 */ 38 public class TestBlocksScheduledCounter { 39 MiniDFSCluster cluster = null; 40 FileSystem fs = null; 41 42 @After tearDown()43 public void tearDown() throws IOException { 44 if (fs != null) { 45 fs.close(); 46 } 47 if(cluster!=null){ 48 cluster.shutdown(); 49 } 50 } 51 52 @Test testBlocksScheduledCounter()53 public void testBlocksScheduledCounter() throws IOException { 54 cluster = new MiniDFSCluster.Builder(new HdfsConfiguration()).build(); 55 56 cluster.waitActive(); 57 fs = cluster.getFileSystem(); 58 59 //open a file an write a few bytes: 60 FSDataOutputStream out = fs.create(new Path("/testBlockScheduledCounter")); 61 for (int i=0; i<1024; i++) { 62 out.write(i); 63 } 64 // flush to make sure a block is allocated. 65 out.hflush(); 66 67 ArrayList<DatanodeDescriptor> dnList = new ArrayList<DatanodeDescriptor>(); 68 final DatanodeManager dm = cluster.getNamesystem().getBlockManager( 69 ).getDatanodeManager(); 70 dm.fetchDatanodes(dnList, dnList, false); 71 DatanodeDescriptor dn = dnList.get(0); 72 73 assertEquals(1, dn.getBlocksScheduled()); 74 75 // close the file and the counter should go to zero. 76 out.close(); 77 assertEquals(0, dn.getBlocksScheduled()); 78 } 79 } 80