Redis源碼剖析和註釋(二十)--- 網絡連接庫剖析(client的創建/釋放、命令接收/回覆、Redis通信協議分析等)

Redis 網絡連接庫剖析

1. Redis網絡連接庫介紹

Redis網絡連接庫對應的文件是networking.c。這個文件主要負責

  • 客戶端的創建與釋放
  • 命令接收與命令回覆
  • Redis通信協議分析
  • CLIENT 命令的實現

我們接下來就這幾塊內容分別列出源碼,進行剖析。

2. 客戶端的創建與釋放

redis 網絡鏈接庫的源碼詳細註釋

2.1客戶端的創建

Redis 服務器是一個同時與多個客戶端建立連接的程序。當客戶端連接上服務器時,服務器會建立一個server.h/client結構來保存客戶端的狀態信息。所以在客戶端創建時,就會初始化這樣一個結構,客戶端的創建源碼如下:

client *createClient(int fd) {
    client *c = zmalloc(sizeof(client));    //分配空間

    // 如果fd爲-1,表示創建的是一個無網絡連接的僞客戶端,用於執行lua腳本的時候。
    // 如果fd不等於-1,表示創建一個有網絡連接的客戶端
    if (fd != -1) {
        // 設置fd爲非阻塞模式
        anetNonBlock(NULL,fd);
        // 禁止使用 Nagle 算法,client向內核遞交的每個數據包都會立即發送給server出去,TCP_NODELAY
        anetEnableTcpNoDelay(NULL,fd);
        // 如果開啓了tcpkeepalive,則設置 SO_KEEPALIVE
        if (server.tcpkeepalive)
            // 設置tcp連接的keep alive選項
            anetKeepAlive(NULL,fd,server.tcpkeepalive);
        // 創建一個文件事件狀態el,且監聽讀事件,開始接受命令的輸入
        if (aeCreateFileEvent(server.el,fd,AE_READABLE,
            readQueryFromClient, c) == AE_ERR)
        {
            close(fd);
            zfree(c);
            return NULL;
        }
    }

    // 默認選0號數據庫
    selectDb(c,0);
    // 設置client的ID
    c->id = server.next_client_id++;
    // client的套接字
    c->fd = fd;
    // client的名字
    c->name = NULL;
    // 回覆固定(靜態)緩衝區的偏移量
    c->bufpos = 0;
    // 輸入緩存區
    c->querybuf = sdsempty();
    // 輸入緩存區的峯值
    c->querybuf_peak = 0;
    // 請求協議類型,內聯或者多條命令,初始化爲0
    c->reqtype = 0;
    // 參數個數
    c->argc = 0;
    // 參數列表
    c->argv = NULL;
    // 當前執行的命令和最近一次執行的命令
    c->cmd = c->lastcmd = NULL;
    // 查詢緩衝區剩餘未讀取命令的數量
    c->multibulklen = 0;
    // 讀入參數的長度
    c->bulklen = -1;
    // 已發的字節數
    c->sentlen = 0;
    // client的狀態
    c->flags = 0;
    // 設置創建client的時間和最後一次互動的時間
    c->ctime = c->lastinteraction = server.unixtime;
    // 認證狀態
    c->authenticated = 0;
    // replication複製的狀態,初始爲無
    c->replstate = REPL_STATE_NONE;
    // 設置從節點的寫處理器爲ack,是否在slave向master發送ack
    c->repl_put_online_on_ack = 0;
    // replication複製的偏移量
    c->reploff = 0;
    // 通過ack命令接收到的偏移量
    c->repl_ack_off = 0;
    // 通過ack命令接收到的偏移量所用的時間
    c->repl_ack_time = 0;
    // 從節點的端口號
    c->slave_listening_port = 0;
    // 從節點IP地址
    c->slave_ip[0] = '\0';
    // 從節點的功能
    c->slave_capa = SLAVE_CAPA_NONE;
    // 回覆鏈表
    c->reply = listCreate();
    // 回覆鏈表的字節數
    c->reply_bytes = 0;
    // 回覆緩衝區的內存大小軟限制
    c->obuf_soft_limit_reached_time = 0;
    // 回覆鏈表的釋放和複製方法
    listSetFreeMethod(c->reply,decrRefCountVoid);
    listSetDupMethod(c->reply,dupClientReplyValue);
    // 阻塞類型
    c->btype = BLOCKED_NONE;
    // 阻塞超過時間
    c->bpop.timeout = 0;
    // 造成阻塞的鍵字典
    c->bpop.keys = dictCreate(&setDictType,NULL);
    // 存儲解除阻塞的鍵,用於保存PUSH入元素的鍵,也就是dstkey
    c->bpop.target = NULL;
    // 阻塞狀態
    c->bpop.numreplicas = 0;
    // 要達到的複製偏移量
    c->bpop.reploffset = 0;
    // 全局的複製偏移量
    c->woff = 0;
    // 監控的鍵
    c->watched_keys = listCreate();
    // 訂閱頻道
    c->pubsub_channels = dictCreate(&setDictType,NULL);
    // 訂閱模式
    c->pubsub_patterns = listCreate();
    // 被緩存的peerid,peerid就是 ip:port
    c->peerid = NULL;
    // 訂閱發佈模式的釋放和比較方法
    listSetFreeMethod(c->pubsub_patterns,decrRefCountVoid);
    listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
    // 將真正的client放在服務器的客戶端鏈表中
    if (fd != -1) listAddNodeTail(server.clients,c);
    // 初始化client的事物狀態
    initClientMultiState(c);
    return c;
}

