Skip to content

Commit e8c3ef9

Browse files
Fix MOVED redirection bug
Addresses phpredis#690 and possibly phpredis#673
1 parent 5241a5c commit e8c3ef9

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cluster_library.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ PHP_REDIS_API void cluster_free(redisCluster *c) {
846846
if (c->err) efree(c->err);
847847

848848
/* Free structure itself */
849-
efree(c);
849+
efree(c);
850850
}
851851

852852
/* Initialize seeds */
@@ -1089,7 +1089,7 @@ static int cluster_dist_write(redisCluster *c, const char *cmd, size_t sz,
10891089
for (i = 0; i < count; i++) {
10901090
/* Skip if this is the master node and we don't want to query that */
10911091
if (nomaster && nodes[i] == 0)
1092-
continue;
1092+
continue;
10931093

10941094
/* Get the slave for this index */
10951095
redis_sock = cluster_slot_sock(c, c->cmd_slot, nodes[i]);
@@ -1240,6 +1240,8 @@ static redisClusterNode *cluster_find_node(redisCluster *c, const char *host,
12401240
* the slot where data was moved, update our node mapping */
12411241
static void cluster_update_slot(redisCluster *c TSRMLS_DC) {
12421242
redisClusterNode *node;
1243+
char key[1024];
1244+
size_t klen;
12431245

12441246
/* Do we already have the new slot mapped */
12451247
if(c->master[c->redir_slot]) {
@@ -1259,6 +1261,11 @@ static void cluster_update_slot(redisCluster *c TSRMLS_DC) {
12591261
node = cluster_node_create(c, c->redir_host, c->redir_host_len,
12601262
c->redir_port, c->redir_slot, 0);
12611263

1264+
/* Our node is new, so keep track of it for cleanup */
1265+
klen = snprintf(key,sizeof(key),"%s:%ld",c->redir_host,c->redir_port);
1266+
zend_hash_update(c->nodes, key, klen+1, (void*)&node,
1267+
sizeof(redisClusterNode*), NULL);
1268+
12621269
/* Now point our slot at the node */
12631270
c->master[c->redir_slot] = node;
12641271
}
@@ -1418,7 +1425,10 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char
14181425
}
14191426

14201427
/* Update mapping if the data has MOVED */
1421-
if (c->redir_type == REDIR_MOVED) cluster_update_slot(c TSRMLS_CC);
1428+
if (c->redir_type == REDIR_MOVED) {
1429+
cluster_update_slot(c TSRMLS_CC);
1430+
c->cmd_sock = SLOT_SOCK(c, slot);
1431+
}
14221432
}
14231433

14241434
/* Figure out if we've timed out trying to read or write the data */
@@ -2302,7 +2312,7 @@ int mbulk_resp_loop(RedisSock *redis_sock, zval *z_result,
23022312
while(count--) {
23032313
/* Read our line */
23042314
line = redis_sock_read(redis_sock, &line_len TSRMLS_CC);
2305-
2315+
23062316
if (line != NULL) {
23072317
zval *z = NULL;
23082318
if(redis_unserialize(redis_sock, line, line_len, &z TSRMLS_CC)==1) {

0 commit comments

Comments
 (0)