Skip to content

Commit

Permalink
ZOOKEEPER-3188: fix PR commits; handle case when Leader can not bind …
Browse files Browse the repository at this point in the history
…to port on startup
  • Loading branch information
Mate Szalay-Beko committed Oct 9, 2019
1 parent 483d2fc commit 2eedf26
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 178 deletions.
Expand Up @@ -24,7 +24,6 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand All @@ -40,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -290,15 +290,20 @@ public boolean isQuorumSynced(QuorumVerifier qv) {
addresses = self.getQuorumAddress().getAllAddresses();
}

for (InetSocketAddress address : addresses) {
serverSockets.add(createServerSocket(address, self.shouldUsePortUnification(), self.isSslQuorum()));
addresses.stream()
.map(address -> createServerSocket(address, self.shouldUsePortUnification(), self.isSslQuorum()))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(serverSockets::add);

if (serverSockets.isEmpty()) {
throw new IOException("Leader failed to initialize any of the following sockets: " + addresses);
}

this.zk = zk;
}

ServerSocket createServerSocket(InetSocketAddress address, boolean portUnification, boolean sslQuorum)
throws IOException {
Optional<ServerSocket> createServerSocket(InetSocketAddress address, boolean portUnification, boolean sslQuorum) {
ServerSocket serverSocket;
try {
if (portUnification || sslQuorum) {
Expand All @@ -308,11 +313,11 @@ ServerSocket createServerSocket(InetSocketAddress address, boolean portUnificati
}
serverSocket.setReuseAddress(true);
serverSocket.bind(address);
return serverSocket;
} catch (BindException e) {
return Optional.of(serverSocket);
} catch (IOException e) {
LOG.error("Couldn't bind to " + address.toString(), e);
throw e;
}
return Optional.empty();
}

/**
Expand Down

0 comments on commit 2eedf26

Please sign in to comment.