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