Skip to content

(fix): Separated retries for read and write operations #3559

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 18 commits into from
Closed
Show file tree
Hide file tree
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
23 changes: 10 additions & 13 deletions redis/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,13 +624,6 @@ async def close(self, close_connection_pool: Optional[bool] = None) -> None:
"""
await self.aclose(close_connection_pool)

async def _send_command_parse_response(self, conn, command_name, *args, **options):
"""
Send a command and parse the response
"""
await conn.send_command(*args)
return await self.parse_response(conn, command_name, **options)

async def _disconnect_raise(self, conn: Connection, error: Exception):
"""
Close the connection and raise an exception
Expand All @@ -655,10 +648,12 @@ async def execute_command(self, *args, **options):
if self.single_connection_client:
await self._single_conn_lock.acquire()
try:
await conn.retry.call_with_retry(
lambda: conn.send_command(*args),
lambda error: self._disconnect_raise(conn, error),
)
return await conn.retry.call_with_retry(
lambda: self._send_command_parse_response(
conn, command_name, *args, **options
),
lambda: self.parse_response(conn, command_name, **options),
lambda error: self._disconnect_raise(conn, error),
)
finally:
Expand Down Expand Up @@ -1383,10 +1378,12 @@ async def immediate_execute_command(self, *args, **options):
conn = await self.connection_pool.get_connection()
self.connection = conn

await conn.retry.call_with_retry(
lambda: conn.send_command(*args),
lambda error: self._disconnect_reset_raise(conn, error),
)
return await conn.retry.call_with_retry(
lambda: self._send_command_parse_response(
conn, command_name, *args, **options
),
lambda: self.parse_response(conn, command_name, **options),
lambda error: self._disconnect_reset_raise(conn, error),
)

Expand Down
23 changes: 10 additions & 13 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,6 @@ def close(self) -> None:
if self.auto_close_connection_pool:
self.connection_pool.disconnect()

def _send_command_parse_response(self, conn, command_name, *args, **options):
"""
Send a command and parse the response
"""
conn.send_command(*args, **options)
return self.parse_response(conn, command_name, **options)

