Skip to content
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

Add anyReplica setting for ReadFrom #1444

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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