1919
2020import java .io .File ;
2121import java .io .IOException ;
22+ import java .lang .management .ManagementFactory ;
2223import java .util .concurrent .ThreadLocalRandom ;
2324
25+ import javax .management .AttributeNotFoundException ;
26+ import javax .management .InstanceNotFoundException ;
27+ import javax .management .MBeanException ;
28+ import javax .management .MBeanServer ;
29+ import javax .management .MalformedObjectNameException ;
30+ import javax .management .ObjectName ;
31+ import javax .management .ReflectionException ;
32+ import javax .management .openmbean .CompositeDataSupport ;
33+
2434import org .apache .commons .logging .Log ;
2535import org .apache .commons .logging .LogFactory ;
2636import org .apache .hadoop .conf .Configuration ;
4555import org .junit .Assert ;
4656import org .junit .Test ;
4757
48- import static org .junit .Assert .assertTrue ;
58+ import static org .junit .Assert .assertEquals ;
59+ import static org .junit .Assert .assertNotEquals ;
60+ import static org .junit .Assert .assertNull ;
4961
5062/**
5163 * This class tests rolling upgrade.
@@ -56,7 +68,7 @@ public class TestRollingUpgrade {
5668 public static void runCmd (DFSAdmin dfsadmin , boolean success ,
5769 String ... args ) throws Exception {
5870 if (success ) {
59- Assert . assertEquals (0 , dfsadmin .run (args ));
71+ assertEquals (0 , dfsadmin .run (args ));
6072 } else {
6173 Assert .assertTrue (dfsadmin .run (args ) != 0 );
6274 }
@@ -86,6 +98,7 @@ public void testDFSAdminRollingUpgradeCommands() throws Exception {
8698 //illegal argument "abc" to rollingUpgrade option
8799 runCmd (dfsadmin , false , "-rollingUpgrade" , "abc" );
88100
101+ checkMxBeanIsNull ();
89102 //query rolling upgrade
90103 runCmd (dfsadmin , true , "-rollingUpgrade" );
91104
@@ -96,11 +109,16 @@ public void testDFSAdminRollingUpgradeCommands() throws Exception {
96109
97110 //query rolling upgrade
98111 runCmd (dfsadmin , true , "-rollingUpgrade" , "query" );
112+ checkMxBean ();
99113
100114 dfs .mkdirs (bar );
101115
102116 //finalize rolling upgrade
103117 runCmd (dfsadmin , true , "-rollingUpgrade" , "finalize" );
118+ // RollingUpgradeInfo should be null after finalization, both via
119+ // Java API and in JMX
120+ assertNull (dfs .rollingUpgrade (RollingUpgradeAction .QUERY ));
121+ checkMxBeanIsNull ();
104122
105123 dfs .mkdirs (baz );
106124
@@ -197,7 +215,7 @@ public void testRollingUpgradeWithQJM() throws Exception {
197215 LOG .info ("START\n " + info1 );
198216
199217 //query rolling upgrade
200- Assert . assertEquals (info1 , dfs .rollingUpgrade (RollingUpgradeAction .QUERY ));
218+ assertEquals (info1 , dfs .rollingUpgrade (RollingUpgradeAction .QUERY ));
201219
202220 dfs .mkdirs (bar );
203221 cluster .shutdown ();
@@ -218,13 +236,13 @@ public void testRollingUpgradeWithQJM() throws Exception {
218236 Assert .assertFalse (dfs2 .exists (baz ));
219237
220238 //query rolling upgrade in cluster2
221- Assert . assertEquals (info1 , dfs2 .rollingUpgrade (RollingUpgradeAction .QUERY ));
239+ assertEquals (info1 , dfs2 .rollingUpgrade (RollingUpgradeAction .QUERY ));
222240
223241 dfs2 .mkdirs (baz );
224242
225243 LOG .info ("RESTART cluster 2" );
226244 cluster2 .restartNameNode ();
227- Assert . assertEquals (info1 , dfs2 .rollingUpgrade (RollingUpgradeAction .QUERY ));
245+ assertEquals (info1 , dfs2 .rollingUpgrade (RollingUpgradeAction .QUERY ));
228246 Assert .assertTrue (dfs2 .exists (foo ));
229247 Assert .assertTrue (dfs2 .exists (bar ));
230248 Assert .assertTrue (dfs2 .exists (baz ));
@@ -238,7 +256,7 @@ public void testRollingUpgradeWithQJM() throws Exception {
238256
239257 LOG .info ("RESTART cluster 2 again" );
240258 cluster2 .restartNameNode ();
241- Assert . assertEquals (info1 , dfs2 .rollingUpgrade (RollingUpgradeAction .QUERY ));
259+ assertEquals (info1 , dfs2 .rollingUpgrade (RollingUpgradeAction .QUERY ));
242260 Assert .assertTrue (dfs2 .exists (foo ));
243261 Assert .assertTrue (dfs2 .exists (bar ));
244262 Assert .assertTrue (dfs2 .exists (baz ));
@@ -259,8 +277,30 @@ public void testRollingUpgradeWithQJM() throws Exception {
259277 }
260278 }
261279
280+ private static CompositeDataSupport getBean ()
281+ throws MalformedObjectNameException , MBeanException ,
282+ AttributeNotFoundException , InstanceNotFoundException ,
283+ ReflectionException {
284+ MBeanServer mbs = ManagementFactory .getPlatformMBeanServer ();
285+ ObjectName mxbeanName =
286+ new ObjectName ("Hadoop:service=NameNode,name=NameNodeInfo" );
287+ return (CompositeDataSupport )mbs .getAttribute (mxbeanName ,
288+ "RollingUpgradeStatus" );
289+ }
290+
291+ private static void checkMxBeanIsNull () throws Exception {
292+ CompositeDataSupport ruBean = getBean ();
293+ assertNull (ruBean );
294+ }
295+
296+ private static void checkMxBean () throws Exception {
297+ CompositeDataSupport ruBean = getBean ();
298+ assertNotEquals (0l , ruBean .get ("startTime" ));
299+ assertEquals (0l , ruBean .get ("finalizeTime" ));
300+ }
301+
262302 @ Test
263- public void testRollback () throws IOException {
303+ public void testRollback () throws Exception {
264304 // start a cluster
265305 final Configuration conf = new HdfsConfiguration ();
266306 MiniDFSCluster cluster = null ;
@@ -279,10 +319,13 @@ public void testRollback() throws IOException {
279319 out .write (data , 0 , data .length );
280320 out .close ();
281321
322+ checkMxBeanIsNull ();
282323 startRollingUpgrade (foo , bar , file , data , cluster );
324+ checkMxBean ();
283325 cluster .getFileSystem ().rollEdits ();
284326 cluster .getFileSystem ().rollEdits ();
285327 rollbackRollingUpgrade (foo , bar , file , data , cluster );
328+ checkMxBeanIsNull ();
286329
287330 startRollingUpgrade (foo , bar , file , data , cluster );
288331 cluster .getFileSystem ().rollEdits ();
@@ -356,18 +399,18 @@ public void testDFSAdminDatanodeUpgradeControlCommands() throws Exception {
356399 // check the datanode
357400 final String dnAddr = dn .getDatanodeId ().getIpcAddr (false );
358401 final String [] args1 = {"-getDatanodeInfo" , dnAddr };
359- Assert . assertEquals ( 0 , dfsadmin . run ( args1 ) );
402+ runCmd ( dfsadmin , true , args1 );
360403
361404 // issue shutdown to the datanode.
362405 final String [] args2 = {"-shutdownDatanode" , dnAddr , "upgrade" };
363- Assert . assertEquals ( 0 , dfsadmin . run ( args2 ) );
406+ runCmd ( dfsadmin , true , args2 );
364407
365408 // the datanode should be down.
366409 Thread .sleep (2000 );
367410 Assert .assertFalse ("DataNode should exit" , dn .isDatanodeUp ());
368411
369412 // ping should fail.
370- Assert . assertEquals (-1 , dfsadmin .run (args1 ));
413+ assertEquals (-1 , dfsadmin .run (args1 ));
371414 } finally {
372415 if (cluster != null ) cluster .shutdown ();
373416 }
0 commit comments