Commit Graph

1893 Commits

Author SHA1 Message Date
Daniele Alessandri 5a1430e99c Implement abstraction for WAIT.
This abstraction makes it possible to issue a WAIT command using a
client instance connected to a single Redis node or configured with
either a replication or cluster backend:

  $client = new Predis\Client($parameters, $options);
  $ack = new Predis\Replication\WaitContext($client);

  $ack->set('key:1', 'value:1');
  $ack->set('key:2', 'value:2');

  if ($ack->wait(2, 500)) {
    // Writes acknowledged by at least 2 slaves.
  } else {
    // Writes were not acknowledged by at least 2 slaves in 500ms.
  }

When the client is operating in replication mode, WAIT is executed
against the connection currently in use by the underlying backend. On
the other hand when it is operating in cluster mode WAIT is executed
against only one connection as cross-slot operations are not allowed.

This is just a first draft in response to #298 that serves as a base
for further improvements and changes. Tests are still missing.
2016-06-15 18:23:24 +02:00
Daniele Alessandri a70971549e Fix GC issues with closures for phpiredis reader handlers.
The following code, despite not being something you would do in real
world scenarios, eventually ended up generating an exception for "Too
many open files".

  for ($i = 0; $i < 10000; $i++) {
    $client = new Predis\Client();
    $client->connect();
  }

The reason was that the destructor for the connection was not invoked
by PHP as expected when the client instance went out of scope, so the
underlying stream resource were not being properly released.
Even without an actual "connect()" operation, the memory usage of PHP
kept growing until exhausting the configured value of max memory.

The source of the issue is related to the use of closures as handlers
for the phpiredis reader resource, to be more precise it seems to be
some kind of circular reference memory leak. Apparently PHP does not
like the fact that a closure, automatically bound to "$this" starting
with PHP 5.4, is stored in the reader resource which in turn is kept
referenced by "$this". This ends up the refcount not going down thus
the GC does not collect those connection objects going out of scope.
This is confirmed by the fact that this issue is not triggered when
using PHP 5.3 where the ZE does not automatically bind closures to
"$this", unless you capture "$this" with the "use()" directive (and
the usual "$that = $this" trick).

Using a static assignement instead of simply returning new closures
in "getStatusHandler()" and "getErrorHandler()" is kind of an hack
that seems to be working fine, the added value of this solution is
that we do not have to break the internal API of the three connection
backends based on ext-phpiredis.

