Skip to content
Open
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@
<version>${resilience4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
Expand Down Expand Up @@ -535,6 +541,7 @@
<include>**/MultiDb*.java</include>
<include>**/ClientTestUtil.java</include>
<include>**/ReflectionTestUtil.java</include>
<include>src/main/java/redis/clients/jedis/util/ReadOnlyCommands.java</include>
</includes>
</configuration>
<executions>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/JedisSentineled.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import redis.clients.jedis.executors.CommandExecutor;
import redis.clients.jedis.providers.ConnectionProvider;
import redis.clients.jedis.providers.SentineledConnectionProvider;
import redis.clients.jedis.util.ReadOnlyCommands;

public class JedisSentineled extends UnifiedJedis {

Expand Down Expand Up @@ -40,6 +41,21 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo
masterClientConfig.getRedisProtocol());
}

public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig,
final GenericObjectPoolConfig<Connection> poolConfig,
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig, ReadFrom readFrom) {
super(new SentineledConnectionProvider(masterName, masterClientConfig, poolConfig, sentinels, sentinelClientConfig, readFrom),
masterClientConfig.getRedisProtocol());
}

public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig,
final GenericObjectPoolConfig<Connection> poolConfig,
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig, ReadFrom readFrom,
ReadOnlyCommands.ReadOnlyPredicate readOnlyPredicate) {
super(new SentineledConnectionProvider(masterName, masterClientConfig, poolConfig, sentinels, sentinelClientConfig, readFrom, readOnlyPredicate),
masterClientConfig.getRedisProtocol());
}

@Experimental
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache,
final GenericObjectPoolConfig<Connection> poolConfig,
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/ReadFrom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package redis.clients.jedis;

public enum ReadFrom {
// read from the upstream only.
UPSTREAM,
// read from the replica only.
REPLICA,
// read preferred from the upstream and fall back to a replica if the upstream is not available.
UPSTREAM_PREFERRED,
// read preferred from replica and fall back to upstream if no replica is not available.
REPLICA_PREFERRED
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import redis.clients.jedis.*;
import redis.clients.jedis.providers.ConnectionProvider;
import redis.clients.jedis.providers.SentineledConnectionProvider;
import redis.clients.jedis.util.ReadOnlyCommands;

/**
* Builder for creating JedisSentineled instances (Redis Sentinel connections).
Expand All @@ -21,6 +22,10 @@ public abstract class SentinelClientBuilder<C>
private Set<HostAndPort> sentinels = null;
private JedisClientConfig sentinelClientConfig = null;

private ReadFrom readFrom = ReadFrom.UPSTREAM;

private ReadOnlyCommands.ReadOnlyPredicate readOnlyPredicate = ReadOnlyCommands.asPredicate();

/**
* Sets the master name for the Redis Sentinel configuration.
* <p>
Expand All @@ -47,6 +52,34 @@ public SentinelClientBuilder<C> sentinels(Set<HostAndPort> sentinels) {
return this;
}

/**
* Sets the readFrom.
* <p>
* It is used to specify the policy preference of which nodes the client should read data from. It
* defines which type of node the client should prioritize reading data from when there are
* multiple Redis instances (such as master nodes and slave nodes) available in the Redis Sentinel
* environment.
* @param readFrom the read preferences
* @return this builder
*/
public SentinelClientBuilder<C> readForm(ReadFrom readFrom) {
this.readFrom = readFrom;
return this;
}

/**
* Sets the readOnlyPredicate.
* <p>
* Check a Redis command is a read request.
* @param readOnlyPredicate
* @return this builder
*/
public SentinelClientBuilder<C> readOnlyPredicate(
ReadOnlyCommands.ReadOnlyPredicate readOnlyPredicate) {
this.readOnlyPredicate = readOnlyPredicate;
return this;
}

/**
* Sets the client configuration for Sentinel connections.
* <p>
Expand All @@ -68,7 +101,8 @@ protected SentinelClientBuilder<C> self() {
@Override
protected ConnectionProvider createDefaultConnectionProvider() {
return new SentineledConnectionProvider(this.masterName, this.clientConfig, this.cache,
this.poolConfig, this.sentinels, this.sentinelClientConfig);
this.poolConfig, this.sentinels, this.sentinelClientConfig, this.readFrom,
this.readOnlyPredicate);
}

@Override
Expand Down
Loading