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.jupiter.api.Assertions.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.jupiter.api.AfterEach;
027import org.junit.jupiter.api.BeforeEach;
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031/**
032 * Tests the boot order indifference between regionserver and master
033 */
034@Tag(MiscTests.TAG)
035@Tag(MediumTests.TAG)
036public class TestClusterBootOrder {
037
038  private static final long SLEEP_INTERVAL = 1000;
039  private static final long SLEEP_TIME = 4000;
040
041  private HBaseTestingUtil testUtil;
042  private LocalHBaseCluster cluster;
043  private RegionServerThread rs;
044  private MasterThread master;
045
046  @BeforeEach
047  public void setUp() throws Exception {
048    testUtil = new HBaseTestingUtil();
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  @AfterEach
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 to ensure that
100   * 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 to ensure that
111   * 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}