2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import static org
.hamcrest
.CoreMatchers
.instanceOf
;
21 import static org
.hamcrest
.MatcherAssert
.assertThat
;
22 import static org
.junit
.Assert
.assertEquals
;
23 import static org
.junit
.Assert
.assertNotEquals
;
24 import static org
.junit
.Assert
.assertNotNull
;
25 import static org
.junit
.Assert
.assertNotSame
;
26 import static org
.junit
.Assert
.fail
;
28 import java
.io
.IOException
;
29 import java
.util
.concurrent
.ExecutionException
;
30 import java
.util
.stream
.IntStream
;
31 import org
.apache
.hadoop
.conf
.Configuration
;
32 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
33 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
34 import org
.apache
.hadoop
.hbase
.HConstants
;
35 import org
.apache
.hadoop
.hbase
.HRegionLocation
;
36 import org
.apache
.hadoop
.hbase
.RegionLocations
;
37 import org
.apache
.hadoop
.hbase
.TableName
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
39 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
40 import org
.apache
.hadoop
.hbase
.zookeeper
.MiniZooKeeperCluster
;
41 import org
.apache
.hadoop
.hbase
.zookeeper
.ReadOnlyZKClient
;
42 import org
.junit
.AfterClass
;
43 import org
.junit
.BeforeClass
;
44 import org
.junit
.ClassRule
;
45 import org
.junit
.Test
;
46 import org
.junit
.experimental
.categories
.Category
;
47 import org
.slf4j
.Logger
;
48 import org
.slf4j
.LoggerFactory
;
50 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.io
.Closeables
;
52 @Category({ MediumTests
.class, ClientTests
.class })
53 public class TestZKConnectionRegistry
{
56 public static final HBaseClassTestRule CLASS_RULE
=
57 HBaseClassTestRule
.forClass(TestZKConnectionRegistry
.class);
59 static final Logger LOG
= LoggerFactory
.getLogger(TestZKConnectionRegistry
.class);
60 static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
62 private static ZKConnectionRegistry REGISTRY
;
65 public static void setUp() throws Exception
{
66 TEST_UTIL
.startMiniCluster(3);
67 HBaseTestingUtil
.setReplicas(TEST_UTIL
.getAdmin(), TableName
.META_TABLE_NAME
, 3);
68 REGISTRY
= new ZKConnectionRegistry(TEST_UTIL
.getConfiguration());
72 public static void tearDown() throws Exception
{
73 Closeables
.close(REGISTRY
, true);
74 TEST_UTIL
.shutdownMiniCluster();
78 public void test() throws InterruptedException
, ExecutionException
, IOException
{
79 LOG
.info("STARTED TEST");
80 String clusterId
= REGISTRY
.getClusterId().get();
81 String expectedClusterId
= TEST_UTIL
.getHBaseCluster().getMaster().getClusterId();
82 assertEquals("Expected " + expectedClusterId
+ ", found=" + clusterId
, expectedClusterId
,
84 assertEquals(TEST_UTIL
.getHBaseCluster().getMaster().getServerName(),
85 REGISTRY
.getActiveMaster().get());
86 RegionReplicaTestHelper
87 .waitUntilAllMetaReplicasAreReady(TEST_UTIL
, REGISTRY
);
88 RegionLocations locs
= REGISTRY
.getMetaRegionLocations().get();
89 assertEquals(3, locs
.getRegionLocations().length
);
90 IntStream
.range(0, 3).forEach(i
-> {
91 HRegionLocation loc
= locs
.getRegionLocation(i
);
92 assertNotNull("Replica " + i
+ " doesn't have location", loc
);
93 assertEquals(TableName
.META_TABLE_NAME
, loc
.getRegion().getTable());
94 assertEquals(i
, loc
.getRegion().getReplicaId());
99 public void testIndependentZKConnections() throws IOException
{
100 try (ReadOnlyZKClient zk1
= REGISTRY
.getZKClient()) {
101 Configuration otherConf
= new Configuration(TEST_UTIL
.getConfiguration());
102 otherConf
.set(HConstants
.ZOOKEEPER_QUORUM
, MiniZooKeeperCluster
.HOST
);
103 try (ZKConnectionRegistry otherRegistry
= new ZKConnectionRegistry(otherConf
)) {
104 ReadOnlyZKClient zk2
= otherRegistry
.getZKClient();
105 assertNotSame("Using a different configuration / quorum should result in different " +
106 "backing zk connection.", zk1
, zk2
);
108 "Using a different configrution / quorum should be reflected in the zk connection.",
109 zk1
.getConnectString(), zk2
.getConnectString());
117 public void testNoMetaAvailable() throws InterruptedException
{
118 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
119 conf
.set("zookeeper.znode.metaserver", "whatever");
120 try (ZKConnectionRegistry registry
= new ZKConnectionRegistry(conf
)) {
122 registry
.getMetaRegionLocations().get();
123 fail("Should have failed since we set an incorrect meta znode prefix");
124 } catch (ExecutionException e
) {
125 assertThat(e
.getCause(), instanceOf(IOException
.class));