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
c7c7cfbd
Commit
c7c7cfbd
authored
Mar 30, 2011
by
antirez
Browse files
cluster configuration saving
parent
e6f0a7b2
Changes
1
Show whitespace changes
Inline
Side-by-side
src/cluster.c
View file @
c7c7cfbd
#include "redis.h"
#include "redis.h"
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
void
clusterAcceptHandler
(
aeEventLoop
*
el
,
int
fd
,
void
*
privdata
,
int
mask
);
void
clusterAcceptHandler
(
aeEventLoop
*
el
,
int
fd
,
void
*
privdata
,
int
mask
);
void
clusterReadHandler
(
aeEventLoop
*
el
,
int
fd
,
void
*
privdata
,
int
mask
);
void
clusterReadHandler
(
aeEventLoop
*
el
,
int
fd
,
void
*
privdata
,
int
mask
);
...
@@ -8,6 +10,7 @@ void clusterSendPing(clusterLink *link, int type);
...
@@ -8,6 +10,7 @@ void clusterSendPing(clusterLink *link, int type);
void
clusterSendFail
(
char
*
nodename
);
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
);
/* -----------------------------------------------------------------------------
/* -----------------------------------------------------------------------------
* Initialization
* Initialization
...
@@ -32,6 +35,7 @@ void clusterGetRandomName(char *p) {
...
@@ -32,6 +35,7 @@ void clusterGetRandomName(char *p) {
int
clusterLoadConfig
(
char
*
filename
)
{
int
clusterLoadConfig
(
char
*
filename
)
{
FILE
*
fp
=
fopen
(
filename
,
"r"
);
FILE
*
fp
=
fopen
(
filename
,
"r"
);
return
REDIS_ERR
;
if
(
fp
==
NULL
)
return
REDIS_ERR
;
if
(
fp
==
NULL
)
return
REDIS_ERR
;
fclose
(
fp
);
fclose
(
fp
);
...
@@ -45,6 +49,25 @@ fmterr:
...
@@ -45,6 +49,25 @@ fmterr:
exit
(
1
);
exit
(
1
);
}
}
/* Cluster node configuration is exactly the same as CLUSTER NODES output.
*
* This function writes the node config and returns 0, on error -1
* is returned. */
int
clusterSaveConfig
(
char
*
filename
)
{
sds
ci
=
clusterGenNodesDescription
();
int
fd
;
if
((
fd
=
open
(
filename
,
O_WRONLY
|
O_CREAT
,
0644
))
==
-
1
)
goto
err
;
if
(
write
(
fd
,
ci
,
sdslen
(
ci
))
!=
(
ssize_t
)
sdslen
(
ci
))
goto
err
;
close
(
fd
);
sdsfree
(
ci
);
return
0
;
err:
sdsfree
(
ci
);
return
-
1
;
}
void
clusterInit
(
void
)
{
void
clusterInit
(
void
)
{
server
.
cluster
.
myself
=
createClusterNode
(
NULL
,
REDIS_NODE_MYSELF
);
server
.
cluster
.
myself
=
createClusterNode
(
NULL
,
REDIS_NODE_MYSELF
);
server
.
cluster
.
state
=
REDIS_CLUSTER_FAIL
;
server
.
cluster
.
state
=
REDIS_CLUSTER_FAIL
;
...
@@ -61,6 +84,10 @@ void clusterInit(void) {
...
@@ -61,6 +84,10 @@ void clusterInit(void) {
* by the createClusterNode() function. */
* by the createClusterNode() function. */
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
);
if
(
clusterSaveConfig
(
"cluster.conf"
)
==
-
1
)
{
redisLog
(
REDIS_WARNING
,
"Fatal: can't update cluster config file."
);
exit
(
1
);
}
}
}
clusterAddNode
(
server
.
cluster
.
myself
);
clusterAddNode
(
server
.
cluster
.
myself
);
/* We need a listening TCP port for our cluster messaging needs */
/* We need a listening TCP port for our cluster messaging needs */
...
@@ -874,41 +901,10 @@ void clusterUpdateState(void) {
...
@@ -874,41 +901,10 @@ void clusterUpdateState(void) {
* CLUSTER command
* CLUSTER command
* -------------------------------------------------------------------------- */
* -------------------------------------------------------------------------- */
void
clusterCommand
(
redisClient
*
c
)
{
sds
clusterGenNodesDescription
(
void
)
{
if
(
server
.
cluster_enabled
==
0
)
{
addReplyError
(
c
,
"This instance has cluster support disabled"
);
return
;
}
if
(
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"meet"
)
&&
c
->
argc
==
4
)
{
clusterNode
*
n
;
struct
sockaddr_in
sa
;
long
port
;
/* Perform sanity checks on IP/port */
if
(
inet_aton
(
c
->
argv
[
2
]
->
ptr
,
&
sa
.
sin_addr
)
==
0
)
{
addReplyError
(
c
,
"Invalid IP address in MEET"
);
return
;
}
if
(
getLongFromObjectOrReply
(
c
,
c
->
argv
[
3
],
&
port
,
NULL
)
!=
REDIS_OK
||
port
<
0
||
port
>
(
65535
-
REDIS_CLUSTER_PORT_INCR
))
{
addReplyError
(
c
,
"Invalid TCP port specified"
);
return
;
}
/* Finally add the node to the cluster with a random name, this
* will get fixed in the first handshake (ping/pong). */
n
=
createClusterNode
(
NULL
,
REDIS_NODE_HANDSHAKE
|
REDIS_NODE_MEET
);
strncpy
(
n
->
ip
,
inet_ntoa
(
sa
.
sin_addr
),
sizeof
(
n
->
ip
));
n
->
port
=
port
;
clusterAddNode
(
n
);
addReply
(
c
,
shared
.
ok
);
}
else
if
(
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"nodes"
)
&&
c
->
argc
==
2
)
{
sds
ci
=
sdsempty
();
sds
ci
=
sdsempty
();
dictIterator
*
di
;
dictIterator
*
di
;
dictEntry
*
de
;
dictEntry
*
de
;
robj
*
o
;
di
=
dictGetIterator
(
server
.
cluster
.
nodes
);
di
=
dictGetIterator
(
server
.
cluster
.
nodes
);
while
((
de
=
dictNext
(
di
))
!=
NULL
)
{
while
((
de
=
dictNext
(
di
))
!=
NULL
)
{
...
@@ -944,6 +940,43 @@ void clusterCommand(redisClient *c) {
...
@@ -944,6 +940,43 @@ void clusterCommand(redisClient *c) {
node
->
link
?
"connected"
:
"disconnected"
);
node
->
link
?
"connected"
:
"disconnected"
);
}
}
dictReleaseIterator
(
di
);
dictReleaseIterator
(
di
);
return
ci
;
}
void
clusterCommand
(
redisClient
*
c
)
{
if
(
server
.
cluster_enabled
==
0
)
{
addReplyError
(
c
,
"This instance has cluster support disabled"
);
return
;
}
if
(
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"meet"
)
&&
c
->
argc
==
4
)
{
clusterNode
*
n
;
struct
sockaddr_in
sa
;
long
port
;
/* Perform sanity checks on IP/port */
if
(
inet_aton
(
c
->
argv
[
2
]
->
ptr
,
&
sa
.
sin_addr
)
==
0
)
{
addReplyError
(
c
,
"Invalid IP address in MEET"
);
return
;
}
if
(
getLongFromObjectOrReply
(
c
,
c
->
argv
[
3
],
&
port
,
NULL
)
!=
REDIS_OK
||
port
<
0
||
port
>
(
65535
-
REDIS_CLUSTER_PORT_INCR
))
{
addReplyError
(
c
,
"Invalid TCP port specified"
);
return
;
}
/* Finally add the node to the cluster with a random name, this
* will get fixed in the first handshake (ping/pong). */
n
=
createClusterNode
(
NULL
,
REDIS_NODE_HANDSHAKE
|
REDIS_NODE_MEET
);
strncpy
(
n
->
ip
,
inet_ntoa
(
sa
.
sin_addr
),
sizeof
(
n
->
ip
));
n
->
port
=
port
;
clusterAddNode
(
n
);
addReply
(
c
,
shared
.
ok
);
}
else
if
(
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"nodes"
)
&&
c
->
argc
==
2
)
{
robj
*
o
;
sds
ci
=
clusterGenNodesDescription
();
o
=
createObject
(
REDIS_STRING
,
ci
);
o
=
createObject
(
REDIS_STRING
,
ci
);
addReplyBulk
(
c
,
o
);
addReplyBulk
(
c
,
o
);
decrRefCount
(
o
);
decrRefCount
(
o
);
...
...
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