Skip to content

[redis filter] Add cluster-scope command support with flexible response handling framework - #41381

Merged
yanavlasov merged 18 commits into
envoyproxy:mainfrom
dinesh-murugiah:cluster_scope_commands
Oct 17, 2025
Merged

yanavlasov merged 18 commits into
envoyproxy:mainfrom
dinesh-murugiah:cluster_scope_commands

Conversation

@dinesh-murugiah

Copy link
Copy Markdown
Contributor

Commit Message
Add cluster-scope command support with flexible response handling framework
This commit introduces a comprehensive framework for handling Redis cluster-scope commands (CONFIG, SLOWLOG, SCRIPT, FLUSHALL, FLUSHDB) that need to be broadcast to all shards with specialized response aggregation logic.

Additional Description
Problem Statement

Previously, Redis cluster-scope commands were not properly supported. These commands need to:

Be sent to all shards in the cluster
Have responses aggregated according to command-specific logic
Handle partial failures gracefully

Design Approach

  1. Strategy Pattern for Response Handling
    Implemented a flexible response handler framework using the Strategy and Template Method patterns:

BaseClusterScopeResponseHandler: Abstract base class providing common response collection logic via template method pattern
AllshardSameResponseHandler: Validates all shards return identical responses (e.g., CONFIG SET, FLUSHALL)
IntegerSumAggregateResponseHandler: Sums integer responses across shards (e.g., SLOWLOG LEN)
ArrayMergeAggregateResponseHandler: Merges array responses from all shards (e.g., SLOWLOG GET, CONFIG GET)

  1. Factory Pattern for Handler Creation
    ClusterResponseHandlerFactory dynamically creates appropriate handlers based on command and subcommand, enabling easy extensibility for new commands.

  2. Command Routing Architecture

Added ClusterScopeCommands() to SupportedCommands for command classification
Created ClusterScopeCmdRequest class that broadcasts commands to all shards
Implemented subcommand validation via commandSubcommandValidationMap() to restrict unsupported subcommand variants, this helpus restrict certain subcommands from being implemented

  1. Key Design Decisions

Separation of Concerns: Response handling logic is isolated from request routing logic
Extensibility: New cluster-scope commands require only adding handler logic and factory mapping
Error Handling: Failures are converted to error responses and handled uniformly; partial failures return first error encountered
Memory Efficiency: Pre-allocate response vectors based on shard count to minimize allocations

Implementation Details
Response Handler Lifecycle:

ClusterScopeCmdRequest::create() initializes handler via factory
Requests sent to all shards concurrently
Each response/failure triggers handleResponse()/handleFailure()
After all responses received, processAllResponses() performs command-specific aggregation
Final result sent via callbacks_.onResponse()

Risk Level: LOW - Changes are additive and isolated to new cluster-scope command handling. Existing command paths remain unchanged.

Testing
Comprehensive test coverage added in redis_command_splitter_impl_test.cc:

  1. Response Handler Tests (ClusterScopeConfigTest, ClusterScopeSlowLogLenTest, ClusterScopeSlowLogGetTest):

All shards return same response (success case)
Different responses from shards (validation failure)
Single shard error handling
Single shard failure handling
No upstream hosts available
Partial upstream host failures
Request cancellation
Mirrored request support

  1. Framework Integration Tests (ClusterResponseHandlerFactoryTest):

Correct handler creation for all supported commands
Handler type verification (AllshardSame, IntegerSum, ArrayMerge)

  1. Command Routing Tests (ClusterScopeCommandRoutingTest):

Supported commands route correctly
Zero shards handled gracefully
Unsupported commands/subcommands rejected with appropriate errors

  1. Edge Cases:

Integer overflow handling in sum aggregation
Empty array responses
Null/unexpected response types
Negative integer values (rejected)
Single vs. multi-shard response unwrapping

Test Statistics:

25+ new test cases covering cluster-scope functionality
Tests verify correct stats increments (success/error/latency)
Mock-based testing ensures no external dependencies

Docs Changes

current.yaml: Updated with new cluster-scope command support
redis.rst: Documentation for supported cluster-scope commands and their behavior

Release Notes
Added support for Redis cluster-scope commands (CONFIG, SLOWLOG, SCRIPT, FLUSHALL, FLUSHDB) with intelligent response aggregation. Commands are automatically broadcast to all cluster shards with responses merged according to command semantics.
Platform Specific Features
None - implementation is platform-agnostic.

First commit: cluster scope command handling framework

Signed-off-by: dinesh-murugiah <[email protected]>
variables in responsehandler and added interface to handle request from
response handling  class, fixed other typos created by agent

Signed-off-by: dinesh-murugiah <[email protected]>
validating subcommands restrictions

Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
Signed-off-by: dinesh-murugiah <[email protected]>
@yanavlasov yanavlasov self-assigned this Oct 9, 2025
@dinesh-murugiah

Copy link
Copy Markdown
Contributor Author

@yanavlasov @mattklein123 kind request to review

