Repository navigation
FEAT: retry transient failures in Cursor.execute() with RetryPolicy (GH-682) - #908
om singhal (Om-singhaI) wants to merge 1 commit into
Conversation
…icrosoftGH-682) Adds retry_policy= on Connection.cursor() and a per call override on Cursor.execute(). A cursor never inherits the policy given to connect(), and without a policy nothing changes. The loop wraps only the native execute and its error check, runs each retry on a fresh handle with a fresh copy of the parameters, and retries only while autocommit is on, checked before and after the delay. Class 08, HYT01 and 40003 are never retried on the same connection; the new RetryPolicy.is_retriable_on_same_connection() says which codes are.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
PR Performance ReportPerformance could not be assessed. No matching performance run appeared within the 220-minute wait for |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Code Coverage Report
Files needing attentionmssql_python.pybind.performance_counter.hpp: 0.7% mssql_python.pybind.logger_bridge.cpp: 57.9% mssql_python.pybind.ddbc_bindings.h: 64.5% mssql_python.pybind.logger_bridge.hpp: 70.8% mssql_python._native_fetch.py: 77.7% mssql_python.pybind.ddbc_bindings.cpp: 81.4% mssql_python.pybind.row_mapping_cache.hpp: 81.5% mssql_python.pybind.connection.connection_pool.cpp: 82.5% mssql_python.pybind.connection.connection.cpp: 83.1% mssql_python.logging.py: 86.8% |
| check_error(ddbc_sql_const.SQL_HANDLE_STMT.value, self.hstmt, ret) | ||
| except Exception as e: # pylint: disable=broad-exception-caught | ||
| logger.warning("Execute failed, resetting cursor: %s", e) | ||
| self._reset_cursor() |
There was a problem hiding this comment.
Missing give-up warning: calls _reset_cursor() before final retry logging. If handle recreation fails after attempt 2, execution exits without the promised attempt-count/reason warning. Log that failure while preserving exception propagation.
| assert "autocommit is off" in warnings[1] and "after the delay" in warnings[1] | ||
|
|
||
|
|
||
| def test_reset_cursor_failure_after_a_failed_attempt_propagates_unwrapped( |
There was a problem hiding this comment.
test_043_cursor_retry_policy.py:640 covers reset failure only on attempt 1. Add a case with two failures and fail_alloc_at=3, asserting the final warning. Also commit live timeout regression coverage—the current fake-only tests cannot verify native diagnostics and re-preparation.
|
om singhal (@Om-singhaI) Please update your PR and resolve the comments that you have received |
Work Item / Issue Reference
Summary
Second half of #682, after #751. Adds statement retry:
retry_policy=onConnection.cursor()and on a singleCursor.execute()call, with the sameRetryPolicy, backoff and full jitter that #751 merged. Without a policy nothing changes.How it works
check_error. Parameter conversion and everything before it run once.DDBCSQLCheckErrorread beforecheck_errorraises. ODBC keeps the diagnostic records until the next call on the handle other than a diagnostic read, andexecutemanyalready reads a failed handle twice. If that read fails, the statement is not retried.helpers.pyandexceptions.pyare untouched.Connection.autocommitis True at the moment it failed. With autocommit off the driver can't see the transaction state, so the error is raised at once and the app retries its own transaction. That read runs last, and an error from it, or any value other than True, counts as a no.connect()defaults to autocommit off, so statement retry only takes effect once the app turns autocommit on.BEGIN TRANSACTIONorSET IMPLICIT_TRANSACTIONS ON) is invisible to the driver too, so after a deadlock only the failed statement runs again.Cursor.executesays so.RetryPolicy.is_retriable_on_same_connection()narrows the policy for this scope. Class08,HYT01and40003are never retried on the same connection, so of the defaults onlyHYT00and40001are. A custom set gets the same filter.connect(): a warning per retry, and a warning when it gives up after a retry, also when the native call raises. Each failed attempt still logs what a failed execute logs today (thecheck_errorerror lines and theExecute failed, resetting cursorwarning), sincehelpers.pyis untouched. Refusing a listed code on the first attempt, for example with autocommit off, is a debug line. None of the retry lines include SQL text or parameter values.Scope
Cursor.execute(). The row by row fallback ofexecutemany()andbatch_execute(reuse_cursor=...)go through it, so they follow the cursor's policy.Connection.execute()(new cursor, no policy), the batchedexecutemany()path, bulk copy and the async cursor.connect(). The issue sketched inheritance; I kept it explicit so a connect policy can't start replaying statements by itself. If you'd rather inherit, it's a two line fallback inCursor.__init__.Testing
tests/test_043_cursor_retry_policy.py: 44 passed, no server. The native execute and diagnostic calls are faked, andretry._sleepand_randomare patched so delays are asserted exactly. It covers one attempt and no extra read without a policy, delays[1.0, 2.0]for two failures, the original values on a fresh handle for every attempt, a reused plan prepared again, autocommit off or reading back as anything but True, a failed autocommit read and the checks after the delay,08S01,40003,HYT01and unlisted codes not retried, the give up log lines,TypeErrorfor a badretry_policy, and no SQL or values in the log lines.tests/test_028_stub_signature_parity.pynow also pinsConnection.cursor,Cursor.__init__andCursor.execute: 7 passed.tests/test_027_retry_policy.py: 74 passed.tests/test_typing.py -m typing, mypy 2.3.1) is clean, and so areblackandflake8with the CI flags.