提交 5b8ce853 编写于 作者: A antirez

more work towards diskstore bgsave

上级 36c17a53
...@@ -17,6 +17,9 @@ DISKSTORE TODO ...@@ -17,6 +17,9 @@ DISKSTORE TODO
* Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover. * Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover.
* Stop BGSAVE thread on shutdown and any other condition where the child is killed during normal bgsave. * Stop BGSAVE thread on shutdown and any other condition where the child is killed during normal bgsave.
* Use a mutex to log on the file, so that we don't get overlapping messages, or even better make sure to use a single write against it. * Use a mutex to log on the file, so that we don't get overlapping messages, or even better make sure to use a single write against it.
* Fix RANDOMKEY to really do something interesting
* Fix DBSIZE to really do something interesting
* Add a DEBUG command to check if an entry is or not in memory currently
REPLICATION REPLICATION
=========== ===========
......
...@@ -633,7 +633,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) { ...@@ -633,7 +633,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
} else { } else {
redisLog(REDIS_WARNING, redisLog(REDIS_WARNING,
"Background append only file rewriting terminated by signal %d", "Background append only file rewriting terminated by signal %d",
bysitnal); bysignal);
} }
cleanup: cleanup:
sdsfree(server.bgrewritebuf); sdsfree(server.bgrewritebuf);
......
...@@ -349,10 +349,17 @@ void dsFlushDb(int dbid) { ...@@ -349,10 +349,17 @@ void dsFlushDb(int dbid) {
} }
} }
void dsRdbSaveSetState(int state) {
pthread_mutex_lock(&server.bgsavethread_mutex);
server.bgsavethread_state = state;
pthread_mutex_unlock(&server.bgsavethread_mutex);
}
void *dsRdbSave_thread(void *arg) { void *dsRdbSave_thread(void *arg) {
char tmpfile[256], *filename = (char*)arg; char tmpfile[256], *filename = (char*)arg;
int j, i; int j, i;
time_t now = time(NULL); time_t now = time(NULL);
FILE *fp;
/* Change state to ACTIVE, to signal there is a saving thead working. */ /* Change state to ACTIVE, to signal there is a saving thead working. */
pthread_mutex_lock(&server.bgsavethread_mutex); pthread_mutex_lock(&server.bgsavethread_mutex);
...@@ -362,11 +369,16 @@ void *dsRdbSave_thread(void *arg) { ...@@ -362,11 +369,16 @@ void *dsRdbSave_thread(void *arg) {
snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
fp = fopen(tmpfile,"w"); fp = fopen(tmpfile,"w");
if (!fp) { if (!fp) {
redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno)); redisLog(REDIS_WARNING, "Failed opening .rdb for saving: %s",
return REDIS_ERR; strerror(errno));
dsRdbSaveSetState(REDIS_BGSAVE_THREAD_DONE_ERR);
return NULL;
} }
if (fwrite("REDIS0001",9,1,fp) == 0) goto werr; if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
sleep(5);
#if 0
/* Scan all diskstore dirs looking for keys */ /* Scan all diskstore dirs looking for keys */
for (j = 0; j < 256; j++) { for (j = 0; j < 256; j++) {
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
...@@ -377,6 +389,7 @@ void *dsRdbSave_thread(void *arg) { ...@@ -377,6 +389,7 @@ void *dsRdbSave_thread(void *arg) {
if (rdbSaveLen(fp,j) == -1) goto werr; if (rdbSaveLen(fp,j) == -1) goto werr;
} }
} }
#endif
/* Make sure data will not remain on the OS's output buffers */ /* Make sure data will not remain on the OS's output buffers */
fflush(fp); fflush(fp);
...@@ -389,16 +402,20 @@ void *dsRdbSave_thread(void *arg) { ...@@ -389,16 +402,20 @@ void *dsRdbSave_thread(void *arg) {
if (rename(tmpfile,filename) == -1) { if (rename(tmpfile,filename) == -1) {
redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno)); redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
unlink(tmpfile); unlink(tmpfile);
return REDIS_ERR; dsRdbSaveSetState(REDIS_BGSAVE_THREAD_DONE_ERR);
return NULL;
} }
redisLog(REDIS_NOTICE,"DB saved on disk"); redisLog(REDIS_NOTICE,"DB saved on disk");
return REDIS_OK; dsRdbSaveSetState(REDIS_BGSAVE_THREAD_DONE_OK);
return NULL;
werr: werr:
zfree(filename); zfree(filename);
fclose(fp); fclose(fp);
unlink(tmpfile); unlink(tmpfile);
dsRdbSaveSetState(REDIS_BGSAVE_THREAD_DONE_ERR);
redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno)); redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
return NULL;
} }
int dsRdbSave(char *filename) { int dsRdbSave(char *filename) {
......
...@@ -437,7 +437,8 @@ int rdbSave(char *filename) { ...@@ -437,7 +437,8 @@ int rdbSave(char *filename) {
snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
fp = fopen(tmpfile,"w"); fp = fopen(tmpfile,"w");
if (!fp) { 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; return REDIS_ERR;
} }
if (fwrite("REDIS0001",9,1,fp) == 0) goto werr; if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
...@@ -973,7 +974,7 @@ void backgroundSaveDoneHandler(int exitcode, int bysignal) { ...@@ -973,7 +974,7 @@ void backgroundSaveDoneHandler(int exitcode, int bysignal) {
} }
void saveCommand(redisClient *c) { void saveCommand(redisClient *c) {
if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread-t)-1) { if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) {
addReplyError(c,"Background save already in progress"); addReplyError(c,"Background save already in progress");
return; return;
} }
...@@ -985,7 +986,7 @@ void saveCommand(redisClient *c) { ...@@ -985,7 +986,7 @@ void saveCommand(redisClient *c) {
} }
void bgsaveCommand(redisClient *c) { void bgsaveCommand(redisClient *c) {
if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread-t)-1) { if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) {
addReplyError(c,"Background save already in progress"); addReplyError(c,"Background save already in progress");
return; return;
} }
......
...@@ -608,10 +608,11 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { ...@@ -608,10 +608,11 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
state = server.bgsavethread_state; state = server.bgsavethread_state;
pthread_mutex_unlock(&server.bgsavethread_mutex); pthread_mutex_unlock(&server.bgsavethread_mutex);
if (state == REDIS_BGSAVE_DONE_OK || state == REDIS_BGSAVE_DONE_ERR) if (state == REDIS_BGSAVE_THREAD_DONE_OK ||
state == REDIS_BGSAVE_THREAD_DONE_ERR)
{ {
backgroundSaveDoneHandler( backgroundSaveDoneHandler(
(state == REDIS_BGSAVE_DONE_OK) ? 0 : 1, 0); (state == REDIS_BGSAVE_THREAD_DONE_OK) ? 0 : 1, 0);
} }
} }
} else if (!server.ds_enabled) { } else if (!server.ds_enabled) {
......
...@@ -756,7 +756,7 @@ int rdbSaveObject(FILE *fp, robj *o); ...@@ -756,7 +756,7 @@ int rdbSaveObject(FILE *fp, robj *o);
off_t rdbSavedObjectLen(robj *o); off_t rdbSavedObjectLen(robj *o);
off_t rdbSavedObjectPages(robj *o); off_t rdbSavedObjectPages(robj *o);
robj *rdbLoadObject(int type, FILE *fp); robj *rdbLoadObject(int type, FILE *fp);
void backgroundSaveDoneHandler(int exitcode, int bysignal) { void backgroundSaveDoneHandler(int exitcode, int bysignal);
int rdbSaveKeyValuePair(FILE *fp, redisDb *db, robj *key, robj *val, time_t now); int rdbSaveKeyValuePair(FILE *fp, redisDb *db, robj *key, robj *val, time_t now);
int rdbLoadType(FILE *fp); int rdbLoadType(FILE *fp);
time_t rdbLoadTime(FILE *fp); time_t rdbLoadTime(FILE *fp);
...@@ -800,6 +800,7 @@ robj *dsGet(redisDb *db, robj *key, time_t *expire); ...@@ -800,6 +800,7 @@ robj *dsGet(redisDb *db, robj *key, time_t *expire);
int dsDel(redisDb *db, robj *key); int dsDel(redisDb *db, robj *key);
int dsExists(redisDb *db, robj *key); int dsExists(redisDb *db, robj *key);
void dsFlushDb(int dbid); void dsFlushDb(int dbid);
int dsRdbSave(char *filename);
/* Disk Store Cache */ /* Disk Store Cache */
void dsInit(void); void dsInit(void);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册