Skip to content

Commit cf60d25

Browse files
authored
Fix probable missing (RESP3) protocol processing (#3692)
* Fix probable missing (RESP3) protocol processing * undo commented lines
1 parent 8cad5ac commit cf60d25

File tree

4 files changed

+99
-81
lines changed

4 files changed

+99
-81
lines changed

src/main/java/redis/clients/jedis/JedisCluster.java

+28-15
Original file line numberDiff line numberDiff line change
@@ -172,43 +172,56 @@ public JedisCluster(Set<HostAndPort> clusterNodes, int connectionTimeout, int so
172172
maxAttempts, poolConfig);
173173
}
174174

175-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig,
176-
int maxAttempts, GenericObjectPoolConfig<Connection> poolConfig) {
177-
this(clusterNodes, clientConfig, maxAttempts,
178-
Duration.ofMillis((long) clientConfig.getSocketTimeoutMillis() * maxAttempts), poolConfig);
179-
}
180-
181-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig,
182-
int maxAttempts, Duration maxTotalRetriesDuration,
183-
GenericObjectPoolConfig<Connection> poolConfig) {
184-
super(clusterNodes, clientConfig, poolConfig, maxAttempts, maxTotalRetriesDuration);
185-
}
186-
187175
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig) {
188176
this(clusterNodes, clientConfig, DEFAULT_MAX_ATTEMPTS);
189177
}
190178

191179
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, int maxAttempts) {
192-
super(clusterNodes, clientConfig, maxAttempts);
180+
this(clusterNodes, clientConfig, maxAttempts,
181+
Duration.ofMillis((long) clientConfig.getSocketTimeoutMillis() * maxAttempts));
193182
}
194183

195184
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, int maxAttempts,
196185
Duration maxTotalRetriesDuration) {
197-
super(clusterNodes, clientConfig, maxAttempts, maxTotalRetriesDuration);
186+
this(new ClusterConnectionProvider(clusterNodes, clientConfig), maxAttempts, maxTotalRetriesDuration,
187+
clientConfig.getRedisProtocol());
188+
}
189+
190+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig,
191+
GenericObjectPoolConfig<Connection> poolConfig) {
192+
this(clusterNodes, clientConfig, DEFAULT_MAX_ATTEMPTS, poolConfig);
193+
}
194+
195+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, int maxAttempts,
196+
GenericObjectPoolConfig<Connection> poolConfig) {
197+
this(clusterNodes, clientConfig, maxAttempts,
198+
Duration.ofMillis((long) clientConfig.getSocketTimeoutMillis() * maxAttempts), poolConfig);
198199
}
199200

200201
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig,
201202
GenericObjectPoolConfig<Connection> poolConfig, Duration topologyRefreshPeriod, int maxAttempts,
202203
Duration maxTotalRetriesDuration) {
203204
this(new ClusterConnectionProvider(clusterNodes, clientConfig, poolConfig, topologyRefreshPeriod),
204-
maxAttempts, maxTotalRetriesDuration);
205+
maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol());
206+
}
207+
208+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, int maxAttempts,
209+
Duration maxTotalRetriesDuration, GenericObjectPoolConfig<Connection> poolConfig) {
210+
this(new ClusterConnectionProvider(clusterNodes, clientConfig, poolConfig), maxAttempts, maxTotalRetriesDuration,
211+
clientConfig.getRedisProtocol());
205212
}
206213

214+
// Uses a fetched connection to process protocol. Should be avoided if possible.
207215
public JedisCluster(ClusterConnectionProvider provider, int maxAttempts,
208216
Duration maxTotalRetriesDuration) {
209217
super(provider, maxAttempts, maxTotalRetriesDuration);
210218
}
211219

220+
private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration,
221+
RedisProtocol protocol) {
222+
super(provider, maxAttempts, maxTotalRetriesDuration, protocol);
223+
}
224+
212225
public Map<String, ConnectionPool> getClusterNodes() {
213226
return ((ClusterConnectionProvider) provider).getNodes();
214227
}