def _disconnect_raise(self, conn, error):
"""
Close the connection and raise an exception
Expand All @@ -623,10 +616,12 @@ def _execute_command(self, *args, **options):
if self._single_connection_client:
self.single_connection_lock.acquire()
try:
conn.retry.call_with_retry(
lambda: conn.send_command(*args, **options),
lambda error: self._disconnect_raise(conn, error),
)
return conn.retry.call_with_retry(
lambda: self._send_command_parse_response(
conn, command_name, *args, **options
),
lambda: self.parse_response(conn, command_name, **options),
lambda error: self._disconnect_raise(conn, error),
)
finally:
Expand Down Expand Up @@ -1408,10 +1403,12 @@ def immediate_execute_command(self, *args, **options):
conn = self.connection_pool.get_connection()
self.connection = conn

conn.retry.call_with_retry(
lambda: conn.send_command(*args, **options),
lambda error: self._disconnect_reset_raise(conn, error),
)
return conn.retry.call_with_retry(
lambda: self._send_command_parse_response(
conn, command_name, *args, **options
),
lambda: self.parse_response(conn, command_name, **options),
lambda error: self._disconnect_reset_raise(conn, error),
)

Expand Down
14 changes: 13 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
CacheKey,
EvictionPolicy,
)
from redis.connection import Connection, ConnectionInterface, SSLConnection, parse_url
from redis.connection import (
Connection,
ConnectionInterface,
SSLConnection,
parse_url,
ConnectionPool,
)
from redis.credentials import CredentialProvider
from redis.exceptions import RedisClusterException
from redis.retry import Retry
Expand Down Expand Up @@ -582,6 +588,12 @@ def mock_connection() -> ConnectionInterface:
return mock_connection


@pytest.fixture()
def mock_pool() -> ConnectionPool:
mock_pool = Mock(spec=ConnectionPool)
return mock_pool


@pytest.fixture()
def cache_key(request) -> CacheKey:
command = request.param.get("command")
Expand Down
15 changes: 14 additions & 1 deletion tests/test_asyncio/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import random
from contextlib import asynccontextmanager as _asynccontextmanager
from typing import Union
from unittest.mock import Mock

import pytest
import pytest_asyncio
import redis.asyncio as redis
from packaging.version import Version
from redis.asyncio import Sentinel
from redis.asyncio.client import Monitor
from redis.asyncio.connection import Connection, parse_url
from redis.asyncio.connection import Connection, parse_url, ConnectionPool
from redis.asyncio.retry import Retry
from redis.backoff import NoBackoff
from redis.credentials import CredentialProvider
Expand Down Expand Up @@ -219,6 +220,18 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):
yield mocked


@pytest_asyncio.fixture()
def mock_connection() -> Connection:
mock_connection = Mock(spec=Connection)
return mock_connection


@pytest_asyncio.fixture()
def mock_pool() -> ConnectionPool:
mock_pool = Mock(spec=ConnectionPool)
return mock_pool


@pytest_asyncio.fixture()
async def credential_provider(request) -> CredentialProvider:
return get_credential_provider(request)
Expand Down
59 changes: 56 additions & 3 deletions tests/test_asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import socket
import types
from errno import ECONNREFUSED
from unittest.mock import patch
from unittest.mock import patch, AsyncMock

import pytest
import redis
Expand All @@ -20,7 +20,7 @@
parse_url,
)
from redis.asyncio.retry import Retry
from redis.backoff import NoBackoff
from redis.backoff import NoBackoff, ExponentialBackoff
from redis.exceptions import ConnectionError, InvalidResponse, TimeoutError
from redis.utils import HIREDIS_AVAILABLE
from tests.conftest import skip_if_server_version_lt
Expand Down Expand Up @@ -91,7 +91,7 @@ async def get_conn():
await asyncio.gather(r.set("a", "b"), r.set("c", "d"))

assert init_call_count == 1
assert command_call_count == 2
assert command_call_count == 4
r.connection = None # it was a Mock
await r.aclose()

Expand Down Expand Up @@ -315,6 +315,59 @@ async def get_redis_connection():
await r1.aclose()


@pytest.mark.onlynoncluster
async def test_client_do_not_retry_write_on_read_failure(mock_connection, mock_pool):
mock_connection.send_command.return_value = True
mock_connection.read_response.side_effect = [
ConnectionError,
ConnectionError,
b"OK",
]
mock_connection.retry = Retry(ExponentialBackoff(), 3)
mock_connection.retry_on_error = (ConnectionError,)
mock_pool.get_connection = AsyncMock(return_value=mock_connection)
mock_pool.connection_kwargs = {}

r = Redis(
connection_pool=mock_pool,
retry=Retry(ExponentialBackoff(), 3),
single_connection_client=True,
)
await r.set("key", "value")

# If read from socket fails, writes won't be executed.
mock_connection.send_command.assert_called_once_with("SET", "key", "value")
mock_connection.read_response.call_count = 3


@pytest.mark.onlynoncluster
async def test_pipeline_immediate_do_not_retry_write_on_read_failure(
mock_connection, mock_pool
):
mock_connection.send_command.return_value = True
mock_connection.read_response.side_effect = [
ConnectionError,
ConnectionError,
b"OK",
]
mock_connection.retry = Retry(ExponentialBackoff(), 3)
mock_connection.retry_on_error = (ConnectionError,)
mock_pool.get_connection = AsyncMock(return_value=mock_connection)
mock_pool.connection_kwargs = {}

r = Redis(
connection_pool=mock_pool,
retry=Retry(ExponentialBackoff(), 3),
single_connection_client=True,
)
pipe = r.pipeline(transaction=False)
await pipe.immediate_execute_command("SET", "key", "value")

# If read from socket fails, writes won't be executed.
mock_connection.send_command.assert_called_once_with("SET", "key", "value")
mock_connection.read_response.call_count = 3


async def test_close_is_aclose(request):
"""Verify close() calls aclose()"""
calls = 0
Expand Down
48 changes: 47 additions & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from unittest.mock import call, patch

import pytest

import redis
from redis import ConnectionPool, Redis
from redis._parsers import _HiredisParser, _RESP2Parser, _RESP3Parser
from redis.backoff import NoBackoff
from redis.backoff import NoBackoff, ExponentialBackoff
from redis.cache import (
CacheConfig,
CacheEntry,
Expand Down Expand Up @@ -249,6 +250,51 @@ def get_redis_connection():
r1.close()


@pytest.mark.onlynoncluster
def test_client_do_not_retry_write_on_read_failure(mock_connection, mock_pool):
mock_connection.send_command.return_value = True
mock_connection.read_response.side_effect = [
ConnectionError,
ConnectionError,
b"OK",
]
mock_connection.retry = Retry(ExponentialBackoff(), 3)
mock_connection.retry_on_error = (ConnectionError,)
mock_pool.get_connection.return_value = mock_connection
mock_pool.connection_kwargs = {}

r = Redis(connection_pool=mock_pool, retry=Retry(ExponentialBackoff(), 3))
r.set("key", "value")

# If read from socket fails, writes won't be executed.
mock_connection.send_command.assert_called_once_with("SET", "key", "value")
mock_connection.read_response.call_count = 3


@pytest.mark.onlynoncluster
def test_pipeline_immediate_do_not_retry_write_on_read_failure(
mock_connection, mock_pool
):
mock_connection.send_command.return_value = True
mock_connection.read_response.side_effect = [
ConnectionError,
ConnectionError,
b"OK",
]
mock_connection.retry = Retry(ExponentialBackoff(), 3)
mock_connection.retry_on_error = (ConnectionError,)
mock_pool.get_connection.return_value = mock_connection
mock_pool.connection_kwargs = {}

r = Redis(connection_pool=mock_pool, retry=Retry(ExponentialBackoff(), 3))
pipe = r.pipeline(transaction=False)
pipe.immediate_execute_command("SET", "key", "value")

# If read from socket fails, writes won't be executed.
mock_connection.send_command.assert_called_once_with("SET", "key", "value")
mock_connection.read_response.call_count = 3


@pytest.mark.skipif(sys.version_info == (3, 9), reason="Flacky test on Python 3.9")
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
def test_redis_connection_pool(request, from_url):
Expand Down
Loading