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.Assert.assertTrue;
022
023import java.io.IOException;
024import java.net.InetAddress;
025import java.net.UnknownHostException;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.Abortable;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.HRegionInfo;
032import org.apache.hadoop.hbase.MetaMockingUtil;
033import org.apache.hadoop.hbase.ServerMetricsBuilder;
034import org.apache.hadoop.hbase.ServerName;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.Waiter;
037import org.apache.hadoop.hbase.ZooKeeperConnectionException;
038import org.apache.hadoop.hbase.client.ClusterConnection;
039import org.apache.hadoop.hbase.client.HConnectionTestingUtility;
040import org.apache.hadoop.hbase.client.Result;
041import org.apache.hadoop.hbase.testclassification.MasterTests;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.util.CommonFSUtils;
044import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
045import org.apache.hadoop.hbase.util.Threads;
046import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
047import org.apache.hadoop.hbase.zookeeper.ZKUtil;
048import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
049import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
050import org.apache.zookeeper.KeeperException;
051import org.junit.After;
052import org.junit.AfterClass;
053import org.junit.Assert;
054import org.junit.BeforeClass;
055import org.junit.ClassRule;
056import org.junit.Ignore;
057import org.junit.Rule;
058import org.junit.Test;
059import org.junit.experimental.categories.Category;
060import org.junit.rules.TestName;
061import org.mockito.Mockito;
062import org.slf4j.Logger;
063import org.slf4j.LoggerFactory;
064
065import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
066import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerReportRequest;
067
068/**
069 * Standup the master and fake it to test various aspects of master function. Does NOT spin up a
070 * mini hbase nor mini dfs cluster testing master (it does put up a zk cluster but this is usually
071 * pretty fast compared). Also, should be possible to inject faults at points difficult to get at in
072 * cluster context. TODO: Speed up the zk connection by Master. It pauses 5 seconds establishing
073 * session.
074 */
075@Category({ MasterTests.class, MediumTests.class })
076public class TestMasterNoCluster {
077
078  @ClassRule
079  public static final HBaseClassTestRule CLASS_RULE =
080    HBaseClassTestRule.forClass(TestMasterNoCluster.class);
081
082  private static final Logger LOG = LoggerFactory.getLogger(TestMasterNoCluster.class);
083  private static final HBaseTestingUtility TESTUTIL = new HBaseTestingUtility();
084
085  @Rule
086  public TestName name = new TestName();
087
088  @BeforeClass
089  public static void setUpBeforeClass() throws Exception {
090    Configuration c = TESTUTIL.getConfiguration();
091    // We use local filesystem. Set it so it writes into the testdir.
092    CommonFSUtils.setRootDir(c, TESTUTIL.getDataTestDir());
093    DefaultMetricsSystem.setMiniClusterMode(true);
094    // Startup a mini zk cluster.
095    TESTUTIL.startMiniZKCluster();
096  }
097
098  @AfterClass
099  public static void tearDownAfterClass() throws Exception {
100    TESTUTIL.shutdownMiniZKCluster();
101  }
102
103  @After
104  public void tearDown() throws KeeperException, ZooKeeperConnectionException, IOException {
105    // Make sure zk is clean before we run the next test.
106    ZKWatcher zkw = new ZKWatcher(TESTUTIL.getConfiguration(), "@Before", new Abortable() {
107      @Override
108      public void abort(String why, Throwable e) {
109        throw new RuntimeException(why, e);
110      }
111
112      @Override
113      public boolean isAborted() {
114        return false;
115      }
116    });
117    // Before fails sometimes so retry.
118    try {
119      TESTUTIL.waitFor(10000, new Waiter.Predicate<Exception>() {
120        @Override
121        public boolean evaluate() throws Exception {
122          try {
123            ZKUtil.deleteNodeRecursively(zkw, zkw.getZNodePaths().baseZNode);
124            return true;
125          } catch (KeeperException.NotEmptyException e) {
126            LOG.info("Failed delete, retrying", e);
127          }
128          return false;
129        }
130      });
131    } catch (Exception e) {
132      LOG.info("Failed zk clear", e);
133    }
134    zkw.close();
135  }
136
137  /**
138   * Test starting master then stopping it before its fully up.
139   */
140  @Test
141  public void testStopDuringStart() throws IOException, KeeperException, InterruptedException {
142    HMaster master = new HMaster(TESTUTIL.getConfiguration());
143    master.start();
144    // Immediately have it stop. We used hang in assigning meta.
145    master.stopMaster();
146    master.join();
147  }
148
149  /**
150   * Test master failover. Start up three fake regionservers and a master.
151   * @throws org.apache.hbase.thirdparty.com.google.protobuf.ServiceException
152   */
153  @Ignore
154  @Test // Disabled since HBASE-18511. Reenable when master can carry regions.
155  public void testFailover() throws Exception {
156    final long now = EnvironmentEdgeManager.currentTime();
157    // Names for our three servers. Make the port numbers match hostname.
158    // Will come in use down in the server when we need to figure how to respond.
159    final ServerName sn0 = ServerName.valueOf("0.example.org", 0, now);
160    final ServerName sn1 = ServerName.valueOf("1.example.org", 1, now);
161    final ServerName sn2 = ServerName.valueOf("2.example.org", 2, now);
162    final ServerName[] sns = new ServerName[] { sn0, sn1, sn2 };
163    // Put up the mock servers
164    final Configuration conf = TESTUTIL.getConfiguration();
165    final MockRegionServer rs0 = new MockRegionServer(conf, sn0);
166    final MockRegionServer rs1 = new MockRegionServer(conf, sn1);
167    final MockRegionServer rs2 = new MockRegionServer(conf, sn2);
168    // Put some data into the servers. Make it look like sn0 has the metaH
169    // Put data into sn2 so it looks like it has a few regions for a table named 't'.
170    MetaTableLocator.setMetaLocation(rs0.getZooKeeper(), rs0.getServerName(),
171      RegionState.State.OPEN);
172    final TableName tableName = TableName.valueOf(name.getMethodName());
173    Result[] results = new Result[] {
174      MetaMockingUtil.getMetaTableRowResult(
175        new HRegionInfo(tableName, HConstants.EMPTY_START_ROW, HBaseTestingUtility.KEYS[1]),
176        rs2.getServerName()),
177      MetaMockingUtil.getMetaTableRowResult(
178        new HRegionInfo(tableName, HBaseTestingUtility.KEYS[1], HBaseTestingUtility.KEYS[2]),
179        rs2.getServerName()),
180      MetaMockingUtil.getMetaTableRowResult(
181        new HRegionInfo(tableName, HBaseTestingUtility.KEYS[2], HConstants.EMPTY_END_ROW),
182        rs2.getServerName()) };
183    rs1.setNextResults(HRegionInfo.FIRST_META_REGIONINFO.getRegionName(), results);
184
185    // Create master. Subclass to override a few methods so we can insert mocks
186    // and get notification on transitions. We need to fake out any rpcs the
187    // master does opening/closing regions. Also need to fake out the address
188    // of the 'remote' mocked up regionservers.
189    // Insert a mock for the connection, use TESTUTIL.getConfiguration rather than
190    // the conf from the master; the conf will already have an ClusterConnection
191    // associate so the below mocking of a connection will fail.
192    final ClusterConnection mockedConnection =
193      HConnectionTestingUtility.getMockedConnectionAndDecorate(TESTUTIL.getConfiguration(), rs0,
194        rs0, rs0.getServerName(), HRegionInfo.FIRST_META_REGIONINFO);
195    HMaster master = new HMaster(conf) {
196      @Override
197      InetAddress getRemoteInetAddress(final int port, final long serverStartCode)
198        throws UnknownHostException {
199        // Return different address dependent on port passed.
200        if (port > sns.length) {
201          return super.getRemoteInetAddress(port, serverStartCode);
202        }
203        ServerName sn = sns[port];
204        return InetAddress.getByAddress(sn.getHostname(),
205          new byte[] { 10, 0, 0, (byte) sn.getPort() });
206      }
207
208      @Override
209      protected void initClusterSchemaService() throws IOException, InterruptedException {
210      }
211
212      @Override
213      protected ServerManager createServerManager(MasterServices master, RegionServerList storage)
214        throws IOException {
215        ServerManager sm = super.createServerManager(master, storage);
216        // Spy on the created servermanager
217        ServerManager spy = Mockito.spy(sm);
218        return spy;
219      }
220
221      @Override
222      public ClusterConnection getConnection() {
223        return mockedConnection;
224      }
225
226      @Override
227      public ClusterConnection getClusterConnection() {
228        return mockedConnection;
229      }
230    };
231    master.start();
232
233    try {
234      // Wait till master is up ready for RPCs.
235      while (!master.serviceStarted)
236        Threads.sleep(10);
237      // Fake master that there are regionservers out there. Report in.
238      for (int i = 0; i < sns.length; i++) {
239        RegionServerReportRequest.Builder request = RegionServerReportRequest.newBuilder();
240        ;
241        ServerName sn = ServerName.parseVersionedServerName(sns[i].getVersionedBytes());
242        request.setServer(ProtobufUtil.toServerName(sn));
243        request.setLoad(ServerMetricsBuilder.toServerLoad(ServerMetricsBuilder.of(sn)));
244        master.getMasterRpcServices().regionServerReport(null, request.build());
245      }
246      // Master should now come up.
247      while (!master.isInitialized()) {
248        Threads.sleep(100);
249      }
250      assertTrue(master.isInitialized());
251    } finally {
252      rs0.stop("Test is done");
253      rs1.stop("Test is done");
254      rs2.stop("Test is done");
255      master.stopMaster();
256      master.join();
257    }
258  }
259
260  @Test
261  public void testMasterInitWithSameClientServerZKQuorum() throws Exception {
262    Configuration conf = new Configuration(TESTUTIL.getConfiguration());
263    conf.set(HConstants.CLIENT_ZOOKEEPER_QUORUM, conf.get(ZOOKEEPER_QUORUM));
264    conf.setInt(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT, TESTUTIL.getZkCluster().getClientPort());
265    HMaster master = new HMaster(conf);
266    master.start();
267    // the master will abort due to IllegalArgumentException so we should finish within 60 seconds
268    master.join();
269  }
270
271  @Test
272  public void testMasterInitWithObserverModeClientZKQuorum() throws Exception {
273    Configuration conf = new Configuration(TESTUTIL.getConfiguration());
274    Assert.assertFalse(Boolean.getBoolean(HConstants.CLIENT_ZOOKEEPER_OBSERVER_MODE));
275    // set client ZK to some non-existing address and make sure server won't access client ZK
276    // (server start should not be affected)
277    conf.set(HConstants.CLIENT_ZOOKEEPER_QUORUM, HConstants.LOCALHOST);
278    conf.setInt(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT,
279      TESTUTIL.getZkCluster().getClientPort() + 1);
280    // settings to allow us not to start additional RS
281    conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
282    conf.setBoolean(LoadBalancer.TABLES_ON_MASTER, true);
283    // main setting for this test case
284    conf.setBoolean(HConstants.CLIENT_ZOOKEEPER_OBSERVER_MODE, true);
285    HMaster master = new HMaster(conf);
286    master.start();
287    while (!master.isInitialized()) {
288      Threads.sleep(200);
289    }
290    Assert.assertNull(master.getMetaLocationSyncer());
291    Assert.assertNull(master.masterAddressSyncer);
292    master.stopMaster();
293    master.join();
294  }
295}