This commit fixes #345.
2016-06-14 21:27:42 +02:00
Daniele Alessandri e88ffc767d Merge remote-tracking branch 'github/pr/346' 2016-06-14 17:19:32 +02:00
Pascal Hofmann ce8b3fb683 Don't send AUTH and/or SELECT command after connecting to sentinels 2016-06-14 16:38:59 +02:00
Daniele Alessandri e0b4b2c20a [tests] Add test for Predis\Cluster\Hash\CRC16. 2016-06-13 18:58:02 +02:00
Daniele Alessandri 2640062803 Use master for connect() when sentinel reports no slaves.
Fixes #342.
2016-06-13 16:32:45 +02:00
orvice 1811288009 fix typos
possibile -> possible
indentify -> identify
publis ->  publish
2016-06-13 15:22:31 +02:00
Daniele Alessandri fa643ac20c Apply CS fixes. 2016-06-10 11:12:26 +02:00
Daniele Alessandri eb1e686ff9 [tests] Change indentation of mock method expectations. 2016-06-10 11:08:26 +02:00
Daniele Alessandri f02f3b6d37 [tests] Improve code coverage.
There is still some work to improve coverage in other classes though.
2016-06-09 16:41:19 +02:00
Daniele Alessandri d72a1b5550 Do not extend EVALSHA for ScriptCommand class. 2016-06-09 14:02:22 +02:00
Daniele Alessandri 8b466d05df Get string of basic connection parameters from parameters class. 2016-06-08 18:36:35 +02:00
Daniele Alessandri b553c6b9d0 Pass dispatcher loop instance to callback.
This should not break existing code but allows users to retrieve more
easily the current dispatcher loop instance without resorting to some
tricks (like relying on the "use()" directive with closures).
2016-06-08 12:12:31 +02:00
Daniele Alessandri c10479e238 Remove executeCommandOnNodes() from predis cluster backend.
It is still possible to achieve the same simply by iterating over the
connection or, even better, over the client instance.
2016-06-08 12:01:54 +02:00
Daniele Alessandri 228ccaebe3 Use static instead of self. 2016-06-07 21:01:59 +02:00
Daniele Alessandri 82c256422a Fix failure on PHP 5.3.
Also use get_called_class() where possible.
2016-06-07 20:42:08 +02:00
Daniele Alessandri ee7104d2e5 Replace "getClientFor()" with "on()" in Client.
This new method acts exactly like "getClientFor()" by returning a new
client instance for the specified node unless a callback is passed as
the second argument, in this case the callback is invoked and the new
client instance is passed to it. The value returned by the callback
is used as the return value of the "on()" method.
2016-06-07 20:00:11 +02:00
Daniele Alessandri 7c4c4ae58a Change methods of replication connection interface.
Promoted the "switchToMaster()" and "switchToSlave()" methods to be
part of the replication connection interface and demoted the method
"switchTo($connection)".
2016-06-07 18:50:11 +02:00
Daniele Alessandri 185e31e080 Make some fields of Predis\Client private. 2016-06-07 18:22:57 +02:00
Daniele Alessandri 64b3a4805c Apply CS fixes. 2016-06-07 15:23:32 +02:00
Daniele Alessandri 5b4e942112 Add notice about dangers of using master branch. 2016-06-07 15:23:32 +02:00
Daniele Alessandri b2547fc7f4 [tests] Ignore code coverage for Predis\Autoloader. 2016-06-07 15:23:32 +02:00
Daniele Alessandri d34bdd38c3 Reorganize and improve client options.
All option classes have been moved in the Predis\Configuration\Option
namespace and some have been optimized to have less impact on client
initialization timings.

Furthermore the accepted values for some options have been changed,
this is the complete list of accepted values:

- _aggregate_: callable returning an aggregate connection.
- cluster: string value ("predis", "redis"), callable returning an
  aggregate connection.
- replication: string value ("predis", "sentinel"), callable returning
  an aggregate connection.
- commands: command factory, named array mapping command IDs to PHP
  classes, callable returning a command factory or a named array.
- connections: connection factory, callable returning a connection
  factory, named array mapping connection schemes to PHP classes.
- _prefix_: string value, command processor, callable.
- _exceptions_: boolean value.

Note that the cluster and replication options now return a closure
acting as initializer instead of an aggregate connection.
2016-06-07 15:23:32 +02:00
Daniele Alessandri 1028599ac5 Split Predis\Connection\Aggregate namespace.
Now we have two namespaces for cluster and replication backends:

  - Predis\Connection\Cluster
  - Predis\Connection\Replication
2016-06-07 15:23:32 +02:00
Daniele Alessandri 822f02b8eb Implement new logic to load command classes.
By default Predis now uses a convention-over-configuration approach
by looking for a command class in the Predis\Command\Redis namespace
if it is not already defined in the commands class map.

