Skip to content

Commit

Permalink
ZOOKEEPER-3877: JMX Bean RemotePeerBean should enclose IPV6 host in s…
Browse files Browse the repository at this point in the history
…quare bracket same as LocalPeerBean

…quare bracket same as LocalPeerBean

Author: Mohammad Arshad <[email protected]>

Reviewers: Enrico Olivelli <[email protected]>

Closes apache#1493 from arshadmohammad/ZOOKEEPER-3877-master
  • Loading branch information
arshadmohammad authored and anurag-harness committed Nov 2, 2022
1 parent c845f89 commit 6b43706
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit 6b43706

Please sign in to comment.