Files
predis/src/Cluster/ClusterStrategy.php
2026-11-06 11:09:20 +02:00

755 lines
25 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2026 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Cluster;
use InvalidArgumentException;
use Predis\Command\CommandInterface;
use Predis\Command\ScriptCommand;
/**
* Common class implementing the logic needed to support clustering strategies.
*/
abstract class ClusterStrategy implements StrategyInterface
{
protected $commands;
public function __construct()
{
$this->commands = $this->getDefaultCommands();
}
/**
* Returns the default map of supported commands with their handlers.
*
* @return array
*/
protected function getDefaultCommands()
{
$getKeyFromFirstArgument = [$this, 'getKeyFromFirstArgument'];
$getKeyFromAllArguments = [$this, 'getKeyFromAllArguments'];
return [
/* commands operating on the key space */
'EXISTS' => $getKeyFromAllArguments,
'DEL' => $getKeyFromAllArguments,
'UNLINK' => $getKeyFromAllArguments,
'TYPE' => $getKeyFromFirstArgument,
'EXPIRE' => $getKeyFromFirstArgument,
'EXPIREAT' => $getKeyFromFirstArgument,
'PERSIST' => $getKeyFromFirstArgument,
'PEXPIRE' => $getKeyFromFirstArgument,
'PEXPIREAT' => $getKeyFromFirstArgument,
'TTL' => $getKeyFromFirstArgument,
'PTTL' => $getKeyFromFirstArgument,
'SORT' => [$this, 'getKeyFromSortCommand'],
'DUMP' => $getKeyFromFirstArgument,
'RESTORE' => $getKeyFromFirstArgument,
'OBJECT' => [$this, 'getKeyFromObjectCommand'],
'FLUSHDB' => [$this, 'getFakeKey'],
/* commands operating on string values */
'APPEND' => $getKeyFromFirstArgument,
'DECR' => $getKeyFromFirstArgument,
'DECRBY' => $getKeyFromFirstArgument,
'GET' => $getKeyFromFirstArgument,
'GETBIT' => $getKeyFromFirstArgument,
'MGET' => $getKeyFromAllArguments,
'SET' => $getKeyFromFirstArgument,
'GETRANGE' => $getKeyFromFirstArgument,
'GETSET' => $getKeyFromFirstArgument,
'INCR' => $getKeyFromFirstArgument,
'INCRBY' => $getKeyFromFirstArgument,
'INCRBYFLOAT' => $getKeyFromFirstArgument,
'INCREX' => $getKeyFromFirstArgument,
'SETBIT' => $getKeyFromFirstArgument,
'SETEX' => $getKeyFromFirstArgument,
'MSET' => [$this, 'getKeyFromInterleavedArguments'],
'MSETNX' => [$this, 'getKeyFromInterleavedArguments'],
'SETNX' => $getKeyFromFirstArgument,
'SETRANGE' => $getKeyFromFirstArgument,
'STRLEN' => $getKeyFromFirstArgument,
'SUBSTR' => $getKeyFromFirstArgument,
'BITOP' => [$this, 'getKeyFromBitOp'],
'BITCOUNT' => $getKeyFromFirstArgument,
'BITFIELD' => $getKeyFromFirstArgument,
'BITRANGEINTARRAY' => $getKeyFromFirstArgument,
/* commands operating on lists */
'LINSERT' => $getKeyFromFirstArgument,
'LINDEX' => $getKeyFromFirstArgument,
'LLEN' => $getKeyFromFirstArgument,
'LMOVEM' => [$this, 'getKeyFromFirstTwoKeys'],
'LPOP' => $getKeyFromFirstArgument,
'RPOP' => $getKeyFromFirstArgument,
'RPOPLPUSH' => $getKeyFromAllArguments,
'BLMOVEM' => [$this, 'getKeyFromFirstTwoKeys'],
'BLPOP' => [$this, 'getKeyFromBlockingListCommands'],
'BRPOP' => [$this, 'getKeyFromBlockingListCommands'],
'BRPOPLPUSH' => [$this, 'getKeyFromBlockingListCommands'],
'LPUSH' => $getKeyFromFirstArgument,
'LPUSHX' => $getKeyFromFirstArgument,
'RPUSH' => $getKeyFromFirstArgument,
'RPUSHX' => $getKeyFromFirstArgument,
'LRANGE' => $getKeyFromFirstArgument,
'LREM' => $getKeyFromFirstArgument,
'LSET' => $getKeyFromFirstArgument,
'LTRIM' => $getKeyFromFirstArgument,
/* commands operating on arrays */
'ARCOUNT' => $getKeyFromFirstArgument,
'ARDEL' => $getKeyFromFirstArgument,
'ARDELRANGE' => $getKeyFromFirstArgument,
'ARGET' => $getKeyFromFirstArgument,
'ARGETRANGE' => $getKeyFromFirstArgument,
'ARGREP' => $getKeyFromFirstArgument,
'ARINFO' => $getKeyFromFirstArgument,
'ARINSERT' => $getKeyFromFirstArgument,
'ARLASTITEMS' => $getKeyFromFirstArgument,
'ARLEN' => $getKeyFromFirstArgument,
'ARMGET' => $getKeyFromFirstArgument,
'ARMSET' => $getKeyFromFirstArgument,
'ARNEXT' => $getKeyFromFirstArgument,
'AROP' => $getKeyFromFirstArgument,
'ARRING' => $getKeyFromFirstArgument,
'ARSCAN' => $getKeyFromFirstArgument,
'ARSEEK' => $getKeyFromFirstArgument,
'ARSET' => $getKeyFromFirstArgument,
/* commands operating on sets */
'SADD' => $getKeyFromFirstArgument,
'SCARD' => $getKeyFromFirstArgument,
'SDIFF' => $getKeyFromAllArguments,
'SDIFFSTORE' => $getKeyFromAllArguments,
'SINTER' => $getKeyFromAllArguments,
'SINTERSTORE' => $getKeyFromAllArguments,
'SUNION' => $getKeyFromAllArguments,
'SUNIONSTORE' => $getKeyFromAllArguments,
'SISMEMBER' => $getKeyFromFirstArgument,
'SMEMBERS' => $getKeyFromFirstArgument,
'SSCAN' => $getKeyFromFirstArgument,
'SPOP' => $getKeyFromFirstArgument,
'SRANDMEMBER' => $getKeyFromFirstArgument,
'SREM' => $getKeyFromFirstArgument,
'SADDEX' => $getKeyFromFirstArgument,
'SEXPIRE' => $getKeyFromFirstArgument,
'SEXPIREAT' => $getKeyFromFirstArgument,
'SPEXPIRE' => $getKeyFromFirstArgument,
'SPEXPIREAT' => $getKeyFromFirstArgument,
'STTL' => $getKeyFromFirstArgument,
'SPTTL' => $getKeyFromFirstArgument,
'SEXPIRETIME' => $getKeyFromFirstArgument,
'SPEXPIRETIME' => $getKeyFromFirstArgument,
'SPERSIST' => $getKeyFromFirstArgument,
/* commands operating on sorted sets */
'ZADD' => $getKeyFromFirstArgument,
'ZCARD' => $getKeyFromFirstArgument,
'ZCOUNT' => $getKeyFromFirstArgument,
'ZINCRBY' => $getKeyFromFirstArgument,
'ZINTERSTORE' => [$this, 'getKeyFromZsetAggregationCommands'],
'ZRANGE' => $getKeyFromFirstArgument,
'ZRANGEBYSCORE' => $getKeyFromFirstArgument,
'ZRANK' => $getKeyFromFirstArgument,
'ZREM' => $getKeyFromFirstArgument,
'ZREMRANGEBYRANK' => $getKeyFromFirstArgument,
'ZREMRANGEBYSCORE' => $getKeyFromFirstArgument,
'ZREVRANGE' => $getKeyFromFirstArgument,
'ZREVRANGEBYSCORE' => $getKeyFromFirstArgument,
'ZREVRANK' => $getKeyFromFirstArgument,
'ZSCORE' => $getKeyFromFirstArgument,
'ZMSCORE' => $getKeyFromFirstArgument,
'ZUNIONSTORE' => [$this, 'getKeyFromZsetAggregationCommands'],
'ZSCAN' => $getKeyFromFirstArgument,
'ZLEXCOUNT' => $getKeyFromFirstArgument,
'ZRANGEBYLEX' => $getKeyFromFirstArgument,
'ZREMRANGEBYLEX' => $getKeyFromFirstArgument,
'ZREVRANGEBYLEX' => $getKeyFromFirstArgument,
/* commands operating on hashes */
'HDEL' => $getKeyFromFirstArgument,
'HEXISTS' => $getKeyFromFirstArgument,
'HGET' => $getKeyFromFirstArgument,
'HGETALL' => $getKeyFromFirstArgument,
'HMGET' => $getKeyFromFirstArgument,
'HMSET' => $getKeyFromFirstArgument,
'HINCRBY' => $getKeyFromFirstArgument,
'HINCRBYFLOAT' => $getKeyFromFirstArgument,
'HKEYS' => $getKeyFromFirstArgument,
'HLEN' => $getKeyFromFirstArgument,
'HSET' => $getKeyFromFirstArgument,
'HSETNX' => $getKeyFromFirstArgument,
'HVALS' => $getKeyFromFirstArgument,
'HSCAN' => $getKeyFromFirstArgument,
'HSTRLEN' => $getKeyFromFirstArgument,
'HIMPORT' => [$this, 'getKeyFromHimportCommands'],
'HEXPIRE' => $getKeyFromFirstArgument,
'HEXPIREAT' => $getKeyFromFirstArgument,
'HPERSIST' => $getKeyFromFirstArgument,
'HPEXPIRE' => $getKeyFromFirstArgument,
'HPEXPIREAT' => $getKeyFromFirstArgument,
'HTTL' => $getKeyFromFirstArgument,
'HPTTL' => $getKeyFromFirstArgument,
'HEXPIRETIME' => $getKeyFromFirstArgument,
'HPEXPIRETIME' => $getKeyFromFirstArgument,
'HGETEX' => $getKeyFromFirstArgument,
'HGETDEL' => $getKeyFromFirstArgument,
/* commands operating on streams */
'XACK' => $getKeyFromFirstArgument,
'XACKDEL' => $getKeyFromFirstArgument,
'XADD' => $getKeyFromFirstArgument,
'XAUTOCLAIM' => $getKeyFromFirstArgument,
'XCFGSET' => $getKeyFromFirstArgument,
'XCLAIM' => $getKeyFromFirstArgument,
'XDEL' => $getKeyFromFirstArgument,
'XDELEX' => $getKeyFromFirstArgument,
'XGROUP' => [$this, 'getKeyFromStreamGroupCommands'],
'XINFO' => [$this, 'getKeyFromStreamGroupCommands'],
'XLEN' => $getKeyFromFirstArgument,
'XNACK' => $getKeyFromFirstArgument,
'XPENDING' => $getKeyFromFirstArgument,
'XRANGE' => $getKeyFromFirstArgument,
'XREAD' => [$this, 'getKeyFromStreamReadCommands'],
'XREADGROUP' => [$this, 'getKeyFromStreamReadCommands'],
'XREVRANGE' => $getKeyFromFirstArgument,
'XSETID' => $getKeyFromFirstArgument,
'XTRIM' => $getKeyFromFirstArgument,
/* commands operating on blessings */
'BLESS' => [$this, 'getKeyFromBlessCommands'],
/* commands operating on vector sets */
'VADD' => $getKeyFromFirstArgument,
'VCARD' => $getKeyFromFirstArgument,
'VDIM' => $getKeyFromFirstArgument,
'VEMB' => $getKeyFromFirstArgument,
'VGETATTR' => $getKeyFromFirstArgument,
'VINFO' => $getKeyFromFirstArgument,
'VLINKS' => $getKeyFromFirstArgument,
'VRANDMEMBER' => $getKeyFromFirstArgument,
'VRANGE' => $getKeyFromFirstArgument,
'VREM' => $getKeyFromFirstArgument,
'VSETATTR' => $getKeyFromFirstArgument,
'VSIM' => $getKeyFromFirstArgument,
/* commands operating on time series */
'TS.READ' => $getKeyFromFirstArgument,
/* commands operating on HyperLogLog */
'PFADD' => $getKeyFromFirstArgument,
'PFCOUNT' => $getKeyFromAllArguments,
'PFMERGE' => $getKeyFromAllArguments,
/* scripting */
'EVAL' => [$this, 'getKeyFromScriptingCommands'],
'EVALSHA' => [$this, 'getKeyFromScriptingCommands'],
'EVAL_RO' => [$this, 'getKeyFromScriptingCommands'],
'EVALSHA_RO' => [$this, 'getKeyFromScriptingCommands'],
/* server */
'INFO' => [$this, 'getFakeKey'],
/* commands performing geospatial operations */
'GEOADD' => $getKeyFromFirstArgument,
'GEOHASH' => $getKeyFromFirstArgument,
'GEOPOS' => $getKeyFromFirstArgument,
'GEODIST' => $getKeyFromFirstArgument,
'GEORADIUS' => [$this, 'getKeyFromGeoradiusCommands'],
'GEORADIUSBYMEMBER' => [$this, 'getKeyFromGeoradiusCommands'],
/* sharded pubsub */
'SSUBSCRIBE' => $getKeyFromAllArguments,
'SUNSUBSCRIBE' => [$this, 'getKeyFromSUnsubscribeCommand'],
'SPUBLISH' => $getKeyFromFirstArgument,
/* cluster */
'CLUSTER' => [$this, 'getFakeKey'],
/* control */
'ACL' => [$this, 'getFakeKey'],
];
}
/**
* Returns the list of IDs for the supported commands.
*
* @return array
*/
public function getSupportedCommands()
{
return array_keys($this->commands);
}
/**
* Sets an handler for the specified command ID.
*
* The signature of the callback must have a single parameter of type
* Predis\Command\CommandInterface.
*
* When the callback argument is omitted or NULL, the previously associated
* handler for the specified command ID is removed.
*
* @param string $commandID Command ID.
* @param mixed $callback A valid callable object, or NULL to unset the handler.
*
* @throws InvalidArgumentException
*/
public function setCommandHandler($commandID, $callback = null)
{
$commandID = strtoupper($commandID);
if (!isset($callback)) {
unset($this->commands[$commandID]);
return;
}
if (!is_callable($callback)) {
throw new InvalidArgumentException(
'The argument must be a callable object or NULL.'
);
}
$this->commands[$commandID] = $callback;
}
/**
* Get fake key for commands with no key argument.
*
* @return string
*/
protected function getFakeKey(): string
{
return 'key';
}
/**
* Extracts the key from the first argument of a command instance.
*
* @param CommandInterface $command Command instance.
*
* @return string
*/
protected function getKeyFromFirstArgument(CommandInterface $command)
{
return $command->getArgument(0);
}
/**
* Extracts the key from a command with multiple keys only when all keys in
* the arguments array produce the same hash.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromAllArguments(CommandInterface $command)
{
$arguments = $command->getArguments();
if (!$this->checkSameSlotForKeys($arguments)) {
return null;
}
return $arguments[0];
}
/**
* Extracts the key from a command with multiple keys only when all keys in
* the arguments array produce the same hash.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromInterleavedArguments(CommandInterface $command)
{
$arguments = $command->getArguments();
$keys = [];
for ($i = 0; $i < count($arguments); $i += 2) {
$keys[] = $arguments[$i];
}
if (!$this->checkSameSlotForKeys($keys)) {
return null;
}
return $arguments[0];
}
/**
* Extracts the key from SORT command.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromSortCommand(CommandInterface $command)
{
$arguments = $command->getArguments();
$firstKey = $arguments[0];
if (1 === $argc = count($arguments)) {
return $firstKey;
}
$keys = [$firstKey];
for ($i = 1; $i < $argc; ++$i) {
if (strtoupper($arguments[$i]) === 'STORE') {
$keys[] = $arguments[++$i];
}
}
if (!$this->checkSameSlotForKeys($keys)) {
return null;
}
return $firstKey;
}
/**
* Extracts the key from the OBJECT command, where it follows the subcommand.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromObjectCommand(CommandInterface $command)
{
$arguments = $command->getArguments();
if (!isset($arguments[1])) {
return null;
}
return $arguments[1];
}
/**
* Extracts the key from commands where the first two arguments are keys,
* followed by non-key arguments (e.g. LMOVEM).
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromFirstTwoKeys(CommandInterface $command)
{
$arguments = $command->getArguments();
if (!isset($arguments[1])) {
return $arguments[0] ?? null;
}
if (!$this->checkSameSlotForKeys(array_slice($arguments, 0, 2))) {
return null;
}
return $arguments[0];
}
/**
* Extracts the key from BLPOP and BRPOP commands.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromBlockingListCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
if (!$this->checkSameSlotForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
return null;
}
return $arguments[0];
}
/**
* Extracts the key from BITOP command.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromBitOp(CommandInterface $command)
{
$arguments = $command->getArguments();
if (!$this->checkSameSlotForKeys(array_slice($arguments, 1, count($arguments)))) {
return null;
}
return $arguments[1];
}
/**
* Extracts the key from BLESS commands.
*
* BLESS GET/SET/CLEAR operate on a key at position 1, after the subcommand.
* BLESS SCAN takes a cursor and a flag instead of a key, so it has no slot
* and this returns null.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromBlessCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
if (isset($arguments[0], $arguments[1]) && in_array(strtoupper($arguments[0]), ['GET', 'SET', 'CLEAR'], true)) {
return $arguments[1];
}
return null;
}
/**
* Extracts the key from HIMPORT commands.
*
* Only HIMPORT SET operates on a key (at position 1, after the subcommand)
* and is routed by its hash slot. HIMPORT PREPARE/DISCARD/DISCARDALL are
* connection-session commands that the server advertises as all-shards
* commands; they have no key, so this returns null and the cluster
* connection rejects them. The dedicated container command is the sanctioned
* path for fanning those out across the master shards.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromHimportCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
if (isset($arguments[0], $arguments[1]) && strtoupper($arguments[0]) === 'SET') {
return $arguments[1];
}
return null;
}
/**
* Extracts the key from GEORADIUS and GEORADIUSBYMEMBER commands.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromGeoradiusCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
$argc = count($arguments);
$startIndex = $command->getId() === 'GEORADIUS' ? 5 : 4;
if ($argc > $startIndex) {
$keys = [$arguments[0]];
for ($i = $startIndex; $i < $argc; ++$i) {
$argument = strtoupper($arguments[$i]);
if ($argument === 'STORE' || $argument === 'STOREDIST') {
$keys[] = $arguments[++$i];
}
}
if (!$this->checkSameSlotForKeys($keys)) {
return null;
}
}
return $arguments[0];
}
/**
* Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
$keys = array_merge([$arguments[0]], array_slice($arguments, 2, $arguments[1]));
if (!$this->checkSameSlotForKeys($keys)) {
return null;
}
return $arguments[0];
}
/**
* Extracts key from SUNSUBSCRIBE command if it's given.
*
* @param CommandInterface $command
* @return string
*/
protected function getKeyFromSUnsubscribeCommand(CommandInterface $command): ?string
{
$arguments = $command->getArguments();
// SUNSUBSCRIBE command could be called without arguments, so it doesn't matter on each node it will be called.
if (empty($arguments)) {
return 'fake';
}
return $this->getKeyFromAllArguments($command);
}
/**
* Extracts the key from XGROUP and XINFO commands, where it follows the subcommand.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromStreamGroupCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
// Subcommands such as XINFO HELP and XGROUP HELP take no key at all.
if (!isset($arguments[1])) {
return null;
}
return $arguments[1];
}
/**
* Extracts the key from XREAD and XREADGROUP commands, where the STREAMS token is followed by
* the same number of keys and IDs.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromStreamReadCommands(CommandInterface $command)
{
$arguments = $command->getArguments();
$offset = $command->getId() === 'XREADGROUP' ? 3 : 0;
$position = null;
for ($index = $offset, $argc = count($arguments); $index < $argc; ++$index) {
if (is_string($arguments[$index]) && strtoupper($arguments[$index]) === 'STREAMS') {
$position = $index;
break;
}
}
if ($position === null) {
return null;
}
$keysAndIds = array_slice($arguments, $position + 1);
$count = count($keysAndIds);
if ($count === 0 || $count % 2 !== 0) {
return null;
}
$keys = array_slice($keysAndIds, 0, intdiv($count, 2));
if (!$this->checkSameSlotForKeys($keys)) {
return null;
}
return $keys[0];
}
/**
* Extracts the key from EVAL and EVALSHA commands.
*
* @param CommandInterface $command Command instance.
*
* @return string|null
*/
protected function getKeyFromScriptingCommands(CommandInterface $command)
{
$keys = $command instanceof ScriptCommand
? $command->getKeys()
: array_slice($args = $command->getArguments(), 2, $args[1]);
if (!$keys || !$this->checkSameSlotForKeys($keys)) {
return null;
}
return $keys[0];
}
/**
* {@inheritdoc}
*/
public function getSlot(CommandInterface $command)
{
$slot = $command->getSlot();
if (!isset($slot) && isset($this->commands[$cmdID = $command->getId()])) {
$key = call_user_func($this->commands[$cmdID], $command);
if (isset($key)) {
$slot = $this->getSlotByKey($key);
$command->setSlot($slot);
}
}
return $slot;
}
/**
* {@inheritdoc}
*/
public function checkSameSlotForKeys(array $keys): bool
{
if (!$count = count($keys)) {
return false;
}
$currentSlot = $this->getSlotByKey($keys[0]);
for ($i = 1; $i < $count; ++$i) {
$nextSlot = $this->getSlotByKey($keys[$i]);
if ($currentSlot !== $nextSlot) {
return false;
}
}
return true;
}
/**
* Returns only the hashable part of a key (delimited by "{...}"), or the
* whole key if a key tag is not found in the string.
*
* @param string $key A key.
*
* @return string
*/
protected function extractKeyTag($key)
{
if (false !== $start = strpos($key, '{')) {
if (false !== ($end = strpos($key, '}', $start)) && $end !== ++$start) {
$key = substr($key, $start, $end - $start);
}
}
return $key;
}
}