久久青草精品A片狠狠,日韩欧美视频一区二区,亚洲国码AV日韩,国产精品黄在

Redis的慢查詢日志

2018-06-04 17:25:54 12567

慢查詢日志

記錄最新的N條執行時間超過M毫秒的命令。慢查詢日志保存在內存中,而不是文件中,這保證了慢查詢日志的效率。

1.慢查詢日志的條目定義

/* This structure defines an entry inside the slow log list */
/*
 * 慢查詢日志
 */
typedef struct slowlogEntry {
 
    // 命令與命令參數
    robj **argv;
 
    // 命令與命令參數的數量
    int argc;
 
    // 唯一標識符
    long long id;       /* Unique entry identifier. */
 
    // 執行命令消耗的時間,以微秒為單位
    // 注釋里說的 nanoseconds 是錯誤的
    long long duration; /* Time spent by the query, in nanoseconds. */
 
    // 命令執行時的時間,格式為 UNIX 時間戳
    time_t time;        /* Unix time at which the query was executed. */
 
} slowlogEntry;

2.服務器和慢查詢有關的定義

/* slowlog */
 
    // 保存了所有慢查詢日志的鏈表
    list *slowlog;                  /* SLOWLOG list of commands */
 
    // 下一條慢查詢日志的 ID
    long long slowlog_entry_id;     /* SLOWLOG current entry ID */
 
    // 服務器配置 slowlog-log-slower-than 選項的值
    long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
 
    // 服務器配置 slowlog-max-len 選項的值
    unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */

服務器的慢查詢存儲在一個list中,list中的每一項都是一條慢查詢日志,較新的日志總是保存在隊首。慢查詢日志中保存命令的執行參數和執行時間,如果超出系統限制,參數和日志可能被截斷。

 

3.慢查詢支持的客戶端操作

GET:獲取某條或者全部慢查詢日志

RESET:清空慢查詢日志

LEN:慢查詢日志的數量

4.慢查詢日志的應用

redis每執行一條命令,就會記錄命令的開始時間和結束時間,由此計算命令的執行時間。并發命令以及命令的執行時間傳遞給slowlogPushEntryIfNeeded,由slowlogPushEntryIfNeeded決定是否生成慢查詢日志。

/* Call() is the core of Redis execution of a command */
// 調用命令的實現函數,執行命令
void call(redisClient *c, int flags)
{
    //獲取命令的執行時間
    /* Log the command into the Slow log if needed, and populate the
     * per-command statistics that we show in INFO commandstats. */
    // 如果有需要,將命令放到 SLOWLOG 里面
    if (flags & REDIS_CALL_SLOWLOG && c->cmd->proc != execCommand)
        slowlogPushEntryIfNeeded(c->argv,c->argc,duration);
}
 
 
5.slowlogPushEntryIfNeeded的實現
判斷系統標志位,并把慢查詢日志加入到服務器的慢查詢鏈表中
/* Push a new entry into the slow log.
 *
 * 如果參數 duration 超過服務器設置的上限時間,
 * 那么將一個新條目以 FIFO 順序推入到慢查詢日志中。
 *
 * This function will make sure to trim the slow log accordingly to the
 * configured max length.
 *
 * 根據服務器設置的最大日志長度,可能會對日志進行截斷(trim)
 */
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {
 
    // 慢查詢功能未開啟,直接返回
    if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
 
    // 如果執行時間超過服務器設置的上限,那么將命令添加到慢查詢日志
    if (duration >= server.slowlog_log_slower_than)
        // 新日志添加到鏈表表頭
        listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
 
    /* Remove old entries if needed. */
    // 如果日志數量過多,那么進行刪除
    while (listLength(server.slowlog) > server.slowlog_max_len)
        listDelNode(server.slowlog,listLast(server.slowlog));
}


提交成功!非常感謝您的反饋,我們會繼續努力做到更好!

這條文檔是否有幫助解決問題?

非常抱歉未能幫助到您。為了給您提供更好的服務,我們很需要您進一步的反饋信息:

在文檔使用中是否遇到以下問題: