diff --git a/src/main/java/io/lettuce/core/ReadFrom.java b/src/main/java/io/lettuce/core/ReadFrom.java index a9b9a3bed4..fc004d783b 100644 --- a/src/main/java/io/lettuce/core/ReadFrom.java +++ b/src/main/java/io/lettuce/core/ReadFrom.java @@ -102,6 +102,13 @@ public abstract class ReadFrom { */ public static final ReadFrom ANY = new ReadFromImpl.ReadFromAnyNode(); + /** + * Setting to read from any replica node. + * + * @since 6.0 + */ + public static final ReadFrom ANY_REPLICA = new ReadFromImpl.ReadFromAnyReplica(); + /** * Chooses the nodes from the matching Redis nodes that match this read selector. * @@ -166,6 +173,10 @@ public static ReadFrom valueOf(String name) { return ANY; } + if (name.equalsIgnoreCase("anyReplica")) { + return ANY_REPLICA; + } + throw new IllegalArgumentException("ReadFrom " + name + " not supported"); } diff --git a/src/main/java/io/lettuce/core/ReadFromImpl.java b/src/main/java/io/lettuce/core/ReadFromImpl.java index 57389c4e06..69f4a69330 100644 --- a/src/main/java/io/lettuce/core/ReadFromImpl.java +++ b/src/main/java/io/lettuce/core/ReadFromImpl.java @@ -115,6 +115,17 @@ public ReadFromAnyNode() { } + /** + * Read from any replica node. + */ + static final class ReadFromAnyReplica extends UnorderedPredicateReadFromAdapter { + + public ReadFromAnyReplica() { + super(IS_REPLICA); + } + + } + /** * {@link Predicate}-based {@link ReadFrom} implementation. * diff --git a/src/test/java/io/lettuce/core/cluster/ReadFromUnitTests.java b/src/test/java/io/lettuce/core/cluster/ReadFromUnitTests.java index 88277218b2..f207bfb355 100644 --- a/src/test/java/io/lettuce/core/cluster/ReadFromUnitTests.java +++ b/src/test/java/io/lettuce/core/cluster/ReadFromUnitTests.java @@ -83,6 +83,12 @@ void nearest() { assertThat(result).hasSize(3).containsExactly(nearest, master, replica); } + @Test + void anyReplica() { + List result = ReadFrom.ANY_REPLICA.select(getNodes()); + assertThat(result).hasSize(2).containsExactly(nearest, replica); + } + @Test void valueOfNull() { assertThatThrownBy(() -> ReadFrom.valueOf(null)).isInstanceOf(IllegalArgumentException.class); @@ -118,6 +124,11 @@ void valueOfSlavePreferred() { assertThat(ReadFrom.valueOf("slavePreferred")).isEqualTo(ReadFrom.REPLICA_PREFERRED); } + @Test + void valueOfAnyReplica() { + assertThat(ReadFrom.valueOf("anyReplica")).isEqualTo(ReadFrom.ANY_REPLICA); + } + private ReadFrom.Nodes getNodes() { return new ReadFrom.Nodes() { @Override