Skip to content

Commit

Permalink
Add ANY_REPLICA setting to ReadFrom
Browse files Browse the repository at this point in the history
Original pull request: #1444
  • Loading branch information
omer.cilingir authored and mp911de committed Oct 26, 2020
1 parent 66bea16 commit 59f4965
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/io/lettuce/core/ReadFrom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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");
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/lettuce/core/ReadFromImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/io/lettuce/core/cluster/ReadFromUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ void nearest() {
assertThat(result).hasSize(3).containsExactly(nearest, master, replica);
}

@Test
void anyReplica() {
List<RedisNodeDescription> result = ReadFrom.ANY_REPLICA.select(getNodes());
assertThat(result).hasSize(2).containsExactly(nearest, replica);
}

@Test
void valueOfNull() {
assertThatThrownBy(() -> ReadFrom.valueOf(null)).isInstanceOf(IllegalArgumentException.class);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 59f4965

Please sign in to comment.