1 #ifndef __CLUSTER_H 2 #define __CLUSTER_H 3 4 /*----------------------------------------------------------------------------- 5 * Redis cluster data structures, defines, exported API. 6 *----------------------------------------------------------------------------*/ 7 8 #define CLUSTER_SLOTS 16384 9 #define CLUSTER_OK 0 /* Everything looks ok */ 10 #define CLUSTER_FAIL 1 /* The cluster can't work */ 11 #define CLUSTER_NAMELEN 40 /* sha1 hex length */ 12 #define CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */ 13 14 /* The following defines are amount of time, sometimes expressed as 15 * multiplicators of the node timeout value (when ending with MULT). */ 16 #define CLUSTER_DEFAULT_NODE_TIMEOUT 15000 17 #define CLUSTER_DEFAULT_SLAVE_VALIDITY 10 /* Slave max data age factor. */ 18 #define CLUSTER_DEFAULT_REQUIRE_FULL_COVERAGE 1 19 #define CLUSTER_FAIL_REPORT_VALIDITY_MULT 2 /* Fail report validity. */ 20 #define CLUSTER_FAIL_UNDO_TIME_MULT 2 /* Undo fail if master is back. */ 21 #define CLUSTER_FAIL_UNDO_TIME_ADD 10 /* Some additional time. */ 22 #define CLUSTER_FAILOVER_DELAY 5 /* Seconds */ 23 #define CLUSTER_DEFAULT_MIGRATION_BARRIER 1 24 #define CLUSTER_MF_TIMEOUT 5000 /* Milliseconds to do a manual failover. */ 25 #define CLUSTER_MF_PAUSE_MULT 2 /* Master pause manual failover mult. */ 26 #define CLUSTER_SLAVE_MIGRATION_DELAY 5000 /* Delay for slave migration. */ 27 28 /* Redirection errors returned by getNodeByQuery(). */ 29 #define CLUSTER_REDIR_NONE 0 /* Node can serve the request. */ 30 #define CLUSTER_REDIR_CROSS_SLOT 1 /* -CROSSSLOT request. */ 31 #define CLUSTER_REDIR_UNSTABLE 2 /* -TRYAGAIN redirection required */ 32 #define CLUSTER_REDIR_ASK 3 /* -ASK redirection required. */ 33 #define CLUSTER_REDIR_MOVED 4 /* -MOVED redirection required. */ 34 #define CLUSTER_REDIR_DOWN_STATE 5 /* -CLUSTERDOWN, global state. */ 35 #define CLUSTER_REDIR_DOWN_UNBOUND 6 /* -CLUSTERDOWN, unbound slot. */ 36 37 struct clusterNode; 38 39 /* clusterLink encapsulates everything needed to talk with a remote node. */ 40 typedef struct clusterLink { 41 mstime_t ctime; /* Link creation time */ 42 int fd; /* TCP socket file descriptor */ 43 sds sndbuf; /* Packet send buffer */ 44 sds rcvbuf; /* Packet reception buffer */ 45 struct clusterNode *node; /* Node related to this link if any, or NULL */ 46 } clusterLink; 47 48 /* Cluster node flags and macros. */ 49 #define CLUSTER_NODE_MASTER 1 /* The node is a master */ 50 #define CLUSTER_NODE_SLAVE 2 /* The node is a slave */ 51 #define CLUSTER_NODE_PFAIL 4 /* Failure? Need acknowledge */ 52 #define CLUSTER_NODE_FAIL 8 /* The node is believed to be malfunctioning */ 53 #define CLUSTER_NODE_MYSELF 16 /* This node is myself */ 54 #define CLUSTER_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */ 55 #define CLUSTER_NODE_NOADDR 64 /* We don't know the address of this node */ 56 #define CLUSTER_NODE_MEET 128 /* Send a MEET message to this node */ 57 #define CLUSTER_NODE_MIGRATE_TO 256 /* Master elegible for replica migration. */ 58 #define CLUSTER_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 59 60 #define nodeIsMaster(n) ((n)->flags & CLUSTER_NODE_MASTER) 61 #define nodeIsSlave(n) ((n)->flags & CLUSTER_NODE_SLAVE) 62 #define nodeInHandshake(n) ((n)->flags & CLUSTER_NODE_HANDSHAKE) 63 #define nodeHasAddr(n) (!((n)->flags & CLUSTER_NODE_NOADDR)) 64 #define nodeWithoutAddr(n) ((n)->flags & CLUSTER_NODE_NOADDR) 65 #define nodeTimedOut(n) ((n)->flags & CLUSTER_NODE_PFAIL) 66 #define nodeFailed(n) ((n)->flags & CLUSTER_NODE_FAIL) 67 68 /* Reasons why a slave is not able to failover. */ 69 #define CLUSTER_CANT_FAILOVER_NONE 0 70 #define CLUSTER_CANT_FAILOVER_DATA_AGE 1 71 #define CLUSTER_CANT_FAILOVER_WAITING_DELAY 2 72 #define CLUSTER_CANT_FAILOVER_EXPIRED 3 73 #define CLUSTER_CANT_FAILOVER_WAITING_VOTES 4 74 #define CLUSTER_CANT_FAILOVER_RELOG_PERIOD (60*5) /* seconds. */ 75 76 /* This structure represent elements of node->fail_reports. */ 77 typedef struct clusterNodeFailReport { 78 struct clusterNode *node; /* Node reporting the failure condition. */ 79 mstime_t time; /* Time of the last report from this node. */ 80 } clusterNodeFailReport; 81 82 typedef struct clusterNode { 83 mstime_t ctime; /* Node object creation time. */ 84 char name[CLUSTER_NAMELEN]; /* Node name, hex string, sha1-size */ 85 int flags; /* CLUSTER_NODE_... */ 86 uint64_t configEpoch; /* Last configEpoch observed for this node */ 87 unsigned char slots[CLUSTER_SLOTS/8]; /* slots handled by this node */ 88 int numslots; /* Number of slots handled by this node */ 89 int numslaves; /* Number of slave nodes, if this is a master */ 90 struct clusterNode **slaves; /* pointers to slave nodes */ 91 struct clusterNode *slaveof; /* pointer to the master node */ 92 mstime_t ping_sent; /* Unix time we sent latest ping */ 93 mstime_t pong_received; /* Unix time we received the pong */ 94 mstime_t fail_time; /* Unix time when FAIL flag was set */ 95 mstime_t voted_time; /* Last time we voted for a slave of this master */ 96 mstime_t repl_offset_time; /* Unix time we received offset for this node */ 97 mstime_t orphaned_time; /* Starting time of orphaned master condition */ 98 long long repl_offset; /* Last known repl offset for this node. */ 99 char ip[NET_IP_STR_LEN]; /* Latest known IP address of this node */ 100 int port; /* Latest known port of this node */ 101 clusterLink *link; /* TCP/IP link with this node */ 102 list *fail_reports; /* List of nodes signaling this as failing */ 103 } clusterNode; 104 105 typedef struct clusterState { 106 clusterNode *myself; /* This node */ 107 uint64_t currentEpoch; 108 int state; /* CLUSTER_OK, CLUSTER_FAIL, ... */ 109 int size; /* Num of master nodes with at least one slot */ 110 dict *nodes; /* Hash table of name -> clusterNode structures */ 111 dict *nodes_black_list; /* Nodes we don't re-add for a few seconds. */ 112 clusterNode *migrating_slots_to[CLUSTER_SLOTS]; 113 clusterNode *importing_slots_from[CLUSTER_SLOTS]; 114 clusterNode *slots[CLUSTER_SLOTS]; 115 zskiplist *slots_to_keys; 116 /* The following fields are used to take the slave state on elections. */ 117 mstime_t failover_auth_time; /* Time of previous or next election. */ 118 int failover_auth_count; /* Number of votes received so far. */ 119 int failover_auth_sent; /* True if we already asked for votes. */ 120 int failover_auth_rank; /* This slave rank for current auth request. */ 121 uint64_t failover_auth_epoch; /* Epoch of the current election. */ 122 int cant_failover_reason; /* Why a slave is currently not able to 123 failover. See the CANT_FAILOVER_* macros. */ 124 /* Manual failover state in common. */ 125 mstime_t mf_end; /* Manual failover time limit (ms unixtime). 126 It is zero if there is no MF in progress. */ 127 /* Manual failover state of master. */ 128 clusterNode *mf_slave; /* Slave performing the manual failover. */ 129 /* Manual failover state of slave. */ 130 long long mf_master_offset; /* Master offset the slave needs to start MF 131 or zero if stil not received. */ 132 int mf_can_start; /* If non-zero signal that the manual failover 133 can start requesting masters vote. */ 134 /* The followign fields are used by masters to take state on elections. */ 135 uint64_t lastVoteEpoch; /* Epoch of the last vote granted. */ 136 int todo_before_sleep; /* Things to do in clusterBeforeSleep(). */ 137 long long stats_bus_messages_sent; /* Num of msg sent via cluster bus. */ 138 long long stats_bus_messages_received; /* Num of msg rcvd via cluster bus.*/ 139 } clusterState; 140 141 /* clusterState todo_before_sleep flags. */ 142 #define CLUSTER_TODO_HANDLE_FAILOVER (1<<0) 143 #define CLUSTER_TODO_UPDATE_STATE (1<<1) 144 #define CLUSTER_TODO_SAVE_CONFIG (1<<2) 145 #define CLUSTER_TODO_FSYNC_CONFIG (1<<3) 146 147 /* Redis cluster messages header */ 148 149 /* Note that the PING, PONG and MEET messages are actually the same exact 150 * kind of packet. PONG is the reply to ping, in the exact format as a PING, 151 * while MEET is a special PING that forces the receiver to add the sender 152 * as a node (if it is not already in the list). */ 153 #define CLUSTERMSG_TYPE_PING 0 /* Ping */ 154 #define CLUSTERMSG_TYPE_PONG 1 /* Pong (reply to Ping) */ 155 #define CLUSTERMSG_TYPE_MEET 2 /* Meet "let's join" message */ 156 #define CLUSTERMSG_TYPE_FAIL 3 /* Mark node xxx as failing */ 157 #define CLUSTERMSG_TYPE_PUBLISH 4 /* Pub/Sub Publish propagation */ 158 #define CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST 5 /* May I failover? */ 159 #define CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK 6 /* Yes, you have my vote */ 160 #define CLUSTERMSG_TYPE_UPDATE 7 /* Another node slots configuration */ 161 #define CLUSTERMSG_TYPE_MFSTART 8 /* Pause clients for manual failover */ 162 163 /* Initially we don't know our "name", but we'll find it once we connect 164 * to the first node, using the getsockname() function. Then we'll use this 165 * address for all the next messages. */ 166 typedef struct { 167 char nodename[CLUSTER_NAMELEN]; 168 uint32_t ping_sent; 169 uint32_t pong_received; 170 char ip[NET_IP_STR_LEN]; /* IP address last time it was seen */ 171 uint16_t port; /* port last time it was seen */ 172 uint16_t flags; /* node->flags copy */ 173 uint16_t notused1; /* Some room for future improvements. */ 174 uint32_t notused2; 175 } clusterMsgDataGossip; 176 177 typedef struct { 178 char nodename[CLUSTER_NAMELEN]; 179 } clusterMsgDataFail; 180 181 typedef struct { 182 uint32_t channel_len; 183 uint32_t message_len; 184 /* We can't reclare bulk_data as bulk_data[] since this structure is 185 * nested. The 8 bytes are removed from the count during the message 186 * length computation. */ 187 unsigned char bulk_data[8]; 188 } clusterMsgDataPublish; 189 190 typedef struct { 191 uint64_t configEpoch; /* Config epoch of the specified instance. */ 192 char nodename[CLUSTER_NAMELEN]; /* Name of the slots owner. */ 193 unsigned char slots[CLUSTER_SLOTS/8]; /* Slots bitmap. */ 194 } clusterMsgDataUpdate; 195 196 union clusterMsgData { 197 /* PING, MEET and PONG */ 198 struct { 199 /* Array of N clusterMsgDataGossip structures */ 200 clusterMsgDataGossip gossip[1]; 201 } ping; 202 203 /* FAIL */ 204 struct { 205 clusterMsgDataFail about; 206 } fail; 207 208 /* PUBLISH */ 209 struct { 210 clusterMsgDataPublish msg; 211 } publish; 212 213 /* UPDATE */ 214 struct { 215 clusterMsgDataUpdate nodecfg; 216 } update; 217 }; 218 219 #define CLUSTER_PROTO_VER 0 /* Cluster bus protocol version. */ 220 221 typedef struct { 222 char sig[4]; /* Siganture "RCmb" (Redis Cluster message bus). */ 223 uint32_t totlen; /* Total length of this message */ 224 uint16_t ver; /* Protocol version, currently set to 0. */ 225 uint16_t notused0; /* 2 bytes not used. */ 226 uint16_t type; /* Message type */ 227 uint16_t count; /* Only used for some kind of messages. */ 228 uint64_t currentEpoch; /* The epoch accordingly to the sending node. */ 229 uint64_t configEpoch; /* The config epoch if it's a master, or the last 230 epoch advertised by its master if it is a 231 slave. */ 232 uint64_t offset; /* Master replication offset if node is a master or 233 processed replication offset if node is a slave. */ 234 char sender[CLUSTER_NAMELEN]; /* Name of the sender node */ 235 unsigned char myslots[CLUSTER_SLOTS/8]; 236 char slaveof[CLUSTER_NAMELEN]; 237 char notused1[32]; /* 32 bytes reserved for future usage. */ 238 uint16_t port; /* Sender TCP base port */ 239 uint16_t flags; /* Sender node flags */ 240 unsigned char state; /* Cluster state from the POV of the sender */ 241 unsigned char mflags[3]; /* Message flags: CLUSTERMSG_FLAG[012]_... */ 242 union clusterMsgData data; 243 } clusterMsg; 244 245 #define CLUSTERMSG_MIN_LEN (sizeof(clusterMsg)-sizeof(union clusterMsgData)) 246 247 /* Message flags better specify the packet content or are used to 248 * provide some information about the node state. */ 249 #define CLUSTERMSG_FLAG0_PAUSED (1<<0) /* Master paused for manual failover. */ 250 #define CLUSTERMSG_FLAG0_FORCEACK (1<<1) /* Give ACK to AUTH_REQUEST even if 251 master is up. */ 252 253 /* ---------------------- API exported outside cluster.c -------------------- */ 254 clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask); 255 int clusterRedirectBlockedClientIfNeeded(client *c); 256 void clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_code); 257 258 #endif /* __CLUSTER_H */ 259