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