Skip to content

Send a transaction to the primary when it has primary-only work and the caller prefers a replica - #3381

Open
ShmueLior wants to merge 1 commit into
seredis:mainfrom
ShmueLior:fix/lock-preferreplica-primary
Open

ShmueLior wants to merge 1 commit into
seredis:mainfrom
ShmueLior:fix/lock-preferreplica-primary

Conversation

@ShmueLior

Copy link
Copy Markdown

Follow up to #3291.

On servers before 8.4 there's no SET IFEQ / DELEX, so LockExtend and LockRelease fall back to a WATCH/MULTI/EXEC transaction, executed with the caller's flags. EXEC isn't primary-only, so with PreferReplica the transaction went to a replica. The queued EXPIRE / DEL was then rejected with Command cannot be issued to a replica: EXPIRE ..., and since that throws inside the write, the replica connection was torn down too. #3291 fixed the IFEQ / DELEX path but not this one.

TransactionMessage now does what Message.SetPrimaryOnly does for a single command: if any queued operation is primary-only, PreferMaster / PreferReplica become DemandMaster. A transaction with only reads still follows the caller's flags, and DemandReplica is left alone so it fails the same way it does today. This also fixes user transactions that contain a write and are executed with PreferReplica.

Why not just pass DemandMaster from LockExtend / LockRelease: that only fixes the two helpers, and it's about the same size.

Tests (TransactionTests, against the primary/replica pair, RESP2 and RESP3):

  • PrimaryOnlyWorkWithPreferReplicaRunsOnThePrimary: condition + EXPIRE, and condition + SET, executed with PreferReplica (sync and async), checks EXEC ran on the primary. Fails on main on any server version.
  • ReadOnlyWorkWithPreferReplicaStillRunsOnTheReplica: condition + GET with PreferReplica still runs EXEC on the replica.

KeyTests.PrimaryOnlyCommandsWithPreferReplicaRunOnThePrimary now passes on Redis 8.0.2 too (it failed on main there). Ran the full suite on net8.0 and net10.0 against 8.0.2 (primary/replica + cluster), no new failures compared to main. Also checked LockExtend / LockRelease with PreferReplica on a local 8.0.2 cluster. Didn't run net481.

Checklist

  • I fully and freely contribute this code in accordance with the project license (and am legally able to do so)
  • I take responsibility for this contribution's quality and correctness, including any portions produced with AI assistance (see CONTRIBUTING.md).

…he caller prefers a replica

LockExtend and LockRelease fall back to a WATCH/MULTI/EXEC transaction on servers without SET IFEQ / DELEX (before 8.4). The transaction was routed with the caller's flags, so PreferReplica sent it to a replica, which then rejected the queued EXPIRE/DEL and dropped the connection. The transaction now applies the same rule as Message.SetPrimaryOnly: if any queued operation is primary-only, PreferMaster/PreferReplica become DemandMaster. DemandReplica is left alone.

This branch has not been deployed

No deployments
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.

1 participant