8 #include <sys/resource.h>
11 void aofUpdateCurrentSize(void);
13 /* Called when the user switches from "appendonly yes" to "appendonly no"
14 * at runtime using the CONFIG command. */
15 void stopAppendOnly(void) {
16 flushAppendOnlyFile();
17 aof_fsync(server
.appendfd
);
18 close(server
.appendfd
);
21 server
.appendseldb
= -1;
22 server
.appendonly
= 0;
23 /* rewrite operation in progress? kill it, wait child exit */
24 if (server
.bgrewritechildpid
!= -1) {
27 if (kill(server
.bgrewritechildpid
,SIGKILL
) != -1)
28 wait3(&statloc
,0,NULL
);
29 /* reset the buffer accumulating changes while the child saves */
30 sdsfree(server
.bgrewritebuf
);
31 server
.bgrewritebuf
= sdsempty();
32 server
.bgrewritechildpid
= -1;
36 /* Called when the user switches from "appendonly no" to "appendonly yes"
37 * at runtime using the CONFIG command. */
38 int startAppendOnly(void) {
39 server
.appendonly
= 1;
40 server
.lastfsync
= time(NULL
);
41 server
.appendfd
= open(server
.appendfilename
,O_WRONLY
|O_APPEND
|O_CREAT
,0644);
42 if (server
.appendfd
== -1) {
43 redisLog(REDIS_WARNING
,"Used tried to switch on AOF via CONFIG, but I can't open the AOF file: %s",strerror(errno
));
46 if (rewriteAppendOnlyFileBackground() == REDIS_ERR
) {
47 server
.appendonly
= 0;
48 close(server
.appendfd
);
49 redisLog(REDIS_WARNING
,"Used tried to switch on AOF via CONFIG, I can't trigger a background AOF rewrite operation. Check the above logs for more info about the error.",strerror(errno
));
55 /* Write the append only file buffer on disk.
57 * Since we are required to write the AOF before replying to the client,
58 * and the only way the client socket can get a write is entering when the
59 * the event loop, we accumulate all the AOF writes in a memory
60 * buffer and write it on disk using this function just before entering
61 * the event loop again. */
62 void flushAppendOnlyFile(void) {
65 if (sdslen(server
.aofbuf
) == 0) return;
67 /* We want to perform a single write. This should be guaranteed atomic
68 * at least if the filesystem we are writing is a real physical one.
69 * While this will save us against the server being killed I don't think
70 * there is much to do about the whole server stopping for power problems
72 nwritten
= write(server
.appendfd
,server
.aofbuf
,sdslen(server
.aofbuf
));
73 if (nwritten
!= (signed)sdslen(server
.aofbuf
)) {
74 /* Ooops, we are in troubles. The best thing to do for now is
75 * aborting instead of giving the illusion that everything is
76 * working as expected. */
78 redisLog(REDIS_WARNING
,"Exiting on error writing to the append-only file: %s",strerror(errno
));
80 redisLog(REDIS_WARNING
,"Exiting on short write while writing to the append-only file: %s",strerror(errno
));
84 server
.appendonly_current_size
+= nwritten
;
86 /* Re-use AOF buffer when it is small enough. The maximum comes from the
87 * arena size of 4k minus some overhead (but is otherwise arbitrary). */
88 if ((sdslen(server
.aofbuf
)+sdsavail(server
.aofbuf
)) < 4000) {
89 sdsclear(server
.aofbuf
);
91 sdsfree(server
.aofbuf
);
92 server
.aofbuf
= sdsempty();
95 /* Don't fsync if no-appendfsync-on-rewrite is set to yes and there are
96 * children doing I/O in the background. */
97 if (server
.no_appendfsync_on_rewrite
&&
98 (server
.bgrewritechildpid
!= -1 || server
.bgsavechildpid
!= -1))
101 /* Perform the fsync if needed. */
102 if (server
.appendfsync
== APPENDFSYNC_ALWAYS
||
103 (server
.appendfsync
== APPENDFSYNC_EVERYSEC
&&
104 server
.unixtime
> server
.lastfsync
))
106 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
107 * flushing metadata. */
108 aof_fsync(server
.appendfd
); /* Let's try to get this data on the disk */
109 server
.lastfsync
= server
.unixtime
;
113 sds
catAppendOnlyGenericCommand(sds dst
, int argc
, robj
**argv
) {
119 len
= 1+ll2string(buf
+1,sizeof(buf
)-1,argc
);
122 dst
= sdscatlen(dst
,buf
,len
);
124 for (j
= 0; j
< argc
; j
++) {
125 o
= getDecodedObject(argv
[j
]);
127 len
= 1+ll2string(buf
+1,sizeof(buf
)-1,sdslen(o
->ptr
));
130 dst
= sdscatlen(dst
,buf
,len
);
131 dst
= sdscatlen(dst
,o
->ptr
,sdslen(o
->ptr
));
132 dst
= sdscatlen(dst
,"\r\n",2);
138 sds
catAppendOnlyExpireAtCommand(sds buf
, robj
*key
, robj
*seconds
) {
143 /* Make sure we can use strtol */
144 seconds
= getDecodedObject(seconds
);
145 when
= time(NULL
)+strtol(seconds
->ptr
,NULL
,10);
146 decrRefCount(seconds
);
148 argv
[0] = createStringObject("EXPIREAT",8);
150 argv
[2] = createObject(REDIS_STRING
,
151 sdscatprintf(sdsempty(),"%ld",when
));
152 buf
= catAppendOnlyGenericCommand(buf
, argc
, argv
);
153 decrRefCount(argv
[0]);
154 decrRefCount(argv
[2]);
158 void feedAppendOnlyFile(struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
) {
159 sds buf
= sdsempty();
162 /* The DB this command was targetting is not the same as the last command
163 * we appendend. To issue a SELECT command is needed. */
164 if (dictid
!= server
.appendseldb
) {
167 snprintf(seldb
,sizeof(seldb
),"%d",dictid
);
168 buf
= sdscatprintf(buf
,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
169 (unsigned long)strlen(seldb
),seldb
);
170 server
.appendseldb
= dictid
;
173 if (cmd
->proc
== expireCommand
) {
174 /* Translate EXPIRE into EXPIREAT */
175 buf
= catAppendOnlyExpireAtCommand(buf
,argv
[1],argv
[2]);
176 } else if (cmd
->proc
== setexCommand
) {
177 /* Translate SETEX to SET and EXPIREAT */
178 tmpargv
[0] = createStringObject("SET",3);
179 tmpargv
[1] = argv
[1];
180 tmpargv
[2] = argv
[3];
181 buf
= catAppendOnlyGenericCommand(buf
,3,tmpargv
);
182 decrRefCount(tmpargv
[0]);
183 buf
= catAppendOnlyExpireAtCommand(buf
,argv
[1],argv
[2]);
185 buf
= catAppendOnlyGenericCommand(buf
,argc
,argv
);
188 /* Append to the AOF buffer. This will be flushed on disk just before
189 * of re-entering the event loop, so before the client will get a
190 * positive reply about the operation performed. */
191 server
.aofbuf
= sdscatlen(server
.aofbuf
,buf
,sdslen(buf
));
193 /* If a background append only file rewriting is in progress we want to
194 * accumulate the differences between the child DB and the current one
195 * in a buffer, so that when the child process will do its work we
196 * can append the differences to the new append only file. */
197 if (server
.bgrewritechildpid
!= -1)
198 server
.bgrewritebuf
= sdscatlen(server
.bgrewritebuf
,buf
,sdslen(buf
));
203 /* In Redis commands are always executed in the context of a client, so in
204 * order to load the append only file we need to create a fake client. */
205 struct redisClient
*createFakeClient(void) {
206 struct redisClient
*c
= zmalloc(sizeof(*c
));
210 c
->querybuf
= sdsempty();
215 /* We set the fake client as a slave waiting for the synchronization
216 * so that Redis will not try to send replies to this client. */
217 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_START
;
218 c
->reply
= listCreate();
219 c
->watched_keys
= listCreate();
220 listSetFreeMethod(c
->reply
,decrRefCount
);
221 listSetDupMethod(c
->reply
,dupClientReplyValue
);
222 initClientMultiState(c
);
226 void freeFakeClient(struct redisClient
*c
) {
227 sdsfree(c
->querybuf
);
228 listRelease(c
->reply
);
229 listRelease(c
->watched_keys
);
230 freeClientMultiState(c
);
234 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
235 * error (the append only file is zero-length) REDIS_ERR is returned. On
236 * fatal error an error message is logged and the program exists. */
237 int loadAppendOnlyFile(char *filename
) {
238 struct redisClient
*fakeClient
;
239 FILE *fp
= fopen(filename
,"r");
240 struct redis_stat sb
;
241 int appendonly
= server
.appendonly
;
244 if (fp
&& redis_fstat(fileno(fp
),&sb
) != -1 && sb
.st_size
== 0) {
245 server
.appendonly_current_size
= 0;
251 redisLog(REDIS_WARNING
,"Fatal error: can't open the append log file for reading: %s",strerror(errno
));
255 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
256 * to the same file we're about to read. */
257 server
.appendonly
= 0;
259 fakeClient
= createFakeClient();
268 struct redisCommand
*cmd
;
270 /* Serve the clients from time to time */
271 if (!(loops
++ % 1000)) {
272 loadingProgress(ftello(fp
));
273 aeProcessEvents(server
.el
, AE_FILE_EVENTS
|AE_DONT_WAIT
);
276 if (fgets(buf
,sizeof(buf
),fp
) == NULL
) {
282 if (buf
[0] != '*') goto fmterr
;
284 argv
= zmalloc(sizeof(robj
*)*argc
);
285 for (j
= 0; j
< argc
; j
++) {
286 if (fgets(buf
,sizeof(buf
),fp
) == NULL
) goto readerr
;
287 if (buf
[0] != '$') goto fmterr
;
288 len
= strtol(buf
+1,NULL
,10);
289 argsds
= sdsnewlen(NULL
,len
);
290 if (len
&& fread(argsds
,len
,1,fp
) == 0) goto fmterr
;
291 argv
[j
] = createObject(REDIS_STRING
,argsds
);
292 if (fread(buf
,2,1,fp
) == 0) goto fmterr
; /* discard CRLF */
296 cmd
= lookupCommand(argv
[0]->ptr
);
298 redisLog(REDIS_WARNING
,"Unknown command '%s' reading the append only file", argv
[0]->ptr
);
301 /* Run the command in the context of a fake client */
302 fakeClient
->argc
= argc
;
303 fakeClient
->argv
= argv
;
304 cmd
->proc(fakeClient
);
306 /* The fake client should not have a reply */
307 redisAssert(fakeClient
->bufpos
== 0 && listLength(fakeClient
->reply
) == 0);
308 /* The fake client should never get blocked */
309 redisAssert((fakeClient
->flags
& REDIS_BLOCKED
) == 0);
311 /* Clean up. Command code may have changed argv/argc so we use the
312 * argv/argc of the client instead of the local variables. */
313 for (j
= 0; j
< fakeClient
->argc
; j
++)
314 decrRefCount(fakeClient
->argv
[j
]);
315 zfree(fakeClient
->argv
);
318 /* This point can only be reached when EOF is reached without errors.
319 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
320 if (fakeClient
->flags
& REDIS_MULTI
) goto readerr
;
323 freeFakeClient(fakeClient
);
324 server
.appendonly
= appendonly
;
326 aofUpdateCurrentSize();
327 server
.auto_aofrewrite_base_size
= server
.appendonly_current_size
;
332 redisLog(REDIS_WARNING
,"Unexpected end of file reading the append only file");
334 redisLog(REDIS_WARNING
,"Unrecoverable error reading the append only file: %s", strerror(errno
));
338 redisLog(REDIS_WARNING
,"Bad file format reading the append only file: make a backup of your AOF file, then use ./redis-check-aof --fix <filename>");
342 /* Write a sequence of commands able to fully rebuild the dataset into
343 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
344 int rewriteAppendOnlyFile(char *filename
) {
345 dictIterator
*di
= NULL
;
350 time_t now
= time(NULL
);
352 /* Note that we have to use a different temp name here compared to the
353 * one used by rewriteAppendOnlyFileBackground() function. */
354 snprintf(tmpfile
,256,"temp-rewriteaof-%d.aof", (int) getpid());
355 fp
= fopen(tmpfile
,"w");
357 redisLog(REDIS_WARNING
, "Failed rewriting the append only file: %s", strerror(errno
));
360 for (j
= 0; j
< server
.dbnum
; j
++) {
361 char selectcmd
[] = "*2\r\n$6\r\nSELECT\r\n";
362 redisDb
*db
= server
.db
+j
;
364 if (dictSize(d
) == 0) continue;
365 di
= dictGetSafeIterator(d
);
371 /* SELECT the new DB */
372 if (fwrite(selectcmd
,sizeof(selectcmd
)-1,1,fp
) == 0) goto werr
;
373 if (fwriteBulkLongLong(fp
,j
) == 0) goto werr
;
375 /* Iterate this DB writing every entry */
376 while((de
= dictNext(di
)) != NULL
) {
381 keystr
= dictGetEntryKey(de
);
382 o
= dictGetEntryVal(de
);
383 initStaticStringObject(key
,keystr
);
385 expiretime
= getExpire(db
,&key
);
387 /* Save the key and associated value */
388 if (o
->type
== REDIS_STRING
) {
389 /* Emit a SET command */
390 char cmd
[]="*3\r\n$3\r\nSET\r\n";
391 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
393 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
394 if (fwriteBulkObject(fp
,o
) == 0) goto werr
;
395 } else if (o
->type
== REDIS_LIST
) {
396 /* Emit the RPUSHes needed to rebuild the list */
397 char cmd
[]="*3\r\n$5\r\nRPUSH\r\n";
398 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
399 unsigned char *zl
= o
->ptr
;
400 unsigned char *p
= ziplistIndex(zl
,0);
405 while(ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
406 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
407 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
409 if (fwriteBulkString(fp
,(char*)vstr
,vlen
) == 0)
412 if (fwriteBulkLongLong(fp
,vlong
) == 0)
415 p
= ziplistNext(zl
,p
);
417 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
422 listRewind(list
,&li
);
423 while((ln
= listNext(&li
))) {
424 robj
*eleobj
= listNodeValue(ln
);
426 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
427 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
428 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
431 redisPanic("Unknown list encoding");
433 } else if (o
->type
== REDIS_SET
) {
434 char cmd
[]="*3\r\n$4\r\nSADD\r\n";
436 /* Emit the SADDs needed to rebuild the set */
437 if (o
->encoding
== REDIS_ENCODING_INTSET
) {
440 while(intsetGet(o
->ptr
,ii
++,&llval
)) {
441 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
442 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
443 if (fwriteBulkLongLong(fp
,llval
) == 0) goto werr
;
445 } else if (o
->encoding
== REDIS_ENCODING_HT
) {
446 dictIterator
*di
= dictGetIterator(o
->ptr
);
448 while((de
= dictNext(di
)) != NULL
) {
449 robj
*eleobj
= dictGetEntryKey(de
);
450 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
451 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
452 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
454 dictReleaseIterator(di
);
456 redisPanic("Unknown set encoding");
458 } else if (o
->type
== REDIS_ZSET
) {
459 /* Emit the ZADDs needed to rebuild the sorted set */
460 char cmd
[]="*4\r\n$4\r\nZADD\r\n";
462 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
463 unsigned char *zl
= o
->ptr
;
464 unsigned char *eptr
, *sptr
;
470 eptr
= ziplistIndex(zl
,0);
471 redisAssert(eptr
!= NULL
);
472 sptr
= ziplistNext(zl
,eptr
);
473 redisAssert(sptr
!= NULL
);
475 while (eptr
!= NULL
) {
476 redisAssert(ziplistGet(eptr
,&vstr
,&vlen
,&vll
));
477 score
= zzlGetScore(sptr
);
479 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
480 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
481 if (fwriteBulkDouble(fp
,score
) == 0) goto werr
;
483 if (fwriteBulkString(fp
,(char*)vstr
,vlen
) == 0)
486 if (fwriteBulkLongLong(fp
,vll
) == 0)
489 zzlNext(zl
,&eptr
,&sptr
);
491 } else if (o
->encoding
== REDIS_ENCODING_SKIPLIST
) {
493 dictIterator
*di
= dictGetIterator(zs
->dict
);
496 while((de
= dictNext(di
)) != NULL
) {
497 robj
*eleobj
= dictGetEntryKey(de
);
498 double *score
= dictGetEntryVal(de
);
500 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
501 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
502 if (fwriteBulkDouble(fp
,*score
) == 0) goto werr
;
503 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
505 dictReleaseIterator(di
);
507 redisPanic("Unknown sorted set encoding");
509 } else if (o
->type
== REDIS_HASH
) {
510 char cmd
[]="*4\r\n$4\r\nHSET\r\n";
512 /* Emit the HSETs needed to rebuild the hash */
513 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
514 unsigned char *p
= zipmapRewind(o
->ptr
);
515 unsigned char *field
, *val
;
516 unsigned int flen
, vlen
;
518 while((p
= zipmapNext(p
,&field
,&flen
,&val
,&vlen
)) != NULL
) {
519 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
520 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
521 if (fwriteBulkString(fp
,(char*)field
,flen
) == 0)
523 if (fwriteBulkString(fp
,(char*)val
,vlen
) == 0)
527 dictIterator
*di
= dictGetIterator(o
->ptr
);
530 while((de
= dictNext(di
)) != NULL
) {
531 robj
*field
= dictGetEntryKey(de
);
532 robj
*val
= dictGetEntryVal(de
);
534 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
535 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
536 if (fwriteBulkObject(fp
,field
) == 0) goto werr
;
537 if (fwriteBulkObject(fp
,val
) == 0) goto werr
;
539 dictReleaseIterator(di
);
542 redisPanic("Unknown object type");
544 /* Save the expire time */
545 if (expiretime
!= -1) {
546 char cmd
[]="*3\r\n$8\r\nEXPIREAT\r\n";
547 /* If this key is already expired skip it */
548 if (expiretime
< now
) continue;
549 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
550 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
551 if (fwriteBulkLongLong(fp
,expiretime
) == 0) goto werr
;
554 dictReleaseIterator(di
);
557 /* Make sure data will not remain on the OS's output buffers */
559 aof_fsync(fileno(fp
));
562 /* Use RENAME to make sure the DB file is changed atomically only
563 * if the generate DB file is ok. */
564 if (rename(tmpfile
,filename
) == -1) {
565 redisLog(REDIS_WARNING
,"Error moving temp append only file on the final destination: %s", strerror(errno
));
569 redisLog(REDIS_NOTICE
,"SYNC append only file rewrite performed");
575 redisLog(REDIS_WARNING
,"Write error writing append only file on disk: %s", strerror(errno
));
576 if (di
) dictReleaseIterator(di
);
580 /* This is how rewriting of the append only file in background works:
582 * 1) The user calls BGREWRITEAOF
583 * 2) Redis calls this function, that forks():
584 * 2a) the child rewrite the append only file in a temp file.
585 * 2b) the parent accumulates differences in server.bgrewritebuf.
586 * 3) When the child finished '2a' exists.
587 * 4) The parent will trap the exit code, if it's OK, will append the
588 * data accumulated into server.bgrewritebuf into the temp file, and
589 * finally will rename(2) the temp file in the actual file name.
590 * The the new file is reopened as the new append only file. Profit!
592 int rewriteAppendOnlyFileBackground(void) {
596 if (server
.bgrewritechildpid
!= -1) return REDIS_ERR
;
598 if ((childpid
= fork()) == 0) {
602 if (server
.ipfd
> 0) close(server
.ipfd
);
603 if (server
.sofd
> 0) close(server
.sofd
);
604 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
605 if (rewriteAppendOnlyFile(tmpfile
) == REDIS_OK
) {
612 server
.stat_fork_time
= ustime()-start
;
613 if (childpid
== -1) {
614 redisLog(REDIS_WARNING
,
615 "Can't rewrite append only file in background: fork: %s",
619 redisLog(REDIS_NOTICE
,
620 "Background append only file rewriting started by pid %d",childpid
);
621 server
.bgrewritechildpid
= childpid
;
622 updateDictResizePolicy();
623 /* We set appendseldb to -1 in order to force the next call to the
624 * feedAppendOnlyFile() to issue a SELECT command, so the differences
625 * accumulated by the parent into server.bgrewritebuf will start
626 * with a SELECT statement and it will be safe to merge. */
627 server
.appendseldb
= -1;
630 return REDIS_OK
; /* unreached */
633 void bgrewriteaofCommand(redisClient
*c
) {
634 if (server
.bgrewritechildpid
!= -1) {
635 addReplyError(c
,"Background append only file rewriting already in progress");
636 } else if (server
.bgsavechildpid
!= -1) {
637 server
.aofrewrite_scheduled
= 1;
638 addReplyStatus(c
,"Background append only file rewriting scheduled");
639 } else if (rewriteAppendOnlyFileBackground() == REDIS_OK
) {
640 addReplyStatus(c
,"Background append only file rewriting started");
642 addReply(c
,shared
.err
);
646 void aofRemoveTempFile(pid_t childpid
) {
649 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) childpid
);
653 /* Update the server.appendonly_current_size filed explicitly using stat(2)
654 * to check the size of the file. This is useful after a rewrite or after
655 * a restart, normally the size is updated just adding the write length
656 * to the current lenght, that is much faster. */
657 void aofUpdateCurrentSize(void) {
658 struct redis_stat sb
;
660 if (redis_fstat(server
.appendfd
,&sb
) == -1) {
661 redisLog(REDIS_WARNING
,"Unable to check the AOF length: %s",
664 server
.appendonly_current_size
= sb
.st_size
;
668 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
670 void backgroundRewriteDoneHandler(int exitcode
, int bysignal
) {
671 if (!bysignal
&& exitcode
== 0) {
675 redisLog(REDIS_NOTICE
,
676 "Background append only file rewriting terminated with success");
677 /* Now it's time to flush the differences accumulated by the parent */
678 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) server
.bgrewritechildpid
);
679 fd
= open(tmpfile
,O_WRONLY
|O_APPEND
);
681 redisLog(REDIS_WARNING
, "Not able to open the temp append only file produced by the child: %s", strerror(errno
));
684 /* Flush our data... */
685 if (write(fd
,server
.bgrewritebuf
,sdslen(server
.bgrewritebuf
)) !=
686 (signed) sdslen(server
.bgrewritebuf
)) {
687 redisLog(REDIS_WARNING
, "Error or short write trying to flush the parent diff of the append log file in the child temp file: %s", strerror(errno
));
691 redisLog(REDIS_NOTICE
,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server
.bgrewritebuf
));
692 /* Now our work is to rename the temp file into the stable file. And
693 * switch the file descriptor used by the server for append only. */
694 if (rename(tmpfile
,server
.appendfilename
) == -1) {
695 redisLog(REDIS_WARNING
,"Can't rename the temp append only file into the stable one: %s", strerror(errno
));
699 /* Mission completed... almost */
700 redisLog(REDIS_NOTICE
,"Append only file successfully rewritten.");
701 if (server
.appendfd
!= -1) {
702 /* If append only is actually enabled... */
703 close(server
.appendfd
);
704 server
.appendfd
= fd
;
705 if (server
.appendfsync
!= APPENDFSYNC_NO
) aof_fsync(fd
);
706 server
.appendseldb
= -1; /* Make sure it will issue SELECT */
707 redisLog(REDIS_NOTICE
,"The new append only file was selected for future appends.");
708 aofUpdateCurrentSize();
709 server
.auto_aofrewrite_base_size
= server
.appendonly_current_size
;
711 /* If append only is disabled we just generate a dump in this
712 * format. Why not? */
715 } else if (!bysignal
&& exitcode
!= 0) {
716 redisLog(REDIS_WARNING
, "Background append only file rewriting error");
718 redisLog(REDIS_WARNING
,
719 "Background append only file rewriting terminated by signal %d",
723 sdsfree(server
.bgrewritebuf
);
724 server
.bgrewritebuf
= sdsempty();
725 aofRemoveTempFile(server
.bgrewritechildpid
);
726 server
.bgrewritechildpid
= -1;