diff options
author | Bar Shaul <88437685+barshaul@users.noreply.github.com> | 2022-04-04 13:33:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-04 13:33:09 +0300 |
commit | 0676892ae3ec18a0e2c292b22f08ec4b86e0bbe4 (patch) | |
tree | 3165b8cae4157bd10522cc930f41b0bafbb16a01 /redis/client.py | |
parent | e6968f8d73f85c2ab50a145aeccfa289719fdddf (diff) | |
download | redis-py-0676892ae3ec18a0e2c292b22f08ec4b86e0bbe4.tar.gz |
Extended "CLUSTER NODES" parser to support special slot entries (importing, migrating). (#2080)
See https://redis.io/commands/cluster-nodes/#special-slot-entries
Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
Diffstat (limited to 'redis/client.py')
-rwxr-xr-x | redis/client.py | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/redis/client.py b/redis/client.py index 8c17665..d8d7a75 100755 --- a/redis/client.py +++ b/redis/client.py @@ -466,7 +466,6 @@ def _parse_node_line(line): line_items = line.split(" ") node_id, addr, flags, master_id, ping, pong, epoch, connected = line.split(" ")[:8] addr = addr.split("@")[0] - slots = [sl.split("-") for sl in line_items[8:]] node_dict = { "node_id": node_id, "flags": flags, @@ -474,18 +473,42 @@ def _parse_node_line(line): "last_ping_sent": ping, "last_pong_rcvd": pong, "epoch": epoch, - "slots": slots, + "slots": [], + "migrations": [], "connected": True if connected == "connected" else False, } + if len(line_items) >= 9: + slots, migrations = _parse_slots(line_items[8:]) + node_dict["slots"], node_dict["migrations"] = slots, migrations return addr, node_dict +def _parse_slots(slot_ranges): + slots, migrations = [], [] + for s_range in slot_ranges: + if "->-" in s_range: + slot_id, dst_node_id = s_range[1:-1].split("->-", 1) + migrations.append( + {"slot": slot_id, "node_id": dst_node_id, "state": "migrating"} + ) + elif "-<-" in s_range: + slot_id, src_node_id = s_range[1:-1].split("-<-", 1) + migrations.append( + {"slot": slot_id, "node_id": src_node_id, "state": "importing"} + ) + else: + s_range = [sl for sl in s_range.split("-")] + slots.append(s_range) + + return slots, migrations + + def parse_cluster_nodes(response, **options): """ - @see: https://redis.io/commands/cluster-nodes # string - @see: https://redis.io/commands/cluster-replicas # list of string + @see: https://redis.io/commands/cluster-nodes # string / bytes + @see: https://redis.io/commands/cluster-replicas # list of string / bytes """ - if isinstance(response, str): + if isinstance(response, (str, bytes)): response = response.splitlines() return dict(_parse_node_line(str_if_bytes(node)) for node in response) |