Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 47 additions & 43 deletions redis_cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,6 @@ static int get_key_ht(redisCluster *c, HashTable *ht, HashPosition *ptr,
return -1;
}

// Always want to work with strings
convert_to_string(z_key);

kv->key = Z_STRVAL_P(z_key);
kv->key_len = Z_STRLEN_P(z_key);
kv->key_free = redis_key_prefix(c->flags, &(kv->key), &(kv->key_len));
Expand All @@ -565,22 +562,41 @@ static int get_key_ht(redisCluster *c, HashTable *ht, HashPosition *ptr,
return 0;
}

/* Turn variable arguments into a HashTable for processing */
/* Own converted keys before any slot batch is sent or queued. */
static HashTable *method_args_to_ht(zval *z_args, int argc) {
HashTable *ht_ret;
HashTable *ht_ret, *ht_src = NULL;
zval *z_arg, z_key;
int i;

if (argc == 1 && Z_TYPE(z_args[0]) == IS_ARRAY) {
ht_src = Z_ARRVAL(z_args[0]);
argc = zend_hash_num_elements(ht_src);
}

/* Allocate our hash table */
ALLOC_HASHTABLE(ht_ret);
zend_hash_init(ht_ret, argc, NULL, NULL, 0);
zend_hash_init(ht_ret, argc, NULL, ZVAL_PTR_DTOR, 0);

/* Populate our return hash table with our arguments */
for (i = 0; i < argc; i++) {
zend_hash_next_index_insert(ht_ret, &z_args[i]);
if (ht_src) {
ZEND_HASH_FOREACH_VAL(ht_src, z_arg) {
ZVAL_STR(&z_key, zval_get_string(z_arg));
zend_hash_next_index_insert(ht_ret, &z_key);
if (UNEXPECTED(EG(exception))) goto failure;
} ZEND_HASH_FOREACH_END();
} else {
for (i = 0; i < argc; i++) {
ZVAL_STR(&z_key, zval_get_string(&z_args[i]));
zend_hash_next_index_insert(ht_ret, &z_key);
if (UNEXPECTED(EG(exception))) goto failure;
}
}

/* Return our hash table */
return ht_ret;

failure:
zend_hash_destroy(ht_ret);
FREE_HASHTABLE(ht_ret);
return NULL;
}

/* Convenience handler for commands that take multiple keys such as
Expand All @@ -594,7 +610,7 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
zval *z_args;
HashTable *ht_arr;
HashPosition ptr;
int i = 1, argc = ZEND_NUM_ARGS(), ht_free = 0;
int i = 1, argc = ZEND_NUM_ARGS();
short slot;

/* If we don't have any arguments we're invalid */
Expand All @@ -607,17 +623,14 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
return -1;
}

/* Determine if we're working with a single array or variadic args */
if (argc == 1 && Z_TYPE(z_args[0]) == IS_ARRAY) {
ht_arr = Z_ARRVAL(z_args[0]);
argc = zend_hash_num_elements(ht_arr);
if (!argc) {
efree(z_args);
return -1;
}
} else {
ht_arr = method_args_to_ht(z_args, argc);
ht_free = 1;
ht_arr = method_args_to_ht(z_args, argc);
efree(z_args);
if (ht_arr == NULL) return -1;

if ((argc = zend_hash_num_elements(ht_arr)) == 0) {
zend_hash_destroy(ht_arr);
FREE_HASHTABLE(ht_arr);
return -1;
}

/* MGET is readonly, DEL is not */
Expand All @@ -630,7 +643,9 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
// it's the first iteration every time, needlessly
zend_hash_internal_pointer_reset_ex(ht_arr, &ptr);
if (get_key_ht(c, ht_arr, &ptr, &kv) < 0) {
efree(z_args);
cluster_multi_free(&mc);
zend_hash_destroy(ht_arr);
FREE_HASHTABLE(ht_arr);
return -1;
}

Expand All @@ -648,11 +663,8 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
while (zend_hash_has_more_elements_ex(ht_arr, &ptr) ==SUCCESS) {
if (get_key_ht(c, ht_arr, &ptr, &kv) < 0) {
cluster_multi_free(&mc);
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
efree(z_args);
zend_hash_destroy(ht_arr);
FREE_HASHTABLE(ht_arr);
return -1;
}

Expand All @@ -662,12 +674,10 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
if (distcmd_resp_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, slot,
&mc, z_ret, i == argc, cb) < 0)
{
if (kv.key_free) efree(kv.key);
cluster_multi_free(&mc);
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
efree(z_args);
zend_hash_destroy(ht_arr);
FREE_HASHTABLE(ht_arr);
return -1;
}
}
Expand All @@ -684,30 +694,24 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,

