]> git.saurik.com Git - redis.git/blob - src/aof.c
Wrapper for adding unknown multi bulk length to reply list
[redis.git] / src / aof.c
1 #include "redis.h"
2
3 #include <signal.h>
4 #include <fcntl.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <sys/wait.h>
10
11 /* Called when the user switches from "appendonly yes" to "appendonly no"
12 * at runtime using the CONFIG command. */
13 void stopAppendOnly(void) {
14 flushAppendOnlyFile();
15 aof_fsync(server.appendfd);
16 close(server.appendfd);
17
18 server.appendfd = -1;
19 server.appendseldb = -1;
20 server.appendonly = 0;
21 /* rewrite operation in progress? kill it, wait child exit */
22 if (server.bgsavechildpid != -1) {
23 int statloc;
24
25 if (kill(server.bgsavechildpid,SIGKILL) != -1)
26 wait3(&statloc,0,NULL);
27 /* reset the buffer accumulating changes while the child saves */
28 sdsfree(server.bgrewritebuf);
29 server.bgrewritebuf = sdsempty();
30 server.bgsavechildpid = -1;
31 }
32 }
33
34 /* Called when the user switches from "appendonly no" to "appendonly yes"
35 * at runtime using the CONFIG command. */
36 int startAppendOnly(void) {
37 server.appendonly = 1;
38 server.lastfsync = time(NULL);
39 server.appendfd = open(server.appendfilename,O_WRONLY|O_APPEND|O_CREAT,0644);
40 if (server.appendfd == -1) {
41 redisLog(REDIS_WARNING,"Used tried to switch on AOF via CONFIG, but I can't open the AOF file: %s",strerror(errno));
42 return REDIS_ERR;
43 }
44 if (rewriteAppendOnlyFileBackground() == REDIS_ERR) {
45 server.appendonly = 0;
46 close(server.appendfd);
47 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));
48 return REDIS_ERR;
49 }
50 return REDIS_OK;
51 }
52
53 /* Write the append only file buffer on disk.
54 *
55 * Since we are required to write the AOF before replying to the client,
56 * and the only way the client socket can get a write is entering when the
57 * the event loop, we accumulate all the AOF writes in a memory
58 * buffer and write it on disk using this function just before entering
59 * the event loop again. */
60 void flushAppendOnlyFile(void) {
61 time_t now;
62 ssize_t nwritten;
63
64 if (sdslen(server.aofbuf) == 0) return;
65
66 /* We want to perform a single write. This should be guaranteed atomic
67 * at least if the filesystem we are writing is a real physical one.
68 * While this will save us against the server being killed I don't think
69 * there is much to do about the whole server stopping for power problems
70 * or alike */
71 nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf));
72 if (nwritten != (signed)sdslen(server.aofbuf)) {
73 /* Ooops, we are in troubles. The best thing to do for now is
74 * aborting instead of giving the illusion that everything is
75 * working as expected. */
76 if (nwritten == -1) {
77 redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
78 } else {
79 redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno));
80 }
81 exit(1);
82 }
83 sdsfree(server.aofbuf);
84 server.aofbuf = sdsempty();
85
86 /* Don't Fsync if no-appendfsync-on-rewrite is set to yes and we have
87 * childs performing heavy I/O on disk. */
88 if (server.no_appendfsync_on_rewrite &&
89 (server.bgrewritechildpid != -1 || server.bgsavechildpid != -1))
90 return;
91 /* Fsync if needed */
92 now = time(NULL);
93 if (server.appendfsync == APPENDFSYNC_ALWAYS ||
94 (server.appendfsync == APPENDFSYNC_EVERYSEC &&
95 now-server.lastfsync > 1))
96 {
97 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
98 * flushing metadata. */
99 aof_fsync(server.appendfd); /* Let's try to get this data on the disk */
100 server.lastfsync = now;
101 }
102 }
103
104 sds catAppendOnlyGenericCommand(sds buf, int argc, robj **argv) {
105 int j;
106 buf = sdscatprintf(buf,"*%d\r\n",argc);
107 for (j = 0; j < argc; j++) {
108 robj *o = getDecodedObject(argv[j]);
109 buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
110 buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
111 buf = sdscatlen(buf,"\r\n",2);
112 decrRefCount(o);
113 }
114 return buf;
115 }
116
117 sds catAppendOnlyExpireAtCommand(sds buf, robj *key, robj *seconds) {
118 int argc = 3;
119 long when;
120 robj *argv[3];
121
122 /* Make sure we can use strtol */
123 seconds = getDecodedObject(seconds);
124 when = time(NULL)+strtol(seconds->ptr,NULL,10);
125 decrRefCount(seconds);
126
127 argv[0] = createStringObject("EXPIREAT",8);
128 argv[1] = key;
129 argv[2] = createObject(REDIS_STRING,
130 sdscatprintf(sdsempty(),"%ld",when));
131 buf = catAppendOnlyGenericCommand(buf, argc, argv);
132 decrRefCount(argv[0]);
133 decrRefCount(argv[2]);
134 return buf;
135 }
136
137 void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
138 sds buf = sdsempty();
139 robj *tmpargv[3];
140
141 /* The DB this command was targetting is not the same as the last command
142 * we appendend. To issue a SELECT command is needed. */
143 if (dictid != server.appendseldb) {
144 char seldb[64];
145
146 snprintf(seldb,sizeof(seldb),"%d",dictid);
147 buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
148 (unsigned long)strlen(seldb),seldb);
149 server.appendseldb = dictid;
150 }
151
152 if (cmd->proc == expireCommand) {
153 /* Translate EXPIRE into EXPIREAT */
154 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
155 } else if (cmd->proc == setexCommand) {
156 /* Translate SETEX to SET and EXPIREAT */
157 tmpargv[0] = createStringObject("SET",3);
158 tmpargv[1] = argv[1];
159 tmpargv[2] = argv[3];
160 buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
161 decrRefCount(tmpargv[0]);
162 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
163 } else {
164 buf = catAppendOnlyGenericCommand(buf,argc,argv);
165 }
166
167 /* Append to the AOF buffer. This will be flushed on disk just before
168 * of re-entering the event loop, so before the client will get a
169 * positive reply about the operation performed. */
170 server.aofbuf = sdscatlen(server.aofbuf,buf,sdslen(buf));
171
172 /* If a background append only file rewriting is in progress we want to
173 * accumulate the differences between the child DB and the current one
174 * in a buffer, so that when the child process will do its work we
175 * can append the differences to the new append only file. */
176 if (server.bgrewritechildpid != -1)
177 server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
178
179 sdsfree(buf);
180 }
181
182 /* In Redis commands are always executed in the context of a client, so in
183 * order to load the append only file we need to create a fake client. */
184 struct redisClient *createFakeClient(void) {
185 struct redisClient *c = zmalloc(sizeof(*c));
186
187 selectDb(c,0);
188 c->fd = -1;
189 c->querybuf = sdsempty();
190 c->argc = 0;
191 c->argv = NULL;
192 c->flags = 0;
193 /* We set the fake client as a slave waiting for the synchronization
194 * so that Redis will not try to send replies to this client. */
195 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
196 c->reply = listCreate();
197 c->watched_keys = listCreate();
198 listSetFreeMethod(c->reply,decrRefCount);
199 listSetDupMethod(c->reply,dupClientReplyValue);
200 initClientMultiState(c);
201 return c;
202 }
203
204 void freeFakeClient(struct redisClient *c) {
205 sdsfree(c->querybuf);
206 listRelease(c->reply);
207 listRelease(c->watched_keys);
208 freeClientMultiState(c);
209 zfree(c);
210 }
211
212 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
213 * error (the append only file is zero-length) REDIS_ERR is returned. On
214 * fatal error an error message is logged and the program exists. */
215 int loadAppendOnlyFile(char *filename) {
216 struct redisClient *fakeClient;
217 FILE *fp = fopen(filename,"r");
218 struct redis_stat sb;
219 int appendonly = server.appendonly;
220
221 if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
222 return REDIS_ERR;
223
224 if (fp == NULL) {
225 redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
226 exit(1);
227 }
228
229 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
230 * to the same file we're about to read. */
231 server.appendonly = 0;
232
233 fakeClient = createFakeClient();
234 while(1) {
235 int argc, j;
236 unsigned long len;
237 robj **argv;
238 char buf[128];
239 sds argsds;
240 struct redisCommand *cmd;
241 int force_swapout;
242
243 if (fgets(buf,sizeof(buf),fp) == NULL) {
244 if (feof(fp))
245 break;
246 else
247 goto readerr;
248 }
249 if (buf[0] != '*') goto fmterr;
250 argc = atoi(buf+1);
251 argv = zmalloc(sizeof(robj*)*argc);
252 for (j = 0; j < argc; j++) {
253 if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;
254 if (buf[0] != '$') goto fmterr;
255 len = strtol(buf+1,NULL,10);
256 argsds = sdsnewlen(NULL,len);
257 if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
258 argv[j] = createObject(REDIS_STRING,argsds);
259 if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
260 }
261
262 /* Command lookup */
263 cmd = lookupCommand(argv[0]->ptr);
264 if (!cmd) {
265 redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr);
266 exit(1);
267 }
268 /* Try object encoding */
269 if (cmd->flags & REDIS_CMD_BULK)
270 argv[argc-1] = tryObjectEncoding(argv[argc-1]);
271 /* Run the command in the context of a fake client */
272 fakeClient->argc = argc;
273 fakeClient->argv = argv;
274 cmd->proc(fakeClient);
275 /* Discard the reply objects list from the fake client */
276 while(listLength(fakeClient->reply))
277 listDelNode(fakeClient->reply,listFirst(fakeClient->reply));
278 /* Clean up, ready for the next command */
279 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
280 zfree(argv);
281 /* Handle swapping while loading big datasets when VM is on */
282 force_swapout = 0;
283 if ((zmalloc_used_memory() - server.vm_max_memory) > 1024*1024*32)
284 force_swapout = 1;
285
286 if (server.vm_enabled && force_swapout) {
287 while (zmalloc_used_memory() > server.vm_max_memory) {
288 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
289 }
290 }
291 }
292
293 /* This point can only be reached when EOF is reached without errors.
294 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
295 if (fakeClient->flags & REDIS_MULTI) goto readerr;
296
297 fclose(fp);
298 freeFakeClient(fakeClient);
299 server.appendonly = appendonly;
300 return REDIS_OK;
301
302 readerr:
303 if (feof(fp)) {
304 redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
305 } else {
306 redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
307 }
308 exit(1);
309 fmterr:
310 redisLog(REDIS_WARNING,"Bad file format reading the append only file");
311 exit(1);
312 }
313
314 /* Write binary-safe string into a file in the bulkformat
315 * $<count>\r\n<payload>\r\n */
316 int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
317 char cbuf[128];
318 int clen;
319 cbuf[0] = '$';
320 clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,len);
321 cbuf[clen++] = '\r';
322 cbuf[clen++] = '\n';
323 if (fwrite(cbuf,clen,1,fp) == 0) return 0;
324 if (len > 0 && fwrite(s,len,1,fp) == 0) return 0;
325 if (fwrite("\r\n",2,1,fp) == 0) return 0;
326 return 1;
327 }
328
329 /* Write a double value in bulk format $<count>\r\n<payload>\r\n */
330 int fwriteBulkDouble(FILE *fp, double d) {
331 char buf[128], dbuf[128];
332
333 snprintf(dbuf,sizeof(dbuf),"%.17g\r\n",d);
334 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(dbuf)-2);
335 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
336 if (fwrite(dbuf,strlen(dbuf),1,fp) == 0) return 0;
337 return 1;
338 }
339
340 /* Write a long value in bulk format $<count>\r\n<payload>\r\n */
341 int fwriteBulkLongLong(FILE *fp, long long l) {
342 char bbuf[128], lbuf[128];
343 unsigned int blen, llen;
344 llen = ll2string(lbuf,32,l);
345 blen = snprintf(bbuf,sizeof(bbuf),"$%u\r\n%s\r\n",llen,lbuf);
346 if (fwrite(bbuf,blen,1,fp) == 0) return 0;
347 return 1;
348 }
349
350 /* Delegate writing an object to writing a bulk string or bulk long long. */
351 int fwriteBulkObject(FILE *fp, robj *obj) {
352 /* Avoid using getDecodedObject to help copy-on-write (we are often
353 * in a child process when this function is called). */
354 if (obj->encoding == REDIS_ENCODING_INT) {
355 return fwriteBulkLongLong(fp,(long)obj->ptr);
356 } else if (obj->encoding == REDIS_ENCODING_RAW) {
357 return fwriteBulkString(fp,obj->ptr,sdslen(obj->ptr));
358 } else {
359 redisPanic("Unknown string encoding");
360 }
361 }
362
363 /* Write a sequence of commands able to fully rebuild the dataset into
364 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
365 int rewriteAppendOnlyFile(char *filename) {
366 dictIterator *di = NULL;
367 dictEntry *de;
368 FILE *fp;
369 char tmpfile[256];
370 int j;
371 time_t now = time(NULL);
372
373 /* Note that we have to use a different temp name here compared to the
374 * one used by rewriteAppendOnlyFileBackground() function. */
375 snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
376 fp = fopen(tmpfile,"w");
377 if (!fp) {
378 redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno));
379 return REDIS_ERR;
380 }
381 for (j = 0; j < server.dbnum; j++) {
382 char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
383 redisDb *db = server.db+j;
384 dict *d = db->dict;
385 if (dictSize(d) == 0) continue;
386 di = dictGetIterator(d);
387 if (!di) {
388 fclose(fp);
389 return REDIS_ERR;
390 }
391
392 /* SELECT the new DB */
393 if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
394 if (fwriteBulkLongLong(fp,j) == 0) goto werr;
395
396 /* Iterate this DB writing every entry */
397 while((de = dictNext(di)) != NULL) {
398 sds keystr = dictGetEntryKey(de);
399 robj key, *o;
400 time_t expiretime;
401 int swapped;
402
403 keystr = dictGetEntryKey(de);
404 o = dictGetEntryVal(de);
405 initStaticStringObject(key,keystr);
406 /* If the value for this key is swapped, load a preview in memory.
407 * We use a "swapped" flag to remember if we need to free the
408 * value object instead to just increment the ref count anyway
409 * in order to avoid copy-on-write of pages if we are forked() */
410 if (!server.vm_enabled || o->storage == REDIS_VM_MEMORY ||
411 o->storage == REDIS_VM_SWAPPING) {
412 swapped = 0;
413 } else {
414 o = vmPreviewObject(o);
415 swapped = 1;
416 }
417 expiretime = getExpire(db,&key);
418
419 /* Save the key and associated value */
420 if (o->type == REDIS_STRING) {
421 /* Emit a SET command */
422 char cmd[]="*3\r\n$3\r\nSET\r\n";
423 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
424 /* Key and value */
425 if (fwriteBulkObject(fp,&key) == 0) goto werr;
426 if (fwriteBulkObject(fp,o) == 0) goto werr;
427 } else if (o->type == REDIS_LIST) {
428 /* Emit the RPUSHes needed to rebuild the list */
429 char cmd[]="*3\r\n$5\r\nRPUSH\r\n";
430 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
431 unsigned char *zl = o->ptr;
432 unsigned char *p = ziplistIndex(zl,0);
433 unsigned char *vstr;
434 unsigned int vlen;
435 long long vlong;
436
437 while(ziplistGet(p,&vstr,&vlen,&vlong)) {
438 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
439 if (fwriteBulkObject(fp,&key) == 0) goto werr;
440 if (vstr) {
441 if (fwriteBulkString(fp,(char*)vstr,vlen) == 0)
442 goto werr;
443 } else {
444 if (fwriteBulkLongLong(fp,vlong) == 0)
445 goto werr;
446 }
447 p = ziplistNext(zl,p);
448 }
449 } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) {
450 list *list = o->ptr;
451 listNode *ln;
452 listIter li;
453
454 listRewind(list,&li);
455 while((ln = listNext(&li))) {
456 robj *eleobj = listNodeValue(ln);
457
458 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
459 if (fwriteBulkObject(fp,&key) == 0) goto werr;
460 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
461 }
462 } else {
463 redisPanic("Unknown list encoding");
464 }
465 } else if (o->type == REDIS_SET) {
466 char cmd[]="*3\r\n$4\r\nSADD\r\n";
467
468 /* Emit the SADDs needed to rebuild the set */
469 if (o->encoding == REDIS_ENCODING_INTSET) {
470 int ii = 0;
471 int64_t llval;
472 while(intsetGet(o->ptr,ii++,&llval)) {
473 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
474 if (fwriteBulkObject(fp,&key) == 0) goto werr;
475 if (fwriteBulkLongLong(fp,llval) == 0) goto werr;
476 }
477 } else if (o->encoding == REDIS_ENCODING_HT) {
478 dictIterator *di = dictGetIterator(o->ptr);
479 dictEntry *de;
480 while((de = dictNext(di)) != NULL) {
481 robj *eleobj = dictGetEntryKey(de);
482 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
483 if (fwriteBulkObject(fp,&key) == 0) goto werr;
484 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
485 }
486 dictReleaseIterator(di);
487 } else {
488 redisPanic("Unknown set encoding");
489 }
490 } else if (o->type == REDIS_ZSET) {
491 /* Emit the ZADDs needed to rebuild the sorted set */
492 zset *zs = o->ptr;
493 dictIterator *di = dictGetIterator(zs->dict);
494 dictEntry *de;
495
496 while((de = dictNext(di)) != NULL) {
497 char cmd[]="*4\r\n$4\r\nZADD\r\n";
498 robj *eleobj = dictGetEntryKey(de);
499 double *score = dictGetEntryVal(de);
500
501 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
502 if (fwriteBulkObject(fp,&key) == 0) goto werr;
503 if (fwriteBulkDouble(fp,*score) == 0) goto werr;
504 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
505 }
506 dictReleaseIterator(di);
507 } else if (o->type == REDIS_HASH) {
508 char cmd[]="*4\r\n$4\r\nHSET\r\n";
509
510 /* Emit the HSETs needed to rebuild the hash */
511 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
512 unsigned char *p = zipmapRewind(o->ptr);
513 unsigned char *field, *val;
514 unsigned int flen, vlen;
515
516 while((p = zipmapNext(p,&field,&flen,&val,&vlen)) != NULL) {
517 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
518 if (fwriteBulkObject(fp,&key) == 0) goto werr;
519 if (fwriteBulkString(fp,(char*)field,flen) == 0)
520 goto werr;
521 if (fwriteBulkString(fp,(char*)val,vlen) == 0)
522 goto werr;
523 }
524 } else {
525 dictIterator *di = dictGetIterator(o->ptr);
526 dictEntry *de;
527
528 while((de = dictNext(di)) != NULL) {
529 robj *field = dictGetEntryKey(de);
530 robj *val = dictGetEntryVal(de);
531
532 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
533 if (fwriteBulkObject(fp,&key) == 0) goto werr;
534 if (fwriteBulkObject(fp,field) == 0) goto werr;
535 if (fwriteBulkObject(fp,val) == 0) goto werr;
536 }
537 dictReleaseIterator(di);
538 }
539 } else {
540 redisPanic("Unknown object type");
541 }
542 /* Save the expire time */
543 if (expiretime != -1) {
544 char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n";
545 /* If this key is already expired skip it */
546 if (expiretime < now) continue;
547 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
548 if (fwriteBulkObject(fp,&key) == 0) goto werr;
549 if (fwriteBulkLongLong(fp,expiretime) == 0) goto werr;
550 }
551 if (swapped) decrRefCount(o);
552 }
553 dictReleaseIterator(di);
554 }
555
556 /* Make sure data will not remain on the OS's output buffers */
557 fflush(fp);
558 aof_fsync(fileno(fp));
559 fclose(fp);
560
561 /* Use RENAME to make sure the DB file is changed atomically only
562 * if the generate DB file is ok. */
563 if (rename(tmpfile,filename) == -1) {
564 redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
565 unlink(tmpfile);
566 return REDIS_ERR;
567 }
568 redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed");
569 return REDIS_OK;
570
571 werr:
572 fclose(fp);
573 unlink(tmpfile);
574 redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
575 if (di) dictReleaseIterator(di);
576 return REDIS_ERR;
577 }
578
579 /* This is how rewriting of the append only file in background works:
580 *
581 * 1) The user calls BGREWRITEAOF
582 * 2) Redis calls this function, that forks():
583 * 2a) the child rewrite the append only file in a temp file.
584 * 2b) the parent accumulates differences in server.bgrewritebuf.
585 * 3) When the child finished '2a' exists.
586 * 4) The parent will trap the exit code, if it's OK, will append the
587 * data accumulated into server.bgrewritebuf into the temp file, and
588 * finally will rename(2) the temp file in the actual file name.
589 * The the new file is reopened as the new append only file. Profit!
590 */
591 int rewriteAppendOnlyFileBackground(void) {
592 pid_t childpid;
593
594 if (server.bgrewritechildpid != -1) return REDIS_ERR;
595 if (server.vm_enabled) waitEmptyIOJobsQueue();
596 if ((childpid = fork()) == 0) {
597 /* Child */
598 char tmpfile[256];
599
600 if (server.vm_enabled) vmReopenSwapFile();
601 close(server.fd);
602 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
603 if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
604 _exit(0);
605 } else {
606 _exit(1);
607 }
608 } else {
609 /* Parent */
610 if (childpid == -1) {
611 redisLog(REDIS_WARNING,
612 "Can't rewrite append only file in background: fork: %s",
613 strerror(errno));
614 return REDIS_ERR;
615 }
616 redisLog(REDIS_NOTICE,
617 "Background append only file rewriting started by pid %d",childpid);
618 server.bgrewritechildpid = childpid;
619 updateDictResizePolicy();
620 /* We set appendseldb to -1 in order to force the next call to the
621 * feedAppendOnlyFile() to issue a SELECT command, so the differences
622 * accumulated by the parent into server.bgrewritebuf will start
623 * with a SELECT statement and it will be safe to merge. */
624 server.appendseldb = -1;
625 return REDIS_OK;
626 }
627 return REDIS_OK; /* unreached */
628 }
629
630 void bgrewriteaofCommand(redisClient *c) {
631 if (server.bgrewritechildpid != -1) {
632 addReplySds(c,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
633 return;
634 }
635 if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
636 char *status = "+Background append only file rewriting started\r\n";
637 addReplySds(c,sdsnew(status));
638 } else {
639 addReply(c,shared.err);
640 }
641 }
642
643 void aofRemoveTempFile(pid_t childpid) {
644 char tmpfile[256];
645
646 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
647 unlink(tmpfile);
648 }
649
650 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
651 * Handle this. */
652 void backgroundRewriteDoneHandler(int statloc) {
653 int exitcode = WEXITSTATUS(statloc);
654 int bysignal = WIFSIGNALED(statloc);
655
656 if (!bysignal && exitcode == 0) {
657 int fd;
658 char tmpfile[256];
659
660 redisLog(REDIS_NOTICE,
661 "Background append only file rewriting terminated with success");
662 /* Now it's time to flush the differences accumulated by the parent */
663 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) server.bgrewritechildpid);
664 fd = open(tmpfile,O_WRONLY|O_APPEND);
665 if (fd == -1) {
666 redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno));
667 goto cleanup;
668 }
669 /* Flush our data... */
670 if (write(fd,server.bgrewritebuf,sdslen(server.bgrewritebuf)) !=
671 (signed) sdslen(server.bgrewritebuf)) {
672 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));
673 close(fd);
674 goto cleanup;
675 }
676 redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf));
677 /* Now our work is to rename the temp file into the stable file. And
678 * switch the file descriptor used by the server for append only. */
679 if (rename(tmpfile,server.appendfilename) == -1) {
680 redisLog(REDIS_WARNING,"Can't rename the temp append only file into the stable one: %s", strerror(errno));
681 close(fd);
682 goto cleanup;
683 }
684 /* Mission completed... almost */
685 redisLog(REDIS_NOTICE,"Append only file successfully rewritten.");
686 if (server.appendfd != -1) {
687 /* If append only is actually enabled... */
688 close(server.appendfd);
689 server.appendfd = fd;
690 if (server.appendfsync != APPENDFSYNC_NO) aof_fsync(fd);
691 server.appendseldb = -1; /* Make sure it will issue SELECT */
692 redisLog(REDIS_NOTICE,"The new append only file was selected for future appends.");
693 } else {
694 /* If append only is disabled we just generate a dump in this
695 * format. Why not? */
696 close(fd);
697 }
698 } else if (!bysignal && exitcode != 0) {
699 redisLog(REDIS_WARNING, "Background append only file rewriting error");
700 } else {
701 redisLog(REDIS_WARNING,
702 "Background append only file rewriting terminated by signal %d",
703 WTERMSIG(statloc));
704 }
705 cleanup:
706 sdsfree(server.bgrewritebuf);
707 server.bgrewritebuf = sdsempty();
708 aofRemoveTempFile(server.bgrewritechildpid);
709 server.bgrewritechildpid = -1;
710 }