为什么 Redis 中要使用 I/O 多路复用技术? |
您所在的位置:网站首页 › 为什么要采用多路复用技术 › 为什么 Redis 中要使用 I/O 多路复用技术? |
最近在看 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 服务就不会对其它的操作作出响应,导致整个服务不可用。 这也就是传统意义上的,也就是我们在编程中使用最多的阻塞模型: blocking-io 阻塞模型虽然开发中非常常见也非常易于理解,但是由于它会影响其他 FD 对应的服务,所以在需要处理多个客户端任务的时候,往往都不会使用阻塞模型。 I/O 多路复用 虽然还有很多其它的 I/O 模型,但是在这里都不会具体介绍。 阻塞式的 I/O 模型并不能满足这里的需求,我们需要一种效率更高的 I/O 模型来支撑 Redis 的多个客户(redis-cli),这里涉及的就是 I/O 多路复用模型了: I:O-Multiplexing-Mode 在 I/O 多路复用模型中,最重要的函数调用就是select,该方法的能够同时监控多个文件描述符的可读可写情况,当其中的某些文件描述符可读或者可写时,select方法就会返回可读以及可写的文件描述符个数。 关于select的具体使用方法,在网络上资料很多,这里就不过多展开介绍了; 与此同时也有其它的 I/O 多路复用函数epoll/kqueue/evport,它们相比select性能更优秀,同时也能支撑更多的服务。 Reactor 设计模式 Redis 服务采用 Reactor 的方式来实现文件事件处理器(每一个网络连接其实都对应一个文件描述符) redis-reactor-pattern 文件事件处理器使用 I/O 多路复用模块同时监听多个 FD,当accept、read、write和close文件事件产生时,文件事件处理器就会回调 FD 绑定的事件处理器。 虽然整个文件事件处理器是在单线程上运行的,但是通过 I/O 多路复用模块的引入,实现了同时对多个 FD 读写的监控,提高了网络通信模型的性能,同时也可以保证整个 Redis 服务实现的简单。 I/O 多路复用模块 I/O 多路复用模块封装了底层的select、epoll、avport以及kqueue这些 I/O 多路复用函数,为上层提供了相同的接口。 ae-module 在这里我们简单介绍 Redis 是如何包装select和epoll的,简要了解该模块的功能,整个 I/O 多路复用模块抹平了不同平台上 I/O 多路复用函数的差异性,提供了相同的接口: static int aeApiCreate(aeEventLoop *eventLoop) static int aeApiResize(aeEventLoop *eventLoop, int setsize) static void aeApiFree(aeEventLoop *eventLoop) static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) 同时,因为各个函数所需要的参数不同,我们在每一个子模块内部通过一个aeApiState来存储需要的上下文信息: //select typedefstructaeApiState{ fd_setrfds,wfds; fd_set_rfds,_wfds; }aeApiState; //epoll typedefstructaeApiState{ intepfd; structepoll_event*events; }aeApiState; 这些上下文信息会存储在eventLoop的void *state中,不会暴露到上层,只在当前子模块中使用。 封装 select 函数 select可以监控 FD 的可读、可写以及出现错误的情况。 在介绍 I/O 多路复用模块如何对select函数封装之前,先来看一下select函数使用的大致流程: intfd=/*filedescriptor*/ fd_setrfds; FD_ZERO(&rfds); FD_SET(fd,&rfds) for(;;){ select(fd+1,&rfds,NULL,NULL,NULL); if(FD_ISSET(fd,&rfds)){ /*filedescriptor`fd`becomesreadable*/ } } 初始化一个可读的fd_set集合,保存需要监控可读性的 FD; 使用FD_SET将fd加入rfds; 调用select方法监控rfds中的 FD 是否可读; 当select返回时,检查 FD 的状态并完成对应的操作。 而在 Redis 的ae_select文件中代码的组织顺序也是差不多的,首先在aeApiCreate函数中初始化rfds和wfds: staticintaeApiCreate(aeEventLoop*eventLoop){ aeApiState*state=zmalloc(sizeof(aeApiState)); if(!state)return-1; FD_ZERO(&state->rfds); FD_ZERO(&state->wfds); eventLoop->apidata=state; return0; } 而aeApiAddEvent和aeApiDelEvent会通过FD_SET和FD_CLR修改fd_set中对应 FD 的标志位: staticintaeApiAddEvent(aeEventLoop*eventLoop,intfd,intmask){ aeApiState*state=eventLoop->apidata; if(mask&AE_READABLE)FD_SET(fd,&state->rfds); if(mask&AE_WRITABLE)FD_SET(fd,&state->wfds); return0; } 整个ae_select子模块中最重要的函数就是aeApiPoll,它是实际调用select函数的部分,其作用就是在 I/O 多路复用函数返回时,将对应的 FD 加入aeEventLoop的fired数组中,并返回事件的个数: staticintaeApiPoll(aeEventLoop*eventLoop,structtimeval*tvp){ aeApiState*state=eventLoop->apidata; intretval,j,numevents=0; memcpy(&state->_rfds,&state->rfds,sizeof(fd_set)); memcpy(&state->_wfds,&state->wfds,sizeof(fd_set)); retval=select(eventLoop->maxfd+1, &state->_rfds,&state->_wfds,NULL,tvp); if(retval>0){ for(j=0;jmaxfd;j++){ intmask=0; aeFileEvent*fe=&eventLoop->events[j]; if(fe->mask==AE_NONE)continue; if(fe->mask&AE_READABLE&&FD_ISSET(j,&state->_rfds)) mask|=AE_READABLE; if(fe->mask&AE_WRITABLE&&FD_ISSET(j,&state->_wfds)) mask|=AE_WRITABLE; eventLoop->fired[numevents].fd=j; eventLoop->fired[numevents].mask=mask; numevents++; } } returnnumevents; } 封装 epoll 函数 Redis 对epoll的封装其实也是类似的,使用epoll_create创建epoll中使用的epfd: staticintaeApiCreate(aeEventLoop*eventLoop){ aeApiState*state=zmalloc(sizeof(aeApiState)); if(!state)return-1; state->events=zmalloc(sizeof(structepoll_event)*eventLoop->setsize); if(!state->events){ zfree(state); return-1; } state->epfd=epoll_create(1024);/*1024isjustahintforthekernel*/ if(state->epfd==-1){ zfree(state->events); zfree(state); return-1; } eventLoop->apidata=state; return0; } 在aeApiAddEvent中使用epoll_ctl向epfd中添加需要监控的 FD 以及监听的事件: staticintaeApiAddEvent(aeEventLoop*eventLoop,intfd,intmask){ aeApiState*state=eventLoop->apidata; structepoll_eventee={0};/*avoidvalgrindwarning*/ /*Ifthefdwasalreadymonitoredforsomeevent,weneedaMOD *operation.OtherwiseweneedanADDoperation.*/ intop=eventLoop->events[fd].mask==AE_NONE? EPOLL_CTL_ADD:EPOLL_CTL_MOD; ee.events=0; mask|=eventLoop->events[fd].mask;/*Mergeoldevents*/ if(mask&AE_READABLE)ee.events|=EPOLLIN; if(mask&AE_WRITABLE)ee.events|=EPOLLOUT; ee.data.fd=fd; if(epoll_ctl(state->epfd,op,fd,&ee)==-1)return-1; return0; } 由于epoll相比select机制略有不同,在epoll_wait函数返回时并不需要遍历所有的 FD 查看读写情况;在epoll_wait函数返回时会提供一个epoll_event数组: typedefunionepoll_data{ void*ptr; intfd;/*文件描述符*/ uint32_tu32; uint64_tu64; }epoll_data_t; structepoll_event{ uint32_tevents;/*Epoll事件*/ epoll_data_tdata; }; 其中保存了发生的epoll事件(EPOLLIN、EPOLLOUT、EPOLLERR和EPOLLHUP)以及发生该事件的 FD。 aeApiPoll函数只需要将epoll_event数组中存储的信息加入eventLoop的fired数组中,将信息传递给上层模块: staticintaeApiPoll(aeEventLoop*eventLoop,structtimeval*tvp){ aeApiState*state=eventLoop->apidata; intretval,numevents=0; retval=epoll_wait(state->epfd,state->events,eventLoop->setsize, tvp?(tvp->tv_sec*1000+tvp->tv_usec/1000):-1); if(retval>0){ intj; numevents=retval; for(j=0;j ;j++){ intmask=0; structepoll_event*e=state->events+j; if(e->events&EPOLLIN)mask|=AE_READABLE; if(e->events&EPOLLOUT)mask|=AE_WRITABLE; if(e->events&EPOLLERR)mask|=AE_WRITABLE; if(e->events&EPOLLHUP)mask|=AE_WRITABLE; eventLoop->fired[j].fd=e->data.fd; eventLoop->fired[j].mask=mask; } } returnnumevents; } 子模块的选择 因为 Redis 需要在多个平台上运行,同时为了最大化执行的效率与性能,所以会根据编译平台的不同选择不同的 I/O 多路复用函数作为子模块,提供给上层统一的接口;在 Redis 中,我们通过宏定义的使用,合理的选择不同的子模块: #ifdefHAVE_EVPORT #include"ae_evport.c" #else #ifdefHAVE_EPOLL #include"ae_epoll.c" #else #ifdefHAVE_KQUEUE #include"ae_kqueue.c" #else #include"ae_select.c" #endif #endif #endif 因为select函数是作为 POSIX 标准中的系统调用,在不同版本的操作系统上都会实现,所以将其作为保底方案: redis-choose-io-function Redis 会优先选择时间复杂度为 的 I/O 多路复用函数作为底层实现,包括 Solaries 10 中的evport、Linux 中的epoll和 macOS/FreeBSD 中的kqueue,上述的这些函数都使用了内核内部的结构,并且能够服务几十万的文件描述符。 但是如果当前编译环境没有上述函数,就会选择select作为备选方案,由于其在使用时会扫描全部监听的描述符,所以其时间复杂度较差 ,并且只能同时服务 1024 个文件描述符,所以一般并不会以select作为第一方案使用。 总结 Redis 对于 I/O 多路复用模块的设计非常简洁,通过宏保证了 I/O 多路复用模块在不同平台上都有着优异的性能,将不同的 I/O 多路复用函数封装成相同的 API 提供给上层使用。 整个模块使 Redis 能以单进程运行的同时服务成千上万个文件描述符,避免了由于多进程应用的引入导致代码实现复杂度的提升,减少了出错的可能性。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |