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;
028
029import java.io.IOException;
030import java.util.concurrent.ExecutionException;
031import java.util.stream.IntStream;
032import org.apache.commons.io.IOUtils;
033import org.apache.hadoop.conf.Configuration;
034import org.apache.hadoop.hbase.HBaseClassTestRule;
035import org.apache.hadoop.hbase.HBaseTestingUtility;
036import org.apache.hadoop.hbase.HConstants;
037import org.apache.hadoop.hbase.HRegionLocation;
038import org.apache.hadoop.hbase.RegionLocations;
039import org.apache.hadoop.hbase.TableName;
040import org.apache.hadoop.hbase.testclassification.ClientTests;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
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 TestZKAsyncRegistry {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestZKAsyncRegistry.class);
057
058  static final Logger LOG = LoggerFactory.getLogger(TestZKAsyncRegistry.class);
059  static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
060
061  private static ZKAsyncRegistry 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 ZKAsyncRegistry(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().getClusterMetrics().getLiveServerMetrics().size(),
088      REGISTRY.getCurrentNrHRS().get().intValue());
089    assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(),
090      REGISTRY.getMasterAddress().get());
091    assertEquals(-1, REGISTRY.getMasterInfoPort().get().intValue());
092    RegionReplicaTestHelper
093      .waitUntilAllMetaReplicasHavingRegionLocation(TEST_UTIL.getConfiguration(), REGISTRY, 3);
094    RegionLocations locs = REGISTRY.getMetaRegionLocation().get();
095    assertEquals(3, locs.getRegionLocations().length);
096    IntStream.range(0, 3).forEach(i -> {
097      HRegionLocation loc = locs.getRegionLocation(i);
098      assertNotNull("Replica " + i + " doesn't have location", loc);
099      assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable());
100      assertEquals(i, loc.getRegion().getReplicaId());
101    });
102  }
103
104  @Test
105  public void testIndependentZKConnections() throws IOException {
106    try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) {
107      Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration());
108      otherConf.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1");
109      try (ZKAsyncRegistry otherRegistry = new ZKAsyncRegistry(otherConf)) {
110        ReadOnlyZKClient zk2 = otherRegistry.getZKClient();
111        assertNotSame("Using a different configuration / quorum should result in different " +
112          "backing zk connection.", zk1, zk2);
113        assertNotEquals(
114          "Using a different configrution / quorum should be reflected in the zk connection.",
115          zk1.getConnectString(), zk2.getConnectString());
116      }
117    } finally {
118      LOG.info("DONE!");
119    }
120  }
121
122  @Test
123  public void testNoMetaAvailable() throws InterruptedException {
124    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
125    conf.set("zookeeper.znode.metaserver", "whatever");
126    try (ZKAsyncRegistry registry = new ZKAsyncRegistry(conf)) {
127      try {
128        registry.getMetaRegionLocation().get();
129        fail("Should have failed since we set an incorrect meta znode prefix");
130      } catch (ExecutionException e) {
131        assertThat(e.getCause(), instanceOf(IOException.class));
132      }
133    }
134  }
135}