Skip to content

Commit

Permalink
Test ClusterCommandExecutor (#3139)
Browse files Browse the repository at this point in the history
* Test ClusterCommandExecutor

* Edit RetryableCommandExecutor

* edit

* cleanup
  • Loading branch information
sazzad16 authored Sep 14, 2022
1 parent 71dd808 commit a692b47
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,7 +53,7 @@ public final <T> T executeCommand(CommandObject<T> commandObject) {
connection = provider.getConnection(commandObject.getArguments());
}

return connection.executeCommand(commandObject);
return execute(connection, commandObject);

} catch (JedisClusterOperationException jnrcne) {
throw jnrcne;
Expand Down Expand Up @@ -95,6 +94,14 @@ public final <T> T executeCommand(CommandObject<T> commandObject) {
throw maxAttemptsException;
}

/**
* WARNING: This method is accessible for the purpose of testing.
* This should not be used or overriden.
*/
protected <T> T execute(Connection connection, CommandObject<T> commandObject) {
return connection.executeCommand(commandObject);
}

/**
* Related values should be reset if <code>TRUE</code> is returned.
*
Expand Down Expand Up @@ -144,6 +151,10 @@ private static long getBackoffSleepMillis(int attemptsLeft, Instant deadline) {
return millisLeft / (attemptsLeft * (attemptsLeft + 1));
}

/**
* WARNING: This method is accessible for the purpose of testing.
* This should not be used or overriden.
*/
protected void sleep(long sleepMillis) {
try {
TimeUnit.MILLISECONDS.sleep(sleepMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.CommandObject;
import redis.clients.jedis.Connection;

import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.IOUtils;
Expand Down Expand Up @@ -45,7 +45,7 @@ public final <T> T executeCommand(CommandObject<T> commandObject) {
try {
connection = provider.getConnection(commandObject.getArguments());

return connection.executeCommand(commandObject);
return execute(connection, commandObject);

} catch (JedisConnectionException jce) {
lastException = jce;
Expand All @@ -62,15 +62,23 @@ public final <T> T executeCommand(CommandObject<T> commandObject) {
}
}
if (Instant.now().isAfter(deadline)) {
throw new JedisException("Cluster retry deadline exceeded.");
throw new JedisException("Retry deadline exceeded.");
}
}

JedisException maxAttemptsException = new JedisException("No more cluster attempts left.");
JedisException maxAttemptsException = new JedisException("No more attempts left.");
maxAttemptsException.addSuppressed(lastException);
throw maxAttemptsException;
}

/**
* WARNING: This method is accessible for the purpose of testing.
* This should not be used or overriden.
*/
protected <T> T execute(Connection connection, CommandObject<T> commandObject) {
return connection.executeCommand(commandObject);
}

/**
* Related values should be reset if <code>TRUE</code> is returned.
*
Expand All @@ -97,12 +105,16 @@ private static long getBackoffSleepMillis(int attemptsLeft, Instant deadline) {

long millisLeft = Duration.between(Instant.now(), deadline).toMillis();
if (millisLeft < 0) {
throw new JedisException("Cluster retry deadline exceeded.");
throw new JedisException("Retry deadline exceeded.");
}

return millisLeft / (attemptsLeft * (attemptsLeft + 1));
}

/**
* WARNING: This method is accessible for the purpose of testing.
* This should not be used or overriden.
*/
protected void sleep(long sleepMillis) {
try {
TimeUnit.MILLISECONDS.sleep(sleepMillis);
Expand Down
Loading

0 comments on commit a692b47

Please sign in to comment.