View Javadoc

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.hbase;
20  
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.hadoop.hbase.testclassification.MediumTests;
24  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
25  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Tests the boot order indifference between regionserver and master
33   */
34  @Category(MediumTests.class)
35  public class TestClusterBootOrder {
36  
37    private static final long SLEEP_INTERVAL = 1000;
38    private static final long SLEEP_TIME = 4000;
39  
40    private HBaseTestingUtility testUtil;
41    private LocalHBaseCluster cluster;
42    private RegionServerThread rs;
43    private MasterThread master;
44  
45    @Before
46    public void setUp() throws Exception {
47      testUtil = new HBaseTestingUtility();
48      testUtil.startMiniDFSCluster(1);
49      testUtil.startMiniZKCluster(1);
50      testUtil.createRootDir(); //manually setup hbase dir to point to minidfscluster
51      cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
52    }
53  
54    @After
55    public void tearDown() throws Exception {
56      cluster.shutdown();
57      cluster.join();
58      testUtil.shutdownMiniZKCluster();
59      testUtil.shutdownMiniDFSCluster();
60    }
61  
62    private void startRegionServer() throws Exception {
63      rs = cluster.addRegionServer();
64      rs.start();
65  
66      for (int i=0; i * SLEEP_INTERVAL < SLEEP_TIME ;i++) {
67        //we cannot block on wait for rs at this point , since master is not up.
68        Thread.sleep(SLEEP_INTERVAL);
69        assertTrue(rs.isAlive());
70      }
71    }
72  
73    private void startMaster() throws Exception {
74      master = cluster.addMaster();
75      master.start();
76  
77      for (int i=0; i * SLEEP_INTERVAL < SLEEP_TIME ;i++) {
78        Thread.sleep(SLEEP_INTERVAL);
79        assertTrue(master.isAlive());
80      }
81    }
82  
83    private void waitForClusterOnline() {
84      while (true) {
85        if (master.getMaster().isInitialized()) {
86          break;
87        }
88        try {
89          Thread.sleep(100);
90        } catch (InterruptedException ignored) {
91          // Keep waiting
92        }
93      }
94      rs.waitForServerOnline();
95    }
96  
97    /**
98     * Tests launching the cluster by first starting regionserver, and then the master
99     * to ensure that it does not matter which is started first.
100    */
101   @Test
102   public void testBootRegionServerFirst() throws Exception {
103     startRegionServer();
104     startMaster();
105     waitForClusterOnline();
106   }
107 
108   /**
109    * Tests launching the cluster by first starting master, and then the regionserver
110    * to ensure that it does not matter which is started first.
111    */
112   @Test
113   public void testBootMasterFirst() throws Exception {
114     startMaster();
115     startRegionServer();
116     waitForClusterOnline();
117   }
118 
119 }