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