Skip to content

Commit 461456b

Browse files
committed
Adapt to API changes in the Jedis 5.0 driver.
Closes spring-projects#2612
1 parent aa34bf0 commit 461456b

15 files changed

+444
-319
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterHashCommands.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import org.springframework.data.redis.core.ScanCursor;
3131
import org.springframework.data.redis.core.ScanIteration;
3232
import org.springframework.data.redis.core.ScanOptions;
33-
import org.springframework.data.util.Streamable;
3433
import org.springframework.lang.Nullable;
3534
import org.springframework.util.Assert;
3635

3736
/**
37+
* Cluster {@link RedisHashCommands} implementation for Jedis.
38+
*
3839
* @author Christoph Strobl
3940
* @author Mark Paluch
41+
* @author John Blum
4042
* @since 2.0
4143
*/
4244
class JedisClusterHashCommands implements RedisHashCommands {
@@ -160,10 +162,10 @@ public Entry<byte[], byte[]> hRandFieldWithValues(byte[] key) {
160162
Assert.notNull(key, "Key must not be null");
161163

162164
try {
163-
Map<byte[], byte[]> map = connection.getCluster().hrandfieldWithValues(key, 1);
164-
return map.isEmpty() ? null : map.entrySet().iterator().next();
165-
} catch (Exception ex) {
166-
throw convertJedisAccessException(ex);
165+
List<Entry<byte[], byte[]>> mapEntryList = connection.getCluster().hrandfieldWithValues(key, 1);
166+
return mapEntryList.isEmpty() ? null : mapEntryList.get(0);
167+
} catch (Exception cause) {
168+
throw convertJedisAccessException(cause);
167169
}
168170
}
169171

@@ -185,10 +187,9 @@ public List<byte[]> hRandField(byte[] key, long count) {
185187
public List<Entry<byte[], byte[]>> hRandFieldWithValues(byte[] key, long count) {
186188

187189
try {
188-
Map<byte[], byte[]> map = connection.getCluster().hrandfieldWithValues(key, count);
189-
return Streamable.of(() -> map.entrySet().iterator()).toList();
190-
} catch (Exception ex) {
191-
throw convertJedisAccessException(ex);
190+
return connection.getCluster().hrandfieldWithValues(key, count);
191+
} catch (Exception cause) {
192+
throw convertJedisAccessException(cause);
192193
}
193194
}
194195

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterServerCommands.java

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
*/
1616
package org.springframework.data.redis.connection.jedis;
1717

18-
import redis.clients.jedis.Jedis;
19-
2018
import java.util.ArrayList;
2119
import java.util.Collection;
2220
import java.util.Collections;
2321
import java.util.List;
22+
import java.util.Map;
2423
import java.util.Map.Entry;
2524
import java.util.Properties;
2625
import java.util.concurrent.TimeUnit;
@@ -38,9 +37,14 @@
3837
import org.springframework.util.Assert;
3938
import org.springframework.util.CollectionUtils;
4039

40+
import redis.clients.jedis.Jedis;
41+
4142
/**
43+
* {@link RedisClusterServerCommands} implementation for Jedis.
44+
*
4245
* @author Mark Paluch
4346
* @author Dennis Neufeld
47+
* @author John Blum
4448
* @since 2.0
4549
*/
4650
class JedisClusterServerCommands implements RedisClusterServerCommands {
@@ -82,7 +86,8 @@ public Long lastSave() {
8286
return null;
8387
}
8488

85-
Collections.sort(result, Collections.reverseOrder());
89+
result.sort(Collections.reverseOrder());
90+
8691
return result.get(0);
8792
}
8893

@@ -221,20 +226,20 @@ public Properties info(RedisClusterNode node, String section) {
221226
public void shutdown() {
222227
connection.getClusterCommandExecutor().executeCommandOnAllNodes((JedisClusterCommandCallback<String>) jedis -> {
223228
jedis.shutdown();
224-
return null;
229+
return "success";
225230
});
226231
}
227232

228233
@Override
229234
public void shutdown(RedisClusterNode node) {
230235
executeCommandOnSingleNode(jedis -> {
231236
jedis.shutdown();
232-
return null;
237+
return "success";
233238
}, node);
234239
}
235240

236241
@Override
237-
public void shutdown(ShutdownOption option) {
242+
public void shutdown(@Nullable ShutdownOption option) {
238243

239244
if (option == null) {
240245
shutdown();
@@ -249,32 +254,37 @@ public Properties getConfig(String pattern) {
249254

250255
Assert.notNull(pattern, "Pattern must not be null");
251256

252-
List<NodeResult<List<String>>> mapResult = connection.getClusterCommandExecutor()
253-
.executeCommandOnAllNodes((JedisClusterCommandCallback<List<String>>) client -> client.configGet(pattern))
257+
JedisClusterCommandCallback<Map<String, String>> command = jedis -> jedis.configGet(pattern);
258+
259+
List<NodeResult<Map<String, String>>> nodeResults = connection.getClusterCommandExecutor()
260+
.executeCommandOnAllNodes(command)
254261
.getResults();
255262

256-
List<String> result = new ArrayList<>();
257-
for (NodeResult<List<String>> entry : mapResult) {
263+
Properties nodesConfiguration = new Properties();
264+
265+
for (NodeResult<Map<String, String>> nodeResult : nodeResults) {
266+
267+
String prefix = nodeResult.getNode().asString();
258268

259-
String prefix = entry.getNode().asString();
260-
int i = 0;
261-
for (String value : entry.getValue()) {
262-
result.add((i++ % 2 == 0 ? (prefix + ".") : "") + value);
269+
for (Entry<String, String> entry : nodeResult.getValue().entrySet()) {
270+
String newKey = prefix.concat(".").concat(entry.getKey());
271+
String value = entry.getValue();
272+
nodesConfiguration.setProperty(newKey, value);
263273
}
264274
}
265275

266-
return Converters.toProperties(result);
276+
return nodesConfiguration;
267277
}
268278

269279
@Override
270280
public Properties getConfig(RedisClusterNode node, String pattern) {
271281

272282
Assert.notNull(pattern, "Pattern must not be null");
273283

284+
JedisClusterCommandCallback<Properties> command = client -> Converters.toProperties(client.configGet(pattern));
285+
274286
return connection.getClusterCommandExecutor()
275-
.executeCommandOnSingleNode(
276-
(JedisClusterCommandCallback<Properties>) client -> Converters.toProperties(client.configGet(pattern)),
277-
node)
287+
.executeCommandOnSingleNode(command, node)
278288
.getValue();
279289
}
280290

@@ -322,19 +332,19 @@ public void rewriteConfig(RedisClusterNode node) {
322332
@Override
323333
public Long time(TimeUnit timeUnit) {
324334

325-
return convertListOfStringToTime(
326-
connection.getClusterCommandExecutor()
327-
.executeCommandOnArbitraryNode((JedisClusterCommandCallback<List<String>>) Jedis::time).getValue(),
328-
timeUnit);
335+
JedisClusterCommandCallback<List<String>> command = Jedis::time;
336+
337+
return convertListOfStringToTime(connection.getClusterCommandExecutor()
338+
.executeCommandOnArbitraryNode(command).getValue(), timeUnit);
329339
}
330340

331341
@Override
332342
public Long time(RedisClusterNode node, TimeUnit timeUnit) {
333343

334-
return convertListOfStringToTime(
335-
connection.getClusterCommandExecutor()
336-
.executeCommandOnSingleNode((JedisClusterCommandCallback<List<String>>) Jedis::time, node).getValue(),
337-
timeUnit);
344+
JedisClusterCommandCallback<List<String>> command = Jedis::time;
345+
346+
return convertListOfStringToTime(connection.getClusterCommandExecutor()
347+
.executeCommandOnSingleNode(command, node).getValue(), timeUnit);
338348
}
339349

340350
@Override
@@ -343,8 +353,9 @@ public void killClient(String host, int port) {
343353
Assert.hasText(host, "Host for 'CLIENT KILL' must not be 'null' or 'empty'");
344354
String hostAndPort = String.format("%s:%s", host, port);
345355

346-
connection.getClusterCommandExecutor()
347-
.executeCommandOnAllNodes((JedisClusterCommandCallback<String>) client -> client.clientKill(hostAndPort));
356+
JedisClusterCommandCallback<String> command = jedis -> jedis.clientKill(hostAndPort);
357+
358+
connection.getClusterCommandExecutor().executeCommandOnAllNodes(command);
348359
}
349360

350361
@Override
@@ -360,21 +371,26 @@ public String getClientName() {
360371
@Override
361372
public List<RedisClientInfo> getClientList() {
362373

374+
JedisClusterCommandCallback<String> command = Jedis::clientList;
375+
363376
Collection<String> map = connection.getClusterCommandExecutor()
364-
.executeCommandOnAllNodes((JedisClusterCommandCallback<String>) Jedis::clientList).resultsAsList();
377+
.executeCommandOnAllNodes(command).resultsAsList();
365378

366379
ArrayList<RedisClientInfo> result = new ArrayList<>();
380+
367381
for (String infos : map) {
368382
result.addAll(JedisConverters.toListOfRedisClientInformation(infos));
369383
}
384+
370385
return result;
371386
}
372387

373388
@Override
374389
public List<RedisClientInfo> getClientList(RedisClusterNode node) {
375390

376-
return JedisConverters
377-
.toListOfRedisClientInformation(executeCommandOnSingleNode(Jedis::clientList, node).getValue());
391+
JedisClusterCommandCallback<String> command = Jedis::clientList;
392+
393+
return JedisConverters.toListOfRedisClientInformation(executeCommandOnSingleNode(command, node).getValue());
378394
}
379395

380396
@Override
@@ -403,8 +419,10 @@ public void migrate(byte[] key, RedisNode target, int dbIndex, @Nullable Migrate
403419

404420
RedisClusterNode node = connection.getTopologyProvider().getTopology().lookup(target.getHost(), target.getPort());
405421

406-
executeCommandOnSingleNode(client -> client.migrate(target.getHost(), target.getPort(), key, dbIndex, timeoutToUse),
407-
node);
422+
JedisClusterCommandCallback<String> command = jedis ->
423+
jedis.migrate(target.getHost(), target.getPort(), key, dbIndex, timeoutToUse);
424+
425+
executeCommandOnSingleNode(command, node);
408426
}
409427

410428
private Long convertListOfStringToTime(List<String> serverTimeInformation, TimeUnit timeUnit) {
@@ -416,12 +434,11 @@ private Long convertListOfStringToTime(List<String> serverTimeInformation, TimeU
416434
return Converters.toTimeMillis(serverTimeInformation.get(0), serverTimeInformation.get(1), timeUnit);
417435
}
418436

419-
private <T> NodeResult<T> executeCommandOnSingleNode(JedisClusterCommandCallback<T> cmd, RedisClusterNode node) {
420-
return connection.getClusterCommandExecutor().executeCommandOnSingleNode(cmd, node);
437+
private <T> NodeResult<T> executeCommandOnSingleNode(JedisClusterCommandCallback<T> command, RedisClusterNode node) {
438+
return connection.getClusterCommandExecutor().executeCommandOnSingleNode(command, node);
421439
}
422440

423-
private <T> MultiNodeResult<T> executeCommandOnAllNodes(JedisClusterCommandCallback<T> cmd) {
424-
return connection.getClusterCommandExecutor().executeCommandOnAllNodes(cmd);
441+
private <T> MultiNodeResult<T> executeCommandOnAllNodes(JedisClusterCommandCallback<T> command) {
442+
return connection.getClusterCommandExecutor().executeCommandOnAllNodes(command);
425443
}
426-
427444
}

0 commit comments

Comments
 (0)