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
4 changes: 3 additions & 1 deletion cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ print_r($obj_cluster->exec());
```

## Pipelining
The RedisCluster class does not support pipelining as there is no way to detect whether the keys still live where our map indicates that they do and would therefore be inherently unsafe. It would be possible to implement this support as an option if there is demand for such a feature.
`RedisCluster` does not expose a general pipelining API. `MGET` can pipeline its internal slot batches as described below.

## Multiple key commands
Redis cluster does allow commands that operate on multiple keys, but only if all of those keys hash to the same slot. Note that it is not enough that the keys are all on the same node, but must actually hash to the exact same hash slot.
Expand All @@ -136,6 +136,8 @@ $obj_cluster->mget(["{hash1}key1","{hash1}key2","{hash1}key3","otherkey"]);

This operation can also be done in MULTI mode transparently.

`MGET` sends its slot batches over the mapped master connections before reading replies. This applies outside `MULTI` with the default `OPT_SLAVE_FAILOVER` value, `FAILOVER_NONE`. Results keep the input key order. Watched connections and other failover modes keep sequential execution.

## Directed node commands
There are a variety of commands which have to be directed at a specific node. In the case of these commands, the caller can either pass a key (which will be hashed and used to direct our command), or an array with host:port.

Expand Down
194 changes: 194 additions & 0 deletions cluster_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <errno.h>

extern zend_class_entry *redis_cluster_exception_ce;
extern zend_class_entry *redis_exception_ce;
int le_cluster_slot_cache;

/* Debugging methods/
Expand Down Expand Up @@ -1331,6 +1332,199 @@ static int cluster_check_response(redisCluster *c, REDIS_REPLY_TYPE *reply_type)
return 0;
}

typedef struct {
RedisCmd *cmd;
RedisSock *sock;
clusterReply *reply;
} clusterMgetBatch;

typedef struct {
RedisSock *sock;
smart_string payload;
} clusterMgetNode;

/* Return zero to use the ordinary MGET path after draining any queued replies. */
int cluster_mget_pipelined(redisCluster *c, HashTable *keys, zval *result)
{
clusterMgetBatch *batches, *batch = NULL;
clusterMgetNode *node;
HashTable nodes;
RedisSock decoder = {0};
zend_string *error = NULL;
zval *key, value;
uint32_t count = 0, i, j;
int status = 0, pending = 0, retry = 0, clusterdown = 0, nodes_initialized = 0;

batches = ecalloc(zend_hash_num_elements(keys), sizeof(*batches));
ZEND_HASH_FOREACH_VAL(keys, key) {
char *str;
size_t len;
unsigned short slot;
int prefixed;

convert_to_string(key);
if (EG(exception)) {
status = -1;
goto cleanup;
}
str = Z_STRVAL_P(key);
len = Z_STRLEN_P(key);
prefixed = redis_key_prefix(c->flags, &str, &len);
slot = cluster_hash_key(str, len);

if (batch == NULL || batch->cmd->slot != slot) {
batch = &batches[count++];
batch->cmd = redis_cmd_create_literal(c->flags, "MGET");
batch->cmd->slot = slot;
}
redis_cmd_cat_str(batch->cmd, str, len);
if (prefixed) efree(str);
} ZEND_HASH_FOREACH_END();

if (count < 2) goto cleanup;

zend_hash_init(&nodes, zend_hash_num_elements(c->nodes), NULL, NULL, 0);
nodes_initialized = 1;

/* Open every connection before leaving responses unread on any socket. */
for (i = 0; i < count; i++) {
batch = &batches[i];
if (!cluster_slot(c, batch->cmd->slot)) goto cleanup;
batch->sock = cluster_slot_master_sock(c, batch->cmd->slot);
if (batch->sock->watching) goto cleanup;
node = zend_hash_index_find_ptr(&nodes, (zend_ulong)(uintptr_t)batch->sock);
if (node == NULL) {
if (!cluster_sock_live(batch->sock)) {
status = EG(exception) ? -1 : 0;
goto cleanup;
}
node = ecalloc(1, sizeof(*node));
node->sock = batch->sock;
zend_hash_index_add_ptr(&nodes, (zend_ulong)(uintptr_t)batch->sock, node);
}
smart_string_appendl(&node->payload, redis_cmd_str(batch->cmd),
redis_cmd_len(batch->cmd));
}

pending = 1;
ZEND_HASH_FOREACH_PTR(&nodes, node) {
if (!cluster_write_payload(node->sock, node->payload.c, node->payload.len)) {
cluster_cache_clear(c);
c->redir_type = REDIR_NONE;
status = EG(exception) ? -1 : 0;
goto cleanup;
}
smart_string_free(&node->payload);
} ZEND_HASH_FOREACH_END();

for (i = 0; i < count; i++) {
int response;

batch = &batches[i];
c->cmd_sock = batch->sock;
c->cmd_slot = batch->cmd->slot;
response = cluster_check_response(c, &c->reply_type);
if (response < 0) goto io_failure;
if (response > 0) {
if (error == NULL) retry = 1;
continue;
}
if (c->err) {
if (error == NULL) {
error = zend_string_copy(c->err);
clusterdown = c->clusterdown;
}
continue;
}

if (c->reply_type != TYPE_MULTIBULK ||
c->reply_len != batch->cmd->argc - 1)
{
goto io_failure;
}
batch->reply = cluster_read_resp(c, 0);
if (batch->reply == NULL) goto io_failure;
for (j = 0; j < batch->reply->elements; j++) {
if (batch->reply->element[j]->type != TYPE_BULK) goto io_failure;
}
}
pending = 0;
c->redir_type = REDIR_NONE;

/* Native redirection handling can safely remap nodes once replies are drained. */
if (retry) goto cleanup;
server_error:
if (error) {
cluster_clear_error(c);
c->err = error;
error = NULL;
c->clusterdown = clusterdown;
if (clusterdown) {
cluster_cache_clear(c);
CLUSTER_THROW_EXCEPTION("The Redis Cluster is down (CLUSTERDOWN)", 0);
}
status = -1;
goto cleanup;
}

/* Unserializers can call back into this client, so decode only after draining. */
for (i = 0; i < count; i++) {
/* Match the ordinary MGET handler's per-command decoding options. */
decoder.serializer = c->flags->serializer;
decoder.compression = c->flags->compression;
for (j = 0; j < batches[i].reply->elements; j++) {
clusterReply *reply = batches[i].reply->element[j];

if (reply->len == -1) {
ZVAL_FALSE(&value);
} else {
redis_unpack(&decoder, reply->str, reply->len, &value);
efree(reply->str);
reply->str = NULL;
}
add_next_index_zval(result, &value);
if (EG(exception)) {
status = -1;
goto cleanup;
}
}
}
status = 1;
goto cleanup;

io_failure:
cluster_cache_clear(c);
c->redir_type = REDIR_NONE;
if (error && !retry &&
(!EG(exception) || EG(exception)->ce == redis_exception_ce ||
EG(exception)->ce == redis_cluster_exception_ce))
{
if (EG(exception)) zend_clear_exception();
goto server_error;
}
if (!EG(exception)) {
CLUSTER_THROW_EXCEPTION("Error processing response from Redis node!", 0);
}
status = -1;

cleanup:
if (nodes_initialized) {
ZEND_HASH_FOREACH_PTR(&nodes, node) {
if (pending) redis_sock_disconnect(node->sock, 1, 1);
smart_string_free(&node->payload);
efree(node);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&nodes);
}
for (i = 0; i < count; i++) {
redis_cmd_free(batches[i].cmd);
if (batches[i].reply) cluster_free_reply(batches[i].reply, 1);
}
if (error) zend_string_release(error);
efree(batches);
return status;
}

/* Disconnect from each node we're connected to */
PHP_REDIS_API void cluster_disconnect(redisCluster *c, int force) {
const redisClusterNode *node, *slave;
Expand Down
2 changes: 2 additions & 0 deletions cluster_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ long long mstime(void);
PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot,
const char *cmd, int cmd_len);

int cluster_mget_pipelined(redisCluster *c, HashTable *keys, zval *result);

static zend_always_inline short
cluster_send_rcmd(redisCluster *c, RedisCmd *cmd)
{
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<!-- liblzf -->
<dir name="tests">
<file role="test" name="RedisArrayTest.php"/>
<file role="test" name="RedisClusterMGetServer.php"/>
<file role="test" name="RedisClusterTest.php"/>
<file role="test" name="RedisSentinelTest.php"/>
<file role="test" name="RedisTest.php"/>
Expand Down
19 changes: 19 additions & 0 deletions redis_cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,25 @@ static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, const char *kw, int kw
/* MGET is readonly, DEL is not */
c->readonly = kw_len == 4 && cluster_is_atomic(c);

if (c->readonly && argc > 1 && c->failover == REDIS_FAILOVER_NONE) {
int status = cluster_mget_pipelined(c, ht_arr, z_ret);

if (status != 0) {
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
efree(z_args);

if (status > 0) {
RETVAL_ZVAL(z_ret, 0, 1);
efree(z_ret);
return 0;
}
return -1;
}
}

// Initialize our "multi" command handler with command/len
cluster_multi_init(&mc, kw, kw_len);

Expand Down
Loading
Loading