根據傳入的文件描述符fd,可以創建用於不同情景下的client。這個fd就是服務器接收客戶端connect後所返回的文件描述符。

  • fd == -1。表示創建一個無網絡連接的客戶端。主要用於執行 lua 腳本時。
  • fd != -1。表示接收到一個正常的客戶端連接,則會創建一個有網絡連接的客戶端,也就是創建一個文件事件,來監聽這個fd是否可讀,當客戶端發送數據,則事件被觸發。創建客戶端時,還會禁用Nagle算法。

Nagle算法能自動連接許多的小緩衝器消息,這一過程(稱爲nagling)通過減少必須發送包的個數來增加網絡軟件系統的效率。但是服務器和客戶端的對即使通信性有很高的要求,因此禁止使用 Nagle 算法,客戶端向內核遞交的每個數據包都會立即發送給服務器。

創建客戶端的過程,會將server.h/client結構的所有成員初始化,接下里會介紹部分重點的成員。

  • int id:服務器對於每一個連接進來的都會創建一個ID,客戶端的ID從1開始。每次重啓服務器會刷新。
  • int fd:當前客戶端狀態描述符。分爲無網絡連接的客戶端和有網絡連接的客戶端。
  • int flags:客戶端狀態的標誌。Redis 3.2.8 中在server.h中定義了23種狀態。
  • robj *name:默認創建的客戶端是沒有名字的,可以通過CLIENT SETNAME命令設置名字。後面會介紹該命令的實現。
  • int reqtype:請求協議的類型。因爲Redis服務器支持Telnet的連接,因此Telnet命令請求協議類型是PROTO_REQ_INLINE,而redis-cli命令請求的協議類型是PROTO_REQ_MULTIBULK

用於保存服務器接受客戶端命令的成員:

  • sds querybuf:保存客戶端發來命令請求的輸入緩衝區。以Redis通信協議的方式保存。
  • size_t querybuf_peak:保存輸入緩衝區的峯值。
  • int argc:命令參數個數。
  • robj *argv:命令參數列表。

用於保存服務器給客戶端回覆的成員:

  • char buf[16*1024]:保存執行完命令所得命令回覆信息的靜態緩衝區,它的大小是固定的,所以主要保存的是一些比較短的回覆。分配client結構空間時,就會分配一個16K的大小。
  • int bufpos:記錄靜態緩衝區的偏移量,也就是buf數組已經使用的字節數。
  • list *reply:保存命令回覆的鏈表。因爲靜態緩衝區大小固定,主要保存固定長度的命令回覆,當處理一些返回大量回復的命令,則會將命令回覆以鏈表的形式連接起來。
  • unsigned long long reply_bytes:保存回覆鏈表的字節數。
  • size_t sentlen:已發送回覆的字節數。

2.2 客戶端的釋放

客戶端的釋放freeClient()函數主要就是釋放各種數據結構和清空一些緩衝區等等操作,這裏就不列出源碼。但是我們關注一下異步釋放客戶端。源碼如下:

// 異步釋放client
void freeClientAsync(client *c) {
    // 如果是已經即將關閉或者是lua腳本的僞client,則直接返回
    if (c->flags & CLIENT_CLOSE_ASAP || c->flags & CLIENT_LUA) return;
    c->flags |= CLIENT_CLOSE_ASAP;
    // 將client加入到即將關閉的client鏈表中
    listAddNodeTail(server.clients_to_close,c);
}
  • server.clients_to_close:是服務器保存所有待關閉的client鏈表。

設置異步釋放客戶端的目的主要是:防止底層函數正在向客戶端的輸出緩衝區寫數據的時候,關閉客戶端,這樣是不安全的。Redis會安排客戶端在serverCron()函數的安全時間釋放它。

當然也可以取消異步釋放,那麼就會調用freeClient()函數立即釋放。源碼如下:

// 取消設置異步釋放的client
void freeClientsInAsyncFreeQueue(void) {
    // 遍歷所有即將關閉的client
    while (listLength(server.clients_to_close)) {
        listNode *ln = listFirst(server.clients_to_close);
        client *c = listNodeValue(ln);

        // 取消立即關閉的標誌
        c->flags &= ~CLIENT_CLOSE_ASAP;
        freeClient(c);
        // 從即將關閉的client鏈表中刪除
        listDelNode(server.clients_to_close,ln);
    }
}

3. 命令接收與命令回覆

redis 網絡鏈接庫的源碼詳細註釋

3.1 命令接收

當客戶端連接上Redis服務器後,服務器會得到一個文件描述符fd,而且服務器會監聽該文件描述符的讀事件,這些在createClient()函數中,我們有分析。那麼當客戶端發送了命令,觸發了AE_READABLE事件,那麼就會調用回調函數readQueryFromClient()來從文件描述符fd中讀發來的命令,並保存在輸入緩衝區中querybuf。而這個回調函數就是我們在Redis 事件處理實現一文中所提到的指向回調函數的指針rfileProcwfileProc。那麼,我們先來分析sendReplyToClient()函數。

