summaryrefslogtreecommitdiff
path: root/src/redis-cli.c
diff options
context:
space:
mode:
authoryongman <yming0221@gmail.com>2018-11-23 16:58:55 +0800
committeryongman <yming0221@gmail.com>2018-11-23 16:58:55 +0800
commit2961c891616a6d60e28aea748c3c2422cb4f90b6 (patch)
tree6b391b5e3a97961b8e58d22e53834fb21da852ec /src/redis-cli.c
parent0c12ebf6e79b3665e0dfa313f27492446b477758 (diff)
downloadredis-2961c891616a6d60e28aea748c3c2422cb4f90b6.tar.gz
Fix choose a random master node for slot assignment
Diffstat (limited to 'src/redis-cli.c')
-rw-r--r--src/redis-cli.c34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c
index 28c842fdb..a7a4d685f 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -3505,6 +3505,34 @@ static clusterManagerNode *clusterManagerNodeWithLeastReplicas() {
return node;
}
+/* This fucntion returns a random master node, return NULL if none */
+
+static clusterManagerNode *clusterManagerNodeMasterRandom() {
+ int master_count = 0;
+ int idx;
+ listIter li;
+ listNode *ln;
+ listRewind(cluster_manager.nodes, &li);
+ while ((ln = listNext(&li)) != NULL) {
+ clusterManagerNode *n = ln->value;
+ if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
+ master_count++;
+ }
+
+ srand(time(NULL));
+ idx = rand() % master_count;
+ listRewind(cluster_manager.nodes, &li);
+ while ((ln = listNext(&li)) != NULL) {
+ clusterManagerNode *n = ln->value;
+ if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
+ if (!idx--) {
+ return n;
+ }
+ }
+ /* Can not be reached */
+ return NULL;
+}
+
static int clusterManagerFixSlotsCoverage(char *all_slots) {
int i, fixed = 0;
list *none = NULL, *single = NULL, *multi = NULL;
@@ -3577,16 +3605,12 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
"across the cluster:\n");
clusterManagerPrintSlotsList(none);
if (confirmWithYes("Fix these slots by covering with a random node?")){
- srand(time(NULL));
listIter li;
listNode *ln;
listRewind(none, &li);
while ((ln = listNext(&li)) != NULL) {
sds slot = ln->value;
- long idx = (long) (rand() % listLength(cluster_manager.nodes));
- listNode *node_n = listIndex(cluster_manager.nodes, idx);
- assert(node_n != NULL);
- clusterManagerNode *n = node_n->value;
+ clusterManagerNode *n = clusterManagerNodeMasterRandom();
clusterManagerLogInfo(">>> Covering slot %s with %s:%d\n",
slot, n->ip, n->port);
/* Ensure the slot is not already assigned. */