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