// 讀取client的輸入緩衝區的內容
void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
    client *c = (client*) privdata;
    int nread, readlen;
    size_t qblen;
    UNUSED(el);
    UNUSED(mask);

    // 讀入的長度,默認16MB
    readlen = PROTO_IOBUF_LEN;
    /* If this is a multi bulk request, and we are processing a bulk reply
     * that is large enough, try to maximize the probability that the query
     * buffer contains exactly the SDS string representing the object, even
     * at the risk of requiring more read(2) calls. This way the function
     * processMultiBulkBuffer() can avoid copying buffers to create the
     * Redis Object representing the argument. */
    // 如果是多條請求,根據請求的大小,設置讀入的長度readlen
    if (c->reqtype == PROTO_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1
        && c->bulklen >= PROTO_MBULK_BIG_ARG)
    {
        int remaining = (unsigned)(c->bulklen+2)-sdslen(c->querybuf);

        if (remaining < readlen) readlen = remaining;
    }

    // 輸入緩衝區的長度
    qblen = sdslen(c->querybuf);
    // 更新緩衝區的峯值
    if (c->querybuf_peak < qblen) c->querybuf_peak = qblen;
    // 擴展緩衝區的大小
    c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);
    // 將client發來的命令,讀入到輸入緩衝區中
    nread = read(fd, c->querybuf+qblen, readlen);
    // 讀操作出錯
    if (nread == -1) {
        if (errno == EAGAIN) {
            return;
        } else {
            serverLog(LL_VERBOSE, "Reading from client: %s",strerror(errno));
            freeClient(c);
            return;
        }
    // 讀操作完成
    } else if (nread == 0) {
        serverLog(LL_VERBOSE, "Client closed connection");
        freeClient(c);
        return;
    }
    // 更新輸入緩衝區的已用大小和未用大小。
    sdsIncrLen(c->querybuf,nread);
    // 設置最後一次服務器和client交互的時間
    c->lastinteraction = server.unixtime;
    // 如果是主節點,則更新複製操作的偏移量
    if (c->flags & CLIENT_MASTER) c->reploff += nread;
    // 更新從網絡輸入的字節數
    server.stat_net_input_bytes += nread;
    // 如果輸入緩衝區長度超過服務器設置的最大緩衝區長度
    if (sdslen(c->querybuf) > server.client_max_querybuf_len) {
        // 將client信息轉換爲sds
        sds ci = catClientInfoString(sdsempty(),c), bytes = sdsempty();

        // 輸入緩衝區保存在bytes中
        bytes = sdscatrepr(bytes,c->querybuf,64);
        // 打印到日誌
        serverLog(LL_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)", ci, bytes);
        // 釋放空間
        sdsfree(ci);
        sdsfree(bytes);
        freeClient(c);
        return;
    }
    // 處理client輸入的命令內容
    processInputBuffer(c);
}

實際上,這個readQueryFromClient()函數是read函數的封裝,從文件描述符fd中讀出數據到輸入緩衝區querybuf中,並更新輸入緩衝區的峯值querybuf_peak,而且會檢查讀的長度,如果大於了server.client_max_querybuf_len則會退出,而這個閥值在服務器初始化爲PROTO_MAX_QUERYBUF_LEN (1024*1024*1024)也就是1G大小。

回憶之前的各種命令實現,都是通過client的argvargc這兩個成員來處理的。因此,服務器還需要將輸入緩衝區querybuf中的數據,處理成參數列表的對象,也就是上面的processInputBuffer()函數。源碼如下:

// 處理client輸入的命令內容
void processInputBuffer(client *c) {
    server.current_client = c;
    /* Keep processing while there is something in the input buffer */
    // 一直讀輸入緩衝區的內容
    while(sdslen(c->querybuf)) {
        /* Return if clients are paused. */
        // 如果處於暫停狀態,直接返回
        if (!(c->flags & CLIENT_SLAVE) && clientsArePaused()) break;

        /* Immediately abort if the client is in the middle of something. */
        // 如果client處於被阻塞狀態,直接返回
        if (c->flags & CLIENT_BLOCKED) break;

        // 如果client處於關閉狀態,則直接返回
        if (c->flags & (CLIENT_CLOSE_AFTER_REPLY|CLIENT_CLOSE_ASAP)) break;

        /* Determine request type when unknown. */
        // 如果是未知的請求類型,則判定請求類型
        if (!c->reqtype) {
            // 如果是"*"開頭,則是多條請求,是client發來的
            if (c->querybuf[0] == '*') {
                c->reqtype = PROTO_REQ_MULTIBULK;
            // 否則就是內聯請求,是Telnet發來的
            } else {
                c->reqtype = PROTO_REQ_INLINE;
            }
        }

        // 如果是內聯請求
        if (c->reqtype == PROTO_REQ_INLINE) {
            // 處理Telnet發來的內聯命令,並創建成對象,保存在client的參數列表中
            if (processInlineBuffer(c) != C_OK) break;
        // 如果是多條請求
        } else if (c->reqtype == PROTO_REQ_MULTIBULK) {
            // 將client的querybuf中的協議內容轉換爲client的參數列表中的對象
            if (processMultibulkBuffer(c) != C_OK) break;
        } else {
            serverPanic("Unknown request type");
        }

        /* Multibulk processing could see a <= 0 length. */
        // 如果參數爲0,則重置client
        if (c->argc == 0) {
            resetClient(c);
        } else {
            /* Only reset the client when the command was executed. */
            // 執行命令成功後重置client
            if (processCommand(c) == C_OK)
                resetClient(c);
            /* freeMemoryIfNeeded may flush slave output buffers. This may result
             * into a slave, that may be the active client, to be freed. */
            if (server.current_client == NULL) break;
        }
    }
    // 執行成功,則將用於崩潰報告的client設置爲NULL
    server.current_client = NULL;
}

