redis 的守护进程
redis 自身支持 4 种方式启动:
启动方式 | 说明 |
---|---|
daemonize no | 不作为后台程序启动 |
daemonize yes | 作为后台程序启动 |
supervised upstart | 做成 upstart 服务启动。 upstart ,centos7 / ubuntu 18 等已经逐渐在废弃它了 |
supervised systemd | 做成 systemd 服务启动 |
相关配置:
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised no
本文主要介绍 systemd 相关代码,模仿这些代码,也可以使自己的服务做成 systemd 服务
源代码分析 - 启动相关
代码以 redis 5.0 为准,后续版本实现稍有不同
int main(int argc, char **argv) {
// ... 略(无关代码略)...
server.supervised = redisIsSupervised(server.supervised_mode);
int background = server.daemonize && !server.supervised;
if (background) daemonize();
// ... 略(无关代码略)...
}
int redisIsSupervised(int mode) {
if (mode == SUPERVISED_AUTODETECT) {
// ... 略(无关代码略)...
} else if (mode == SUPERVISED_UPSTART) {
return redisSupervisedUpstart();
} else if (mode == SUPERVISED_SYSTEMD) {
return redisSupervisedSystemd();
}
return 0;
}
int redisSupervisedSystemd(void) {
const char *notify_socket = getenv("NOTIFY_SOCKET");
int fd = 1;
struct sockaddr_un su;
struct iovec iov;
struct msghdr hdr;
int sendto_flags = 0;
if (!notify_socket) {
serverLog(LL_WARNING,
"systemd supervision requested, but NOTIFY_SOCKET not found");
return 0;
}
if ((strchr("@/", notify_socket[0])) == NULL || strlen(notify_socket) < 2) {
return 0;
}
serverLog(LL_NOTICE, "supervised by systemd, will signal readiness");
if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
serverLog(LL_WARNING,
"Can't connect to systemd socket %s", notify_socket);
return 0;
}
memset(&su, 0, sizeof(su));
su.sun_family = AF_UNIX;
strncpy (su.sun_path, notify_socket, sizeof(su.sun_path) -1);
su.sun_path[sizeof(su.sun_path) - 1] = '\0';
if (notify_socket[0] == '@')
su.sun_path[0] = '\0';
memset(&iov, 0, sizeof(iov));
iov.iov_base = "READY=1";
iov.iov_len = strlen("READY=1");
memset(&hdr, 0, sizeof(hdr));
hdr.msg_name = &su;
hdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) +
strlen(notify_socket);
hdr.msg_iov = &iov;
hdr.msg_iovlen = 1;
unsetenv("NOTIFY_SOCKET");
#ifdef HAVE_MSG_NOSIGNAL
sendto_flags |= MSG_NOSIGNAL;
#endif
if (sendmsg(fd, &hdr, sendto_flags) < 0) {
serverLog(LL_WARNING, "Can't send notification to systemd");
close(fd);
return 0;
}
close(fd);
return 1;
}
分析,对 systemd 的支持,主要在 redisSupervisedSystemd
函数内,粗看下这段代码,应该是一脸的闷逼
NOTIFY_SOCKET
环境变量怎么回事,代码中搜不到哪里设置了- 往
NOTIFY_SOCKET
指向的本地域 socket 上发READY=1
又是做啥
这涉及到 systemd 的一些机制的相关知识,可以参考:
- https://askubuntu.com/questions/1120023/how-to-use-systemd-notify
- https://www.freedesktop.org/software/systemd/man/systemd-notify.html
然后就可以
系统配置
代码支持后,然后就是配置实装 systemd , 配置类似以下:
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-onlne.target
Wants=network-online.target
[service]
ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
其他
官网 issue 中,有人提出了支持 systemd 更好的代码实现方式:
https://github.com/antirez/redis/pull/6052
该代码已经合并进最新版本,有兴趣可以对比参考之