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.server.namenode; 19 20 import static org.junit.Assert.*; 21 22 import java.io.File; 23 import java.lang.management.ManagementFactory; 24 import java.util.Collection; 25 import java.util.Map; 26 27 import javax.management.MBeanServer; 28 import javax.management.ObjectName; 29 30 import org.apache.hadoop.conf.Configuration; 31 import org.apache.hadoop.fs.FileUtil; 32 import org.apache.hadoop.hdfs.MiniDFSCluster; 33 import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; 34 import org.apache.hadoop.util.VersionInfo; 35 36 import org.junit.Test; 37 import org.mortbay.util.ajax.JSON; 38 39 import junit.framework.Assert; 40 41 /** 42 * Class for testing {@link NameNodeMXBean} implementation 43 */ 44 public class TestNameNodeMXBean { 45 @SuppressWarnings({ "unchecked", "deprecation" }) 46 @Test testNameNodeMXBeanInfo()47 public void testNameNodeMXBeanInfo() throws Exception { 48 Configuration conf = new Configuration(); 49 MiniDFSCluster cluster = null; 50 51 try { 52 cluster = new MiniDFSCluster(conf, 1, true, null); 53 cluster.waitActive(); 54 55 FSNamesystem fsn = cluster.getNameNode().getNamesystem(); 56 57 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 58 ObjectName mxbeanName = new ObjectName( 59 "Hadoop:service=NameNode,name=NameNodeInfo"); 60 // get attribute "HostName" 61 String hostname = (String) mbs.getAttribute(mxbeanName, "HostName"); 62 Assert.assertEquals(fsn.getHostName(), hostname); 63 // get attribute "Version" 64 String version = (String) mbs.getAttribute(mxbeanName, "Version"); 65 Assert.assertEquals(fsn.getVersion(), version); 66 Assert.assertTrue(version.equals(VersionInfo.getVersion() 67 + ", r" + VersionInfo.getRevision())); 68 // get attribute "Used" 69 Long used = (Long) mbs.getAttribute(mxbeanName, "Used"); 70 Assert.assertEquals(fsn.getUsed(), used.longValue()); 71 // get attribute "Total" 72 Long total = (Long) mbs.getAttribute(mxbeanName, "Total"); 73 Assert.assertEquals(fsn.getTotal(), total.longValue()); 74 // get attribute "safemode" 75 String safemode = (String) mbs.getAttribute(mxbeanName, "Safemode"); 76 Assert.assertEquals(fsn.getSafemode(), safemode); 77 // get attribute nondfs 78 Long nondfs = (Long) (mbs.getAttribute(mxbeanName, "NonDfsUsedSpace")); 79 Assert.assertEquals(fsn.getNonDfsUsedSpace(), nondfs.longValue()); 80 // get attribute percentremaining 81 Float percentremaining = (Float) (mbs.getAttribute(mxbeanName, 82 "PercentRemaining")); 83 Assert.assertEquals(fsn.getPercentRemaining(), percentremaining 84 .floatValue()); 85 // get attribute Totalblocks 86 Long totalblocks = (Long) (mbs.getAttribute(mxbeanName, "TotalBlocks")); 87 Assert.assertEquals(fsn.getTotalBlocks(), totalblocks.longValue()); 88 // get attribute alivenodeinfo 89 String alivenodeinfo = (String) (mbs.getAttribute(mxbeanName, 90 "LiveNodes")); 91 Assert.assertEquals(fsn.getLiveNodes(), alivenodeinfo); 92 // get attribute deadnodeinfo 93 String deadnodeinfo = (String) (mbs.getAttribute(mxbeanName, 94 "DeadNodes")); 95 Assert.assertEquals(fsn.getDeadNodes(), deadnodeinfo); 96 // get attribute NameDirStatuses 97 String nameDirStatuses = (String) (mbs.getAttribute(mxbeanName, 98 "NameDirStatuses")); 99 Assert.assertEquals(fsn.getNameDirStatuses(), nameDirStatuses); 100 Map<String, Map<String, String>> statusMap = 101 (Map<String, Map<String, String>>) JSON.parse(nameDirStatuses); 102 Collection<File> nameDirs = cluster.getNameDirs(); 103 for (File nameDir : nameDirs) { 104 System.out.println("Checking for the presence of " + nameDir + 105 " in active name dirs."); 106 assertTrue(statusMap.get("active").containsKey(nameDir.getAbsolutePath())); 107 } 108 assertEquals(2, statusMap.get("active").size()); 109 assertEquals(0, statusMap.get("failed").size()); 110 111 // This will cause the first dir to fail. 112 File failedNameDir = nameDirs.toArray(new File[0])[0]; 113 assertEquals(0, FileUtil.chmod(failedNameDir.getAbsolutePath(), "000")); 114 cluster.getNameNode().rollEditLog(); 115 116 nameDirStatuses = (String) (mbs.getAttribute(mxbeanName, 117 "NameDirStatuses")); 118 statusMap = (Map<String, Map<String, String>>) JSON.parse(nameDirStatuses); 119 for (File nameDir : nameDirs) { 120 String expectedStatus = 121 nameDir.equals(failedNameDir) ? "failed" : "active"; 122 System.out.println("Checking for the presence of " + nameDir + 123 " in " + expectedStatus + " name dirs."); 124 assertTrue(statusMap.get(expectedStatus).containsKey( 125 nameDir.getAbsolutePath())); 126 } 127 assertEquals(1, statusMap.get("active").size()); 128 assertEquals(1, statusMap.get("failed").size()); 129 } finally { 130 if (cluster != null) { 131 for (File dir : cluster.getNameDirs()) { 132 FileUtil.chmod(dir.toString(), "700"); 133 } 134 cluster.shutdown(); 135 } 136 } 137 } 138 } 139