@@ -1072,10 +1072,12 @@ static int cluster_dist_write(redisCluster *c, const char *cmd, size_t sz,
1072
1072
int i , count = 1 , * nodes ;
1073
1073
RedisSock * redis_sock ;
1074
1074
1075
- /* Allocate enough memory for the master and all of our slaves */
1075
+ /* Determine our overall node count */
1076
1076
if (c -> master [c -> cmd_slot ]-> slaves ) {
1077
1077
count += zend_hash_num_elements (c -> master [c -> cmd_slot ]-> slaves );
1078
1078
}
1079
+
1080
+ /* Allocate memory for master + slaves or just slaves */
1079
1081
nodes = emalloc (sizeof (int )* count );
1080
1082
1081
1083
/* Populate our array with the master and each of it's slaves, then
@@ -1084,15 +1086,19 @@ static int cluster_dist_write(redisCluster *c, const char *cmd, size_t sz,
1084
1086
fyshuffle (nodes , count );
1085
1087
1086
1088
/* Iterate through our nodes until we find one we can write to or fail */
1087
- for (i = nomaster ; i < count ; i ++ ) {
1089
+ for (i = 0 ; i < count ; i ++ ) {
1090
+ /* Skip if this is the master node and we don't want to query that */
1091
+ if (nomaster && nodes [i ] == 0 )
1092
+ continue ;
1093
+
1088
1094
/* Get the slave for this index */
1089
1095
redis_sock = cluster_slot_sock (c , c -> cmd_slot , nodes [i ]);
1090
1096
if (!redis_sock ) continue ;
1091
1097
1092
1098
/* Connect to this node if we haven't already */
1093
1099
CLUSTER_LAZY_CONNECT (redis_sock );
1094
1100
1095
- /* If we're not on the master, attempt to send the READONLY commadn to
1101
+ /* If we're not on the master, attempt to send the READONLY command to
1096
1102
* this slave, and skip it if that fails */
1097
1103
if (nodes [i ] == 0 || redis_sock -> readonly ||
1098
1104
cluster_send_readonly (redis_sock TSRMLS_CC ) == 0 )
@@ -1127,7 +1133,10 @@ static int cluster_dist_write(redisCluster *c, const char *cmd, size_t sz,
1127
1133
* If we're unable to communicate with this slot's master, we attempt the query
1128
1134
* against any slaves (at random) that this master has.
1129
1135
* REDIS_FAILOVER_DISTRIBUTE:
1130
- * We pick at random from the master and any slaves it has. This option is
1136
+ * We pick at random from the master and any slaves it has. This option will
1137
+ * load balance between masters and slaves
1138
+ * REDIS_FAILOVER_DISTRIBUTE_SLAVES:
1139
+ * We pick at random from slave nodes of a given master. This option is
1131
1140
* used to load balance read queries against N slaves.
1132
1141
*
1133
1142
* Once we are able to find a node we can write to, we check for MOVED or
@@ -1138,7 +1147,7 @@ static int cluster_sock_write(redisCluster *c, const char *cmd, size_t sz,
1138
1147
{
1139
1148
redisClusterNode * * seed_node ;
1140
1149
RedisSock * redis_sock ;
1141
- int failover ;
1150
+ int failover , nomaster ;
1142
1151
1143
1152
/* First try the socket requested */
1144
1153
redis_sock = c -> cmd_sock ;
@@ -1170,9 +1179,14 @@ static int cluster_sock_write(redisCluster *c, const char *cmd, size_t sz,
1170
1179
CLUSTER_LAZY_CONNECT (redis_sock );
1171
1180
if (CLUSTER_SEND_PAYLOAD (redis_sock , cmd , sz ) ||
1172
1181
!cluster_dist_write (c , cmd , sz , 1 TSRMLS_CC )) return 0 ;
1173
- } else if (!cluster_dist_write (c , cmd , sz , 0 TSRMLS_CC )) {
1174
- /* We were able to write to a master or slave at random */
1175
- return 0 ;
1182
+ } else {
1183
+ /* Include or exclude master node depending on failover option and
1184
+ * attempt to make our write */
1185
+ nomaster = failover == REDIS_FAILOVER_DISTRIBUTE_SLAVES ;
1186
+ if (!cluster_dist_write (c , cmd , sz , nomaster TSRMLS_CC )) {
1187
+ /* We were able to write to a master or slave at random */
1188
+ return 0 ;
1189
+ }
1176
1190
}
1177
1191
1178
1192
/* Don't fall back if direct communication with this slot is required. */
0 commit comments