int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
。。。。。。
// Check if a background saving or AOF rewrite in progress terminated.
// 如果已有rdb和aof进程,检查进程是否已退出。
// 如果已退出,则会善后处理,否则什么也不做,等待下一次循环时再次检查
if (server.rdb_child_pid != -1 ||
server.aof_child_pid != -1 ||
ldbPendingChildren()) {
int statloc;
pid_t pid;
if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
int exitcode = WEXITSTATUS(statloc);
。。。。。。
}
} else {
// If there is not a background saving/rewrite
// in progress check if we have to save/rewrite now.
// 按在redis.conf中定义的顺序依次遍历每一行配置项
// 最终是否进行写rdb操作(即生成快照文件),不仅由redis.conf
// 中的配置项决定,还要看上一次操作的结果和状态。
for (j = 0; j < server.saveparamslen; j++) {
struct saveparam *sp = server.saveparams+j;
// Save if we reached the given amount of changes,
// the given amount of seconds, and if the latest bgsave was
// successful or if, in case of an error, at least
// CONFIG_BGSAVE_RETRY_DELAY seconds already elapsed.
// CONFIG_BGSAVE_RETRY_DELAY(5): Wait a few secs before trying again.
if (server.dirty >= sp->changes &&
server.unixtime-server.lastsave > sp->seconds &&
(server.unixtime-server.lastbgsave_try>CONFIG_BGSAVE_RETRY_DELAY ||
server.lastbgsave_status == C_OK)) {
serverLog(LL_NOTICE,"%d changes in %d seconds. Saving...",
sp->changes, (int)sp->seconds);
rdbSaveInfo rsi, *rsiptr;
rsiptr = rdbPopulateSaveInfo(&rsi);
rdbSaveBackground(server.rdb_filename,rsiptr);
break; // 遇到一条满足的即结束处理,因为已没有必要判断是否满足下一条配置规则
}
}
}
。。。。。。
}
// rdb.c
int rdbSaveBackground(char *filename, rdbSaveInfo *rsi) {
。。。。。。
server.lastbgsave_try = time(NULL);
。。。。。。
// 创建写rdb的子进程
if ((childpid = fork()) == 0) {
redisSetProcTitle("redis-rdb-bgsave");
retval = rdbSave(filename,rsi);
}
。。。。。。
}
/* Save the DB on disk. Return C_ERR on error, C_OK on success. */
// rdb.c
// rdbSave调用rdbSaveRio将数据写入到rdb文件中
int rdbSave(char *filename, rdbSaveInfo *rsi) {
。。。。。。
// 写rdb文件
if (rdbSaveRio(&rdb,&error,RDB_SAVE_NONE,rsi) == C_ERR) {
errno = error;
goto werr;
}
。。。。。。
serverLog(LL_NOTICE,"DB saved on disk");
server.dirty = 0;
server.lastsave = time(NULL);
server.lastbgsave_status = C_OK;
return C_OK;
}
/* Produces a dump of the database in RDB format sending it to the specified
* Redis I/O channel. On success C_OK is returned, otherwise C_ERR
* is returned and part of the output, or all the output, can be
* missing because of I/O errors.
*
* When the function returns C_ERR and if 'error' is not NULL, the
* integer pointed by 'error' is set to the value of errno just after the I/O
* error. */
int rdbSaveRio(rio *rdb, int *error, int flags, rdbSaveInfo *rsi) {
for (j = 0; j < server.dbnum; j++) {
。。。。。。
// Iterate this DB writing every entry
while((de = dictNext(di)) != NULL) {
。。。。。。
// 将一对对KV写入到rdb文件
if (rdbSaveKeyValuePair(rdb,&key,o,expire) == -1) goto werr;
。。。。。。
}
}
。。。。。。
werr:
if (error) *error = errno;
if (di) dictReleaseIterator(di);
return C_ERR;
}
// 以SADD命令为例,所有写操作,均会修改dirty 的值
void saddCommand(client *c) {
。。。。。。
for (j = 2; j < c->argc; j++) {
if (setTypeAdd(set,c->argv[j]->ptr)) added++;
}
。。。。。。
server.dirty += added;
addReplyLongLong(c,added);
}