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