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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FSDataOutputStream;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.StartMiniClusterOption;
032import org.apache.hadoop.hbase.master.HMaster;
033import org.apache.hadoop.hbase.master.LoadBalancer;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.testclassification.RegionServerTests;
036import org.apache.hadoop.hbase.util.CommonFSUtils;
037import org.apache.hadoop.hbase.util.JVMClusterUtil;
038import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
039import org.junit.After;
040import org.junit.Before;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045/**
046 * Test metrics incremented on region server operations.
047 */
048@Category({ RegionServerTests.class, MediumTests.class })
049public class TestClusterId {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestClusterId.class);
054
055  private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
056
057  private JVMClusterUtil.RegionServerThread rst;
058
059  @Before
060  public void setUp() throws Exception {
061    TEST_UTIL.getConfiguration().setBoolean(ShutdownHook.RUN_SHUTDOWN_HOOK, false);
062  }
063
064  @After
065  public void tearDown() throws Exception {
066    TEST_UTIL.shutdownMiniCluster();
067    if (rst != null && rst.getRegionServer() != null) {
068      rst.getRegionServer().stop("end of test");
069      rst.join();
070    }
071  }
072
073  @Test
074  public void testClusterId() throws Exception {
075    TEST_UTIL.startMiniZKCluster();
076    TEST_UTIL.startMiniDFSCluster(1);
077
078    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
079    // start region server, needs to be separate
080    // so we get an unset clusterId
081    rst = JVMClusterUtil.createRegionServerThread(conf, HRegionServer.class, 0);
082    rst.start();
083    // Make sure RS is in blocking state
084    Thread.sleep(10000);
085
086    StartMiniClusterOption option =
087      StartMiniClusterOption.builder().numMasters(1).numRegionServers(0).build();
088    TEST_UTIL.startMiniHBaseCluster(option);
089
090    rst.waitForServerOnline();
091
092    String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher());
093    assertNotNull(clusterId);
094    assertEquals(clusterId, rst.getRegionServer().getClusterId());
095  }
096
097  @Test
098  public void testRewritingClusterIdToPB() throws Exception {
099    TEST_UTIL.startMiniZKCluster();
100    TEST_UTIL.startMiniDFSCluster(1);
101    TEST_UTIL.createRootDir();
102    Path rootDir = CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration());
103    FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
104    Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME);
105    FSDataOutputStream s = null;
106    try {
107      s = fs.create(filePath);
108      s.writeUTF(HBaseCommonTestingUtility.getRandomUUID().toString());
109    } finally {
110      if (s != null) {
111        s.close();
112      }
113    }
114    TEST_UTIL.startMiniHBaseCluster();
115    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
116    int expected = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()) ? 2 : 1;
117    assertEquals(expected, master.getServerManager().getOnlineServersList().size());
118  }
119
120}