src/main/java/redis/clients/jedis/JedisPooled.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public JedisPooled(final String host, final int port) {
5353
}
5454

5555
public JedisPooled(final HostAndPort hostAndPort) {
56-
this(new PooledConnectionProvider(hostAndPort));
56+
super(hostAndPort);
5757
}
5858

5959
public JedisPooled(final String host, final int port, final boolean ssl) {
@@ -73,7 +73,7 @@ public JedisPooled(final String host, final int port, final String user, final S
7373
}
7474

7575
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) {
76-
this(new PooledConnectionProvider(hostAndPort, clientConfig));
76+
super(hostAndPort, clientConfig);
7777
}
7878

7979
public JedisPooled(PooledObjectFactory<Connection> factory) {
@@ -373,12 +373,13 @@ public JedisPooled(final GenericObjectPoolConfig<Connection> poolConfig, final H
373373

374374
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig,
375375
final GenericObjectPoolConfig<Connection> poolConfig) {
376-
this(new PooledConnectionProvider(hostAndPort, clientConfig, poolConfig));
376+
super(new PooledConnectionProvider(hostAndPort, clientConfig, poolConfig), clientConfig.getRedisProtocol());
377377
}
378378

379379
public JedisPooled(final GenericObjectPoolConfig<Connection> poolConfig,
380380
final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) {
381-
this(new ConnectionFactory(jedisSocketFactory, clientConfig), poolConfig);
381+
super(new PooledConnectionProvider(new ConnectionFactory(jedisSocketFactory, clientConfig), poolConfig),
382+
clientConfig.getRedisProtocol());
382383
}
383384

384385
public JedisPooled(GenericObjectPoolConfig<Connection> poolConfig, PooledObjectFactory<Connection> factory) {

src/main/java/redis/clients/jedis/JedisSentineled.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ public class JedisSentineled extends UnifiedJedis {
88

99
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig,
1010
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig) {
11-
this(new SentineledConnectionProvider(masterName, masterClientConfig, sentinels, sentinelClientConfig));
11+
super(new SentineledConnectionProvider(masterName, masterClientConfig, sentinels, sentinelClientConfig),
12+
masterClientConfig.getRedisProtocol());
1213
}
1314

1415
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig,
1516
final GenericObjectPoolConfig<Connection> poolConfig,
1617
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig) {
17-
this(new SentineledConnectionProvider(masterName, masterClientConfig, poolConfig, sentinels, sentinelClientConfig));
18+
super(new SentineledConnectionProvider(masterName, masterClientConfig, poolConfig, sentinels, sentinelClientConfig),
19+
masterClientConfig.getRedisProtocol());
1820
}
1921

2022
public JedisSentineled(SentineledConnectionProvider sentineledConnectionProvider) {

src/main/java/redis/clients/jedis/UnifiedJedis.java

+62-60
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public UnifiedJedis() {
6060
}
6161

6262
public UnifiedJedis(HostAndPort hostAndPort) {
63-
// this(new Connection(hostAndPort));
64-
this(new PooledConnectionProvider(hostAndPort));
63+
this(new PooledConnectionProvider(hostAndPort), (RedisProtocol) null);
6564
}
6665

6766
public UnifiedJedis(final String url) {
@@ -89,27 +88,15 @@ public UnifiedJedis(final URI uri, JedisClientConfig config) {
8988
}
9089

9190
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
92-
// this(new Connection(hostAndPort, clientConfig));
93-
this(new PooledConnectionProvider(hostAndPort, clientConfig));
94-
RedisProtocol proto = clientConfig.getRedisProtocol();
95-
if (proto != null) commandObjects.setProtocol(proto);
91+
this(new PooledConnectionProvider(hostAndPort, clientConfig), clientConfig.getRedisProtocol());
9692
}
9793

9894
public UnifiedJedis(ConnectionProvider provider) {
99-
this.provider = provider;
100-
this.executor = new DefaultCommandExecutor(provider);
101-
this.commandObjects = new CommandObjects();
102-
this.graphCommandObjects = new GraphCommandObjects(this);
103-
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
104-
try (Connection conn = this.provider.getConnection()) {
105-
if (conn != null) {
106-
RedisProtocol proto = conn.getRedisProtocol();
107-
if (proto != null) commandObjects.setProtocol(proto);
108-
}
109-
//} catch (JedisAccessControlException ace) {
110-
} catch (JedisException je) { // TODO: use specific exception(s)
111-
// use default protocol
112-
}
95+
this(new DefaultCommandExecutor(provider), provider);
96+
}
97+
98+
protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol) {
99+
this(new DefaultCommandExecutor(provider), provider, new CommandObjects(), protocol);
113100
}
114101

115102
/**
@@ -142,69 +129,60 @@ public UnifiedJedis(Connection connection) {
142129
this.provider = null;
143130
this.executor = new SimpleCommandExecutor(connection);
144131
this.commandObjects = new CommandObjects();
145-
this.graphCommandObjects = new GraphCommandObjects(this);
146132
RedisProtocol proto = connection.getRedisProtocol();
147-
if (proto == RedisProtocol.RESP3) this.commandObjects.setProtocol(proto);
133+
if (proto != null) this.commandObjects.setProtocol(proto);
134+
this.graphCommandObjects = new GraphCommandObjects(this);
148135
}
149136

137+
@Deprecated
150138
public UnifiedJedis(Set<HostAndPort> jedisClusterNodes, JedisClientConfig clientConfig, int maxAttempts) {
151-
this(new ClusterConnectionProvider(jedisClusterNodes, clientConfig), maxAttempts,
152-
Duration.ofMillis(maxAttempts * clientConfig.getSocketTimeoutMillis()));
153-
RedisProtocol proto = clientConfig.getRedisProtocol();
154-
if (proto != null) commandObjects.setProtocol(proto);
139+
this(jedisClusterNodes, clientConfig, maxAttempts, Duration.ofMillis(maxAttempts * clientConfig.getSocketTimeoutMillis()));
155140
}
156141

157-
public UnifiedJedis(Set<HostAndPort> jedisClusterNodes, JedisClientConfig clientConfig, int maxAttempts, Duration maxTotalRetriesDuration) {
158-
this(new ClusterConnectionProvider(jedisClusterNodes, clientConfig), maxAttempts, maxTotalRetriesDuration);
159-
RedisProtocol proto = clientConfig.getRedisProtocol();
160-
if (proto != null) commandObjects.setProtocol(proto);
142+
@Deprecated
143+
public UnifiedJedis(Set<HostAndPort> jedisClusterNodes, JedisClientConfig clientConfig, int maxAttempts,
144+
Duration maxTotalRetriesDuration) {
145+
this(new ClusterConnectionProvider(jedisClusterNodes, clientConfig), maxAttempts, maxTotalRetriesDuration,
146+
clientConfig.getRedisProtocol());
161147
}
162148

149+
@Deprecated
163150
public UnifiedJedis(Set<HostAndPort> jedisClusterNodes, JedisClientConfig clientConfig,
164151
GenericObjectPoolConfig<Connection> poolConfig, int maxAttempts, Duration maxTotalRetriesDuration) {
165-
this(new ClusterConnectionProvider(jedisClusterNodes, clientConfig, poolConfig), maxAttempts, maxTotalRetriesDuration);
166-
RedisProtocol proto = clientConfig.getRedisProtocol();
167-
if (proto != null) commandObjects.setProtocol(proto);
152+
this(new ClusterConnectionProvider(jedisClusterNodes, clientConfig, poolConfig), maxAttempts,
153+
maxTotalRetriesDuration, clientConfig.getRedisProtocol());
168154
}
169155

156+
// Uses a fetched connection to process protocol. Should be avoided if possible.
170157
public UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration) {
171-
this.provider = provider;
172-
this.executor = new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration);
173-
this.commandObjects = new ClusterCommandObjects();
174-
this.graphCommandObjects = new GraphCommandObjects(this);
175-
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
158+
this(new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider,
159+
new ClusterCommandObjects());
160+
}
161+
162+
protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration,
163+
RedisProtocol protocol) {
164+
this(new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider,
165+
new ClusterCommandObjects(), protocol);
176166
}
177167

178168
/**
179169
* @deprecated Sharding/Sharded feature will be removed in next major release.
180170
*/
181171
@Deprecated
182172
public UnifiedJedis(ShardedConnectionProvider provider) {
183-
this.provider = provider;
184-
this.executor = new DefaultCommandExecutor(provider);
185-
this.commandObjects = new ShardedCommandObjects(provider.getHashingAlgo());
186-
this.graphCommandObjects = new GraphCommandObjects(this);
187-
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
173+
this(new DefaultCommandExecutor(provider), provider, new ShardedCommandObjects(provider.getHashingAlgo()));
188174
}
189175

190176
/**
191177
* @deprecated Sharding/Sharded feature will be removed in next major release.
192178
*/
193179
@Deprecated
194180
public UnifiedJedis(ShardedConnectionProvider provider, Pattern tagPattern) {
195-
this.provider = provider;
196-
this.executor = new DefaultCommandExecutor(provider);
197-
this.commandObjects = new ShardedCommandObjects(provider.getHashingAlgo(), tagPattern);
198-
this.graphCommandObjects = new GraphCommandObjects(this);
199-
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
181+
this(new DefaultCommandExecutor(provider), provider, new ShardedCommandObjects(provider.getHashingAlgo(), tagPattern));
200182
}
201183

202184
public UnifiedJedis(ConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration) {
203-
this.provider = provider;
204-
this.executor = new RetryableCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration);
205-
this.commandObjects = new CommandObjects();
206-
this.graphCommandObjects = new GraphCommandObjects(this);
207-
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
185+
this(new RetryableCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider);
208186
}
209187