這個processInputBuffer()函數只要根據reqtype來判斷和設置請求的類型,之前提過,因爲Redis服務器支持Telnet的連接,因此Telnet命令請求協議類型是PROTO_REQ_INLINE,進而調用processInlineBuffer()函數處理,而redis-cli命令請求的協議類型是PROTO_REQ_MULTIBULK,進而調用processMultibulkBuffer()函數來處理。我們只要看processMultibulkBuffer()函數,是如果將Redis協議的命令,處理成參數列表的對象的。源碼如下:

// 將client的querybuf中的協議內容轉換爲client的參數列表中的對象
int processMultibulkBuffer(client *c) {
    char *newline = NULL;
    int pos = 0, ok;
    long long ll;

    // 參數列表中命令數量爲0
    if (c->multibulklen == 0) {
        /* The client should have been reset */
        serverAssertWithInfo(c,NULL,c->argc == 0);

        /* Multi bulk length cannot be read without a \r\n */
        // 查詢第一個換行符
        newline = strchr(c->querybuf,'\r');
        // 沒有找到\r\n,表示不符合協議,返回錯誤
        if (newline == NULL) {
            if (sdslen(c->querybuf) > PROTO_INLINE_MAX_SIZE) {
                addReplyError(c,"Protocol error: too big mbulk count string");
                setProtocolError(c,0);
            }
            return C_ERR;
        }

        /* Buffer should also contain \n */
        // 檢查格式
        if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
            return C_ERR;

        /* We know for sure there is a whole line since newline != NULL,
         * so go ahead and find out the multi bulk length. */
        // 保證第一個字符爲'*'
        serverAssertWithInfo(c,NULL,c->querybuf[0] == '*');
        // 將'*'之後的數字轉換爲整數。*3\r\n
        ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
        if (!ok || ll > 1024*1024) {
            addReplyError(c,"Protocol error: invalid multibulk length");
            setProtocolError(c,pos);
            return C_ERR;
        }

        // 指向"*3\r\n"的"\r\n"之後的位置
        pos = (newline-c->querybuf)+2;
        // 空白命令,則將之前的刪除,保留未閱讀的部分
        if (ll <= 0) {
            sdsrange(c->querybuf,pos,-1);
            return C_OK;
        }

        // 參數數量
        c->multibulklen = ll;

        /* Setup argv array on client structure */
        // 分配client參數列表的空間
        if (c->argv) zfree(c->argv);
        c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
    }

    serverAssertWithInfo(c,NULL,c->multibulklen > 0);
    // 讀入multibulklen個參數,並創建對象保存在參數列表中
    while(c->multibulklen) {
        /* Read bulk length if unknown */
        // 讀入參數的長度
        if (c->bulklen == -1) {
            // 找到換行符,確保"\r\n"存在
            newline = strchr(c->querybuf+pos,'\r');
            if (newline == NULL) {
                if (sdslen(c->querybuf) > PROTO_INLINE_MAX_SIZE) {
                    addReplyError(c,
                        "Protocol error: too big bulk count string");
                    setProtocolError(c,0);
                    return C_ERR;
                }
                break;
            }

            /* Buffer should also contain \n */
            // 檢查格式
            if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
                break;

            // $3\r\nSET\r\n...,確保是'$'字符,保證格式
            if (c->querybuf[pos] != '$') {
                addReplyErrorFormat(c,
                    "Protocol error: expected '$', got '%c'",
                    c->querybuf[pos]);
                setProtocolError(c,pos);
                return C_ERR;
            }

            // 將命令長度保存到ll。
            ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
            if (!ok || ll < 0 || ll > 512*1024*1024) {
                addReplyError(c,"Protocol error: invalid bulk length");
                setProtocolError(c,pos);
                return C_ERR;
            }

            // 定位第一個參數的位置,也就是SET的S
            pos += newline-(c->querybuf+pos)+2;
            // 參數太長,進行優化
            if (ll >= PROTO_MBULK_BIG_ARG) {
                size_t qblen;

                /* If we are going to read a large object from network
                 * try to make it likely that it will start at c->querybuf
                 * boundary so that we can optimize object creation
                 * avoiding a large copy of data. */
                // 如果我們要從網絡中讀取一個大的對象,嘗試使它可能從c-> querybuf邊界開始,以便我們可以優化對象創建,避免大量的數據副本
                // 保存未讀取的部分
                sdsrange(c->querybuf,pos,-1);
                // 重置偏移量
                pos = 0;
                // 獲取querybuf中已使用的長度
                qblen = sdslen(c->querybuf);
                /* Hint the sds library about the amount of bytes this string is
                 * going to contain. */
                // 擴展querybuf的大小
                if (qblen < (size_t)ll+2)
                    c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2-qblen);
            }
            // 保存參數的長度
            c->bulklen = ll;
        }

        /* Read bulk argument */
        // 因爲只讀了multibulklen字節的數據,讀到的數據不夠,則直接跳出循環,執行processInputBuffer()函數循環讀取
        if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
            /* Not enough data (+2 == trailing \r\n) */
            break;
        // 爲參數創建了對象
        } else {
            /* Optimization: if the buffer contains JUST our bulk element
             * instead of creating a new object by *copying* the sds we
             * just use the current sds string. */
            // 如果讀入的長度大於32k
            if (pos == 0 &&
                c->bulklen >= PROTO_MBULK_BIG_ARG &&
                (signed) sdslen(c->querybuf) == c->bulklen+2)
            {
                c->argv[c->argc++] = createObject(OBJ_STRING,c->querybuf);
                // 跳過換行
                sdsIncrLen(c->querybuf,-2); /* remove CRLF */
                /* Assume that if we saw a fat argument we'll see another one
                 * likely... */
                // 設置一個新長度
                c->querybuf = sdsnewlen(NULL,c->bulklen+2);
                sdsclear(c->querybuf);
                pos = 0;
            // 創建對象保存在client的參數列表中
            } else {
                c->argv[c->argc++] =
                    createStringObject(c->querybuf+pos,c->bulklen);
                pos += c->bulklen+2;
            }
            // 清空命令內容的長度
            c->bulklen = -1;
            // 未讀取命令參數的數量,讀取一個,該值減1
            c->multibulklen--;
        }
    }

    /* Trim to pos */
    // 刪除已經讀取的,保留未讀取的
    if (pos) sdsrange(c->querybuf,pos,-1);

    /* We're done when c->multibulk == 0 */
    // 命令的參數全部被讀取完
    if (c->multibulklen == 0) return C_OK;

    /* Still not read to process the command */
    return C_ERR;
}

