diff --git a/pymongo/asynchronous/mongo_client.py b/pymongo/asynchronous/mongo_client.py index df390a218e..fcfe94fd2f 100644 --- a/pymongo/asynchronous/mongo_client.py +++ b/pymongo/asynchronous/mongo_client.py @@ -56,7 +56,6 @@ from bson.codec_options import DEFAULT_CODEC_OPTIONS, CodecOptions, TypeRegistry from bson.timestamp import Timestamp from pymongo import _csot, _op_id, common, helpers_shared, periodic_executor -from pymongo._telemetry import _generate_op_id_or_none, log_command_retry from pymongo.asynchronous import client_session, database, uri_parser from pymongo.asynchronous.change_stream import AsyncChangeStream, AsyncClusterChangeStream from pymongo.asynchronous.client_bulk import _AsyncClientBulk @@ -88,6 +87,7 @@ from pymongo.mongo_client_shared import ( _add_retryable_write_error, _after_fork_child, + _AgnosticClientConnectionRetryableBase, _detect_external_db, ) from pymongo.monitoring import ConnectionClosedReason, _EventListeners @@ -2866,76 +2866,15 @@ async def __aenter__(self) -> tuple[AsyncConnection, _ServerMode]: # type: igno return conn, read_pref -class _ClientConnectionRetryable(Generic[T]): +class _ClientConnectionRetryable( + _AgnosticClientConnectionRetryableBase[T, "Server", _WriteCall[T] | _ReadCall[T]] +): """Responsible for executing retryable connections on read or write operations""" - __slots__ = ( - "_address", - "_always_retryable", - "_attempt_number", - "_base_backoff_ms", - "_bulk", - "_client", - "_deprioritized_servers", - "_func", - "_is_aggregate_write", - "_is_read", - "_is_run_command", - "_last_error", - "_max_retries", - "_operation", - "_operation_id", - "_read_pref", - "_retry_policy", - "_retryable", - "_retrying", - "_server", - "_server_selector", - "_session", - ) + __slots__ = () - def __init__( - self, - mongo_client: AsyncMongoClient, # type: ignore[type-arg] - func: _WriteCall[T] | _ReadCall[T], - bulk: Optional[Union[_AsyncBulk, _AsyncClientBulk]], - operation: str, - is_read: bool = False, - session: Optional[AsyncClientSession] = None, - read_pref: Optional[_ServerMode] = None, - address: Optional[_Address] = None, - retryable: bool = False, - operation_id: Optional[int] = None, - is_run_command: bool = False, - is_aggregate_write: bool = False, - ): - self._last_error: Optional[Exception] = None - self._retrying = False - self._always_retryable = False - self._max_retries = float("inf") if _csot.get_timeout() is not None else 1 - self._client = mongo_client - self._retry_policy = mongo_client._retry_policy - self._func = func - self._bulk = bulk - self._session = session - self._is_read = is_read - self._retryable = retryable - self._read_pref = read_pref - self._server_selector: Callable[[Selection], Selection] = ( - read_pref if is_read else writable_server_selector # type: ignore - ) - self._address = address - self._server: Server = None # type: ignore - self._deprioritized_servers: Optional[list[Server]] = None - self._operation = operation - # Only generate an operation id when APM/logging is enabled. - if operation_id is None: - operation_id = _generate_op_id_or_none(self._client._event_listeners) - self._operation_id = operation_id - self._attempt_number = 0 - self._is_run_command = is_run_command - self._is_aggregate_write = is_aggregate_write - self._base_backoff_ms: Optional[float] = None + _client: AsyncMongoClient[Any] + _session: Optional[AsyncClientSession] async def run(self) -> T: """Runs the supplied func() and attempts a retry @@ -3091,38 +3030,6 @@ async def run(self) -> T: raise await asyncio.sleep(delay) - def _is_not_eligible_for_retry(self) -> bool: - """Checks if the exchange is not eligible for retry""" - return not self._retryable or ( - self._is_retrying() and self._attempt_number >= self._max_retries - ) - - def _is_retrying(self) -> bool: - """Checks if the exchange is currently undergoing a retry""" - return self._bulk.retrying if self._bulk else self._retrying - - def _is_session_state_retryable(self) -> bool: - """Checks if provided session is eligible for retry - - reads: Make sure there is no ongoing transaction (if provided a session) - writes: Make sure there is a session without an active transaction - """ - if self._is_read: - return not (self._session and self._session.in_transaction) - return bool(self._session and not self._session.in_transaction) - - def _check_last_error(self, check_csot: bool = False) -> None: - """Checks if the ongoing client exchange experienced a exception previously. - If so, raise last error - - :param check_csot: Checks CSOT to ensure we are retrying with time remaining defaults to False - """ - if self._is_retrying(): - remaining = _csot.remaining() - if not check_csot or (remaining is not None and remaining <= 0): - assert self._last_error is not None - raise self._last_error - async def _get_server(self) -> Server: """Retrieves a server object based on provided object context @@ -3137,15 +3044,6 @@ async def _get_server(self) -> Server: operation_id=self._operation_id, ) - def _log_retry(self, is_write: bool) -> None: - log_command_retry( - self._client._topology_id, - self._operation, - self._operation_id, - self._attempt_number, - is_write, - ) - async def _write(self) -> T: """Wrapper method for write-type retryable client executions diff --git a/pymongo/mongo_client_shared.py b/pymongo/mongo_client_shared.py index 29a3db6e7e..2951af5db1 100644 --- a/pymongo/mongo_client_shared.py +++ b/pymongo/mongo_client_shared.py @@ -17,8 +17,10 @@ from __future__ import annotations from collections.abc import Mapping -from typing import Any, Optional, cast +from typing import TYPE_CHECKING, Any, Callable, Generic, Optional, TypeVar, Union, cast +from pymongo import _csot +from pymongo._telemetry import _generate_op_id_or_none, log_command_retry from pymongo.errors import ( BulkWriteError, ClientBulkWriteException, @@ -33,6 +35,23 @@ _CLIENT_LOGGER, _log_or_warn, ) +from pymongo.server_selectors import writable_server_selector + +if TYPE_CHECKING: + from pymongo.read_preferences import _ServerMode + from pymongo.server_selectors import Selection + from pymongo.typings import ( + _Address, + _AgnosticBulk, + _AgnosticClientBulk, + _AgnosticClientSession, + _AgnosticMongoClient, + _AgnosticServer, + ) + +T = TypeVar("T") +_ServerT = TypeVar("_ServerT", bound="_AgnosticServer") +_FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) def _retryable_error_doc(exc: PyMongoError) -> Optional[Mapping[str, Any]]: @@ -109,3 +128,116 @@ def _detect_external_db(entity: str) -> bool: ) return True return False + + +class _AgnosticClientConnectionRetryableBase(Generic[T, _ServerT, _FuncT]): + """Responsible for executing retryable connections on read or write operations""" + + __slots__ = ( + "_address", + "_always_retryable", + "_attempt_number", + "_base_backoff_ms", + "_bulk", + "_client", + "_deprioritized_servers", + "_func", + "_is_aggregate_write", + "_is_read", + "_is_run_command", + "_last_error", + "_max_retries", + "_operation", + "_operation_id", + "_read_pref", + "_retry_policy", + "_retryable", + "_retrying", + "_server", + "_server_selector", + "_session", + ) + + def __init__( + self, + mongo_client: _AgnosticMongoClient, + func: _FuncT, + bulk: Optional[Union[_AgnosticBulk, _AgnosticClientBulk]], + operation: str, + is_read: bool = False, + session: Optional[_AgnosticClientSession] = None, + read_pref: Optional[_ServerMode] = None, + address: Optional[_Address] = None, + retryable: bool = False, + operation_id: Optional[int] = None, + is_run_command: bool = False, + is_aggregate_write: bool = False, + ): + self._last_error: Optional[Exception] = None + self._retrying = False + self._always_retryable = False + self._max_retries = float("inf") if _csot.get_timeout() is not None else 1 + self._client = mongo_client + self._retry_policy = mongo_client._retry_policy + self._func: _FuncT = func + self._bulk = bulk + self._session = session + self._is_read = is_read + self._retryable = retryable + self._read_pref = read_pref + self._server_selector: Callable[[Selection], Selection] = ( + read_pref if is_read else writable_server_selector # type: ignore + ) + self._address = address + self._server: _ServerT = None # type: ignore + self._deprioritized_servers: Optional[list[_ServerT]] = None + self._operation = operation + # Only generate an operation id when APM/logging is enabled. + if operation_id is None: + operation_id = _generate_op_id_or_none(self._client._event_listeners) + self._operation_id = operation_id + self._attempt_number = 0 + self._is_run_command = is_run_command + self._is_aggregate_write = is_aggregate_write + self._base_backoff_ms: Optional[float] = None + + def _is_not_eligible_for_retry(self) -> bool: + """Checks if the exchange is not eligible for retry""" + return not self._retryable or ( + self._is_retrying() and self._attempt_number >= self._max_retries + ) + + def _is_retrying(self) -> bool: + """Checks if the exchange is currently undergoing a retry""" + return self._bulk.retrying if self._bulk else self._retrying + + def _is_session_state_retryable(self) -> bool: + """Checks if provided session is eligible for retry + + reads: Make sure there is no ongoing transaction (if provided a session) + writes: Make sure there is a session without an active transaction + """ + if self._is_read: + return not (self._session and self._session.in_transaction) + return bool(self._session and not self._session.in_transaction) + + def _check_last_error(self, check_csot: bool = False) -> None: + """Checks if the ongoing client exchange experienced a exception previously. + If so, raise last error + + :param check_csot: Checks CSOT to ensure we are retrying with time remaining defaults to False + """ + if self._is_retrying(): + remaining = _csot.remaining() + if not check_csot or (remaining is not None and remaining <= 0): + assert self._last_error is not None + raise self._last_error + + def _log_retry(self, is_write: bool) -> None: + log_command_retry( + self._client._topology_id, + self._operation, + self._operation_id, + self._attempt_number, + is_write, + ) diff --git a/pymongo/synchronous/mongo_client.py b/pymongo/synchronous/mongo_client.py index 6aaaaed20a..52ca029d26 100644 --- a/pymongo/synchronous/mongo_client.py +++ b/pymongo/synchronous/mongo_client.py @@ -56,7 +56,6 @@ from bson.codec_options import DEFAULT_CODEC_OPTIONS, CodecOptions, TypeRegistry from bson.timestamp import Timestamp from pymongo import _csot, _op_id, common, helpers_shared, periodic_executor -from pymongo._telemetry import _generate_op_id_or_none, log_command_retry from pymongo.client_options import ClientOptions from pymongo.client_session_shared import SessionOptions, TransactionOptions, _EmptyServerSession from pymongo.driver_info import DriverInfo @@ -81,6 +80,7 @@ from pymongo.mongo_client_shared import ( _add_retryable_write_error, _after_fork_child, + _AgnosticClientConnectionRetryableBase, _detect_external_db, ) from pymongo.monitoring import ConnectionClosedReason, _EventListeners @@ -2855,76 +2855,15 @@ def __enter__(self) -> tuple[Connection, _ServerMode]: # type: ignore[override] return conn, read_pref -class _ClientConnectionRetryable(Generic[T]): +class _ClientConnectionRetryable( + _AgnosticClientConnectionRetryableBase[T, "Server", _WriteCall[T] | _ReadCall[T]] +): """Responsible for executing retryable connections on read or write operations""" - __slots__ = ( - "_address", - "_always_retryable", - "_attempt_number", - "_base_backoff_ms", - "_bulk", - "_client", - "_deprioritized_servers", - "_func", - "_is_aggregate_write", - "_is_read", - "_is_run_command", - "_last_error", - "_max_retries", - "_operation", - "_operation_id", - "_read_pref", - "_retry_policy", - "_retryable", - "_retrying", - "_server", - "_server_selector", - "_session", - ) + __slots__ = () - def __init__( - self, - mongo_client: MongoClient, # type: ignore[type-arg] - func: _WriteCall[T] | _ReadCall[T], - bulk: Optional[Union[_Bulk, _ClientBulk]], - operation: str, - is_read: bool = False, - session: Optional[ClientSession] = None, - read_pref: Optional[_ServerMode] = None, - address: Optional[_Address] = None, - retryable: bool = False, - operation_id: Optional[int] = None, - is_run_command: bool = False, - is_aggregate_write: bool = False, - ): - self._last_error: Optional[Exception] = None - self._retrying = False - self._always_retryable = False - self._max_retries = float("inf") if _csot.get_timeout() is not None else 1 - self._client = mongo_client - self._retry_policy = mongo_client._retry_policy - self._func = func - self._bulk = bulk - self._session = session - self._is_read = is_read - self._retryable = retryable - self._read_pref = read_pref - self._server_selector: Callable[[Selection], Selection] = ( - read_pref if is_read else writable_server_selector # type: ignore - ) - self._address = address - self._server: Server = None # type: ignore - self._deprioritized_servers: Optional[list[Server]] = None - self._operation = operation - # Only generate an operation id when APM/logging is enabled. - if operation_id is None: - operation_id = _generate_op_id_or_none(self._client._event_listeners) - self._operation_id = operation_id - self._attempt_number = 0 - self._is_run_command = is_run_command - self._is_aggregate_write = is_aggregate_write - self._base_backoff_ms: Optional[float] = None + _client: MongoClient[Any] + _session: Optional[ClientSession] def run(self) -> T: """Runs the supplied func() and attempts a retry @@ -3080,38 +3019,6 @@ def run(self) -> T: raise time.sleep(delay) - def _is_not_eligible_for_retry(self) -> bool: - """Checks if the exchange is not eligible for retry""" - return not self._retryable or ( - self._is_retrying() and self._attempt_number >= self._max_retries - ) - - def _is_retrying(self) -> bool: - """Checks if the exchange is currently undergoing a retry""" - return self._bulk.retrying if self._bulk else self._retrying - - def _is_session_state_retryable(self) -> bool: - """Checks if provided session is eligible for retry - - reads: Make sure there is no ongoing transaction (if provided a session) - writes: Make sure there is a session without an active transaction - """ - if self._is_read: - return not (self._session and self._session.in_transaction) - return bool(self._session and not self._session.in_transaction) - - def _check_last_error(self, check_csot: bool = False) -> None: - """Checks if the ongoing client exchange experienced a exception previously. - If so, raise last error - - :param check_csot: Checks CSOT to ensure we are retrying with time remaining defaults to False - """ - if self._is_retrying(): - remaining = _csot.remaining() - if not check_csot or (remaining is not None and remaining <= 0): - assert self._last_error is not None - raise self._last_error - def _get_server(self) -> Server: """Retrieves a server object based on provided object context @@ -3126,15 +3033,6 @@ def _get_server(self) -> Server: operation_id=self._operation_id, ) - def _log_retry(self, is_write: bool) -> None: - log_command_retry( - self._client._topology_id, - self._operation, - self._operation_id, - self._attempt_number, - is_write, - ) - def _write(self) -> T: """Wrapper method for write-type retryable client executions