diff options
Diffstat (limited to 'src/cluster.c')
-rw-r--r-- | src/cluster.c | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/src/cluster.c b/src/cluster.c index d2d179e6f..32335bbf9 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -106,7 +106,7 @@ dictType clusterNodesDictType = { }; /* Cluster re-addition blacklist. This maps node IDs to the time - * we can re-add this node. The goal is to avoid readding a removed + * we can re-add this node. The goal is to avoid reading a removed * node for some time. */ dictType clusterNodesBlackListDictType = { dictSdsCaseHash, /* hash function */ @@ -243,10 +243,9 @@ int clusterLoadConfig(char *filename) { if (hostname) { *hostname = '\0'; hostname++; - zfree(n->hostname); - n->hostname = zstrdup(hostname); - } else { - n->hostname = NULL; + n->hostname = sdscpy(n->hostname, hostname); + } else if (sdslen(n->hostname) != 0) { + sdsclear(n->hostname); } /* The plaintext port for client in a TLS cluster (n->pport) is not @@ -570,20 +569,15 @@ void clusterUpdateMyselfIp(void) { /* Update the hostname for the specified node with the provided C string. */ static void updateAnnouncedHostname(clusterNode *node, char *new) { - if (!node->hostname && !new) { - return; - } - /* Previous and new hostname are the same, no need to update. */ - if (new && node->hostname && !strcmp(new, node->hostname)) { + if (new && !strcmp(new, node->hostname)) { return; } - if (node->hostname) zfree(node->hostname); if (new) { - node->hostname = zstrdup(new); - } else { - node->hostname = NULL; + node->hostname = sdscpy(node->hostname, new); + } else if (sdslen(node->hostname) != 0) { + sdsclear(node->hostname); } } @@ -959,7 +953,7 @@ clusterNode *createClusterNode(char *nodename, int flags) { node->link = NULL; node->inbound_link = NULL; memset(node->ip,0,sizeof(node->ip)); - node->hostname = NULL; + node->hostname = sdsempty(); node->port = 0; node->cport = 0; node->pport = 0; @@ -1125,7 +1119,7 @@ void freeClusterNode(clusterNode *n) { nodename = sdsnewlen(n->name, CLUSTER_NAMELEN); serverAssert(dictDelete(server.cluster->nodes,nodename) == DICT_OK); sdsfree(nodename); - zfree(n->hostname); + sdsfree(n->hostname); /* Release links and associated data structures. */ if (n->link) freeClusterLink(n->link); @@ -1947,9 +1941,9 @@ static clusterMsgPingExt *getNextPingExt(clusterMsgPingExt *ext) { * will be 8 byte padded. */ int getHostnamePingExtSize() { /* If hostname is not set, we don't send this extension */ - if (!myself->hostname) return 0; + if (sdslen(myself->hostname) == 0) return 0; - int totlen = sizeof(clusterMsgPingExt) + EIGHT_BYTE_ALIGN(strlen(myself->hostname) + 1); + int totlen = sizeof(clusterMsgPingExt) + EIGHT_BYTE_ALIGN(sdslen(myself->hostname) + 1); return totlen; } @@ -1958,19 +1952,18 @@ int getHostnamePingExtSize() { * will return the amount of bytes written. */ int writeHostnamePingExt(clusterMsgPingExt **cursor) { /* If hostname is not set, we don't send this extension */ - if (!myself->hostname) return 0; + if (sdslen(myself->hostname) == 0) return 0; /* Add the hostname information at the extension cursor */ clusterMsgPingExtHostname *ext = &(*cursor)->ext[0].hostname; - size_t hostname_len = strlen(myself->hostname); - memcpy(ext->hostname, myself->hostname, hostname_len); + memcpy(ext->hostname, myself->hostname, sdslen(myself->hostname)); uint32_t extension_size = getHostnamePingExtSize(); /* Move the write cursor */ (*cursor)->type = CLUSTERMSG_EXT_TYPE_HOSTNAME; (*cursor)->length = htonl(extension_size); /* Make sure the string is NULL terminated by adding 1 */ - *cursor = (clusterMsgPingExt *) (ext->hostname + EIGHT_BYTE_ALIGN(strlen(myself->hostname) + 1)); + *cursor = (clusterMsgPingExt *) (ext->hostname + EIGHT_BYTE_ALIGN(sdslen(myself->hostname) + 1)); return extension_size; } @@ -2975,7 +2968,7 @@ void clusterSendPing(clusterLink *link, int type) { /* Set the initial extension position */ clusterMsgPingExt *cursor = getInitialPingExt(hdr, gossipcount); /* Add in the extensions */ - if (myself->hostname) { + if (sdslen(myself->hostname) != 0) { hdr->mflags[0] |= CLUSTERMSG_FLAG0_EXT_DATA; totlen += writeHostnamePingExt(&cursor); extensions++; @@ -3959,7 +3952,8 @@ void clusterCron(void) { iteration++; /* Number of times this function was called so far. */ - updateAnnouncedHostname(myself, server.cluster_announce_hostname); + clusterUpdateMyselfHostname(); + /* The handshake timeout is the time after which a handshake node that was * not turned into a normal node is removed from the nodes. Usually it is * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use @@ -4578,7 +4572,7 @@ sds clusterGenNodeDescription(clusterNode *node, int use_pport) { /* Node coordinates */ ci = sdscatlen(sdsempty(),node->name,CLUSTER_NAMELEN); - if (node->hostname) { + if (sdslen(node->hostname) != 0) { ci = sdscatfmt(ci," %s:%i@%i,%s ", node->ip, port, @@ -4804,7 +4798,7 @@ void addReplyClusterLinksDescription(client *c) { const char *getPreferredEndpoint(clusterNode *n) { switch(server.cluster_preferred_endpoint_type) { case CLUSTER_ENDPOINT_TYPE_IP: return n->ip; - case CLUSTER_ENDPOINT_TYPE_HOSTNAME: return n->hostname ? n->hostname : "?"; + case CLUSTER_ENDPOINT_TYPE_HOSTNAME: return (sdslen(n->hostname) != 0) ? n->hostname : "?"; case CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT: return ""; } return "unknown"; @@ -4898,7 +4892,7 @@ void addNodeToNodeReply(client *c, clusterNode *node) { if (server.cluster_preferred_endpoint_type == CLUSTER_ENDPOINT_TYPE_IP) { addReplyBulkCString(c, node->ip); } else if (server.cluster_preferred_endpoint_type == CLUSTER_ENDPOINT_TYPE_HOSTNAME) { - addReplyBulkCString(c, node->hostname ? node->hostname : "?"); + addReplyBulkCString(c, sdslen(node->hostname) != 0 ? node->hostname : "?"); } else if (server.cluster_preferred_endpoint_type == CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT) { addReplyNull(c); } else { @@ -4921,7 +4915,7 @@ void addNodeToNodeReply(client *c, clusterNode *node) { length++; } if (server.cluster_preferred_endpoint_type != CLUSTER_ENDPOINT_TYPE_HOSTNAME - && node->hostname) + && sdslen(node->hostname) != 0) { addReplyBulkCString(c, "hostname"); addReplyBulkCString(c, node->hostname); @@ -5032,7 +5026,7 @@ void clusterCommand(client *c) { " Reset current node (default: soft).", "SET-CONFIG-EPOCH <epoch>", " Set config epoch of current node.", -"SETSLOT <slot> (IMPORTING|MIGRATING|STABLE|NODE <node-id>)", +"SETSLOT <slot> (IMPORTING <node-id>|MIGRATING <node-id>|STABLE|NODE <node-id>)", " Set slot state.", "REPLICAS <node-id>", " Return <node-id> replicas.", @@ -5226,6 +5220,10 @@ NULL (char*)c->argv[4]->ptr); return; } + if (nodeIsSlave(n)) { + addReplyError(c,"Target node is not a master"); + return; + } /* If this hash slot was served by 'myself' before to switch * make sure there are no longer local keys for this hash slot. */ if (server.cluster->slots[slot] == myself && n != myself) { @@ -5285,7 +5283,7 @@ NULL addReplySds(c,reply); } else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) { /* CLUSTER INFO */ - char *statestr[] = {"ok","fail","needhelp"}; + char *statestr[] = {"ok","fail"}; int slots_assigned = 0, slots_ok = 0, slots_pfail = 0, slots_fail = 0; uint64_t myepoch; int j; @@ -5703,7 +5701,7 @@ int verifyDumpPayload(unsigned char *p, size_t len, uint16_t *rdbver_ptr) { if (len < 10) return C_ERR; footer = p+(len-10); - /* Verify RDB version */ + /* Set and verify RDB version. */ rdbver = (footer[1] << 8) | footer[0]; if (rdbver_ptr) { *rdbver_ptr = rdbver; |