Commit Graph

70 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 aa36cc571b Keep a transaction in CAS mode when Redis rejects MULTI (#1766)
* Keep a transaction in CAS mode when Redis rejects MULTI

multi() left CAS mode before sending MULTI. When Redis rejected MULTI,
for example for an ACL user denied it, the transaction looked open
although the connection was still before MULTI, watching its keys.
discard() then sent DISCARD, which failed and closed the connection.
Later commands ran at once on the server, but were expected to reply
QUEUED and failed with a protocol error, and calling multi() again sent
nothing. When the connection was lost on MULTI, unwatch() failed on it
instead of releasing it.

multi() now leaves CAS mode only once MULTI succeeds, so a rejected
MULTI leaves the transaction where Redis left the connection: discard()
and unwatch() send UNWATCH, commands keep running on their own, and
multi() can be called again.

* Add the rejected MULTI 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 1cc16c359c Discard a fluent transaction when Redis rejects a queued command (#1755)
When Redis rejected a command while queueing it (OOM, READONLY, NOPERM, MISCONF, wrong arity), MultiExec threw AbortedMultiExecException without sending DISCARD.

A transaction block discards on the way out, the fluent interface didn't, so the connection stayed in MULTI and every later command on the client returned QUEUED without running.

The fluent interface now discards the transaction, or closes the connection if DISCARD fails too.

Until discard() is called, the aborted MultiExec refuses new commands and execute(), so a caller that catches the exception can't commit only the commands queued after the failed one.

Cluster connections skip the abort path: their errors before execute()
come from the client and nothing is queued on a node yet. watch() refuses
to run on an aborted transaction and unwatch() sends nothing, so only
discard() makes it usable again. Every discard on the way out closes the
connection when DISCARD fails.
2026-11-12 11:01:48 -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 78c91a5628 Support numbered databases in Valkey clusters (#1742) 2026-10-09 08:11:13 -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
vladvildanov 8bc9a4d432 Merge branch 'v2.x' of github.com:predis/predis into vv-main-2.x-sync 2025-03-31 11:36:25 +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
Till Krüss ba66e22c92 bump year to 2025 2025-01-22 09:24:26 -08:00
Till Krüss 7631d317fd update year 2024-11-21 11:37:10 -08:00
vladvildanov f239e989fe Resolve merge conflicts 2023-06-13 14:05:29 +03:00
Vladyslav Vildanov 9b7963be8a Add RESP3 response handle (#1298)
* Add RESP3 response handle

* Fix replication connection to return parameters of any connection role

* Removed expectations on unstable error messages

* Codestyle fixes
2023-05-18 11:04:32 +03:00
Till Krüss 946c4b7bd7 [v2.x] Relay support (#1263) 2023-05-09 10:39:00 -07:00
Vladyslav Vildanov 971e47672d Added Redis stack support (JSON, Search, TimeSeries, Bloom) (#1253) 2023-04-11 08:09:14 -07:00
Fran Moreno d6dc6e51e1 Add CS job (#1024)
* Add CS job

* Apply PHP71Migration ruleset
2023-01-18 10:02:26 -08:00
Till Krüss 82791e55e1 format tests 2023-01-17 15:08:18 -08:00
Till Krüss af2e5cc553 order imports 2023-01-17 15:08:18 -08:00
Till Krüss 06c00ac43f import global namespace 2023-01-17 15:08:18 -08:00
Till Krüss cc40219268 use short array syntax 2023-01-17 15:08:18 -08:00
Till Krüss c9babb1984 update copyright comments 2023-01-17 13:41:35 -08:00
Peter 20f8faee6f Improve PHPUnit assertions 2021-04-04 23:30:14 +08:00
Daniele Alessandri c6f2ce4b83 [tests] Replace generic MockObject::will() with specialized methods. 2020-09-01 14:03:19 +02:00
Daniele Alessandri 36c9bb047e [tests] Start improving test suite.
- Make use of more typehints for function parameters
- Make use of typehints for function return values
- Use @var where needed to give proper hints to IDEs and avoid warnings
- Replace MockObject::setMethods() with addMethods() and onlyMethods()
- Rewording of some phpdocs
2020-09-01 13:58:14 +02:00
Daniele Alessandri cc2a7657db Rework Predis\Command\FactoryInterface and related classes.
We have renamed most methods to drop the "command" suffix as it is quite
redundant. Due to this change and thanks to variadic methods introduced
with PHP 5.6 we took the opportunity to replace both "supportsCommand()"
and "supportsCommands()" with a single new method "supports()".

Added more stringent typehints for method arguments and typehints for
return values now that we do not need to support anything below PHP 7.2.
Also moved from using array() to [] in source code of class involved.
2020-08-28 14:23:12 +02:00
Daniele Alessandri c26909699b Merge remote-tracking branch 'github/pr/639' into main 2020-08-18 16:03:10 +02:00
Raza Mehdi 5edf443141 Fix tests. 2020-08-16 01:59:41 +05:00
Daniele Alessandri eb1e686ff9 [tests] Change indentation of mock method expectations. 2016-06-10 11:08:26 +02:00
Daniele Alessandri 62b421f20e Switch from server profiles to commands factory.
This change reduces some unnecessary complexity in the library, Redis
commands do not change much after all. Developers can still implement
their own commands factory, inject new commands or override existing
ones. The "profile" client options has been renamed to "commands" and
it accepts instances of Predis\Command\FactoryInterface.

The test suite checks at runtime the version of the running instance
of Redis for integration tests to adapt itself automatically.
2016-06-04 15:36:21 +02:00
Daniele Alessandri 8dd9893a2f Run php-cs-fixer with new configuration. 2015-07-24 23:17:02 +02:00
Daniele Alessandri 6590c44a27 Run php-cs-fixer against codebase in src/ and tests/. 2015-07-24 18:04:42 +02:00
Daniele Alessandri ab20c52115 Do not parse response to EXISTS into boolean value.
Starting with Redis 3.0.3 the EXISTS command is variadic so that it is
possible to check for the existence of multiple keys in one request,
with the server returning the number of keys found.

This change could break codebases relying on strict comparison (===)
against a boolean value, but just doing $redis->exists('key') == TRUE
is totally fine.
2015-07-24 15:49:40 +02:00
Daniele Alessandri 45e351be79 [phpdoc] Fix formatting of phpdoc headers. 2014-07-27 23:59:37 +02:00
Daniele Alessandri dbfc1a74ba More random fixes after inspection. 2014-07-27 23:06:17 +02:00
Daniele Alessandri 4e1186f845 [phpdoc] Fix undefined classes. 2014-07-27 21:57:50 +02:00
Daniele Alessandri aa5c893d5a [phpdoc] Fix undefined namespaces. 2014-07-27 21:57:40 +02:00
Daniele Alessandri fff303a03b Comply with PSR-2 on intentional fall-through in non-empty case. 2014-07-27 21:57:40 +02:00
Daniele Alessandri 2e0c55ac57 Remove support for Redis 1.2.
Actually we have just removed the server profile so nothing stops you
from reimplementing it, but Redis 2.0 has been released 4 years ago so
we are speaking of ancient releases that should probably not even be
used anymore.
2014-07-16 11:14:36 +02:00
Daniele Alessandri 959734cf8c Porperly handle -ERR instead of +QUEUED inside MULTI ... EXEC.
Error responses such as -OOM or -ERR on invalid arguments in commands
are returned immediatly instead of +QUEUED when using a transaction,
which is a condition that had not been tested enough. This condition
led to a bug in which Predis was not invalidating the transaction, so
when trying to create a new transaction Redis returned a "-ERR MULTI
calls can not be nested".

This commit fixes #187.
2014-07-14 16:28:29 +02:00
Daniele Alessandri abd284c972 Complete reorganization of the Predis\Connection namespace.
* Renamed SingleConnectionInterface to NodeConnectionInterface since
  this name is better and makes even more sense in the context of
  cluster and replication scenarios.

* Moved specialized aggregate connections (the ones implementing both
  predis and redis cluster and master/slave replication) in a newly
  created Predis\Connection\Aggregate sub-namespace.

* Removed the "Connection" part from names of aggregate connection
  interfaces in the Predis\Connection\Aggregate sub-namespace.

* Changed "Composable" to "Composite" in the name of interfaces and
  classes that can use pluggable protocol processors.
2014-06-03 15:19:32 +02:00
Daniele Alessandri 6a577a0eb5 Apply more phpdoc fixes. 2013-12-22 15:00:51 +01:00
Daniele Alessandri 131a5a4ef9 Fix a few exceptions. 2013-12-21 11:08:29 +01:00