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