cluster configuration saving

This commit is contained in:
antirez 2011-03-30 14:58:19 +02:00
parent e6f0a7b237
commit c7c7cfbddc

View File

@ -1,6 +1,8 @@
#include "redis.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
void clusterAcceptHandler(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);
void clusterSendFail(char *nodename);
void clusterUpdateState(void);
int clusterNodeGetSlotBit(clusterNode *n, int slot);
sds clusterGenNodesDescription(void);
/* -----------------------------------------------------------------------------
* Initialization
@ -32,6 +35,7 @@ void clusterGetRandomName(char *p) {
int clusterLoadConfig(char *filename) {
FILE *fp = fopen(filename,"r");
return REDIS_ERR;
if (fp == NULL) return REDIS_ERR;
fclose(fp);
@ -45,6 +49,25 @@ fmterr:
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) {
server.cluster.myself = createClusterNode(NULL,REDIS_NODE_MYSELF);
server.cluster.state = REDIS_CLUSTER_FAIL;
@ -61,6 +84,10 @@ void clusterInit(void) {
* by the createClusterNode() function. */
redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s",
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);
/* We need a listening TCP port for our cluster messaging needs */
@ -874,41 +901,10 @@ void clusterUpdateState(void) {
* CLUSTER command
* -------------------------------------------------------------------------- */
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) {
sds clusterGenNodesDescription(void) {
sds ci = sdsempty();
dictIterator *di;
dictEntry *de;
robj *o;
di = dictGetIterator(server.cluster.nodes);
while((de = dictNext(di)) != NULL) {
@ -944,6 +940,43 @@ void clusterCommand(redisClient *c) {
node->link ? "connected" : "disconnected");
}
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);
addReplyBulk(c,o);
decrRefCount(o);