Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
2961c891
Commit
2961c891
authored
Nov 23, 2018
by
yongman
Browse files
Fix choose a random master node for slot assignment
parent
0c12ebf6
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/redis-cli.c
View file @
2961c891
...
@@ -3505,6 +3505,34 @@ static clusterManagerNode *clusterManagerNodeWithLeastReplicas() {
...
@@ -3505,6 +3505,34 @@ static clusterManagerNode *clusterManagerNodeWithLeastReplicas() {
return
node
;
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
)
{
static
int
clusterManagerFixSlotsCoverage
(
char
*
all_slots
)
{
int
i
,
fixed
=
0
;
int
i
,
fixed
=
0
;
list
*
none
=
NULL
,
*
single
=
NULL
,
*
multi
=
NULL
;
list
*
none
=
NULL
,
*
single
=
NULL
,
*
multi
=
NULL
;
...
@@ -3577,16 +3605,12 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
...
@@ -3577,16 +3605,12 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
"across the cluster:
\n
"
);
"across the cluster:
\n
"
);
clusterManagerPrintSlotsList
(
none
);
clusterManagerPrintSlotsList
(
none
);
if
(
confirmWithYes
(
"Fix these slots by covering with a random node?"
)){
if
(
confirmWithYes
(
"Fix these slots by covering with a random node?"
)){
srand
(
time
(
NULL
));
listIter
li
;
listIter
li
;
listNode
*
ln
;
listNode
*
ln
;
listRewind
(
none
,
&
li
);
listRewind
(
none
,
&
li
);
while
((
ln
=
listNext
(
&
li
))
!=
NULL
)
{
while
((
ln
=
listNext
(
&
li
))
!=
NULL
)
{
sds
slot
=
ln
->
value
;
sds
slot
=
ln
->
value
;
long
idx
=
(
long
)
(
rand
()
%
listLength
(
cluster_manager
.
nodes
));
clusterManagerNode
*
n
=
clusterManagerNodeMasterRandom
();
listNode
*
node_n
=
listIndex
(
cluster_manager
.
nodes
,
idx
);
assert
(
node_n
!=
NULL
);
clusterManagerNode
*
n
=
node_n
->
value
;
clusterManagerLogInfo
(
">>> Covering slot %s with %s:%d
\n
"
,
clusterManagerLogInfo
(
">>> Covering slot %s with %s:%d
\n
"
,
slot
,
n
->
ip
,
n
->
port
);
slot
,
n
->
ip
,
n
->
port
);
/* Ensure the slot is not already assigned. */
/* Ensure the slot is not already assigned. */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment