Redis源碼分析(二十二)--- networking網路通訊協定傳輸

來源:互聯網
上載者:User

標籤:記憶體資料庫   redis   networking   源碼   磁碟   

          上次我只分析了Redis網路部分的代碼一部分,今天我把networking的代碼實現部分也學習了一遍,netWorking的代碼更多偏重的是Client用戶端的操作。裡面addReply()系列的方法操作是主要的部分。光光這個系列的方法,應該佔據了一半的API的數量。我把API分成了3個部分:

/* ------------ API ---------------------- */void *dupClientReplyValue(void *o)/* 複製value一份 */int listMatchObjects(void *a, void *b) /* 比價2個obj是否相等 */robj *dupLastObjectIfNeeded(list *reply) /* 返回回複列表中最後一個元素對象 */void copyClientOutputBuffer(redisClient *dst, redisClient *src) /* 將源Client的輸出buffer複製給目標Client */static void acceptCommonHandler(int fd, int flags) /* 網路連接後的調用方法 */void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask)void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask)void disconnectSlaves(void) /* 使server的slave失去串連 */void replicationHandleMasterDisconnection(void)void flushSlavesOutputBuffers(void) /* 從方法將會在freeMemoryIfNeeded(),釋放記憶體空間函數,將存在記憶體中資料操作結果重新整理到磁碟中 */int processEventsWhileBlocked(void)/* ------------- addReply API -----------------   */int _addReplyToBuffer(redisClient *c, char *s, size_t len) /* 往用戶端緩衝區中新增內容 */void _addReplyObjectToList(redisClient *c, robj *o) /* robj添加到reply的列表中 */void _addReplySdsToList(redisClient *c, sds s) /* 在回複列表中添加Sds字串對象 */void _addReplyStringToList(redisClient *c, char *s, size_t len) /* 在回複列表中添加字串對象,參數中已經給定字元的長度 */void addReply(redisClient *c, robj *obj) /* 在redisClient的buffer中寫入資料,資料存在obj->ptr的指標中 */void addReplySds(redisClient *c, sds s) /* 在回複中添加Sds字串,下面的額addReply()系列方法原理基本類似 */void addReplyString(redisClient *c, char *s, size_t len)void addReplyErrorLength(redisClient *c, char *s, size_t len)void addReplyError(redisClient *c, char *err) /* 往Reply中添加error類的資訊 */void addReplyErrorFormat(redisClient *c, const char *fmt, ...)void addReplyStatusLength(redisClient *c, char *s, size_t len)void addReplyStatus(redisClient *c, char *status)void addReplyStatusFormat(redisClient *c, const char *fmt, ...)void *addDeferredMultiBulkLength(redisClient *c) /* 在reply list 中添加一個空的obj對象 */void setDeferredMultiBulkLength(redisClient *c, void *node, long length)void addReplyDouble(redisClient *c, double d) /* 在bulk reply中添加一個double類型值,bulk的意思為大塊的,bulk reply的意思為大資料量的回複 */void addReplyLongLongWithPrefix(redisClient *c, long long ll, char prefix)void addReplyLongLong(redisClient *c, long long ll)void addReplyMultiBulkLen(redisClient *c, long length)void addReplyBulkLen(redisClient *c, robj *obj) /* 添加bulk 大塊的資料的長度 */void addReplyBulk(redisClient *c, robj *obj) /* 將一個obj的資料,拆分成大塊資料的添加 */void addReplyBulkCBuffer(redisClient *c, void *p, size_t len)void addReplyBulkCString(redisClient *c, char *s)void addReplyBulkLongLong(redisClient *c, long long ll)/* ------------- Client API -----------------   */redisClient *createClient(int fd) /* 建立redisClient用戶端,1.建立串連,2.設定資料庫,3.屬性設定 */int prepareClientToWrite(redisClient *c) /* 此方法將會被調用於Client準備接受新資料之前調用,在fileEvent為用戶端設定writer的handler處理事件 */static void freeClientArgv(redisClient *c)void freeClient(redisClient *c) /* 釋放freeClient,要分為Master和Slave2種情況作不同的處理 */void freeClientAsync(redisClient *c)void freeClientsInAsyncFreeQueue(void) /* 非同步free用戶端 */void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) /* 將Client中的reply資料存入檔案中 */void resetClient(redisClient *c)int processInlineBuffer(redisClient *c) /* 處理redis Client的內鏈的buffer,就是c->querybuf */static void setProtocolError(redisClient *c, int pos)int processMultibulkBuffer(redisClient *c) /* 處理大塊的buffer */void processInputBuffer(redisClient *c) /* 處理redisClient的查詢buffer */void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) /* 從Client擷取查詢query語句 */void getClientsMaxBuffers(unsigned long *longest_output_list,                          unsigned long *biggest_input_buffer) /* 擷取Client中輸入buffer和輸出buffer的最大長度值 */void formatPeerId(char *peerid, size_t peerid_len, char *ip, int port) /* 格式化ip,port連接埠號碼的輸出,ip:port */int genClientPeerId(redisClient *client, char *peerid, size_t peerid_len) /* 擷取Client用戶端的ip,port地址資訊 */char *getClientPeerId(redisClient *c) /* 擷取c->peerid用戶端的地址資訊 */sds catClientInfoString(sds s, redisClient *client) /* 格式化的輸出用戶端的屬性資訊,直接返回一個拼接好的字串 */sds getAllClientsInfoString(void) /* 擷取所有Client用戶端的屬性資訊,並串連成一個總的字串並輸出 */void clientCommand(redisClient *c) /* 執行用戶端的命令的作法 */void rewriteClientCommandVector(redisClient *c, int argc, ...) /* 重寫用戶端的命令集合,舊的命令集合的應用計數減1,新的Command  Vector的命令集合增1 */void rewriteClientCommandArgument(redisClient *c, int i, robj *newval) /* 重寫Client中的第i個參數 */unsigned long getClientOutputBufferMemoryUsage(redisClient *c) /* 擷取Client中已經用去的輸出buffer的大小 */int getClientType(redisClient *c)int getClientTypeByName(char *name) /* Client中的名字的3種類型,normal,slave,pubsub */char *getClientTypeName(int class)int checkClientOutputBufferLimits(redisClient *c) /* 判斷Clint的輸出緩衝區的已經佔用大小是否超過軟式節流或是硬限制 */void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) /* 非同步關閉Client,如果緩衝區中的軟式節流或是硬限制已經到達的時候,緩衝區超出限制的結果會導致釋放不安全, */
我們從最簡單的_addReplyToBuffer在緩衝區中添加回複資料開始說起,因為後面的各種addReply的方法都或多或少的調用了和這個歌方法。

/* ----------------------------------------------------------------------------- * Low level functions to add more data to output buffers. * -------------------------------------------------------------------------- *//* 往用戶端緩衝區中新增內容 */int _addReplyToBuffer(redisClient *c, char *s, size_t len) {    size_t available = sizeof(c->buf)-c->bufpos;    if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK;    /* If there already are entries in the reply list, we cannot     * add anything more to the static buffer. */     //如果當前的reply已經存在內容,則操作出錯    if (listLength(c->reply) > 0) return REDIS_ERR;    /* Check that the buffer has enough space available for this string. */    if (len > available) return REDIS_ERR;    memcpy(c->buf+c->bufpos,s,len);    c->bufpos+=len;    return REDIS_OK;}
最直接影響的一句話,就是memcpy(c->buf+c->bufpos,s,len);所以內容是加到c->buf中的,這也就是用戶端的輸出buffer,添加操作還有另外一種形式是添加物件類型:

/* robj添加到reply的列表中 */void _addReplyObjectToList(redisClient *c, robj *o) {    robj *tail;    if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;    if (listLength(c->reply) == 0) {        incrRefCount(o);        //在回複列表匯總添加robj內容        listAddNodeTail(c->reply,o);        c->reply_bytes += zmalloc_size_sds(o->ptr);    } else {        tail = listNodeValue(listLast(c->reply));        /* Append to this object when possible. */        if (tail->ptr != NULL &&            sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)        {            c->reply_bytes -= zmalloc_size_sds(tail->ptr);            tail = dupLastObjectIfNeeded(c->reply);            tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));            c->reply_bytes += zmalloc_size_sds(tail->ptr);        } else {            incrRefCount(o);            listAddNodeTail(c->reply,o);            c->reply_bytes += zmalloc_size_sds(o->ptr);        }    }    asyncCloseClientOnOutputBufferLimitReached(c);}
把robj對象載入reply列表中,並且改變reply的byte大小,最後還調用了一個asyncCloseClientOnOutputBufferLimitReached(c);方法,這個方法我是在這個檔案的最底部找到的,一開始還真不知道什麼意思,作用就是當添加完資料後,當用戶端的輸出緩衝的大小超出限制時,會被非同步關閉:

/* Asynchronously close a client if soft or hard limit is reached on the * output buffer size. The caller can check if the client will be closed * checking if the client REDIS_CLOSE_ASAP flag is set. * * Note: we need to close the client asynchronously because this function is * called from contexts where the client can't be freed safely, i.e. from the * lower level functions pushing data inside the client output buffers. *//* 非同步關閉Client,如果緩衝區中的軟式節流或是硬限制已經到達的時候,緩衝區超出限制的結果會導致釋放不安全, */void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) {    redisAssert(c->reply_bytes < ULONG_MAX-(1024*64));    if (c->reply_bytes == 0 || c->flags & REDIS_CLOSE_ASAP) return;    if (checkClientOutputBufferLimits(c)) {        sds client = catClientInfoString(sdsempty(),c);        freeClientAsync(c);        redisLog(REDIS_WARNING,"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.", client);        sdsfree(client);    }}
在addReply方法調用的時候,有時是需要一個前提的,我說的是在寫資料事件發生的時候,你得先對寫的檔案建立一個監聽事件:

/* 在回複中添加Sds字串 */void addReplySds(redisClient *c, sds s) {//在調用添加操作之前,都要先執行prepareClientToWrite(c),設定檔案事件的寫事件    if (prepareClientToWrite(c) != REDIS_OK) {        /* The caller expects the sds to be free'd. */        sdsfree(s);        return;    }    if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {        sdsfree(s);    } else {        /* This method free's the sds when it is no longer needed. */        _addReplySdsToList(c,s);    }}
在這個prepareClientToWrite()裡面是幹嘛的呢?

/* This function is called every time we are going to transmit new data * to the client. The behavior is the following: * * If the client should receive new data (normal clients will) the function * returns REDIS_OK, and make sure to install the write handler in our event * loop so that when the socket is writable new data gets written. * * If the client should not receive new data, because it is a fake client, * a master, a slave not yet online, or because the setup of the write handler * failed, the function returns REDIS_ERR. * * Typically gets called every time a reply is built, before adding more * data to the clients output buffers. If the function returns REDIS_ERR no * data should be appended to the output buffers. *//* 此方法將會被調用於Client準備接受新資料之前調用,在fileEvent為用戶端設定writer的handler處理事件 */int prepareClientToWrite(redisClient *c) {    if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK;    if ((c->flags & REDIS_MASTER) &&        !(c->flags & REDIS_MASTER_FORCE_REPLY)) return REDIS_ERR;    if (c->fd <= 0) return REDIS_ERR; /* Fake client */    if (c->bufpos == 0 && listLength(c->reply) == 0 &&        (c->replstate == REDIS_REPL_NONE ||         c->replstate == REDIS_REPL_ONLINE) &&        //在這裡建立寫的檔案事件        aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,        sendReplyToClient, c) == AE_ERR) return REDIS_ERR;    return REDIS_OK;}
在addReply的方法裡提到了一個addReplyBulk類型方法,Bulk的中文意思為大塊的,說明addReplyBulk添加的都是一些比較大塊的資料,找一個方法看看:

/* Add a Redis Object as a bulk reply *//* 將一個obj的資料,拆分成大塊資料的添加 */void addReplyBulk(redisClient *c, robj *obj) {//reply添加長度    addReplyBulkLen(c,obj);    //reply添加對象    addReply(c,obj);    addReply(c,shared.crlf);}
將原本一個robj的資料拆分成可3個普通的addReply的方法調用。就變成了資料量變大了的資料。大資料的回複一個比較不好的地方是到時解析的時候或者是Data的複製的時候會比較耗時。在networking的方法裡還提供了freeClient()的操作:

/* 釋放freeClient,要分為Master和Slave2種情況作不同的處理 */void freeClient(redisClient *c) {    listNode *ln;    /* If this is marked as current client unset it */    if (server.current_client == c) server.current_client = NULL;    /* If it is our master that's beging disconnected we should make sure     * to cache the state to try a partial resynchronization later.     *     * Note that before doing this we make sure that the client is not in     * some unexpected state, by checking its flags. */    if (server.master && c->flags & REDIS_MASTER) {        redisLog(REDIS_WARNING,"Connection with master lost.");        if (!(c->flags & (REDIS_CLOSE_AFTER_REPLY|                          REDIS_CLOSE_ASAP|                          REDIS_BLOCKED|                          REDIS_UNBLOCKED)))        {        //如果是Master用戶端,需要做緩衝Client的處理,可以迅速重新啟用            replicationCacheMaster(c);            return;        }    }
...後面代碼略去了

   當Client中的輸出buffer資料漸漸層多了的時候就要準備持久化到磁碟檔案了,要調用下面這個方法了,

/* Helper function used by freeMemoryIfNeeded() in order to flush slave * output buffers without returning control to the event loop. *//* 從方法將會在freeMemoryIfNeeded(),釋放記憶體空間函數,將存在記憶體中資料操作結果重新整理到磁碟中 */void flushSlavesOutputBuffers(void) {    listIter li;    listNode *ln;    listRewind(server.slaves,&li);    while((ln = listNext(&li))) {        redisClient *slave = listNodeValue(ln);        int events;        events = aeGetFileEvents(server.el,slave->fd);        if (events & AE_WRITABLE &&            slave->replstate == REDIS_REPL_ONLINE &&            listLength(slave->reply))        {        //在這裡調用了write的方法            sendReplyToClient(server.el,slave->fd,slave,0);        }    }}
這個方法的核心調用又在sendReplyToClient()方法,就是把Client的reply內容和buf內容存入檔案。以上就是我的理解了,代碼量有點大,的確看的我頭有點大。

Redis源碼分析(二十二)--- networking網路通訊協定傳輸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.