谷动谷力

 找回密码
 立即注册
查看: 3549|回复: 18
打印 上一主题 下一主题
收起左侧

为什么单线程的Redis却能支撑高并发?

  [复制链接]
跳转到指定楼层
楼主
发表于 2019-6-4 22:42:40 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
为什么单线程的Redis却能支撑高并发?


最近在看 UNIX 网络编程并研究了一下 Redis 的实现,感觉 Redis 的源代码十分适合阅读和分析,其中 I/O 多路复用(mutiplexing)部分的实现非常干净和优雅,在这里想对这部分的内容进行简单的整理。
                                    
             最近在看 UNIX 网络编程并研究了一下 Redis 的实现,感觉 Redis 的源代码十分适合阅读和分析,其中 I/O 多路复用(mutiplexing)部分的实现非常干净和优雅,在这里想对这部分的内容进行简单的整理。
几种 I/O 模型

为什么 Redis 中要使用 I/O 多路复用这种技术呢?首先,Redis 是跑在单线程中的,所有的操作都是按照顺序线性执行的。
但是由于读写操作等待用户输入或输出都是阻塞的,所以 I/O 操作在一般情况下往往不能直接返回。
这会导致某一文件的 I/O 阻塞导致整个进程无法对其他客户提供服务,而 I/O 多路复用就是为了解决这个问题而出现的。

Blocking I/O

先来看一下传统的阻塞 I/O 模型到底是如何工作的:当使用 Read 或者 Write 对某一个文件描述符(File Descriptor 以下简称  FD)进行读写时。
如果当前 FD 不可读或不可写,整个 Redis 服务就不会对其他的操作作出响应,导致整个服务不可用。
这也就是传统意义上的,我们在编程中使用最多的阻塞模型:
阻塞模型虽然开发中非常常见也非常易于理解,但是由于它会影响其他 FD 对应的服务,所以在需要处理多个客户端任务的时候,往往都不会使用阻塞模型。

I/O 多路复用
虽然还有很多其他的 I/O 模型,但是在这里都不会具体介绍。阻塞式的 I/O 模型并不能满足这里的需求,我们需要一种效率更高的 I/O 模型来支撑  Redis 的多个客户(redis-cli)。
这里涉及的就是 I/O 多路复用模型了:
在 I/O 多路复用模型中,最重要的函数调用就是 select,该方法的能够同时监控多个文件描述符的可读可写情况,当其中的某些文件描述符可读或者可写时,select 方法就会返回可读以及可写的文件描述符个数。
关于 select 的具体使用方法,在网络上资料很多,这里就不过多展开介绍了;
与此同时也有其它的 I/O 多路复用函数 epoll/kqueue/evport,它们相比 select 性能更优秀,同时也能支撑更多的服务。

Reactor 设计模式

Redis 服务采用 Reactor 的方式来实现文件事件处理器(每一个网络连接其实都对应一个文件描述符)
文件事件处理器使用 I/O 多路复用模块同时监听多个 FD,当 accept、read、write 和 close 文件事件产生时,文件事件处理器就会回调 FD 绑定的事件处理器。
虽然整个文件事件处理器是在单线程上运行的,但是通过 I/O 多路复用模块的引入,实现了同时对多个 FD 读写的监控,提高了网络通信模型的性能,同时也可以保证整个 Redis 服务实现的简单。

I/O 多路复用模块

I/O 多路复用模块封装了底层的 select、epoll、avport 以及 kqueue 这些 I/O 多路复用函数,为上层提供了相同的接口。
在这里我们简单介绍 Redis 是如何包装 select 和 epoll 的,简要了解该模块的功能,整个 I/O 多路复用模块抹平了不同平台上 I/O 多路复用函数的差异性,提供了相同的接口:
  1. static int  aeApiCreate(aeEventLoop *eventLoop)
  2. static int  aeApiResize(aeEventLoop *eventLoop, int setsize)
  3. static void aeApiFree(aeEventLoop *eventLoop)
  4. static int  aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask)
  5. static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask)
  6. static int  aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp)
复制代码


同时,因为各个函数所需要的参数不同,我们在每一个子模块内部通过一个 aeApiState 来存储需要的上下文信息:
  1. // select
  2. typedef struct aeApiState {
  3.     fd_set rfds, wfds;
  4.     fd_set _rfds, _wfds;
  5. } aeApiState;

  6. // epoll
  7. typedef struct aeApiState {
  8.     int epfd;
  9.     struct epoll_event *events;
  10. } aeApiState;
