13 Commits

Author SHA1 Message Date
Till Krüss 8e34223fb9 Do not follow -ASK on the first command of a cluster transaction (#1767)
* Do not follow -ASK on the first command of a cluster transaction

A cluster transaction is held on the node the slots map associates
with its slot, but its first command went through the cluster like any
other one. While a slot was being migrated, an -ASK reply to WATCH (or
to the first command in CAS mode) was followed to the node importing
the slot, and only then was the connection pinned from the slots map,
which -ASK does not update. WATCH was left on the importing node while
MULTI, the queued commands and EXEC ran on the source node with nothing
watched, so the transaction committed even though the watched key had
changed. When the transaction failed after that WATCH, the cleanup was
sent to the source node and the WATCH on the importing node was never
released, so its dirty flag aborted a later, unrelated transaction sent
over the pooled connection.

The first command is now sent through
RedisCluster::executeCommandOnSlotOwner(), which retries and follows
-MOVED and -READONLY like executeCommand() but returns -ASK replies.
The transaction then fails with a ServerException carrying the -ASK
reply, as later CAS commands already did, without touching the
importing node. Retrying once the migration has finished reaches the
new owner. An -ASK reply to a queued command still aborts the
transaction, as before.

The README no longer claims that the first command is redirected like
any other command, and describes how an -ASK reply fails a transaction.

* Add the cluster ASK fix to the changelog
2026-11-18 01:23:59 -07:00
Till Krüss eb8805e25e Keep the WATCH of a cluster transaction after a server error (#1764)
* Unwatch the keys when a server error escapes a transaction block

A transaction block that threw a server error, such as WRONGTYPE from a
command run before multi() in CAS mode, left its WATCH on the connection
because MultiExec ignored the exception. The next transaction on that
connection then failed with a spurious abort once a watched key had
changed. A server error now discards the transaction like any other
exception leaving the block, which sends UNWATCH before MULTI and
DISCARD after it.

* Keep the WATCH of a cluster transaction after a server error

A cluster transaction sent UNWATCH and released its node when a command
run before multi() in CAS mode got an error reply, such as WRONGTYPE
from GET or NOPERM from a second WATCH. A block that caught the error
and went on to multi() then ran MULTI and EXEC on a connection that
watched nothing, so a change to a watched key was silently overwritten.
Redis keeps the WATCH after an error outside MULTI, and so does a
non-cluster transaction, which fails EXEC on a conflicting change.

The node that already holds the transaction now keeps its WATCH and the
error is thrown. A -MOVED reply only updates the slots map: the queued
commands are rejected by that node, so the transaction cannot commit on
the new one without the WATCH. An error on the first command still
releases the node, as nothing is watched yet.

* Add the cluster WATCH fix to the changelog

* Give the CAS MOVED test the slot its WATCH key has

Cluster transactions now retry a MOVED reply only for the transaction's
own slot. The test's mocked strategy returned no slot, which a WATCH
key always has, so its queued MOVED was reported instead of retried.
2026-11-18 01:23:59 -07:00
Till Krüss a65a761f11 Report cluster transaction server errors instead of retrying them (#1763)
* Report cluster transaction server errors instead of retrying them

Every error reply to MULTI, to a queued command or to EXEC went through
abort() and made executeTransaction() return null, which
MultiExec::execute() reads as a WATCH conflict. A command Redis
rejected (CROSSSLOT, OOM, wrong arity, BUSY) therefore surfaced as "The
current transaction has been aborted by the server." without the
server's message, and with the retry option the block ran again against
an error that would repeat.

Only the errors a retry can get past still return null: MOVED for the
transaction's own slot, whose abort updates the slots map, TRYAGAIN,
CLUSTERDOWN, READONLY, and the "Transaction failed" error Relay uses to
report a WATCH conflict. Everything else is reported like on a single
node: a rejected queued command is returned to MultiExec, which throws
AbortedMultiExecException with the server's message, and an error from
MULTI or EXEC throws ServerException. Cleanup and slots map updates stay
as they were, and the transaction is reset, so it can be used again.

ASK is reported too. A transaction does not send ASKING, so every retry
reaches the same node and gets the same ASK until the migration ends.

LOADING, BUSY and MASTERDOWN are not retried either, as MultiExec
retries without waiting and single node transactions don't retry them.

* Add the cluster transaction error fix to the changelog

* Avoid a word the spell check rejects in the README

* Drop the Relay case from cluster transaction errors

RelayConnection now returns null when a watched key aborts EXEC, like
the stream connection, so a cluster transaction no longer sees Relay's
"Transaction failed" error and does not need to treat it as retryable.
2026-11-18 01:23:59 -07:00
Till Krüss dc61d2d81e Report a Relay WATCH abort as an aborted transaction (#1765)
* Return null when a watched key aborts a Relay EXEC

When a watched key aborts EXEC, Redis replies with a null array but
Relay throws "Transaction failed", and RelayConnection returns that as
an error response. The strategy mapped it to null in a catch for a
ServerException, which RelayConnection never throws, so a transaction
aborted by WATCH failed with a ServerException instead of
AbortedMultiExecException and the retry option was ignored.

RelayConnection now returns null for that failure of EXEC, as the
stream connection does for the null array, so every strategy handles it
the same way. The dead catch in NonClusterConnectionStrategy is removed,
and so is the exclusion that kept a Relay connection open when EXEC
fails with an error other than EXECABORT, since a WATCH abort no longer
reaches it. A Relay connection whose EXEC was rejected while MULTI
stays open is now closed like any other.

Client::exec() on a Relay connection returns null after a WATCH abort,
as it does on other connections.

* Add the Relay WATCH abort fix to the changelog
2026-11-18 01:23:58 -07:00
Till Krüss 7bfe1357e8 Fail cluster transactions after a lost WATCH or a rejected command (#1759)
* Fail cluster transactions after a lost WATCH or a rejected command

A cluster transaction kept in use after losing its connection between
WATCH and MULTI committed on a new connection, without the WATCH. A
block that caught a cross-slot error committed the other commands.

Like with a single node, commands now fail until discard() or unwatch()
once the connection is lost, and EXEC fails with EXECABORT after a
rejected command.

* Add the cluster transaction fixes to the changelog

* Release a cluster transaction even when its cleanup fails

A connection lost while discard() or unwatch() sent UNWATCH marked the
transaction as lost again, after MultiExec had already reset it, so it
stayed unusable.

* Only fail a cluster transaction for commands rejected after MULTI

A cross-slot command before multi() in CAS mode runs on its own and is
never queued, but it still made EXEC fail with EXECABORT, so a block
that caught the error and went on to multi() could not commit, while
Redis and non-cluster transactions commit it.

* Report a lost cluster connection before a rejected command on EXEC

MultiExec now resets once EXEC fails with a server error. A cluster
transaction that lost its connection and also had a command rejected
failed EXEC with EXECABORT, so MultiExec reset and discard() could no
longer release the lost connection, failing every later command. EXEC
now reports the lost connection first, so the transaction keeps its
state until discard(), as it does on a single node.
2026-11-18 01:23:58 -07:00
Till Krüss 897b238c7e Fix transactions after lost connections, failed DISCARD and protocol errors (#1758)
* Fail a transaction whose connection was lost until it is discarded

A connection error released the connection holding the transaction, so a
caller that caught the error and kept using the same MultiExec sent the
next commands on a new connection, outside of the transaction. In CAS
mode, MULTI and EXEC then ran on a connection that watched nothing and
overwrote a change made after WATCH. The lost connection now stays held,
and every command fails until discard() or unwatch(). discard() sends
nothing to it, since closing the connection already ended the
transaction on the server.

discard() now always ends the transaction: the strategy closes the
connection when DISCARD fails, so that it cannot stay in MULTI, and
MultiExec resets its state even when the strategy throws. Before, a
failed DISCARD after a queue error reset MultiExec while the strategy
still held the lost connection, and every later transaction failed.
discard() also unwatches the keys when MULTI was never sent, for example
when the connection was lost between WATCH and MULTI.

Cluster transactions built with the fluent interface are aborted after a
cross-slot error too, instead of committing the remaining commands.

The StreamInitException handling is removed: a held connection that is
not open is never reconnected, so it cannot be thrown there. Also drops
the isAborted() check in unwatch(), the inline comments added in #1756,
and merges the changelog into a single Unreleased section.

* Merge overlapping transaction tests

The tests for a queue error with the fluent interface (discarded, not
reusable, unwatch() and watch() while aborted) become one test, and the
one for a failed DISCARD is covered by the recovery test, which now also
covers a connection lost between WATCH and MULTI. The strategy tests for
commands not being retried after WATCH or MULTI fold into the tests for
a lost connection, and the connected tests for a connection lost after
WATCH share one helper.

The persistence data provider is renamed, since it is not only used by
cluster tests.

* Report protocol errors in transactions on the node holding them

With replication, Sentinel or cluster, MultiExec passed the aggregate
connection to ProtocolException, which requires a node connection, so an
unexpected reply inside MULTI or an EXEC reply with the wrong number of
items threw a TypeError. The exception is now built with the node the
transaction's commands are routed to, which is also the connection that
gets closed.

* Remove unreachable branches from the transaction retry callback

A CommunicationException always carries a node connection, so the
callback only ever disconnected it: the branches removing the node from
a replication or handling timeouts never ran.

* Align the comment of MultiExecState::ABORTED with the other flags

* Only realign the comment of MultiExecState::ABORTED

* Shorten the transaction changelog entries

* Cover the remaining branches of the changed transaction methods

Adds tests for discard() in CAS mode before MULTI, a non-callable
argument to execute(), RESP3 replies to EXEC, and server errors from a
node or Relay connection in executeBypassingTransaction().

* Retry transaction commands after a failed connect

The retry callback only accepted a CommunicationException, so retrying
after a StreamInitException failed with a TypeError.

* Reset a transaction once Redis replies to EXEC

execute() kept the transaction state after EXEC, so a transaction that
Redis had already ended still looked open. discard() then sent DISCARD
outside of MULTI, and since a failed DISCARD closes the connection, it
dropped a healthy connection. Reusing the transaction sent the next
command without MULTI, outside of any transaction.

The state is now reset once EXEC replies, whether it succeeded, reported
a failed command, was aborted by a watched key or was rejected. It is
kept when EXEC fails on a lost connection, so that discard() still
releases that connection.

* Close the connection when Redis rejects EXEC but keeps MULTI open

Since MultiExec resets once EXEC replies, discard() no longer sends
DISCARD after a rejected EXEC. Redis 6.0.6 and later discard the
transaction when they reject EXEC and reply with EXECABORT, but older
versions reject it with BUSY, LOADING or MASTERDOWN and keep the
connection in MULTI, so later commands on it were queued.

The strategy now closes the connection when EXEC fails with any error
other than EXECABORT. Relay is excluded, since it reports a transaction
aborted by WATCH as an error.

* Skip UNWATCH on a transaction connection that was lost

unwatch() reconnected to send UNWATCH after the connection holding the
WATCH was lost, although closing that connection already dropped the
watched keys on the server. With the server still down, discard() of a
transaction in CAS mode, or of one that lost its connection between
WATCH and MULTI, then failed although there was nothing left to clean
up. unwatch() now releases a lost connection without sending anything,
as discard() already does.

* Keep a rejected command before MULTI from aborting a transaction

In CAS mode commands run on their own until multi(), so an error there
does not affect the transaction, as in Redis. A fluent cluster
transaction still treated a cross-slot error before multi() like a
rejected queued command, discarding the transaction and failing every
later command until discard(). Only errors after MULTI abort it now.

* Cover the Relay, replication and server-down transaction paths

No test failed when the Relay exception or the choice of connection in
the rejected EXEC handling was removed, or when discard() of a CAS
transaction failed while the server stayed down. New tests check that a
transaction aborted by WATCH under Relay keeps its connection, that a
rejected EXEC with replication closes only the node holding the
transaction, and that discard() succeeds while the server is down after
the connection was lost. The docblock now says that the connection is
closed on any EXEC error but EXECABORT, as the code does.

* Send nothing from unwatch() on an aborted transaction

Aborting the transaction already discarded it and released the
connection holding it, so the UNWATCH that unwatch() sent was a wasted
round trip. Restores the isAborted() check from #1755.
2026-11-18 01:23:57 -07:00
Andrei Fedoseev 45b6bcc6d9 Keep a transaction on the connection that received WATCH or MULTI (#1756)
Retrying one of its commands on a new connection runs it outside of the
transaction: with Sentinel even without the retry option, since
SentinelReplication retries on its own. A transaction block then threw
with one command already written, and a CAS transaction overwrote a
change made after WATCH, because MULTI and EXEC ran on a connection
that watched nothing.

MultiExec::unwatch() now goes through the strategy before MULTI, a held connection that is no longer open fails instead of reconnecting outside the transaction, a StreamInitException releases it too, and replication holds getCurrent().

In CAS mode the first command initializes the transaction without sending MULTI, so MultiExec::unwatch() now checks for CAS too.
2026-11-12 09:40:49 -07:00
Till Krüss 5a9a7665cb Fix cluster transaction cleanup and CAS behavior (#1735) 2026-10-07 10:02:58 -07:00
Till Krüss 87390e7381 Use static closures (#1652) 2026-03-09 13:30:46 -07:00
Till Krüss 581c5659bd update year in headers 2026-02-23 11:48:06 -08:00
Vladyslav Vildanov 980326d5d8 Added retry support (#1616)
* Initial work on retries

* Added retry class and test coverage

* Added support for standalone and cluster

* Make TimeoutException instance of CommunicationException

* Added pipeline, trasnaction, replication support

* Fixed broken test

* Marked test as relay-incompatible

* Marked test as relay-incompatible

* Fixed analysis errors, added missing tests

* Codestyle fixes

* Fixed test

* Update README.md

* Update README.md

* Update README.md

* Updated README.md

* Refactor retry on read and write

* Added check for timeout value

* Updated README.md

* Fixed README.md

* Codestyle changes

* Added missing coverage

* Added missing test coverage

* Removed comments

* Added retry support for Relay connection (#1620)

* Added integration test case with mocked retry

* Changed client initialisation in tests

* Marked test as relay-incompatible

---------

Co-authored-by: Pavlo Yatsukhnenko <yatsukhnenko@users.noreply.github.com>
2025-12-23 10:40:46 +02:00
vladvildanov bf526cc129 Codestyle fixes 2025-03-31 12:14:37 +03:00
Vladyslav Vildanov 73e6a03fd8 Added support for transactions in OSS Cluster (#1497)
* Added support for transactions in OSS Cluster

* Updated test cases

* Updated version restriction

* Updated server version restriction

* Added test skip for Relay

* Added test case for Relay

* Set transaction slot to null in case of failed transaction

* Restrict usage of transaction commands outside of transaction context

* Added handling for Relay responses

* Reverted changes

* Removed unsupported command

* Added dummy arguments

* Codestyle fixes

* Added additional test coverage

* Added CHANGELOG and README entries

* Added missing PR reference

* Missing word

* Fixed README
2025-03-07 10:31:02 +02:00