redis是当下比较流行的KV数据库之一,是抵御高并发的一把利器,本着知其然还要知其因此然的目的,我决定花一点时间来研究其源码,但愿最后能向本身解释清楚“redis为何这么快”这个疑惑,第一篇主要介绍环境搭建和redis工做流程初探,后期会陆续献上其余有意思的章节。html
我本身的电脑是win10系统,因此我会准备一套适合windows系统的环境来供本身学习,这样方便调试分析。linux
redis自己是不支持windows系统的,可是微软的工程师针对windows平台作了支持,源码放在了github上,有须要的能够本身去下载,我这里下载的是v2.8.9这个tag的源码,下载地址https://github.com/microsoftarchive/redis。这里扯个题外话,学习一个开源软件的时候不要一上来就下载最新版本的源码看,通过的迭代太多代码量就上来了,对于新手来讲容易晕,先下一个早期的稳定版本了解其体系结构和工做流程,等待熟悉了之后再按部就班。git
其余软件我没尝试过,这个是官方推荐的ide,必定必定必定下载 Visual Studio 2013 update5这个版本的,不然编译的时候各类报错,下载地址
http://download.microsoft.com/download/9/3/E/93EA27FF-DB02-4822-8771-DCA0238957E9/vs2013.5_ult_chs.iso。这里再扯个题外话,我刚开始下载的是最新版本的Visual Studio,结果编译的时候各类报错,而后就去网络上一顿查一顿试,折腾半天仍是没好,最后下载了 Visual Studio 2013 update5这个版本,结果一把就成功,有些牛角尖必定得钻,可是有些牛角尖不值得钻。github
按照下图方式打开下载的redis源码
redis
c程序的入口是main方法,redis main方法的位置在redis.c文件中,下面咱们经过main方法来逐步了解redis的工做流程。数据库
跟着main方法顺序看下去,大概有如下几个关键步骤(略过了sentinel相关逻辑):
1.设置随机数种子、获取当前时间等;
2.初始化服务配置信息,设置默认值(initServerConfig);
3.解析配置文件(loadServerConfig);
4.初始化server对象(initServer);
4.1建立eventLoop对象;
4.2建立serverSocket,监听端口;
4.3添加定时事件到eventLoop对象中;
4.4将serverSocket文件描述符添加到监视集中,这里借助IO多路复用框架的能力(windows平台使用IOCP,其余平台使用select、epoll、evport等);
5.从磁盘加载数据到内存中(loadDataFromDisk);
6.执行事件循环逻辑(aeMain),这是redis真正挥洒汗水的地方,下一节会单独讲述这块内容。windows
咱们都知道redis是单线程执行客户端命令的,那到底是怎样一种设计才能支持高并发的读写呢。网络
1.server启动,建立serverSocket监听端口,将serverSocket对应的FD(文件描述符)简称为FD-Server添加到IO多路复用框架的监视集当中,注册AE_READABLE事件(可读),关联的事件处理器是acceptTcpHandler;
2.client链接server;
3.事件循环开始轮询IO多路复用框架接口aeApiPoll,会获得就绪的FD,执行对应的事件处理器;
4.由第3步事件循环触发FD-Server AE_READABLE事件对应的事件处理器acceptTcpHandler;
4.1调用accept得到clientSocket对应的FD简称为FD-Client;
4.2将FD-Client添加到IO多路复用框架的监视集当中,注册AE_READABLE事件(可读),关联的事件处理器是readQueryFromClient;
5.client发送redis命令;
6.由第3步事件循环触发FD-Clien AE_READABLE事件对应的事件处理器readQueryFromClient;
6.1解析客户端发来的redis命令,找到命令对应的redisCommandProc(命令对应的处理函数);
6.2执行redisCommandProc;
6.3prepareClientToWrite准备回写响应信息,为FD-Client注册AE_WRITEABLE事件(可写),关联的事件处理器是sendReplyToClient;
7.执行redis中的定时任务;
8.由第3步事件循环触发FD-Clien AE_WRITEABLE事件对应的事件处理器sendReplyToClient,发送响应内容给client;
并发
server启动,建立serverSocket并注册AE_READABLE事件,设置事件处理器为acceptTcpHandler框架
void initServer() { //省略部分代码 //初始化eventLoop对象,eventLoop对象里面存储了全部的事件 server.el = aeCreateEventLoop(server.maxclients+REDIS_EVENTLOOP_FDSET_INCR); //建立serverSocket,监听端口 if (server.port != 0 && listenToPort(server.port,server.ipfd,&server.ipfd_count) == REDIS_ERR) exit(1); //添加定时任务到eventLoop中 if(aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL) == AE_ERR) { } //将serverSocket对应的文件描述符添加到监视集中,关联的事件处理器是acceptTcpHandler for (j = 0; j < server.ipfd_count; j++) { if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler,NULL) == AE_ERR) } }
acceptTcpHandler当有链接过来的时候被触发,调用accept获得client socket对应的FD,并将FD添加到监视集中,关联的事件处理器是readQueryFromClient
void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { int cport, cfd; //调用accept得到clientSocket对应的FD cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport); //将clientSocket对应的FD添加到监视集中 acceptCommonHandler(cfd,0); } static void acceptCommonHandler(int fd, int flags) { redisClient *c; //调用createClient添加 if ((c = createClient(fd)) == NULL) { } } redisClient *createClient(int fd) { redisClient *c = zmalloc(sizeof(redisClient)); if (fd != -1) { anetNonBlock(NULL,fd); anetEnableTcpNoDelay(NULL,fd); if (server.tcpkeepalive) anetKeepAlive(NULL,fd,server.tcpkeepalive); //将fd添加到监视集中,关联的事件处理器是readQueryFromClient if (aeCreateFileEvent(server.el,fd,AE_READABLE, readQueryFromClient, c) == AE_ERR) { } } }
aeMain就是跑一个循环,一直去调用aeProcessEvents
void aeMain(aeEventLoop *eventLoop) { eventLoop->stop = 0; while (!eventLoop->stop) { if (eventLoop->beforesleep != NULL) eventLoop->beforesleep(eventLoop); aeProcessEvents(eventLoop, AE_ALL_EVENTS); } }
aeProcessEvents会调用aeApiPoll方法来得到就绪的文件描述符,而后执行文件描述符关联的的事件处理器
int aeProcessEvents(aeEventLoop *eventLoop, int flags) { int processed = 0, numevents; #ifdef _WIN32 if (ServiceStopIssued() == TRUE) aeStop(eventLoop); #endif /* Nothing to do? return ASAP */ if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0; /* Note that we want call select() even if there are no * file events to process as long as we want to process time * events, in order to sleep until the next time event is ready * to fire. */ if (eventLoop->maxfd != -1 || ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) { int j; aeTimeEvent *shortest = NULL; struct timeval tv, *tvp; if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT)) shortest = aeSearchNearestTimer(eventLoop); if (shortest) { long now_sec, now_ms; /* Calculate the time missing for the nearest * timer to fire. */ aeGetTime(&now_sec, &now_ms); tvp = &tv; tvp->tv_sec = shortest->when_sec - now_sec; if (shortest->when_ms < now_ms) { tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000; tvp->tv_sec --; } else { tvp->tv_usec = (shortest->when_ms - now_ms)*1000; } if (tvp->tv_sec < 0) tvp->tv_sec = 0; if (tvp->tv_usec < 0) tvp->tv_usec = 0; } else { /* If we have to check for events but need to return * ASAP because of AE_DONT_WAIT we need to set the timeout * to zero */ if (flags & AE_DONT_WAIT) { tv.tv_sec = tv.tv_usec = 0; tvp = &tv; } else { /* Otherwise we can block */ tvp = NULL; /* wait forever */ } } numevents = aeApiPoll(eventLoop, tvp); for (j = 0; j < numevents; j++) { aeFileEvent *fe; int mask = eventLoop->fired[j].mask; int fd = eventLoop->fired[j].fd; int rfired = 0; fe = &eventLoop->events[eventLoop->fired[j].fd]; /* note the fe->mask & mask & ... code: maybe an already processed * event removed an element that fired and we still didn't * processed, so we check if the event is still valid. */ if (fe->mask & mask & AE_READABLE) { rfired = 1; fe->rfileProc(eventLoop,fd,fe->clientData,mask); } if (fe->mask & mask & AE_WRITABLE) { if (!rfired || fe->wfileProc != fe->rfileProc) fe->wfileProc(eventLoop,fd,fe->clientData,mask); } processed++; } } /* Check time events */ if (flags & AE_TIME_EVENTS) processed += processTimeEvents(eventLoop);//处理延迟任务 return processed; /* return the number of processed file/time events */ }
作了一个动画帮助理解工做过程(redis启动以后使用命令行telnet到6379端口,而后执行keys *命令,最终拿到结果)
这部份内容网络上精彩的内容太多,这里把我认为比较经典的一些内容贴出来供你们品读(建议从上往下顺序阅读)
The C10K problem
socket阻塞非阻塞等头疼问题解释
LINUX – IO MULTIPLEXING – SELECT VS POLL VS EPOLL
poll vs select vs event-based
redis事件驱动