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.client; 019 020import static org.apache.hadoop.hbase.HConstants.META_REPLICAS_NUM; 021import static org.hamcrest.CoreMatchers.instanceOf; 022import static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertNotEquals; 024import static org.junit.Assert.assertNotNull; 025import static org.junit.Assert.assertNotSame; 026import static org.junit.Assert.assertThat; 027import static org.junit.Assert.fail; 028import java.io.IOException; 029import java.util.concurrent.ExecutionException; 030import java.util.stream.IntStream; 031import org.apache.commons.io.IOUtils; 032import org.apache.hadoop.conf.Configuration; 033import org.apache.hadoop.hbase.HBaseClassTestRule; 034import org.apache.hadoop.hbase.HBaseTestingUtility; 035import org.apache.hadoop.hbase.HConstants; 036import org.apache.hadoop.hbase.HRegionLocation; 037import org.apache.hadoop.hbase.RegionLocations; 038import org.apache.hadoop.hbase.TableName; 039import org.apache.hadoop.hbase.testclassification.ClientTests; 040import org.apache.hadoop.hbase.testclassification.MediumTests; 041import org.apache.hadoop.hbase.zookeeper.ReadOnlyZKClient; 042import org.junit.AfterClass; 043import org.junit.BeforeClass; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.slf4j.Logger; 048import org.slf4j.LoggerFactory; 049 050@Category({ MediumTests.class, ClientTests.class }) 051public class TestZKAsyncRegistry { 052 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestZKAsyncRegistry.class); 056 057 static final Logger LOG = LoggerFactory.getLogger(TestZKAsyncRegistry.class); 058 static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 059 060 private static ZKAsyncRegistry REGISTRY; 061 062 @BeforeClass 063 public static void setUp() throws Exception { 064 TEST_UTIL.getConfiguration().setInt(META_REPLICAS_NUM, 3); 065 TEST_UTIL.startMiniCluster(3); 066 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 067 // make sure that we do not depend on this config when getting locations for meta replicas, see 068 // HBASE-21658. 069 conf.setInt(META_REPLICAS_NUM, 1); 070 REGISTRY = new ZKAsyncRegistry(conf); 071 } 072 073 @AfterClass 074 public static void tearDown() throws Exception { 075 IOUtils.closeQuietly(REGISTRY); 076 TEST_UTIL.shutdownMiniCluster(); 077 } 078 079 @Test 080 public void test() throws InterruptedException, ExecutionException, IOException { 081 LOG.info("STARTED TEST"); 082 String clusterId = REGISTRY.getClusterId().get(); 083 String expectedClusterId = TEST_UTIL.getHBaseCluster().getMaster().getClusterId(); 084 assertEquals("Expected " + expectedClusterId + ", found=" + clusterId, expectedClusterId, 085 clusterId); 086 assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(), 087 REGISTRY.getMasterAddress().get()); 088 RegionReplicaTestHelper.waitUntilAllMetaReplicasHavingRegionLocation(REGISTRY, 3); 089 RegionLocations locs = REGISTRY.getMetaRegionLocation().get(); 090 assertEquals(3, locs.getRegionLocations().length); 091 IntStream.range(0, 3).forEach(i -> { 092 HRegionLocation loc = locs.getRegionLocation(i); 093 assertNotNull("Replica " + i + " doesn't have location", loc); 094 assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable()); 095 assertEquals(i, loc.getRegion().getReplicaId()); 096 }); 097 } 098 099 @Test 100 public void testIndependentZKConnections() throws IOException { 101 try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) { 102 Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration()); 103 otherConf.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1"); 104 try (ZKAsyncRegistry otherRegistry = new ZKAsyncRegistry(otherConf)) { 105 ReadOnlyZKClient zk2 = otherRegistry.getZKClient(); 106 assertNotSame("Using a different configuration / quorum should result in different " + 107 "backing zk connection.", zk1, zk2); 108 assertNotEquals( 109 "Using a different configrution / quorum should be reflected in the zk connection.", 110 zk1.getConnectString(), zk2.getConnectString()); 111 } 112 } finally { 113 LOG.info("DONE!"); 114 } 115 } 116 117 @Test 118 public void testNoMetaAvailable() throws InterruptedException { 119 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 120 conf.set("zookeeper.znode.metaserver", "whatever"); 121 try (ZKAsyncRegistry registry = new ZKAsyncRegistry(conf)) { 122 try { 123 registry.getMetaRegionLocation().get(); 124 fail("Should have failed since we set an incorrect meta znode prefix"); 125 } catch (ExecutionException e) { 126 assertThat(e.getCause(), instanceOf(IOException.class)); 127 } 128 } 129 } 130}