]>
Commit | Line | Data |
---|---|---|
e2641e09 | 1 | #include "redis.h" |
2 | ||
3 | #include <sys/time.h> | |
4 | #include <unistd.h> | |
5 | #include <fcntl.h> | |
6 | #include <sys/stat.h> | |
7 | ||
f4aa600b | 8 | /* ---------------------------------- MASTER -------------------------------- */ |
9 | ||
e2641e09 | 10 | void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) { |
11 | listNode *ln; | |
12 | listIter li; | |
632e4c09 | 13 | int j; |
e2641e09 | 14 | |
e2641e09 | 15 | listRewind(slaves,&li); |
16 | while((ln = listNext(&li))) { | |
17 | redisClient *slave = ln->value; | |
18 | ||
19 | /* Don't feed slaves that are still waiting for BGSAVE to start */ | |
20 | if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue; | |
21 | ||
632e4c09 PN |
22 | /* Feed slaves that are waiting for the initial SYNC (so these commands |
23 | * are queued in the output buffer until the intial SYNC completes), | |
24 | * or are already in sync with the master. */ | |
e2641e09 | 25 | if (slave->slaveseldb != dictid) { |
26 | robj *selectcmd; | |
27 | ||
f892797e | 28 | if (dictid >= 0 && dictid < REDIS_SHARED_SELECT_CMDS) { |
f892797e | 29 | selectcmd = shared.select[dictid]; |
c2672a06 | 30 | incrRefCount(selectcmd); |
f892797e | 31 | } else { |
e2641e09 | 32 | selectcmd = createObject(REDIS_STRING, |
33 | sdscatprintf(sdsempty(),"select %d\r\n",dictid)); | |
e2641e09 | 34 | } |
35 | addReply(slave,selectcmd); | |
f892797e | 36 | decrRefCount(selectcmd); |
e2641e09 | 37 | slave->slaveseldb = dictid; |
38 | } | |
632e4c09 PN |
39 | addReplyMultiBulkLen(slave,argc); |
40 | for (j = 0; j < argc; j++) addReplyBulk(slave,argv[j]); | |
e2641e09 | 41 | } |
e2641e09 | 42 | } |
43 | ||
e31b615e | 44 | void replicationFeedMonitors(redisClient *c, list *monitors, int dictid, robj **argv, int argc) { |
e2641e09 | 45 | listNode *ln; |
46 | listIter li; | |
e31b615e | 47 | int j, port; |
e2641e09 | 48 | sds cmdrepr = sdsnew("+"); |
49 | robj *cmdobj; | |
e31b615e | 50 | char ip[32]; |
e2641e09 | 51 | struct timeval tv; |
52 | ||
53 | gettimeofday(&tv,NULL); | |
2b2eca1f | 54 | cmdrepr = sdscatprintf(cmdrepr,"%ld.%06ld ",(long)tv.tv_sec,(long)tv.tv_usec); |
e31b615e | 55 | if (c->flags & REDIS_LUA_CLIENT) { |
56 | cmdrepr = sdscatprintf(cmdrepr,"[%d lua] ", dictid); | |
57 | } else { | |
58 | anetPeerToString(c->fd,ip,&port); | |
59 | cmdrepr = sdscatprintf(cmdrepr,"[%d %s:%d] ", dictid,ip,port); | |
60 | } | |
e2641e09 | 61 | |
62 | for (j = 0; j < argc; j++) { | |
63 | if (argv[j]->encoding == REDIS_ENCODING_INT) { | |
d3b958c3 | 64 | cmdrepr = sdscatprintf(cmdrepr, "\"%ld\"", (long)argv[j]->ptr); |
e2641e09 | 65 | } else { |
66 | cmdrepr = sdscatrepr(cmdrepr,(char*)argv[j]->ptr, | |
67 | sdslen(argv[j]->ptr)); | |
68 | } | |
69 | if (j != argc-1) | |
70 | cmdrepr = sdscatlen(cmdrepr," ",1); | |
71 | } | |
72 | cmdrepr = sdscatlen(cmdrepr,"\r\n",2); | |
73 | cmdobj = createObject(REDIS_STRING,cmdrepr); | |
74 | ||
75 | listRewind(monitors,&li); | |
76 | while((ln = listNext(&li))) { | |
77 | redisClient *monitor = ln->value; | |
78 | addReply(monitor,cmdobj); | |
79 | } | |
80 | decrRefCount(cmdobj); | |
81 | } | |
82 | ||
e2641e09 | 83 | void syncCommand(redisClient *c) { |
84 | /* ignore SYNC if aleady slave or in monitor mode */ | |
85 | if (c->flags & REDIS_SLAVE) return; | |
86 | ||
778b2210 | 87 | /* Refuse SYNC requests if we are a slave but the link with our master |
88 | * is not ok... */ | |
1844f990 | 89 | if (server.masterhost && server.repl_state != REDIS_REPL_CONNECTED) { |
3ab20376 | 90 | addReplyError(c,"Can't SYNC while not connected with my master"); |
778b2210 | 91 | return; |
92 | } | |
93 | ||
e2641e09 | 94 | /* SYNC can't be issued when the server has pending data to send to |
95 | * the client about already issued commands. We need a fresh reply | |
96 | * buffer registering the differences between the BGSAVE and the current | |
97 | * dataset, so that we can copy to other slaves if needed. */ | |
98 | if (listLength(c->reply) != 0) { | |
3ab20376 | 99 | addReplyError(c,"SYNC is invalid with pending input"); |
e2641e09 | 100 | return; |
101 | } | |
102 | ||
103 | redisLog(REDIS_NOTICE,"Slave ask for synchronization"); | |
104 | /* Here we need to check if there is a background saving operation | |
105 | * in progress, or if it is required to start one */ | |
f48cd4b9 | 106 | if (server.rdb_child_pid != -1) { |
e2641e09 | 107 | /* Ok a background save is in progress. Let's check if it is a good |
108 | * one for replication, i.e. if there is another slave that is | |
109 | * registering differences since the server forked to save */ | |
110 | redisClient *slave; | |
111 | listNode *ln; | |
112 | listIter li; | |
113 | ||
114 | listRewind(server.slaves,&li); | |
115 | while((ln = listNext(&li))) { | |
116 | slave = ln->value; | |
117 | if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break; | |
118 | } | |
119 | if (ln) { | |
120 | /* Perfect, the server is already registering differences for | |
121 | * another slave. Set the right state, and copy the buffer. */ | |
1824e3a3 | 122 | copyClientOutputBuffer(c,slave); |
e2641e09 | 123 | c->replstate = REDIS_REPL_WAIT_BGSAVE_END; |
124 | redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC"); | |
125 | } else { | |
126 | /* No way, we need to wait for the next BGSAVE in order to | |
127 | * register differences */ | |
128 | c->replstate = REDIS_REPL_WAIT_BGSAVE_START; | |
129 | redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC"); | |
130 | } | |
131 | } else { | |
132 | /* Ok we don't have a BGSAVE in progress, let's start one */ | |
133 | redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC"); | |
f48cd4b9 | 134 | if (rdbSaveBackground(server.rdb_filename) != REDIS_OK) { |
e2641e09 | 135 | redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE"); |
3ab20376 | 136 | addReplyError(c,"Unable to perform background save"); |
e2641e09 | 137 | return; |
138 | } | |
139 | c->replstate = REDIS_REPL_WAIT_BGSAVE_END; | |
140 | } | |
141 | c->repldbfd = -1; | |
142 | c->flags |= REDIS_SLAVE; | |
143 | c->slaveseldb = 0; | |
144 | listAddNodeTail(server.slaves,c); | |
145 | return; | |
146 | } | |
147 | ||
3a328978 | 148 | /* REPLCONF <option> <value> <option> <value> ... |
149 | * This command is used by a slave in order to configure the replication | |
150 | * process before starting it with the SYNC command. | |
151 | * | |
152 | * Currently the only use of this command is to communicate to the master | |
153 | * what is the listening port of the Slave redis instance, so that the | |
154 | * master can accurately list slaves and their listening ports in | |
155 | * the INFO output. | |
156 | * | |
157 | * In the future the same command can be used in order to configure | |
158 | * the replication to initiate an incremental replication instead of a | |
159 | * full resync. */ | |
160 | void replconfCommand(redisClient *c) { | |
161 | int j; | |
162 | ||
163 | if ((c->argc % 2) == 0) { | |
164 | /* Number of arguments must be odd to make sure that every | |
165 | * option has a corresponding value. */ | |
166 | addReply(c,shared.syntaxerr); | |
167 | return; | |
168 | } | |
169 | ||
170 | /* Process every option-value pair. */ | |
171 | for (j = 1; j < c->argc; j+=2) { | |
172 | if (!strcasecmp(c->argv[j]->ptr,"listening-port")) { | |
173 | long port; | |
174 | ||
175 | if ((getLongFromObjectOrReply(c,c->argv[j+1], | |
176 | &port,NULL) != REDIS_OK)) | |
177 | return; | |
178 | c->slave_listening_port = port; | |
179 | } else { | |
180 | addReplyErrorFormat(c,"Unrecognized REPLCONF option: %s", | |
181 | (char*)c->argv[j]->ptr); | |
182 | return; | |
183 | } | |
184 | } | |
185 | addReply(c,shared.ok); | |
186 | } | |
187 | ||
e2641e09 | 188 | void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) { |
189 | redisClient *slave = privdata; | |
190 | REDIS_NOTUSED(el); | |
191 | REDIS_NOTUSED(mask); | |
192 | char buf[REDIS_IOBUF_LEN]; | |
193 | ssize_t nwritten, buflen; | |
194 | ||
195 | if (slave->repldboff == 0) { | |
196 | /* Write the bulk write count before to transfer the DB. In theory here | |
197 | * we don't know how much room there is in the output buffer of the | |
198 | * socket, but in pratice SO_SNDLOWAT (the minimum count for output | |
199 | * operations) will never be smaller than the few bytes we need. */ | |
200 | sds bulkcount; | |
201 | ||
202 | bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long) | |
203 | slave->repldbsize); | |
204 | if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount)) | |
205 | { | |
206 | sdsfree(bulkcount); | |
207 | freeClient(slave); | |
208 | return; | |
209 | } | |
210 | sdsfree(bulkcount); | |
211 | } | |
212 | lseek(slave->repldbfd,slave->repldboff,SEEK_SET); | |
213 | buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN); | |
214 | if (buflen <= 0) { | |
215 | redisLog(REDIS_WARNING,"Read error sending DB to slave: %s", | |
216 | (buflen == 0) ? "premature EOF" : strerror(errno)); | |
217 | freeClient(slave); | |
218 | return; | |
219 | } | |
220 | if ((nwritten = write(fd,buf,buflen)) == -1) { | |
221 | redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s", | |
222 | strerror(errno)); | |
223 | freeClient(slave); | |
224 | return; | |
225 | } | |
226 | slave->repldboff += nwritten; | |
227 | if (slave->repldboff == slave->repldbsize) { | |
228 | close(slave->repldbfd); | |
229 | slave->repldbfd = -1; | |
230 | aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE); | |
231 | slave->replstate = REDIS_REPL_ONLINE; | |
232 | if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, | |
233 | sendReplyToClient, slave) == AE_ERR) { | |
234 | freeClient(slave); | |
235 | return; | |
236 | } | |
e2641e09 | 237 | redisLog(REDIS_NOTICE,"Synchronization with slave succeeded"); |
238 | } | |
239 | } | |
240 | ||
241 | /* This function is called at the end of every backgrond saving. | |
242 | * The argument bgsaveerr is REDIS_OK if the background saving succeeded | |
243 | * otherwise REDIS_ERR is passed to the function. | |
244 | * | |
245 | * The goal of this function is to handle slaves waiting for a successful | |
246 | * background saving in order to perform non-blocking synchronization. */ | |
247 | void updateSlavesWaitingBgsave(int bgsaveerr) { | |
248 | listNode *ln; | |
249 | int startbgsave = 0; | |
250 | listIter li; | |
251 | ||
252 | listRewind(server.slaves,&li); | |
253 | while((ln = listNext(&li))) { | |
254 | redisClient *slave = ln->value; | |
255 | ||
256 | if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) { | |
257 | startbgsave = 1; | |
258 | slave->replstate = REDIS_REPL_WAIT_BGSAVE_END; | |
259 | } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) { | |
260 | struct redis_stat buf; | |
261 | ||
262 | if (bgsaveerr != REDIS_OK) { | |
263 | freeClient(slave); | |
264 | redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error"); | |
265 | continue; | |
266 | } | |
f48cd4b9 | 267 | if ((slave->repldbfd = open(server.rdb_filename,O_RDONLY)) == -1 || |
e2641e09 | 268 | redis_fstat(slave->repldbfd,&buf) == -1) { |
269 | freeClient(slave); | |
270 | redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno)); | |
271 | continue; | |
272 | } | |
273 | slave->repldboff = 0; | |
274 | slave->repldbsize = buf.st_size; | |
275 | slave->replstate = REDIS_REPL_SEND_BULK; | |
276 | aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE); | |
277 | if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) { | |
278 | freeClient(slave); | |
279 | continue; | |
280 | } | |
281 | } | |
282 | } | |
283 | if (startbgsave) { | |
f48cd4b9 | 284 | if (rdbSaveBackground(server.rdb_filename) != REDIS_OK) { |
e2641e09 | 285 | listIter li; |
286 | ||
287 | listRewind(server.slaves,&li); | |
288 | redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed"); | |
289 | while((ln = listNext(&li))) { | |
290 | redisClient *slave = ln->value; | |
291 | ||
292 | if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) | |
293 | freeClient(slave); | |
294 | } | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
f4aa600b | 299 | /* ----------------------------------- SLAVE -------------------------------- */ |
300 | ||
301 | /* Abort the async download of the bulk dataset while SYNC-ing with master */ | |
302 | void replicationAbortSyncTransfer(void) { | |
1844f990 | 303 | redisAssert(server.repl_state == REDIS_REPL_TRANSFER); |
f4aa600b | 304 | |
305 | aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE); | |
306 | close(server.repl_transfer_s); | |
307 | close(server.repl_transfer_fd); | |
308 | unlink(server.repl_transfer_tmpfile); | |
309 | zfree(server.repl_transfer_tmpfile); | |
1844f990 | 310 | server.repl_state = REDIS_REPL_CONNECT; |
f4aa600b | 311 | } |
312 | ||
313 | /* Asynchronously read the SYNC payload we receive from a master */ | |
784b9308 | 314 | #define REPL_MAX_WRITTEN_BEFORE_FSYNC (1024*1024*8) /* 8 MB */ |
f4aa600b | 315 | void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) { |
26b33669 | 316 | char buf[4096]; |
62ec599c | 317 | ssize_t nread, readlen; |
784b9308 | 318 | off_t left; |
62ec599c | 319 | REDIS_NOTUSED(el); |
320 | REDIS_NOTUSED(privdata); | |
321 | REDIS_NOTUSED(mask); | |
f4aa600b | 322 | |
784b9308 | 323 | /* If repl_transfer_size == -1 we still have to read the bulk length |
26b33669 | 324 | * from the master reply. */ |
784b9308 | 325 | if (server.repl_transfer_size == -1) { |
9157549f | 326 | if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) { |
26b33669 | 327 | redisLog(REDIS_WARNING, |
328 | "I/O error reading bulk count from MASTER: %s", | |
329 | strerror(errno)); | |
b075621f | 330 | goto error; |
26b33669 | 331 | } |
b075621f | 332 | |
26b33669 | 333 | if (buf[0] == '-') { |
334 | redisLog(REDIS_WARNING, | |
335 | "MASTER aborted replication with an error: %s", | |
336 | buf+1); | |
b075621f | 337 | goto error; |
89a1433e | 338 | } else if (buf[0] == '\0') { |
339 | /* At this stage just a newline works as a PING in order to take | |
340 | * the connection live. So we refresh our last interaction | |
341 | * timestamp. */ | |
d1949054 | 342 | server.repl_transfer_lastio = server.unixtime; |
89a1433e | 343 | return; |
26b33669 | 344 | } else if (buf[0] != '$') { |
345 | redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?"); | |
b075621f | 346 | goto error; |
26b33669 | 347 | } |
784b9308 | 348 | server.repl_transfer_size = strtol(buf+1,NULL,10); |
26b33669 | 349 | redisLog(REDIS_NOTICE, |
350 | "MASTER <-> SLAVE sync: receiving %ld bytes from master", | |
784b9308 | 351 | server.repl_transfer_size); |
26b33669 | 352 | return; |
353 | } | |
354 | ||
355 | /* Read bulk data */ | |
784b9308 | 356 | left = server.repl_transfer_size - server.repl_transfer_read; |
357 | readlen = (left < (signed)sizeof(buf)) ? left : (signed)sizeof(buf); | |
f4aa600b | 358 | nread = read(fd,buf,readlen); |
359 | if (nread <= 0) { | |
360 | redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s", | |
361 | (nread == -1) ? strerror(errno) : "connection lost"); | |
362 | replicationAbortSyncTransfer(); | |
363 | return; | |
364 | } | |
d1949054 | 365 | server.repl_transfer_lastio = server.unixtime; |
f4aa600b | 366 | if (write(server.repl_transfer_fd,buf,nread) != nread) { |
31788f50 | 367 | redisLog(REDIS_WARNING,"Write error or short write writing to the DB dump file needed for MASTER <-> SLAVE synchronization: %s", strerror(errno)); |
b075621f | 368 | goto error; |
f4aa600b | 369 | } |
784b9308 | 370 | server.repl_transfer_read += nread; |
371 | ||
372 | /* Sync data on disk from time to time, otherwise at the end of the transfer | |
373 | * we may suffer a big delay as the memory buffers are copied into the | |
374 | * actual disk. */ | |
375 | if (server.repl_transfer_read >= | |
376 | server.repl_transfer_last_fsync_off + REPL_MAX_WRITTEN_BEFORE_FSYNC) | |
377 | { | |
378 | off_t sync_size = server.repl_transfer_read - | |
379 | server.repl_transfer_last_fsync_off; | |
380 | rdb_fsync_range(server.repl_transfer_fd, | |
381 | server.repl_transfer_last_fsync_off, sync_size); | |
382 | server.repl_transfer_last_fsync_off += sync_size; | |
383 | } | |
384 | ||
f4aa600b | 385 | /* Check if the transfer is now complete */ |
784b9308 | 386 | if (server.repl_transfer_read == server.repl_transfer_size) { |
f48cd4b9 | 387 | if (rename(server.repl_transfer_tmpfile,server.rdb_filename) == -1) { |
f4aa600b | 388 | redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno)); |
389 | replicationAbortSyncTransfer(); | |
390 | return; | |
391 | } | |
f6433915 | 392 | redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Loading DB in memory"); |
f4aa600b | 393 | emptyDb(); |
9fd01051 | 394 | /* Before loading the DB into memory we need to delete the readable |
395 | * handler, otherwise it will get called recursively since | |
396 | * rdbLoad() will call the event loop to process events from time to | |
397 | * time for non blocking loading. */ | |
398 | aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE); | |
f48cd4b9 | 399 | if (rdbLoad(server.rdb_filename) != REDIS_OK) { |
f4aa600b | 400 | redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk"); |
401 | replicationAbortSyncTransfer(); | |
402 | return; | |
403 | } | |
404 | /* Final setup of the connected slave <- master link */ | |
f4aa600b | 405 | zfree(server.repl_transfer_tmpfile); |
406 | close(server.repl_transfer_fd); | |
407 | server.master = createClient(server.repl_transfer_s); | |
408 | server.master->flags |= REDIS_MASTER; | |
409 | server.master->authenticated = 1; | |
1844f990 | 410 | server.repl_state = REDIS_REPL_CONNECTED; |
26b33669 | 411 | redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Finished with success"); |
e7a2e7c1 | 412 | /* Restart the AOF subsystem now that we finished the sync. This |
413 | * will trigger an AOF rewrite, and when done will start appending | |
414 | * to the new file. */ | |
e394114d | 415 | if (server.aof_state != REDIS_AOF_OFF) { |
e7a2e7c1 | 416 | int retry = 10; |
417 | ||
418 | stopAppendOnly(); | |
419 | while (retry-- && startAppendOnly() == REDIS_ERR) { | |
420 | redisLog(REDIS_WARNING,"Failed enabling the AOF after successful master synchrnization! Trying it again in one second."); | |
421 | sleep(1); | |
422 | } | |
423 | if (!retry) { | |
424 | redisLog(REDIS_WARNING,"FATAL: this slave instance finished the synchronization with its master, but the AOF can't be turned on. Exiting now."); | |
425 | exit(1); | |
426 | } | |
427 | } | |
f4aa600b | 428 | } |
b075621f PN |
429 | |
430 | return; | |
431 | ||
432 | error: | |
433 | replicationAbortSyncTransfer(); | |
434 | return; | |
f4aa600b | 435 | } |
436 | ||
3a328978 | 437 | /* Send a synchronous command to the master. Used to send AUTH and |
36def8fd | 438 | * REPLCONF commands before starting the replication with SYNC. |
3a328978 | 439 | * |
440 | * On success NULL is returned. | |
441 | * On error an sds string describing the error is returned. | |
442 | */ | |
443 | char *sendSynchronousCommand(int fd, ...) { | |
444 | va_list ap; | |
445 | sds cmd = sdsempty(); | |
446 | char *arg, buf[256]; | |
447 | ||
448 | /* Create the command to send to the master, we use simple inline | |
449 | * protocol for simplicity as currently we only send simple strings. */ | |
450 | va_start(ap,fd); | |
451 | while(1) { | |
452 | arg = va_arg(ap, char*); | |
453 | if (arg == NULL) break; | |
454 | ||
455 | if (sdslen(cmd) != 0) cmd = sdscatlen(cmd," ",1); | |
456 | cmd = sdscat(cmd,arg); | |
457 | } | |
458 | cmd = sdscatlen(cmd,"\r\n",2); | |
459 | ||
460 | /* Transfer command to the server. */ | |
461 | if (syncWrite(fd,cmd,sdslen(cmd),server.repl_syncio_timeout*1000) == -1) { | |
462 | sdsfree(cmd); | |
463 | return sdscatprintf(sdsempty(),"Writing to master: %s", | |
464 | strerror(errno)); | |
465 | } | |
466 | sdsfree(cmd); | |
467 | ||
468 | /* Read the reply from the server. */ | |
469 | if (syncReadLine(fd,buf,sizeof(buf),server.repl_syncio_timeout*1000) == -1) | |
470 | { | |
471 | return sdscatprintf(sdsempty(),"Reading from master: %s", | |
472 | strerror(errno)); | |
473 | } | |
474 | ||
475 | /* Check for errors from the server. */ | |
476 | if (buf[0] != '+') { | |
477 | return sdscatprintf(sdsempty(),"Error from master: %s", buf); | |
478 | } | |
479 | ||
480 | return NULL; /* No errors. */ | |
481 | } | |
482 | ||
a3309139 | 483 | void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) { |
3a328978 | 484 | char tmpfile[256], *err; |
e2641e09 | 485 | int dfd, maxtries = 5; |
bb66fc31 | 486 | int sockerr = 0; |
487 | socklen_t errlen = sizeof(sockerr); | |
a3309139 PN |
488 | REDIS_NOTUSED(el); |
489 | REDIS_NOTUSED(privdata); | |
490 | REDIS_NOTUSED(mask); | |
e2641e09 | 491 | |
76e772f3 | 492 | /* If this event fired after the user turned the instance into a master |
493 | * with SLAVEOF NO ONE we must just return ASAP. */ | |
1844f990 | 494 | if (server.repl_state == REDIS_REPL_NONE) { |
76e772f3 | 495 | close(fd); |
496 | return; | |
497 | } | |
498 | ||
bb66fc31 | 499 | /* Check for errors in the socket. */ |
500 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1) | |
501 | sockerr = errno; | |
502 | if (sockerr) { | |
503 | aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE); | |
504 | redisLog(REDIS_WARNING,"Error condition on socket for SYNC: %s", | |
505 | strerror(sockerr)); | |
506 | goto error; | |
507 | } | |
508 | ||
509 | /* If we were connecting, it's time to send a non blocking PING, we want to | |
510 | * make sure the master is able to reply before going into the actual | |
511 | * replication process where we have long timeouts in the order of | |
512 | * seconds (in the meantime the slave would block). */ | |
513 | if (server.repl_state == REDIS_REPL_CONNECTING) { | |
514 | redisLog(REDIS_NOTICE,"Non blocking connect for SYNC fired the event."); | |
515 | /* Delete the writable event so that the readable event remains | |
516 | * registered and we can wait for the PONG reply. */ | |
517 | aeDeleteFileEvent(server.el,fd,AE_WRITABLE); | |
518 | server.repl_state = REDIS_REPL_RECEIVE_PONG; | |
519 | /* Send the PING, don't check for errors at all, we have the timeout | |
520 | * that will take care about this. */ | |
521 | syncWrite(fd,"PING\r\n",6,100); | |
522 | return; | |
523 | } | |
524 | ||
525 | /* Receive the PONG command. */ | |
526 | if (server.repl_state == REDIS_REPL_RECEIVE_PONG) { | |
527 | char buf[1024]; | |
528 | ||
529 | /* Delete the readable event, we no longer need it now that there is | |
530 | * the PING reply to read. */ | |
531 | aeDeleteFileEvent(server.el,fd,AE_READABLE); | |
532 | ||
533 | /* Read the reply with explicit timeout. */ | |
534 | buf[0] = '\0'; | |
535 | if (syncReadLine(fd,buf,sizeof(buf), | |
536 | server.repl_syncio_timeout*1000) == -1) | |
537 | { | |
538 | redisLog(REDIS_WARNING, | |
539 | "I/O error reading PING reply from master: %s", | |
540 | strerror(errno)); | |
541 | goto error; | |
542 | } | |
543 | ||
544 | /* We don't care about the reply, it can be +PONG or an error since | |
545 | * the server requires AUTH. As long as it replies correctly, it's | |
546 | * fine from our point of view. */ | |
547 | if (buf[0] != '-' && buf[0] != '+') { | |
548 | redisLog(REDIS_WARNING,"Unexpected reply to PING from master."); | |
549 | goto error; | |
550 | } else { | |
551 | redisLog(REDIS_NOTICE, | |
552 | "Master replied to PING, replication can continue..."); | |
553 | } | |
554 | } | |
e2641e09 | 555 | |
556 | /* AUTH with the master if required. */ | |
557 | if(server.masterauth) { | |
3a328978 | 558 | err = sendSynchronousCommand(fd,"AUTH",server.masterauth,NULL); |
559 | if (err) { | |
560 | redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",err); | |
561 | sdsfree(err); | |
a3309139 PN |
562 | goto error; |
563 | } | |
3a328978 | 564 | } |
565 | ||
566 | /* Set the slave port, so that Master's INFO command can list the | |
567 | * slave listening port correctly. */ | |
568 | { | |
569 | sds port = sdsfromlonglong(server.port); | |
570 | err = sendSynchronousCommand(fd,"REPLCONF","listening-port",port, | |
571 | NULL); | |
572 | sdsfree(port); | |
573 | /* Ignore the error if any, not all the Redis versions support | |
574 | * REPLCONF listening-port. */ | |
575 | if (err) { | |
576 | redisLog(REDIS_NOTICE,"(non critical): Master does not understand REPLCONF listening-port: %s", err); | |
577 | sdsfree(err); | |
e2641e09 | 578 | } |
579 | } | |
580 | ||
581 | /* Issue the SYNC command */ | |
299290d3 | 582 | if (syncWrite(fd,"SYNC\r\n",6,server.repl_syncio_timeout*1000) == -1) { |
e2641e09 | 583 | redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s", |
584 | strerror(errno)); | |
a3309139 | 585 | goto error; |
e2641e09 | 586 | } |
26b33669 | 587 | |
588 | /* Prepare a suitable temp file for bulk transfer */ | |
e2641e09 | 589 | while(maxtries--) { |
590 | snprintf(tmpfile,256, | |
d1949054 | 591 | "temp-%d.%ld.rdb",(int)server.unixtime,(long int)getpid()); |
e2641e09 | 592 | dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644); |
593 | if (dfd != -1) break; | |
594 | sleep(1); | |
595 | } | |
596 | if (dfd == -1) { | |
e2641e09 | 597 | redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno)); |
a3309139 | 598 | goto error; |
e2641e09 | 599 | } |
e2641e09 | 600 | |
f4aa600b | 601 | /* Setup the non blocking download of the bulk file. */ |
a3309139 | 602 | if (aeCreateFileEvent(server.el,fd, AE_READABLE,readSyncBulkPayload,NULL) |
62ec599c | 603 | == AE_ERR) |
f4aa600b | 604 | { |
f4aa600b | 605 | redisLog(REDIS_WARNING,"Can't create readable event for SYNC"); |
a3309139 | 606 | goto error; |
e2641e09 | 607 | } |
a3309139 | 608 | |
1844f990 | 609 | server.repl_state = REDIS_REPL_TRANSFER; |
784b9308 | 610 | server.repl_transfer_size = -1; |
611 | server.repl_transfer_read = 0; | |
612 | server.repl_transfer_last_fsync_off = 0; | |
f4aa600b | 613 | server.repl_transfer_fd = dfd; |
d1949054 | 614 | server.repl_transfer_lastio = server.unixtime; |
f4aa600b | 615 | server.repl_transfer_tmpfile = zstrdup(tmpfile); |
a3309139 PN |
616 | return; |
617 | ||
618 | error: | |
a3309139 | 619 | close(fd); |
bb66fc31 | 620 | server.repl_transfer_s = -1; |
621 | server.repl_state = REDIS_REPL_CONNECT; | |
a3309139 PN |
622 | return; |
623 | } | |
624 | ||
625 | int connectWithMaster(void) { | |
626 | int fd; | |
627 | ||
628 | fd = anetTcpNonBlockConnect(NULL,server.masterhost,server.masterport); | |
629 | if (fd == -1) { | |
630 | redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s", | |
631 | strerror(errno)); | |
632 | return REDIS_ERR; | |
633 | } | |
634 | ||
45029d37 | 635 | if (aeCreateFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE,syncWithMaster,NULL) == |
b075621f | 636 | AE_ERR) |
a3309139 PN |
637 | { |
638 | close(fd); | |
639 | redisLog(REDIS_WARNING,"Can't create readable event for SYNC"); | |
640 | return REDIS_ERR; | |
641 | } | |
642 | ||
d1949054 | 643 | server.repl_transfer_lastio = server.unixtime; |
a3309139 | 644 | server.repl_transfer_s = fd; |
1844f990 | 645 | server.repl_state = REDIS_REPL_CONNECTING; |
e2641e09 | 646 | return REDIS_OK; |
647 | } | |
648 | ||
27acd7aa | 649 | /* This function can be called when a non blocking connection is currently |
650 | * in progress to undo it. */ | |
651 | void undoConnectWithMaster(void) { | |
652 | int fd = server.repl_transfer_s; | |
653 | ||
bb66fc31 | 654 | redisAssert(server.repl_state == REDIS_REPL_CONNECTING || |
655 | server.repl_state == REDIS_REPL_RECEIVE_PONG); | |
27acd7aa | 656 | aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE); |
657 | close(fd); | |
658 | server.repl_transfer_s = -1; | |
1844f990 | 659 | server.repl_state = REDIS_REPL_CONNECT; |
27acd7aa | 660 | } |
661 | ||
e2641e09 | 662 | void slaveofCommand(redisClient *c) { |
663 | if (!strcasecmp(c->argv[1]->ptr,"no") && | |
664 | !strcasecmp(c->argv[2]->ptr,"one")) { | |
665 | if (server.masterhost) { | |
666 | sdsfree(server.masterhost); | |
667 | server.masterhost = NULL; | |
668 | if (server.master) freeClient(server.master); | |
1844f990 | 669 | if (server.repl_state == REDIS_REPL_TRANSFER) |
f4aa600b | 670 | replicationAbortSyncTransfer(); |
bb66fc31 | 671 | else if (server.repl_state == REDIS_REPL_CONNECTING || |
672 | server.repl_state == REDIS_REPL_RECEIVE_PONG) | |
27acd7aa | 673 | undoConnectWithMaster(); |
1844f990 | 674 | server.repl_state = REDIS_REPL_NONE; |
e2641e09 | 675 | redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)"); |
676 | } | |
677 | } else { | |
ebdfad69 | 678 | long port; |
679 | ||
680 | if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != REDIS_OK)) | |
681 | return; | |
682 | ||
683 | /* Check if we are already attached to the specified slave */ | |
684 | if (server.masterhost && !strcasecmp(server.masterhost,c->argv[1]->ptr) | |
685 | && server.masterport == port) { | |
686 | redisLog(REDIS_NOTICE,"SLAVE OF would result into synchronization with the master we are already connected with. No operation performed."); | |
687 | addReplySds(c,sdsnew("+OK Already connected to specified master\r\n")); | |
688 | return; | |
689 | } | |
690 | /* There was no previous master or the user specified a different one, | |
691 | * we can continue. */ | |
e2641e09 | 692 | sdsfree(server.masterhost); |
693 | server.masterhost = sdsdup(c->argv[1]->ptr); | |
ebdfad69 | 694 | server.masterport = port; |
e2641e09 | 695 | if (server.master) freeClient(server.master); |
179e54d2 | 696 | disconnectSlaves(); /* Force our slaves to resync with us as well. */ |
1844f990 | 697 | if (server.repl_state == REDIS_REPL_TRANSFER) |
f4aa600b | 698 | replicationAbortSyncTransfer(); |
1844f990 | 699 | server.repl_state = REDIS_REPL_CONNECT; |
e2641e09 | 700 | redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)", |
701 | server.masterhost, server.masterport); | |
702 | } | |
703 | addReply(c,shared.ok); | |
704 | } | |
f4aa600b | 705 | |
706 | /* --------------------------- REPLICATION CRON ---------------------------- */ | |
707 | ||
f4aa600b | 708 | void replicationCron(void) { |
27acd7aa | 709 | /* Non blocking connection timeout? */ |
bb66fc31 | 710 | if (server.masterhost && |
711 | (server.repl_state == REDIS_REPL_CONNECTING || | |
712 | server.repl_state == REDIS_REPL_RECEIVE_PONG) && | |
27acd7aa | 713 | (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout) |
714 | { | |
715 | redisLog(REDIS_WARNING,"Timeout connecting to the MASTER..."); | |
716 | undoConnectWithMaster(); | |
717 | } | |
718 | ||
f4aa600b | 719 | /* Bulk transfer I/O timeout? */ |
1844f990 | 720 | if (server.masterhost && server.repl_state == REDIS_REPL_TRANSFER && |
8996bf77 | 721 | (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout) |
f4aa600b | 722 | { |
723 | redisLog(REDIS_WARNING,"Timeout receiving bulk data from MASTER..."); | |
724 | replicationAbortSyncTransfer(); | |
725 | } | |
726 | ||
89a1433e | 727 | /* Timed out master when we are an already connected slave? */ |
1844f990 | 728 | if (server.masterhost && server.repl_state == REDIS_REPL_CONNECTED && |
8996bf77 | 729 | (time(NULL)-server.master->lastinteraction) > server.repl_timeout) |
89a1433e | 730 | { |
731 | redisLog(REDIS_WARNING,"MASTER time out: no data nor PING received..."); | |
732 | freeClient(server.master); | |
733 | } | |
734 | ||
f4aa600b | 735 | /* Check if we should connect to a MASTER */ |
1844f990 | 736 | if (server.repl_state == REDIS_REPL_CONNECT) { |
f4aa600b | 737 | redisLog(REDIS_NOTICE,"Connecting to MASTER..."); |
a3309139 PN |
738 | if (connectWithMaster() == REDIS_OK) { |
739 | redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync started"); | |
f4aa600b | 740 | } |
741 | } | |
89a1433e | 742 | |
743 | /* If we have attached slaves, PING them from time to time. | |
744 | * So slaves can implement an explicit timeout to masters, and will | |
745 | * be able to detect a link disconnection even if the TCP connection | |
746 | * will not actually go down. */ | |
9edfe635 | 747 | if (!(server.cronloops % (server.repl_ping_slave_period * REDIS_HZ))) { |
89a1433e | 748 | listIter li; |
749 | listNode *ln; | |
750 | ||
751 | listRewind(server.slaves,&li); | |
752 | while((ln = listNext(&li))) { | |
753 | redisClient *slave = ln->value; | |
754 | ||
755 | /* Don't ping slaves that are in the middle of a bulk transfer | |
756 | * with the master for first synchronization. */ | |
757 | if (slave->replstate == REDIS_REPL_SEND_BULK) continue; | |
758 | if (slave->replstate == REDIS_REPL_ONLINE) { | |
759 | /* If the slave is online send a normal ping */ | |
a950a843 | 760 | addReplySds(slave,sdsnew("*1\r\n$4\r\nPING\r\n")); |
89a1433e | 761 | } else { |
762 | /* Otherwise we are in the pre-synchronization stage. | |
763 | * Just a newline will do the work of refreshing the | |
764 | * connection last interaction time, and at the same time | |
765 | * we'll be sure that being a single char there are no | |
766 | * short-write problems. */ | |
f96a9f82 | 767 | if (write(slave->fd, "\n", 1) == -1) { |
768 | /* Don't worry, it's just a ping. */ | |
769 | } | |
89a1433e | 770 | } |
771 | } | |
772 | } | |
f4aa600b | 773 | } |