001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertTrue;
021
022import org.apache.hadoop.hbase.testclassification.MediumTests;
023import org.apache.hadoop.hbase.testclassification.MiscTests;
024import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
025import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
026import org.junit.After;
027import org.junit.Before;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032/**
033 * Tests the boot order indifference between regionserver and master
034 */
035@Category({ MiscTests.class, MediumTests.class })
036public class TestClusterBootOrder {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestClusterBootOrder.class);
041
042  private static final long SLEEP_INTERVAL = 1000;
043  private static final long SLEEP_TIME = 4000;
044
045  private HBaseTestingUtility testUtil;
046  private LocalHBaseCluster cluster;
047  private RegionServerThread rs;
048  private MasterThread master;
049
050  @Before
051  public void setUp() throws Exception {
052    testUtil = new HBaseTestingUtility();
053    testUtil.startMiniDFSCluster(1);
054    testUtil.startMiniZKCluster(1);
055    testUtil.createRootDir(); // manually setup hbase dir to point to minidfscluster
056    cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
057  }
058
059  @After
060  public void tearDown() throws Exception {
061    cluster.shutdown();
062    cluster.join();
063    testUtil.shutdownMiniZKCluster();
064    testUtil.shutdownMiniDFSCluster();
065  }
066
067  private void startRegionServer() throws Exception {
068    rs = cluster.addRegionServer();
069    rs.start();
070
071    for (int i = 0; i * SLEEP_INTERVAL < SLEEP_TIME; i++) {
072      // we cannot block on wait for rs at this point , since master is not up.
073      Thread.sleep(SLEEP_INTERVAL);
074      assertTrue(rs.isAlive());
075    }
076  }
077
078  private void startMaster() throws Exception {
079    master = cluster.addMaster();
080    master.start();
081
082    for (int i = 0; i * SLEEP_INTERVAL < SLEEP_TIME; i++) {
083      Thread.sleep(SLEEP_INTERVAL);
084      assertTrue(master.isAlive());
085    }
086  }
087
088  private void waitForClusterOnline() {
089    while (true) {
090      if (master.getMaster().isInitialized()) {
091        break;
092      }
093      try {
094        Thread.sleep(100);
095      } catch (InterruptedException ignored) {
096        // Keep waiting
097      }
098    }
099    rs.waitForServerOnline();
100  }
101
102  /**
103   * Tests launching the cluster by first starting regionserver, and then the master to ensure that
104   * it does not matter which is started first.
105   */
106  @Test
107  public void testBootRegionServerFirst() throws Exception {
108    startRegionServer();
109    startMaster();
110    waitForClusterOnline();
111  }
112
113  /**
114   * Tests launching the cluster by first starting master, and then the regionserver to ensure that
115   * it does not matter which is started first.
116   */
117  @Test
118  public void testBootMasterFirst() throws Exception {
119    startMaster();
120    startRegionServer();
121    waitForClusterOnline();
122  }
123
124}