Skip to content

Commit

Permalink
Consider ReadFrom.isOrderSensitive() in cluster scan command #1146
Browse files Browse the repository at this point in the history
ReadFrom order sensitivity is now considered in Cluster SCAN for keys.
  • Loading branch information
mp911de committed Oct 16, 2019
1 parent 63f79fd commit a8afab9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;

import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -158,7 +159,13 @@ public Iterator<RedisNodeDescription> iterator() {
});

if (!selection.isEmpty()) {
RedisClusterNode selectedNode = (RedisClusterNode) selection.get(0);

int indexToUse = 0;
if (!OrderingReadFromAccessor.isOrderSensitive(connection.getReadFrom())) {
indexToUse = ThreadLocalRandom.current().nextInt(selection.size());
}

RedisClusterNode selectedNode = (RedisClusterNode) selection.get(indexToUse);
nodeIds.add(selectedNode.getNodeId());
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ScanIteratorIntegrationTests extends TestSupport {
@Inject
ScanIteratorIntegrationTests(StatefulRedisClusterConnection<String, String> connection) {
this.connection = connection;
this.connection.setReadFrom(ReadFrom.MASTER);
this.redis = connection.sync();
}

Expand Down Expand Up @@ -103,6 +104,19 @@ void keysMultiPass() {
assertThat(keys).containsAll(KeysAndValues.KEYS);
}

@Test
void keysMultiPassFromAnyNode() {

redis.mset(KeysAndValues.MAP);
this.connection.setReadFrom(ReadFrom.ANY);

ScanIterator<String> scan = ScanIterator.scan(redis);

List<String> keys = scan.stream().collect(Collectors.toList());

assertThat(keys).containsAll(KeysAndValues.KEYS);
}

@Test
void hscanShouldThrowNoSuchElementExceptionOnEmpty() {

Expand Down

0 comments on commit a8afab9

Please sign in to comment.