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 = 056 new HBaseTestingUtility(); 057 058 private JVMClusterUtil.RegionServerThread rst; 059 060 @Before 061 public void setUp() throws Exception { 062 TEST_UTIL.getConfiguration().setBoolean(ShutdownHook.RUN_SHUTDOWN_HOOK, false); 063 } 064 065 @After 066 public void tearDown() throws Exception { 067 TEST_UTIL.shutdownMiniCluster(); 068 if(rst != null && rst.getRegionServer() != null) { 069 rst.getRegionServer().stop("end of test"); 070 rst.join(); 071 } 072 } 073 074 @Test 075 public void testClusterId() throws Exception { 076 TEST_UTIL.startMiniZKCluster(); 077 TEST_UTIL.startMiniDFSCluster(1); 078 079 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 080 //start region server, needs to be separate 081 //so we get an unset clusterId 082 rst = JVMClusterUtil.createRegionServerThread(conf, HRegionServer.class, 0); 083 rst.start(); 084 //Make sure RS is in blocking state 085 Thread.sleep(10000); 086 087 StartMiniClusterOption option = StartMiniClusterOption.builder() 088 .numMasters(1).numRegionServers(0).build(); 089 TEST_UTIL.startMiniHBaseCluster(option); 090 091 rst.waitForServerOnline(); 092 093 String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher()); 094 assertNotNull(clusterId); 095 assertEquals(clusterId, rst.getRegionServer().getClusterId()); 096 } 097 098 @Test 099 public void testRewritingClusterIdToPB() throws Exception { 100 TEST_UTIL.startMiniZKCluster(); 101 TEST_UTIL.startMiniDFSCluster(1); 102 TEST_UTIL.createRootDir(); 103 Path rootDir = CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration()); 104 FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration()); 105 Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME); 106 FSDataOutputStream s = null; 107 try { 108 s = fs.create(filePath); 109 s.writeUTF(HBaseCommonTestingUtility.getRandomUUID().toString()); 110 } finally { 111 if (s != null) { 112 s.close(); 113 } 114 } 115 TEST_UTIL.startMiniHBaseCluster(); 116 HMaster master = TEST_UTIL.getHBaseCluster().getMaster(); 117 int expected = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 2: 1; 118 assertEquals(expected, master.getServerManager().getOnlineServersList().size()); 119 } 120 121} 122