15
15
*/
16
16
package org .springframework .data .redis .connection .jedis ;
17
17
18
- import redis .clients .jedis .Jedis ;
19
-
20
18
import java .util .ArrayList ;
21
19
import java .util .Collection ;
22
20
import java .util .Collections ;
23
21
import java .util .List ;
22
+ import java .util .Map ;
24
23
import java .util .Map .Entry ;
25
24
import java .util .Properties ;
26
25
import java .util .concurrent .TimeUnit ;
38
37
import org .springframework .util .Assert ;
39
38
import org .springframework .util .CollectionUtils ;
40
39
40
+ import redis .clients .jedis .Jedis ;
41
+
41
42
/**
43
+ * {@link RedisClusterServerCommands} implementation for Jedis.
44
+ *
42
45
* @author Mark Paluch
43
46
* @author Dennis Neufeld
47
+ * @author John Blum
44
48
* @since 2.0
45
49
*/
46
50
class JedisClusterServerCommands implements RedisClusterServerCommands {
@@ -82,7 +86,8 @@ public Long lastSave() {
82
86
return null ;
83
87
}
84
88
85
- Collections .sort (result , Collections .reverseOrder ());
89
+ result .sort (Collections .reverseOrder ());
90
+
86
91
return result .get (0 );
87
92
}
88
93
@@ -221,20 +226,20 @@ public Properties info(RedisClusterNode node, String section) {
221
226
public void shutdown () {
222
227
connection .getClusterCommandExecutor ().executeCommandOnAllNodes ((JedisClusterCommandCallback <String >) jedis -> {
223
228
jedis .shutdown ();
224
- return null ;
229
+ return "success" ;
225
230
});
226
231
}
227
232
228
233
@ Override
229
234
public void shutdown (RedisClusterNode node ) {
230
235
executeCommandOnSingleNode (jedis -> {
231
236
jedis .shutdown ();
232
- return null ;
237
+ return "success" ;
233
238
}, node );
234
239
}
235
240
236
241
@ Override
237
- public void shutdown (ShutdownOption option ) {
242
+ public void shutdown (@ Nullable ShutdownOption option ) {
238
243
239
244
if (option == null ) {
240
245
shutdown ();
@@ -249,32 +254,37 @@ public Properties getConfig(String pattern) {
249
254
250
255
Assert .notNull (pattern , "Pattern must not be null" );
251
256
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 )
254
261
.getResults ();
255
262
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 ();
258
268
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 );
263
273
}
264
274
}
265
275
266
- return Converters . toProperties ( result ) ;
276
+ return nodesConfiguration ;
267
277
}
268
278
269
279
@ Override
270
280
public Properties getConfig (RedisClusterNode node , String pattern ) {
271
281
272
282
Assert .notNull (pattern , "Pattern must not be null" );
273
283
284
+ JedisClusterCommandCallback <Properties > command = client -> Converters .toProperties (client .configGet (pattern ));
285
+
274
286
return connection .getClusterCommandExecutor ()
275
- .executeCommandOnSingleNode (
276
- (JedisClusterCommandCallback <Properties >) client -> Converters .toProperties (client .configGet (pattern )),
277
- node )
287
+ .executeCommandOnSingleNode (command , node )
278
288
.getValue ();
279
289
}
280
290
@@ -322,19 +332,19 @@ public void rewriteConfig(RedisClusterNode node) {
322
332
@ Override
323
333
public Long time (TimeUnit timeUnit ) {
324
334
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 );
329
339
}
330
340
331
341
@ Override
332
342
public Long time (RedisClusterNode node , TimeUnit timeUnit ) {
333
343
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 );
338
348
}
339
349
340
350
@ Override
@@ -343,8 +353,9 @@ public void killClient(String host, int port) {
343
353
Assert .hasText (host , "Host for 'CLIENT KILL' must not be 'null' or 'empty'" );
344
354
String hostAndPort = String .format ("%s:%s" , host , port );
345
355
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 );
348
359
}
349
360
350
361
@ Override
@@ -360,21 +371,26 @@ public String getClientName() {
360
371
@ Override
361
372
public List <RedisClientInfo > getClientList () {
362
373
374
+ JedisClusterCommandCallback <String > command = Jedis ::clientList ;
375
+
363
376
Collection <String > map = connection .getClusterCommandExecutor ()
364
- .executeCommandOnAllNodes (( JedisClusterCommandCallback < String >) Jedis :: clientList ).resultsAsList ();
377
+ .executeCommandOnAllNodes (command ).resultsAsList ();
365
378
366
379
ArrayList <RedisClientInfo > result = new ArrayList <>();
380
+
367
381
for (String infos : map ) {
368
382
result .addAll (JedisConverters .toListOfRedisClientInformation (infos ));
369
383
}
384
+
370
385
return result ;
371
386
}
372
387
373
388
@ Override
374
389
public List <RedisClientInfo > getClientList (RedisClusterNode node ) {
375
390
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 ());
378
394
}
379
395
380
396
@ Override
@@ -403,8 +419,10 @@ public void migrate(byte[] key, RedisNode target, int dbIndex, @Nullable Migrate
403
419
404
420
RedisClusterNode node = connection .getTopologyProvider ().getTopology ().lookup (target .getHost (), target .getPort ());
405
421
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 );
408
426
}
409
427
410
428
private Long convertListOfStringToTime (List <String > serverTimeInformation , TimeUnit timeUnit ) {
@@ -416,12 +434,11 @@ private Long convertListOfStringToTime(List<String> serverTimeInformation, TimeU
416
434
return Converters .toTimeMillis (serverTimeInformation .get (0 ), serverTimeInformation .get (1 ), timeUnit );
417
435
}
418
436
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 );
421
439
}
422
440
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 );
425
443
}
426
-
427
444
}
0 commit comments