@@ -466,26 +466,49 @@ def _parse_node_line(line):
466
466
line_items = line .split (" " )
467
467
node_id , addr , flags , master_id , ping , pong , epoch , connected = line .split (" " )[:8 ]
468
468
addr = addr .split ("@" )[0 ]
469
- slots = [sl .split ("-" ) for sl in line_items [8 :]]
470
469
node_dict = {
471
470
"node_id" : node_id ,
472
471
"flags" : flags ,
473
472
"master_id" : master_id ,
474
473
"last_ping_sent" : ping ,
475
474
"last_pong_rcvd" : pong ,
476
475
"epoch" : epoch ,
477
- "slots" : slots ,
476
+ "slots" : [],
477
+ "migrations" : [],
478
478
"connected" : True if connected == "connected" else False ,
479
479
}
480
+ if len (line_items ) >= 9 :
481
+ slots , migrations = _parse_slots (line_items [8 :])
482
+ node_dict ["slots" ], node_dict ["migrations" ] = slots , migrations
480
483
return addr , node_dict
481
484
482
485
486
+ def _parse_slots (slot_ranges ):
487
+ slots , migrations = [], []
488
+ for s_range in slot_ranges :
489
+ if "->-" in s_range :
490
+ slot_id , dst_node_id = s_range [1 :- 1 ].split ("->-" , 1 )
491
+ migrations .append (
492
+ {"slot" : slot_id , "node_id" : dst_node_id , "state" : "migrating" }
493
+ )
494
+ elif "-<-" in s_range :
495
+ slot_id , src_node_id = s_range [1 :- 1 ].split ("-<-" , 1 )
496
+ migrations .append (
497
+ {"slot" : slot_id , "node_id" : src_node_id , "state" : "importing" }
498
+ )
499
+ else :
500
+ s_range = [sl for sl in s_range .split ("-" )]
501
+ slots .append (s_range )
502
+
503
+ return slots , migrations
504
+
505
+
483
506
def parse_cluster_nodes (response , ** options ):
484
507
"""
485
- @see: https://redis.io/commands/cluster-nodes # string
486
- @see: https://redis.io/commands/cluster-replicas # list of string
508
+ @see: https://redis.io/commands/cluster-nodes # string / bytes
509
+ @see: https://redis.io/commands/cluster-replicas # list of string / bytes
487
510
"""
488
- if isinstance (response , str ):
511
+ if isinstance (response , ( str , bytes ) ):
489
512
response = response .splitlines ()
490
513
return dict (_parse_node_line (str_if_bytes (node )) for node in response )
491
514
0 commit comments