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
089      .waitUntilAllMetaReplicasHavingRegionLocation(TEST_UTIL.getConfiguration(), REGISTRY, 3);
090    RegionLocations locs = REGISTRY.getMetaRegionLocation().get();
091    assertEquals(3, locs.getRegionLocations().length);
092    IntStream.range(0, 3).forEach(i -> {
093      HRegionLocation loc = locs.getRegionLocation(i);
094      assertNotNull("Replica " + i + " doesn't have location", loc);
095      assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable());
096      assertEquals(i, loc.getRegion().getReplicaId());
097    });
098  }
099
100  @Test
101  public void testIndependentZKConnections() throws IOException {
102    try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) {
103      Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration());
104      otherConf.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1");
105      try (ZKAsyncRegistry otherRegistry = new ZKAsyncRegistry(otherConf)) {
106        ReadOnlyZKClient zk2 = otherRegistry.getZKClient();
107        assertNotSame("Using a different configuration / quorum should result in different " +
108          "backing zk connection.", zk1, zk2);
109        assertNotEquals(
110          "Using a different configrution / quorum should be reflected in the zk connection.",
111          zk1.getConnectString(), zk2.getConnectString());
112      }
113    } finally {
114      LOG.info("DONE!");
115    }
116  }
117
118  @Test
119  public void testNoMetaAvailable() throws InterruptedException {
120    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
121    conf.set("zookeeper.znode.metaserver", "whatever");
122    try (ZKAsyncRegistry registry = new ZKAsyncRegistry(conf)) {
123      try {
124        registry.getMetaRegionLocation().get();
125        fail("Should have failed since we set an incorrect meta znode prefix");
126      } catch (ExecutionException e) {
127        assertThat(e.getCause(), instanceOf(IOException.class));
128      }
129    }
130  }
131}