X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/221782ccc69b4c56608942f3fe9e47773a32866e..b83e95830f45eb5e3b4e7c0a997d611f75f758b8:/src/rdb.c diff --git a/src/rdb.c b/src/rdb.c index ee992809..d2d54807 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -1,3 +1,6 @@ +#include "redis.h" +#include "lzf.h" /* LZF compression library */ + #include #include #include @@ -5,13 +8,11 @@ #include #include #include -#include "rdb.h" -#include "lzf.h" /* LZF compression library */ static int rdbWriteRaw(rio *rdb, void *p, size_t len) { - if (rioWrite(rdb,p,len) == 0) + if (rdb && rioWrite(rdb,p,len) == 0) return -1; - return 1; + return len; } int rdbSaveType(rio *rdb, unsigned char type) { @@ -284,7 +285,7 @@ int rdbSaveStringObject(rio *rdb, robj *obj) { if (obj->encoding == REDIS_ENCODING_INT) { return rdbSaveLongLongAsStringObject(rdb,(long)obj->ptr); } else { - redisAssert(obj->encoding == REDIS_ENCODING_RAW); + redisAssertWithInfo(NULL,obj,obj->encoding == REDIS_ENCODING_RAW); return rdbSaveRawString(rdb,obj->ptr,sdslen(obj->ptr)); } } @@ -553,7 +554,7 @@ int rdbSaveObject(rio *rdb, robj *o) { * we could switch to a faster solution. */ off_t rdbSavedObjectLen(robj *o) { int len = rdbSaveObject(NULL,o); - redisAssert(len != -1); + redisAssertWithInfo(NULL,o,len != -1); return len; } @@ -589,11 +590,6 @@ int rdbSave(char *filename) { FILE *fp; rio rdb; - if (server.ds_enabled) { - cacheForcePointInTime(); - return dsRdbSave(filename); - } - snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); fp = fopen(tmpfile,"w"); if (!fp) { @@ -602,14 +598,14 @@ int rdbSave(char *filename) { return REDIS_ERR; } - rdb = rioInitWithFile(fp); + rioInitWithFile(&rdb,fp); if (rdbWriteRaw(&rdb,"REDIS0002",9) == -1) goto werr; for (j = 0; j < server.dbnum; j++) { redisDb *db = server.db+j; dict *d = db->dict; if (dictSize(d) == 0) continue; - di = dictGetIterator(d); + di = dictGetSafeIterator(d); if (!di) { fclose(fp); return REDIS_ERR; @@ -661,17 +657,13 @@ werr: int rdbSaveBackground(char *filename) { pid_t childpid; + long long start; - if (server.bgsavechildpid != -1 || - server.bgsavethread != (pthread_t) -1) return REDIS_ERR; + if (server.bgsavechildpid != -1) return REDIS_ERR; server.dirty_before_bgsave = server.dirty; - if (server.ds_enabled) { - cacheForcePointInTime(); - return dsRdbSaveBackground(filename); - } - + start = ustime(); if ((childpid = fork()) == 0) { int retval; @@ -682,6 +674,7 @@ int rdbSaveBackground(char *filename) { _exit((retval == REDIS_OK) ? 0 : 1); } else { /* Parent */ + server.stat_fork_time = ustime()-start; if (childpid == -1) { redisLog(REDIS_WARNING,"Can't save in background: fork: %s", strerror(errno)); @@ -946,7 +939,7 @@ void stopLoading(void) { int rdbLoad(char *filename) { uint32_t dbid; - int type, retval, rdbver; + int type, rdbver; redisDb *db = server.db+0; char buf[1024]; time_t expiretime, now = time(NULL); @@ -955,23 +948,28 @@ int rdbLoad(char *filename) { rio rdb; fp = fopen(filename,"r"); - if (!fp) return REDIS_ERR; - if (fread(buf,9,1,fp) == 0) goto eoferr; + if (!fp) { + errno = ENOENT; + return REDIS_ERR; + } + rioInitWithFile(&rdb,fp); + if (rioRead(&rdb,buf,9) == 0) goto eoferr; buf[9] = '\0'; if (memcmp(buf,"REDIS",5) != 0) { fclose(fp); redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file"); + errno = EINVAL; return REDIS_ERR; } rdbver = atoi(buf+5); if (rdbver < 1 || rdbver > 2) { fclose(fp); redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver); + errno = EINVAL; return REDIS_ERR; } startLoading(fp); - rdb = rioInitWithFile(fp); while(1) { robj *key, *val; expiretime = -1; @@ -1015,11 +1013,8 @@ int rdbLoad(char *filename) { continue; } /* Add the new object in the hash table */ - retval = dbAdd(db,key,val); - if (retval == REDIS_ERR) { - redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr); - exit(1); - } + dbAdd(db,key,val); + /* Set the expire time if needed */ if (expiretime != -1) setExpire(db,key,expiretime); @@ -1050,15 +1045,13 @@ void backgroundSaveDoneHandler(int exitcode, int 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) { + if (server.bgsavechildpid != -1) { addReplyError(c,"Background save already in progress"); return; } @@ -1070,11 +1063,11 @@ void saveCommand(redisClient *c) { } void bgsaveCommand(redisClient *c) { - if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) { + if (server.bgsavechildpid != -1) { addReplyError(c,"Background save already in progress"); - return; - } - if (rdbSaveBackground(server.dbfilename) == REDIS_OK) { + } else if (server.bgrewritechildpid != -1) { + addReplyError(c,"Can't BGSAVE while AOF log rewriting is in progress"); + } else if (rdbSaveBackground(server.dbfilename) == REDIS_OK) { addReplyStatus(c,"Background saving started"); } else { addReply(c,shared.err);