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.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotEquals;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertNotSame;
025
026import java.io.IOException;
027import java.util.concurrent.ExecutionException;
028import java.util.stream.IntStream;
029import org.apache.commons.io.IOUtils;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
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.ReadOnlyZKClient;
040import org.junit.AfterClass;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048@Category({ MediumTests.class, ClientTests.class })
049public class TestZKAsyncRegistry {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestZKAsyncRegistry.class);
054
055  static final Logger LOG = LoggerFactory.getLogger(TestZKAsyncRegistry.class);
056  static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
057
058  private static ZKAsyncRegistry REGISTRY;
059
060  @BeforeClass
061  public static void setUp() throws Exception {
062    TEST_UTIL.getConfiguration().setInt(META_REPLICAS_NUM, 3);
063    TEST_UTIL.startMiniCluster(3);
064    REGISTRY = new ZKAsyncRegistry(TEST_UTIL.getConfiguration());
065  }
066
067  @AfterClass
068  public static void tearDown() throws Exception {
069    IOUtils.closeQuietly(REGISTRY);
070    TEST_UTIL.shutdownMiniCluster();
071  }
072
073  @Test
074  public void test() throws InterruptedException, ExecutionException, IOException {
075    LOG.info("STARTED TEST");
076    String clusterId = REGISTRY.getClusterId().get();
077    String expectedClusterId = TEST_UTIL.getHBaseCluster().getMaster().getClusterId();
078    assertEquals("Expected " + expectedClusterId + ", found=" + clusterId, expectedClusterId,
079      clusterId);
080    assertEquals(TEST_UTIL.getHBaseCluster().getClusterMetrics().getLiveServerMetrics().size(),
081      REGISTRY.getCurrentNrHRS().get().intValue());
082    assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(),
083      REGISTRY.getMasterAddress().get());
084    assertEquals(-1, REGISTRY.getMasterInfoPort().get().intValue());
085    RegionReplicaTestHelper.waitUntilAllMetaReplicasHavingRegionLocation(REGISTRY, 3);
086    RegionLocations locs = REGISTRY.getMetaRegionLocation().get();
087    assertEquals(3, locs.getRegionLocations().length);
088    IntStream.range(0, 3).forEach(i -> {
089      HRegionLocation loc = locs.getRegionLocation(i);
090      assertNotNull("Replica " + i + " doesn't have location", loc);
091      assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable());
092      assertEquals(i, loc.getRegion().getReplicaId());
093    });
094  }
095
096  @Test
097  public void testIndependentZKConnections() throws IOException {
098    try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) {
099      Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration());
100      otherConf.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1");
101      try (ZKAsyncRegistry otherRegistry = new ZKAsyncRegistry(otherConf)) {
102        ReadOnlyZKClient zk2 = otherRegistry.getZKClient();
103        assertNotSame("Using a different configuration / quorum should result in different " +
104          "backing zk connection.", zk1, zk2);
105        assertNotEquals(
106          "Using a different configrution / quorum should be reflected in the zk connection.",
107          zk1.getConnectString(), zk2.getConnectString());
108      }
109    } finally {
110      LOG.info("DONE!");
111    }
112  }
113}