X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/96ffb2fe97c3e77879e7a4f6f7457397a18bf233..452229b6fb0251f82d9caa5388db1160f10bc2d5:/src/rdb.c diff --git a/src/rdb.c b/src/rdb.c index 019aa9a0..83fe81e5 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -7,39 +7,51 @@ #include #include #include +#include + +/* Convenience wrapper around fwrite, that returns the number of bytes written + * to the file instead of the number of objects (see fwrite(3)) and -1 in the + * case of an error. It also supports a NULL *fp to skip writing altogether + * instead of writing to /dev/null. */ +static int rdbWriteRaw(FILE *fp, void *p, size_t len) { + if (fp != NULL && fwrite(p,len,1,fp) == 0) return -1; + return len; +} int rdbSaveType(FILE *fp, unsigned char type) { - if (fwrite(&type,1,1,fp) == 0) return -1; - return 0; + return rdbWriteRaw(fp,&type,1); } int rdbSaveTime(FILE *fp, time_t t) { int32_t t32 = (int32_t) t; - if (fwrite(&t32,4,1,fp) == 0) return -1; - return 0; + return rdbWriteRaw(fp,&t32,4); } /* check rdbLoadLen() comments for more info */ int rdbSaveLen(FILE *fp, uint32_t len) { unsigned char buf[2]; + int nwritten; if (len < (1<<6)) { /* Save a 6 bit len */ buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6); - if (fwrite(buf,1,1,fp) == 0) return -1; + if (rdbWriteRaw(fp,buf,1) == -1) return -1; + nwritten = 1; } else if (len < (1<<14)) { /* Save a 14 bit len */ buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6); buf[1] = len&0xFF; - if (fwrite(buf,2,1,fp) == 0) return -1; + if (rdbWriteRaw(fp,buf,2) == -1) return -1; + nwritten = 2; } else { /* Save a 32 bit len */ buf[0] = (REDIS_RDB_32BITLEN<<6); - if (fwrite(buf,1,1,fp) == 0) return -1; + if (rdbWriteRaw(fp,buf,1) == -1) return -1; len = htonl(len); - if (fwrite(&len,4,1,fp) == 0) return -1; + if (rdbWriteRaw(fp,&len,4) == -1) return -1; + nwritten = 1+4; } - return 0; + return nwritten; } /* Encode 'value' as an integer if possible (if integer will fit the @@ -92,6 +104,7 @@ int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) { int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) { size_t comprlen, outlen; unsigned char byte; + int n, nwritten = 0; void *out; /* We require at least four bytes compression for this to be worth it */ @@ -105,12 +118,20 @@ int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) { } /* Data compressed! Let's save it on disk */ byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF; - if (fwrite(&byte,1,1,fp) == 0) goto writeerr; - if (rdbSaveLen(fp,comprlen) == -1) goto writeerr; - if (rdbSaveLen(fp,len) == -1) goto writeerr; - if (fwrite(out,comprlen,1,fp) == 0) goto writeerr; + if ((n = rdbWriteRaw(fp,&byte,1)) == -1) goto writeerr; + nwritten += n; + + if ((n = rdbSaveLen(fp,comprlen)) == -1) goto writeerr; + nwritten += n; + + if ((n = rdbSaveLen(fp,len)) == -1) goto writeerr; + nwritten += n; + + if ((n = rdbWriteRaw(fp,out,comprlen)) == -1) goto writeerr; + nwritten += n; + zfree(out); - return comprlen; + return nwritten; writeerr: zfree(out); @@ -121,47 +142,53 @@ writeerr: * representation of an integer value we try to safe it in a special form */ int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) { int enclen; + int n, nwritten = 0; /* Try integer encoding */ if (len <= 11) { unsigned char buf[5]; if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) { - if (fwrite(buf,enclen,1,fp) == 0) return -1; - return 0; + if (rdbWriteRaw(fp,buf,enclen) == -1) return -1; + return enclen; } } /* Try LZF compression - under 20 bytes it's unable to compress even * aaaaaaaaaaaaaaaaaa so skip it */ if (server.rdbcompression && len > 20) { - int retval; - - retval = rdbSaveLzfStringObject(fp,s,len); - if (retval == -1) return -1; - if (retval > 0) return 0; - /* retval == 0 means data can't be compressed, save the old way */ + n = rdbSaveLzfStringObject(fp,s,len); + if (n == -1) return -1; + if (n > 0) return n; + /* Return value of 0 means data can't be compressed, save the old way */ } /* Store verbatim */ - if (rdbSaveLen(fp,len) == -1) return -1; - if (len && fwrite(s,len,1,fp) == 0) return -1; - return 0; + if ((n = rdbSaveLen(fp,len)) == -1) return -1; + nwritten += n; + if (len > 0) { + if (rdbWriteRaw(fp,s,len) == -1) return -1; + nwritten += len; + } + return nwritten; } /* Save a long long value as either an encoded string or a string. */ int rdbSaveLongLongAsStringObject(FILE *fp, long long value) { unsigned char buf[32]; + int n, nwritten = 0; int enclen = rdbEncodeInteger(value,buf); if (enclen > 0) { - if (fwrite(buf,enclen,1,fp) == 0) return -1; + return rdbWriteRaw(fp,buf,enclen); } else { /* Encode as string */ enclen = ll2string((char*)buf,32,value); redisAssert(enclen < 32); - if (rdbSaveLen(fp,enclen) == -1) return -1; - if (fwrite(buf,enclen,1,fp) == 0) return -1; + if ((n = rdbSaveLen(fp,enclen)) == -1) return -1; + nwritten += n; + if ((n = rdbWriteRaw(fp,buf,enclen)) == -1) return -1; + nwritten += n; } - return 0; + return nwritten; } /* Like rdbSaveStringObjectRaw() but handle encoded objects */ @@ -215,15 +242,17 @@ int rdbSaveDoubleValue(FILE *fp, double val) { buf[0] = strlen((char*)buf+1); len = buf[0]+1; } - if (fwrite(buf,len,1,fp) == 0) return -1; - return 0; + return rdbWriteRaw(fp,buf,len); } /* Save a Redis object. */ int rdbSaveObject(FILE *fp, robj *o) { + int n, nwritten = 0; + if (o->type == REDIS_STRING) { /* Save a string value */ - if (rdbSaveStringObject(fp,o) == -1) return -1; + if ((n = rdbSaveStringObject(fp,o)) == -1) return -1; + nwritten += n; } else if (o->type == REDIS_LIST) { /* Save a list value */ if (o->encoding == REDIS_ENCODING_ZIPLIST) { @@ -232,15 +261,19 @@ int rdbSaveObject(FILE *fp, robj *o) { unsigned int vlen; long long vlong; - if (rdbSaveLen(fp,ziplistLen(o->ptr)) == -1) return -1; + if ((n = rdbSaveLen(fp,ziplistLen(o->ptr))) == -1) return -1; + nwritten += n; + p = ziplistIndex(o->ptr,0); while(ziplistGet(p,&vstr,&vlen,&vlong)) { if (vstr) { - if (rdbSaveRawString(fp,vstr,vlen) == -1) + if ((n = rdbSaveRawString(fp,vstr,vlen)) == -1) return -1; + nwritten += n; } else { - if (rdbSaveLongLongAsStringObject(fp,vlong) == -1) + if ((n = rdbSaveLongLongAsStringObject(fp,vlong)) == -1) return -1; + nwritten += n; } p = ziplistNext(o->ptr,p); } @@ -249,11 +282,14 @@ int rdbSaveObject(FILE *fp, robj *o) { listIter li; listNode *ln; - if (rdbSaveLen(fp,listLength(list)) == -1) return -1; + if ((n = rdbSaveLen(fp,listLength(list))) == -1) return -1; + nwritten += n; + listRewind(list,&li); while((ln = listNext(&li))) { robj *eleobj = listNodeValue(ln); - if (rdbSaveStringObject(fp,eleobj) == -1) return -1; + if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + nwritten += n; } } else { redisPanic("Unknown list encoding"); @@ -265,20 +301,26 @@ int rdbSaveObject(FILE *fp, robj *o) { dictIterator *di = dictGetIterator(set); dictEntry *de; - if (rdbSaveLen(fp,dictSize(set)) == -1) return -1; + if ((n = rdbSaveLen(fp,dictSize(set))) == -1) return -1; + nwritten += n; + while((de = dictNext(di)) != NULL) { robj *eleobj = dictGetEntryKey(de); - if (rdbSaveStringObject(fp,eleobj) == -1) return -1; + if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + nwritten += n; } dictReleaseIterator(di); } else if (o->encoding == REDIS_ENCODING_INTSET) { intset *is = o->ptr; - long long llval; + int64_t llval; int i = 0; - if (rdbSaveLen(fp,intsetLen(is)) == -1) return -1; + if ((n = rdbSaveLen(fp,intsetLen(is))) == -1) return -1; + nwritten += n; + while(intsetGet(is,i++,&llval)) { - if (rdbSaveLongLongAsStringObject(fp,llval) == -1) return -1; + if ((n = rdbSaveLongLongAsStringObject(fp,llval)) == -1) return -1; + nwritten += n; } } else { redisPanic("Unknown set encoding"); @@ -289,13 +331,17 @@ int rdbSaveObject(FILE *fp, robj *o) { dictIterator *di = dictGetIterator(zs->dict); dictEntry *de; - if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1; + if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1; + nwritten += n; + while((de = dictNext(di)) != NULL) { robj *eleobj = dictGetEntryKey(de); double *score = dictGetEntryVal(de); - if (rdbSaveStringObject(fp,eleobj) == -1) return -1; - if (rdbSaveDoubleValue(fp,*score) == -1) return -1; + if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + nwritten += n; + if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1; + nwritten += n; } dictReleaseIterator(di); } else if (o->type == REDIS_HASH) { @@ -306,47 +352,72 @@ int rdbSaveObject(FILE *fp, robj *o) { unsigned char *key, *val; unsigned int klen, vlen; - if (rdbSaveLen(fp,count) == -1) return -1; + if ((n = rdbSaveLen(fp,count)) == -1) return -1; + nwritten += n; + while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) { - if (rdbSaveRawString(fp,key,klen) == -1) return -1; - if (rdbSaveRawString(fp,val,vlen) == -1) return -1; + if ((n = rdbSaveRawString(fp,key,klen)) == -1) return -1; + nwritten += n; + if ((n = rdbSaveRawString(fp,val,vlen)) == -1) return -1; + nwritten += n; } } else { dictIterator *di = dictGetIterator(o->ptr); dictEntry *de; - if (rdbSaveLen(fp,dictSize((dict*)o->ptr)) == -1) return -1; + if ((n = rdbSaveLen(fp,dictSize((dict*)o->ptr))) == -1) return -1; + nwritten += n; + while((de = dictNext(di)) != NULL) { robj *key = dictGetEntryKey(de); robj *val = dictGetEntryVal(de); - if (rdbSaveStringObject(fp,key) == -1) return -1; - if (rdbSaveStringObject(fp,val) == -1) return -1; + if ((n = rdbSaveStringObject(fp,key)) == -1) return -1; + nwritten += n; + if ((n = rdbSaveStringObject(fp,val)) == -1) return -1; + nwritten += n; } dictReleaseIterator(di); } } else { redisPanic("Unknown object type"); } - return 0; + return nwritten; } /* Return the length the object will have on disk if saved with * the rdbSaveObject() function. Currently we use a trick to get * this length with very little changes to the code. In the future * we could switch to a faster solution. */ -off_t rdbSavedObjectLen(robj *o, FILE *fp) { - if (fp == NULL) fp = server.devnull; - rewind(fp); - redisAssert(rdbSaveObject(fp,o) != 1); - return ftello(fp); +off_t rdbSavedObjectLen(robj *o) { + int len = rdbSaveObject(NULL,o); + redisAssert(len != -1); + return len; } -/* Return the number of pages required to save this object in the swap file */ -off_t rdbSavedObjectPages(robj *o, FILE *fp) { - off_t bytes = rdbSavedObjectLen(o,fp); - - return (bytes+(server.vm_page_size-1))/server.vm_page_size; +/* Save a key-value pair, with expire time, type, key, value. + * On error -1 is returned. + * On success if the key was actaully saved 1 is returned, otherwise 0 + * is returned (the key was already expired). */ +int rdbSaveKeyValuePair(FILE *fp, redisDb *db, robj *key, robj *val, + time_t now) +{ + time_t expiretime; + + expiretime = getExpire(db,key); + + /* Save the expire time */ + if (expiretime != -1) { + /* If this key is already expired skip it */ + if (expiretime < now) return 0; + if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) return -1; + if (rdbSaveTime(fp,expiretime) == -1) return -1; + } + /* Save type, key, value */ + if (rdbSaveType(fp,val->type) == -1) return -1; + if (rdbSaveStringObject(fp,key) == -1) return -1; + if (rdbSaveObject(fp,val) == -1) return -1; + return 1; } /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */ @@ -358,16 +429,16 @@ int rdbSave(char *filename) { int j; time_t now = time(NULL); - /* Wait for I/O therads to terminate, just in case this is a - * foreground-saving, to avoid seeking the swap file descriptor at the - * same time. */ - if (server.vm_enabled) - waitEmptyIOJobsQueue(); + if (server.ds_enabled) { + cacheForcePointInTime(); + return dsRdbSave(filename); + } snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); fp = fopen(tmpfile,"w"); if (!fp) { - redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno)); + redisLog(REDIS_WARNING, "Failed opening .rdb for saving: %s", + strerror(errno)); return REDIS_ERR; } if (fwrite("REDIS0001",9,1,fp) == 0) goto werr; @@ -389,38 +460,9 @@ int rdbSave(char *filename) { while((de = dictNext(di)) != NULL) { sds keystr = dictGetEntryKey(de); robj key, *o = dictGetEntryVal(de); - time_t expiretime; initStaticStringObject(key,keystr); - expiretime = getExpire(db,&key); - - /* Save the expire time */ - if (expiretime != -1) { - /* If this key is already expired skip it */ - if (expiretime < now) continue; - if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr; - if (rdbSaveTime(fp,expiretime) == -1) goto werr; - } - /* Save the key and associated value. This requires special - * handling if the value is swapped out. */ - if (!server.vm_enabled || o->storage == REDIS_VM_MEMORY || - o->storage == REDIS_VM_SWAPPING) { - /* Save type, key, value */ - if (rdbSaveType(fp,o->type) == -1) goto werr; - if (rdbSaveStringObject(fp,&key) == -1) goto werr; - if (rdbSaveObject(fp,o) == -1) goto werr; - } else { - /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */ - robj *po; - /* Get a preview of the object in memory */ - po = vmPreviewObject(o); - /* Save type, key, value */ - if (rdbSaveType(fp,po->type) == -1) goto werr; - if (rdbSaveStringObject(fp,&key) == -1) goto werr; - if (rdbSaveObject(fp,po) == -1) goto werr; - /* Remove the loaded object from memory */ - decrRefCount(po); - } + if (rdbSaveKeyValuePair(fp,db,&key,o,now) == -1) goto werr; } dictReleaseIterator(di); } @@ -455,17 +497,24 @@ werr: int rdbSaveBackground(char *filename) { pid_t childpid; - if (server.bgsavechildpid != -1) return REDIS_ERR; - if (server.vm_enabled) waitEmptyIOJobsQueue(); + if (server.bgsavechildpid != -1 || + server.bgsavethread != (pthread_t) -1) return REDIS_ERR; + + server.dirty_before_bgsave = server.dirty; + + if (server.ds_enabled) { + cacheForcePointInTime(); + return dsRdbSaveBackground(filename); + } + if ((childpid = fork()) == 0) { + int retval; + /* Child */ - if (server.vm_enabled) vmReopenSwapFile(); - close(server.fd); - if (rdbSave(filename) == REDIS_OK) { - _exit(0); - } else { - _exit(1); - } + if (server.ipfd > 0) close(server.ipfd); + if (server.sofd > 0) close(server.sofd); + retval = rdbSave(filename); + _exit((retval == REDIS_OK) ? 0 : 1); } else { /* Parent */ if (childpid == -1) { @@ -702,7 +751,7 @@ robj *rdbLoadObject(int type, FILE *fp) { if (o->encoding == REDIS_ENCODING_INTSET) { /* Fetch integer value from element */ - if (getLongLongFromObject(ele,&llval) == REDIS_OK) { + if (isObjectRepresentableAsLongLong(ele,&llval) == REDIS_OK) { o->ptr = intsetAdd(o->ptr,llval,NULL); } else { setTypeConvert(o,REDIS_ENCODING_HT); @@ -714,6 +763,8 @@ robj *rdbLoadObject(int type, FILE *fp) { * to regular hashtable encoded set */ if (o->encoding == REDIS_ENCODING_HT) { dictAdd((dict*)o->ptr,ele,NULL); + } else { + decrRefCount(ele); } } } else if (type == REDIS_ZSET) { @@ -727,13 +778,14 @@ robj *rdbLoadObject(int type, FILE *fp) { /* Load every single element of the list/set */ while(zsetlen--) { robj *ele; - double *score = zmalloc(sizeof(double)); + double score; + zskiplistNode *znode; if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL; ele = tryObjectEncoding(ele); - if (rdbLoadDoubleValue(fp,score) == -1) return NULL; - dictAdd(zs->dict,ele,score); - zslInsert(zs->zsl,*score,ele); + if (rdbLoadDoubleValue(fp,&score) == -1) return NULL; + znode = zslInsert(zs->zsl,score,ele); + dictAdd(zs->dict,ele,&znode->score); incrRefCount(ele); /* added to skiplist */ } } else if (type == REDIS_HASH) { @@ -788,14 +840,39 @@ robj *rdbLoadObject(int type, FILE *fp) { return o; } +/* Mark that we are loading in the global state and setup the fields + * needed to provide loading stats. */ +void startLoading(FILE *fp) { + struct stat sb; + + /* Load the DB */ + server.loading = 1; + server.loading_start_time = time(NULL); + if (fstat(fileno(fp), &sb) == -1) { + server.loading_total_bytes = 1; /* just to avoid division by zero */ + } else { + server.loading_total_bytes = sb.st_size; + } +} + +/* Refresh the loading progress info */ +void loadingProgress(off_t pos) { + server.loading_loaded_bytes = pos; +} + +/* Loading finished */ +void stopLoading(void) { + server.loading = 0; +} + int rdbLoad(char *filename) { FILE *fp; uint32_t dbid; int type, retval, rdbver; - int swap_all_values = 0; redisDb *db = server.db+0; char buf[1024]; time_t expiretime, now = time(NULL); + long loops = 0; fp = fopen(filename,"r"); if (!fp) return REDIS_ERR; @@ -812,11 +889,18 @@ int rdbLoad(char *filename) { redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver); return REDIS_ERR; } + + startLoading(fp); while(1) { robj *key, *val; - int force_swapout; - expiretime = -1; + + /* Serve the clients from time to time */ + if (!(loops++ % 1000)) { + loadingProgress(ftello(fp)); + aeProcessEvents(server.el, AE_FILE_EVENTS|AE_DONT_WAIT); + } + /* Read type. */ if ((type = rdbLoadType(fp)) == -1) goto eoferr; if (type == REDIS_EXPIRETIME) { @@ -855,46 +939,10 @@ int rdbLoad(char *filename) { /* Set the expire time if needed */ if (expiretime != -1) setExpire(db,key,expiretime); - /* Handle swapping while loading big datasets when VM is on */ - - /* If we detecter we are hopeless about fitting something in memory - * we just swap every new key on disk. Directly... - * Note that's important to check for this condition before resorting - * to random sampling, otherwise we may try to swap already - * swapped keys. */ - if (swap_all_values) { - dictEntry *de = dictFind(db->dict,key->ptr); - - /* de may be NULL since the key already expired */ - if (de) { - vmpointer *vp; - val = dictGetEntryVal(de); - - if (val->refcount == 1 && - (vp = vmSwapObjectBlocking(val)) != NULL) - dictGetEntryVal(de) = vp; - } - decrRefCount(key); - continue; - } decrRefCount(key); - - /* Flush data on disk once 32 MB of additional RAM are used... */ - force_swapout = 0; - if ((zmalloc_used_memory() - server.vm_max_memory) > 1024*1024*32) - force_swapout = 1; - - /* If we have still some hope of having some value fitting memory - * then we try random sampling. */ - if (!swap_all_values && server.vm_enabled && force_swapout) { - while (zmalloc_used_memory() > server.vm_max_memory) { - if (vmSwapOneObjectBlocking() == REDIS_ERR) break; - } - if (zmalloc_used_memory() > server.vm_max_memory) - swap_all_values = 1; /* We are already using too much mem */ - } } fclose(fp); + stopLoading(); return REDIS_OK; eoferr: /* unexpected end of file is handled here with a fatal exit */ @@ -904,24 +952,47 @@ eoferr: /* unexpected end of file is handled here with a fatal exit */ } /* A background saving child (BGSAVE) terminated its work. Handle this. */ -void backgroundSaveDoneHandler(int statloc) { - int exitcode = WEXITSTATUS(statloc); - int bysignal = WIFSIGNALED(statloc); - +void backgroundSaveDoneHandler(int exitcode, int bysignal) { if (!bysignal && exitcode == 0) { redisLog(REDIS_NOTICE, "Background saving terminated with success"); - server.dirty = 0; + server.dirty = server.dirty - server.dirty_before_bgsave; server.lastsave = time(NULL); } else if (!bysignal && exitcode != 0) { redisLog(REDIS_WARNING, "Background saving error"); } else { redisLog(REDIS_WARNING, - "Background saving terminated by signal %d", WTERMSIG(statloc)); + "Background saving terminated by signal %d", bysignal); rdbRemoveTempFile(server.bgsavechildpid); } server.bgsavechildpid = -1; + server.bgsavethread = (pthread_t) -1; + server.bgsavethread_state = REDIS_BGSAVE_THREAD_UNACTIVE; /* Possibly there are slaves waiting for a BGSAVE in order to be served * (the first stage of SYNC is a bulk transfer of dump.rdb) */ updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR); } + +void saveCommand(redisClient *c) { + if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) { + addReplyError(c,"Background save already in progress"); + return; + } + if (rdbSave(server.dbfilename) == REDIS_OK) { + addReply(c,shared.ok); + } else { + addReply(c,shared.err); + } +} + +void bgsaveCommand(redisClient *c) { + if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) { + addReplyError(c,"Background save already in progress"); + return; + } + if (rdbSaveBackground(server.dbfilename) == REDIS_OK) { + addReplyStatus(c,"Background saving started"); + } else { + addReply(c,shared.err); + } +}