zend_hash_move_forward_ex(ht_arr, &ptr);
}
efree(z_args);

// If we've got straggler(s) process them
if (mc.argc > 0) {
if (distcmd_resp_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, slot,
&mc, z_ret, 1, cb) < 0)
{
cluster_multi_free(&mc);
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
zend_hash_destroy(ht_arr);
FREE_HASHTABLE(ht_arr);
return -1;
}
}

// Free our command
cluster_multi_free(&mc);

/* Clean up our hash table if we constructed it from variadic args */
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
zend_hash_destroy(ht_arr);
FREE_HASHTABLE(ht_arr);

/* Return our object if we're in MULTI mode */
if (!cluster_is_atomic(c))
Expand Down
135 changes: 135 additions & 0 deletions tests/RedisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,141 @@ public function testSession_defaultLockRetryCount() { $this->markTestSkipped();
public function testSession_noUnlockOfOtherProcess() { $this->markTestSkipped(); }
public function testSession_lockWaitTime() { $this->markTestSkipped(); }

public function testMultiKeyCommandsHandleKeyConversionErrors() {
$invalid = new class {
public function __toString() {
throw new RuntimeException('key conversion failed');
}
};

foreach (['mget', 'del', 'unlink'] as $command) {
foreach ([[$invalid], ['{key-arguments}first', $invalid]] as $keys) {
$exception = null;
try {
if ($command === 'mget') {
$this->redis->mget($keys);
} else {
$this->redis->$command(...$keys);
}
} catch (RuntimeException $e) {
$exception = $e;
}

$this->assertIsObject($exception, RuntimeException::class);
$this->assertEquals('key conversion failed', $exception->getMessage());
$this->assertTrue($keys[count($keys) - 1] === $invalid);
$this->assertTrue($this->redis->set('{key-arguments}after', 'clean'));
$this->assertEquals('clean', $this->redis->get('{key-arguments}after'));
}
}
$this->redis->del('{key-arguments}after');
}

public function testMultiKeyCommandsRejectInvalidKeysBeforeDispatch() {
$invalid = new class {
public function __toString() {
throw new RuntimeException('key conversion failed');
}
};
$first = '{key-arguments-a}first';
$second = '{key-arguments-b}second';
$this->assertNotEquals(
$this->redis->rawCommand($first, 'CLUSTER', 'KEYSLOT', $first),
$this->redis->rawCommand($second, 'CLUSTER', 'KEYSLOT', $second)
);

foreach (['mget', 'del', 'unlink'] as $command) {
foreach ([Redis::ATOMIC, Redis::MULTI] as $mode) {
foreach ([$invalid, []] as $badKey) {
$this->redis->set($first, 'first');
$this->redis->set($second, 'second');
$transactionOpen = false;
if ($mode === Redis::MULTI) {
$this->redis->multi()->get($first);
$transactionOpen = true;
}

// Two slots precede the invalid key, so no earlier batch may escape.
$keys = [$first, $second, $badKey];
$exception = null;
try {
if ($command === 'mget') {
$this->redis->mget($keys);
} else {
$this->redis->$command(...$keys);
}
} catch (RuntimeException | ErrorException $e) {
$exception = $e;
}

try {
$this->assertIsObject($exception, is_array($badKey)
? ErrorException::class : RuntimeException::class);
$this->assertEquals(is_array($badKey)
? 'Array to string conversion' : 'key conversion failed',
$exception->getMessage());
$this->assertEquals([$first, $second, $badKey], $keys);

if ($mode === Redis::MULTI) {
$this->redis->get($second);
$result = $this->redis->exec();
$transactionOpen = false;
$this->assertEquals(['first', 'second'], $result);
}
$this->assertEquals('first', $this->redis->get($first));
$this->assertEquals('second', $this->redis->get($second));
} finally {
if ($transactionOpen) $this->redis->discard();
$this->redis->del($first, $second);
}
}
}
}
}

public function testMultiKeyCommandsKeepArrayKeyWarnings() {
$first = '{key-arguments}warning';
$keys = [$first, []];
$warning = null;
set_error_handler(function ($severity, $message) use (&$warning) {
$warning = $message;
return true;
}, E_WARNING | E_NOTICE);

try {
foreach (['mget', 'del', 'unlink'] as $command) {
$this->redis->set($first, 'first');
$this->redis->set('Array', 'array');
$warning = null;

$result = $command === 'mget'
? $this->redis->mget($keys) : $this->redis->$command(...$keys);

$this->assertEquals('Array to string conversion', $warning);
$this->assertEquals($command === 'mget' ? ['first', 'array'] : 2, $result);
$this->assertEquals([$first, []], $keys);
}
} finally {
restore_error_handler();
$this->redis->del($first, 'Array');
}
}

public function testMultiKeyCommandsIgnoreEmptyKeyArrays() {
$key = '{key-arguments}empty';
foreach (['mget', 'del', 'unlink'] as $command) {
$this->redis->del($key);
$this->assertFalse($this->redis->$command([]));

$this->redis->multi()->get($key);
$this->assertFalse($this->redis->$command([]));
$this->redis->set($key, 'value');
$this->assertEquals([false, true], $this->redis->exec());
$this->assertEquals('value', $this->redis->get($key));
}
$this->redis->del($key);
}

/* Regression test for GH #2810 */
public function testConstructNullSeeds() {
/* new RedisCluster(null, null) must not throw TypeError.
Expand Down
79 changes: 79 additions & 0 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,85 @@ public function testMultiple() {
$this->assertEquals(['test'], $this->redis->mget([1])); // non-string
}

public function testMultiKeyCommandsKeepArrayArguments() {
$prefix = $this->redis->getOption(Redis::OPT_PREFIX);

try {
foreach (['', 'key-arguments:'] as $testPrefix) {
$this->redis->setOption(Redis::OPT_PREFIX, $testPrefix);

foreach (['mget', 'del', 'unlink'] as $command) {
$number = 101;
$keys = [&$number, 202, '{key-arguments}one', '{key-arguments}two'];
$copy = $keys;
$values = ['first', 'second', 'third', 'fourth'];

foreach ($keys as $i => $key) {
$this->redis->set((string)$key, $values[$i]);
}

$result = $this->redis->$command($keys);
$this->assertEquals($command === 'mget' ? $values : 4, $result);
$this->assertEquals([101, 202, '{key-arguments}one', '{key-arguments}two'], $keys);
$this->assertEquals($keys, $copy);
$this->assertEquals(101, $number);
$number = 303;
$this->assertEquals(303, $keys[0]);
$this->assertEquals(303, $copy[0]);
}
}
} finally {
$this->redis->del(['101', '202', '{key-arguments}one', '{key-arguments}two']);
$this->redis->setOption(Redis::OPT_PREFIX, $prefix);
}
}

public function testMultiKeyCommandsKeepStringableArguments() {
foreach (['del', 'unlink'] as $command) {
$destroyed = 0;
$key = new class($destroyed) {
private $destroyed;

public function __construct(&$destroyed) {
$this->destroyed = &$destroyed;
}

public function __toString() {
return '{key-arguments}object';
}

public function __destruct() {
$this->destroyed++;
}
};

$this->redis->set((string)$key, 'value');
$this->assertEquals(1, $this->redis->$command($key));
$this->assertEquals(0, $destroyed);
$this->assertEquals('{key-arguments}object', (string)$key);

unset($key);
$this->assertEquals(1, $destroyed);
}
}

public function testMultiKeyCommandsDoNotLeakIntegerKeys() {
if (memory_get_usage() === 0)
$this->markTestSkipped('Zend memory manager is required');

foreach (['del', 'unlink'] as $command) {
for ($i = 0; $i < 10; $i++) {
$this->redis->$command(101, 202);
}

$before = memory_get_usage();
for ($i = 0; $i < 100; $i++) {
$this->redis->$command(101, 202);
}
$this->assertLTE(0, memory_get_usage() - $before);
}
}

public function testMultipleBin() {
$kvals = [
'binkey-1' => random_bytes(16),
Expand Down
Loading