This change allow us to decrease the time needed to load Predis on
each request since we removed 99% of the mappings in the commands
class map. Classes defined in the internal class map still take the
precedence over this mechanism, so users can still define their own
command classes to handle each command.
2016-06-07 15:23:28 +02:00
Daniele Alessandri 4b47639f9e Rename command classes using command ID as name.
Some notable exceptions are EVAL and ECHO because having these names
as class names would raise a syntax error.
2016-06-04 20:54:41 +02:00
Daniele Alessandri e4872af747 Modify ctor signature of Predis\Command\RawCommand. 2016-06-04 20:54:36 +02:00
Daniele Alessandri c1d34f022f Drop useless method from base command class. 2016-06-04 15:57:12 +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 6e3f301588 Move commands classes in Predis\Command\Redis. 2016-06-04 15:36:16 +02:00
Daniele Alessandri 3dccd6bf87 Back to development.
The master branch now hosts the development of Predis v2.0.0-dev.
2016-06-02 09:10:25 +02:00
Daniele Alessandri 0e17edbefb Update CHANGELOG and bump VERSION. v1.1.0 2016-06-02 00:06:21 +02:00
Daniele Alessandri 81c0a8f051 Ensure big ints are not truncated on 32 bits PHP.
We check if the string value is different than the casted int value,
if so it means that the integer is beyond PHP_INT_MAX or PHP_INT_MIN
and we simply return the string value. This is also useful on Windows
builds of PHP since the maximum integer size (prior to PHP 7.0) is 32
bits even for 64 bit builds.
2016-06-01 23:49:23 +02:00
Daniele Alessandri 1065edc8d1 Apply last round of CS fixes. 2016-06-01 22:31:06 +02:00
Daniele Alessandri 843ad23ea7 [tests] Move utility method into base test class. 2016-06-01 22:27:36 +02:00
Daniele Alessandri e386f5c732 Update README.
[ci skip]
2016-06-01 22:20:58 +02:00
Daniele Alessandri a06063d2e6 Update CHANGELOG.
[ci skip]
2016-06-01 21:59:28 +02:00
Daniele Alessandri d58929e5a5 Update README.
Let's try using Gitter...

[ci skip]
2016-06-01 20:59:25 +02:00
Daniele Alessandri ecab7e4642 Implement IteratorAggregate interface for Client.
Now it is possible to iterate over traversable aggregate connections
and get a key/value pair of $connectionId => $clientInstance for each
node.
2016-06-01 12:33:20 +02:00
Daniele Alessandri a22fc17800 Update CHANGELOG.
[ci skip]
2016-05-30 17:45:08 +02:00
Daniele Alessandri 7a50b02c36 [tests] Do not allow failures for HHVM on Travis CI. 2016-05-30 17:09:15 +02:00
Daniele Alessandri f6bf2b5977 [tests] Troubles with HHVM <= 3.6.6 and float timeouts.
HHVM is still being used by Travis CI but this bug makes the build to
take 14 minutes to complete, which is unacceptable.
2016-05-30 17:09:02 +02:00
Daniele Alessandri 5850029f89 Update README.
[ci skip]
2016-05-28 20:28:18 +02:00
Daniele Alessandri f64bd83f9d Update README.
[ci skip]
2016-05-28 20:27:10 +02:00
Daniele Alessandri 922e56b480 Iterate only over connections mapped in slots map.
Iterating over Predis\Connection\Aggregate\RedisCluster returns all
the connections currently mapped in the slots map instead of just the
ones initialized in the pool.

When the slots map is retrieved from Redis (which by default is done
automatically) this allows to iterate over all of the current master
nodes of the cluster. When the underlying use of "CLUSTER SLOTS" is
disabled the iteration returns only connections with a slots range
associated in their parameters or initialized by `-MOVED` responses
to make the behaviour of the iteration consistent between the two
modes of operation.
2016-05-28 17:58:38 +02:00
Daniele Alessandri 5a0dfc3602 Fix parameters overriding for sentinels.
Different fix than PR #339 but thanks @phofmann-trust for spotting.
2016-05-28 15:53:23 +02:00
Daniele Alessandri 39a6e18d71 Update README of test directory.
[ci skip]
2016-05-27 22:03:32 +02:00
Daniele Alessandri a19de6356c Update README.
[ci skip]
2016-05-27 21:31:54 +02:00
Daniele Alessandri 2d01a27e17 Fix fetching slots map from unreachable nodes.
When various nodes in the configuration are unreachable while trying
to send a command, we should attempt to contact a reachable node to
fetch an updated slots map up to $retryLimit times or until there are
no more servers in the pool before giving up.

It is possible that the slots map fetched from Redis contains stale
data and points to a dead server, this happens when the nodes still
have to agree that a master server is down before promoting a slave
to the role of master. In this case no further attempts to execute
the command are performed and an exception is thrown.

This still needs some more testing and will delay v1.0.4 a few days
past its scheduled release.
2016-05-27 14:30:07 +02:00
Daniele Alessandri ad7b8b08cb Run php-cs-fixer. 2016-05-26 09:54:09 +02:00