我們結合一個多條批量回復進行分析。一個多條批量回復以 *<argc>\r\n 爲前綴,後跟多條不同的批量回復,其中 argc 爲這些批量回復的數量。那麼SET nmykey nmyvalue命令轉換爲Redis協議內容如下:

"*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n"

當進入processMultibulkBuffer()函數之後,如果是第一次執行該函數,那麼argv中未讀取的命令數量爲0,也就是說參數列表爲空,那麼會執行if (c->multibulklen == 0)的代碼,這裏的代碼會解析*3\r\n,將3保存到multibulklen中,表示後面的參數個數,然後根據參數個數,爲argv分配空間。

接着,執行multibulklen次while循環,每次讀一個參數,例如$3\r\nSET\r\n,也是先讀出參數長度,保存在bulklen中,然後將參數SET保存構建成對象保存到參數列表中。每次讀一個參數,multibulklen就會減1,當等於0時,就表示命令的參數全部讀取到參數列表完畢。

於是命令接收的整個過程完成。

3.2 命令回覆

命令回覆的函數,也是事件處理程序的回調函數之一。當服務器的client的回覆緩衝區有數據,那麼就會調用aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,sendReplyToClient, c)函數,將文件描述符fdAE_WRITABLE事件關聯起來,當客戶端可寫時,就會觸發事件,調用sendReplyToClient()函數,執行寫事件。我們重點看這個函數的代碼:

// 寫事件處理程序,只是發送回覆給client
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
    UNUSED(el);
    UNUSED(mask);
    // 發送完數據會刪除fd的可讀事件
    writeToClient(fd,privdata,1);
}

這個函數直接調用了writeToClient()函數,該函數源碼如下:

