Skip to content

Commit

Permalink
Failover from auth error
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Dec 25, 2023
1 parent 40113d9 commit 388712c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public final class Protocol {
private static final String CLUSTERDOWN_PREFIX = "CLUSTERDOWN ";
private static final String BUSY_PREFIX = "BUSY ";
private static final String NOSCRIPT_PREFIX = "NOSCRIPT ";
private static final String NOAUTH_PREFIX = "NOAUTH";
private static final String WRONGPASS_PREFIX = "WRONGPASS";
private static final String NOPERM_PREFIX = "NOPERM";

Expand Down Expand Up @@ -97,9 +98,9 @@ private static void processError(final RedisInputStream is) {
throw new JedisBusyException(message);
} else if (message.startsWith(NOSCRIPT_PREFIX)) {
throw new JedisNoScriptException(message);
} else if (message.startsWith(WRONGPASS_PREFIX)) {
throw new JedisAccessControlException(message);
} else if (message.startsWith(NOPERM_PREFIX)) {
} else if (message.startsWith(NOAUTH_PREFIX)
|| message.startsWith(WRONGPASS_PREFIX)
|| message.startsWith(NOPERM_PREFIX)) {
throw new JedisAccessControlException(message);
}
throw new JedisDataException(message);
Expand Down
44 changes: 40 additions & 4 deletions src/test/java/redis/clients/jedis/misc/AutomaticFailoverTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redis.clients.jedis.misc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -23,6 +25,7 @@
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.MultiClusterClientConfig;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisAccessControlException;
import redis.clients.jedis.providers.MultiClusterPooledConnectionProvider;
import redis.clients.jedis.util.IOUtils;

Expand Down Expand Up @@ -98,15 +101,17 @@ public void commandFailover() {
.circuitBreakerSlidingWindowMinCalls(slidingWindowMinCalls)
.circuitBreakerSlidingWindowSize(slidingWindowSize);

RedisFailoverReporter rsasRedisFailoverReporter = new RedisFailoverReporter();
RedisFailoverReporter failoverReporter = new RedisFailoverReporter();
MultiClusterPooledConnectionProvider cacheProvider = new MultiClusterPooledConnectionProvider(builder.build());
cacheProvider.setClusterFailoverPostProcessor(rsasRedisFailoverReporter);
cacheProvider.setClusterFailoverPostProcessor(failoverReporter);

UnifiedJedis jedis = new UnifiedJedis(cacheProvider);

assertFalse(failoverReporter.failedOver);
log.info("Starting calls to Redis");
String key = "hash-" + System.nanoTime();
jedis.hset(key, "f1", "v1");
assertTrue(failoverReporter.failedOver);

assertEquals(Collections.singletonMap("f1", "v1"), jedis.hgetAll(key));
jedis.flushAll();
Expand All @@ -124,17 +129,48 @@ public void pipelineFailover() {
.circuitBreakerSlidingWindowMinCalls(slidingWindowMinCalls)
.circuitBreakerSlidingWindowSize(slidingWindowSize);

RedisFailoverReporter rsasRedisFailoverReporter = new RedisFailoverReporter();
RedisFailoverReporter failoverReporter = new RedisFailoverReporter();
MultiClusterPooledConnectionProvider cacheProvider = new MultiClusterPooledConnectionProvider(builder.build());
cacheProvider.setClusterFailoverPostProcessor(rsasRedisFailoverReporter);
cacheProvider.setClusterFailoverPostProcessor(failoverReporter);

UnifiedJedis jedis = new UnifiedJedis(cacheProvider);

assertFalse(failoverReporter.failedOver);
log.info("Starting calls to Redis");
AbstractPipeline pipe = jedis.pipelined();
String key = "hash-" + System.nanoTime();
pipe.hset(key, "f1", "v1");
pipe.sync();
assertTrue(failoverReporter.failedOver);

assertEquals(Collections.singletonMap("f1", "v1"), jedis.hgetAll(key));
jedis.flushAll();

jedis.close();
}

@Test
public void failoverAuthError() {
int slidingWindowMinCalls = 10;
int slidingWindowSize = 10;

MultiClusterClientConfig.Builder builder = new MultiClusterClientConfig.Builder(
getClusterConfigs(clientConfig, hostPort_1_2, hostPort_2))
.circuitBreakerSlidingWindowMinCalls(slidingWindowMinCalls)
.circuitBreakerSlidingWindowSize(slidingWindowSize)
.circuitBreakerFallbackExceptionList(Arrays.asList(JedisAccessControlException.class));

RedisFailoverReporter failoverReporter = new RedisFailoverReporter();
MultiClusterPooledConnectionProvider cacheProvider = new MultiClusterPooledConnectionProvider(builder.build());
cacheProvider.setClusterFailoverPostProcessor(failoverReporter);

UnifiedJedis jedis = new UnifiedJedis(cacheProvider);

assertFalse(failoverReporter.failedOver);
log.info("Starting calls to Redis");
String key = "hash-" + System.nanoTime();
jedis.hset(key, "f1", "v1");
assertTrue(failoverReporter.failedOver);

assertEquals(Collections.singletonMap("f1", "v1"), jedis.hgetAll(key));
jedis.flushAll();
Expand Down

0 comments on commit 388712c

Please sign in to comment.