Skip to content

Open slots also need to be parsed #2072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,41 @@ def parse_cluster_info(response, **options):
return dict(line.split(":") for line in response.splitlines() if line)


def _parse_open_slots(open_slots):
"""
Change the expression of the open slot to dictionary.
ref: https://redis.io/commands/cluster-nodes/#special-slot-entries
"""

migrating = {}
importing = {}

for slot_exp in open_slots:
# examples
# migrating: [77->-e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca]
# importing: [93-<-292f8b365bb7edb5e285caf0b7e6ddc7265d2f4f]
slot, direction, node_id = slot_exp[1:-1].split("-")

# migrating
if direction == ">":
migrating[slot] = node_id
# importing
elif direction == "<":
importing[slot] = node_id

return migrating, importing


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:]]
slots = [sl.split("-") for sl in line_items[8:] if not sl.startswith("[")]

open_slots = [sl for sl in line_items[8:] if sl.startswith("[")]
migrating, importing = _parse_open_slots(open_slots)

node_dict = {
"node_id": node_id,
"flags": flags,
Expand All @@ -475,6 +505,8 @@ def _parse_node_line(line):
"last_pong_rcvd": pong,
"epoch": epoch,
"slots": slots,
"migrating": migrating,
"importing": importing,
"connected": True if connected == "connected" else False,
}
return addr, node_dict
Expand All @@ -485,9 +517,8 @@ def parse_cluster_nodes(response, **options):
@see: https://redis.io/commands/cluster-nodes # string
@see: https://redis.io/commands/cluster-replicas # list of string
"""
if isinstance(response, str):
response = response.splitlines()
return dict(_parse_node_line(str_if_bytes(node)) for node in response)
response = str_if_bytes(response)
return dict(_parse_node_line(node) for node in response.splitlines())


def parse_geosearch_generic(response, **options):
Expand Down