+ /* Read bulk data */
+ readlen = (server.repl_transfer_left < (signed)sizeof(buf)) ?
+ server.repl_transfer_left : (signed)sizeof(buf);
+ nread = read(fd,buf,readlen);
+ if (nread <= 0) {
+ redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
+ (nread == -1) ? strerror(errno) : "connection lost");
+ replicationAbortSyncTransfer();
+ return;
+ }
+ server.repl_transfer_lastio = time(NULL);
+ if (write(server.repl_transfer_fd,buf,nread) != nread) {
+ redisLog(REDIS_WARNING,"Write error or short write writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
+ goto error;
+ }
+ server.repl_transfer_left -= nread;
+ /* Check if the transfer is now complete */
+ if (server.repl_transfer_left == 0) {
+ if (rename(server.repl_transfer_tmpfile,server.dbfilename) == -1) {
+ redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
+ replicationAbortSyncTransfer();
+ return;
+ }
+ redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Loading DB in memory");
+ emptyDb();
+ /* Before loading the DB into memory we need to delete the readable
+ * handler, otherwise it will get called recursively since
+ * rdbLoad() will call the event loop to process events from time to
+ * time for non blocking loading. */
+ aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE);
+ if (rdbLoad(server.dbfilename) != REDIS_OK) {
+ redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
+ replicationAbortSyncTransfer();
+ return;
+ }
+ /* Final setup of the connected slave <- master link */
+ zfree(server.repl_transfer_tmpfile);
+ close(server.repl_transfer_fd);
+ server.master = createClient(server.repl_transfer_s);
+ server.master->flags |= REDIS_MASTER;
+ server.master->authenticated = 1;
+ server.replstate = REDIS_REPL_CONNECTED;
+ redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Finished with success");
+ /* Rewrite the AOF file now that the dataset changed. */
+ if (server.appendonly) rewriteAppendOnlyFileBackground();
+ }
+
+ return;
+
+error:
+ replicationAbortSyncTransfer();
+ return;
+}
+
+void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) {
+ char buf[1024], tmpfile[256];
+ int dfd, maxtries = 5;
+ REDIS_NOTUSED(el);
+ REDIS_NOTUSED(privdata);
+ REDIS_NOTUSED(mask);
+
+ redisLog(REDIS_NOTICE,"Non blocking connect for SYNC fired the event.");
+ /* This event should only be triggered once since it is used to have a
+ * non-blocking connect(2) to the master. It has been triggered when this
+ * function is called, so we can delete it. */
+ aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
+