44import java .time .Instant ;
55import java .util .concurrent .TimeUnit ;
66import java .util .function .Supplier ;
7+ import org .slf4j .Logger ;
8+ import org .slf4j .LoggerFactory ;
79import redis .clients .jedis .exceptions .JedisAskDataException ;
810import redis .clients .jedis .exceptions .JedisClusterMaxAttemptsException ;
911import 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