Skip to content

Commit b17d3e1

Browse files
committed
Use command flags
1 parent bdfe389 commit b17d3e1

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/main/java/redis/clients/jedis/executors/ClusterCommandExecutor.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,18 @@ public final <T> T executeKeylessCommand(CommandObject<T> commandObject) {
102102
int consecutiveConnectionFailures = 0;
103103
Exception lastException = null;
104104

105+
RequiredConnectionType connectionType;
106+
if (commandObject.getFlags().contains(CommandObject.CommandFlag.READONLY)) {
107+
connectionType = RequiredConnectionType.REPLICA;
108+
} else {
109+
connectionType = RequiredConnectionType.PRIMARY;
110+
}
111+
105112
for (int attemptsLeft = this.maxAttempts; attemptsLeft > 0; attemptsLeft--) {
106113
Connection connection = null;
107114
try {
108115
// Use round-robin distribution for keyless commands
109-
connection = getNextConnection();
116+
connection = getNextConnection(connectionType);
110117
return execute(connection, commandObject);
111118

112119
} catch (JedisConnectionException jce) {
@@ -209,23 +216,20 @@ private <T> T doExecuteCommand(CommandObject<T> commandObject, boolean toReplica
209216
throw maxAttemptsException;
210217
}
211218

219+
private enum RequiredConnectionType {
220+
PRIMARY,
221+
REPLICA
222+
}
223+
212224
/**
213225
* Gets a connection using round-robin distribution across all cluster nodes.
214226
* This ensures even distribution of keyless commands across the cluster.
215227
*
216228
* @return Connection from the next node in round-robin sequence
217229
* @throws JedisClusterOperationException if no cluster nodes are available
218230
*/
219-
private Connection getNextConnection() {
220-
Map<String, ConnectionPool> connectionMap = provider.getConnectionMap();
221-
222-
if (connectionMap.isEmpty()) {
223-
throw new JedisClusterOperationException("No cluster nodes available.");
224-
}
225-
226-
// Convert connection map to list for round-robin access
227-
List<Map.Entry<String, ConnectionPool>> nodeList = new ArrayList<>(connectionMap.entrySet());
228-
231+
private Connection getNextConnection(RequiredConnectionType connectionType) {
232+
List<Map.Entry<String, ConnectionPool>> nodeList = selectNextConnectionPool(connectionType);
229233
// Select node using round-robin distribution for true unified distribution
230234
// Use modulo directly on the node list size to create a circular counter
231235
int roundRobinIndex = roundRobinCounter.getAndUpdate(current -> (current + 1) % nodeList.size());
@@ -235,7 +239,25 @@ private Connection getNextConnection() {
235239
return pool.getResource();
236240
}
237241

238-
/**
242+
private List<Map.Entry<String, ConnectionPool>> selectNextConnectionPool(RequiredConnectionType connectionType) {
243+
Map<String, ConnectionPool> connectionMap;
244+
245+
// NOTE(imalinovskyi): If we need to connect to replica, we use all nodes, otherwise we use only primary nodes
246+
if (connectionType == RequiredConnectionType.REPLICA) {
247+
connectionMap = provider.getConnectionMap();
248+
} else {
249+
connectionMap = provider.getPrimaryNodesConnectionMap();
250+
}
251+
252+
if (connectionMap.isEmpty()) {
253+
throw new JedisClusterOperationException("No cluster nodes available.");
254+
}
255+
256+
// Convert connection map to list for round-robin access
257+
return new ArrayList<>(connectionMap.entrySet());
258+
}
259+
260+
/**
239261
* WARNING: This method is accessible for the purpose of testing.
240262
* This should not be used or overriden.
241263
*/

0 commit comments

Comments
 (0)