-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fix retries logic #2351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Fix retries logic #2351
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
src/test/java/redis/clients/jedis/tests/commands/JedisClusterCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| package redis.clients.jedis.tests.commands; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
| import redis.clients.jedis.HostAndPort; | ||
| import redis.clients.jedis.Jedis; | ||
| import redis.clients.jedis.JedisClusterCommand; | ||
| import redis.clients.jedis.JedisClusterConnectionHandler; | ||
| import redis.clients.jedis.JedisSlotBasedConnectionHandler; | ||
| import redis.clients.jedis.exceptions.JedisAskDataException; | ||
| import redis.clients.jedis.exceptions.JedisClusterMaxAttemptsException; | ||
| import redis.clients.jedis.exceptions.JedisConnectionException; | ||
| import redis.clients.jedis.exceptions.JedisMovedDataException; | ||
| import redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException; | ||
|
|
||
| public class JedisClusterCommandTest { | ||
|
|
||
| @Test(expected = JedisClusterMaxAttemptsException.class) | ||
| public void runZeroAttempts() { | ||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(null, 0) { | ||
| @Override | ||
| public String execute(Jedis connection) { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| testMe.run(""); | ||
| } | ||
|
|
||
| @Test | ||
| public void runSuccessfulExecute() { | ||
| JedisClusterConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisClusterConnectionHandler.class); | ||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| @Override | ||
| public String execute(Jedis connection) { | ||
| return "foo"; | ||
| } | ||
| }; | ||
| String actual = testMe.run(""); | ||
| assertEquals("foo", actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void runFailOnFirstExecSuccessOnSecondExec() { | ||
| JedisClusterConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisClusterConnectionHandler.class); | ||
|
|
||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| boolean isFirstCall = true; | ||
|
|
||
| @Override | ||
| public String execute(Jedis connection) { | ||
| if (isFirstCall) { | ||
| isFirstCall = false; | ||
| throw new JedisConnectionException("Borkenz"); | ||
| } | ||
|
|
||
| return "foo"; | ||
| } | ||
| }; | ||
|
|
||
| String actual = testMe.run(""); | ||
| assertEquals("foo", actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void runReconnectWithRandomConnection() { | ||
| JedisSlotBasedConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisSlotBasedConnectionHandler.class); | ||
| // simulate failing connection | ||
| Mockito.when(connectionHandler.getConnectionFromSlot(Mockito.anyInt())).thenReturn(null); | ||
| // simulate good connection | ||
| Mockito.when(connectionHandler.getConnection()).thenReturn(Mockito.mock(Jedis.class)); | ||
|
|
||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| @Override | ||
| public String execute(Jedis connection) { | ||
| if (connection == null) { | ||
| throw new JedisConnectionException(""); | ||
| } | ||
| return "foo"; | ||
| } | ||
| }; | ||
|
|
||
| String actual = testMe.run(""); | ||
| assertEquals("foo", actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void runMovedSuccess() { | ||
| JedisClusterConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisClusterConnectionHandler.class); | ||
|
|
||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| boolean isFirstCall = true; | ||
|
|
||
| @Override | ||
| public String execute(Jedis connection) { | ||
| if (isFirstCall) { | ||
| isFirstCall = false; | ||
|
|
||
| // Slot 0 moved | ||
| throw new JedisMovedDataException("", null, 0); | ||
| } | ||
|
|
||
| return "foo"; | ||
| } | ||
| }; | ||
|
|
||
| String actual = testMe.run(""); | ||
| assertEquals("foo", actual); | ||
|
|
||
| Mockito.verify(connectionHandler).renewSlotCache(Mockito.<Jedis> any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void runAskSuccess() { | ||
| JedisSlotBasedConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisSlotBasedConnectionHandler.class); | ||
| Jedis jedis = Mockito.mock(Jedis.class); | ||
| Mockito.when(connectionHandler.getConnectionFromNode(Mockito.<HostAndPort> any())).thenReturn( | ||
| jedis); | ||
|
|
||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| boolean isFirstCall = true; | ||
|
|
||
| @Override | ||
| public String execute(Jedis connection) { | ||
| if (isFirstCall) { | ||
| isFirstCall = false; | ||
|
|
||
| // Slot 0 moved | ||
| throw new JedisAskDataException("", null, 0); | ||
| } | ||
|
|
||
| return "foo"; | ||
| } | ||
| }; | ||
|
|
||
| String actual = testMe.run(""); | ||
| assertEquals("foo", actual); | ||
| Mockito.verify(jedis).asking(); | ||
| } | ||
|
|
||
| @Test | ||
| public void runMovedFailSuccess() { | ||
| // Test: | ||
| // First attempt is a JedisMovedDataException() move, because we asked the wrong node | ||
| // Second attempt is a JedisConnectionException, because this node is down | ||
| // In response to that, runWithTimeout() requests a random node using | ||
| // connectionHandler.getConnection() | ||
| // Third attempt works | ||
| JedisSlotBasedConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisSlotBasedConnectionHandler.class); | ||
|
|
||
| Jedis fromGetConnectionFromSlot = Mockito.mock(Jedis.class); | ||
| Mockito.when(fromGetConnectionFromSlot.toString()).thenReturn("getConnectionFromSlot"); | ||
| Mockito.when(connectionHandler.getConnectionFromSlot(Mockito.anyInt())).thenReturn( | ||
| fromGetConnectionFromSlot); | ||
|
|
||
| Jedis fromGetConnectionFromNode = Mockito.mock(Jedis.class); | ||
| Mockito.when(fromGetConnectionFromNode.toString()).thenReturn("getConnectionFromNode"); | ||
| Mockito.when(connectionHandler.getConnectionFromNode(Mockito.<HostAndPort> any())).thenReturn( | ||
| fromGetConnectionFromNode); | ||
|
|
||
| Jedis fromGetConnection = Mockito.mock(Jedis.class); | ||
| Mockito.when(fromGetConnection.toString()).thenReturn("getConnection"); | ||
| Mockito.when(connectionHandler.getConnection()).thenReturn(fromGetConnection); | ||
|
|
||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| @Override | ||
| public String execute(Jedis connection) { | ||
| String source = connection.toString(); | ||
| if ("getConnectionFromSlot".equals(source)) { | ||
| // First attempt, report moved | ||
| throw new JedisMovedDataException("Moved", null, 0); | ||
| } | ||
|
|
||
| if ("getConnectionFromNode".equals(source)) { | ||
| // Second attempt in response to the move, report failure | ||
| throw new JedisConnectionException("Connection failed"); | ||
| } | ||
|
|
||
| // This is the third and last case we handle | ||
| assert "getConnection".equals(source); | ||
| return "foo"; | ||
| } | ||
| }; | ||
|
|
||
| String actual = testMe.run(""); | ||
| assertEquals("foo", actual); | ||
| } | ||
|
|
||
| @Test(expected = JedisNoReachableClusterNodeException.class) | ||
| public void runRethrowsJedisNoReachableClusterNodeException() { | ||
| JedisSlotBasedConnectionHandler connectionHandler = Mockito | ||
| .mock(JedisSlotBasedConnectionHandler.class); | ||
| Mockito.when(connectionHandler.getConnectionFromSlot(Mockito.anyInt())).thenThrow( | ||
| JedisNoReachableClusterNodeException.class); | ||
|
|
||
| JedisClusterCommand<String> testMe = new JedisClusterCommand<String>(connectionHandler, 10) { | ||
| @Override | ||
| public String execute(Jedis connection) { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| testMe.run(""); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/test/java/redis/clients/jedis/tests/demo/ClusterDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package redis.clients.jedis.tests.demo; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import redis.clients.jedis.HostAndPort; | ||
| import redis.clients.jedis.JedisCluster; | ||
| import redis.clients.jedis.JedisPoolConfig; | ||
|
|
||
| // See: https://github.com/redis/jedis/issues/2347 | ||
| public class ClusterDemo { | ||
| // Expect to find a Redis cluster on this and the five following ports | ||
| private final static int BASE_PORT = 6379; | ||
|
|
||
| // This program should survive any node being down for up to 30s, and keep printing. Having | ||
| // printouts pause while the node is down is fine. | ||
| public static void main(String[] args) throws InterruptedException { | ||
| JedisPoolConfig poolConfig = new JedisPoolConfig(); | ||
|
|
||
| Set<HostAndPort> nodes = new HashSet<>(); | ||
| for (int i = 0; i < 6; i++) { | ||
| nodes.add(new HostAndPort("127.0.0.1", BASE_PORT + i)); | ||
| } | ||
| JedisCluster cluster = new JedisCluster(nodes, 10_000, 10, poolConfig); | ||
|
|
||
| // noinspection InfiniteLoopStatement | ||
| while (true) { | ||
| final Long foo = cluster.incr("foo"); | ||
| System.out.printf("foo=%d%n", foo); | ||
| // noinspection BusyWait | ||
| Thread.sleep(1000); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would heavily affect current applications. Connection exceptions can happen for silliest of the reasons. So we can't backoff right after a connection exception. There should a balance between retry and backoff. For example, a slot based node or a redirected node should be given at least 2/3 consecutive tries before backing off.
In all of the cases,
maxAttempts, remainingattempts,renewSlotCache()should be kept in mind.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll just try to rephrase what you said so I know I understand:
I think this sounds reasonable, just want to ensure I change the right thing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walles Yes, you've got it right.