// 將輸出緩衝區的數據寫給client,如果client被釋放則返回C_ERR,沒被釋放則返回C_OK
int writeToClient(int fd, client *c, int handler_installed) {
    ssize_t nwritten = 0, totwritten = 0;
    size_t objlen;
    size_t objmem;
    robj *o;

    // 如果指定的client的回覆緩衝區中還有數據,則返回真,表示可以寫socket
    while(clientHasPendingReplies(c)) {
        // 固定緩衝區發送未完成
        if (c->bufpos > 0) {
            // 將緩衝區的數據寫到fd中
            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. */
            // 如果發送的數據等於buf的偏移量,表示發送完成
            if ((int)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;
            }

            // 將當前節點的值寫到fd中
            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;
            }
        }
        // 更新寫到網絡的字節數
        server.stat_net_output_bytes += totwritten;
        // 如果這次寫的總量大於NET_MAX_WRITES_PER_EVENT的限制,則會中斷本次的寫操作,將處理時間讓給其他的client,以免一個非常的回覆獨佔服務器,剩餘的數據下次繼續在寫
        // 但是,如果當服務器的內存數已經超過maxmemory,即使超過最大寫NET_MAX_WRITES_PER_EVENT的限制,也會繼續執行寫入操作,是爲了儘快寫入給客戶端
        if (totwritten > NET_MAX_WRITES_PER_EVENT &&
            (server.maxmemory == 0 ||
             zmalloc_used_memory() < server.maxmemory)) break;
    }
    // 處理寫入失敗
    if (nwritten == -1) {
        if (errno == EAGAIN) {
            nwritten = 0;
        } else {
            serverLog(LL_VERBOSE,
                "Error writing to client: %s", strerror(errno));
            freeClient(c);
            return C_ERR;
        }
    }
    // 寫入成功
    if (totwritten > 0) {
        // 如果不是主節點服務器,則更新最近和服務器交互的時間
        if (!(c->flags & CLIENT_MASTER)) c->lastinteraction = server.unixtime;
    }
    // 如果指定的client的回覆緩衝區中已經沒有數據,發送完成
    if (!clientHasPendingReplies(c)) {
        c->sentlen = 0;
        // 刪除當前client的可讀事件的監聽
        if (handler_installed) aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);

        /* Close connection after entire reply has been sent. */
        // 如果指定了寫入按成之後立即關閉的標誌,則釋放client
        if (c->flags & CLIENT_CLOSE_AFTER_REPLY) {
            freeClient(c);
            return C_ERR;
        }
    }
    return C_OK;
}

這個函數實際上是對write()函數的封裝,將靜態回覆緩衝區buf或回覆鏈表reply中的數據循環寫到文件描述符fd中。如果寫完了,則將當前客戶端的AE_WRITABLE事件刪除。

至此,命令回覆就執行完畢。

3.3 服務器連接應答函數

我們在上面的分析中,將文件事件的兩種處理程序,命令接受和命令回覆分別分析了,那麼就乾脆將剩下的服務器連接應答函數的源碼也列出來,可以根據Redis 事件處理實現源碼剖析來一起學習。

連接應答函數分兩種,分別是本地和TCP連接,但是都是對accept()函數的封裝。

#define MAX_ACCEPTS_PER_CALL 1000
// TCP連接處理程序,創建一個client的連接狀態
static void acceptCommonHandler(int fd, int flags, char *ip) {
    client *c;
    // 創建一個新的client
    if ((c = createClient(fd)) == NULL) {
        serverLog(LL_WARNING,
            "Error registering fd event for the new client: %s (fd=%d)",
            strerror(errno),fd);
        close(fd); /* May be already closed, just ignore errors */
        return;
    }
    // 如果新的client超過server規定的maxclients的限制,那麼想新client的fd寫入錯誤信息,關閉該client
    // 先創建client,在進行數量檢查,是因爲更好的寫入錯誤信息
    if (listLength(server.clients) > server.maxclients) {
        char *err = "-ERR max number of clients reached\r\n";

        /* That's a best effort error message, don't check write errors */
        if (write(c->fd,err,strlen(err)) == -1) {
            /* Nothing to do, Just to avoid the warning... */
        }
        // 更新拒接連接的個數
        server.stat_rejected_conn++;
        freeClient(c);
        return;
    }
    // 如果服務器正在以保護模式運行(默認),且沒有設置密碼,也沒有綁定指定的接口,我們就不接受非迴環接口的請求。相反,如果需要,我們會嘗試解釋用戶如何解決問題
    if (server.protected_mode &&
        server.bindaddr_count == 0 &&
        server.requirepass == NULL &&
        !(flags & CLIENT_UNIX_SOCKET) &&
        ip != NULL)
    {
        if (strcmp(ip,"127.0.0.1") && strcmp(ip,"::1")) {
            char *err =
                "-DENIED Redis is running in protected mode because protected "
                //太長省略。。。
                "the server to start accepting connections from the outside.\r\n";
            if (write(c->fd,err,strlen(err)) == -1) {
                /* Nothing to do, Just to avoid the warning... */
            }
            // 更新拒接連接的個數
            server.stat_rejected_conn++;
            freeClient(c);
            return;
        }
    }

    // 更新連接的數量
    server.stat_numconnections++;
    // 更新client狀態的標誌
    c->flags |= flags;
}

// 創建一個TCP的連接處理程序
void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    int cport, cfd, max = MAX_ACCEPTS_PER_CALL; //最大一個處理1000次連接
    char cip[NET_IP_STR_LEN];
    UNUSED(el);
    UNUSED(mask);
    UNUSED(privdata);

    while(max--) {
        // accept接受client的連接
        cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
        if (cfd == ANET_ERR) {
            if (errno != EWOULDBLOCK)
                serverLog(LL_WARNING,
                    "Accepting client connection: %s", server.neterr);
            return;
        }
        // 打印連接的日誌
        serverLog(LL_VERBOSE,"Accepted %s:%d", cip, cport);
        // 創建一個連接狀態的client
        acceptCommonHandler(cfd,0,cip);
    }
}

