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

Rename readonly config param to specify Redis Cluster #3932

Merged
merged 2 commits into from
Aug 22, 2024
Merged
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
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 @@ -497,8 +497,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
Loading