复制代码


这些上下文信息会存储在 eventLoop 的 void *state 中,不会暴露到上层,只在当前子模块中使用。

封装 Select 函数

Select 可以监控 FD 的可读、可写以及出现错误的情况。在介绍 I/O 多路复用模块如何对 Select 函数封装之前,先来看一下 Select  函数使用的大致流程:
  • 初始化一个可读的 fd_set 集合,保存需要监控可读性的 FD。
  • 使用 FD_SET 将 fd 加入 RFDS。
  • 调用 Select 方法监控 RFDS 中的 FD 是否可读。
  • 当 Select 返回时,检查 FD 的状态并完成对应的操作。

  1. int fd = /* file descriptor */

  2. fd_set rfds;
  3. FD_ZERO(&rfds);
  4. FD_SET(fd, &rfds)

  5. for ( ; ; ) {
  6.     select(fd+1, &rfds, NULL, NULL, NULL);
  7.     if (FD_ISSET(fd, &rfds)) {
  8.         /* file descriptor `fd` becomes readable */
  9.     }
  10. }
复制代码

而在 Redis 的 ae_select 文件中代码的组织顺序也是差不多的,首先在 aeApiCreate 函数中初始化 rfds 和 wfds:
  1. static int aeApiCreate(aeEventLoop *eventLoop) {
  2.     aeApiState *state = zmalloc(sizeof(aeApiState));
  3.     if (!state) return -1;
  4.     FD_ZERO(&state->rfds);
  5.     FD_ZERO(&state->wfds);
  6.     eventLoop->apidata = state;
  7.     return 0;
  8. }
复制代码


而 aeApiAddEvent 和 aeApiDelEvent 会通过 FD_SET 和 FD_CLR 修改 fd_set 中对应 FD 的标志位:
  1. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  2.     aeApiState *state = eventLoop->apidata;
  3.     if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
  4.     if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
  5.     return 0;
  6. }
复制代码


整个 ae_select 子模块中最重要的函数就是 aeApiPoll,它是实际调用 select 函数的部分,其作用就是在 I/O 多路复用函数返回时,将对应的 FD 加入 aeEventLoop 的 fired 数组中,并返回事件的个数:
  1. static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
  2.     aeApiState *state = eventLoop->apidata;
  3.     int retval, j, numevents = 0;

  4.     memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
  5.     memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));

  6.     retval = select(eventLoop->maxfd+1,
  7.                 &state->_rfds,&state->_wfds,NULL,tvp);
  8.     if (retval > 0) {
  9.         for (j = 0; j <= eventLoop->maxfd; j++) {
  10.             int mask = 0;
  11.             aeFileEvent *fe = &eventLoop->events[j];

  12.             if (fe->mask == AE_NONE) continue;
  13.             if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
  14.                 mask |= AE_READABLE;
  15.             if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
  16.                 mask |= AE_WRITABLE;
  17.             eventLoop->fired[numevents].fd = j;
  18.             eventLoop->fired[numevents].mask = mask;
  19.             numevents++;
  20.         }
  21.     }
  22.     return numevents;
  23. }
复制代码


封装 Epoll 函数
Redis 对 epoll 的封装其实也是类似的,使用 epoll_create 创建 epoll 中使用的 epfd:
  1. static int aeApiCreate(aeEventLoop *eventLoop) {
  2.     aeApiState *state = zmalloc(sizeof(aeApiState));

  3.     if (!state) return -1;
  4.     state->events = zmalloc(sizeof(struct epoll_event)*eventLoop->setsize);
  5.     if (!state->events) {
  6.         zfree(state);
  7.         return -1;
  8.     }
  9.     state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */
  10.     if (state->epfd == -1) {
  11.         zfree(state->events);
  12.         zfree(state);
  13.         return -1;
  14.     }
  15.     eventLoop->apidata = state;
  16.     return 0;
  17. }
复制代码


