关于Redis中交互的过程
一、Redis啟動
-
加載配置(命令行或者配置文件)
-
啟動TCP監聽,客戶端的列表保存在redisserver的clients中
-
啟動AE Event Loop事件,異步處理客戶請求
事件處理器的主循環
aeMain
void aeMain(aeEventLoop *eventLoop) {eventLoop->stop = 0;while (!eventLoop->stop) {// 如果有需要在事件處理前執行的函數,那么運行它if (eventLoop->beforesleep != NULL)eventLoop->beforesleep(eventLoop);aeProcessEvents(eventLoop, AE_ALL_EVENTS); // 開始處理事件} }事件處理框架非常簡單,從初始化、服務到結束,分別對應的函數:aeCreateEventLoop、aeMain、aeDeleteEventLoop 其中,aeMain是事件循環的主體函數,它又會調用 aeProcessEvents函數,三個主體函數會調用aeApiCreate、aeApiPool、aeApiFree三個接口函數進行處理。 這三個接口函數又會映射到具體的某一種網絡模型中
aeDeleteEventLoop
void aeDeleteEventLoop(aeEventLoop *eventLoop) {aeApiFree(eventLoop);zfree(eventLoop->events);zfree(eventLoop->fired);zfree(eventLoop); }aeCreateEventLoop
aeEventLoop *aeCreateEventLoop(int setsize) {aeEventLoop *eventLoop;int i;if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;eventLoop->setsize = setsize;eventLoop->lastTime = time(NULL);eventLoop->timeEventHead = NULL;eventLoop->timeEventNextId = 0;eventLoop->stop = 0;eventLoop->maxfd = -1;eventLoop->beforesleep = NULL;if (aeApiCreate(eventLoop) == -1) goto err;/* Events with mask == AE_NONE are not set. So let's initialize the* vector with it. */for (i = 0; i < setsize; i++)eventLoop->events[i].mask = AE_NONE;return eventLoop;err:if (eventLoop) {zfree(eventLoop->events);zfree(eventLoop->fired);zfree(eventLoop);} return NULL; }處理事件的時候,aeMain函數調用aeProcessEvents函數,在一個循環中處理文件事件和到期的時間事件。 aeProcessEvents函數調用aeSearchNearestTimer函數來查詢事件循環中最先要過期的事件,時間復雜度為O(N)。先處理文件事件,然后再處理時間事件。
aeProcessEvents
int aeProcessEvents(aeEventLoop *eventLoop, int flags) { int processed = 0, numevents;/* 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 = &eventLoop->events[eventLoop->fired[j].fd];int mask = eventLoop->fired[j].mask;int fd = eventLoop->fired[j].fd;int rfired = 0;| /* 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 */ }二、接受客戶端TCP連接流程
acceptTcpHandler()該函數會調用acceptCommonHander(),而acceptCommonHander()又會調用createClient()來為該client創建一個redisClient對象,最終,redis會根據用戶輸入的命令通過查找命令表調用已經寫好的命令執行函數
acceptTcpHandler
void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {int cport, cfd, max = MAX_ACCEPTS_PER_CALL;char cip[REDIS_IP_STR_LEN];REDIS_NOTUSED(el);REDIS_NOTUSED(mask);REDIS_NOTUSED(privdata);while(max--) {cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);if (cfd == ANET_ERR) {if (errno != EWOULDBLOCK)redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);return;} redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);acceptCommonHandler(cfd,0);} }client輸入get命令,redis server最終會調用getCommand函數,client輸入set命令,redis最終會調用setCommand函數
redis執行完用戶的一個命令后,會將結果寫入到redisClient對象中的reply list中,而sendReplyToClient函數會不斷的從該list中數據,異步地發送給client。需要注意的是,sendReplyToClient函數也是通過aeCreateFileEvent注冊的
三、處理客戶請求流程
通過processInputBuffer()來解析querybuf, 若c->querybuf存在多條命令,則依次解析并處理這些命令
processInputBuffer
void processInputBuffer(redisClient *c) {/* Keep processing while there is something in the input buffer */while(sdslen(c->querybuf)) {/* Return if clients are paused. */if (!(c->flags & REDIS_SLAVE) && clientsArePaused()) return;/* Immediately abort if the client is in the middle of something. */if (c->flags & REDIS_BLOCKED) return;/* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is* written to the client. Make sure to not let the reply grow after* this flag has been set (i.e. don't process more commands). */if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;/* Determine request type when unknown. */if (!c->reqtype) {if (c->querybuf[0] == '*') {c->reqtype = REDIS_REQ_MULTIBULK;} else {c->reqtype = REDIS_REQ_INLINE;}}if (c->reqtype == REDIS_REQ_INLINE) {if (processInlineBuffer(c) != REDIS_OK) break;} else if (c->reqtype == REDIS_REQ_MULTIBULK) {if (processMultibulkBuffer(c) != REDIS_OK) break;} else {redisPanic("Unknown request type");}/* Multibulk processing could see a <= 0 length. */if (c->argc == 0) {resetClient(c);} else {/* Only reset the client when the command was executed. */if (processCommand(c) == REDIS_OK)resetClient(c);}} }-
如果是telnet發送的裸協議數據是沒有*打頭的表示參數個數的輔助信息,用processInlineBuffer()函數解析輸入
-
其他則通過processMultibulkBuffer()函數解析
-
若解析函數返回REDIS_ERR,則等待下一次read(),是因為客戶端緩存數據還沒構成一條命令即不滿足Redis協議格式;否則返回REDIS_OK, 處理命令
四、響應客戶流程
數據讀取?readQueryFromClient
調用系統函數read來讀取客戶端傳送過來的數據, 調用read后對讀取過程中所遇到的情況:
-
系統中斷(nread == -1 && errno == EAGAIN)
-
讀取出錯(nread == -1 && errno != EAGAIN) freeClient()
-
客戶端關閉(nread == 0) freeClient()
-
超過讀取數據限制(1GB)則報錯。 讀取完后進入processInputBuffer進行協議解析
數據解析
從readQueryFromClient()函數讀取客戶端傳過來的數據,進入processInputBuffer()函數進行協議解析,可以把processInputBuffer函數看作是輸入數據的協議解析器
Redis支持兩種協議,一種是inline,一種是multibulk。inline協議是老協議,現在一般只在命令行下的redis客戶端使用,其他情況一般是使用multibulk協議。
如果客戶端傳送的數據的第一個字符時‘*’,那么傳送數據將被當做multibulk協議處理,否則將被當做inline協議處理。Inline協議的具體解析函數是processInlineBuffer(),multibulk協議的具體解析函數是processMultibulkBuffer()。 當協議解析完畢,即客戶端傳送的數據已經解析出命令字段和參數字段,接下來進行命令處理,命令處理函數是processCommand。
發送數據?sendReplyToClient
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {redisClient *c = privdata;int nwritten = 0, totwritten = 0, objlen;size_t objmem;robj *o;REDIS_NOTUSED(el);REDIS_NOTUSED(mask);while(c->bufpos > 0 || listLength(c->reply)) {if (c->bufpos > 0) {nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);if (nwritten <= 0) break;c->sentlen += nwritten;totwritten += nwritten;/* If the buffer was sent, set bufpos to zero to continue with* the remainder of the reply. */if (c->sentlen == c->bufpos) {c->bufpos = 0;c->sentlen = 0;}} else {o = listNodeValue(listFirst(c->reply));objlen = sdslen(o->ptr);objmem = getStringObjectSdsUsedMemory(o);if (objlen == 0) {listDelNode(c->reply,listFirst(c->reply));c->reply_bytes -= objmem;continue;}nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);if (nwritten <= 0) break;c->sentlen += nwritten;totwritten += nwritten;/* If we fully sent the object on head go to the next one */if (c->sentlen == objlen) {listDelNode(c->reply,listFirst(c->reply));c->sentlen = 0;c->reply_bytes -= objmem;}}/* Note that we avoid to send more than REDIS_MAX_WRITE_PER_EVENT* bytes, in a single threaded server it's a good idea to serve* other clients as well, even if a very large request comes from* super fast link that is always able to accept data (in real world* scenario think about 'KEYS *' against the loopback interface).** However if we are over the maxmemory limit we ignore that and* just deliver as much data as it is possible to deliver. */server.stat_net_output_bytes += totwritten;if (totwritten > REDIS_MAX_WRITE_PER_EVENT &&(server.maxmemory == 0 ||zmalloc_used_memory() < server.maxmemory)) break;}if (nwritten == -1) {if (errno == EAGAIN) {nwritten = 0;} else {redisLog(REDIS_VERBOSE,"Error writing to client: %s", strerror(errno));freeClient(c);return;}}if (totwritten > 0) {/* For clients representing masters we don't count sending data* as an interaction, since we always send REPLCONF ACK commands* that take some time to just fill the socket output buffer.* We just rely on data / pings received for timeout detection. */if (!(c->flags & REDIS_MASTER)) c->lastinteraction = server.unixtime;}if (c->bufpos == 0 && listLength(c->reply) == 0) {c->sentlen = 0;aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);/* Close connection after entire reply has been sent. */if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);} }通過調用系統函數write給客戶端發送數據,如果緩沖區有數據就把緩沖區的數據發送給客戶端,緩沖區的數據發送完了,如果有排隊數據,則繼續發送。
-
發送緩沖區(c->buf)的內容
-
發送回復鏈表(c->reply)的內容
寫入異常處理
-
被系統中斷(nwritten == -1 && errno == EAGAIN)
-
寫數據出錯(nwritten == -1 && errno != EAGAIN),釋放客戶端freeClient()
?
參考文章
http://arc8.riaos.com/?p=6061
https://github.com/microheart/annotated_memcached/blob/master/note/Redis/Redis_main_flow.md
http://blog.csdn.net/ordeder/article/details/12791359
http://blog.nosqlfan.com/tags/redis
轉載于:https://www.cnblogs.com/chenpingzhao/p/5215467.html
總結
以上是生活随笔為你收集整理的关于Redis中交互的过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 另类vs2015+xamarin 的an
- 下一篇: RunTime 入门