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.concurrent.atomic.AtomicBoolean;
029import java.util.stream.IntStream;
030import org.apache.commons.io.IOUtils;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HBaseTestingUtility;
034import org.apache.hadoop.hbase.HConstants;
035import org.apache.hadoop.hbase.HRegionLocation;
036import org.apache.hadoop.hbase.RegionLocations;
037import org.apache.hadoop.hbase.TableName;
038import org.apache.hadoop.hbase.Waiter.ExplainingPredicate;
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  private static final Logger LOG = LoggerFactory.getLogger(TestZKAsyncRegistry.class);
058  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
059
060  private static ZKAsyncRegistry REGISTRY;
061
062  // waits for all replicas to have region location
063  static void waitUntilAllReplicasHavingRegionLocation(TableName tbl) throws IOException {
064    TEST_UTIL.waitFor(
065      TEST_UTIL.getConfiguration().getLong("hbase.client.sync.wait.timeout.msec", 60000), 200, true,
066      new ExplainingPredicate<IOException>() {
067        @Override
068        public String explainFailure() throws IOException {
069          return TEST_UTIL.explainTableAvailability(tbl);
070        }
071
072        @Override
073        public boolean evaluate() throws IOException {
074          AtomicBoolean ready = new AtomicBoolean(true);
075          try {
076            RegionLocations locs = REGISTRY.getMetaRegionLocation().get();
077            assertEquals(3, locs.getRegionLocations().length);
078            IntStream.range(0, 3).forEach(i -> {
079              HRegionLocation loc = locs.getRegionLocation(i);
080              if (loc == null) {
081                ready.set(false);
082              }
083            });
084          } catch (Exception e) {
085            ready.set(false);
086          }
087          return ready.get();
088        }
089      });
090  }
091
092  @BeforeClass
093  public static void setUp() throws Exception {
094    TEST_UTIL.getConfiguration().setInt(META_REPLICAS_NUM, 3);
095    TEST_UTIL.startMiniCluster(3);
096    REGISTRY = new ZKAsyncRegistry(TEST_UTIL.getConfiguration());
097  }
098
099  @AfterClass
100  public static void tearDown() throws Exception {
101    IOUtils.closeQuietly(REGISTRY);
102    TEST_UTIL.shutdownMiniCluster();
103  }
104
105  @Test
106  public void test() throws InterruptedException, ExecutionException, IOException {
107    LOG.info("STARTED TEST");
108    String clusterId = REGISTRY.getClusterId().get();
109    String expectedClusterId = TEST_UTIL.getHBaseCluster().getMaster().getClusterId();
110    assertEquals("Expected " + expectedClusterId + ", found=" + clusterId,
111        expectedClusterId, clusterId);
112    assertEquals(TEST_UTIL.getHBaseCluster().getClusterMetrics().getLiveServerMetrics().size(),
113      REGISTRY.getCurrentNrHRS().get().intValue());
114    assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(),
115      REGISTRY.getMasterAddress().get());
116    assertEquals(-1, REGISTRY.getMasterInfoPort().get().intValue());
117    waitUntilAllReplicasHavingRegionLocation(TableName.META_TABLE_NAME);
118    RegionLocations locs = REGISTRY.getMetaRegionLocation().get();
119    assertEquals(3, locs.getRegionLocations().length);
120    IntStream.range(0, 3).forEach(i -> {
121      HRegionLocation loc = locs.getRegionLocation(i);
122      assertNotNull("Replica " + i + " doesn't have location", loc);
123      assertEquals(TableName.META_TABLE_NAME, loc.getRegion().getTable());
124      assertEquals(i, loc.getRegion().getReplicaId());
125    });
126  }
127
128  @Test
129  public void testIndependentZKConnections() throws IOException {
130    try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) {
131      Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration());
132      otherConf.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1");
133      try (ZKAsyncRegistry otherRegistry = new ZKAsyncRegistry(otherConf)) {
134        ReadOnlyZKClient zk2 = otherRegistry.getZKClient();
135        assertNotSame("Using a different configuration / quorum should result in different " +
136          "backing zk connection.", zk1, zk2);
137        assertNotEquals(
138          "Using a different configrution / quorum should be reflected in the zk connection.",
139          zk1.getConnectString(), zk2.getConnectString());
140      }
141    } finally {
142      LOG.info("DONE!");
143    }
144  }
145}