Skip to content

Commit

Permalink
Rename readonly config param to specify Redis Cluster (#3932)
Browse files Browse the repository at this point in the history
* Rename readonly config param to specify Redis Cluster

* clarify javadoc
  • Loading branch information
sazzad16 authored Aug 22, 2024
1 parent 3b6b2b0 commit beadf6a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ private void initializeFromClientConfig(final JedisClientConfig config) {
}
}

// set readonly flag to ALL connections (including master nodes) when enable read from replica
if (config.isReadOnlyForReplica()) {
// set READONLY flag to ALL connections (including master nodes) when enable read from replica
if (config.isReadOnlyForRedisClusterReplicas()) {
fireAndForgetMsg.add(new CommandArguments(Command.READONLY));
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/redis/clients/jedis/DefaultJedisClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public final class DefaultJedisClientConfig implements JedisClientConfig {

private final ClientSetInfoConfig clientSetInfoConfig;

private final boolean readOnlyForReplica;
private final boolean readOnlyForRedisClusterReplicas;

private DefaultJedisClientConfig(RedisProtocol protocol, int connectionTimeoutMillis, int soTimeoutMillis,
int blockingSocketTimeoutMillis, Supplier<RedisCredentials> credentialsProvider, int database,
String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMapper,
ClientSetInfoConfig clientSetInfoConfig, boolean readOnlyForReplica) {
ClientSetInfoConfig clientSetInfoConfig, boolean readOnlyForRedisClusterReplicas) {
this.redisProtocol = protocol;
this.connectionTimeoutMillis = connectionTimeoutMillis;
this.socketTimeoutMillis = soTimeoutMillis;
Expand All @@ -46,7 +46,7 @@ private DefaultJedisClientConfig(RedisProtocol protocol, int connectionTimeoutMi
this.hostnameVerifier = hostnameVerifier;
this.hostAndPortMapper = hostAndPortMapper;
this.clientSetInfoConfig = clientSetInfoConfig;
this.readOnlyForReplica = readOnlyForReplica;
this.readOnlyForRedisClusterReplicas = readOnlyForRedisClusterReplicas;
}

@Override
Expand Down Expand Up @@ -126,8 +126,8 @@ public ClientSetInfoConfig getClientSetInfoConfig() {
}

@Override
public boolean isReadOnlyForReplica() {
return readOnlyForReplica;
public boolean isReadOnlyForRedisClusterReplicas() {
return readOnlyForRedisClusterReplicas;
}

public static Builder builder() {
Expand Down Expand Up @@ -157,7 +157,7 @@ public static class Builder {

private ClientSetInfoConfig clientSetInfoConfig = ClientSetInfoConfig.DEFAULT;

private boolean readOnlyForReplicas = false;
private boolean readOnlyForRedisClusterReplicas = false;

private Builder() {
}
Expand All @@ -171,7 +171,7 @@ public DefaultJedisClientConfig build() {
return new DefaultJedisClientConfig(redisProtocol, connectionTimeoutMillis, socketTimeoutMillis,
blockingSocketTimeoutMillis, credentialsProvider, database, clientName, ssl,
sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMapper, clientSetInfoConfig,
readOnlyForReplicas);
readOnlyForRedisClusterReplicas);
}

/**
Expand Down Expand Up @@ -267,8 +267,8 @@ public Builder clientSetInfoConfig(ClientSetInfoConfig setInfoConfig) {
return this;
}

public Builder readOnlyForReplicas() {
this.readOnlyForReplicas = true;
public Builder readOnlyForRedisClusterReplicas() {
this.readOnlyForRedisClusterReplicas = true;
return this;
}
}
Expand All @@ -290,6 +290,6 @@ public static DefaultJedisClientConfig copyConfig(JedisClientConfig copy) {
copy.getBlockingSocketTimeoutMillis(), copy.getCredentialsProvider(),
copy.getDatabase(), copy.getClientName(), copy.isSsl(), copy.getSslSocketFactory(),
copy.getSslParameters(), copy.getHostnameVerifier(), copy.getHostAndPortMapper(),
copy.getClientSetInfoConfig(), copy.isReadOnlyForReplica());
copy.getClientSetInfoConfig(), copy.isReadOnlyForRedisClusterReplicas());
}
}
11 changes: 9 additions & 2 deletions src/main/java/redis/clients/jedis/JedisClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default String getClientName() {
}

/**
* @return <code>true</code> - to create a TLS connection. <code>false</code> - otherwise.
* @return {@code true} - to create TLS connection(s). {@code false} - otherwise.
*/
default boolean isSsl() {
return false;
Expand All @@ -80,7 +80,14 @@ default HostAndPortMapper getHostAndPortMapper() {
return null;
}

default boolean isReadOnlyForReplica() {
/**
* Execute READONLY command to connections.
* <p>
* READONLY command is specific to Redis Cluster replica nodes. So this config param is only
* intended for Redis Cluster connections.
* @return {@code true} - to execute READONLY command to connection(s). {@code false} - otherwise.
*/
default boolean isReadOnlyForRedisClusterReplicas() {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/redis/clients/jedis/JedisClusterInfoCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig,
topologyRefreshExecutor.scheduleWithFixedDelay(new TopologyRefreshTask(), topologyRefreshPeriod.toMillis(),
topologyRefreshPeriod.toMillis(), TimeUnit.MILLISECONDS);
}
if (clientConfig.isReadOnlyForReplica()) {
if (clientConfig.isReadOnlyForRedisClusterReplicas()) {
replicaSlots = new ArrayList[Protocol.CLUSTER_HASHSLOTS];
} else {
replicaSlots = null;
Expand Down Expand Up @@ -150,7 +150,7 @@ public void discoverClusterNodesAndSlots(Connection jedis) {
setupNodeIfNotExist(targetNode);
if (i == MASTER_NODE_INDEX) {
assignSlotsToNode(slotNums, targetNode);
} else if (clientConfig.isReadOnlyForReplica()) {
} else if (clientConfig.isReadOnlyForRedisClusterReplicas()) {
assignSlotsToReplicaNode(slotNums, targetNode);
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ private void discoverClusterSlots(Connection jedis) {
setupNodeIfNotExist(targetNode);
if (i == MASTER_NODE_INDEX) {
assignSlotsToNode(slotNums, targetNode);
} else if (clientConfig.isReadOnlyForReplica()) {
} else if (clientConfig.isReadOnlyForRedisClusterReplicas()) {
assignSlotsToReplicaNode(slotNums, targetNode);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/redis/clients/jedis/JedisClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ public void testReadFromReplicas() throws Exception {
}
}

DefaultJedisClientConfig READ_REPLICAS_CLIENT_CONFIG
= DefaultJedisClientConfig.builder().password("cluster").readOnlyForReplicas().build();
DefaultJedisClientConfig READ_REPLICAS_CLIENT_CONFIG = DefaultJedisClientConfig.builder()
.password("cluster").readOnlyForRedisClusterReplicas().build();
ClusterCommandObjects commandObjects = new ClusterCommandObjects();
try (JedisCluster jedisCluster = new JedisCluster(nodeInfo1, READ_REPLICAS_CLIENT_CONFIG,
DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
Expand Down

0 comments on commit beadf6a

Please sign in to comment.