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.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.master.HMaster;
031import org.apache.hadoop.hbase.master.LoadBalancer;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.RegionServerTests;
034import org.apache.hadoop.hbase.util.FSUtils;
035import org.apache.hadoop.hbase.util.JVMClusterUtil;
036import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043/**
044 * Test metrics incremented on region server operations.
045 */
046@Category({RegionServerTests.class, MediumTests.class})
047public class TestClusterId {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestClusterId.class);
052
053  private final HBaseTestingUtility TEST_UTIL =
054      new HBaseTestingUtility();
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    TEST_UTIL.startMiniHBaseCluster(1, 0);
086
087    rst.waitForServerOnline();
088
089    String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher());
090    assertNotNull(clusterId);
091    assertEquals(clusterId, rst.getRegionServer().getClusterId());
092  }
093
094  @Test
095  public void testRewritingClusterIdToPB() throws Exception {
096    TEST_UTIL.startMiniZKCluster();
097    TEST_UTIL.startMiniDFSCluster(1);
098    TEST_UTIL.createRootDir();
099    Path rootDir = FSUtils.getRootDir(TEST_UTIL.getConfiguration());
100    FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
101    Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME);
102    FSDataOutputStream s = null;
103    try {
104      s = fs.create(filePath);
105      s.writeUTF(TEST_UTIL.getRandomUUID().toString());
106    } finally {
107      if (s != null) {
108        s.close();
109      }
110    }
111    TEST_UTIL.startMiniHBaseCluster(1, 1);
112    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
113    int expected = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 2: 1;
114    assertEquals(expected, master.getServerManager().getOnlineServersList().size());
115  }
116
117}
118