// 創建一個本地連接處理程序
void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    int cfd, max = MAX_ACCEPTS_PER_CALL;
    UNUSED(el);
    UNUSED(mask);
    UNUSED(privdata);

    while(max--) {
        // accept接受client的連接
        cfd = anetUnixAccept(server.neterr, fd);
        if (cfd == ANET_ERR) {
            if (errno != EWOULDBLOCK)
                serverLog(LL_WARNING,
                    "Accepting client connection: %s", server.neterr);
            return;
        }
        serverLog(LL_VERBOSE,"Accepted connection to %s", server.unixsocket);
        // 創建一個本地連接狀態的client
        acceptCommonHandler(cfd,CLIENT_UNIX_SOCKET,NULL);
    }
}

4. Redis通信協議分析

redis 網絡鏈接庫的源碼詳細註釋

4.1 協議的目標:

  • 易於實現
  • 可以高效地被計算機分析(parse)
  • 可以很容易地被人類讀懂

4.2 協議的一般形式

*<參數數量> CR LF
$<參數 1 的字節數量> CR LF
<參數 1 的數據> CR LF
...
$<參數 N 的字節數量> CR LF
<參數 N 的數據> CR LF
//命令本身會被當做一個參數來發送

之前在命令接收我們已經分析過協議了,這了就不在仔細分析了。

4.3 回覆的類型

Redis 命令會返回多種不同類型的回覆。

通過檢查服務器發回數據的第一個字節,可以確定這個回覆是什麼類型:

  • 狀態回覆(status reply)的第一個字節是 "+"
  • 錯誤回覆(error reply)的第一個字節是 "-"
  • 整數回覆(integer reply)的第一個字節是 ":"
  • 批量回復(bulk reply)的第一個字節是 "$"
  • 多條批量回復(multi bulk reply)的第一個字節是 "*"

我們用Telnet連接服務器,來看看這些回覆的類型:

➜  ~ telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
GET key                     //發送 GET key 命令
$5                         //批量回復類型
value
EXISTS key                  //發送 EXISTS key 命令
:1                          //整數回覆類型
SS                          //發送 SS 命令
-ERR unknown command 'SS'   //錯誤回覆類型
SET key hello               //發送 SET key hello 命令
+OK                         //狀態回覆類型
SMEMBERS set                //發送 SMEMBERS set 命令
*2                          //多條批量回復類型
$2
m1
$2
m2

5. CLIENT 命令的實現

關於CLIENT的命令,Redis 3.2.8一共有6條,分別是:redis 網絡鏈接庫的源碼詳細註釋

