]> git.saurik.com Git - redis.git/commitdiff
centralized cluster config file name. Assigned slots in CLUSTER NODES output and...
authorantirez <antirez@gmail.com>
Wed, 30 Mar 2011 15:41:13 +0000 (17:41 +0200)
committerantirez <antirez@gmail.com>
Wed, 30 Mar 2011 15:41:13 +0000 (17:41 +0200)
src/cluster.c
src/redis.c
src/redis.h

index 1ffe0cdc97b3007fc4cdb98b8dacd34d650fa3f2..225fcc541a860eee544c21f0bf80c5954454997e 100644 (file)
@@ -44,7 +44,7 @@ int clusterLoadConfig(char *filename) {
     return REDIS_OK;
 
 fmterr:
-    redisLog(REDIS_WARNING,"Unrecovarable error: corrupted redis-cluster.conf file.");
+    redisLog(REDIS_WARNING,"Unrecovarable error: corrupted cluster config file.");
     fclose(fp);
     exit(1);
 }
@@ -53,11 +53,12 @@ fmterr:
  *
  * This function writes the node config and returns 0, on error -1
  * is returned. */
-int clusterSaveConfig(char *filename) {
+int clusterSaveConfig(void) {
     sds ci = clusterGenNodesDescription();
     int fd;
     
-    if ((fd = open(filename,O_WRONLY|O_CREAT,0644)) == -1) goto err;
+    if ((fd = open(server.cluster.configfile,O_WRONLY|O_CREAT,0644)) == -1)
+        goto err;
     if (write(fd,ci,sdslen(ci)) != (ssize_t)sdslen(ci)) goto err;
     close(fd);
     sdsfree(ci);
@@ -68,6 +69,13 @@ err:
     return -1;
 }
 
+void clusterSaveConfigOrDie(void) {
+    if (clusterSaveConfig() == -1) {
+        redisLog(REDIS_WARNING,"Fatal: can't update cluster config file.");
+        exit(1);
+    }
+}
+
 void clusterInit(void) {
     int saveconf = 0;
 
@@ -81,7 +89,7 @@ void clusterInit(void) {
         sizeof(server.cluster.importing_slots_from));
     memset(server.cluster.slots,0,
         sizeof(server.cluster.slots));
-    if (clusterLoadConfig("redis-cluster.conf") == REDIS_ERR) {
+    if (clusterLoadConfig(server.cluster.configfile) == REDIS_ERR) {
         /* No configuration found. We will just use the random name provided
          * by the createClusterNode() function. */
         redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s",
@@ -89,12 +97,7 @@ void clusterInit(void) {
         saveconf = 1;
     }
     clusterAddNode(server.cluster.myself);
-    if (saveconf) {
-        if (clusterSaveConfig("redis-cluster.conf") == -1) {
-            redisLog(REDIS_WARNING,"Fatal: can't update cluster config file.");
-            exit(1);
-        }
-    }
+    if (saveconf) clusterSaveConfigOrDie();
     /* We need a listening TCP port for our cluster messaging needs */
     server.cfd = anetTcpServer(server.neterr,
             server.port+REDIS_CLUSTER_PORT_INCR, server.bindaddr);
@@ -910,6 +913,7 @@ sds clusterGenNodesDescription(void) {
     sds ci = sdsempty();
     dictIterator *di;
     dictEntry *de;
+    int j, start;
 
     di = dictGetIterator(server.cluster.nodes);
     while((de = dictNext(di)) != NULL) {
@@ -939,11 +943,32 @@ sds clusterGenNodesDescription(void) {
             ci = sdscatprintf(ci,"- ");
 
         /* Latency from the POV of this node, link status */
-        ci = sdscatprintf(ci,"%ld %ld %s\n",
+        ci = sdscatprintf(ci,"%ld %ld %s",
             (long) node->ping_sent,
             (long) node->pong_received,
             node->link ? "connected" : "disconnected");
+
+        /* Slots served by this instance */
+        start = -1;
+        for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) {
+            int bit;
+
+            if ((bit = clusterNodeGetSlotBit(node,j)) != 0) {
+                if (start == -1) start = j;
+            }
+            if (start != -1 && (!bit || j == REDIS_CLUSTER_SLOTS-1)) {
+                if (j == REDIS_CLUSTER_SLOTS-1) j++;
+
+                if (start == j-1) {
+                    ci = sdscatprintf(ci," %d",start);
+                } else {
+                    ci = sdscatprintf(ci," %d-%d",start,j-1);
+                }
+                start = -1;
+            }
+        }
     }
+    ci = sdscatlen(ci,"\n",1);
     dictReleaseIterator(di);
     return ci;
 }
index 25205752e1dfedef745ae9fbf8b31d751a37dcda..8b8c49eb7e5abf1def75bb1762399a36c266f9b6 100644 (file)
@@ -841,6 +841,7 @@ void initServerConfig() {
     server.shutdown_asap = 0;
     server.cache_flush_delay = 0;
     server.cluster_enabled = 0;
+    server.cluster.configfile = zstrdup("nodes.conf");
 
     updateLRUClock();
     resetServerSaveParams();
index 0ddd4730f625d42e9c4884de72d3c8d901a7cafb..5b403798bf037a2f3fd2ec06692a23ce139d7ff1 100644 (file)
@@ -411,6 +411,7 @@ struct clusterNode {
 typedef struct clusterNode clusterNode;
 
 typedef struct {
+    char *configfile;
     clusterNode *myself;  /* This node */
     int state;            /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */
     int node_timeout;