Skip to content

Commit a271c48

Browse files
Initial commit incorporating a "readonly" flag.
We may want to configure phpredis such that it will attempt to fall back to a given master's slave, if the master were to go down (and the command is read only).
1 parent 48e6e67 commit a271c48

File tree

4 files changed

+134
-199
lines changed

4 files changed

+134
-199
lines changed

cluster_library.c

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -502,100 +502,6 @@ static char **split_str_by_delim(char *str, char *delim, int *len) {
502502
return array;
503503
}
504504

505-
/*static int
506-
cluster_parse_node_line(RedisSock *sock, char *line, clusterNodeInfo *info) {
507-
char **array, *p, *p2;
508-
int count, i;
509-
510-
// First split by space
511-
array = split_str_by_delim(line, " ", &count);
512-
513-
// Sanity check on the number of elements we see
514-
if(count < CLUSTER_MIN_NODE_LINE) {
515-
efree(array);
516-
return -1;
517-
}
518-
519-
// Sanity check on our cluster ID value
520-
if(strlen(array[CLUSTER_NODES_HASH])!=CLUSTER_NAME_LEN) {
521-
efree(array);
522-
return -1;
523-
}
524-
525-
// Our cluster ID
526-
info->name = estrndup(array[CLUSTER_NODES_HASH], CLUSTER_NAME_LEN);
527-
528-
// Set the host/port
529-
if(memcmp(array[CLUSTER_NODES_HOST_PORT], ":0", sizeof(":0"))==0) {
530-
info->seed = 1;
531-
info->host_len = strlen(sock->host);
532-
info->host = estrndup(sock->host, info->host_len);
533-
info->port = sock->port;
534-
} else if((p = strchr(array[CLUSTER_NODES_HOST_PORT], ':'))!=NULL) {
535-
// Null terminate at the : character
536-
*p = '\0';
537-
538-
info->seed = 0;
539-
info->host_len = p - array[CLUSTER_NODES_HOST_PORT];
540-
info->host = estrndup(array[CLUSTER_NODES_HOST_PORT], info->host_len);
541-
info->port = atoi(p+1);
542-
} else {
543-
efree(array);
544-
return -1;
545-
}
546-
547-
// Flag this a slave node if it's a slave
548-
info->slave = strstr(array[CLUSTER_NODES_TYPE], "slave")!=NULL;
549-
550-
// If we've got a master hash slot, set it
551-
if(memcmp(array[CLUSTER_NODES_MASTER_HASH], "-", sizeof("-"))!=0) {
552-
if(strlen(array[CLUSTER_NODES_MASTER_HASH])!=CLUSTER_NAME_LEN) {
553-
efree(array);
554-
return -1;
555-
}
556-
info->master_name = estrndup(array[CLUSTER_NODES_MASTER_HASH],
557-
CLUSTER_NAME_LEN);
558-
} else if(info->slave) {
559-
// Slaves should always have a master hash
560-
efree(array);
561-
return -1;
562-
}
563-
564-
// See if the node serves slots
565-
if(count >= CLUSTER_MIN_SLOTS_COUNT) {
566-
// Allocate for enough ranges
567-
info->slots_size = count - CLUSTER_MIN_SLOTS_COUNT + 1;
568-
info->slots = ecalloc(info->slots_size, sizeof(clusterSlotRange));
569-
570-
// Now iterate over each range
571-
for(i=0;i<info->slots_size;i++) {
572-
p = array[i+CLUSTER_MIN_SLOTS_COUNT-1];
573-
574-
// If we don't see -, this node only serves one slot
575-
if((p2 = strchr(p,'-'))==NULL) {
576-
info->slots[i].start = atoi(p);
577-
info->slots[i].end = atoi(p);
578-
} else {
579-
*p2++ = '\0';
580-
581-
// Set this range
582-
info->slots[i].start = atoi(p);
583-
info->slots[i].end = atoi(p2);
584-
}
585-
}
586-
} else {
587-
info->slots_size = 0;
588-
info->slots = NULL;
589-
}
590-
591-
// Free our array
592-
efree(array);
593-
594-
// Success!
595-
return 0;
596-
}
597-
*/
598-
599505
/* Execute a CLUSTER SLOTS command against the seed socket, and return the
600506
* reply or NULL on failure. */
601507
clusterReply* cluster_get_slots(RedisSock *redis_sock TSRMLS_DC)
@@ -1357,8 +1263,7 @@ PHPAPI short cluster_send_command(redisCluster *c, short slot, const char *cmd,
13571263
* we try to send the request to the Cluster *or* if a non MOVED or ASK
13581264
* error is encountered, in which case our response processing macro will
13591265
* short circuit and RETURN_FALSE, as the error will have already been
1360-
* consumed.
1361-
*/
1266+
* consumed. */
13621267

13631268
/* RAW bulk response handler */
13641269
PHPAPI void cluster_bulk_raw_resp(INTERNAL_FUNCTION_PARAMETERS,

cluster_library.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ typedef struct redisCluster {
185185
/* How long in milliseconds should we wait when being bounced around */
186186
long waitms;
187187

188+
/* Are we flagged as being in readonly mode, meaning we could fall back to
189+
* a given master's slave */
190+
short readonly;
191+
188192
/* Hash table of seed host/ports */
189193
HashTable *seeds;
190194

0 commit comments

Comments
 (0)