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
.apache
.hadoop
.hbase
.HConstants
.META_REPLICAS_NUM
;
21 import static org
.hamcrest
.CoreMatchers
.instanceOf
;
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
.assertThat
;
27 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
.commons
.io
.IOUtils
;
32 import org
.apache
.hadoop
.conf
.Configuration
;
33 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
34 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
35 import org
.apache
.hadoop
.hbase
.HConstants
;
36 import org
.apache
.hadoop
.hbase
.HRegionLocation
;
37 import org
.apache
.hadoop
.hbase
.RegionLocations
;
38 import org
.apache
.hadoop
.hbase
.TableName
;
39 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
40 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
41 import org
.apache
.hadoop
.hbase
.zookeeper
.MiniZooKeeperCluster
;
42 import org
.apache
.hadoop
.hbase
.zookeeper
.ReadOnlyZKClient
;
43 import org
.junit
.AfterClass
;
44 import org
.junit
.BeforeClass
;
45 import org
.junit
.ClassRule
;
46 import org
.junit
.Test
;
47 import org
.junit
.experimental
.categories
.Category
;
48 import org
.slf4j
.Logger
;
49 import org
.slf4j
.LoggerFactory
;
51 @Category({ MediumTests
.class, ClientTests
.class })
52 public class TestZKConnectionRegistry
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestZKConnectionRegistry
.class);
58 static final Logger LOG
= LoggerFactory
.getLogger(TestZKConnectionRegistry
.class);
59 static final HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
61 private static ZKConnectionRegistry REGISTRY
;
64 public static void setUp() throws Exception
{
65 TEST_UTIL
.getConfiguration().setInt(META_REPLICAS_NUM
, 3);
66 TEST_UTIL
.startMiniCluster(3);
67 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
68 // make sure that we do not depend on this config when getting locations for meta replicas, see
70 conf
.setInt(META_REPLICAS_NUM
, 1);
71 REGISTRY
= new ZKConnectionRegistry(conf
);
75 public static void tearDown() throws Exception
{
76 IOUtils
.closeQuietly(REGISTRY
);
77 TEST_UTIL
.shutdownMiniCluster();
81 public void test() throws InterruptedException
, ExecutionException
, IOException
{
82 LOG
.info("STARTED TEST");
83 String clusterId
= REGISTRY
.getClusterId().get();
84 String expectedClusterId
= TEST_UTIL
.getHBaseCluster().getMaster().getClusterId();
85 assertEquals("Expected " + expectedClusterId
+ ", found=" + clusterId
, expectedClusterId
,
87 assertEquals(TEST_UTIL
.getHBaseCluster().getMaster().getServerName(),
88 REGISTRY
.getActiveMaster().get());
89 RegionReplicaTestHelper
90 .waitUntilAllMetaReplicasAreReady(TEST_UTIL
, REGISTRY
);
91 RegionLocations locs
= REGISTRY
.getMetaRegionLocations().get();
92 assertEquals(3, locs
.getRegionLocations().length
);
93 IntStream
.range(0, 3).forEach(i
-> {
94 HRegionLocation loc
= locs
.getRegionLocation(i
);
95 assertNotNull("Replica " + i
+ " doesn't have location", loc
);
96 assertEquals(TableName
.META_TABLE_NAME
, loc
.getRegion().getTable());
97 assertEquals(i
, loc
.getRegion().getReplicaId());
102 public void testIndependentZKConnections() throws IOException
{
103 try (ReadOnlyZKClient zk1
= REGISTRY
.getZKClient()) {
104 Configuration otherConf
= new Configuration(TEST_UTIL
.getConfiguration());
105 otherConf
.set(HConstants
.ZOOKEEPER_QUORUM
, MiniZooKeeperCluster
.HOST
);
106 try (ZKConnectionRegistry otherRegistry
= new ZKConnectionRegistry(otherConf
)) {
107 ReadOnlyZKClient zk2
= otherRegistry
.getZKClient();
108 assertNotSame("Using a different configuration / quorum should result in different " +
109 "backing zk connection.", zk1
, zk2
);
111 "Using a different configrution / quorum should be reflected in the zk connection.",
112 zk1
.getConnectString(), zk2
.getConnectString());
120 public void testNoMetaAvailable() throws InterruptedException
{
121 Configuration conf
= new Configuration(TEST_UTIL
.getConfiguration());
122 conf
.set("zookeeper.znode.metaserver", "whatever");
123 try (ZKConnectionRegistry registry
= new ZKConnectionRegistry(conf
)) {
125 registry
.getMetaRegionLocations().get();
126 fail("Should have failed since we set an incorrect meta znode prefix");
127 } catch (ExecutionException e
) {
128 assertThat(e
.getCause(), instanceOf(IOException
.class));