Skip to content

Commit f8d09c2

Browse files
author
Johan Walles
committed
Add retries logging
1 parent 85fa21c commit f8d09c2

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/main/java/redis/clients/jedis/BinaryJedis.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ public BinaryJedis(final JedisSocketFactory jedisSocketFactory) {
189189
client = new Client(jedisSocketFactory);
190190
}
191191

192+
@Override
193+
public String toString() {
194+
return "BinaryJedis{" + client + '}';
195+
}
196+
192197
private void initializeClientFromURI(URI uri) {
193198
initializeClientFromURI(uri, null, null, null);
194199
}

src/main/java/redis/clients/jedis/Connection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public Connection(final JedisSocketFactory jedisSocketFactory) {
5757
this.jedisSocketFactory = jedisSocketFactory;
5858
}
5959

60+
@Override
61+
public String toString() {
62+
return "Connection{" + jedisSocketFactory + "}";
63+
}
64+
6065
public Socket getSocket() {
6166
return socket;
6267
}

src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ public int getSoTimeout() {
121121
public void setSoTimeout(int soTimeout) {
122122
this.soTimeout = soTimeout;
123123
}
124+
125+
@Override
126+
public String toString() {
127+
return "DefaultJedisSocketFactory{" + host + ":" + +port + "}";
128+
}
124129
}

src/main/java/redis/clients/jedis/JedisClusterCommand.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.time.Instant;
55
import java.util.concurrent.TimeUnit;
66
import java.util.function.Supplier;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
79
import redis.clients.jedis.exceptions.JedisAskDataException;
810
import redis.clients.jedis.exceptions.JedisClusterMaxAttemptsException;
911
import redis.clients.jedis.exceptions.JedisClusterOperationException;
@@ -24,6 +26,8 @@ public abstract class JedisClusterCommand<T> {
2426
*/
2527
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(20);
2628

29+
private static final Logger LOG = LoggerFactory.getLogger(JedisClusterCommand.class);
30+
2731
private final JedisClusterConnectionHandler connectionHandler;
2832
private final int maxAttempts;
2933
private final Duration timeout;
@@ -143,10 +147,15 @@ private T runWithRetries(final int slot) {
143147
redirectionSupplier = null;
144148
}
145149

146-
return execute(connection);
150+
final T result = execute(connection);
151+
if (currentAttempt > 0) {
152+
LOG.info("Success after {} attempts", currentAttempt + 1);
153+
}
154+
return result;
147155
} catch (JedisNoReachableClusterNodeException e) {
148156
throw e;
149157
} catch (JedisConnectionException e) {
158+
LOG.warn("Failed connecting to Redis: {}", connection, e);
150159
// "- 1" because we just did one, but the currentAttempt counter hasn't increased yet
151160
int attemptsLeft = maxAttempts - currentAttempt - 1;
152161
connectionSupplier = handleConnectionProblem(connection, slot, attemptsLeft, deadline);
@@ -155,13 +164,16 @@ private T runWithRetries(final int slot) {
155164
} finally {
156165
releaseConnection(connection);
157166
}
167+
168+
LOG.info("{} retries left...", maxAttempts - currentAttempt - 1);
158169
}
159170

160171
throw new JedisClusterMaxAttemptsException("No more cluster attempts left.");
161172
}
162173

163174
protected void sleep(long sleepMillis) {
164175
try {
176+
LOG.info("Backing off, sleeping {}ms before trying again...", sleepMillis);
165177
TimeUnit.MILLISECONDS.sleep(sleepMillis);
166178
} catch (InterruptedException e) {
167179
throw new RuntimeException(e);
@@ -171,7 +183,11 @@ protected void sleep(long sleepMillis) {
171183
private Supplier<Jedis> handleConnectionProblem(Jedis failedConnection, final int slot, int attemptsLeft,
172184
Instant doneDeadline) {
173185
if (!shouldBackOff(attemptsLeft)) {
174-
return () -> connectionHandler.getConnectionFromSlot(slot);
186+
return () -> {
187+
Jedis connection = connectionHandler.getConnectionFromSlot(slot);
188+
LOG.info("Retrying with {}", connection);
189+
return connection;
190+
};
175191
}
176192

177193
// Must release current connection before renewing the slot cache below. If we fail to do this,
@@ -188,11 +204,16 @@ private Supplier<Jedis> handleConnectionProblem(Jedis failedConnection, final in
188204
return () -> {
189205
sleep(getBackoffSleepMillis(attemptsLeft, doneDeadline));
190206
// Get a random connection, it will redirect us if it's not the right one
191-
return connectionHandler.getConnection();
207+
LOG.info("Retrying with a random node...");
208+
Jedis connection = connectionHandler.getConnection();
209+
LOG.info("Retrying with random pick: {}", connection);
210+
return connection;
192211
};
193212
}
194213

195214
private Supplier<Jedis> handleRedirection(Jedis connection, final JedisRedirectionException jre) {
215+
LOG.debug("Redirected by server to {}", jre.getTargetNode());
216+
196217
// if MOVED redirection occurred,
197218
if (jre instanceof JedisMovedDataException) {
198219
// it rebuilds cluster's slot cache recommended by Redis cluster specification
@@ -204,6 +225,7 @@ private Supplier<Jedis> handleRedirection(Jedis connection, final JedisRedirecti
204225

205226
return () -> {
206227
Jedis redirectedConnection = connectionHandler.getConnectionFromNode(jre.getTargetNode());
228+
LOG.info("Retrying with redirection target {}", connection);
207229
if (jre instanceof JedisAskDataException) {
208230
// TODO: Pipeline asking with the original command to make it faster....
209231
redirectedConnection.asking();

0 commit comments

Comments
 (0)