在 aeApiAddEvent 中使用 epoll_ctl 向 epfd 中添加需要监控的 FD 以及监听的事件:
  1. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  2.     aeApiState *state = eventLoop->apidata;
  3.     struct epoll_event ee = {0}; /* avoid valgrind warning */
  4.     /* If the fd was already monitored for some event, we need a MOD
  5.      * operation. Otherwise we need an ADD operation. */
  6.     int op = eventLoop->events[fd].mask == AE_NONE ?
  7.             EPOLL_CTL_ADD : EPOLL_CTL_MOD;

  8.     ee.events = 0;
  9.     mask |= eventLoop->events[fd].mask; /* Merge old events */
  10.     if (mask & AE_READABLE) ee.events |= EPOLLIN;
  11.     if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
  12.     ee.data.fd = fd;
  13.     if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;
  14.     return 0;
  15. }
复制代码


由于 epoll 相比 select 机制略有不同,在 epoll_wait 函数返回时并不需要遍历所有的 FD 查看读写情况。
在 epoll_wait 函数返回时会提供一个 epoll_event 数组:
  1. typedef union epoll_data {
  2.     void    *ptr;
  3.     int      fd; /* 文件描述符 */
  4.     uint32_t u32;
  5.     uint64_t u64;
  6. } epoll_data_t;

  7. struct epoll_event {
  8.     uint32_t     events; /* Epoll 事件 */
  9.     epoll_data_t data;
  10. };
复制代码


其中保存了发生的 epoll 事件(EPOLLIN、EPOLLOUT、EPOLLERR 和 EPOLLHUP)以及发生该事件的 FD。
aeApiPoll 函数只需要将 epoll_event 数组中存储的信息加入 eventLoop 的 fired 数组中,将信息传递给上层模块:
  1. static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
  2.     aeApiState *state = eventLoop->apidata;
  3.     int retval, numevents = 0;

  4.     retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
  5.             tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
  6.     if (retval > 0) {
  7.         int j;

  8.         numevents = retval;
  9.         for (j = 0; j < numevents; j++) {
  10.             int mask = 0;
  11.             struct epoll_event *e = state->events+j;

  12.             if (e->events & EPOLLIN) mask |= AE_READABLE;
  13.             if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
  14.             if (e->events & EPOLLERR) mask |= AE_WRITABLE;
  15.             if (e->events & EPOLLHUP) mask |= AE_WRITABLE;
  16.             eventLoop->fired[j].fd = e->data.fd;
  17.             eventLoop->fired[j].mask = mask;
  18.         }
  19.     }
  20.     return numevents;
  21. }
复制代码


子模块的选择

因为 Redis 需要在多个平台上运行,同时为了最大化执行的效率与性能,所以会根据编译平台的不同选择不同的 I/O 多路复用函数作为子模块,提供给上层统一的接口。

在 Redis 中,我们通过宏定义的使用,合理的选择不同的子模块:
  1. #ifdef HAVE_EVPORT
  2. #include "ae_evport.c"
  3. #else
  4.     #ifdef HAVE_EPOLL
  5.     #include "ae_epoll.c"
  6.     #else
  7.         #ifdef HAVE_KQUEUE
  8.         #include "ae_kqueue.c"
  9.         #else
  10.         #include "ae_select.c"
  11.         #endif
  12.     #endif
  13. #endif
复制代码


因为 select 函数是作为 POSIX 标准中的系统调用,在不同版本的操作系统上都会实现,所以将其作为保底方案:
Redis 会优先选择时间复杂度为 $O(1)$ 的 I/O 多路复用函数作为底层实现,包括 Solaries 10 中的 evport、Linux 中的 epoll 和 macOS/FreeBSD 中的 kqueue。

上述的这些函数都使用了内核内部的结构,并且能够服务几十万的文件描述符。

但是如果当前编译环境没有上述函数,就会选择 select 作为备选方案,由于其在使用时会扫描全部监听的描述符,所以其时间复杂度较差 O(n)。
并且只能同时服务 1024 个文件描述符,所以一般并不会以 select 作为第一方案使用。

总结

Redis 对于 I/O 多路复用模块的设计非常简洁,通过宏保证了 I/O 多路复用模块在不同平台上都有着优异的性能,将不同的 I/O 多路复用函数封装成相同的 API 提供给上层使用。

整个模块使 Redis 能以单进程运行的同时服务成千上万个文件描述符,避免了由于多进程应用的引入导致代码实现复杂度的提升,减少了出错的可能性。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-26 05:20 , Processed in 0.084383 second(s), 38 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表