@@ -99,11 +99,18 @@ public final <T> T executeKeylessCommand(CommandObject<T> commandObject) {
9999 int consecutiveConnectionFailures = 0 ;
100100 Exception lastException = null ;
101101
102+ RequiredConnectionType connectionType ;
103+ if (commandObject .getFlags ().contains (CommandObject .CommandFlag .READONLY )) {
104+ connectionType = RequiredConnectionType .REPLICA ;
105+ } else {
106+ connectionType = RequiredConnectionType .PRIMARY ;
107+ }
108+
102109 for (int attemptsLeft = this .maxAttempts ; attemptsLeft > 0 ; attemptsLeft --) {
103110 Connection connection = null ;
104111 try {
105112 // Use round-robin distribution for keyless commands
106- connection = getNextConnection ();
113+ connection = getNextConnection (connectionType );
107114 return execute (connection , commandObject );
108115
109116 } catch (JedisConnectionException jce ) {
@@ -206,23 +213,20 @@ private <T> T doExecuteCommand(CommandObject<T> commandObject, boolean toReplica
206213 throw maxAttemptsException ;
207214 }
208215
216+ private enum RequiredConnectionType {
217+ PRIMARY ,
218+ REPLICA
219+ }
220+
209221 /**
210222 * Gets a connection using round-robin distribution across all cluster nodes.
211223 * This ensures even distribution of keyless commands across the cluster.
212224 *
213225 * @return Connection from the next node in round-robin sequence
214226 * @throws JedisClusterOperationException if no cluster nodes are available
215227 */
216- private Connection getNextConnection () {
217- Map <String , ConnectionPool > connectionMap = provider .getConnectionMap ();
218-
219- if (connectionMap .isEmpty ()) {
220- throw new JedisClusterOperationException ("No cluster nodes available." );
221- }
222-
223- // Convert connection map to list for round-robin access
224- List <Map .Entry <String , ConnectionPool >> nodeList = new ArrayList <>(connectionMap .entrySet ());
225-
228+ private Connection getNextConnection (RequiredConnectionType connectionType ) {
229+ List <Map .Entry <String , ConnectionPool >> nodeList = selectNextConnectionPool (connectionType );
226230 // Select node using round-robin distribution for true unified distribution
227231 // Use modulo directly on the node list size to create a circular counter
228232 int roundRobinIndex = roundRobinCounter .getAndUpdate (current -> (current + 1 ) % nodeList .size ());
@@ -232,7 +236,25 @@ private Connection getNextConnection() {
232236 return pool .getResource ();
233237 }
234238
235- /**
239+ private List <Map .Entry <String , ConnectionPool >> selectNextConnectionPool (RequiredConnectionType connectionType ) {
240+ Map <String , ConnectionPool > connectionMap ;
241+
242+ // NOTE(imalinovskyi): If we need to connect to replica, we use all nodes, otherwise we use only primary nodes
243+ if (connectionType == RequiredConnectionType .REPLICA ) {
244+ connectionMap = provider .getConnectionMap ();
245+ } else {
246+ connectionMap = provider .getPrimaryNodesConnectionMap ();
247+ }
248+
249+ if (connectionMap .isEmpty ()) {
250+ throw new JedisClusterOperationException ("No cluster nodes available." );
251+ }
252+
253+ // Convert connection map to list for round-robin access
254+ return new ArrayList <>(connectionMap .entrySet ());
255+ }
256+
257+ /**
236258 * WARNING: This method is accessible for the purpose of testing.
237259 * This should not be used or overriden.
238260 */
0 commit comments