From 3c30a5c0178dde4c1861d0b93e6900bd7c7f07d5 Mon Sep 17 00:00:00 2001 From: Mohammad Arshad Date: Tue, 2 Mar 2021 11:37:47 +0530 Subject: [PATCH] ZOOKEEPER-3877: JMX Bean RemotePeerBean should enclose IPV6 host in square bracket same as LocalPeerBean MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …quare bracket same as LocalPeerBean Author: Mohammad Arshad Reviewers: Enrico Olivelli Closes #1493 from arshadmohammad/ZOOKEEPER-3877-master --- .../zookeeper/server/quorum/RemotePeerBean.java | 14 +++++++------- .../server/quorum/RemotePeerBeanTest.java | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java index 2d9d7d3dd65..522ba3ad345 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java @@ -18,7 +18,9 @@ package org.apache.zookeeper.server.quorum; +import static org.apache.zookeeper.common.NetUtils.formatInetAddr; import java.util.stream.Collectors; +import org.apache.zookeeper.common.NetUtils; import org.apache.zookeeper.jmx.ZKMBeanInfo; /** @@ -47,22 +49,20 @@ public boolean isHidden() { } public String getQuorumAddress() { - return peer.addr.getAllAddresses().stream() - .map(address -> String.format("%s:%d", address.getHostString(), address.getPort())) - .collect(Collectors.joining("|")); + return peer.addr.getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|")); } public String getElectionAddress() { - return peer.electionAddr.getAllAddresses().stream() - .map(address -> String.format("%s:%d", address.getHostString(), address.getPort())) - .collect(Collectors.joining("|")); + return peer.electionAddr.getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|")); } public String getClientAddress() { if (null == peer.clientAddr) { return ""; } - return peer.clientAddr.getHostString() + ":" + peer.clientAddr.getPort(); + return formatInetAddr(peer.clientAddr); } public String getLearnerType() { diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java index de145ea4f53..cc2321c10f2 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java @@ -59,4 +59,20 @@ public void testIsLeader() { assertFalse(remotePeerBean.isLeader()); } + @Test + public void testHostPortReturnedWhenIPIsIPV6() { + QuorumPeer.QuorumServer quorumServerMock = mock(QuorumPeer.QuorumServer.class); + InetSocketAddress address = new InetSocketAddress("127::1", 2181); + MultipleAddresses multipleAddresses = new MultipleAddresses(address); + quorumServerMock.clientAddr = address; + quorumServerMock.electionAddr = multipleAddresses; + quorumServerMock.addr = multipleAddresses; + QuorumPeer peerMock = mock(QuorumPeer.class); + RemotePeerBean remotePeerBean = new RemotePeerBean(peerMock, quorumServerMock); + String expectedHostPort = "[127:0:0:0:0:0:0:1]:2181"; + assertEquals(expectedHostPort, remotePeerBean.getClientAddress()); + assertEquals(expectedHostPort, remotePeerBean.getElectionAddress()); + assertEquals(expectedHostPort, remotePeerBean.getQuorumAddress()); + } + }