]>
Commit | Line | Data |
---|---|---|
e2641e09 | 1 | #include "redis.h" |
2 | ||
3 | #include <signal.h> | |
4 | #include <fcntl.h> | |
5 | #include <sys/stat.h> | |
3688d7f3 | 6 | #include <sys/types.h> |
7 | #include <sys/time.h> | |
8 | #include <sys/resource.h> | |
9 | #include <sys/wait.h> | |
e2641e09 | 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; | |
2403fc9f | 192 | c->bufpos = 0; |
e2641e09 | 193 | c->flags = 0; |
194 | /* We set the fake client as a slave waiting for the synchronization | |
195 | * so that Redis will not try to send replies to this client. */ | |
196 | c->replstate = REDIS_REPL_WAIT_BGSAVE_START; | |
197 | c->reply = listCreate(); | |
b67d2345 | 198 | c->watched_keys = listCreate(); |
e2641e09 | 199 | listSetFreeMethod(c->reply,decrRefCount); |
200 | listSetDupMethod(c->reply,dupClientReplyValue); | |
201 | initClientMultiState(c); | |
202 | return c; | |
203 | } | |
204 | ||
205 | void freeFakeClient(struct redisClient *c) { | |
206 | sdsfree(c->querybuf); | |
207 | listRelease(c->reply); | |
b67d2345 | 208 | listRelease(c->watched_keys); |
e2641e09 | 209 | freeClientMultiState(c); |
210 | zfree(c); | |
211 | } | |
212 | ||
213 | /* Replay the append log file. On error REDIS_OK is returned. On non fatal | |
214 | * error (the append only file is zero-length) REDIS_ERR is returned. On | |
215 | * fatal error an error message is logged and the program exists. */ | |
216 | int loadAppendOnlyFile(char *filename) { | |
217 | struct redisClient *fakeClient; | |
218 | FILE *fp = fopen(filename,"r"); | |
219 | struct redis_stat sb; | |
220 | int appendonly = server.appendonly; | |
97e7f8ae | 221 | long loops = 0; |
e2641e09 | 222 | |
4aec2ec8 | 223 | if (fp && redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) { |
224 | fclose(fp); | |
e2641e09 | 225 | return REDIS_ERR; |
4aec2ec8 | 226 | } |
e2641e09 | 227 | |
228 | if (fp == NULL) { | |
229 | redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno)); | |
230 | exit(1); | |
231 | } | |
232 | ||
233 | /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI | |
234 | * to the same file we're about to read. */ | |
235 | server.appendonly = 0; | |
236 | ||
237 | fakeClient = createFakeClient(); | |
97e7f8ae | 238 | startLoading(fp); |
239 | ||
e2641e09 | 240 | while(1) { |
241 | int argc, j; | |
242 | unsigned long len; | |
243 | robj **argv; | |
244 | char buf[128]; | |
245 | sds argsds; | |
246 | struct redisCommand *cmd; | |
e2641e09 | 247 | |
97e7f8ae | 248 | /* Serve the clients from time to time */ |
249 | if (!(loops++ % 1000)) { | |
250 | loadingProgress(ftello(fp)); | |
251 | aeProcessEvents(server.el, AE_FILE_EVENTS|AE_DONT_WAIT); | |
252 | } | |
253 | ||
e2641e09 | 254 | if (fgets(buf,sizeof(buf),fp) == NULL) { |
255 | if (feof(fp)) | |
256 | break; | |
257 | else | |
258 | goto readerr; | |
259 | } | |
260 | if (buf[0] != '*') goto fmterr; | |
261 | argc = atoi(buf+1); | |
262 | argv = zmalloc(sizeof(robj*)*argc); | |
263 | for (j = 0; j < argc; j++) { | |
264 | if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr; | |
265 | if (buf[0] != '$') goto fmterr; | |
266 | len = strtol(buf+1,NULL,10); | |
267 | argsds = sdsnewlen(NULL,len); | |
268 | if (len && fread(argsds,len,1,fp) == 0) goto fmterr; | |
269 | argv[j] = createObject(REDIS_STRING,argsds); | |
270 | if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */ | |
271 | } | |
272 | ||
273 | /* Command lookup */ | |
274 | cmd = lookupCommand(argv[0]->ptr); | |
275 | if (!cmd) { | |
276 | redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr); | |
277 | exit(1); | |
278 | } | |
e2641e09 | 279 | /* Run the command in the context of a fake client */ |
280 | fakeClient->argc = argc; | |
281 | fakeClient->argv = argv; | |
282 | cmd->proc(fakeClient); | |
57b07380 PN |
283 | |
284 | /* The fake client should not have a reply */ | |
285 | redisAssert(fakeClient->bufpos == 0 && listLength(fakeClient->reply) == 0); | |
286 | ||
45b0f6fb PN |
287 | /* Clean up. Command code may have changed argv/argc so we use the |
288 | * argv/argc of the client instead of the local variables. */ | |
289 | for (j = 0; j < fakeClient->argc; j++) | |
290 | decrRefCount(fakeClient->argv[j]); | |
291 | zfree(fakeClient->argv); | |
e2641e09 | 292 | } |
293 | ||
294 | /* This point can only be reached when EOF is reached without errors. | |
295 | * If the client is in the middle of a MULTI/EXEC, log error and quit. */ | |
296 | if (fakeClient->flags & REDIS_MULTI) goto readerr; | |
297 | ||
298 | fclose(fp); | |
299 | freeFakeClient(fakeClient); | |
300 | server.appendonly = appendonly; | |
97e7f8ae | 301 | stopLoading(); |
e2641e09 | 302 | return REDIS_OK; |
303 | ||
304 | readerr: | |
305 | if (feof(fp)) { | |
306 | redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file"); | |
307 | } else { | |
308 | redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno)); | |
309 | } | |
310 | exit(1); | |
311 | fmterr: | |
412e457c | 312 | 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>"); |
e2641e09 | 313 | exit(1); |
314 | } | |
315 | ||
e2641e09 | 316 | /* Write a sequence of commands able to fully rebuild the dataset into |
317 | * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */ | |
318 | int rewriteAppendOnlyFile(char *filename) { | |
319 | dictIterator *di = NULL; | |
320 | dictEntry *de; | |
321 | FILE *fp; | |
322 | char tmpfile[256]; | |
323 | int j; | |
324 | time_t now = time(NULL); | |
325 | ||
326 | /* Note that we have to use a different temp name here compared to the | |
327 | * one used by rewriteAppendOnlyFileBackground() function. */ | |
328 | snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid()); | |
329 | fp = fopen(tmpfile,"w"); | |
330 | if (!fp) { | |
331 | redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno)); | |
332 | return REDIS_ERR; | |
333 | } | |
334 | for (j = 0; j < server.dbnum; j++) { | |
335 | char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n"; | |
336 | redisDb *db = server.db+j; | |
337 | dict *d = db->dict; | |
338 | if (dictSize(d) == 0) continue; | |
339 | di = dictGetIterator(d); | |
340 | if (!di) { | |
341 | fclose(fp); | |
342 | return REDIS_ERR; | |
343 | } | |
344 | ||
345 | /* SELECT the new DB */ | |
346 | if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr; | |
347 | if (fwriteBulkLongLong(fp,j) == 0) goto werr; | |
348 | ||
349 | /* Iterate this DB writing every entry */ | |
350 | while((de = dictNext(di)) != NULL) { | |
6901fe77 | 351 | sds keystr; |
e2641e09 | 352 | robj key, *o; |
353 | time_t expiretime; | |
e2641e09 | 354 | |
355 | keystr = dictGetEntryKey(de); | |
356 | o = dictGetEntryVal(de); | |
357 | initStaticStringObject(key,keystr); | |
16d77878 | 358 | |
e2641e09 | 359 | expiretime = getExpire(db,&key); |
360 | ||
361 | /* Save the key and associated value */ | |
362 | if (o->type == REDIS_STRING) { | |
363 | /* Emit a SET command */ | |
364 | char cmd[]="*3\r\n$3\r\nSET\r\n"; | |
365 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
366 | /* Key and value */ | |
367 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
368 | if (fwriteBulkObject(fp,o) == 0) goto werr; | |
369 | } else if (o->type == REDIS_LIST) { | |
370 | /* Emit the RPUSHes needed to rebuild the list */ | |
371 | char cmd[]="*3\r\n$5\r\nRPUSH\r\n"; | |
372 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { | |
373 | unsigned char *zl = o->ptr; | |
374 | unsigned char *p = ziplistIndex(zl,0); | |
375 | unsigned char *vstr; | |
376 | unsigned int vlen; | |
377 | long long vlong; | |
378 | ||
379 | while(ziplistGet(p,&vstr,&vlen,&vlong)) { | |
380 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
381 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
382 | if (vstr) { | |
383 | if (fwriteBulkString(fp,(char*)vstr,vlen) == 0) | |
384 | goto werr; | |
385 | } else { | |
386 | if (fwriteBulkLongLong(fp,vlong) == 0) | |
387 | goto werr; | |
388 | } | |
389 | p = ziplistNext(zl,p); | |
390 | } | |
391 | } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) { | |
392 | list *list = o->ptr; | |
393 | listNode *ln; | |
394 | listIter li; | |
395 | ||
396 | listRewind(list,&li); | |
397 | while((ln = listNext(&li))) { | |
398 | robj *eleobj = listNodeValue(ln); | |
399 | ||
400 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
401 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
402 | if (fwriteBulkObject(fp,eleobj) == 0) goto werr; | |
403 | } | |
404 | } else { | |
405 | redisPanic("Unknown list encoding"); | |
406 | } | |
407 | } else if (o->type == REDIS_SET) { | |
2767f1c0 | 408 | char cmd[]="*3\r\n$4\r\nSADD\r\n"; |
e2641e09 | 409 | |
2767f1c0 PN |
410 | /* Emit the SADDs needed to rebuild the set */ |
411 | if (o->encoding == REDIS_ENCODING_INTSET) { | |
412 | int ii = 0; | |
23c64fe5 | 413 | int64_t llval; |
2767f1c0 PN |
414 | while(intsetGet(o->ptr,ii++,&llval)) { |
415 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
416 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
417 | if (fwriteBulkLongLong(fp,llval) == 0) goto werr; | |
418 | } | |
419 | } else if (o->encoding == REDIS_ENCODING_HT) { | |
420 | dictIterator *di = dictGetIterator(o->ptr); | |
421 | dictEntry *de; | |
422 | while((de = dictNext(di)) != NULL) { | |
423 | robj *eleobj = dictGetEntryKey(de); | |
424 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
425 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
426 | if (fwriteBulkObject(fp,eleobj) == 0) goto werr; | |
427 | } | |
428 | dictReleaseIterator(di); | |
429 | } else { | |
430 | redisPanic("Unknown set encoding"); | |
e2641e09 | 431 | } |
e2641e09 | 432 | } else if (o->type == REDIS_ZSET) { |
433 | /* Emit the ZADDs needed to rebuild the sorted set */ | |
dddf5335 PN |
434 | char cmd[]="*4\r\n$4\r\nZADD\r\n"; |
435 | ||
436 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { | |
437 | unsigned char *zl = o->ptr; | |
438 | unsigned char *eptr, *sptr; | |
439 | unsigned char *vstr; | |
440 | unsigned int vlen; | |
441 | long long vll; | |
442 | double score; | |
443 | ||
444 | eptr = ziplistIndex(zl,0); | |
445 | redisAssert(eptr != NULL); | |
446 | sptr = ziplistNext(zl,eptr); | |
447 | redisAssert(sptr != NULL); | |
448 | ||
449 | while (eptr != NULL) { | |
450 | redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll)); | |
451 | score = zzlGetScore(sptr); | |
452 | ||
453 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
454 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
455 | if (fwriteBulkDouble(fp,score) == 0) goto werr; | |
456 | if (vstr != NULL) { | |
457 | if (fwriteBulkString(fp,(char*)vstr,vlen) == 0) | |
458 | goto werr; | |
459 | } else { | |
460 | if (fwriteBulkLongLong(fp,vll) == 0) | |
461 | goto werr; | |
462 | } | |
463 | zzlNext(zl,&eptr,&sptr); | |
464 | } | |
100ed062 | 465 | } else if (o->encoding == REDIS_ENCODING_SKIPLIST) { |
dddf5335 PN |
466 | zset *zs = o->ptr; |
467 | dictIterator *di = dictGetIterator(zs->dict); | |
468 | dictEntry *de; | |
469 | ||
470 | while((de = dictNext(di)) != NULL) { | |
471 | robj *eleobj = dictGetEntryKey(de); | |
472 | double *score = dictGetEntryVal(de); | |
473 | ||
474 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
475 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
476 | if (fwriteBulkDouble(fp,*score) == 0) goto werr; | |
477 | if (fwriteBulkObject(fp,eleobj) == 0) goto werr; | |
478 | } | |
479 | dictReleaseIterator(di); | |
480 | } else { | |
481 | redisPanic("Unknown sorted set encoding"); | |
e2641e09 | 482 | } |
e2641e09 | 483 | } else if (o->type == REDIS_HASH) { |
484 | char cmd[]="*4\r\n$4\r\nHSET\r\n"; | |
485 | ||
486 | /* Emit the HSETs needed to rebuild the hash */ | |
487 | if (o->encoding == REDIS_ENCODING_ZIPMAP) { | |
488 | unsigned char *p = zipmapRewind(o->ptr); | |
489 | unsigned char *field, *val; | |
490 | unsigned int flen, vlen; | |
491 | ||
492 | while((p = zipmapNext(p,&field,&flen,&val,&vlen)) != NULL) { | |
493 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
494 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
daf2049d | 495 | if (fwriteBulkString(fp,(char*)field,flen) == 0) |
5bd09cd4 | 496 | goto werr; |
daf2049d | 497 | if (fwriteBulkString(fp,(char*)val,vlen) == 0) |
5bd09cd4 | 498 | goto werr; |
e2641e09 | 499 | } |
500 | } else { | |
501 | dictIterator *di = dictGetIterator(o->ptr); | |
502 | dictEntry *de; | |
503 | ||
504 | while((de = dictNext(di)) != NULL) { | |
505 | robj *field = dictGetEntryKey(de); | |
506 | robj *val = dictGetEntryVal(de); | |
507 | ||
508 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
509 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
5bd09cd4 | 510 | if (fwriteBulkObject(fp,field) == 0) goto werr; |
511 | if (fwriteBulkObject(fp,val) == 0) goto werr; | |
e2641e09 | 512 | } |
513 | dictReleaseIterator(di); | |
514 | } | |
515 | } else { | |
516 | redisPanic("Unknown object type"); | |
517 | } | |
518 | /* Save the expire time */ | |
519 | if (expiretime != -1) { | |
520 | char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n"; | |
521 | /* If this key is already expired skip it */ | |
522 | if (expiretime < now) continue; | |
523 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
524 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
525 | if (fwriteBulkLongLong(fp,expiretime) == 0) goto werr; | |
526 | } | |
e2641e09 | 527 | } |
528 | dictReleaseIterator(di); | |
529 | } | |
530 | ||
531 | /* Make sure data will not remain on the OS's output buffers */ | |
532 | fflush(fp); | |
533 | aof_fsync(fileno(fp)); | |
534 | fclose(fp); | |
535 | ||
536 | /* Use RENAME to make sure the DB file is changed atomically only | |
537 | * if the generate DB file is ok. */ | |
538 | if (rename(tmpfile,filename) == -1) { | |
539 | redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno)); | |
540 | unlink(tmpfile); | |
541 | return REDIS_ERR; | |
542 | } | |
543 | redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed"); | |
544 | return REDIS_OK; | |
545 | ||
546 | werr: | |
547 | fclose(fp); | |
548 | unlink(tmpfile); | |
549 | redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno)); | |
550 | if (di) dictReleaseIterator(di); | |
551 | return REDIS_ERR; | |
552 | } | |
553 | ||
554 | /* This is how rewriting of the append only file in background works: | |
555 | * | |
556 | * 1) The user calls BGREWRITEAOF | |
557 | * 2) Redis calls this function, that forks(): | |
558 | * 2a) the child rewrite the append only file in a temp file. | |
559 | * 2b) the parent accumulates differences in server.bgrewritebuf. | |
560 | * 3) When the child finished '2a' exists. | |
561 | * 4) The parent will trap the exit code, if it's OK, will append the | |
562 | * data accumulated into server.bgrewritebuf into the temp file, and | |
563 | * finally will rename(2) the temp file in the actual file name. | |
564 | * The the new file is reopened as the new append only file. Profit! | |
565 | */ | |
566 | int rewriteAppendOnlyFileBackground(void) { | |
567 | pid_t childpid; | |
615e414c | 568 | long long start; |
e2641e09 | 569 | |
570 | if (server.bgrewritechildpid != -1) return REDIS_ERR; | |
69bfffb4 | 571 | if (server.ds_enabled != 0) { |
572 | redisLog(REDIS_WARNING,"BGREWRITEAOF called with diskstore enabled: AOF is not supported when diskstore is enabled. Operation not performed."); | |
573 | return REDIS_ERR; | |
574 | } | |
615e414c | 575 | start = ustime(); |
e2641e09 | 576 | if ((childpid = fork()) == 0) { |
e2641e09 | 577 | char tmpfile[256]; |
578 | ||
615e414c | 579 | /* Child */ |
a5639e7d PN |
580 | if (server.ipfd > 0) close(server.ipfd); |
581 | if (server.sofd > 0) close(server.sofd); | |
e2641e09 | 582 | snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); |
583 | if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) { | |
584 | _exit(0); | |
585 | } else { | |
586 | _exit(1); | |
587 | } | |
588 | } else { | |
589 | /* Parent */ | |
615e414c | 590 | server.stat_fork_time = ustime()-start; |
e2641e09 | 591 | if (childpid == -1) { |
592 | redisLog(REDIS_WARNING, | |
593 | "Can't rewrite append only file in background: fork: %s", | |
594 | strerror(errno)); | |
595 | return REDIS_ERR; | |
596 | } | |
597 | redisLog(REDIS_NOTICE, | |
598 | "Background append only file rewriting started by pid %d",childpid); | |
599 | server.bgrewritechildpid = childpid; | |
600 | updateDictResizePolicy(); | |
601 | /* We set appendseldb to -1 in order to force the next call to the | |
602 | * feedAppendOnlyFile() to issue a SELECT command, so the differences | |
603 | * accumulated by the parent into server.bgrewritebuf will start | |
604 | * with a SELECT statement and it will be safe to merge. */ | |
605 | server.appendseldb = -1; | |
606 | return REDIS_OK; | |
607 | } | |
608 | return REDIS_OK; /* unreached */ | |
609 | } | |
610 | ||
611 | void bgrewriteaofCommand(redisClient *c) { | |
612 | if (server.bgrewritechildpid != -1) { | |
3ab20376 | 613 | addReplyError(c,"Background append only file rewriting already in progress"); |
e2641e09 | 614 | return; |
615 | } | |
616 | if (rewriteAppendOnlyFileBackground() == REDIS_OK) { | |
3ab20376 | 617 | addReplyStatus(c,"Background append only file rewriting started"); |
e2641e09 | 618 | } else { |
619 | addReply(c,shared.err); | |
620 | } | |
621 | } | |
622 | ||
623 | void aofRemoveTempFile(pid_t childpid) { | |
624 | char tmpfile[256]; | |
625 | ||
626 | snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid); | |
627 | unlink(tmpfile); | |
628 | } | |
629 | ||
630 | /* A background append only file rewriting (BGREWRITEAOF) terminated its work. | |
631 | * Handle this. */ | |
36c17a53 | 632 | void backgroundRewriteDoneHandler(int exitcode, int bysignal) { |
e2641e09 | 633 | if (!bysignal && exitcode == 0) { |
634 | int fd; | |
635 | char tmpfile[256]; | |
636 | ||
637 | redisLog(REDIS_NOTICE, | |
638 | "Background append only file rewriting terminated with success"); | |
639 | /* Now it's time to flush the differences accumulated by the parent */ | |
640 | snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) server.bgrewritechildpid); | |
641 | fd = open(tmpfile,O_WRONLY|O_APPEND); | |
642 | if (fd == -1) { | |
643 | redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno)); | |
644 | goto cleanup; | |
645 | } | |
646 | /* Flush our data... */ | |
647 | if (write(fd,server.bgrewritebuf,sdslen(server.bgrewritebuf)) != | |
648 | (signed) sdslen(server.bgrewritebuf)) { | |
649 | 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)); | |
650 | close(fd); | |
651 | goto cleanup; | |
652 | } | |
653 | redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf)); | |
654 | /* Now our work is to rename the temp file into the stable file. And | |
655 | * switch the file descriptor used by the server for append only. */ | |
656 | if (rename(tmpfile,server.appendfilename) == -1) { | |
657 | redisLog(REDIS_WARNING,"Can't rename the temp append only file into the stable one: %s", strerror(errno)); | |
658 | close(fd); | |
659 | goto cleanup; | |
660 | } | |
661 | /* Mission completed... almost */ | |
662 | redisLog(REDIS_NOTICE,"Append only file successfully rewritten."); | |
663 | if (server.appendfd != -1) { | |
664 | /* If append only is actually enabled... */ | |
665 | close(server.appendfd); | |
666 | server.appendfd = fd; | |
667 | if (server.appendfsync != APPENDFSYNC_NO) aof_fsync(fd); | |
668 | server.appendseldb = -1; /* Make sure it will issue SELECT */ | |
669 | redisLog(REDIS_NOTICE,"The new append only file was selected for future appends."); | |
670 | } else { | |
671 | /* If append only is disabled we just generate a dump in this | |
672 | * format. Why not? */ | |
673 | close(fd); | |
674 | } | |
675 | } else if (!bysignal && exitcode != 0) { | |
676 | redisLog(REDIS_WARNING, "Background append only file rewriting error"); | |
677 | } else { | |
678 | redisLog(REDIS_WARNING, | |
679 | "Background append only file rewriting terminated by signal %d", | |
5b8ce853 | 680 | bysignal); |
e2641e09 | 681 | } |
682 | cleanup: | |
683 | sdsfree(server.bgrewritebuf); | |
684 | server.bgrewritebuf = sdsempty(); | |
685 | aofRemoveTempFile(server.bgrewritechildpid); | |
686 | server.bgrewritechildpid = -1; | |
687 | } |