CLIENT KILL [ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [ADDR ip:port] [SKIPME yes/no] 
CLIENT GETNAME
CLIENT LIST
CLIENT PAUSE timeout 
CLIENT REPLY ON|OFF|SKIP 
CLIENT SETNAME connection-name 

直接結合源碼和操作查看實現吧。CLIENT 命令的實現的源碼如下:

// client 命令的實現
void clientCommand(client *c) {
    listNode *ln;
    listIter li;
    client *client;

    //  CLIENT LIST 的實現
    if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
        /* CLIENT LIST */
        // 獲取所有的client信息
        sds o = getAllClientsInfoString();
        // 添加到到輸入緩衝區中
        addReplyBulkCBuffer(c,o,sdslen(o));
        sdsfree(o);
    // CLIENT REPLY ON|OFF|SKIP 命令實現
    } else if (!strcasecmp(c->argv[1]->ptr,"reply") && c->argc == 3) {
        /* CLIENT REPLY ON|OFF|SKIP */
        // 如果是 ON
        if (!strcasecmp(c->argv[2]->ptr,"on")) {
            // 取消 off 和 skip 的標誌
            c->flags &= ~(CLIENT_REPLY_SKIP|CLIENT_REPLY_OFF);
            // 回覆 +OK
            addReply(c,shared.ok);
        // 如果是 OFF
        } else if (!strcasecmp(c->argv[2]->ptr,"off")) {
            // 打開 OFF標誌
            c->flags |= CLIENT_REPLY_OFF;
        // 如果是 SKIP
        } else if (!strcasecmp(c->argv[2]->ptr,"skip")) {
            // 沒有設置 OFF 則設置 SKIP 標誌
            if (!(c->flags & CLIENT_REPLY_OFF))
                c->flags |= CLIENT_REPLY_SKIP_NEXT;
        } else {
            addReply(c,shared.syntaxerr);
            return;
        }
    //  CLIENT KILL [ip:port] [ID client-id] [TYPE normal | master | slave | pubsub] [ADDR ip:port] [SKIPME yes / no]
    } else if (!strcasecmp(c->argv[1]->ptr,"kill")) {
        /* CLIENT KILL <ip:port>
         * CLIENT KILL <option> [value] ... <option> [value] */
        char *addr = NULL;
        int type = -1;
        uint64_t id = 0;
        int skipme = 1;
        int killed = 0, close_this_client = 0;

        // CLIENT KILL addr:port只能通過地址殺死client,舊版本兼容
        if (c->argc == 3) {
            /* Old style syntax: CLIENT KILL <addr> */
            addr = c->argv[2]->ptr;
            skipme = 0; /* With the old form, you can kill yourself. */
        // 新版本可以根據[ID client-id] [master|normal|slave|pubsub] [ADDR ip:port] [SKIPME yes/no]殺死client
        } else if (c->argc > 3) {
            int i = 2; /* Next option index. */

            /* New style syntax: parse options. */
            // 解析語法
            while(i < c->argc) {
                int moreargs = c->argc > i+1;

                // CLIENT KILL [ID client-id]
                if (!strcasecmp(c->argv[i]->ptr,"id") && moreargs) {
                    long long tmp;
                    // 獲取client的ID
                    if (getLongLongFromObjectOrReply(c,c->argv[i+1],&tmp,NULL)
                        != C_OK) return;
                    id = tmp;
                // CLIENT KILL TYPE type, 這裏的 type 可以是 [master|normal|slave|pubsub]
                } else if (!strcasecmp(c->argv[i]->ptr,"type") && moreargs) {
                    // 獲取client的類型,[master|normal|slave|pubsub]四種之一
                    type = getClientTypeByName(c->argv[i+1]->ptr);
                    if (type == -1) {
                        addReplyErrorFormat(c,"Unknown client type '%s'",
                            (char*) c->argv[i+1]->ptr);
                        return;
                    }
                // CLIENT KILL [ADDR ip:port]
                } else if (!strcasecmp(c->argv[i]->ptr,"addr") && moreargs) {
                    // 獲取ip:port
                    addr = c->argv[i+1]->ptr;
                // CLIENT KILL [SKIPME yes/no]
                } else if (!strcasecmp(c->argv[i]->ptr,"skipme") && moreargs) {
                    // 如果是yes,設置設置skipme,調用該命令的客戶端將不會被殺死
                    if (!strcasecmp(c->argv[i+1]->ptr,"yes")) {
                        skipme = 1;
                    // 設置爲no會影響到還會殺死調用該命令的客戶端。
                    } else if (!strcasecmp(c->argv[i+1]->ptr,"no")) {
                        skipme = 0;
                    } else {
                        addReply(c,shared.syntaxerr);
                        return;
                    }
                } else {
                    addReply(c,shared.syntaxerr);
                    return;
                }
                i += 2;
            }
        } else {
            addReply(c,shared.syntaxerr);
            return;
        }

        /* Iterate clients killing all the matching clients. */
        listRewind(server.clients,&li);
        // 迭代所有的client節點
        while ((ln = listNext(&li)) != NULL) {
            client = listNodeValue(ln);
            // 比較當前client和這四類信息,如果有一個不符合就跳過本層循環,否則就比較下一個信息
            if (addr && strcmp(getClientPeerId(client),addr) != 0) continue;
            if (type != -1 && getClientType(client) != type) continue;
            if (id != 0 && client->id != id) continue;
            if (c == client && skipme) continue;

            /* Kill it. */
            // 殺死當前的client
            if (c == client) {
                close_this_client = 1;
            } else {
                freeClient(client);
            }
            // 計算殺死client的個數
            killed++;
        }

        /* Reply according to old/new format. */
        // 回覆client信息
        if (c->argc == 3) {
            // 沒找到符合信息的
            if (killed == 0)
                addReplyError(c,"No such client");
            else
                addReply(c,shared.ok);
        } else {
            // 發送殺死的個數
            addReplyLongLong(c,killed);
        }

        /* If this client has to be closed, flag it as CLOSE_AFTER_REPLY
         * only after we queued the reply to its output buffers. */
        if (close_this_client) c->flags |= CLIENT_CLOSE_AFTER_REPLY;
    //  CLIENT SETNAME connection-name
    } else if (!strcasecmp(c->argv[1]->ptr,"setname") && c->argc == 3) {
        int j, len = sdslen(c->argv[2]->ptr);
        char *p = c->argv[2]->ptr;

        /* Setting the client name to an empty string actually removes
         * the current name. */
        // 設置名字爲空
        if (len == 0) {
            // 先釋放掉原來的名字
            if (c->name) decrRefCount(c->name);
            c->name = NULL;
            addReply(c,shared.ok);
            return;
        }

        /* Otherwise check if the charset is ok. We need to do this otherwise
         * CLIENT LIST format will break. You should always be able to
         * split by space to get the different fields. */
        // 檢查名字格式是否正確
        for (j = 0; j < len; j++) {
            if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */
                addReplyError(c,
                    "Client names cannot contain spaces, "
                    "newlines or special characters.");
                return;
            }
        }
        // 釋放原來的名字
        if (c->name) decrRefCount(c->name);
        // 設置新名字
        c->name = c->argv[2];
        incrRefCount(c->name);
        addReply(c,shared.ok);
    //  CLIENT GETNAME
    } else if (!strcasecmp(c->argv[1]->ptr,"getname") && c->argc == 2) {
        // 回覆名字
        if (c->name)
            addReplyBulk(c,c->name);
        else
            addReply(c,shared.nullbulk);
    //  CLIENT PAUSE timeout
    } else if (!strcasecmp(c->argv[1]->ptr,"pause") && c->argc == 3) {
        long long duration;

        // 以毫秒爲單位將等待時間保存在duration中
        if (getTimeoutFromObjectOrReply(c,c->argv[2],&duration,UNIT_MILLISECONDS)
                                        != C_OK) return;
        // 暫停client
        pauseClients(duration);
        addReply(c,shared.ok);
    } else {
        addReplyError(c, "Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章