Skip to content

Commit

Permalink
ZOOKEEPER-4819 Support more tcp options when calling FourLetterWordMain
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiner committed Oct 21, 2024
1 parent 4c26a3f commit 36fc60d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ private void pingRwServer() throws RWServerFoundException {

LOG.info("Checking server {} for being r/w. Timeout {}", addr, pingRwTimeout);
try {
result = FourLetterWordMain.send4LetterWord(addr.getHostString(), addr.getPort(), "isro", 1000, clientConfig);
result = FourLetterWordMain.send4LetterWord(addr.getHostString(), addr.getPort(), "isro", clientConfig, 1000);
} catch (ConnectException e) {
// ignore, this just means server is not up
} catch (IOException | X509Exception.SSLContextException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class FourLetterWordMain {

//in milliseconds, socket should connect/read within this period otherwise SocketTimeoutException
private static final int DEFAULT_SOCKET_TIMEOUT = 5000;
private static final boolean DEFAULT_TCP_NO_DELAY = true;
private static final boolean DEFAULT_TCP_LINGER_ON = false;
protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordMain.class);
/**
* Send the 4letterword
Expand Down Expand Up @@ -109,8 +111,8 @@ public static String send4LetterWord(
String host,
int port,
String cmd,
int timeout,
ZKClientConfig clientConfig) throws SSLContextException, IOException {
ZKClientConfig clientConfig,
int timeout) throws SSLContextException, IOException {
boolean useSecure = clientConfig.getBoolean(ZKClientConfig.SECURE_CLIENT);
SSLContext sslContext = null;
if (useSecure) {
Expand All @@ -133,12 +135,41 @@ public static String send4LetterWord(
* @throws java.io.IOException
* @throws SSLContextException
*/
public static String send4LetterWord(
String host,
int port,
String cmd,
boolean secure,
int timeout,
SSLContext sslContext) throws IOException, SSLContextException {
return send4LetterWord(host, port, cmd, secure,
timeout, DEFAULT_TCP_NO_DELAY, DEFAULT_TCP_LINGER_ON, -1, sslContext);
}

/**
* Send the 4letterword
* @param host the destination host
* @param port the destination port
* @param cmd the 4letterword
* @param secure whether to use SSL
* @param timeout in milliseconds, maximum time to wait while connecting/reading data
* @param tcpNoDelay tcp option no-delay
* @param soLingerOn tcp option linger-on
* @param soLinger tcp option linger
* @param sslContext SSL context
* @return server response
* @throws java.io.IOException
* @throws SSLContextException
*/
public static String send4LetterWord(
String host,
int port,
String cmd,
boolean secure,
int timeout,
boolean tcpNoDelay,
boolean soLingerOn,
int soLinger,
SSLContext sslContext) throws IOException, SSLContextException {
LOG.info("connecting to {}:{} (secure={})", host, port, secure);

Expand All @@ -164,7 +195,9 @@ public static String send4LetterWord(
sock = new Socket();
sock.connect(hostaddress, timeout);
}
sock.setSoLinger(soLingerOn, soLinger);
sock.setSoTimeout(timeout);
sock.setTcpNoDelay(tcpNoDelay);
OutputStream outstream = sock.getOutputStream();
outstream.write(cmd.getBytes(UTF_8));
outstream.flush();
Expand Down

0 comments on commit 36fc60d

Please sign in to comment.