Skip to content

Commit d5fc77c

Browse files
committed
HBASE-23993 Use loopback for zk standalone server in minizkcluster
hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java Have client and server use loopback instead of 'localhost'
1 parent bb8e13f commit d5fc77c

File tree

10 files changed

+40
-32
lines changed

10 files changed

+40
-32
lines changed

hbase-examples/src/test/java/org/apache/hadoop/hbase/coprocessor/example/TestZooKeeperScanPolicyObserver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void setUp() throws Exception {
6666
.createTable(TableDescriptorBuilder.newBuilder(NAME)
6767
.setCoprocessor(ZooKeeperScanPolicyObserver.class.getName())
6868
.setValue(ZooKeeperScanPolicyObserver.ZK_ENSEMBLE_KEY,
69-
"localhost:" + UTIL.getZkCluster().getClientPort())
69+
UTIL.getZkCluster().getAddress().toString())
7070
.setValue(ZooKeeperScanPolicyObserver.ZK_SESSION_TIMEOUT_KEY, "2000")
7171
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()).build());
7272
TABLE = UTIL.getConnection().getTable(NAME);

hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationStateBasic.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -33,6 +33,7 @@
3333
import org.apache.hadoop.hbase.ServerName;
3434
import org.apache.hadoop.hbase.replication.ReplicationPeer.PeerState;
3535
import org.apache.hadoop.hbase.util.Pair;
36+
import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
3637
import org.apache.hadoop.hbase.zookeeper.ZKConfig;
3738
import org.apache.zookeeper.KeeperException;
3839
import org.junit.Test;
@@ -364,8 +365,8 @@ protected void populateQueues() throws ReplicationException {
364365
}
365366
// Add peers for the corresponding queues so they are not orphans
366367
rp.getPeerStorage().addPeer("qId" + i,
367-
ReplicationPeerConfig.newBuilder().setClusterKey("localhost:2818:/bogus" + i).build(),
368-
true);
368+
ReplicationPeerConfig.newBuilder().
369+
setClusterKey(MiniZooKeeperCluster.HOST + ":2818:/bogus" + i).build(), true);
369370
}
370371
}
371372
}

hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestZKConnectionRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.hadoop.hbase.TableName;
3939
import org.apache.hadoop.hbase.testclassification.ClientTests;
4040
import org.apache.hadoop.hbase.testclassification.MediumTests;
41+
import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
4142
import org.apache.hadoop.hbase.zookeeper.ReadOnlyZKClient;
4243
import org.junit.AfterClass;
4344
import org.junit.BeforeClass;
@@ -101,7 +102,7 @@ public void test() throws InterruptedException, ExecutionException, IOException
101102
public void testIndependentZKConnections() throws IOException {
102103
try (ReadOnlyZKClient zk1 = REGISTRY.getZKClient()) {
103104
Configuration otherConf = new Configuration(TEST_UTIL.getConfiguration());
104-
otherConf.set(HConstants.ZOOKEEPER_QUORUM, "localhost");
105+
otherConf.set(HConstants.ZOOKEEPER_QUORUM, MiniZooKeeperCluster.HOST);
105106
try (ZKConnectionRegistry otherRegistry = new ZKConnectionRegistry(otherConf)) {
106107
ReadOnlyZKClient zk2 = otherRegistry.getZKClient();
107108
assertNotSame("Using a different configuration / quorum should result in different " +

hbase-server/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationProcedureRetry.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.mockito.ArgumentMatchers.anyString;
2626
import static org.mockito.Mockito.doAnswer;
2727
import static org.mockito.Mockito.spy;
28-
2928
import java.io.IOException;
3029
import org.apache.hadoop.conf.Configuration;
3130
import org.apache.hadoop.hbase.HBaseClassTestRule;
@@ -78,7 +77,7 @@ private void doTest() throws IOException {
7877
Admin admin = UTIL.getAdmin();
7978
String peerId = "1";
8079
ReplicationPeerConfig peerConfig = ReplicationPeerConfig.newBuilder()
81-
.setClusterKey("localhost:" + UTIL.getZkCluster().getClientPort() + ":/hbase2").build();
80+
.setClusterKey(UTIL.getZkCluster().getAddress().toString() + ":/hbase2").build();
8281
admin.addReplicationPeer(peerId, peerConfig, true);
8382

8483
assertEquals(peerConfig.getClusterKey(),

hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.apache.hadoop.hbase.wal.WALEdit;
8484
import org.apache.hadoop.hbase.wal.WALFactory;
8585
import org.apache.hadoop.hbase.wal.WALKeyImpl;
86+
import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
8687
import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
8788
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
8889
import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
@@ -485,7 +486,7 @@ public void testPeerRemovalCleanup() throws Exception{
485486
String replicationSourceImplName = conf.get("replication.replicationsource.implementation");
486487
final String peerId = "FakePeer";
487488
final ReplicationPeerConfig peerConfig = new ReplicationPeerConfig()
488-
.setClusterKey("localhost:" + utility.getZkCluster().getClientPort() + ":/hbase");
489+
.setClusterKey(utility.getZkCluster().getAddress().toString() + ":/hbase");
489490
try {
490491
DummyServer server = new DummyServer();
491492
ReplicationQueueStorage rq = ReplicationStorageFactory
@@ -540,7 +541,7 @@ private static long getSizeOfLatestPath() {
540541
public void testRemovePeerMetricsCleanup() throws Exception {
541542
final String peerId = "DummyPeer";
542543
final ReplicationPeerConfig peerConfig = new ReplicationPeerConfig()
543-
.setClusterKey("localhost:" + utility.getZkCluster().getClientPort() + ":/hbase");
544+
.setClusterKey(utility.getZkCluster().getAddress().toString() + ":/hbase");
544545
try {
545546
MetricsReplicationSourceSource globalSource = getGlobalSource();
546547
final int globalLogQueueSizeInitial = globalSource.getSizeOfLogQueue();

hbase-server/src/test/java/org/apache/hadoop/hbase/tool/TestCanaryTool.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static org.mockito.Mockito.spy;
3232
import static org.mockito.Mockito.times;
3333
import static org.mockito.Mockito.verify;
34-
3534
import java.util.List;
3635
import java.util.Map;
3736
import java.util.concurrent.ExecutorService;
@@ -62,7 +61,6 @@
6261
import org.mockito.ArgumentMatcher;
6362
import org.mockito.Mock;
6463
import org.mockito.runners.MockitoJUnitRunner;
65-
6664
import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
6765

6866
@RunWith(MockitoJUnitRunner.class)
@@ -309,16 +307,15 @@ private void runRegionserverCanary() throws Exception {
309307
private void testZookeeperCanaryWithArgs(String[] args) throws Exception {
310308
Integer port =
311309
Iterables.getOnlyElement(testingUtility.getZkCluster().getClientPortList(), null);
312-
testingUtility.getConfiguration().set(HConstants.ZOOKEEPER_QUORUM,
313-
"localhost:" + port + "/hbase");
310+
String hostPort = testingUtility.getZkCluster().getAddress().toString();
311+
testingUtility.getConfiguration().set(HConstants.ZOOKEEPER_QUORUM, hostPort + "/hbase");
314312
ExecutorService executor = new ScheduledThreadPoolExecutor(2);
315313
CanaryTool.ZookeeperStdOutSink sink = spy(new CanaryTool.ZookeeperStdOutSink());
316314
CanaryTool canary = new CanaryTool(executor, sink);
317315
assertEquals(0, ToolRunner.run(testingUtility.getConfiguration(), canary, args));
318316

319317
String baseZnode = testingUtility.getConfiguration()
320318
.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
321-
verify(sink, atLeastOnce())
322-
.publishReadTiming(eq(baseZnode), eq("localhost:" + port), anyLong());
319+
verify(sink, atLeastOnce()).publishReadTiming(eq(baseZnode), eq(hostPort), anyLong());
323320
}
324321
}

hbase-shell/src/test/ruby/hbase/replication_admin_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def assert_tablecfs_equal(table_cfs, table_cfs_map)
575575
end
576576

577577
define_test "set_peer_bandwidth: works with peer bandwidth upper limit" do
578-
cluster_key = "localhost:2181:/hbase-test"
578+
cluster_key = MiniZooKeeperCluster::HOST + ":2181:/hbase-test"
579579
args = { CLUSTER_KEY => cluster_key }
580580
command(:add_peer, @peer_id, args)
581581

@@ -590,7 +590,7 @@ def assert_tablecfs_equal(table_cfs, table_cfs_map)
590590
end
591591

592592
define_test "get_peer_config: works with simple clusterKey peer" do
593-
cluster_key = "localhost:2181:/hbase-test"
593+
cluster_key = MiniZooKeeperCluster::HOST + ":2181:/hbase-test"
594594
args = { CLUSTER_KEY => cluster_key }
595595
command(:add_peer, @peer_id, args)
596596
peer_config = command(:get_peer_config, @peer_id)
@@ -600,7 +600,7 @@ def assert_tablecfs_equal(table_cfs, table_cfs_map)
600600
end
601601

602602
define_test "get_peer_config: works with replicationendpointimpl peer and config params" do
603-
cluster_key = 'localhost:2181:/hbase-test'
603+
cluster_key = MiniZooKeeperCluster::HOST + ":2181:/hbase-test"
604604
repl_impl = 'org.apache.hadoop.hbase.replication.DummyReplicationEndpoint'
605605
config_params = { "config1" => "value1", "config2" => "value2" }
606606
args = { CLUSTER_KEY => cluster_key, ENDPOINT_CLASSNAME => repl_impl,
@@ -616,7 +616,7 @@ def assert_tablecfs_equal(table_cfs, table_cfs_map)
616616
end
617617

618618
define_test "list_peer_configs: returns all peers' ReplicationPeerConfig objects" do
619-
cluster_key = "localhost:2181:/hbase-test"
619+
cluster_key = MiniZooKeeperCluster::HOST + ":2181:/hbase-test"
620620
args = { CLUSTER_KEY => cluster_key }
621621
peer_id_second = '2'
622622
command(:add_peer, @peer_id, args)

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import java.io.StringWriter;
2626
import java.net.BindException;
2727
import java.net.ConnectException;
28+
import java.net.InetAddress;
2829
import java.net.InetSocketAddress;
2930
import java.util.ArrayList;
3031
import java.util.List;
3132
import java.util.Random;
3233
import org.apache.hadoop.conf.Configuration;
3334
import org.apache.hadoop.hbase.HConstants;
35+
import org.apache.hadoop.hbase.net.Address;
3436
import org.apache.hadoop.hbase.util.Threads;
3537
import org.apache.yetus.audience.InterfaceAudience;
3638
import org.apache.zookeeper.server.NIOServerCnxnFactory;
@@ -48,11 +50,12 @@
4850
@InterfaceAudience.Public
4951
public class MiniZooKeeperCluster {
5052
private static final Logger LOG = LoggerFactory.getLogger(MiniZooKeeperCluster.class);
51-
5253
private static final int TICK_TIME = 2000;
5354
private static final int TIMEOUT = 1000;
5455
private static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
5556
private int connectionTimeout;
57+
public static final String LOOPBACK_HOST = InetAddress.getLoopbackAddress().getHostName();
58+
public static final String HOST = LOOPBACK_HOST;
5659

5760
private boolean started;
5861

@@ -234,7 +237,7 @@ public int startup(File baseDir, int numZooKeeperServers)
234237
while (true) {
235238
try {
236239
standaloneServerFactory = new NIOServerCnxnFactory();
237-
standaloneServerFactory.configure(new InetSocketAddress(currentClientPort),
240+
standaloneServerFactory.configure(new InetSocketAddress(LOOPBACK_HOST, currentClientPort),
238241
configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS,
239242
HConstants.DEFAULT_ZOOKEEPER_MAX_CLIENT_CNXNS));
240243
} catch (BindException e) {
@@ -416,7 +419,7 @@ private static boolean waitForServerDown(int port, long timeout) throws IOExcept
416419
long start = System.currentTimeMillis();
417420
while (true) {
418421
try {
419-
send4LetterWord("localhost", port, "stat", (int)timeout);
422+
send4LetterWord(HOST, port, "stat", (int)timeout);
420423
} catch (IOException e) {
421424
return true;
422425
}
@@ -439,18 +442,18 @@ private static boolean waitForServerUp(int port, long timeout) throws IOExceptio
439442
long start = System.currentTimeMillis();
440443
while (true) {
441444
try {
442-
String result = send4LetterWord("localhost", port, "stat", (int)timeout);
445+
String result = send4LetterWord(HOST, port, "stat", (int)timeout);
443446
if (result.startsWith("Zookeeper version:") && !result.contains("READ-ONLY")) {
444447
return true;
445448
} else {
446449
LOG.debug("Read {}", result);
447450
}
448451
} catch (ConnectException e) {
449452
// ignore as this is expected, do not log stacktrace
450-
LOG.info("localhost:{} not up: {}", port, e.toString());
453+
LOG.info("{}:{} not up: {}", HOST, port, e.toString());
451454
} catch (IOException e) {
452455
// ignore as this is expected
453-
LOG.info("localhost:{} not up", port, e);
456+
LOG.info("{}:{} not up", HOST, port, e);
454457
}
455458

456459
if (System.currentTimeMillis() > start + timeout) {
@@ -470,6 +473,13 @@ public int getClientPort() {
470473
: clientPortList.get(activeZKServerIndex);
471474
}
472475

476+
/**
477+
* @return Address for this cluster instance.
478+
*/
479+
public Address getAddress() {
480+
return Address.fromParts(HOST, getClientPort());
481+
}
482+
473483
List<ZooKeeperServer> getZooKeeperServers() {
474484
return zooKeeperServers;
475485
}

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestReadOnlyZKClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -36,7 +36,6 @@
3636
import static org.mockito.Mockito.times;
3737
import static org.mockito.Mockito.verify;
3838
import static org.mockito.Mockito.when;
39-
4039
import java.io.IOException;
4140
import java.util.Collections;
4241
import java.util.List;
@@ -83,8 +82,9 @@ public class TestReadOnlyZKClient {
8382
@BeforeClass
8483
public static void setUp() throws Exception {
8584
final int port = UTIL.startMiniZKCluster().getClientPort();
85+
String hostPort = UTIL.getZkCluster().getAddress().toString();
8686

87-
ZooKeeper zk = ZooKeeperHelper.getConnectedZooKeeper("localhost:" + port, 10000);
87+
ZooKeeper zk = ZooKeeperHelper.getConnectedZooKeeper(hostPort, 10000);
8888
DATA = new byte[10];
8989
ThreadLocalRandom.current().nextBytes(DATA);
9090
zk.create(PATH, DATA, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
@@ -93,7 +93,7 @@ public static void setUp() throws Exception {
9393
}
9494
zk.close();
9595
Configuration conf = UTIL.getConfiguration();
96-
conf.set(HConstants.ZOOKEEPER_QUORUM, "localhost:" + port);
96+
conf.set(HConstants.ZOOKEEPER_QUORUM, hostPort);
9797
conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY, 3);
9898
conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY_INTERVAL_MILLIS, 100);
9999
conf.setInt(ReadOnlyZKClient.KEEPALIVE_MILLIS, 3000);

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZKMainServer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertTrue;
22-
2322
import java.security.Permission;
2423
import org.apache.hadoop.conf.Configuration;
2524
import org.apache.hadoop.hbase.HBaseClassTestRule;
@@ -83,8 +82,8 @@ public void testCommandLineWorks() throws Exception {
8382
ZKUtil.checkExists(zkw, znode);
8483
boolean exception = false;
8584
try {
86-
ZKMainServer.main(new String [] {"-server",
87-
"localhost:" + htu.getZkCluster().getClientPort(), "delete", znode});
85+
ZKMainServer.main(new String [] {"-server", htu.getZkCluster().getAddress().toString(),
86+
"delete", znode});
8887
} catch (ExitException ee) {
8988
// ZKMS calls System.exit which should trigger this exception.
9089
exception = true;

0 commit comments

Comments
 (0)