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