Skip to content

Commit

Permalink
Pretty print cluster connect timeout in logs (#18648)
Browse files Browse the repository at this point in the history
The default value for the cluster connect timeout is infinite,
so it is represented with `Long.MAX_VALUE`. When we try to log
this as it is, we output something like below.

```
... cluster connect timeout: 9223372036854775807 ms, ...
```

To make this more clear, we will replace this with the
output below.

```
... cluster connect timeout: INFINITE, ...
```

If the user has provided some other timeout value (such as 999 ms),
the output will still be as it is now.

```
... cluster connect timeout: 999 ms, ...
```
  • Loading branch information
mdumandag committed May 6, 2021
1 parent 8bcf23d commit bb4a38f
Showing 1 changed file with 13 additions and 4 deletions.
Expand Up @@ -32,6 +32,7 @@ public class WaitStrategy {
private int attempt;
private int currentBackoffMillis;
private final long clusterConnectTimeoutMillis;
private final String clusterConnectTimeoutText;
private long clusterConnectAttemptBegin;

WaitStrategy(int initialBackoffMillis, int maxBackoffMillis,
Expand All @@ -40,6 +41,13 @@ public class WaitStrategy {
this.maxBackoffMillis = maxBackoffMillis;
this.multiplier = multiplier;
this.clusterConnectTimeoutMillis = clusterConnectTimeoutMillis;
if (clusterConnectTimeoutMillis == Long.MAX_VALUE) {
// For a better logging output for the default values, we will
// replace "Long.MAX_VALUE ms" with this in our output.
clusterConnectTimeoutText = "INFINITE";
} else {
clusterConnectTimeoutText = clusterConnectTimeoutMillis + " ms";
}
this.jitter = jitter;
this.logger = logger;
}
Expand All @@ -54,9 +62,10 @@ public boolean sleep() {
attempt++;
long currentTimeMillis = Clock.currentTimeMillis();
long timePassed = currentTimeMillis - clusterConnectAttemptBegin;

if (timePassed > clusterConnectTimeoutMillis) {
logger.warning(String.format("Unable to get live cluster connection, cluster connect timeout (%d ms) is "
+ "reached. Attempt %d.", clusterConnectTimeoutMillis, attempt));
logger.warning(String.format("Unable to get live cluster connection, cluster connect timeout (%s) is "
+ "reached. Attempt %d.", clusterConnectTimeoutText, attempt));
return false;
}

Expand All @@ -68,8 +77,8 @@ public boolean sleep() {
actualSleepTime = Math.min(actualSleepTime, clusterConnectTimeoutMillis - timePassed);

logger.warning(String.format("Unable to get live cluster connection, retry in %d ms, attempt: %d"
+ ", cluster connect timeout: %d ms"
+ ", max backoff: %d ms", actualSleepTime, attempt, clusterConnectTimeoutMillis, maxBackoffMillis));
+ ", cluster connect timeout: %s"
+ ", max backoff: %d ms", actualSleepTime, attempt, clusterConnectTimeoutText, maxBackoffMillis));

try {
Thread.sleep(actualSleepTime);
Expand Down

0 comments on commit bb4a38f

Please sign in to comment.