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