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.master; 019 020import static org.apache.hadoop.hbase.HConstants.ZOOKEEPER_QUORUM; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertNull; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024 025import java.io.IOException; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.Abortable; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.Waiter; 031import org.apache.hadoop.hbase.ZooKeeperConnectionException; 032import org.apache.hadoop.hbase.io.MetricsIO; 033import org.apache.hadoop.hbase.testclassification.MasterTests; 034import org.apache.hadoop.hbase.testclassification.MediumTests; 035import org.apache.hadoop.hbase.util.CommonFSUtils; 036import org.apache.hadoop.hbase.util.Threads; 037import org.apache.hadoop.hbase.zookeeper.ZKUtil; 038import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 039import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; 040import org.apache.zookeeper.KeeperException; 041import org.junit.jupiter.api.AfterAll; 042import org.junit.jupiter.api.AfterEach; 043import org.junit.jupiter.api.BeforeAll; 044import org.junit.jupiter.api.Tag; 045import org.junit.jupiter.api.Test; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049/** 050 * Standup the master and fake it to test various aspects of master function. Does NOT spin up a 051 * mini hbase nor mini dfs cluster testing master (it does put up a zk cluster but this is usually 052 * pretty fast compared). Also, should be possible to inject faults at points difficult to get at in 053 * cluster context. TODO: Speed up the zk connection by Master. It pauses 5 seconds establishing 054 * session. 055 */ 056@Tag(MasterTests.TAG) 057@Tag(MediumTests.TAG) 058public class TestMasterNoCluster { 059 060 private static final Logger LOG = LoggerFactory.getLogger(TestMasterNoCluster.class); 061 062 private static final HBaseTestingUtil TESTUTIL = new HBaseTestingUtil(); 063 064 @BeforeAll 065 public static void setUpBeforeClass() throws Exception { 066 Configuration c = TESTUTIL.getConfiguration(); 067 // We use local filesystem. Set it so it writes into the testdir. 068 CommonFSUtils.setRootDir(c, TESTUTIL.getDataTestDir()); 069 DefaultMetricsSystem.setMiniClusterMode(true); 070 // Startup a mini zk cluster. 071 TESTUTIL.startMiniZKCluster(); 072 } 073 074 @AfterAll 075 public static void tearDownAfterClass() throws Exception { 076 TESTUTIL.shutdownMiniZKCluster(); 077 } 078 079 @AfterEach 080 public void tearDown() throws KeeperException, ZooKeeperConnectionException, IOException { 081 // Make sure zk is clean before we run the next test. 082 ZKWatcher zkw = new ZKWatcher(TESTUTIL.getConfiguration(), "@BeforeEach", new Abortable() { 083 @Override 084 public void abort(String why, Throwable e) { 085 throw new RuntimeException(why, e); 086 } 087 088 @Override 089 public boolean isAborted() { 090 return false; 091 } 092 }); 093 // Before fails sometimes so retry. 094 try { 095 TESTUTIL.waitFor(10000, (Waiter.Predicate<Exception>) () -> { 096 try { 097 ZKUtil.deleteNodeRecursively(zkw, zkw.getZNodePaths().baseZNode); 098 return true; 099 } catch (KeeperException.NotEmptyException e) { 100 LOG.info("Failed delete, retrying", e); 101 } 102 return false; 103 }); 104 } catch (Exception e) { 105 LOG.info("Failed zk clear", e); 106 } 107 zkw.close(); 108 } 109 110 /** 111 * Test starting master then stopping it before its fully up. 112 */ 113 @Test 114 public void testStopDuringStart() throws IOException, KeeperException, InterruptedException { 115 HMaster master = new HMaster(TESTUTIL.getConfiguration()); 116 master.start(); 117 // Immediately have it stop. We used hang in assigning meta. 118 master.stopMaster(); 119 master.join(); 120 } 121 122 /** 123 * The master reads and writes HFiles through its local MasterRegion, so it must enable IO metrics 124 * even when no RegionServer runs in the same JVM. See HBASE-30231. 125 */ 126 @Test 127 public void testMasterEnablesMetricsIO() throws IOException, InterruptedException { 128 MetricsIO.resetForTesting(); 129 HMaster master = new HMaster(TESTUTIL.getConfiguration()); 130 master.start(); 131 try { 132 assertTrue(MetricsIO.isEnabled()); 133 } finally { 134 master.stopMaster(); 135 master.join(); 136 MetricsIO.resetForTesting(); 137 } 138 } 139 140 @Test 141 public void testMasterInitWithSameClientServerZKQuorum() throws Exception { 142 Configuration conf = new Configuration(TESTUTIL.getConfiguration()); 143 conf.set(HConstants.CLIENT_ZOOKEEPER_QUORUM, conf.get(ZOOKEEPER_QUORUM)); 144 conf.setInt(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT, TESTUTIL.getZkCluster().getClientPort()); 145 HMaster master = new HMaster(conf); 146 master.start(); 147 // the master will abort due to IllegalArgumentException so we should finish within 60 seconds 148 master.join(); 149 } 150 151 @Test 152 public void testMasterInitWithObserverModeClientZKQuorum() throws Exception { 153 Configuration conf = new Configuration(TESTUTIL.getConfiguration()); 154 assertFalse(Boolean.getBoolean(HConstants.CLIENT_ZOOKEEPER_OBSERVER_MODE)); 155 // set client ZK to some non-existing address and make sure server won't access client ZK 156 // (server start should not be affected) 157 conf.set(HConstants.CLIENT_ZOOKEEPER_QUORUM, HConstants.LOCALHOST); 158 conf.setInt(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT, 159 TESTUTIL.getZkCluster().getClientPort() + 1); 160 // need to enable maintenance mode so we will start master and an in process region server 161 conf.setBoolean(HMaster.MAINTENANCE_MODE, true); 162 // settings to allow us not to start additional RS 163 conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1); 164 // main setting for this test case 165 conf.setBoolean(HConstants.CLIENT_ZOOKEEPER_OBSERVER_MODE, true); 166 HMaster master = new HMaster(conf); 167 master.start(); 168 while (!master.isInitialized()) { 169 Threads.sleep(200); 170 } 171 assertNull(master.getMetaLocationSyncer()); 172 assertNull(master.masterAddressSyncer); 173 master.stopMaster(); 174 master.join(); 175 } 176}