ASTERIXDB-1687 - Configure Timeout On HTTP Connection
Fixes ASTERIXDB-1687 - asterixhelper get_cluster_state can hang forever,
by configuring a timeout on connect and read for HTTP calls, in the
event the CC is in a bad state and cannot answer requests
Change-Id: Ice9606acaec8f27b56d1d8ed947cc1588074ff2f
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1276
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/Args.java b/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/Args.java
index e767f28..ceb873d 100644
--- a/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/Args.java
+++ b/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/Args.java
@@ -45,7 +45,7 @@
@Option(name = "-timeout", metaVar = "<secs>", usage = "Timeout for wait commands in seconds")
- protected int timeoutSecs = -1;
+ protected int timeoutSecs = 0;
@Option(name = "-quiet", aliases = "-q", usage = "Suppress all normal output")
protected boolean quiet;
diff --git a/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/RemoteCommand.java b/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/RemoteCommand.java
index 031a721..e7b6be3 100644
--- a/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/RemoteCommand.java
+++ b/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/RemoteCommand.java
@@ -23,10 +23,14 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.concurrent.TimeUnit;
import org.apache.asterix.clienthelper.Args;
public abstract class RemoteCommand extends ClientCommand {
+
+ public static final int MAX_CONNECTION_TIMEOUT_SECS = 60;
+
protected enum Method {
GET,
POST
@@ -64,6 +68,10 @@
protected HttpURLConnection openConnection(String path, Method method) throws IOException {
URL url = new URL("http://" + hostPort + "/" + path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+ final int timeoutMillis =
+ (int) TimeUnit.SECONDS.toMillis(Math.max(MAX_CONNECTION_TIMEOUT_SECS, args.getTimeoutSecs()));
+ conn.setConnectTimeout(timeoutMillis);
+ conn.setReadTimeout(timeoutMillis);
conn.setRequestMethod(method.name());
return conn;
}
diff --git a/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/WaitForClusterCommand.java b/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/WaitForClusterCommand.java
index 390ce7b..b0b4c6f 100644
--- a/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/WaitForClusterCommand.java
+++ b/asterixdb/asterix-client-helper/src/main/java/org/apache/asterix/clienthelper/commands/WaitForClusterCommand.java
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
@@ -38,17 +39,18 @@
@Override
@SuppressWarnings("squid:S2142") // interrupted exception
public int execute() throws IOException {
+ final int timeoutSecs = args.getTimeoutSecs();
log("Waiting "
- + (args.getTimeoutSecs() > 0 ? "up to " + args.getTimeoutSecs() + " seconds " : "")
+ + (timeoutSecs > 0 ? "up to " + timeoutSecs + " seconds " : "")
+ "for cluster " + hostPort + " to be available.");
long startTime = System.currentTimeMillis();
+ long timeoutMillis = TimeUnit.SECONDS.toMillis(timeoutSecs);
boolean first = true;
String lastState = null;
while (true) {
if (!first) {
- if (args.getTimeoutSecs() >= 0
- && (startTime + (args.getTimeoutSecs() * 1000) < System.currentTimeMillis())) {
+ if (timeoutMillis > 0 && (startTime + timeoutMillis < System.currentTimeMillis())) {
break;
}
try {
@@ -75,7 +77,7 @@
// ignore exception, try again
}
}
- log("Cluster " + hostPort + " was not available before timeout of " + args.getTimeoutSecs()
+ log("Cluster " + hostPort + " was not available before timeout of " + timeoutSecs
+ " seconds was exhausted" + (lastState != null ? " (state: " + lastState + ")" : "")
+ "; check logs for more information");
return 1;