@ravenblackx ravenblackx changed the title Add cluster-scope command support with flexible response handling framework [redis filter] Add cluster-scope command support with flexible response handling framework Oct 16, 2025

@yanavlasov yanavlasov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but the changelog needs clean-up.

/wait

Comment thread changelogs/current.yaml Outdated
Removed runtime guard ``envoy.reloadable_features.original_src_fix_port_exhaustion`` and legacy code paths.

new_features:
- area: router

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like a bad merge.

@dinesh-murugiah dinesh-murugiah Oct 17, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@yanavlasov thanks for your efforts to review , i have fixed the merge problem

Signed-off-by: dinesh-murugiah <[email protected]>
@yanavlasov
yanavlasov enabled auto-merge (squash) October 17, 2025 12:29
@yanavlasov
yanavlasov merged commit cfa5954 into envoyproxy:main Oct 17, 2025
mattklein123 pushed a commit that referenced this pull request Aug 18, 2026
**Commit Message**: redis filter: support `CLUSTER SHARDS`

Adds ``shards`` to the CLUSTER subcommand allowlist so cluster-aware
clients can issue CLUSTER SHARDS (Redis 7.0+, the successor to the
deprecated CLUSTER SLOTS) through the proxy. Like the other supported
CLUSTER introspection subcommands (INFO, SLOTS, KEYSLOT, NODES), the
command is forwarded to a single random upstream shard and the reply is
returned to the client unmodified.

