]>
git.saurik.com Git - redis.git/blob - src/cluster.c
5 void clusterAcceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
6 void clusterReadHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
7 void clusterSendPing(clusterLink
*link
, int type
);
8 void clusterSendFail(char *nodename
);
9 void clusterUpdateState(void);
10 int clusterNodeGetSlotBit(clusterNode
*n
, int slot
);
12 /* -----------------------------------------------------------------------------
14 * -------------------------------------------------------------------------- */
16 void clusterGetRandomName(char *p
) {
17 FILE *fp
= fopen("/dev/urandom","r");
18 char *charset
= "0123456789abcdef";
22 redisLog(REDIS_WARNING
,
23 "Unrecovarable error: can't open /dev/urandom:%s" ,strerror(errno
));
26 fread(p
,REDIS_CLUSTER_NAMELEN
,1,fp
);
27 for (j
= 0; j
< REDIS_CLUSTER_NAMELEN
; j
++)
28 p
[j
] = charset
[p
[j
] & 0x0F];
32 int clusterLoadConfig(char *filename
) {
33 FILE *fp
= fopen(filename
,"r");
35 if (fp
== NULL
) return REDIS_ERR
;
38 redisLog(REDIS_NOTICE
,"Node configuration loaded, I'm %.40s",
39 server
.cluster
.myself
->name
);
43 redisLog(REDIS_WARNING
,"Unrecovarable error: corrupted cluster.conf file.");
48 void clusterInit(void) {
49 server
.cluster
.myself
= createClusterNode(NULL
,REDIS_NODE_MYSELF
);
50 server
.cluster
.state
= REDIS_CLUSTER_FAIL
;
51 server
.cluster
.nodes
= dictCreate(&clusterNodesDictType
,NULL
);
52 server
.cluster
.node_timeout
= 15;
53 memset(server
.cluster
.migrating_slots_to
,0,
54 sizeof(server
.cluster
.migrating_slots_to
));
55 memset(server
.cluster
.importing_slots_from
,0,
56 sizeof(server
.cluster
.importing_slots_from
));
57 memset(server
.cluster
.slots
,0,
58 sizeof(server
.cluster
.slots
));
59 if (clusterLoadConfig("cluster.conf") == REDIS_ERR
) {
60 /* No configuration found. We will just use the random name provided
61 * by the createClusterNode() function. */
62 redisLog(REDIS_NOTICE
,"No cluster configuration found, I'm %.40s",
63 server
.cluster
.myself
->name
);
65 clusterAddNode(server
.cluster
.myself
);
66 /* We need a listening TCP port for our cluster messaging needs */
67 server
.cfd
= anetTcpServer(server
.neterr
,
68 server
.port
+REDIS_CLUSTER_PORT_INCR
, server
.bindaddr
);
69 if (server
.cfd
== -1) {
70 redisLog(REDIS_WARNING
, "Opening cluster TCP port: %s", server
.neterr
);
73 if (aeCreateFileEvent(server
.el
, server
.cfd
, AE_READABLE
,
74 clusterAcceptHandler
, NULL
) == AE_ERR
) oom("creating file event");
77 /* -----------------------------------------------------------------------------
78 * CLUSTER communication link
79 * -------------------------------------------------------------------------- */
81 clusterLink
*createClusterLink(clusterNode
*node
) {
82 clusterLink
*link
= zmalloc(sizeof(*link
));
83 link
->sndbuf
= sdsempty();
84 link
->rcvbuf
= sdsempty();
90 /* Free a cluster link, but does not free the associated node of course.
91 * Just this function will make sure that the original node associated
92 * with this link will have the 'link' field set to NULL. */
93 void freeClusterLink(clusterLink
*link
) {
95 aeDeleteFileEvent(server
.el
, link
->fd
, AE_WRITABLE
);
96 aeDeleteFileEvent(server
.el
, link
->fd
, AE_READABLE
);
98 sdsfree(link
->sndbuf
);
99 sdsfree(link
->rcvbuf
);
101 link
->node
->link
= NULL
;
106 void clusterAcceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
112 REDIS_NOTUSED(privdata
);
114 cfd
= anetTcpAccept(server
.neterr
, fd
, cip
, &cport
);
116 redisLog(REDIS_VERBOSE
,"Accepting cluster node: %s", server
.neterr
);
119 redisLog(REDIS_VERBOSE
,"Accepted cluster node %s:%d", cip
, cport
);
120 /* We need to create a temporary node in order to read the incoming
121 * packet in a valid contest. This node will be released once we
122 * read the packet and reply. */
123 link
= createClusterLink(NULL
);
125 aeCreateFileEvent(server
.el
,cfd
,AE_READABLE
,clusterReadHandler
,link
);
128 /* -----------------------------------------------------------------------------
130 * -------------------------------------------------------------------------- */
132 /* We have 4096 hash slots. The hash slot of a given key is obtained
133 * as the least significant 12 bits of the crc16 of the key. */
134 unsigned int keyHashSlot(char *key
, int keylen
) {
135 return crc16(key
,keylen
) & 0x0FFF;
138 /* -----------------------------------------------------------------------------
140 * -------------------------------------------------------------------------- */
142 /* Create a new cluster node, with the specified flags.
143 * If "nodename" is NULL this is considered a first handshake and a random
144 * node name is assigned to this node (it will be fixed later when we'll
145 * receive the first pong).
147 * The node is created and returned to the user, but it is not automatically
148 * added to the nodes hash table. */
149 clusterNode
*createClusterNode(char *nodename
, int flags
) {
150 clusterNode
*node
= zmalloc(sizeof(*node
));
153 memcpy(node
->name
, nodename
, REDIS_CLUSTER_NAMELEN
);
155 clusterGetRandomName(node
->name
);
157 memset(node
->slots
,0,sizeof(node
->slots
));
160 node
->slaveof
= NULL
;
161 node
->ping_sent
= node
->pong_received
= 0;
162 node
->configdigest
= NULL
;
163 node
->configdigest_ts
= 0;
168 int clusterNodeRemoveSlave(clusterNode
*master
, clusterNode
*slave
) {
171 for (j
= 0; j
< master
->numslaves
; j
++) {
172 if (master
->slaves
[j
] == slave
) {
173 memmove(master
->slaves
+j
,master
->slaves
+(j
+1),
174 (master
->numslaves
-1)-j
);
182 int clusterNodeAddSlave(clusterNode
*master
, clusterNode
*slave
) {
185 /* If it's already a slave, don't add it again. */
186 for (j
= 0; j
< master
->numslaves
; j
++)
187 if (master
->slaves
[j
] == slave
) return REDIS_ERR
;
188 master
->slaves
= zrealloc(master
->slaves
,
189 sizeof(clusterNode
*)*(master
->numslaves
+1));
190 master
->slaves
[master
->numslaves
] = slave
;
195 void clusterNodeResetSlaves(clusterNode
*n
) {
200 void freeClusterNode(clusterNode
*n
) {
203 nodename
= sdsnewlen(n
->name
, REDIS_CLUSTER_NAMELEN
);
204 redisAssert(dictDelete(server
.cluster
.nodes
,nodename
) == DICT_OK
);
206 if (n
->slaveof
) clusterNodeRemoveSlave(n
->slaveof
, n
);
207 if (n
->link
) freeClusterLink(n
->link
);
211 /* Add a node to the nodes hash table */
212 int clusterAddNode(clusterNode
*node
) {
215 retval
= dictAdd(server
.cluster
.nodes
,
216 sdsnewlen(node
->name
,REDIS_CLUSTER_NAMELEN
), node
);
217 return (retval
== DICT_OK
) ? REDIS_OK
: REDIS_ERR
;
220 /* Node lookup by name */
221 clusterNode
*clusterLookupNode(char *name
) {
222 sds s
= sdsnewlen(name
, REDIS_CLUSTER_NAMELEN
);
223 struct dictEntry
*de
;
225 de
= dictFind(server
.cluster
.nodes
,s
);
227 if (de
== NULL
) return NULL
;
228 return dictGetEntryVal(de
);
231 /* This is only used after the handshake. When we connect a given IP/PORT
232 * as a result of CLUSTER MEET we don't have the node name yet, so we
233 * pick a random one, and will fix it when we receive the PONG request using
235 void clusterRenameNode(clusterNode
*node
, char *newname
) {
237 sds s
= sdsnewlen(node
->name
, REDIS_CLUSTER_NAMELEN
);
239 redisLog(REDIS_DEBUG
,"Renaming node %.40s into %.40s",
240 node
->name
, newname
);
241 retval
= dictDelete(server
.cluster
.nodes
, s
);
243 redisAssert(retval
== DICT_OK
);
244 memcpy(node
->name
, newname
, REDIS_CLUSTER_NAMELEN
);
245 clusterAddNode(node
);
248 /* -----------------------------------------------------------------------------
249 * CLUSTER messages exchange - PING/PONG and gossip
250 * -------------------------------------------------------------------------- */
252 /* Process the gossip section of PING or PONG packets.
253 * Note that this function assumes that the packet is already sanity-checked
254 * by the caller, not in the content of the gossip section, but in the
256 void clusterProcessGossipSection(clusterMsg
*hdr
, clusterLink
*link
) {
257 uint16_t count
= ntohs(hdr
->count
);
258 clusterMsgDataGossip
*g
= (clusterMsgDataGossip
*) hdr
->data
.ping
.gossip
;
259 clusterNode
*sender
= link
->node
? link
->node
: clusterLookupNode(hdr
->sender
);
263 uint16_t flags
= ntohs(g
->flags
);
266 if (flags
== 0) ci
= sdscat(ci
,"noflags,");
267 if (flags
& REDIS_NODE_MYSELF
) ci
= sdscat(ci
,"myself,");
268 if (flags
& REDIS_NODE_MASTER
) ci
= sdscat(ci
,"master,");
269 if (flags
& REDIS_NODE_SLAVE
) ci
= sdscat(ci
,"slave,");
270 if (flags
& REDIS_NODE_PFAIL
) ci
= sdscat(ci
,"fail?,");
271 if (flags
& REDIS_NODE_FAIL
) ci
= sdscat(ci
,"fail,");
272 if (flags
& REDIS_NODE_HANDSHAKE
) ci
= sdscat(ci
,"handshake,");
273 if (flags
& REDIS_NODE_NOADDR
) ci
= sdscat(ci
,"noaddr,");
274 if (ci
[sdslen(ci
)-1] == ',') ci
[sdslen(ci
)-1] = ' ';
276 redisLog(REDIS_DEBUG
,"GOSSIP %.40s %s:%d %s",
283 /* Update our state accordingly to the gossip sections */
284 node
= clusterLookupNode(g
->nodename
);
286 /* We already know this node. Let's start updating the last
287 * time PONG figure if it is newer than our figure.
288 * Note that it's not a problem if we have a PING already
289 * in progress against this node. */
290 if (node
->pong_received
< ntohl(g
->pong_received
)) {
291 redisLog(REDIS_DEBUG
,"Node pong_received updated by gossip");
292 node
->pong_received
= ntohl(g
->pong_received
);
294 /* Mark this node as FAILED if we think it is possibly failing
295 * and another node also thinks it's failing. */
296 if (node
->flags
& REDIS_NODE_PFAIL
&&
297 (flags
& (REDIS_NODE_FAIL
|REDIS_NODE_PFAIL
)))
299 redisLog(REDIS_NOTICE
,"Received a PFAIL acknowledge from node %.40s, marking node %.40s as FAIL!", hdr
->sender
, node
->name
);
300 node
->flags
&= ~REDIS_NODE_PFAIL
;
301 node
->flags
|= REDIS_NODE_FAIL
;
302 /* Broadcast the failing node name to everybody */
303 clusterSendFail(node
->name
);
304 clusterUpdateState();
307 /* If it's not in NOADDR state and we don't have it, we
308 * start an handshake process against this IP/PORT pairs.
310 * Note that we require that the sender of this gossip message
311 * is a well known node in our cluster, otherwise we risk
312 * joining another cluster. */
313 if (sender
&& !(flags
& REDIS_NODE_NOADDR
)) {
314 clusterNode
*newnode
;
316 redisLog(REDIS_DEBUG
,"Adding the new node");
317 newnode
= createClusterNode(NULL
,REDIS_NODE_HANDSHAKE
);
318 memcpy(newnode
->ip
,g
->ip
,sizeof(g
->ip
));
319 newnode
->port
= ntohs(g
->port
);
320 clusterAddNode(newnode
);
329 /* IP -> string conversion. 'buf' is supposed to at least be 16 bytes. */
330 void nodeIp2String(char *buf
, clusterLink
*link
) {
331 struct sockaddr_in sa
;
332 socklen_t salen
= sizeof(sa
);
334 if (getpeername(link
->fd
, (struct sockaddr
*) &sa
, &salen
) == -1)
335 redisPanic("getpeername() failed.");
336 strncpy(buf
,inet_ntoa(sa
.sin_addr
),sizeof(link
->node
->ip
));
340 /* Update the node address to the IP address that can be extracted
341 * from link->fd, and at the specified port. */
342 void nodeUpdateAddress(clusterNode
*node
, clusterLink
*link
, int port
) {
345 /* When this function is called, there is a packet to process starting
346 * at node->rcvbuf. Releasing the buffer is up to the caller, so this
347 * function should just handle the higher level stuff of processing the
348 * packet, modifying the cluster state if needed.
350 * The function returns 1 if the link is still valid after the packet
351 * was processed, otherwise 0 if the link was freed since the packet
352 * processing lead to some inconsistency error (for instance a PONG
353 * received from the wrong sender ID). */
354 int clusterProcessPacket(clusterLink
*link
) {
355 clusterMsg
*hdr
= (clusterMsg
*) link
->rcvbuf
;
356 uint32_t totlen
= ntohl(hdr
->totlen
);
357 uint16_t type
= ntohs(hdr
->type
);
360 redisLog(REDIS_DEBUG
,"--- packet to process %lu bytes (%lu) ---",
361 (unsigned long) totlen
, sdslen(link
->rcvbuf
));
362 if (totlen
< 8) return 1;
363 if (totlen
> sdslen(link
->rcvbuf
)) return 1;
364 if (type
== CLUSTERMSG_TYPE_PING
|| type
== CLUSTERMSG_TYPE_PONG
||
365 type
== CLUSTERMSG_TYPE_MEET
)
367 uint16_t count
= ntohs(hdr
->count
);
368 uint32_t explen
; /* expected length of this packet */
370 explen
= sizeof(clusterMsg
)-sizeof(union clusterMsgData
);
371 explen
+= (sizeof(clusterMsgDataGossip
)*count
);
372 if (totlen
!= explen
) return 1;
374 if (type
== CLUSTERMSG_TYPE_FAIL
) {
375 uint32_t explen
= sizeof(clusterMsg
)-sizeof(union clusterMsgData
);
377 explen
+= sizeof(clusterMsgDataFail
);
378 if (totlen
!= explen
) return 1;
381 sender
= clusterLookupNode(hdr
->sender
);
382 if (type
== CLUSTERMSG_TYPE_PING
|| type
== CLUSTERMSG_TYPE_MEET
) {
383 redisLog(REDIS_DEBUG
,"Ping packet received: %p", link
->node
);
385 /* Add this node if it is new for us and the msg type is MEET.
386 * In this stage we don't try to add the node with the right
387 * flags, slaveof pointer, and so forth, as this details will be
388 * resolved when we'll receive PONGs from the server. */
389 if (!sender
&& type
== CLUSTERMSG_TYPE_MEET
) {
392 node
= createClusterNode(NULL
,REDIS_NODE_HANDSHAKE
);
393 nodeIp2String(node
->ip
,link
);
394 node
->port
= ntohs(hdr
->port
);
395 clusterAddNode(node
);
398 /* Get info from the gossip section */
399 clusterProcessGossipSection(hdr
,link
);
401 /* Anyway reply with a PONG */
402 clusterSendPing(link
,CLUSTERMSG_TYPE_PONG
);
403 } else if (type
== CLUSTERMSG_TYPE_PONG
) {
406 redisLog(REDIS_DEBUG
,"Pong packet received: %p", link
->node
);
408 if (link
->node
->flags
& REDIS_NODE_HANDSHAKE
) {
409 /* If we already have this node, try to change the
410 * IP/port of the node with the new one. */
412 redisLog(REDIS_WARNING
,
413 "Handshake error: we already know node %.40s, updating the address if needed.", sender
->name
);
414 nodeUpdateAddress(sender
,link
,ntohs(hdr
->port
));
415 freeClusterNode(link
->node
); /* will free the link too */
419 /* First thing to do is replacing the random name with the
420 * right node name if this was an handshake stage. */
421 clusterRenameNode(link
->node
, hdr
->sender
);
422 redisLog(REDIS_DEBUG
,"Handshake with node %.40s completed.",
424 link
->node
->flags
&= ~REDIS_NODE_HANDSHAKE
;
425 } else if (memcmp(link
->node
->name
,hdr
->sender
,
426 REDIS_CLUSTER_NAMELEN
) != 0)
428 /* If the reply has a non matching node ID we
429 * disconnect this node and set it as not having an associated
431 redisLog(REDIS_DEBUG
,"PONG contains mismatching sender ID");
432 link
->node
->flags
|= REDIS_NODE_NOADDR
;
433 freeClusterLink(link
);
434 /* FIXME: remove this node if we already have it.
436 * If we already have it but the IP is different, use
437 * the new one if the old node is in FAIL, PFAIL, or NOADDR
442 /* Update our info about the node */
443 link
->node
->pong_received
= time(NULL
);
445 /* Update master/slave info */
447 if (!memcmp(hdr
->slaveof
,REDIS_NODE_NULL_NAME
,
448 sizeof(hdr
->slaveof
)))
450 sender
->flags
&= ~REDIS_NODE_SLAVE
;
451 sender
->flags
|= REDIS_NODE_MASTER
;
452 sender
->slaveof
= NULL
;
454 clusterNode
*master
= clusterLookupNode(hdr
->slaveof
);
456 sender
->flags
&= ~REDIS_NODE_MASTER
;
457 sender
->flags
|= REDIS_NODE_SLAVE
;
458 if (sender
->numslaves
) clusterNodeResetSlaves(sender
);
459 if (master
) clusterNodeAddSlave(master
,sender
);
463 /* Update our info about served slots if this new node is serving
464 * slots that are not served from our point of view. */
465 if (sender
&& sender
->flags
& REDIS_NODE_MASTER
) {
469 memcmp(sender
->slots
,hdr
->myslots
,sizeof(hdr
->myslots
)) != 0;
470 memcpy(sender
->slots
,hdr
->myslots
,sizeof(hdr
->myslots
));
472 for (j
= 0; j
< REDIS_CLUSTER_SLOTS
; j
++) {
473 if (clusterNodeGetSlotBit(sender
,j
)) {
474 if (server
.cluster
.slots
[j
] == sender
) continue;
475 if (server
.cluster
.slots
[j
] == NULL
||
476 server
.cluster
.slots
[j
]->flags
& REDIS_NODE_FAIL
)
478 server
.cluster
.slots
[j
] = sender
;
486 /* Get info from the gossip section */
487 clusterProcessGossipSection(hdr
,link
);
489 /* Update the cluster state if needed */
490 if (update
) clusterUpdateState();
491 } else if (type
== CLUSTERMSG_TYPE_FAIL
&& sender
) {
492 clusterNode
*failing
;
494 failing
= clusterLookupNode(hdr
->data
.fail
.about
.nodename
);
495 if (failing
&& !(failing
->flags
& REDIS_NODE_FAIL
)) {
496 redisLog(REDIS_NOTICE
,
497 "FAIL message received from %.40s about %.40s",
498 hdr
->sender
, hdr
->data
.fail
.about
.nodename
);
499 failing
->flags
|= REDIS_NODE_FAIL
;
500 failing
->flags
&= ~REDIS_NODE_PFAIL
;
501 clusterUpdateState();
504 redisLog(REDIS_NOTICE
,"Received unknown packet type: %d", type
);
509 /* This function is called when we detect the link with this node is lost.
510 We set the node as no longer connected. The Cluster Cron will detect
511 this connection and will try to get it connected again.
513 Instead if the node is a temporary node used to accept a query, we
514 completely free the node on error. */
515 void handleLinkIOError(clusterLink
*link
) {
516 freeClusterLink(link
);
519 /* Send data. This is handled using a trivial send buffer that gets
520 * consumed by write(). We don't try to optimize this for speed too much
521 * as this is a very low traffic channel. */
522 void clusterWriteHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
523 clusterLink
*link
= (clusterLink
*) privdata
;
528 nwritten
= write(fd
, link
->sndbuf
, sdslen(link
->sndbuf
));
530 redisLog(REDIS_NOTICE
,"I/O error writing to node link: %s",
532 handleLinkIOError(link
);
535 link
->sndbuf
= sdsrange(link
->sndbuf
,nwritten
,-1);
536 if (sdslen(link
->sndbuf
) == 0)
537 aeDeleteFileEvent(server
.el
, link
->fd
, AE_WRITABLE
);
540 /* Read data. Try to read the first field of the header first to check the
541 * full length of the packet. When a whole packet is in memory this function
542 * will call the function to process the packet. And so forth. */
543 void clusterReadHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
547 clusterLink
*link
= (clusterLink
*) privdata
;
553 if (sdslen(link
->rcvbuf
) >= 4) {
554 hdr
= (clusterMsg
*) link
->rcvbuf
;
555 readlen
= ntohl(hdr
->totlen
) - sdslen(link
->rcvbuf
);
557 readlen
= 4 - sdslen(link
->rcvbuf
);
560 nread
= read(fd
,buf
,readlen
);
561 if (nread
== -1 && errno
== EAGAIN
) return; /* Just no data */
565 redisLog(REDIS_NOTICE
,"I/O error reading from node link: %s",
566 (nread
== 0) ? "connection closed" : strerror(errno
));
567 handleLinkIOError(link
);
570 /* Read data and recast the pointer to the new buffer. */
571 link
->rcvbuf
= sdscatlen(link
->rcvbuf
,buf
,nread
);
572 hdr
= (clusterMsg
*) link
->rcvbuf
;
575 /* Total length obtained? read the payload now instead of burning
576 * cycles waiting for a new event to fire. */
577 if (sdslen(link
->rcvbuf
) == 4) goto again
;
579 /* Whole packet in memory? We can process it. */
580 if (sdslen(link
->rcvbuf
) == ntohl(hdr
->totlen
)) {
581 if (clusterProcessPacket(link
)) {
582 sdsfree(link
->rcvbuf
);
583 link
->rcvbuf
= sdsempty();
588 /* Put stuff into the send buffer. */
589 void clusterSendMessage(clusterLink
*link
, unsigned char *msg
, size_t msglen
) {
590 if (sdslen(link
->sndbuf
) == 0 && msglen
!= 0)
591 aeCreateFileEvent(server
.el
,link
->fd
,AE_WRITABLE
,
592 clusterWriteHandler
,link
);
594 link
->sndbuf
= sdscatlen(link
->sndbuf
, msg
, msglen
);
597 /* Build the message header */
598 void clusterBuildMessageHdr(clusterMsg
*hdr
, int type
) {
601 memset(hdr
,0,sizeof(*hdr
));
602 hdr
->type
= htons(type
);
603 memcpy(hdr
->sender
,server
.cluster
.myself
->name
,REDIS_CLUSTER_NAMELEN
);
604 memcpy(hdr
->myslots
,server
.cluster
.myself
->slots
,
605 sizeof(hdr
->myslots
));
606 memset(hdr
->slaveof
,0,REDIS_CLUSTER_NAMELEN
);
607 if (server
.cluster
.myself
->slaveof
!= NULL
) {
608 memcpy(hdr
->slaveof
,server
.cluster
.myself
->slaveof
->name
,
609 REDIS_CLUSTER_NAMELEN
);
611 hdr
->port
= htons(server
.port
);
612 hdr
->state
= server
.cluster
.state
;
613 memset(hdr
->configdigest
,0,32); /* FIXME: set config digest */
615 if (type
== CLUSTERMSG_TYPE_FAIL
) {
616 totlen
= sizeof(clusterMsg
)-sizeof(union clusterMsgData
);
617 totlen
+= sizeof(clusterMsgDataFail
);
619 hdr
->totlen
= htonl(totlen
);
620 /* For PING, PONG, and MEET, fixing the totlen field is up to the caller */
623 /* Send a PING or PONG packet to the specified node, making sure to add enough
624 * gossip informations. */
625 void clusterSendPing(clusterLink
*link
, int type
) {
626 unsigned char buf
[1024];
627 clusterMsg
*hdr
= (clusterMsg
*) buf
;
628 int gossipcount
= 0, totlen
;
629 /* freshnodes is the number of nodes we can still use to populate the
630 * gossip section of the ping packet. Basically we start with the nodes
631 * we have in memory minus two (ourself and the node we are sending the
632 * message to). Every time we add a node we decrement the counter, so when
633 * it will drop to <= zero we know there is no more gossip info we can
635 int freshnodes
= dictSize(server
.cluster
.nodes
)-2;
637 if (link
->node
&& type
== CLUSTERMSG_TYPE_PING
)
638 link
->node
->ping_sent
= time(NULL
);
639 clusterBuildMessageHdr(hdr
,type
);
641 /* Populate the gossip fields */
642 while(freshnodes
> 0 && gossipcount
< 3) {
643 struct dictEntry
*de
= dictGetRandomKey(server
.cluster
.nodes
);
644 clusterNode
*this = dictGetEntryVal(de
);
645 clusterMsgDataGossip
*gossip
;
648 /* Not interesting to gossip about ourself.
649 * Nor to send gossip info about HANDSHAKE state nodes (zero info). */
650 if (this == server
.cluster
.myself
||
651 this->flags
& REDIS_NODE_HANDSHAKE
) {
652 freshnodes
--; /* otherwise we may loop forever. */
656 /* Check if we already added this node */
657 for (j
= 0; j
< gossipcount
; j
++) {
658 if (memcmp(hdr
->data
.ping
.gossip
[j
].nodename
,this->name
,
659 REDIS_CLUSTER_NAMELEN
) == 0) break;
661 if (j
!= gossipcount
) continue;
665 gossip
= &(hdr
->data
.ping
.gossip
[gossipcount
]);
666 memcpy(gossip
->nodename
,this->name
,REDIS_CLUSTER_NAMELEN
);
667 gossip
->ping_sent
= htonl(this->ping_sent
);
668 gossip
->pong_received
= htonl(this->pong_received
);
669 memcpy(gossip
->ip
,this->ip
,sizeof(this->ip
));
670 gossip
->port
= htons(this->port
);
671 gossip
->flags
= htons(this->flags
);
674 totlen
= sizeof(clusterMsg
)-sizeof(union clusterMsgData
);
675 totlen
+= (sizeof(clusterMsgDataGossip
)*gossipcount
);
676 hdr
->count
= htons(gossipcount
);
677 hdr
->totlen
= htonl(totlen
);
678 clusterSendMessage(link
,buf
,totlen
);
681 /* Send a message to all the nodes with a reliable link */
682 void clusterBroadcastMessage(void *buf
, size_t len
) {
686 di
= dictGetIterator(server
.cluster
.nodes
);
687 while((de
= dictNext(di
)) != NULL
) {
688 clusterNode
*node
= dictGetEntryVal(de
);
690 if (!node
->link
) continue;
691 if (node
->flags
& (REDIS_NODE_MYSELF
|REDIS_NODE_NOADDR
)) continue;
692 clusterSendMessage(node
->link
,buf
,len
);
694 dictReleaseIterator(di
);
697 /* Send a FAIL message to all the nodes we are able to contact.
698 * The FAIL message is sent when we detect that a node is failing
699 * (REDIS_NODE_PFAIL) and we also receive a gossip confirmation of this:
700 * we switch the node state to REDIS_NODE_FAIL and ask all the other
701 * nodes to do the same ASAP. */
702 void clusterSendFail(char *nodename
) {
703 unsigned char buf
[1024];
704 clusterMsg
*hdr
= (clusterMsg
*) buf
;
706 clusterBuildMessageHdr(hdr
,CLUSTERMSG_TYPE_FAIL
);
707 memcpy(hdr
->data
.fail
.about
.nodename
,nodename
,REDIS_CLUSTER_NAMELEN
);
708 clusterBroadcastMessage(buf
,ntohl(hdr
->totlen
));
711 /* -----------------------------------------------------------------------------
713 * -------------------------------------------------------------------------- */
715 /* This is executed 1 time every second */
716 void clusterCron(void) {
720 time_t min_ping_sent
= 0;
721 clusterNode
*min_ping_node
= NULL
;
723 /* Check if we have disconnected nodes and reestablish the connection. */
724 di
= dictGetIterator(server
.cluster
.nodes
);
725 while((de
= dictNext(di
)) != NULL
) {
726 clusterNode
*node
= dictGetEntryVal(de
);
728 if (node
->flags
& (REDIS_NODE_MYSELF
|REDIS_NODE_NOADDR
)) continue;
729 if (node
->link
== NULL
) {
733 fd
= anetTcpNonBlockConnect(server
.neterr
, node
->ip
,
734 node
->port
+REDIS_CLUSTER_PORT_INCR
);
735 if (fd
== -1) continue;
736 link
= createClusterLink(node
);
739 aeCreateFileEvent(server
.el
,link
->fd
,AE_READABLE
,clusterReadHandler
,link
);
740 /* If the node is flagged as MEET, we send a MEET message instead
741 * of a PING one, to force the receiver to add us in its node
743 clusterSendPing(link
, node
->flags
& REDIS_NODE_MEET
?
744 CLUSTERMSG_TYPE_MEET
: CLUSTERMSG_TYPE_PING
);
745 /* We can clear the flag after the first packet is sent.
746 * If we'll never receive a PONG, we'll never send new packets
747 * to this node. Instead after the PONG is received and we
748 * are no longer in meet/handshake status, we want to send
749 * normal PING packets. */
750 node
->flags
&= ~REDIS_NODE_MEET
;
752 redisLog(REDIS_NOTICE
,"Connecting with Node %.40s at %s:%d\n", node
->name
, node
->ip
, node
->port
+REDIS_CLUSTER_PORT_INCR
);
755 dictReleaseIterator(di
);
757 /* Ping some random node. Check a few random nodes and ping the one with
758 * the oldest ping_sent time */
759 for (j
= 0; j
< 5; j
++) {
760 de
= dictGetRandomKey(server
.cluster
.nodes
);
761 clusterNode
*this = dictGetEntryVal(de
);
763 if (this->link
== NULL
) continue;
764 if (this->flags
& (REDIS_NODE_MYSELF
|REDIS_NODE_HANDSHAKE
)) continue;
765 if (min_ping_node
== NULL
|| min_ping_sent
> this->ping_sent
) {
766 min_ping_node
= this;
767 min_ping_sent
= this->ping_sent
;
771 redisLog(REDIS_DEBUG
,"Pinging node %40s", min_ping_node
->name
);
772 clusterSendPing(min_ping_node
->link
, CLUSTERMSG_TYPE_PING
);
775 /* Iterate nodes to check if we need to flag something as failing */
776 di
= dictGetIterator(server
.cluster
.nodes
);
777 while((de
= dictNext(di
)) != NULL
) {
778 clusterNode
*node
= dictGetEntryVal(de
);
782 (REDIS_NODE_MYSELF
|REDIS_NODE_NOADDR
|REDIS_NODE_HANDSHAKE
|
783 REDIS_NODE_FAIL
)) continue;
784 /* Check only if we already sent a ping and did not received
786 if (node
->ping_sent
== 0 ||
787 node
->ping_sent
<= node
->pong_received
) continue;
789 delay
= time(NULL
) - node
->pong_received
;
790 if (node
->flags
& REDIS_NODE_PFAIL
) {
791 /* The PFAIL condition can be reversed without external
792 * help if it is not transitive (that is, if it does not
793 * turn into a FAIL state). */
794 if (delay
< server
.cluster
.node_timeout
)
795 node
->flags
&= ~REDIS_NODE_PFAIL
;
797 if (delay
>= server
.cluster
.node_timeout
) {
798 redisLog(REDIS_DEBUG
,"*** NODE %.40s possibly failing",
800 node
->flags
|= REDIS_NODE_PFAIL
;
804 dictReleaseIterator(di
);
807 /* -----------------------------------------------------------------------------
809 * -------------------------------------------------------------------------- */
811 /* Set the slot bit and return the old value. */
812 int clusterNodeSetSlotBit(clusterNode
*n
, int slot
) {
815 int old
= (n
->slots
[byte
] & (1<<bit
)) != 0;
816 n
->slots
[byte
] |= 1<<bit
;
820 /* Clear the slot bit and return the old value. */
821 int clusterNodeClearSlotBit(clusterNode
*n
, int slot
) {
824 int old
= (n
->slots
[byte
] & (1<<bit
)) != 0;
825 n
->slots
[byte
] &= ~(1<<bit
);
829 /* Return the slot bit from the cluster node structure. */
830 int clusterNodeGetSlotBit(clusterNode
*n
, int slot
) {
833 return (n
->slots
[byte
] & (1<<bit
)) != 0;
836 /* Add the specified slot to the list of slots that node 'n' will
837 * serve. Return REDIS_OK if the operation ended with success.
838 * If the slot is already assigned to another instance this is considered
839 * an error and REDIS_ERR is returned. */
840 int clusterAddSlot(clusterNode
*n
, int slot
) {
841 redisAssert(clusterNodeSetSlotBit(n
,slot
) == 0);
842 server
.cluster
.slots
[slot
] = server
.cluster
.myself
;
843 printf("SLOT %d added to %.40s\n", slot
, n
->name
);
847 /* -----------------------------------------------------------------------------
848 * Cluster state evaluation function
849 * -------------------------------------------------------------------------- */
850 void clusterUpdateState(void) {
854 for (j
= 0; j
< REDIS_CLUSTER_SLOTS
; j
++) {
855 if (server
.cluster
.slots
[j
] == NULL
||
856 server
.cluster
.slots
[j
]->flags
& (REDIS_NODE_FAIL
))
863 if (server
.cluster
.state
== REDIS_CLUSTER_NEEDHELP
) {
864 server
.cluster
.state
= REDIS_CLUSTER_NEEDHELP
;
866 server
.cluster
.state
= REDIS_CLUSTER_OK
;
869 server
.cluster
.state
= REDIS_CLUSTER_FAIL
;
873 /* -----------------------------------------------------------------------------
875 * -------------------------------------------------------------------------- */
877 void clusterCommand(redisClient
*c
) {
878 if (server
.cluster_enabled
== 0) {
879 addReplyError(c
,"This instance has cluster support disabled");
883 if (!strcasecmp(c
->argv
[1]->ptr
,"meet") && c
->argc
== 4) {
885 struct sockaddr_in sa
;
888 /* Perform sanity checks on IP/port */
889 if (inet_aton(c
->argv
[2]->ptr
,&sa
.sin_addr
) == 0) {
890 addReplyError(c
,"Invalid IP address in MEET");
893 if (getLongFromObjectOrReply(c
, c
->argv
[3], &port
, NULL
) != REDIS_OK
||
894 port
< 0 || port
> (65535-REDIS_CLUSTER_PORT_INCR
))
896 addReplyError(c
,"Invalid TCP port specified");
900 /* Finally add the node to the cluster with a random name, this
901 * will get fixed in the first handshake (ping/pong). */
902 n
= createClusterNode(NULL
,REDIS_NODE_HANDSHAKE
|REDIS_NODE_MEET
);
903 strncpy(n
->ip
,inet_ntoa(sa
.sin_addr
),sizeof(n
->ip
));
906 addReply(c
,shared
.ok
);
907 } else if (!strcasecmp(c
->argv
[1]->ptr
,"nodes") && c
->argc
== 2) {
913 di
= dictGetIterator(server
.cluster
.nodes
);
914 while((de
= dictNext(di
)) != NULL
) {
915 clusterNode
*node
= dictGetEntryVal(de
);
917 /* Node coordinates */
918 ci
= sdscatprintf(ci
,"%.40s %s:%d ",
924 if (node
->flags
== 0) ci
= sdscat(ci
,"noflags,");
925 if (node
->flags
& REDIS_NODE_MYSELF
) ci
= sdscat(ci
,"myself,");
926 if (node
->flags
& REDIS_NODE_MASTER
) ci
= sdscat(ci
,"master,");
927 if (node
->flags
& REDIS_NODE_SLAVE
) ci
= sdscat(ci
,"slave,");
928 if (node
->flags
& REDIS_NODE_PFAIL
) ci
= sdscat(ci
,"fail?,");
929 if (node
->flags
& REDIS_NODE_FAIL
) ci
= sdscat(ci
,"fail,");
930 if (node
->flags
& REDIS_NODE_HANDSHAKE
) ci
=sdscat(ci
,"handshake,");
931 if (node
->flags
& REDIS_NODE_NOADDR
) ci
= sdscat(ci
,"noaddr,");
932 if (ci
[sdslen(ci
)-1] == ',') ci
[sdslen(ci
)-1] = ' ';
934 /* Slave of... or just "-" */
936 ci
= sdscatprintf(ci
,"%.40s ",node
->slaveof
->name
);
938 ci
= sdscatprintf(ci
,"- ");
940 /* Latency from the POV of this node, link status */
941 ci
= sdscatprintf(ci
,"%ld %ld %s\n",
942 (long) node
->ping_sent
,
943 (long) node
->pong_received
,
944 node
->link
? "connected" : "disconnected");
946 dictReleaseIterator(di
);
947 o
= createObject(REDIS_STRING
,ci
);
950 } else if (!strcasecmp(c
->argv
[1]->ptr
,"addslots") && c
->argc
>= 3) {
953 unsigned char *slots
= zmalloc(REDIS_CLUSTER_SLOTS
);
955 memset(slots
,0,REDIS_CLUSTER_SLOTS
);
956 /* Check that all the arguments are parsable and that all the
957 * slots are not already busy. */
958 for (j
= 2; j
< c
->argc
; j
++) {
959 if (getLongLongFromObject(c
->argv
[j
],&slot
) != REDIS_OK
||
960 slot
< 0 || slot
> REDIS_CLUSTER_SLOTS
)
962 addReplyError(c
,"Invalid or out of range slot index");
966 if (server
.cluster
.slots
[slot
]) {
967 addReplyErrorFormat(c
,"Slot %lld is already busy", slot
);
971 if (slots
[slot
]++ == 1) {
972 addReplyErrorFormat(c
,"Slot %d specified multiple times",
978 for (j
= 0; j
< REDIS_CLUSTER_SLOTS
; j
++) {
980 int retval
= clusterAddSlot(server
.cluster
.myself
,j
);
982 redisAssert(retval
== REDIS_OK
);
986 clusterUpdateState();
987 addReply(c
,shared
.ok
);
988 } else if (!strcasecmp(c
->argv
[1]->ptr
,"info") && c
->argc
== 2) {
989 char *statestr
[] = {"ok","fail","needhelp"};
990 int slots_assigned
= 0, slots_ok
= 0, slots_pfail
= 0, slots_fail
= 0;
993 for (j
= 0; j
< REDIS_CLUSTER_SLOTS
; j
++) {
994 clusterNode
*n
= server
.cluster
.slots
[j
];
996 if (n
== NULL
) continue;
998 if (n
->flags
& REDIS_NODE_FAIL
) {
1000 } else if (n
->flags
& REDIS_NODE_PFAIL
) {
1007 sds info
= sdscatprintf(sdsempty(),
1008 "cluster_state:%s\r\n"
1009 "cluster_slots_assigned:%d\r\n"
1010 "cluster_slots_ok:%d\r\n"
1011 "cluster_slots_pfail:%d\r\n"
1012 "cluster_slots_fail:%d\r\n"
1013 , statestr
[server
.cluster
.state
],
1019 addReplySds(c
,sdscatprintf(sdsempty(),"$%lu\r\n",
1020 (unsigned long)sdslen(info
)));
1021 addReplySds(c
,info
);
1022 addReply(c
,shared
.crlf
);
1024 addReplyError(c
,"Wrong CLUSTER subcommand or number of arguments");
1028 /* -----------------------------------------------------------------------------
1029 * RESTORE and MIGRATE commands
1030 * -------------------------------------------------------------------------- */
1032 /* RESTORE key ttl serialized-value */
1033 void restoreCommand(redisClient
*c
) {
1037 unsigned char *data
;
1040 /* Make sure this key does not already exist here... */
1041 if (dbExists(c
->db
,c
->argv
[1])) {
1042 addReplyError(c
,"Target key name is busy.");
1046 /* Check if the TTL value makes sense */
1047 if (getLongFromObjectOrReply(c
,c
->argv
[2],&ttl
,NULL
) != REDIS_OK
) {
1049 } else if (ttl
< 0) {
1050 addReplyError(c
,"Invalid TTL value, must be >= 0");
1054 /* rdbLoadObject() only works against file descriptors so we need to
1055 * dump the serialized object into a file and reload. */
1056 snprintf(buf
,sizeof(buf
),"redis-restore-%d.tmp",getpid());
1057 fp
= fopen(buf
,"w+");
1059 redisLog(REDIS_WARNING
,"Can't open tmp file for RESTORE: %s",
1061 addReplyErrorFormat(c
,"RESTORE failed, tmp file creation error: %s",
1067 /* Write the actual data and rewind the file */
1068 data
= (unsigned char*) c
->argv
[3]->ptr
;
1069 if (fwrite(data
+1,sdslen((sds
)data
)-1,1,fp
) != 1) {
1070 redisLog(REDIS_WARNING
,"Can't write against tmp file for RESTORE: %s",
1072 addReplyError(c
,"RESTORE failed, tmp file I/O error.");
1078 /* Finally create the object from the serialized dump and
1079 * store it at the specified key. */
1080 o
= rdbLoadObject(data
[0],fp
);
1082 addReplyError(c
,"Bad data format.");
1088 /* Create the key and set the TTL if any */
1089 dbAdd(c
->db
,c
->argv
[1],o
);
1090 if (ttl
) setExpire(c
->db
,c
->argv
[1],time(NULL
)+ttl
);
1091 addReply(c
,shared
.ok
);
1094 /* MIGRATE host port key dbid timeout */
1095 void migrateCommand(redisClient
*c
) {
1107 if (getLongFromObjectOrReply(c
,c
->argv
[5],&timeout
,NULL
) != REDIS_OK
)
1109 if (getLongFromObjectOrReply(c
,c
->argv
[4],&dbid
,NULL
) != REDIS_OK
)
1111 if (timeout
<= 0) timeout
= 1;
1113 /* Check if the key is here. If not we reply with success as there is
1114 * nothing to migrate (for instance the key expired in the meantime), but
1115 * we include such information in the reply string. */
1116 if ((o
= lookupKeyRead(c
->db
,c
->argv
[3])) == NULL
) {
1117 addReplySds(c
,sdsnew("+NOKEY"));
1122 fd
= anetTcpNonBlockConnect(server
.neterr
,c
->argv
[1]->ptr
,
1123 atoi(c
->argv
[2]->ptr
));
1125 addReplyErrorFormat(c
,"Can't connect to target node: %s",
1129 if ((aeWait(fd
,AE_WRITABLE
,timeout
*1000) & AE_WRITABLE
) == 0) {
1130 addReplyError(c
,"Timeout connecting to the client");
1134 /* Create temp file */
1135 snprintf(buf
,sizeof(buf
),"redis-migrate-%d.tmp",getpid());
1136 fp
= fopen(buf
,"w+");
1138 redisLog(REDIS_WARNING
,"Can't open tmp file for MIGRATE: %s",
1140 addReplyErrorFormat(c
,"MIGRATE failed, tmp file creation error: %s.",
1146 /* Build the SELECT + RESTORE query writing it in our temp file. */
1147 if (fwriteBulkCount(fp
,'*',2) == 0) goto file_wr_err
;
1148 if (fwriteBulkString(fp
,"SELECT",6) == 0) goto file_wr_err
;
1149 if (fwriteBulkLongLong(fp
,dbid
) == 0) goto file_wr_err
;
1151 ttl
= getExpire(c
->db
,c
->argv
[3]);
1153 if (fwriteBulkCount(fp
,'*',4) == 0) goto file_wr_err
;
1154 if (fwriteBulkString(fp
,"RESTORE",7) == 0) goto file_wr_err
;
1155 if (fwriteBulkObject(fp
,c
->argv
[3]) == 0) goto file_wr_err
;
1156 if (fwriteBulkLongLong(fp
, (ttl
== -1) ? 0 : ttl
) == 0) goto file_wr_err
;
1158 /* Finally the last argument that is the serailized object payload
1159 * in the form: <type><rdb-serailized-object>. */
1160 payload_len
= rdbSavedObjectLen(o
);
1161 if (fwriteBulkCount(fp
,'$',payload_len
+1) == 0) goto file_wr_err
;
1162 if (fwrite(&type
,1,1,fp
) == 0) goto file_wr_err
;
1163 if (rdbSaveObject(fp
,o
) == -1) goto file_wr_err
;
1164 if (fwrite("\r\n",2,1,fp
) == 0) goto file_wr_err
;
1166 /* Tranfer the query to the other node */
1172 while ((nread
= fread(buf
,1,sizeof(buf
),fp
)) != 0) {
1175 nwritten
= syncWrite(fd
,buf
,nread
,timeout
);
1176 if (nwritten
!= (signed)nread
) goto socket_wr_err
;
1178 if (ferror(fp
)) goto file_rd_err
;
1181 /* Read back the reply */
1186 /* Read the two replies */
1187 if (syncReadLine(fd
, buf1
, sizeof(buf1
), timeout
) <= 0)
1189 if (syncReadLine(fd
, buf2
, sizeof(buf2
), timeout
) <= 0)
1191 if (buf1
[0] == '-' || buf2
[0] == '-') {
1192 addReplyErrorFormat(c
,"Target instance replied with error: %s",
1193 (buf1
[0] == '-') ? buf1
+1 : buf2
+1);
1195 dbDelete(c
->db
,c
->argv
[3]);
1196 addReply(c
,shared
.ok
);
1204 redisLog(REDIS_WARNING
,"Can't write on tmp file for MIGRATE: %s",
1206 addReplyErrorFormat(c
,"MIGRATE failed, tmp file write error: %s.",
1212 redisLog(REDIS_WARNING
,"Can't read from tmp file for MIGRATE: %s",
1214 addReplyErrorFormat(c
,"MIGRATE failed, tmp file read error: %s.",
1220 redisLog(REDIS_NOTICE
,"Can't write to target node for MIGRATE: %s",
1222 addReplyErrorFormat(c
,"MIGRATE failed, writing to target node: %s.",
1228 redisLog(REDIS_NOTICE
,"Can't read from target node for MIGRATE: %s",
1230 addReplyErrorFormat(c
,"MIGRATE failed, reading from target node: %s.",
1236 /* -----------------------------------------------------------------------------
1237 * Cluster functions related to serving / redirecting clients
1238 * -------------------------------------------------------------------------- */
1240 /* Return the pointer to the cluster node that is able to serve the query
1241 * as all the keys belong to hash slots for which the node is in charge.
1243 * If keys in query spawn multiple nodes NULL is returned. */
1244 clusterNode
*getNodeByQuery(redisClient
*c
, struct redisCommand
*cmd
, robj
**argv
, int argc
, int *hashslot
) {
1245 clusterNode
*n
= NULL
;
1246 multiState
*ms
, _ms
;
1250 /* We handle all the cases as if they were EXEC commands, so we have
1251 * a common code path for everything */
1252 if (cmd
->proc
== execCommand
) {
1253 /* If REDIS_MULTI flag is not set EXEC is just going to return an
1255 if (!(c
->flags
& REDIS_MULTI
)) return server
.cluster
.myself
;
1258 /* Create a fake Multi State structure, with just one command */
1267 for (i
= 0; i
< ms
->count
; i
++) {
1268 struct redisCommand
*mcmd
;
1270 int margc
, *keyindex
, numkeys
, j
;
1272 mcmd
= ms
->commands
[i
].cmd
;
1273 margc
= ms
->commands
[i
].argc
;
1274 margv
= ms
->commands
[i
].argv
;
1276 keyindex
= getKeysFromCommand(mcmd
,margv
,margc
,&numkeys
,
1277 REDIS_GETKEYS_PRELOAD
);
1278 for (j
= 0; j
< numkeys
; j
++) {
1279 int slot
= keyHashSlot((char*)margv
[keyindex
[j
]]->ptr
,
1280 sdslen(margv
[keyindex
[j
]]->ptr
));
1281 struct clusterNode
*slotnode
;
1283 slotnode
= server
.cluster
.slots
[slot
];
1284 if (hashslot
) *hashslot
= slot
;
1285 /* Node not assigned? (Should never happen actually
1286 * if we reached this function).
1287 * Different node than the previous one?
1288 * Return NULL, the cluster can't serve multi-node requests */
1289 if (slotnode
== NULL
|| (n
&& slotnode
!= n
)) {
1290 getKeysFreeResult(keyindex
);
1296 getKeysFreeResult(keyindex
);
1298 return (n
== NULL
) ? server
.cluster
.myself
: n
;