Skip to content

Fix SlotNotCoveredError when cluster is resharding #2989

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
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
6 changes: 5 additions & 1 deletion redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,11 @@ def get_node_from_slot(
node_idx = self.read_load_balancer.get_server_index(
primary_name, len(self.slots_cache[slot])
)
return self.slots_cache[slot][node_idx]

# we use the node returned by RR in the load balancer
# if it's part of the slots cache, otherwise we use primary
node = node_idx if node_idx < len(self.slots_cache[slot]) else 0
Copy link

@johan-seesaw johan-seesaw Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only case I can see this happening, is the LoadBalancer object having a pre-existing history with a list_size of lets say 3 nodes for the given primary. If an event happens where the list is no longer 3 (lets say new list is size 1), and we enter LoadBalancer with the existing dictionary having a value of 2 for this primary, then we will return 2 from the get_server_index method, but it will perform a % 1 operation before storing the "next value" in the dictionary.

Perhaps a simpler rewrite would be to store the last-used value, not the next-used value in the LoadBalancer class, then there would only be one modulo operation, and we would always be performing it with the current list size.

    def get_server_index(self, primary: str, list_size: int) -> int:
        # default to -1 if not found, so after incrementing it will be 0
        server_index = (self.primary_to_idx.get(primary, -1) + 1) % list_size
        self.primary_to_idx[primary] = server_index
        return server_index

return self.slots_cache[slot][node]
return self.slots_cache[slot][0]
except (IndexError, TypeError):
raise SlotNotCoveredError(
Expand Down