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 
19 package org.apache.hadoop.tools;
20 
21 import static org.apache.hadoop.test.MetricsAsserts.assertGauge;
22 import static org.apache.hadoop.test.MetricsAsserts.getMetrics;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.PipedInputStream;
29 import java.io.PipedOutputStream;
30 import java.io.PrintStream;
31 import java.lang.management.ManagementFactory;
32 import java.util.Random;
33 import java.util.Set;
34 
35 import javax.management.MBeanServerConnection;
36 import javax.management.ObjectName;
37 
38 import org.apache.hadoop.conf.Configuration;
39 import org.apache.hadoop.fs.CommonConfigurationKeys;
40 import org.apache.hadoop.fs.FSDataOutputStream;
41 import org.apache.hadoop.fs.FileSystem;
42 import org.apache.hadoop.fs.FileUtil;
43 import org.apache.hadoop.fs.Path;
44 import org.apache.hadoop.hdfs.HdfsConfiguration;
45 import org.apache.hadoop.hdfs.MiniDFSCluster;
46 import org.apache.hadoop.hdfs.tools.JMXGet;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 
51 
52 /**
53  * Startup and checkpoint tests
54  *
55  */
56 public class TestJMXGet {
57 
58   private Configuration config;
59   private MiniDFSCluster cluster;
60 
61   static final long seed = 0xAAAAEEFL;
62   static final int blockSize = 4096;
63   static final int fileSize = 8192;
64 
writeFile(FileSystem fileSys, Path name, int repl)65   private void writeFile(FileSystem fileSys, Path name, int repl)
66   throws IOException {
67     FSDataOutputStream stm = fileSys.create(name, true,
68         fileSys.getConf().getInt(CommonConfigurationKeys.IO_FILE_BUFFER_SIZE_KEY, 4096),
69         (short)repl, blockSize);
70     byte[] buffer = new byte[fileSize];
71     Random rand = new Random(seed);
72     rand.nextBytes(buffer);
73     stm.write(buffer);
74     stm.close();
75   }
76 
77 
78   @Before
setUp()79   public void setUp() throws Exception {
80     config = new HdfsConfiguration();
81   }
82 
83   /**
84    * clean up
85    */
86   @After
tearDown()87   public void tearDown() throws Exception {
88     if(cluster.isClusterUp())
89       cluster.shutdown();
90 
91     File data_dir = new File(cluster.getDataDirectory());
92     if(data_dir.exists() && !FileUtil.fullyDelete(data_dir)) {
93       throw new IOException("Could not delete hdfs directory in tearDown '"
94           + data_dir + "'");
95     }
96   }
97 
98   /**
99    * test JMX connection to NameNode..
100    * @throws Exception
101    */
102   @Test
testNameNode()103   public void testNameNode() throws Exception {
104     int numDatanodes = 2;
105     cluster = new MiniDFSCluster.Builder(config).numDataNodes(numDatanodes).build();
106     cluster.waitActive();
107 
108     writeFile(cluster.getFileSystem(), new Path("/test1"), 2);
109 
110     JMXGet jmx = new JMXGet();
111     String serviceName = "NameNode";
112     jmx.setService(serviceName);
113     jmx.init(); // default lists namenode mbeans only
114     assertTrue("error printAllValues", checkPrintAllValues(jmx));
115 
116     //get some data from different source
117     assertEquals(numDatanodes, Integer.parseInt(
118         jmx.getValue("NumLiveDataNodes")));
119     assertGauge("CorruptBlocks", Long.parseLong(jmx.getValue("CorruptBlocks")),
120                 getMetrics("FSNamesystem"));
121     assertEquals(numDatanodes, Integer.parseInt(
122         jmx.getValue("NumOpenConnections")));
123 
124     cluster.shutdown();
125     MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();
126     ObjectName query = new ObjectName("Hadoop:service=" + serviceName + ",*");
127     Set<ObjectName> names = mbsc.queryNames(query, null);
128     assertTrue("No beans should be registered for " + serviceName, names.isEmpty());
129   }
130 
checkPrintAllValues(JMXGet jmx)131   private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
132     int size = 0;
133     byte[] bytes = null;
134     String pattern = "List of all the available keys:";
135     PipedOutputStream pipeOut = new PipedOutputStream();
136     PipedInputStream pipeIn = new PipedInputStream(pipeOut);
137     System.setErr(new PrintStream(pipeOut));
138     jmx.printAllValues();
139     if ((size = pipeIn.available()) != 0) {
140       bytes = new byte[size];
141       pipeIn.read(bytes, 0, bytes.length);
142     }
143     pipeOut.close();
144     pipeIn.close();
145     return bytes != null ? new String(bytes).contains(pattern) : false;
146   }
147 
148   /**
149    * test JMX connection to DataNode..
150    * @throws Exception
151    */
152   @Test
testDataNode()153   public void testDataNode() throws Exception {
154     int numDatanodes = 2;
155     cluster = new MiniDFSCluster.Builder(config).numDataNodes(numDatanodes).build();
156     cluster.waitActive();
157 
158     writeFile(cluster.getFileSystem(), new Path("/test"), 2);
159 
160     JMXGet jmx = new JMXGet();
161     String serviceName = "DataNode";
162     jmx.setService(serviceName);
163     jmx.init();
164     assertEquals(fileSize, Integer.parseInt(jmx.getValue("BytesWritten")));
165 
166     cluster.shutdown();
167     MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();
168     ObjectName query = new ObjectName("Hadoop:service=" + serviceName + ",*");
169     Set<ObjectName> names = mbsc.queryNames(query, null);
170     assertTrue("No beans should be registered for " + serviceName, names.isEmpty());
171   }
172 }
173