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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.master
;
20 import static org
.apache
.hadoop
.hbase
.HConstants
.ZOOKEEPER_QUORUM
;
22 import java
.io
.IOException
;
23 import org
.apache
.hadoop
.conf
.Configuration
;
24 import org
.apache
.hadoop
.hbase
.Abortable
;
25 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
26 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
27 import org
.apache
.hadoop
.hbase
.HConstants
;
28 import org
.apache
.hadoop
.hbase
.Waiter
;
29 import org
.apache
.hadoop
.hbase
.ZooKeeperConnectionException
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
31 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
32 import org
.apache
.hadoop
.hbase
.util
.CommonFSUtils
;
33 import org
.apache
.hadoop
.hbase
.util
.Threads
;
34 import org
.apache
.hadoop
.hbase
.zookeeper
.ZKUtil
;
35 import org
.apache
.hadoop
.hbase
.zookeeper
.ZKWatcher
;
36 import org
.apache
.hadoop
.metrics2
.lib
.DefaultMetricsSystem
;
37 import org
.apache
.zookeeper
.KeeperException
;
38 import org
.junit
.After
;
39 import org
.junit
.AfterClass
;
40 import org
.junit
.Assert
;
41 import org
.junit
.BeforeClass
;
42 import org
.junit
.ClassRule
;
43 import org
.junit
.Rule
;
44 import org
.junit
.Test
;
45 import org
.junit
.experimental
.categories
.Category
;
46 import org
.junit
.rules
.TestName
;
47 import org
.slf4j
.Logger
;
48 import org
.slf4j
.LoggerFactory
;
51 * Standup the master and fake it to test various aspects of master function.
52 * Does NOT spin up a mini hbase nor mini dfs cluster testing master (it does
53 * put up a zk cluster but this is usually pretty fast compared). Also, should
54 * be possible to inject faults at points difficult to get at in cluster context.
55 * TODO: Speed up the zk connection by Master. It pauses 5 seconds establishing
58 @Category({MasterTests
.class, MediumTests
.class})
59 public class TestMasterNoCluster
{
62 public static final HBaseClassTestRule CLASS_RULE
=
63 HBaseClassTestRule
.forClass(TestMasterNoCluster
.class);
65 private static final Logger LOG
= LoggerFactory
.getLogger(TestMasterNoCluster
.class);
67 private static final HBaseTestingUtil TESTUTIL
= new HBaseTestingUtil();
70 public TestName name
= new TestName();
73 public static void setUpBeforeClass() throws Exception
{
74 Configuration c
= TESTUTIL
.getConfiguration();
75 // We use local filesystem. Set it so it writes into the testdir.
76 CommonFSUtils
.setRootDir(c
, TESTUTIL
.getDataTestDir());
77 DefaultMetricsSystem
.setMiniClusterMode(true);
78 // Startup a mini zk cluster.
79 TESTUTIL
.startMiniZKCluster();
83 public static void tearDownAfterClass() throws Exception
{
84 TESTUTIL
.shutdownMiniZKCluster();
88 public void tearDown()
89 throws KeeperException
, ZooKeeperConnectionException
, IOException
{
90 // Make sure zk is clean before we run the next test.
91 ZKWatcher zkw
= new ZKWatcher(TESTUTIL
.getConfiguration(),
92 "@Before", new Abortable() {
94 public void abort(String why
, Throwable e
) {
95 throw new RuntimeException(why
, e
);
99 public boolean isAborted() {
103 // Before fails sometimes so retry.
105 TESTUTIL
.waitFor(10000, (Waiter
.Predicate
<Exception
>) () -> {
107 ZKUtil
.deleteNodeRecursively(zkw
, zkw
.getZNodePaths().baseZNode
);
109 } catch (KeeperException
.NotEmptyException e
) {
110 LOG
.info("Failed delete, retrying", e
);
114 } catch (Exception e
) {
115 LOG
.info("Failed zk clear", e
);
121 * Test starting master then stopping it before its fully up.
124 public void testStopDuringStart() throws IOException
, KeeperException
, InterruptedException
{
125 HMaster master
= new HMaster(TESTUTIL
.getConfiguration());
127 // Immediately have it stop. We used hang in assigning meta.
133 public void testMasterInitWithSameClientServerZKQuorum() throws Exception
{
134 Configuration conf
= new Configuration(TESTUTIL
.getConfiguration());
135 conf
.set(HConstants
.CLIENT_ZOOKEEPER_QUORUM
, conf
.get(ZOOKEEPER_QUORUM
));
136 conf
.setInt(HConstants
.CLIENT_ZOOKEEPER_CLIENT_PORT
, TESTUTIL
.getZkCluster().getClientPort());
137 HMaster master
= new HMaster(conf
);
139 // the master will abort due to IllegalArgumentException so we should finish within 60 seconds
144 public void testMasterInitWithObserverModeClientZKQuorum() throws Exception
{
145 Configuration conf
= new Configuration(TESTUTIL
.getConfiguration());
146 Assert
.assertFalse(Boolean
.getBoolean(HConstants
.CLIENT_ZOOKEEPER_OBSERVER_MODE
));
147 // set client ZK to some non-existing address and make sure server won't access client ZK
148 // (server start should not be affected)
149 conf
.set(HConstants
.CLIENT_ZOOKEEPER_QUORUM
, HConstants
.LOCALHOST
);
150 conf
.setInt(HConstants
.CLIENT_ZOOKEEPER_CLIENT_PORT
,
151 TESTUTIL
.getZkCluster().getClientPort() + 1);
152 // need to enable maintenance mode so we will start master and an in process region server
153 conf
.setBoolean(HMaster
.MAINTENANCE_MODE
, true);
154 // settings to allow us not to start additional RS
155 conf
.setInt(ServerManager
.WAIT_ON_REGIONSERVERS_MINTOSTART
, 1);
156 // main setting for this test case
157 conf
.setBoolean(HConstants
.CLIENT_ZOOKEEPER_OBSERVER_MODE
, true);
158 HMaster master
= new HMaster(conf
);
160 while (!master
.isInitialized()) {
163 Assert
.assertNull(master
.getMetaLocationSyncer());
164 Assert
.assertNull(master
.masterAddressSyncer
);