Skip to content

Commit

Permalink
ZOOKEEPER-4728: force to re-resolve hostname into IP when binding. (#…
Browse files Browse the repository at this point in the history
…2040)

* ZOOKEEPER-4728: force to re-resolve hostname into IP when binding

* ZOOKEEPER-4708: add test

(cherry picked from commit 40aed41)
Signed-off-by: Andor Molnar <andor@apache.org>
  • Loading branch information
showuon authored and anmolnar committed Oct 17, 2023
1 parent 66771be commit f5a017c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ public Leader(QuorumPeer self, LeaderZooKeeperServer zk) throws IOException {
this.zk = zk;
}

InetSocketAddress recreateInetSocketAddr(String hostString, int port) {
return new InetSocketAddress(hostString, port);
}

Optional<ServerSocket> createServerSocket(InetSocketAddress address, boolean portUnification, boolean sslQuorum) {
ServerSocket serverSocket;
try {
Expand All @@ -318,7 +322,7 @@ Optional<ServerSocket> createServerSocket(InetSocketAddress address, boolean por
serverSocket = new ServerSocket();
}
serverSocket.setReuseAddress(true);
serverSocket.bind(address);
serverSocket.bind(recreateInetSocketAddr(address.getHostString(), address.getPort()));
return Optional.of(serverSocket);
} catch (IOException e) {
LOG.error("Couldn't bind to {}", address.toString(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
Expand All @@ -47,6 +48,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand Down Expand Up @@ -92,6 +94,15 @@ public void tearDown() throws IOException {
fileTxnSnapLog.close();
}

@Test
public void testCreateServerSocketWillRecreateInetSocketAddr() {
Leader spyLeader = Mockito.spy(leader);
InetSocketAddress addr = new InetSocketAddress("localhost", PortAssignment.unique());
spyLeader.createServerSocket(addr, false, false);
// make sure the address to be bound will be recreated with expected hostString and port
Mockito.verify(spyLeader, times(1)).recreateInetSocketAddr(addr.getHostString(), addr.getPort());
}

@Test
public void testGetName() {
assertEquals("Leader", leaderBean.getName());
Expand Down

0 comments on commit f5a017c

Please sign in to comment.