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.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNotEquals; 023import static org.junit.Assert.assertNotNull; 024import static org.junit.Assert.assertNotSame; 025import static org.junit.Assert.assertThat; 026import static org.junit.Assert.fail; 027import java.io.IOException; 028import java.util.List; 029import java.util.concurrent.CompletableFuture; 030import java.util.concurrent.ExecutionException; 031import java.util.concurrent.TimeUnit; 032import java.util.concurrent.TimeoutException; 033import java.util.stream.IntStream; 034import org.apache.commons.io.IOUtils; 035import org.apache.hadoop.conf.Configuration; 036import org.apache.hadoop.hbase.Abortable; 037import org.apache.hadoop.hbase.HBaseClassTestRule; 038import org.apache.hadoop.hbase.HBaseTestingUtility; 039import org.apache.hadoop.hbase.HConstants; 040import org.apache.hadoop.hbase.HRegionLocation; 041import org.apache.hadoop.hbase.RegionLocations; 042import org.apache.hadoop.hbase.ServerName; 043import org.apache.hadoop.hbase.TableName; 044import org.apache.hadoop.hbase.master.RegionState; 045import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos; 047import org.apache.hadoop.hbase.testclassification.ClientTests; 048import org.apache.hadoop.hbase.testclassification.MediumTests; 049import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 050import org.apache.hadoop.hbase.zookeeper.ReadOnlyZKClient; 051import org.apache.hadoop.hbase.zookeeper.ZKUtil; 052import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 053import org.apache.zookeeper.KeeperException; 054import org.junit.AfterClass; 055import org.junit.BeforeClass; 056import org.junit.ClassRule; 057import org.junit.Rule; 058import org.junit.Test; 059import org.junit.experimental.categories.Category; 060import org.junit.rules.TestName; 061import org.slf4j.Logger; 062import org.slf4j.LoggerFactory; 063 064@Category({ MediumTests.class, ClientTests.class }) 065public class TestZKConnectionRegistry { 066 067 @ClassRule 068 public static final HBaseClassTestRule CLASS_RULE = 069 HBaseClassTestRule.forClass(TestZKConnectionRegistry.class); 070 071 @Rule 072 public final TestName name = new TestName(); 073 074 static final Logger LOG = LoggerFactory.getLogger(TestZKConnectionRegistry.class); 075 static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 076 077 private static ZKConnectionRegistry REGISTRY; 078 079 @BeforeClass 080 public static void setUp() throws Exception { 081 TEST_UTIL.startMiniCluster(3); 082 HBaseTestingUtility.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3); 083 REGISTRY = new ZKConnectionRegistry(TEST_UTIL.getConfiguration()); 084 } 085 086 @AfterClass 087 public static void tearDown() throws Exception { 088 IOUtils.closeQuietly(REGISTRY); 089 TEST_UTIL.shutdownMiniCluster(); 090 } 091 092 @Test 093 public void test() throws InterruptedException, ExecutionException, IOException { 094 LOG.info("STARTED TEST"); 095 String clusterId = REGISTRY.getClusterId().get(); 096 String expectedClusterId = TEST_UTIL.getHBaseCluster().getMaster().getClusterId(); 097 assertEquals("Expected " + expectedClusterId + ", found=" + clusterId, expectedClusterId, 098 clusterId); 099 assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(), 100 REGISTRY.getActiveMaster().get()); 101 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, REGISTRY); 102 RegionLocations locs = REGISTRY.getMetaRegionLocations().get(); 103 assertEquals(3, locs.getRegionLocations().length); 104 IntStream.range(0, 3).forEach(i -> { 105 HRegionLocation loc = locs.getRegionLocation(i); 106 assertNotNull("Replica " + i + " doesn't have location", loc); 107 assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable()); 108 assertEquals(i, loc.getRegion().getReplicaId()); 109 }); 110 } 111 112 @Test 113 public void testIndependentZKConnections() throws IOException { 114 try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) { 115 Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration()); 116 otherConf.set(HConstants.ZOOKEEPER_QUORUM, MiniZooKeeperCluster.HOST); 117 try (ZKConnectionRegistry otherRegistry = new ZKConnectionRegistry(otherConf)) { 118 ReadOnlyZKClient zk2 = otherRegistry.getZKClient(); 119 assertNotSame("Using a different configuration / quorum should result in " + 120 "different backing zk connection.", zk1, zk2); 121 assertNotEquals( 122 "Using a different configrution / quorum should be reflected in the zk connection.", 123 zk1.getConnectString(), zk2.getConnectString()); 124 } 125 } finally { 126 LOG.info("DONE!"); 127 } 128 } 129 130 @Test 131 public void testNoMetaAvailable() throws InterruptedException { 132 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 133 conf.set("zookeeper.znode.metaserver", "whatever"); 134 try (ZKConnectionRegistry registry = new ZKConnectionRegistry(conf)) { 135 try { 136 registry.getMetaRegionLocations().get(); 137 fail("Should have failed since we set an incorrect meta znode prefix"); 138 } catch (ExecutionException e) { 139 assertThat(e.getCause(), instanceOf(IOException.class)); 140 } 141 } 142 } 143 144 /** 145 * Pass discontinuous list of znodes to registry getMetaRegionLocation. Should work fine. 146 * It used to throw ArrayOutOfBoundsException. See HBASE-25280. 147 */ 148 @Test 149 public void testDiscontinuousLocations() 150 throws ExecutionException, InterruptedException, IOException, KeeperException, 151 TimeoutException { 152 // Write discontinuous meta replica locations to a zk namespace particular to this test to 153 // avoid polluting other tests. 154 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 155 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/" + this.name.getMethodName()); 156 ZooKeeperProtos.MetaRegionServer pbrsr = ZooKeeperProtos.MetaRegionServer.newBuilder() 157 .setServer(ProtobufUtil.toServerName(ServerName.valueOf("example.org,1,1"))) 158 .setRpcVersion(HConstants.RPC_CURRENT_VERSION) 159 .setState(RegionState.State.OPEN.convert()).build(); 160 byte[] data = ProtobufUtil.prependPBMagic(pbrsr.toByteArray()); 161 try (ZKWatcher zkw = new ZKWatcher(conf, this.name.getMethodName(), new Abortable() { 162 @Override public void abort(String why, Throwable e) {} 163 @Override public boolean isAborted() { 164 return false; 165 } 166 })) { 167 // Write default replica and then a replica for replicaId #3. 168 ZKUtil.createSetData(zkw, zkw.getZNodePaths().getZNodeForReplica(0), data); 169 ZKUtil.createSetData(zkw, zkw.getZNodePaths().getZNodeForReplica(3), data); 170 List<String> znodes = zkw.getMetaReplicaNodes(); 171 assertEquals(2, znodes.size()); 172 try (ZKConnectionRegistry registry = new ZKConnectionRegistry(conf)) { 173 CompletableFuture<RegionLocations> cf = registry.getMetaRegionLocations(); 174 RegionLocations locations = cf.get(60, TimeUnit.SECONDS); 175 assertEquals(2, locations.numNonNullElements()); 176 } 177 } 178 } 179}