Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Turbo码先生
redis
提交
36c17a53
R
redis
项目概览
Turbo码先生
/
redis
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
36c17a53
编写于
1月 07, 2011
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
source reshaped a bit to play well with a bgsaving thread, still work to do, does not compile.
上级
cb9b35c8
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
103 addition
and
54 deletion
+103
-54
TODO
TODO
+2
-0
src/aof.c
src/aof.c
+2
-5
src/db.c
src/db.c
+0
-24
src/diskstore.c
src/diskstore.c
+22
-4
src/dscache.c
src/dscache.c
+1
-0
src/rdb.c
src/rdb.c
+36
-12
src/redis.c
src/redis.c
+22
-2
src/redis.h
src/redis.h
+18
-7
未找到文件。
TODO
浏览文件 @
36c17a53
...
...
@@ -15,6 +15,8 @@ DISKSTORE TODO
* Check that 00/00 and ff/ff exist at startup, otherwise exit with error.
* Implement sync flush option, where data is written synchronously on disk when a command is executed.
* 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.
* 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.
REPLICATION
===========
...
...
src/aof.c
浏览文件 @
36c17a53
...
...
@@ -585,10 +585,7 @@ void aofRemoveTempFile(pid_t childpid) {
/* A background append only file rewriting (BGREWRITEAOF) terminated its work.
* Handle this. */
void
backgroundRewriteDoneHandler
(
int
statloc
)
{
int
exitcode
=
WEXITSTATUS
(
statloc
);
int
bysignal
=
WIFSIGNALED
(
statloc
);
void
backgroundRewriteDoneHandler
(
int
exitcode
,
int
bysignal
)
{
if
(
!
bysignal
&&
exitcode
==
0
)
{
int
fd
;
char
tmpfile
[
256
];
...
...
@@ -636,7 +633,7 @@ void backgroundRewriteDoneHandler(int statloc) {
}
else
{
redisLog
(
REDIS_WARNING
,
"Background append only file rewriting terminated by signal %d"
,
WTERMSIG
(
statloc
)
);
bysitnal
);
}
cleanup:
sdsfree
(
server
.
bgrewritebuf
);
...
...
src/db.c
浏览文件 @
36c17a53
...
...
@@ -379,30 +379,6 @@ void typeCommand(redisClient *c) {
addReplyStatus
(
c
,
type
);
}
void
saveCommand
(
redisClient
*
c
)
{
if
(
server
.
bgsavechildpid
!=
-
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
)
{
addReplyError
(
c
,
"Background save already in progress"
);
return
;
}
if
(
rdbSaveBackground
(
server
.
dbfilename
)
==
REDIS_OK
)
{
addReplyStatus
(
c
,
"Background saving started"
);
}
else
{
addReply
(
c
,
shared
.
err
);
}
}
void
shutdownCommand
(
redisClient
*
c
)
{
if
(
prepareForShutdown
()
==
REDIS_OK
)
exit
(
0
);
...
...
src/diskstore.c
浏览文件 @
36c17a53
...
...
@@ -349,11 +349,16 @@ void dsFlushDb(int dbid) {
}
}
int
dsRdbSave
(
char
*
filename
)
{
char
tmpfile
[
256
];
void
*
dsRdbSave_thread
(
void
*
arg
)
{
char
tmpfile
[
256
]
,
*
filename
=
(
char
*
)
arg
;
int
j
,
i
;
time_t
now
=
time
(
NULL
);
/* Change state to ACTIVE, to signal there is a saving thead working. */
pthread_mutex_lock
(
&
server
.
bgsavethread_mutex
);
server
.
bgsavethread_state
=
REDIS_BGSAVE_THREAD_ACTIVE
;
pthread_mutex_unlock
(
&
server
.
bgsavethread_mutex
);
snprintf
(
tmpfile
,
256
,
"temp-%d.rdb"
,
(
int
)
getpid
());
fp
=
fopen
(
tmpfile
,
"w"
);
if
(
!
fp
)
{
...
...
@@ -377,6 +382,7 @@ int dsRdbSave(char *filename) {
fflush
(
fp
);
fsync
(
fileno
(
fp
));
fclose
(
fp
);
zfree
(
filename
);
/* Use RENAME to make sure the DB file is changed atomically only
* if the generate DB file is ok. */
...
...
@@ -386,12 +392,24 @@ int dsRdbSave(char *filename) {
return
REDIS_ERR
;
}
redisLog
(
REDIS_NOTICE
,
"DB saved on disk"
);
server
.
dirty
=
0
;
server
.
lastsave
=
time
(
NULL
);
return
REDIS_OK
;
werr:
zfree
(
filename
);
fclose
(
fp
);
unlink
(
tmpfile
);
redisLog
(
REDIS_WARNING
,
"Write error saving DB on disk: %s"
,
strerror
(
errno
));
}
int
dsRdbSave
(
char
*
filename
)
{
pthread_t
thread
;
if
(
pthread_create
(
&
thread
,
NULL
,
dsRdbSave_thread
,
zstrdup
(
filename
))
!=
0
)
{
redisLog
(
REDIS_WARNING
,
"Can't create diskstore BGSAVE thread: %s"
,
strerror
(
errno
));
return
REDIS_ERR
;
}
else
{
server
.
bgsavethread
=
thread
;
return
REDIS_OK
;
}
}
src/dscache.c
浏览文件 @
36c17a53
...
...
@@ -132,6 +132,7 @@ void dsInit(void) {
server
.
io_ready_clients
=
listCreate
();
pthread_mutex_init
(
&
server
.
io_mutex
,
NULL
);
pthread_cond_init
(
&
server
.
io_condvar
,
NULL
);
pthread_mutex_init
(
&
server
.
bgsavethread_mutex
,
NULL
);
server
.
io_active_threads
=
0
;
if
(
pipe
(
pipefds
)
==
-
1
)
{
redisLog
(
REDIS_WARNING
,
"Unable to intialized DS: pipe(2): %s. Exiting."
...
...
src/rdb.c
浏览文件 @
36c17a53
...
...
@@ -496,22 +496,23 @@ werr:
int
rdbSaveBackground
(
char
*
filename
)
{
pid_t
childpid
;
if
(
server
.
bgsavechildpid
!=
-
1
)
return
REDIS_ERR
;
if
(
server
.
bgsavechildpid
!=
-
1
||
server
.
bgsavethread
!=
(
pthread_t
)
-
1
)
return
REDIS_ERR
;
server
.
dirty_before_bgsave
=
server
.
dirty
;
if
(
server
.
ds_enabled
)
{
cacheForcePointInTime
();
return
dsRdbSave
(
filename
);
}
if
((
childpid
=
fork
())
==
0
)
{
int
retval
;
/* Child */
if
(
server
.
ipfd
>
0
)
close
(
server
.
ipfd
);
if
(
server
.
sofd
>
0
)
close
(
server
.
sofd
);
if
(
server
.
ds_enabled
)
{
cacheForcePointInTime
();
dsRdbSave
(
filename
);
}
else
{
rdbSave
(
filename
);
}
retval
=
rdbSave
(
filename
);
_exit
((
retval
==
REDIS_OK
)
?
0
:
1
);
}
else
{
/* Parent */
...
...
@@ -950,10 +951,7 @@ 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"
);
...
...
@@ -963,11 +961,37 @@ void backgroundSaveDoneHandler(int statloc) {
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
);
}
}
src/redis.c
浏览文件 @
36c17a53
...
...
@@ -589,13 +589,31 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
pid_t
pid
;
if
((
pid
=
wait3
(
&
statloc
,
WNOHANG
,
NULL
))
!=
0
)
{
int
exitcode
=
WEXITSTATUS
(
statloc
);
int
bysignal
=
0
;
if
(
WIFSIGNALED
(
statloc
))
bysignal
=
WTERMSIG
(
statloc
);
if
(
pid
==
server
.
bgsavechildpid
)
{
backgroundSaveDoneHandler
(
statloc
);
backgroundSaveDoneHandler
(
exitcode
,
bysignal
);
}
else
{
backgroundRewriteDoneHandler
(
statloc
);
backgroundRewriteDoneHandler
(
exitcode
,
bysignal
);
}
updateDictResizePolicy
();
}
if
(
server
.
bgsavethread
!=
(
pthread_t
)
-
1
)
{
int
state
;
pthread_mutex_lock
(
&
server
.
bgsavethread_mutex
);
state
=
server
.
bgsavethread_state
;
pthread_mutex_unlock
(
&
server
.
bgsavethread_mutex
);
if
(
state
==
REDIS_BGSAVE_DONE_OK
||
state
==
REDIS_BGSAVE_DONE_ERR
)
{
backgroundSaveDoneHandler
(
(
state
==
REDIS_BGSAVE_DONE_OK
)
?
0
:
1
,
0
);
}
}
}
else
if
(
!
server
.
ds_enabled
)
{
/* If there is not a background saving in progress check if
* we have to save now */
...
...
@@ -867,6 +885,8 @@ void initServer() {
server
.
cronloops
=
0
;
server
.
bgsavechildpid
=
-
1
;
server
.
bgrewritechildpid
=
-
1
;
server
.
bgsavethread_state
=
REDIS_BGSAVE_THREAD_UNACTIVE
;
server
.
bgsavethread
=
(
pthread_t
)
-
1
;
server
.
bgrewritebuf
=
sdsempty
();
server
.
aofbuf
=
sdsempty
();
server
.
lastsave
=
time
(
NULL
);
...
...
src/redis.h
浏览文件 @
36c17a53
...
...
@@ -203,6 +203,12 @@
#define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4
#define REDIS_MAXMEMORY_NO_EVICTION 5
/* Diskstore background saving thread states */
#define REDIS_BGSAVE_THREAD_UNACTIVE 0
#define REDIS_BGSAVE_THREAD_ACTIVE 1
#define REDIS_BGSAVE_THREAD_DONE_OK 2
#define REDIS_BGSAVE_THREAD_DONE_ERR 3
/* We can print the stacktrace, so our assert is defined this way: */
#define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
#define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
...
...
@@ -390,25 +396,30 @@ struct redisServer {
int
appendfsync
;
int
no_appendfsync_on_rewrite
;
int
shutdown_asap
;
int
activerehashing
;
char
*
requirepass
;
/* Persistence */
time_t
lastfsync
;
int
appendfd
;
int
appendseldb
;
char
*
pidfile
;
pid_t
bgsavechildpid
;
pid_t
bgrewritechildpid
;
int
bgsavethread_state
;
pthread_mutex_t
bgsavethread_mutex
;
pthread_t
bgsavethread
;
sds
bgrewritebuf
;
/* buffer taken by parent during oppend only rewrite */
sds
aofbuf
;
/* AOF buffer, written before entering the event loop */
struct
saveparam
*
saveparams
;
int
saveparamslen
;
char
*
dbfilename
;
int
rdbcompression
;
char
*
appendfilename
;
/* Logging */
char
*
logfile
;
int
syslog_enabled
;
char
*
syslog_ident
;
int
syslog_facility
;
char
*
dbfilename
;
char
*
appendfilename
;
char
*
requirepass
;
int
rdbcompression
;
int
activerehashing
;
/* Replication related */
int
isslave
;
/* Slave specific fields */
...
...
@@ -745,7 +756,7 @@ int rdbSaveObject(FILE *fp, robj *o);
off_t
rdbSavedObjectLen
(
robj
*
o
);
off_t
rdbSavedObjectPages
(
robj
*
o
);
robj
*
rdbLoadObject
(
int
type
,
FILE
*
fp
);
void
backgroundSaveDoneHandler
(
int
statloc
);
void
backgroundSaveDoneHandler
(
int
exitcode
,
int
bysignal
)
{
int
rdbSaveKeyValuePair
(
FILE
*
fp
,
redisDb
*
db
,
robj
*
key
,
robj
*
val
,
time_t
now
);
int
rdbLoadType
(
FILE
*
fp
);
time_t
rdbLoadTime
(
FILE
*
fp
);
...
...
@@ -759,7 +770,7 @@ int rewriteAppendOnlyFileBackground(void);
int
loadAppendOnlyFile
(
char
*
filename
);
void
stopAppendOnly
(
void
);
int
startAppendOnly
(
void
);
void
backgroundRewriteDoneHandler
(
int
statloc
);
void
backgroundRewriteDoneHandler
(
int
exitcode
,
int
bysignal
);
/* Sorted sets data type */
zskiplist
*
zslCreate
(
void
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录