Skip to content

Implement redis blocking command - #42823

Closed
Sasidharan3094 wants to merge 2 commits into
envoyproxy:mainfrom
Sasidharan3094:Implement-redis-blocking-command
Closed

Sasidharan3094 wants to merge 2 commits into
envoyproxy:mainfrom
Sasidharan3094:Implement-redis-blocking-command

Conversation

@Sasidharan3094

Copy link
Copy Markdown

Add Support for Redis Blocking Commands (BLPOP)

Summary

This PR implements support for Redis blocking commands, starting with BLPOP (Blocking List Pop). The implementation uses an is_blocking_client flag pattern to bypass Envoy's operation timeout for blocking commands, allowing them to wait longer than the configured op_timeout without causing connection failures.

Problem

Redis blocking commands like BLPOP can have timeouts that exceed Envoy's configured op_timeout. For example:

  • User executes: BLPOP mylist 6 (6 second timeout)
  • Envoy has op_timeout: 5s configured
  • Result: Connection closes at 5 seconds with "upstream failure" error
  • BLPOP never gets a chance to complete

Solution

Implemented an is_blocking_client flag (similar to the existing is_transaction_client pattern) that:

  1. Is passed through the entire request chain as is_blocking_command parameter
  2. Gets set on the Redis client connection before making the request
  3. Bypasses operation timeout checks in three critical locations
  4. Automatically resets when all requests complete, allowing connection reuse

Architecture

Request Flow

BLPOP command → BlockingRequest::create() 
  → validates timeout parameter
  → passes is_blocking_command=true
  → Connection pool sets client->setBlockingClient(true)
  → ClientImpl skips timeout checks (!is_blocking_client_)
  → Request waits for Redis timeout
  → Auto-resets flag when complete

Key Components

1. Command Registration (supported_commands.h/cc)

  • Added blockingCommands() set containing "blpop"
  • Added isBlockingCommand() helper method
  • Added "blpop" to writeCommands() set

2. Client Interface (client.h, client_impl.h/cc)

  • Added setBlockingClient(bool) method to Client interface
  • Added is_blocking_client_ member variable to ClientImpl
  • Modified three timeout check locations:
    • makeRequest(): Skip timeout when making blocking request
    • onEvent(): Skip timeout when connection establishes
    • onRespValue(): Skip timeout when handling responses + auto-reset flag

3. Connection Pool (conn_pool.h, conn_pool_impl.h/cc)

  • Added is_blocking_command parameter to all request methods
  • Calls client->setBlockingClient(true) before making blocking requests
  • Supports both regular clients and transaction clients

4. Command Splitter (command_splitter_impl.h/cc)

  • Created BlockingRequest class for timeout validation
  • Uses SupportedCommands::isBlockingCommand() for routing
  • Validates timeout parameter with clear error messages
  • Passes is_blocking_command=true through request chain

Design Decisions

  1. Per-Request Flag: Flag is set per-request and auto-resets, allowing same connection to handle both blocking and non-blocking commands

  2. Three-Point Protection: Timeout checks at request initiation, connection establishment, and response handling ensure complete coverage

  3. Extensible Architecture: Uses isBlockingCommand() check instead of hardcoding command names, making it trivial to add BRPOP, BRPOPLPUSH, etc.

  4. Transaction Compatible: Works with both regular clients and transaction clients

  5. Mirror Safety: Mirror requests always use is_blocking_command=false

Testing

Manual Testing

# Configure Envoy with op_timeout: 5s
redis-cli -p 1999

# Test 1: BLPOP with timeout > op_timeout (now works!)
BLPOP mylist 6
# Waits up to 6 seconds instead of failing at 5s

# Test 2: BLPOP with data available
LPUSH mylist value1
BLPOP mylist 6
# Returns: 1) "mylist" 2) "value1"

# Test 3: Invalid timeout validation
BLPOP mylist abc
# Returns: ERR timeout is not a float or out of range

Files Modified

Core Implementation

  • source/extensions/filters/network/common/redis/supported_commands.h - Command registration
  • source/extensions/filters/network/common/redis/supported_commands.cc - Command validation
  • source/extensions/filters/network/common/redis/client.h - Client interface
  • source/extensions/filters/network/common/redis/client_impl.h - Client implementation header
  • source/extensions/filters/network/common/redis/client_impl.cc - Timeout bypass logic

Connection Pool

  • source/extensions/filters/network/redis_proxy/conn_pool.h - Pool interface
  • source/extensions/filters/network/redis_proxy/conn_pool_impl.h - Pool implementation header
  • source/extensions/filters/network/redis_proxy/conn_pool_impl.cc - Flag propagation

Command Handling

  • source/extensions/filters/network/redis_proxy/command_splitter_impl.h - BlockingRequest class
  • source/extensions/filters/network/redis_proxy/command_splitter_impl.cc - Request validation

Documentation

  • BLPOP_IMPLEMENTATION_SUMMARY.md - Comprehensive implementation guide

Benefits

  1. No Configuration Required: Users don't need to increase op_timeout to accommodate blocking commands
  2. Connection Reuse: Same connection handles both blocking and non-blocking commands
  3. Automatic Cleanup: Flag resets automatically, preventing state leakage
  4. Clear Error Messages: Validation provides helpful feedback for invalid timeouts
  5. Extensible: Easy to add other blocking commands (BRPOP, BRPOPLPUSH, BLMOVE, BZPOPMIN, BZPOPMAX, BLMPOP)

Future Enhancements

Adding new blocking commands is straightforward:

  1. Add command to blockingCommands() set in supported_commands.h
  2. Add to writeCommands() if it modifies state
  3. That's it! No other code changes needed.

Commands that can be added using the same pattern:

  • BRPOP - Blocking right pop
  • BRPOPLPUSH - Blocking right pop + left push
  • BLMOVE - Blocking list move (Redis 6.2+)
  • BZPOPMIN/BZPOPMAX - Blocking sorted set pop
  • BLMPOP - Blocking multi-list pop (Redis 7.0+)

Backward Compatibility

  • All changes are backward compatible
  • Default parameter values maintain existing behavior
  • No configuration changes required
  • Existing commands continue to work as before

Risk Assessment

Low Risk

  • Follows existing is_transaction_client pattern
  • Flag automatically resets, preventing state leakage
  • Only affects blocking commands explicitly added to blockingCommands() set
  • Timeout validation prevents invalid requests
  • No changes to existing command handling

@repokitteh-read-only

Copy link
Copy Markdown

Hi @Sasidharan3094, welcome and thank you for your contribution.

We will try to review your Pull Request as quickly as possible.

In the meantime, please take a look at the contribution guidelines if you have not done so already.

🐱

Caused by: #42823 was opened by Sasidharan3094.

see: more, trace.

@dinesh-murugiah dinesh-murugiah 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.

Request to hold this until pub sub client implemtation where we will have a clean abstraction for creating clients

const RouteSharedPtr& route, const std::string& command, const std::string& key,
Common::Redis::RespValueConstSharedPtr incoming_request, ConnPool::PoolCallbacks& callbacks,
Common::Redis::Client::Transaction& transaction) {
Common::Redis::Client::Transaction& transaction, bool is_blocking_command = false) {

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.

passing a bool here doesnt look elegant , we need to have a clean design which is inclusive of accomodating other type of cliuents

@dinesh-murugiah

Copy link
Copy Markdown
Contributor

@Sasidharan-Gopal request to close this PR , better to colloborate in my fork to handle both pubsub and blocking client to avoid repetitive changes

@Sasidharan3094

Copy link
Copy Markdown
Author

Closing this for now.

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.

3 participants