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 java.util.UUID; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FSDataOutputStream; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.master.HMaster; 032import org.apache.hadoop.hbase.master.LoadBalancer; 033import org.apache.hadoop.hbase.testclassification.MediumTests; 034import org.apache.hadoop.hbase.testclassification.RegionServerTests; 035import org.apache.hadoop.hbase.util.FSUtils; 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 HBaseTestingUtility TEST_UTIL = 055 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 TEST_UTIL.startMiniHBaseCluster(1, 0); 087 088 rst.waitForServerOnline(); 089 090 String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher()); 091 assertNotNull(clusterId); 092 assertEquals(clusterId, rst.getRegionServer().getClusterId()); 093 } 094 095 @Test 096 public void testRewritingClusterIdToPB() throws Exception { 097 TEST_UTIL.startMiniZKCluster(); 098 TEST_UTIL.startMiniDFSCluster(1); 099 TEST_UTIL.createRootDir(); 100 Path rootDir = FSUtils.getRootDir(TEST_UTIL.getConfiguration()); 101 FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration()); 102 Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME); 103 FSDataOutputStream s = null; 104 try { 105 s = fs.create(filePath); 106 s.writeUTF(UUID.randomUUID().toString()); 107 } finally { 108 if (s != null) { 109 s.close(); 110 } 111 } 112 TEST_UTIL.startMiniHBaseCluster(1, 1); 113 HMaster master = TEST_UTIL.getHBaseCluster().getMaster(); 114 int expected = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 2: 1; 115 assertEquals(expected, master.getServerManager().getOnlineServersList().size()); 116 } 117 118} 119