Skip to content

cachedb_redis: add ASK redirect handling and fix hash slot calculation#3852

Open
dondetir wants to merge 1 commit intoOpenSIPS:masterfrom
dondetir:feature/redis-cluster-ask-topology
Open

cachedb_redis: add ASK redirect handling and fix hash slot calculation#3852
dondetir wants to merge 1 commit intoOpenSIPS:masterfrom
dondetir:feature/redis-cluster-ask-topology

Conversation

@dondetir
Copy link
Copy Markdown

Summary

Add support for Redis ASK redirects during cluster resharding and fix the hash slot calculation.

ASK Redirect Handling

When a Redis cluster is resharding (migrating slots between nodes), the source node returns an ASK response instead of MOVED. The key difference:

  • MOVED = permanent redirect (slot ownership changed) — already handled by PR Add Redis MOVED Redirection support #3639
  • ASK = temporary redirect (slot is mid-migration) — requires sending ASKING to the target node before retrying

Without ASK handling, queries during resharding operations fail with unhandled error responses. This completes the cluster redirect support started in #3639.

Implementation

  • Detects ASK responses alongside existing MOVED handling in the query retry loop
  • Sends ASKING command to the target node before retrying the original query
  • Refactors parse_moved_reply() into parse_redirect_reply() that accepts the prefix as a parameter, with inline wrappers parse_moved_reply() and parse_ask_reply() for backward compatibility
  • Cleans up inconsistent indentation in the redirect handling block

Hash Slot Fix

Replaces crc16(...) & con->slots_assigned with crc16(...) % REDIS_CLUSTER_SLOTS (16384). The bitwise AND only produces correct modulo results when the operand is 2^n - 1. Using the spec-defined constant (RFC: Redis Cluster Specification) is always correct regardless of the slots_assigned value.

Testing

  • Builds clean with -Wall -Wextra -Werror, zero warnings
  • All 2545 unit tests pass

Partially addresses #2811

Add support for Redis ASK redirects during cluster resharding.  When
a slot is being migrated between nodes, Redis returns an ASK response
instead of MOVED.  Unlike MOVED (permanent redirect), ASK is a
one-time redirect that requires sending the ASKING command to the
target node before retrying the original query.

The implementation:
 - Detects ASK responses alongside existing MOVED handling
 - Sends ASKING command to the target node before retrying
 - Reuses the MOVED redirect infrastructure (endpoint lookup,
   reconnection, retry logic)

Also refactor parse_moved_reply() into parse_redirect_reply() that
accepts the prefix as a parameter, with inline wrappers
parse_moved_reply() and parse_ask_reply() for backward compatibility.

Fix the hash slot calculation in redisHash(): replace the bitwise AND
operator with modulo using the standard Redis cluster slot count
(16384).  The previous code used '& con->slots_assigned' which only
produces correct results when slots_assigned is a power-of-2 minus 1.
Using the spec-defined constant is always correct.

Partially addresses OpenSIPS#2811
@NormB
Copy link
Copy Markdown
Member

NormB commented Mar 29, 2026

This appears to be a partial duplicate of #3815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants