Skip to content

Revert "PYTHON-4018 Clarify exactly what code/label fields drivers should inspect to determine retryability" #1495

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

Merged
merged 1 commit into from
Jan 31, 2024
Merged
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
12 changes: 3 additions & 9 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
PyMongoError,
ServerSelectionTimeoutError,
WaitQueueTimeoutError,
WriteConcernError,
)
from pymongo.lock import _HAS_REGISTER_AT_FORK, _create_lock, _release_locks
from pymongo.monitoring import ConnectionClosedReason
Expand Down Expand Up @@ -2142,7 +2141,7 @@ def _retryable_error_doc(exc: PyMongoError) -> Optional[Mapping[str, Any]]:
return None


def _add_retryable_write_error(exc: PyMongoError, max_wire_version: int, is_mongos: bool) -> None:
def _add_retryable_write_error(exc: PyMongoError, max_wire_version: int) -> None:
doc = _retryable_error_doc(exc)
if doc:
code = doc.get("code", 0)
Expand All @@ -2159,10 +2158,7 @@ def _add_retryable_write_error(exc: PyMongoError, max_wire_version: int, is_mong
for label in doc.get("errorLabels", []):
exc._add_error_label(label)
else:
# Do not consult writeConcernError for pre-4.4 mongos.
if isinstance(exc, WriteConcernError) and is_mongos:
pass
elif code in helpers._RETRYABLE_ERROR_CODES:
if code in helpers._RETRYABLE_ERROR_CODES:
exc._add_error_label("RetryableWriteError")

# Connection errors are always retryable except NotPrimaryError and WaitQueueTimeoutError which is
Expand Down Expand Up @@ -2396,14 +2392,12 @@ def _write(self) -> T:
"""
try:
max_wire_version = 0
is_mongos = False
self._server = self._get_server()
supports_session = (
self._session is not None and self._server.description.retryable_writes_supported
)
with self._client._checkout(self._server, self._session) as conn:
max_wire_version = conn.max_wire_version
is_mongos = conn.is_mongos
if self._retryable and not supports_session:
# A retry is not possible because this server does
# not support sessions raise the last error.
Expand All @@ -2414,7 +2408,7 @@ def _write(self) -> T:
if not self._retryable:
raise
# Add the RetryableWriteError label, if applicable.
_add_retryable_write_error(exc, max_wire_version, is_mongos)
_add_retryable_write_error(exc, max_wire_version)
raise

def _read(self) -> T:
Expand Down
322 changes: 11 additions & 311 deletions test/retryable_writes/unified/insertOne-serverErrors.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
"schemaVersion": "1.0",
"runOnRequirements": [
{
"minServerVersion": "4.0",
"minServerVersion": "3.6",
"topologies": [
"replicaset"
]
},
{
"minServerVersion": "4.1.7",
"topologies": [
"sharded"
]
}
],
"createEntities": [
Expand Down Expand Up @@ -61,7 +55,16 @@
"description": "InsertOne succeeds after retryable writeConcernError",
"runOnRequirements": [
{
"minServerVersion": "4.3.1"
"minServerVersion": "4.0",
"topologies": [
"replicaset"
]
},
{
"minServerVersion": "4.1.7",
"topologies": [
"sharded-replicaset"
]
}
],
"operations": [
Expand Down Expand Up @@ -165,309 +168,6 @@
]
}
]
},
{
"description": "RetryableWriteError label is added based on top-level code in pre-4.4 server response",
"runOnRequirements": [
{
"minServerVersion": "4.2",
"maxServerVersion": "4.2.99",
"topologies": [
"replicaset",
"sharded"
]
}
],
"operations": [
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "client0",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 2
},
"data": {
"failCommands": [
"insert"
],
"errorCode": 189
}
}
}
},
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 3,
"x": 33
}
},
"expectError": {
"errorLabelsContain": [
"RetryableWriteError"
]
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"insert": "coll",
"documents": [
{
"_id": 3,
"x": 33
}
]
},
"commandName": "insert",
"databaseName": "retryable-writes-tests"
}
},
{
"commandStartedEvent": {
"command": {
"insert": "coll",
"documents": [
{
"_id": 3,
"x": 33
}
]
},
"commandName": "insert",
"databaseName": "retryable-writes-tests"
}
}
]
}
],
"outcome": [
{
"collectionName": "coll",
"databaseName": "retryable-writes-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
}
]
}
]
},
{
"description": "RetryableWriteError label is added based on writeConcernError in pre-4.4 mongod response",
"runOnRequirements": [
{
"minServerVersion": "4.2",
"maxServerVersion": "4.2.99",
"topologies": [
"replicaset"
]
}
],
"operations": [
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "client0",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 2
},
"data": {
"failCommands": [
"insert"
],
"writeConcernError": {
"code": 91,
"errmsg": "Replication is being shut down"
}
}
}
}
},
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 3,
"x": 33
}
},
"expectError": {
"errorLabelsContain": [
"RetryableWriteError"
]
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"insert": "coll",
"documents": [
{
"_id": 3,
"x": 33
}
]
},
"commandName": "insert",
"databaseName": "retryable-writes-tests"
}
},
{
"commandStartedEvent": {
"command": {
"insert": "coll",
"documents": [
{
"_id": 3,
"x": 33
}
]
},
"commandName": "insert",
"databaseName": "retryable-writes-tests"
}
}
]
}
],
"outcome": [
{
"collectionName": "coll",
"databaseName": "retryable-writes-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
},
{
"description": "RetryableWriteError label is not added based on writeConcernError in pre-4.4 mongos response",
"runOnRequirements": [
{
"minServerVersion": "4.2",
"maxServerVersion": "4.2.99",
"topologies": [
"sharded"
]
}
],
"operations": [
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "client0",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 1
},
"data": {
"failCommands": [
"insert"
],
"writeConcernError": {
"code": 91,
"errmsg": "Replication is being shut down"
}
}
}
}
},
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 3,
"x": 33
}
},
"expectError": {
"errorLabelsOmit": [
"RetryableWriteError"
]
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"insert": "coll",
"documents": [
{
"_id": 3,
"x": 33
}
]
},
"commandName": "insert",
"databaseName": "retryable-writes-tests"
}
}
]
}
],
"outcome": [
{
"collectionName": "coll",
"databaseName": "retryable-writes-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
}
]
}