210188
/**
@@ -215,11 +193,7 @@ public UnifiedJedis(ConnectionProvider provider, int maxAttempts, Duration maxTo
215193
* <p>
216194
*/
217195
public UnifiedJedis(MultiClusterPooledConnectionProvider provider) {
218-
this.provider = provider;
219-
this.executor = new CircuitBreakerCommandExecutor(provider);
220-
this.commandObjects = new CommandObjects();
221-
this.graphCommandObjects = new GraphCommandObjects(this);
222-
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
196+
this(new CircuitBreakerCommandExecutor(provider), provider);
223197
}
224198

225199
/**
@@ -229,9 +203,36 @@ public UnifiedJedis(MultiClusterPooledConnectionProvider provider) {
229203
* {@link UnifiedJedis#provider} is accessed.
230204
*/
231205
public UnifiedJedis(CommandExecutor executor) {
232-
this.provider = null;
206+
this(executor, (ConnectionProvider) null);
207+
}
208+
209+
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider) {
210+
this(executor, provider, new CommandObjects());
211+
}
212+
213+
// Uses a fetched connection to process protocol. Should be avoided if possible.
214+
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects) {
215+
this(executor, provider, commandObjects, null);
216+
if (this.provider != null) {
217+
try (Connection conn = this.provider.getConnection()) {
218+
if (conn != null) {
219+
RedisProtocol proto = conn.getRedisProtocol();
220+
if (proto != null) this.commandObjects.setProtocol(proto);
221+
}
222+
} catch (JedisException je) { }
223+
}
224+
}
225+
226+
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects,
227+
RedisProtocol protocol) {
228+
this.provider = provider;
233229
this.executor = executor;
234-
this.commandObjects = new CommandObjects();
230+
231+
this.commandObjects = commandObjects;
232+
if (protocol != null) {
233+
this.commandObjects.setProtocol(protocol);
234+
}
235+
235236
this.graphCommandObjects = new GraphCommandObjects(this);
236237
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
237238
}
@@ -241,6 +242,7 @@ public void close() {
241242
IOUtils.closeQuietly(this.executor);
242243
}
243244

245+
@Deprecated
244246
protected final void setProtocol(RedisProtocol protocol) {
245247
this.protocol = protocol;
246248
this.commandObjects.setProtocol(this.protocol);

0 commit comments

Comments
 (0)