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. Note that it 92 may be NULL even if the node is a slave 93 if we don't have the master node in our 94 tables. */ 95 mstime_t ping_sent; /* Unix time we sent latest ping */ 96 mstime_t pong_received; /* Unix time we received the pong */ 97 mstime_t fail_time; /* Unix time when FAIL flag was set */ 98 mstime_t voted_time; /* Last time we voted for a slave of this master */ 99 mstime_t repl_offset_time; /* Unix time we received offset for this node */ 100 mstime_t orphaned_time; /* Starting time of orphaned master condition */ 101 long long repl_offset; /* Last known repl offset for this node. */ 102 char ip[NET_IP_STR_LEN]; /* Latest known IP address of this node */ 103 int port; /* Latest known port of this node */ 104 clusterLink *link; /* TCP/IP link with this node */ 105 list *fail_reports; /* List of nodes signaling this as failing */ 106 } clusterNode; 107 108 typedef struct clusterState { 109 clusterNode *myself; /* This node */ 110 uint64_t currentEpoch; 111 int state; /* CLUSTER_OK, CLUSTER_FAIL, ... */ 112 int size; /* Num of master nodes with at least one slot */ 113 dict *nodes; /* Hash table of name -> clusterNode structures */ 114 dict *nodes_black_list; /* Nodes we don't re-add for a few seconds. */ 115 clusterNode *migrating_slots_to[CLUSTER_SLOTS]; 116 clusterNode *importing_slots_from[CLUSTER_SLOTS]; 117 clusterNode *slots[CLUSTER_SLOTS]; 118 zskiplist *slots_to_keys; 119 /* The following fields are used to take the slave state on elections. */ 120 mstime_t failover_auth_time; /* Time of previous or next election. */ 121 int failover_auth_count; /* Number of votes received so far. */ 122 int failover_auth_sent; /* True if we already asked for votes. */ 123 int failover_auth_rank; /* This slave rank for current auth request. */ 124 uint64_t failover_auth_epoch; /* Epoch of the current election. */ 125 int cant_failover_reason; /* Why a slave is currently not able to 126 failover. See the CANT_FAILOVER_* macros. */ 127 /* Manual failover state in common. */ 128 mstime_t mf_end; /* Manual failover time limit (ms unixtime). 129 It is zero if there is no MF in progress. */ 130 /* Manual failover state of master. */ 131 clusterNode *mf_slave; /* Slave performing the manual failover. */ 132 /* Manual failover state of slave. */ 133 long long mf_master_offset; /* Master offset the slave needs to start MF 134 or zero if stil not received. */ 135 int mf_can_start; /* If non-zero signal that the manual failover 136 can start requesting masters vote. */ 137 /* The followign fields are used by masters to take state on elections. */ 138 uint64_t lastVoteEpoch; /* Epoch of the last vote granted. */ 139 int todo_before_sleep; /* Things to do in clusterBeforeSleep(). */ 140 long long stats_bus_messages_sent; /* Num of msg sent via cluster bus. */ 141 long long stats_bus_messages_received; /* Num of msg rcvd via cluster bus.*/ 142 } clusterState; 143 144 /* clusterState todo_before_sleep flags. */ 145 #define CLUSTER_TODO_HANDLE_FAILOVER (1<<0) 146 #define CLUSTER_TODO_UPDATE_STATE (1<<1) 147 #define CLUSTER_TODO_SAVE_CONFIG (1<<2) 148 #define CLUSTER_TODO_FSYNC_CONFIG (1<<3) 149 150 /* Redis cluster messages header */ 151 152 /* Note that the PING, PONG and MEET messages are actually the same exact 153 * kind of packet. PONG is the reply to ping, in the exact format as a PING, 154 * while MEET is a special PING that forces the receiver to add the sender 155 * as a node (if it is not already in the list). */ 156 #define CLUSTERMSG_TYPE_PING 0 /* Ping */ 157 #define CLUSTERMSG_TYPE_PONG 1 /* Pong (reply to Ping) */ 158 #define CLUSTERMSG_TYPE_MEET 2 /* Meet "let's join" message */ 159 #define CLUSTERMSG_TYPE_FAIL 3 /* Mark node xxx as failing */ 160 #define CLUSTERMSG_TYPE_PUBLISH 4 /* Pub/Sub Publish propagation */ 161 #define CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST 5 /* May I failover? */ 162 #define CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK 6 /* Yes, you have my vote */ 163 #define CLUSTERMSG_TYPE_UPDATE 7 /* Another node slots configuration */ 164 #define CLUSTERMSG_TYPE_MFSTART 8 /* Pause clients for manual failover */ 165 166 /* Initially we don't know our "name", but we'll find it once we connect 167 * to the first node, using the getsockname() function. Then we'll use this 168 * address for all the next messages. */ 169 typedef struct { 170 char nodename[CLUSTER_NAMELEN]; 171 uint32_t ping_sent; 172 uint32_t pong_received; 173 char ip[NET_IP_STR_LEN]; /* IP address last time it was seen */ 174 uint16_t port; /* port last time it was seen */ 175 uint16_t flags; /* node->flags copy */ 176 uint16_t notused1; /* Some room for future improvements. */ 177 uint32_t notused2; 178 } clusterMsgDataGossip; 179 180 typedef struct { 181 char nodename[CLUSTER_NAMELEN]; 182 } clusterMsgDataFail; 183 184 typedef struct { 185 uint32_t channel_len; 186 uint32_t message_len; 187 /* We can't reclare bulk_data as bulk_data[] since this structure is 188 * nested. The 8 bytes are removed from the count during the message 189 * length computation. */ 190 unsigned char bulk_data[8]; 191 } clusterMsgDataPublish; 192 193 typedef struct { 194 uint64_t configEpoch; /* Config epoch of the specified instance. */ 195 char nodename[CLUSTER_NAMELEN]; /* Name of the slots owner. */ 196 unsigned char slots[CLUSTER_SLOTS/8]; /* Slots bitmap. */ 197 } clusterMsgDataUpdate; 198 199 union clusterMsgData { 200 /* PING, MEET and PONG */ 201 struct { 202 /* Array of N clusterMsgDataGossip structures */ 203 clusterMsgDataGossip gossip[1]; 204 } ping; 205 206 /* FAIL */ 207 struct { 208 clusterMsgDataFail about; 209 } fail; 210 211 /* PUBLISH */ 212 struct { 213 clusterMsgDataPublish msg; 214 } publish; 215 216 /* UPDATE */ 217 struct { 218 clusterMsgDataUpdate nodecfg; 219 } update; 220 }; 221 222 #define CLUSTER_PROTO_VER 0 /* Cluster bus protocol version. */ 223 224 typedef struct { 225 char sig[4]; /* Siganture "RCmb" (Redis Cluster message bus). */ 226 uint32_t totlen; /* Total length of this message */ 227 uint16_t ver; /* Protocol version, currently set to 0. */ 228 uint16_t notused0; /* 2 bytes not used. */ 229 uint16_t type; /* Message type */ 230 uint16_t count; /* Only used for some kind of messages. */ 231 uint64_t currentEpoch; /* The epoch accordingly to the sending node. */ 232 uint64_t configEpoch; /* The config epoch if it's a master, or the last 233 epoch advertised by its master if it is a 234 slave. */ 235 uint64_t offset; /* Master replication offset if node is a master or 236 processed replication offset if node is a slave. */ 237 char sender[CLUSTER_NAMELEN]; /* Name of the sender node */ 238 unsigned char myslots[CLUSTER_SLOTS/8]; 239 char slaveof[CLUSTER_NAMELEN]; 240 char notused1[32]; /* 32 bytes reserved for future usage. */ 241 uint16_t port; /* Sender TCP base port */ 242 uint16_t flags; /* Sender node flags */ 243 unsigned char state; /* Cluster state from the POV of the sender */ 244 unsigned char mflags[3]; /* Message flags: CLUSTERMSG_FLAG[012]_... */ 245 union clusterMsgData data; 246 } clusterMsg; 247 248 #define CLUSTERMSG_MIN_LEN (sizeof(clusterMsg)-sizeof(union clusterMsgData)) 249 250 /* Message flags better specify the packet content or are used to 251 * provide some information about the node state. */ 252 #define CLUSTERMSG_FLAG0_PAUSED (1<<0) /* Master paused for manual failover. */ 253 #define CLUSTERMSG_FLAG0_FORCEACK (1<<1) /* Give ACK to AUTH_REQUEST even if 254 master is up. */ 255 256 /* ---------------------- API exported outside cluster.c -------------------- */ 257 clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask); 258 int clusterRedirectBlockedClientIfNeeded(client *c); 259 void clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_code); 260 261 #endif /* __CLUSTER_H */ 262