]>
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 | ||
148 | void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) { | |
149 | redisClient *slave = privdata; | |
150 | REDIS_NOTUSED(el); | |
151 | REDIS_NOTUSED(mask); | |
152 | char buf[REDIS_IOBUF_LEN]; | |
153 | ssize_t nwritten, buflen; | |
154 | ||
155 | if (slave->repldboff == 0) { | |
156 | /* Write the bulk write count before to transfer the DB. In theory here | |
157 | * we don't know how much room there is in the output buffer of the | |
158 | * socket, but in pratice SO_SNDLOWAT (the minimum count for output | |
159 | * operations) will never be smaller than the few bytes we need. */ | |
160 | sds bulkcount; | |
161 | ||
162 | bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long) | |
163 | slave->repldbsize); | |
164 | if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount)) | |
165 | { | |
166 | sdsfree(bulkcount); | |
167 | freeClient(slave); | |
168 | return; | |
169 | } | |
170 | sdsfree(bulkcount); | |
171 | } | |
172 | lseek(slave->repldbfd,slave->repldboff,SEEK_SET); | |
173 | buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN); | |
174 | if (buflen <= 0) { | |
175 | redisLog(REDIS_WARNING,"Read error sending DB to slave: %s", | |
176 | (buflen == 0) ? "premature EOF" : strerror(errno)); | |
177 | freeClient(slave); | |
178 | return; | |
179 | } | |
180 | if ((nwritten = write(fd,buf,buflen)) == -1) { | |
181 | redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s", | |
182 | strerror(errno)); | |
183 | freeClient(slave); | |
184 | return; | |
185 | } | |
186 | slave->repldboff += nwritten; | |
187 | if (slave->repldboff == slave->repldbsize) { | |
188 | close(slave->repldbfd); | |
189 | slave->repldbfd = -1; | |
190 | aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE); | |
191 | slave->replstate = REDIS_REPL_ONLINE; | |
192 | if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, | |
193 | sendReplyToClient, slave) == AE_ERR) { | |
194 | freeClient(slave); | |
195 | return; | |
196 | } | |
e2641e09 | 197 | redisLog(REDIS_NOTICE,"Synchronization with slave succeeded"); |
198 | } | |
199 | } | |
200 | ||
201 | /* This function is called at the end of every backgrond saving. | |
202 | * The argument bgsaveerr is REDIS_OK if the background saving succeeded | |
203 | * otherwise REDIS_ERR is passed to the function. | |
204 | * | |
205 | * The goal of this function is to handle slaves waiting for a successful | |
206 | * background saving in order to perform non-blocking synchronization. */ | |
207 | void updateSlavesWaitingBgsave(int bgsaveerr) { | |
208 | listNode *ln; | |
209 | int startbgsave = 0; | |
210 | listIter li; | |
211 | ||
212 | listRewind(server.slaves,&li); | |
213 | while((ln = listNext(&li))) { | |
214 | redisClient *slave = ln->value; | |
215 | ||
216 | if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) { | |
217 | startbgsave = 1; | |
218 | slave->replstate = REDIS_REPL_WAIT_BGSAVE_END; | |
219 | } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) { | |
220 | struct redis_stat buf; | |
221 | ||
222 | if (bgsaveerr != REDIS_OK) { | |
223 | freeClient(slave); | |
224 | redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error"); | |
225 | continue; | |
226 | } | |
f48cd4b9 | 227 | if ((slave->repldbfd = open(server.rdb_filename,O_RDONLY)) == -1 || |
e2641e09 | 228 | redis_fstat(slave->repldbfd,&buf) == -1) { |
229 | freeClient(slave); | |
230 | redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno)); | |
231 | continue; | |
232 | } | |
233 | slave->repldboff = 0; | |
234 | slave->repldbsize = buf.st_size; | |
235 | slave->replstate = REDIS_REPL_SEND_BULK; | |
236 | aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE); | |
237 | if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) { | |
238 | freeClient(slave); | |
239 | continue; | |
240 | } | |
241 | } | |
242 | } | |
243 | if (startbgsave) { | |
f48cd4b9 | 244 | if (rdbSaveBackground(server.rdb_filename) != REDIS_OK) { |
e2641e09 | 245 | listIter li; |
246 | ||
247 | listRewind(server.slaves,&li); | |
248 | redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed"); | |
249 | while((ln = listNext(&li))) { | |
250 | redisClient *slave = ln->value; | |
251 | ||
252 | if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) | |
253 | freeClient(slave); | |
254 | } | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
f4aa600b | 259 | /* ----------------------------------- SLAVE -------------------------------- */ |
260 | ||
261 | /* Abort the async download of the bulk dataset while SYNC-ing with master */ | |
262 | void replicationAbortSyncTransfer(void) { | |
1844f990 | 263 | redisAssert(server.repl_state == REDIS_REPL_TRANSFER); |
f4aa600b | 264 | |
265 | aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE); | |
266 | close(server.repl_transfer_s); | |
267 | close(server.repl_transfer_fd); | |
268 | unlink(server.repl_transfer_tmpfile); | |
269 | zfree(server.repl_transfer_tmpfile); | |
1844f990 | 270 | server.repl_state = REDIS_REPL_CONNECT; |
f4aa600b | 271 | } |
272 | ||
273 | /* Asynchronously read the SYNC payload we receive from a master */ | |
274 | void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) { | |
26b33669 | 275 | char buf[4096]; |
62ec599c | 276 | ssize_t nread, readlen; |
277 | REDIS_NOTUSED(el); | |
278 | REDIS_NOTUSED(privdata); | |
279 | REDIS_NOTUSED(mask); | |
f4aa600b | 280 | |
26b33669 | 281 | /* If repl_transfer_left == -1 we still have to read the bulk length |
282 | * from the master reply. */ | |
283 | if (server.repl_transfer_left == -1) { | |
9157549f | 284 | if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) { |
26b33669 | 285 | redisLog(REDIS_WARNING, |
286 | "I/O error reading bulk count from MASTER: %s", | |
287 | strerror(errno)); | |
b075621f | 288 | goto error; |
26b33669 | 289 | } |
b075621f | 290 | |
26b33669 | 291 | if (buf[0] == '-') { |
292 | redisLog(REDIS_WARNING, | |
293 | "MASTER aborted replication with an error: %s", | |
294 | buf+1); | |
b075621f | 295 | goto error; |
89a1433e | 296 | } else if (buf[0] == '\0') { |
297 | /* At this stage just a newline works as a PING in order to take | |
298 | * the connection live. So we refresh our last interaction | |
299 | * timestamp. */ | |
d1949054 | 300 | server.repl_transfer_lastio = server.unixtime; |
89a1433e | 301 | return; |
26b33669 | 302 | } else if (buf[0] != '$') { |
303 | redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?"); | |
b075621f | 304 | goto error; |
26b33669 | 305 | } |
306 | server.repl_transfer_left = strtol(buf+1,NULL,10); | |
307 | redisLog(REDIS_NOTICE, | |
308 | "MASTER <-> SLAVE sync: receiving %ld bytes from master", | |
309 | server.repl_transfer_left); | |
310 | return; | |
311 | } | |
312 | ||
313 | /* Read bulk data */ | |
62ec599c | 314 | readlen = (server.repl_transfer_left < (signed)sizeof(buf)) ? |
315 | server.repl_transfer_left : (signed)sizeof(buf); | |
f4aa600b | 316 | nread = read(fd,buf,readlen); |
317 | if (nread <= 0) { | |
318 | redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s", | |
319 | (nread == -1) ? strerror(errno) : "connection lost"); | |
320 | replicationAbortSyncTransfer(); | |
321 | return; | |
322 | } | |
d1949054 | 323 | server.repl_transfer_lastio = server.unixtime; |
f4aa600b | 324 | if (write(server.repl_transfer_fd,buf,nread) != nread) { |
31788f50 | 325 | redisLog(REDIS_WARNING,"Write error or short write writing to the DB dump file needed for MASTER <-> SLAVE synchronization: %s", strerror(errno)); |
b075621f | 326 | goto error; |
f4aa600b | 327 | } |
328 | server.repl_transfer_left -= nread; | |
329 | /* Check if the transfer is now complete */ | |
330 | if (server.repl_transfer_left == 0) { | |
f48cd4b9 | 331 | if (rename(server.repl_transfer_tmpfile,server.rdb_filename) == -1) { |
f4aa600b | 332 | redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno)); |
333 | replicationAbortSyncTransfer(); | |
334 | return; | |
335 | } | |
f6433915 | 336 | redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Loading DB in memory"); |
f4aa600b | 337 | emptyDb(); |
9fd01051 | 338 | /* Before loading the DB into memory we need to delete the readable |
339 | * handler, otherwise it will get called recursively since | |
340 | * rdbLoad() will call the event loop to process events from time to | |
341 | * time for non blocking loading. */ | |
342 | aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE); | |
f48cd4b9 | 343 | if (rdbLoad(server.rdb_filename) != REDIS_OK) { |
f4aa600b | 344 | redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk"); |
345 | replicationAbortSyncTransfer(); | |
346 | return; | |
347 | } | |
348 | /* Final setup of the connected slave <- master link */ | |
f4aa600b | 349 | zfree(server.repl_transfer_tmpfile); |
350 | close(server.repl_transfer_fd); | |
351 | server.master = createClient(server.repl_transfer_s); | |
352 | server.master->flags |= REDIS_MASTER; | |
353 | server.master->authenticated = 1; | |
1844f990 | 354 | server.repl_state = REDIS_REPL_CONNECTED; |
26b33669 | 355 | redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Finished with success"); |
e7a2e7c1 | 356 | /* Restart the AOF subsystem now that we finished the sync. This |
357 | * will trigger an AOF rewrite, and when done will start appending | |
358 | * to the new file. */ | |
e394114d | 359 | if (server.aof_state != REDIS_AOF_OFF) { |
e7a2e7c1 | 360 | int retry = 10; |
361 | ||
362 | stopAppendOnly(); | |
363 | while (retry-- && startAppendOnly() == REDIS_ERR) { | |
364 | redisLog(REDIS_WARNING,"Failed enabling the AOF after successful master synchrnization! Trying it again in one second."); | |
365 | sleep(1); | |
366 | } | |
367 | if (!retry) { | |
368 | redisLog(REDIS_WARNING,"FATAL: this slave instance finished the synchronization with its master, but the AOF can't be turned on. Exiting now."); | |
369 | exit(1); | |
370 | } | |
371 | } | |
f4aa600b | 372 | } |
b075621f PN |
373 | |
374 | return; | |
375 | ||
376 | error: | |
377 | replicationAbortSyncTransfer(); | |
378 | return; | |
f4aa600b | 379 | } |
380 | ||
a3309139 PN |
381 | void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) { |
382 | char buf[1024], tmpfile[256]; | |
e2641e09 | 383 | int dfd, maxtries = 5; |
a3309139 PN |
384 | REDIS_NOTUSED(el); |
385 | REDIS_NOTUSED(privdata); | |
386 | REDIS_NOTUSED(mask); | |
e2641e09 | 387 | |
76e772f3 | 388 | /* If this event fired after the user turned the instance into a master |
389 | * with SLAVEOF NO ONE we must just return ASAP. */ | |
1844f990 | 390 | if (server.repl_state == REDIS_REPL_NONE) { |
76e772f3 | 391 | close(fd); |
392 | return; | |
393 | } | |
394 | ||
45029d37 | 395 | redisLog(REDIS_NOTICE,"Non blocking connect for SYNC fired the event."); |
b075621f PN |
396 | /* This event should only be triggered once since it is used to have a |
397 | * non-blocking connect(2) to the master. It has been triggered when this | |
398 | * function is called, so we can delete it. */ | |
45029d37 | 399 | aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE); |
e2641e09 | 400 | |
401 | /* AUTH with the master if required. */ | |
402 | if(server.masterauth) { | |
a3309139 PN |
403 | char authcmd[1024]; |
404 | size_t authlen; | |
405 | ||
406 | authlen = snprintf(authcmd,sizeof(authcmd),"AUTH %s\r\n",server.masterauth); | |
9157549f | 407 | if (syncWrite(fd,authcmd,authlen,server.repl_syncio_timeout*1000) == -1) { |
e2641e09 | 408 | redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s", |
409 | strerror(errno)); | |
a3309139 PN |
410 | goto error; |
411 | } | |
e2641e09 | 412 | /* Read the AUTH result. */ |
9157549f | 413 | if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) { |
e2641e09 | 414 | redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s", |
415 | strerror(errno)); | |
a3309139 | 416 | goto error; |
e2641e09 | 417 | } |
418 | if (buf[0] != '+') { | |
e2641e09 | 419 | redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?"); |
a3309139 | 420 | goto error; |
e2641e09 | 421 | } |
422 | } | |
423 | ||
424 | /* Issue the SYNC command */ | |
299290d3 | 425 | if (syncWrite(fd,"SYNC\r\n",6,server.repl_syncio_timeout*1000) == -1) { |
e2641e09 | 426 | redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s", |
427 | strerror(errno)); | |
a3309139 | 428 | goto error; |
e2641e09 | 429 | } |
26b33669 | 430 | |
431 | /* Prepare a suitable temp file for bulk transfer */ | |
e2641e09 | 432 | while(maxtries--) { |
433 | snprintf(tmpfile,256, | |
d1949054 | 434 | "temp-%d.%ld.rdb",(int)server.unixtime,(long int)getpid()); |
e2641e09 | 435 | dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644); |
436 | if (dfd != -1) break; | |
437 | sleep(1); | |
438 | } | |
439 | if (dfd == -1) { | |
e2641e09 | 440 | redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno)); |
a3309139 | 441 | goto error; |
e2641e09 | 442 | } |
e2641e09 | 443 | |
f4aa600b | 444 | /* Setup the non blocking download of the bulk file. */ |
a3309139 | 445 | if (aeCreateFileEvent(server.el,fd, AE_READABLE,readSyncBulkPayload,NULL) |
62ec599c | 446 | == AE_ERR) |
f4aa600b | 447 | { |
f4aa600b | 448 | redisLog(REDIS_WARNING,"Can't create readable event for SYNC"); |
a3309139 | 449 | goto error; |
e2641e09 | 450 | } |
a3309139 | 451 | |
1844f990 | 452 | server.repl_state = REDIS_REPL_TRANSFER; |
26b33669 | 453 | server.repl_transfer_left = -1; |
f4aa600b | 454 | server.repl_transfer_fd = dfd; |
d1949054 | 455 | server.repl_transfer_lastio = server.unixtime; |
f4aa600b | 456 | server.repl_transfer_tmpfile = zstrdup(tmpfile); |
a3309139 PN |
457 | return; |
458 | ||
459 | error: | |
1844f990 | 460 | server.repl_state = REDIS_REPL_CONNECT; |
a3309139 PN |
461 | close(fd); |
462 | return; | |
463 | } | |
464 | ||
465 | int connectWithMaster(void) { | |
466 | int fd; | |
467 | ||
468 | fd = anetTcpNonBlockConnect(NULL,server.masterhost,server.masterport); | |
469 | if (fd == -1) { | |
470 | redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s", | |
471 | strerror(errno)); | |
472 | return REDIS_ERR; | |
473 | } | |
474 | ||
45029d37 | 475 | if (aeCreateFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE,syncWithMaster,NULL) == |
b075621f | 476 | AE_ERR) |
a3309139 PN |
477 | { |
478 | close(fd); | |
479 | redisLog(REDIS_WARNING,"Can't create readable event for SYNC"); | |
480 | return REDIS_ERR; | |
481 | } | |
482 | ||
d1949054 | 483 | server.repl_transfer_lastio = server.unixtime; |
a3309139 | 484 | server.repl_transfer_s = fd; |
1844f990 | 485 | server.repl_state = REDIS_REPL_CONNECTING; |
e2641e09 | 486 | return REDIS_OK; |
487 | } | |
488 | ||
27acd7aa | 489 | /* This function can be called when a non blocking connection is currently |
490 | * in progress to undo it. */ | |
491 | void undoConnectWithMaster(void) { | |
492 | int fd = server.repl_transfer_s; | |
493 | ||
1844f990 | 494 | redisAssert(server.repl_state == REDIS_REPL_CONNECTING); |
27acd7aa | 495 | aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE); |
496 | close(fd); | |
497 | server.repl_transfer_s = -1; | |
1844f990 | 498 | server.repl_state = REDIS_REPL_CONNECT; |
27acd7aa | 499 | } |
500 | ||
e2641e09 | 501 | void slaveofCommand(redisClient *c) { |
502 | if (!strcasecmp(c->argv[1]->ptr,"no") && | |
503 | !strcasecmp(c->argv[2]->ptr,"one")) { | |
504 | if (server.masterhost) { | |
505 | sdsfree(server.masterhost); | |
506 | server.masterhost = NULL; | |
507 | if (server.master) freeClient(server.master); | |
1844f990 | 508 | if (server.repl_state == REDIS_REPL_TRANSFER) |
f4aa600b | 509 | replicationAbortSyncTransfer(); |
1844f990 | 510 | else if (server.repl_state == REDIS_REPL_CONNECTING) |
27acd7aa | 511 | undoConnectWithMaster(); |
1844f990 | 512 | server.repl_state = REDIS_REPL_NONE; |
e2641e09 | 513 | redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)"); |
514 | } | |
515 | } else { | |
ebdfad69 | 516 | long port; |
517 | ||
518 | if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != REDIS_OK)) | |
519 | return; | |
520 | ||
521 | /* Check if we are already attached to the specified slave */ | |
522 | if (server.masterhost && !strcasecmp(server.masterhost,c->argv[1]->ptr) | |
523 | && server.masterport == port) { | |
524 | redisLog(REDIS_NOTICE,"SLAVE OF would result into synchronization with the master we are already connected with. No operation performed."); | |
525 | addReplySds(c,sdsnew("+OK Already connected to specified master\r\n")); | |
526 | return; | |
527 | } | |
528 | /* There was no previous master or the user specified a different one, | |
529 | * we can continue. */ | |
e2641e09 | 530 | sdsfree(server.masterhost); |
531 | server.masterhost = sdsdup(c->argv[1]->ptr); | |
ebdfad69 | 532 | server.masterport = port; |
e2641e09 | 533 | if (server.master) freeClient(server.master); |
179e54d2 | 534 | disconnectSlaves(); /* Force our slaves to resync with us as well. */ |
1844f990 | 535 | if (server.repl_state == REDIS_REPL_TRANSFER) |
f4aa600b | 536 | replicationAbortSyncTransfer(); |
1844f990 | 537 | server.repl_state = REDIS_REPL_CONNECT; |
e2641e09 | 538 | redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)", |
539 | server.masterhost, server.masterport); | |
540 | } | |
541 | addReply(c,shared.ok); | |
542 | } | |
f4aa600b | 543 | |
544 | /* --------------------------- REPLICATION CRON ---------------------------- */ | |
545 | ||
f4aa600b | 546 | void replicationCron(void) { |
27acd7aa | 547 | /* Non blocking connection timeout? */ |
1844f990 | 548 | if (server.masterhost && server.repl_state == REDIS_REPL_CONNECTING && |
27acd7aa | 549 | (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout) |
550 | { | |
551 | redisLog(REDIS_WARNING,"Timeout connecting to the MASTER..."); | |
552 | undoConnectWithMaster(); | |
553 | } | |
554 | ||
f4aa600b | 555 | /* Bulk transfer I/O timeout? */ |
1844f990 | 556 | if (server.masterhost && server.repl_state == REDIS_REPL_TRANSFER && |
8996bf77 | 557 | (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout) |
f4aa600b | 558 | { |
559 | redisLog(REDIS_WARNING,"Timeout receiving bulk data from MASTER..."); | |
560 | replicationAbortSyncTransfer(); | |
561 | } | |
562 | ||
89a1433e | 563 | /* Timed out master when we are an already connected slave? */ |
1844f990 | 564 | if (server.masterhost && server.repl_state == REDIS_REPL_CONNECTED && |
8996bf77 | 565 | (time(NULL)-server.master->lastinteraction) > server.repl_timeout) |
89a1433e | 566 | { |
567 | redisLog(REDIS_WARNING,"MASTER time out: no data nor PING received..."); | |
568 | freeClient(server.master); | |
569 | } | |
570 | ||
f4aa600b | 571 | /* Check if we should connect to a MASTER */ |
1844f990 | 572 | if (server.repl_state == REDIS_REPL_CONNECT) { |
f4aa600b | 573 | redisLog(REDIS_NOTICE,"Connecting to MASTER..."); |
a3309139 PN |
574 | if (connectWithMaster() == REDIS_OK) { |
575 | redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync started"); | |
f4aa600b | 576 | } |
577 | } | |
89a1433e | 578 | |
579 | /* If we have attached slaves, PING them from time to time. | |
580 | * So slaves can implement an explicit timeout to masters, and will | |
581 | * be able to detect a link disconnection even if the TCP connection | |
582 | * will not actually go down. */ | |
8996bf77 | 583 | if (!(server.cronloops % (server.repl_ping_slave_period*10))) { |
89a1433e | 584 | listIter li; |
585 | listNode *ln; | |
586 | ||
587 | listRewind(server.slaves,&li); | |
588 | while((ln = listNext(&li))) { | |
589 | redisClient *slave = ln->value; | |
590 | ||
591 | /* Don't ping slaves that are in the middle of a bulk transfer | |
592 | * with the master for first synchronization. */ | |
593 | if (slave->replstate == REDIS_REPL_SEND_BULK) continue; | |
594 | if (slave->replstate == REDIS_REPL_ONLINE) { | |
595 | /* If the slave is online send a normal ping */ | |
a950a843 | 596 | addReplySds(slave,sdsnew("*1\r\n$4\r\nPING\r\n")); |
89a1433e | 597 | } else { |
598 | /* Otherwise we are in the pre-synchronization stage. | |
599 | * Just a newline will do the work of refreshing the | |
600 | * connection last interaction time, and at the same time | |
601 | * we'll be sure that being a single char there are no | |
602 | * short-write problems. */ | |
f96a9f82 | 603 | if (write(slave->fd, "\n", 1) == -1) { |
604 | /* Don't worry, it's just a ping. */ | |
605 | } | |
89a1433e | 606 | } |
607 | } | |
608 | } | |
f4aa600b | 609 | } |