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.hamcrest.CoreMatchers.instanceOf; 021import static org.hamcrest.MatcherAssert.assertThat; 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.fail; 027 028import java.io.IOException; 029import java.util.concurrent.ExecutionException; 030import java.util.stream.IntStream; 031import org.apache.hadoop.conf.Configuration; 032import org.apache.hadoop.hbase.HBaseClassTestRule; 033import org.apache.hadoop.hbase.HBaseTestingUtil; 034import org.apache.hadoop.hbase.HConstants; 035import org.apache.hadoop.hbase.HRegionLocation; 036import org.apache.hadoop.hbase.RegionLocations; 037import org.apache.hadoop.hbase.TableName; 038import org.apache.hadoop.hbase.testclassification.ClientTests; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 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 050import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 051 052@Category({ MediumTests.class, ClientTests.class }) 053public class TestZKConnectionRegistry { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestZKConnectionRegistry.class); 058 059 static final Logger LOG = LoggerFactory.getLogger(TestZKConnectionRegistry.class); 060 static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 061 062 private static ZKConnectionRegistry REGISTRY; 063 064 @BeforeClass 065 public static void setUp() throws Exception { 066 TEST_UTIL.startMiniCluster(3); 067 HBaseTestingUtil.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3); 068 REGISTRY = new ZKConnectionRegistry(TEST_UTIL.getConfiguration(), null); 069 } 070 071 @AfterClass 072 public static void tearDown() throws Exception { 073 Closeables.close(REGISTRY, true); 074 TEST_UTIL.shutdownMiniCluster(); 075 } 076 077 @Test 078 public void test() throws InterruptedException, ExecutionException, IOException { 079 LOG.info("STARTED TEST"); 080 String clusterId = REGISTRY.getClusterId().get(); 081 String expectedClusterId = TEST_UTIL.getHBaseCluster().getMaster().getClusterId(); 082 assertEquals("Expected " + expectedClusterId + ", found=" + clusterId, expectedClusterId, 083 clusterId); 084 assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(), 085 REGISTRY.getActiveMaster().get()); 086 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, REGISTRY); 087 RegionLocations locs = REGISTRY.getMetaRegionLocations().get(); 088 assertEquals(3, locs.getRegionLocations().length); 089 IntStream.range(0, 3).forEach(i -> { 090 HRegionLocation loc = locs.getRegionLocation(i); 091 assertNotNull("Replica " + i + " doesn't have location", loc); 092 assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable()); 093 assertEquals(i, loc.getRegion().getReplicaId()); 094 }); 095 } 096 097 @Test 098 public void testIndependentZKConnections() throws IOException { 099 try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) { 100 Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration()); 101 otherConf.set(HConstants.ZOOKEEPER_QUORUM, MiniZooKeeperCluster.HOST); 102 try (ZKConnectionRegistry otherRegistry = new ZKConnectionRegistry(otherConf, null)) { 103 ReadOnlyZKClient zk2 = otherRegistry.getZKClient(); 104 assertNotSame("Using a different configuration / quorum should result in different " 105 + "backing zk connection.", zk1, zk2); 106 assertNotEquals( 107 "Using a different configrution / quorum should be reflected in the zk connection.", 108 zk1.getConnectString(), zk2.getConnectString()); 109 } 110 } finally { 111 LOG.info("DONE!"); 112 } 113 } 114 115 @Test 116 public void testNoMetaAvailable() throws InterruptedException { 117 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 118 conf.set("zookeeper.znode.metaserver", "whatever"); 119 try (ZKConnectionRegistry registry = new ZKConnectionRegistry(conf, null)) { 120 try { 121 registry.getMetaRegionLocations().get(); 122 fail("Should have failed since we set an incorrect meta znode prefix"); 123 } catch (ExecutionException e) { 124 assertThat(e.getCause(), instanceOf(IOException.class)); 125 } 126 } 127 } 128}