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