See [here](https://redis.io/docs/latest/commands/cluster-shards/) for
`CLUSTER SHARDS` docs.

**Additional Description**:
This builds on the random-shard command framework introduced in #41381:
subcommand validation is data-driven via
`commandSubcommandValidationMap()`, and routing/response handling reuse
the existing `RandomShardRequest` passthrough (works identically for
RESP2 and RESP3 connections since the reply is relayed verbatim). On
upstreams older than Redis 7.0, the upstream's `ERR unknown subcommand`
error is passed through to the client unchanged.

I guess I can also add a small comment on why this is needed - we have a
case where we want to use Envoy to fetch Redis cluster state before
initializing a client, e.g. ruby `redis-cluster-client`. This library,
however, first calls `CLUSTER SHARDS` and only then falls back to
`CLUSTER NODES`. See
[here](https://github.com/redis-rb/redis-cluster-client/blob/25603bb42d15e3c719a3e505891ce3e425a4d225/lib/redis_client/cluster/node.rb#L357-L365).

Having Envoy support `CLUSTER SHARDS` would help us save some network
roundtrips and potentially avoid thundering herd problem on our network
links during large scale app rollouts.

**Related issues, not fixed by this PR**: #38492 tracks broader
redis_proxy command coverage; #36975 asks for Envoy's own cluster
topology discovery to use CLUSTER SHARDS, which is out of scope here
(discovery still uses CLUSTER SLOTS).

**Per the generative AI policy**: this change was developed with the
assistance of an AI tool (Claude Code). I have reviewed the change,
understand it fully, and take ownership of it.

**Risk Level**: Low — additive one-line allowlist change enabling
passthrough of a single read-only introspection subcommand; no behavior
change for any existing command.

**Testing**: New unit test `RandomShardRequestTest.ClusterShards`,
mirroring the existing `ClusterNodes` test. The full
`//test/extensions/filters/network/redis_proxy:command_splitter_impl_test`
target passes (249/249 tests), including the existing
unsupported-subcommand rejection tests. `ci/do_ci.sh format` passes.

**Docs Changes**: Added a ``CLUSTER SHARDS, Generic`` row to the
supported commands table in
`docs/root/intro/arch_overview/other_protocols/redis.rst`.

**Release Notes**:
`changelogs/current/new_features/redis_proxy__cluster-shards-command.rst`

**Platform Specific Features**: N/A

Signed-off-by: Aurimas Ragulis <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Aug 18, 2026
**Commit Message**: redis filter: support `CLUSTER SHARDS`

Adds ``shards`` to the CLUSTER subcommand allowlist so cluster-aware
clients can issue CLUSTER SHARDS (Redis 7.0+, the successor to the
deprecated CLUSTER SLOTS) through the proxy. Like the other supported
CLUSTER introspection subcommands (INFO, SLOTS, KEYSLOT, NODES), the
command is forwarded to a single random upstream shard and the reply is
returned to the client unmodified.

See [here](https://redis.io/docs/latest/commands/cluster-shards/) for
`CLUSTER SHARDS` docs.

**Additional Description**:
This builds on the random-shard command framework introduced in envoyproxy#41381:
subcommand validation is data-driven via
`commandSubcommandValidationMap()`, and routing/response handling reuse
the existing `RandomShardRequest` passthrough (works identically for
RESP2 and RESP3 connections since the reply is relayed verbatim). On
upstreams older than Redis 7.0, the upstream's `ERR unknown subcommand`
error is passed through to the client unchanged.

I guess I can also add a small comment on why this is needed - we have a
case where we want to use Envoy to fetch Redis cluster state before
initializing a client, e.g. ruby `redis-cluster-client`. This library,
however, first calls `CLUSTER SHARDS` and only then falls back to
`CLUSTER NODES`. See
[here](https://github.com/redis-rb/redis-cluster-client/blob/25603bb42d15e3c719a3e505891ce3e425a4d225/lib/redis_client/cluster/node.rb#L357-L365).

Having Envoy support `CLUSTER SHARDS` would help us save some network
roundtrips and potentially avoid thundering herd problem on our network
links during large scale app rollouts.

**Related issues, not fixed by this PR**: envoyproxy#38492 tracks broader
redis_proxy command coverage; envoyproxy#36975 asks for Envoy's own cluster
topology discovery to use CLUSTER SHARDS, which is out of scope here
(discovery still uses CLUSTER SLOTS).

**Per the generative AI policy**: this change was developed with the
assistance of an AI tool (Claude Code). I have reviewed the change,
understand it fully, and take ownership of it.

**Risk Level**: Low — additive one-line allowlist change enabling
passthrough of a single read-only introspection subcommand; no behavior
change for any existing command.

**Testing**: New unit test `RandomShardRequestTest.ClusterShards`,
mirroring the existing `ClusterNodes` test. The full
`//test/extensions/filters/network/redis_proxy:command_splitter_impl_test`
target passes (249/249 tests), including the existing
unsupported-subcommand rejection tests. `ci/do_ci.sh format` passes.

**Docs Changes**: Added a ``CLUSTER SHARDS, Generic`` row to the
supported commands table in
`docs/root/intro/arch_overview/other_protocols/redis.rst`.

**Release Notes**:
`changelogs/current/new_features/redis_proxy__cluster-shards-command.rst`

**Platform Specific Features**: N/A

Signed-off-by: Aurimas Ragulis <[email protected]>
Wenwei-Zhao pushed a commit to Wenwei-Zhao/envoy that referenced this pull request Aug 18, 2026
**Commit Message**: redis filter: support `CLUSTER SHARDS`

Adds ``shards`` to the CLUSTER subcommand allowlist so cluster-aware
clients can issue CLUSTER SHARDS (Redis 7.0+, the successor to the
deprecated CLUSTER SLOTS) through the proxy. Like the other supported
CLUSTER introspection subcommands (INFO, SLOTS, KEYSLOT, NODES), the
command is forwarded to a single random upstream shard and the reply is
returned to the client unmodified.

See [here](https://redis.io/docs/latest/commands/cluster-shards/) for
`CLUSTER SHARDS` docs.

**Additional Description**:
This builds on the random-shard command framework introduced in envoyproxy#41381:
subcommand validation is data-driven via
`commandSubcommandValidationMap()`, and routing/response handling reuse
the existing `RandomShardRequest` passthrough (works identically for
RESP2 and RESP3 connections since the reply is relayed verbatim). On
upstreams older than Redis 7.0, the upstream's `ERR unknown subcommand`
error is passed through to the client unchanged.

I guess I can also add a small comment on why this is needed - we have a
case where we want to use Envoy to fetch Redis cluster state before
initializing a client, e.g. ruby `redis-cluster-client`. This library,
however, first calls `CLUSTER SHARDS` and only then falls back to
`CLUSTER NODES`. See
[here](https://github.com/redis-rb/redis-cluster-client/blob/25603bb42d15e3c719a3e505891ce3e425a4d225/lib/redis_client/cluster/node.rb#L357-L365).

Having Envoy support `CLUSTER SHARDS` would help us save some network
roundtrips and potentially avoid thundering herd problem on our network
links during large scale app rollouts.

**Related issues, not fixed by this PR**: envoyproxy#38492 tracks broader
redis_proxy command coverage; envoyproxy#36975 asks for Envoy's own cluster
topology discovery to use CLUSTER SHARDS, which is out of scope here
(discovery still uses CLUSTER SLOTS).

**Per the generative AI policy**: this change was developed with the
assistance of an AI tool (Claude Code). I have reviewed the change,
understand it fully, and take ownership of it.

**Risk Level**: Low — additive one-line allowlist change enabling
passthrough of a single read-only introspection subcommand; no behavior
change for any existing command.

**Testing**: New unit test `RandomShardRequestTest.ClusterShards`,
mirroring the existing `ClusterNodes` test. The full
`//test/extensions/filters/network/redis_proxy:command_splitter_impl_test`
target passes (249/249 tests), including the existing
unsupported-subcommand rejection tests. `ci/do_ci.sh format` passes.

**Docs Changes**: Added a ``CLUSTER SHARDS, Generic`` row to the
supported commands table in
`docs/root/intro/arch_overview/other_protocols/redis.rst`.

**Release Notes**:
`changelogs/current/new_features/redis_proxy__cluster-shards-command.rst`

**Platform Specific Features**: N/A

Signed-off-by: Aurimas Ragulis <[email protected]>
Signed-off-by: Wenwei Zhao <[email protected]>
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