]>
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) { | |
e2641e09 | 63 | ssize_t nwritten; |
64 | ||
65 | if (sdslen(server.aofbuf) == 0) return; | |
66 | ||
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 | |
71 | * or alike */ | |
a57225c2 PN |
72 | nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf)); |
73 | if (nwritten != (signed)sdslen(server.aofbuf)) { | |
e2641e09 | 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. */ | |
a57225c2 | 77 | if (nwritten == -1) { |
e2641e09 | 78 | redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno)); |
a57225c2 | 79 | } else { |
e2641e09 | 80 | redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno)); |
a57225c2 PN |
81 | } |
82 | exit(1); | |
e2641e09 | 83 | } |
b333e239 | 84 | server.appendonly_current_size += nwritten; |
e2641e09 | 85 | |
f990782f PN |
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); | |
90 | } else { | |
91 | sdsfree(server.aofbuf); | |
92 | server.aofbuf = sdsempty(); | |
93 | } | |
94 | ||
29732248 PN |
95 | /* Don't fsync if no-appendfsync-on-rewrite is set to yes and there are |
96 | * children doing I/O in the background. */ | |
e2641e09 | 97 | if (server.no_appendfsync_on_rewrite && |
98 | (server.bgrewritechildpid != -1 || server.bgsavechildpid != -1)) | |
99 | return; | |
29732248 PN |
100 | |
101 | /* Perform the fsync if needed. */ | |
e2641e09 | 102 | if (server.appendfsync == APPENDFSYNC_ALWAYS || |
103 | (server.appendfsync == APPENDFSYNC_EVERYSEC && | |
29732248 | 104 | server.unixtime > server.lastfsync)) |
e2641e09 | 105 | { |
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 */ | |
29732248 | 109 | server.lastfsync = server.unixtime; |
e2641e09 | 110 | } |
111 | } | |
112 | ||
d1ec6c8b PN |
113 | sds catAppendOnlyGenericCommand(sds dst, int argc, robj **argv) { |
114 | char buf[32]; | |
115 | int len, j; | |
116 | robj *o; | |
117 | ||
118 | buf[0] = '*'; | |
119 | len = 1+ll2string(buf+1,sizeof(buf)-1,argc); | |
120 | buf[len++] = '\r'; | |
121 | buf[len++] = '\n'; | |
122 | dst = sdscatlen(dst,buf,len); | |
123 | ||
e2641e09 | 124 | for (j = 0; j < argc; j++) { |
d1ec6c8b PN |
125 | o = getDecodedObject(argv[j]); |
126 | buf[0] = '$'; | |
127 | len = 1+ll2string(buf+1,sizeof(buf)-1,sdslen(o->ptr)); | |
128 | buf[len++] = '\r'; | |
129 | buf[len++] = '\n'; | |
130 | dst = sdscatlen(dst,buf,len); | |
131 | dst = sdscatlen(dst,o->ptr,sdslen(o->ptr)); | |
132 | dst = sdscatlen(dst,"\r\n",2); | |
e2641e09 | 133 | decrRefCount(o); |
134 | } | |
d1ec6c8b | 135 | return dst; |
e2641e09 | 136 | } |
137 | ||
138 | sds catAppendOnlyExpireAtCommand(sds buf, robj *key, robj *seconds) { | |
139 | int argc = 3; | |
140 | long when; | |
141 | robj *argv[3]; | |
142 | ||
143 | /* Make sure we can use strtol */ | |
144 | seconds = getDecodedObject(seconds); | |
145 | when = time(NULL)+strtol(seconds->ptr,NULL,10); | |
146 | decrRefCount(seconds); | |
147 | ||
148 | argv[0] = createStringObject("EXPIREAT",8); | |
149 | argv[1] = key; | |
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]); | |
155 | return buf; | |
156 | } | |
157 | ||
158 | void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) { | |
159 | sds buf = sdsempty(); | |
160 | robj *tmpargv[3]; | |
161 | ||
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) { | |
165 | char seldb[64]; | |
166 | ||
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; | |
171 | } | |
172 | ||
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]); | |
184 | } else { | |
185 | buf = catAppendOnlyGenericCommand(buf,argc,argv); | |
186 | } | |
187 | ||
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)); | |
192 | ||
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)); | |
199 | ||
200 | sdsfree(buf); | |
201 | } | |
202 | ||
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)); | |
207 | ||
208 | selectDb(c,0); | |
209 | c->fd = -1; | |
210 | c->querybuf = sdsempty(); | |
211 | c->argc = 0; | |
212 | c->argv = NULL; | |
2403fc9f | 213 | c->bufpos = 0; |
e2641e09 | 214 | c->flags = 0; |
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(); | |
b67d2345 | 219 | c->watched_keys = listCreate(); |
e2641e09 | 220 | listSetFreeMethod(c->reply,decrRefCount); |
221 | listSetDupMethod(c->reply,dupClientReplyValue); | |
222 | initClientMultiState(c); | |
223 | return c; | |
224 | } | |
225 | ||
226 | void freeFakeClient(struct redisClient *c) { | |
227 | sdsfree(c->querybuf); | |
228 | listRelease(c->reply); | |
b67d2345 | 229 | listRelease(c->watched_keys); |
e2641e09 | 230 | freeClientMultiState(c); |
231 | zfree(c); | |
232 | } | |
233 | ||
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; | |
97e7f8ae | 242 | long loops = 0; |
e2641e09 | 243 | |
4aec2ec8 | 244 | if (fp && redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) { |
b333e239 | 245 | server.appendonly_current_size = 0; |
4aec2ec8 | 246 | fclose(fp); |
e2641e09 | 247 | return REDIS_ERR; |
4aec2ec8 | 248 | } |
e2641e09 | 249 | |
250 | if (fp == NULL) { | |
251 | redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno)); | |
252 | exit(1); | |
253 | } | |
254 | ||
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; | |
258 | ||
259 | fakeClient = createFakeClient(); | |
97e7f8ae | 260 | startLoading(fp); |
261 | ||
e2641e09 | 262 | while(1) { |
263 | int argc, j; | |
264 | unsigned long len; | |
265 | robj **argv; | |
266 | char buf[128]; | |
267 | sds argsds; | |
268 | struct redisCommand *cmd; | |
e2641e09 | 269 | |
97e7f8ae | 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); | |
274 | } | |
275 | ||
e2641e09 | 276 | if (fgets(buf,sizeof(buf),fp) == NULL) { |
277 | if (feof(fp)) | |
278 | break; | |
279 | else | |
280 | goto readerr; | |
281 | } | |
282 | if (buf[0] != '*') goto fmterr; | |
283 | argc = atoi(buf+1); | |
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 */ | |
293 | } | |
294 | ||
295 | /* Command lookup */ | |
296 | cmd = lookupCommand(argv[0]->ptr); | |
297 | if (!cmd) { | |
298 | redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr); | |
299 | exit(1); | |
300 | } | |
e2641e09 | 301 | /* Run the command in the context of a fake client */ |
302 | fakeClient->argc = argc; | |
303 | fakeClient->argv = argv; | |
304 | cmd->proc(fakeClient); | |
57b07380 PN |
305 | |
306 | /* The fake client should not have a reply */ | |
307 | redisAssert(fakeClient->bufpos == 0 && listLength(fakeClient->reply) == 0); | |
ef67a2fc | 308 | /* The fake client should never get blocked */ |
309 | redisAssert((fakeClient->flags & REDIS_BLOCKED) == 0); | |
57b07380 | 310 | |
45b0f6fb PN |
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); | |
e2641e09 | 316 | } |
317 | ||
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; | |
321 | ||
322 | fclose(fp); | |
323 | freeFakeClient(fakeClient); | |
324 | server.appendonly = appendonly; | |
97e7f8ae | 325 | stopLoading(); |
b333e239 | 326 | aofUpdateCurrentSize(); |
c66bf1fa | 327 | server.auto_aofrewrite_base_size = server.appendonly_current_size; |
e2641e09 | 328 | return REDIS_OK; |
329 | ||
330 | readerr: | |
331 | if (feof(fp)) { | |
332 | redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file"); | |
333 | } else { | |
334 | redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno)); | |
335 | } | |
336 | exit(1); | |
337 | fmterr: | |
412e457c | 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>"); |
e2641e09 | 339 | exit(1); |
340 | } | |
341 | ||
e2641e09 | 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; | |
346 | dictEntry *de; | |
347 | FILE *fp; | |
348 | char tmpfile[256]; | |
349 | int j; | |
350 | time_t now = time(NULL); | |
351 | ||
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"); | |
356 | if (!fp) { | |
357 | redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno)); | |
358 | return REDIS_ERR; | |
359 | } | |
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; | |
363 | dict *d = db->dict; | |
364 | if (dictSize(d) == 0) continue; | |
591f29e0 | 365 | di = dictGetSafeIterator(d); |
e2641e09 | 366 | if (!di) { |
367 | fclose(fp); | |
368 | return REDIS_ERR; | |
369 | } | |
370 | ||
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; | |
374 | ||
375 | /* Iterate this DB writing every entry */ | |
376 | while((de = dictNext(di)) != NULL) { | |
6901fe77 | 377 | sds keystr; |
e2641e09 | 378 | robj key, *o; |
379 | time_t expiretime; | |
e2641e09 | 380 | |
381 | keystr = dictGetEntryKey(de); | |
382 | o = dictGetEntryVal(de); | |
383 | initStaticStringObject(key,keystr); | |
16d77878 | 384 | |
e2641e09 | 385 | expiretime = getExpire(db,&key); |
386 | ||
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; | |
392 | /* Key and value */ | |
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); | |
401 | unsigned char *vstr; | |
402 | unsigned int vlen; | |
403 | long long vlong; | |
404 | ||
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; | |
408 | if (vstr) { | |
409 | if (fwriteBulkString(fp,(char*)vstr,vlen) == 0) | |
410 | goto werr; | |
411 | } else { | |
412 | if (fwriteBulkLongLong(fp,vlong) == 0) | |
413 | goto werr; | |
414 | } | |
415 | p = ziplistNext(zl,p); | |
416 | } | |
417 | } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) { | |
418 | list *list = o->ptr; | |
419 | listNode *ln; | |
420 | listIter li; | |
421 | ||
422 | listRewind(list,&li); | |
423 | while((ln = listNext(&li))) { | |
424 | robj *eleobj = listNodeValue(ln); | |
425 | ||
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; | |
429 | } | |
430 | } else { | |
431 | redisPanic("Unknown list encoding"); | |
432 | } | |
433 | } else if (o->type == REDIS_SET) { | |
2767f1c0 | 434 | char cmd[]="*3\r\n$4\r\nSADD\r\n"; |
e2641e09 | 435 | |
2767f1c0 PN |
436 | /* Emit the SADDs needed to rebuild the set */ |
437 | if (o->encoding == REDIS_ENCODING_INTSET) { | |
438 | int ii = 0; | |
23c64fe5 | 439 | int64_t llval; |
2767f1c0 PN |
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; | |
444 | } | |
445 | } else if (o->encoding == REDIS_ENCODING_HT) { | |
446 | dictIterator *di = dictGetIterator(o->ptr); | |
447 | dictEntry *de; | |
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; | |
453 | } | |
454 | dictReleaseIterator(di); | |
455 | } else { | |
456 | redisPanic("Unknown set encoding"); | |
e2641e09 | 457 | } |
e2641e09 | 458 | } else if (o->type == REDIS_ZSET) { |
459 | /* Emit the ZADDs needed to rebuild the sorted set */ | |
dddf5335 PN |
460 | char cmd[]="*4\r\n$4\r\nZADD\r\n"; |
461 | ||
462 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { | |
463 | unsigned char *zl = o->ptr; | |
464 | unsigned char *eptr, *sptr; | |
465 | unsigned char *vstr; | |
466 | unsigned int vlen; | |
467 | long long vll; | |
468 | double score; | |
469 | ||
470 | eptr = ziplistIndex(zl,0); | |
471 | redisAssert(eptr != NULL); | |
472 | sptr = ziplistNext(zl,eptr); | |
473 | redisAssert(sptr != NULL); | |
474 | ||
475 | while (eptr != NULL) { | |
476 | redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll)); | |
477 | score = zzlGetScore(sptr); | |
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 (vstr != NULL) { | |
483 | if (fwriteBulkString(fp,(char*)vstr,vlen) == 0) | |
484 | goto werr; | |
485 | } else { | |
486 | if (fwriteBulkLongLong(fp,vll) == 0) | |
487 | goto werr; | |
488 | } | |
489 | zzlNext(zl,&eptr,&sptr); | |
490 | } | |
100ed062 | 491 | } else if (o->encoding == REDIS_ENCODING_SKIPLIST) { |
dddf5335 PN |
492 | zset *zs = o->ptr; |
493 | dictIterator *di = dictGetIterator(zs->dict); | |
494 | dictEntry *de; | |
495 | ||
496 | while((de = dictNext(di)) != NULL) { | |
497 | robj *eleobj = dictGetEntryKey(de); | |
498 | double *score = dictGetEntryVal(de); | |
499 | ||
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; | |
504 | } | |
505 | dictReleaseIterator(di); | |
506 | } else { | |
507 | redisPanic("Unknown sorted set encoding"); | |
e2641e09 | 508 | } |
e2641e09 | 509 | } else if (o->type == REDIS_HASH) { |
510 | char cmd[]="*4\r\n$4\r\nHSET\r\n"; | |
511 | ||
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; | |
517 | ||
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; | |
daf2049d | 521 | if (fwriteBulkString(fp,(char*)field,flen) == 0) |
5bd09cd4 | 522 | goto werr; |
daf2049d | 523 | if (fwriteBulkString(fp,(char*)val,vlen) == 0) |
5bd09cd4 | 524 | goto werr; |
e2641e09 | 525 | } |
526 | } else { | |
527 | dictIterator *di = dictGetIterator(o->ptr); | |
528 | dictEntry *de; | |
529 | ||
530 | while((de = dictNext(di)) != NULL) { | |
531 | robj *field = dictGetEntryKey(de); | |
532 | robj *val = dictGetEntryVal(de); | |
533 | ||
534 | if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; | |
535 | if (fwriteBulkObject(fp,&key) == 0) goto werr; | |
5bd09cd4 | 536 | if (fwriteBulkObject(fp,field) == 0) goto werr; |
537 | if (fwriteBulkObject(fp,val) == 0) goto werr; | |
e2641e09 | 538 | } |
539 | dictReleaseIterator(di); | |
540 | } | |
541 | } else { | |
542 | redisPanic("Unknown object type"); | |
543 | } | |
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; | |
552 | } | |
e2641e09 | 553 | } |
554 | dictReleaseIterator(di); | |
555 | } | |
556 | ||
557 | /* Make sure data will not remain on the OS's output buffers */ | |
558 | fflush(fp); | |
559 | aof_fsync(fileno(fp)); | |
560 | fclose(fp); | |
561 | ||
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)); | |
566 | unlink(tmpfile); | |
567 | return REDIS_ERR; | |
568 | } | |
569 | redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed"); | |
570 | return REDIS_OK; | |
571 | ||
572 | werr: | |
573 | fclose(fp); | |
574 | unlink(tmpfile); | |
575 | redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno)); | |
576 | if (di) dictReleaseIterator(di); | |
577 | return REDIS_ERR; | |
578 | } | |
579 | ||
580 | /* This is how rewriting of the append only file in background works: | |
581 | * | |
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! | |
591 | */ | |
592 | int rewriteAppendOnlyFileBackground(void) { | |
593 | pid_t childpid; | |
615e414c | 594 | long long start; |
e2641e09 | 595 | |
596 | if (server.bgrewritechildpid != -1) return REDIS_ERR; | |
615e414c | 597 | start = ustime(); |
e2641e09 | 598 | if ((childpid = fork()) == 0) { |
e2641e09 | 599 | char tmpfile[256]; |
600 | ||
615e414c | 601 | /* Child */ |
a5639e7d PN |
602 | if (server.ipfd > 0) close(server.ipfd); |
603 | if (server.sofd > 0) close(server.sofd); | |
e2641e09 | 604 | snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); |
605 | if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) { | |
606 | _exit(0); | |
607 | } else { | |
608 | _exit(1); | |
609 | } | |
610 | } else { | |
611 | /* Parent */ | |
615e414c | 612 | server.stat_fork_time = ustime()-start; |
e2641e09 | 613 | if (childpid == -1) { |
614 | redisLog(REDIS_WARNING, | |
615 | "Can't rewrite append only file in background: fork: %s", | |
616 | strerror(errno)); | |
617 | return REDIS_ERR; | |
618 | } | |
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; | |
628 | return REDIS_OK; | |
629 | } | |
630 | return REDIS_OK; /* unreached */ | |
631 | } | |
632 | ||
633 | void bgrewriteaofCommand(redisClient *c) { | |
634 | if (server.bgrewritechildpid != -1) { | |
3ab20376 | 635 | addReplyError(c,"Background append only file rewriting already in progress"); |
b333e239 | 636 | } else if (server.bgsavechildpid != -1) { |
637 | server.aofrewrite_scheduled = 1; | |
9e40bce3 | 638 | addReplyStatus(c,"Background append only file rewriting scheduled"); |
b333e239 | 639 | } else if (rewriteAppendOnlyFileBackground() == REDIS_OK) { |
3ab20376 | 640 | addReplyStatus(c,"Background append only file rewriting started"); |
e2641e09 | 641 | } else { |
642 | addReply(c,shared.err); | |
643 | } | |
644 | } | |
645 | ||
646 | void aofRemoveTempFile(pid_t childpid) { | |
647 | char tmpfile[256]; | |
648 | ||
649 | snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid); | |
650 | unlink(tmpfile); | |
651 | } | |
652 | ||
b333e239 | 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; | |
659 | ||
660 | if (redis_fstat(server.appendfd,&sb) == -1) { | |
661 | redisLog(REDIS_WARNING,"Unable to check the AOF length: %s", | |
662 | strerror(errno)); | |
663 | } else { | |
664 | server.appendonly_current_size = sb.st_size; | |
665 | } | |
666 | } | |
667 | ||
e2641e09 | 668 | /* A background append only file rewriting (BGREWRITEAOF) terminated its work. |
669 | * Handle this. */ | |
36c17a53 | 670 | void backgroundRewriteDoneHandler(int exitcode, int bysignal) { |
e2641e09 | 671 | if (!bysignal && exitcode == 0) { |
672 | int fd; | |
673 | char tmpfile[256]; | |
674 | ||
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); | |
680 | if (fd == -1) { | |
681 | redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno)); | |
682 | goto cleanup; | |
683 | } | |
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)); | |
688 | close(fd); | |
689 | goto cleanup; | |
690 | } | |
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)); | |
696 | close(fd); | |
697 | goto cleanup; | |
698 | } | |
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."); | |
b333e239 | 708 | aofUpdateCurrentSize(); |
c66bf1fa | 709 | server.auto_aofrewrite_base_size = server.appendonly_current_size; |
e2641e09 | 710 | } else { |
711 | /* If append only is disabled we just generate a dump in this | |
712 | * format. Why not? */ | |
713 | close(fd); | |
714 | } | |
715 | } else if (!bysignal && exitcode != 0) { | |
716 | redisLog(REDIS_WARNING, "Background append only file rewriting error"); | |
717 | } else { | |
718 | redisLog(REDIS_WARNING, | |
719 | "Background append only file rewriting terminated by signal %d", | |
5b8ce853 | 720 | bysignal); |
e2641e09 | 721 | } |
722 | cleanup: | |
723 | sdsfree(server.bgrewritebuf); | |
724 | server.bgrewritebuf = sdsempty(); | |
725 | aofRemoveTempFile(server.bgrewritechildpid); | |
726 | server.bgrewritechildpid = -1; | |
727 | } |