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 */
018
019package org.apache.hadoop.hbase;
020
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.testclassification.MediumTests;
024import org.apache.hadoop.hbase.testclassification.MiscTests;
025import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
026import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
027import org.junit.After;
028import org.junit.Before;
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  private static final long SLEEP_INTERVAL = 1000;
039  private static final long SLEEP_TIME = 4000;
040
041  private HBaseTestingUtility testUtil;
042  private LocalHBaseCluster cluster;
043  private RegionServerThread rs;
044  private MasterThread master;
045
046  @Before
047  public void setUp() throws Exception {
048    testUtil = new HBaseTestingUtility();
049    testUtil.startMiniDFSCluster(1);
050    testUtil.startMiniZKCluster(1);
051    testUtil.createRootDir(); //manually setup hbase dir to point to minidfscluster
052    cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
053  }
054
055  @After
056  public void tearDown() throws Exception {
057    cluster.shutdown();
058    cluster.join();
059    testUtil.shutdownMiniZKCluster();
060    testUtil.shutdownMiniDFSCluster();
061  }
062
063  private void startRegionServer() throws Exception {
064    rs = cluster.addRegionServer();
065    rs.start();
066
067    for (int i=0; i * SLEEP_INTERVAL < SLEEP_TIME ;i++) {
068      //we cannot block on wait for rs at this point , since master is not up.
069      Thread.sleep(SLEEP_INTERVAL);
070      assertTrue(rs.isAlive());
071    }
072  }
073
074  private void startMaster() throws Exception {
075    master = cluster.addMaster();
076    master.start();
077
078    for (int i=0; i * SLEEP_INTERVAL < SLEEP_TIME ;i++) {
079      Thread.sleep(SLEEP_INTERVAL);
080      assertTrue(master.isAlive());
081    }
082  }
083
084  private void waitForClusterOnline() {
085    while (true) {
086      if (master.getMaster().isInitialized()) {
087        break;
088      }
089      try {
090        Thread.sleep(100);
091      } catch (InterruptedException ignored) {
092        // Keep waiting
093      }
094    }
095    rs.waitForServerOnline();
096  }
097
098  /**
099   * 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}