Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
92690d29
Commit
92690d29
authored
Apr 07, 2011
by
antirez
Browse files
first version of cluster config loading code
parent
726a39c1
Changes
1
Show whitespace changes
Inline
Side-by-side
src/cluster.c
View file @
92690d29
...
@@ -11,6 +11,8 @@ void clusterSendFail(char *nodename);
...
@@ -11,6 +11,8 @@ void clusterSendFail(char *nodename);
void
clusterUpdateState
(
void
);
void
clusterUpdateState
(
void
);
int
clusterNodeGetSlotBit
(
clusterNode
*
n
,
int
slot
);
int
clusterNodeGetSlotBit
(
clusterNode
*
n
,
int
slot
);
sds
clusterGenNodesDescription
(
void
);
sds
clusterGenNodesDescription
(
void
);
clusterNode
*
clusterLookupNode
(
char
*
name
);
int
clusterNodeAddSlave
(
clusterNode
*
master
,
clusterNode
*
slave
);
/* -----------------------------------------------------------------------------
/* -----------------------------------------------------------------------------
* Initialization
* Initialization
...
@@ -35,7 +37,7 @@ void clusterGetRandomName(char *p) {
...
@@ -35,7 +37,7 @@ void clusterGetRandomName(char *p) {
int
clusterLoadConfig
(
char
*
filename
)
{
int
clusterLoadConfig
(
char
*
filename
)
{
FILE
*
fp
=
fopen
(
filename
,
"r"
);
FILE
*
fp
=
fopen
(
filename
,
"r"
);
char
*
line
;
char
*
line
;
int
maxline
;
int
maxline
,
j
;
if
(
fp
==
NULL
)
return
REDIS_ERR
;
if
(
fp
==
NULL
)
return
REDIS_ERR
;
...
@@ -48,8 +50,73 @@ int clusterLoadConfig(char *filename) {
...
@@ -48,8 +50,73 @@ int clusterLoadConfig(char *filename) {
while
(
fgets
(
line
,
maxline
,
fp
)
!=
NULL
)
{
while
(
fgets
(
line
,
maxline
,
fp
)
!=
NULL
)
{
int
argc
;
int
argc
;
sds
*
argv
=
sdssplitargs
(
line
,
&
argc
);
sds
*
argv
=
sdssplitargs
(
line
,
&
argc
);
clusterNode
*
n
,
*
master
;
char
*
p
,
*
s
;
printf
(
"Node: %s
\n
"
,
argv
[
0
]);
/* Create this node if it does not exist */
n
=
clusterLookupNode
(
argv
[
0
]);
if
(
!
n
)
{
n
=
createClusterNode
(
argv
[
0
],
0
);
clusterAddNode
(
n
);
}
/* Address and port */
if
((
p
=
strchr
(
argv
[
1
],
':'
))
==
NULL
)
goto
fmterr
;
*
p
=
'\0'
;
memcpy
(
n
->
ip
,
argv
[
1
],
strlen
(
argv
[
1
])
+
1
);
n
->
port
=
atoi
(
p
+
1
);
/* Parse flags */
p
=
s
=
argv
[
2
];
while
(
p
)
{
p
=
strchr
(
s
,
','
);
if
(
p
)
*
p
=
'\0'
;
if
(
!
strcasecmp
(
s
,
"myself"
))
{
redisAssert
(
server
.
cluster
.
myself
==
NULL
);
server
.
cluster
.
myself
=
n
;
n
->
flags
|=
REDIS_NODE_MYSELF
;
}
else
if
(
!
strcasecmp
(
s
,
"master"
))
{
n
->
flags
|=
REDIS_NODE_MASTER
;
}
else
if
(
!
strcasecmp
(
s
,
"slave"
))
{
n
->
flags
|=
REDIS_NODE_SLAVE
;
}
else
if
(
!
strcasecmp
(
s
,
"fail?"
))
{
n
->
flags
|=
REDIS_NODE_PFAIL
;
}
else
if
(
!
strcasecmp
(
s
,
"fail"
))
{
n
->
flags
|=
REDIS_NODE_FAIL
;
}
else
if
(
!
strcasecmp
(
s
,
"handshake"
))
{
n
->
flags
|=
REDIS_NODE_HANDSHAKE
;
}
else
if
(
!
strcasecmp
(
s
,
"noaddr"
))
{
n
->
flags
|=
REDIS_NODE_NOADDR
;
}
else
{
redisPanic
(
"Unknown flag in redis cluster config file"
);
}
if
(
p
)
s
=
p
+
1
;
}
/* Get master if any. Set the master and populate master's
* slave list. */
if
(
argv
[
3
][
0
]
!=
'-'
)
{
master
=
clusterLookupNode
(
argv
[
3
]);
if
(
!
master
)
{
master
=
createClusterNode
(
argv
[
3
],
0
);
clusterAddNode
(
master
);
}
n
->
slaveof
=
master
;
clusterNodeAddSlave
(
master
,
n
);
}
/* Populate hash slots served by this instance. */
for
(
j
=
7
;
j
<
argc
;
j
++
)
{
int
start
,
stop
;
if
((
p
=
strchr
(
argv
[
j
],
'-'
))
!=
NULL
)
{
*
p
=
'\0'
;
start
=
atoi
(
argv
[
j
]);
stop
=
atoi
(
p
+
1
);
}
else
{
start
=
stop
=
atoi
(
argv
[
j
]);
}
while
(
start
<=
stop
)
clusterAddSlot
(
n
,
start
++
);
}
sdssplitargs_free
(
argv
,
argc
);
sdssplitargs_free
(
argv
,
argc
);
}
}
...
@@ -57,9 +124,7 @@ int clusterLoadConfig(char *filename) {
...
@@ -57,9 +124,7 @@ int clusterLoadConfig(char *filename) {
fclose
(
fp
);
fclose
(
fp
);
/* Config sanity check */
/* Config sanity check */
/* TODO: check that myself is set. */
redisAssert
(
server
.
cluster
.
myself
!=
NULL
);
return
REDIS_ERR
;
redisLog
(
REDIS_NOTICE
,
"Node configuration loaded, I'm %.40s"
,
redisLog
(
REDIS_NOTICE
,
"Node configuration loaded, I'm %.40s"
,
server
.
cluster
.
myself
->
name
);
server
.
cluster
.
myself
->
name
);
return
REDIS_OK
;
return
REDIS_OK
;
...
@@ -100,7 +165,7 @@ void clusterSaveConfigOrDie(void) {
...
@@ -100,7 +165,7 @@ void clusterSaveConfigOrDie(void) {
void
clusterInit
(
void
)
{
void
clusterInit
(
void
)
{
int
saveconf
=
0
;
int
saveconf
=
0
;
server
.
cluster
.
myself
=
createClusterNode
(
NULL
,
REDIS_NODE_MYSELF
)
;
server
.
cluster
.
myself
=
NULL
;
server
.
cluster
.
state
=
REDIS_CLUSTER_FAIL
;
server
.
cluster
.
state
=
REDIS_CLUSTER_FAIL
;
server
.
cluster
.
nodes
=
dictCreate
(
&
clusterNodesDictType
,
NULL
);
server
.
cluster
.
nodes
=
dictCreate
(
&
clusterNodesDictType
,
NULL
);
server
.
cluster
.
node_timeout
=
15
;
server
.
cluster
.
node_timeout
=
15
;
...
@@ -113,6 +178,7 @@ void clusterInit(void) {
...
@@ -113,6 +178,7 @@ void clusterInit(void) {
if
(
clusterLoadConfig
(
server
.
cluster
.
configfile
)
==
REDIS_ERR
)
{
if
(
clusterLoadConfig
(
server
.
cluster
.
configfile
)
==
REDIS_ERR
)
{
/* No configuration found. We will just use the random name provided
/* No configuration found. We will just use the random name provided
* by the createClusterNode() function. */
* by the createClusterNode() function. */
server
.
cluster
.
myself
=
createClusterNode
(
NULL
,
REDIS_NODE_MYSELF
);
redisLog
(
REDIS_NOTICE
,
"No cluster configuration found, I'm %.40s"
,
redisLog
(
REDIS_NOTICE
,
"No cluster configuration found, I'm %.40s"
,
server
.
cluster
.
myself
->
name
);
server
.
cluster
.
myself
->
name
);
clusterAddNode
(
server
.
cluster
.
myself
);
clusterAddNode
(
server
.
cluster
.
myself
);
...
...
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