zoukankan      html  css  js  c++  java
  • Redis 慢查询日志

    慢查询日志是 Redis 提供的一个用于观察系统性能的功能, 这个功能的实现非常简单, 这里我们也简单地讲解一下。

    Redis的慢查询日志用来记录执行时间超过给定时长的命令请求,用户可以通过这个功能生产的日志来监视和优化查询速度。

    服务器配置有两个慢查询日志相关的选项:

    • slowlog-log-slower-than:规定执行时间超过多少微秒的命令会被记录到日志上。
    • slowlog-max-len:执行服务器上可以保存多少条慢查询日志。

    本章先介绍和慢查询功能相关的数据结构和变量, 然后介绍 Redis 是如何记录命令的执行时间, 以及如何为执行超过限制事件的命令记录慢查询日志的。

    慢日志示例:

    一、慢查询记录的保存

    1.1 相关数据结构

    每条慢查询日志都以一个 slowlog.h/slowlogEntry 结构定义:

    typedef struct slowlogEntry {
    
        // 命令参数
        robj **argv;
    
        // 命令参数数量
        int argc;
    
        // 唯一标识符
        long long id;       /* Unique entry identifier. */
    
        // 执行命令消耗的时间,以纳秒(1 / 1,000,000,000 秒)为单位
        long long duration; /* Time spent by the query, in nanoseconds. */
    
        // 命令执行时的时间
        time_t time;        /* Unix time at which the query was executed. */
    
    } slowlogEntry;
    

    记录服务器状态的 redis.h/redisServer 结构里保存了几个和慢查询有关的属性:

    struct redisServer {
    
        // ... other fields
    
        // 保存慢查询日志的链表
        list *slowlog;                  /* SLOWLOG list of commands */
    
        // 慢查询日志的当前 id 值
        long long slowlog_entry_id;     /* SLOWLOG current entry ID */
    
        // 慢查询时间限制
        long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
    
        // 慢查询日志的最大条目数量
        unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */
    
        // ... other fields
    };
    

    slowlog 属性是一个链表, 链表里的每个节点保存了一个慢查询日志结构, 所有日志按添加时间从新到旧排序,新的日志在链表的左端,旧的日志在链表的右端。

    slowlog_entry_id 在创建每条新的慢查询日志时增一,用于产生慢查询日志的 ID (这个 ID 在执行 SLOWLOG RESET 之后会被重置)。

    slowlog_log_slower_than 是用户指定的命令执行时间上限,执行时间大于等于这个值的命令会被慢查询日志记录。

    slowlog_max_len 慢查询日志的最大数量,当日志数量等于这个值时,添加一条新日志会造成最旧的一条日志被删除。

    下图展示了一个 slowlog 属性的实例:

    1.2 慢查询日志的记录

    在每次执行命令之前, Redis 都会用一个参数记录命令执行前的时间, 在命令执行完之后, 再计算一次当前时间, 然后将两个时间值相减, 得出执行命令所耗费的时间值 duration , 并将 duration 传给 slowlogPushEntryIfNeed 函数。

    如果 duration 超过服务器设置的执行时间上限 server.slowlog_log_slower_than 的话, slowlogPushEntryIfNeed 就会创建一条新的慢查询日志, 并将它加入到慢查询日志链表里。

    可以用一段伪代码来表示这个过程:

    def execute_redis_command_with_slowlog():
    
        # 命令执行前的时间
        start = ustime()
    
        # 执行命令
        execute_command(argv, argc)
    
        # 计算命令执行所耗费的时间
        duration = ustime() - start
    
        if slowlog_is_enabled:
            slowlogPushEntryIfNeed(argv, argc, duration)
    
    
    def slowlogPushEntryIfNeed(argv, argc, duration)
    
        # 如果执行命令耗费的时间超过服务器设置命令执行时间上限
        # 那么创建一条新的 slowlog
        if duration > server.slowlog_log_slower_than:
    
            # 创建新 slowlog
            log = new slowlogEntry()
    
            # 设置各个域
            log.argv = argv
            log.argc = argc
            log.duration = duration
            log.id = server.slowlog_entry_id
            log.time = now()
    
            # 将新 slowlog 追加到日志链表末尾
            server.slowlog.append(log)
    
            # 更新服务器 slowlog
            server.slowlog_entry_id += 1
    

    二、慢查询日志的操作

    2.1 slowlog设置

    slowlog的设置有两种方式:

    2.1.1 redis.conf设置

    在redis.conf中有关于slowlog的设置:

    slowlog-log-slower-than 10000
    slowlog-max-len 128

    其中slowlog-log-slower-than表示slowlog的划定界限,只有query执行时间大于slowlog-log-slower-than的才会定义成慢查询,才会被slowlog进行记录。slowlog-log-slower-than设置的单位是微妙,默认是10000微妙,也就是10ms 
    slowlog-max-len表示慢查询最大的条数,当slowlog超过设定的最大值后,会将最早的slowlog删除,是个FIFO队列

    2.1.2 使用config方式动态设置slowlog

    如下,可以通过config方式动态设置slowlog

        - 查看当前slowlog-log-slower-than设置
        127.0.0.1:6379> CONFIG GET slowlog-log-slower-than
        1) "slowlog-log-slower-than"
        2) "10000"
        - 设置slowlog-log-slower-than为100ms
        127.0.0.1:6379> CONFIG SET slowlog-log-slower-than 100000
        OK
        - 设置slowlog-max-len为1000
        127.0.0.1:6379> CONFIG SET slowlog-max-len 1000
        OK

    2.2 slowlog查看

    2.2.1 查看slowlog总条数

     示例:

    10.200.140.20:36379> slowlog len
    (integer) 18
    10.200.140.20:36379>

    2.2.2 查看slowlog

    127.0.0.1:6379> SLOWLOG GET
    1) 1) (integer) 25
       2) (integer) 1440057769
       3) (integer) 6
       4) 1) "SLOWLOG"
          2) "LEN"
    2) 1) (integer) 24
       2) (integer) 1440057756
       3) (integer) 36
       4) 1) "CONFIG"
          2) "GET"
          3) "slowlog-log-slower-than"
    3) 1) (integer) 23
       2) (integer) 1440057752
       3) (integer) 11
       4) 1) "CONFIG"
          2) "SET"
          3) "slowlog-log-slower-than"
          4) "1"
    4) 1) (integer) 22
       2) (integer) 1440057493
       3) (integer) 27
       4) 1) "CONFIG"
          2) "GET"
          3) "slowlog-log-slower-than"
    5) 1) (integer) 21
       2) (integer) 1440057133
       3) (integer) 7
       4) 1) "monitor"
    如果要获取指定的条数可以使用SLOWLOG GET N命令

    127.0.0.1:6379> SLOWLOG GET 1
    1) 1) (integer) 26            // slowlog唯一编号id
       2) (integer) 1440057815    // 查询的时间戳
       3) (integer) 47            // 查询的耗时(微妙),如表示本条命令查询耗时47微秒
       4) 1) "SLOWLOG"            // 查询命令,完整命令为 SLOWLOG GET,slowlog最多保存前面的31个key和128字符
          2) "GET"

    2.3 slowlog清空

     命令:slowlog reset

    10.200.140.20:36379> slowlog reset
    OK
    10.200.140.20:36379> slowlog get 1
    (empty list or set)
    10.200.140.20:36379>

    2.4 获取slowlog数量

    命令:slowlog len

    10.200.140.20:36379> slowlog len
    (integer) 18
    • Redis 用一个链表以 FIFO 的顺序保存着所有慢查询日志。
    • 每条慢查询日志以一个慢查询节点表示,节点中记录着执行超时的命令、命令的参数、命令执行时的时间,以及执行命令所消耗的时间等信息。

    转自:

    https://redisbook.readthedocs.io/en/latest/feature/slowlog.html

    https://blog.csdn.net/qq_36263268/article/details/111504650

  • 相关阅读:
    北航OO第三单元总结
    北航OO第二单元总结
    提问回顾和个人总结
    Unity 制作不规则形状button
    Unity 3D手游对不同分辨率屏幕的UI自适应
    软工结队作业
    CSDN app分析
    软工作业——求交点
    软工第一次作业
    软工热身作业
  • 原文地址:https://www.cnblogs.com/duanxz/p/14748386.html
Copyright © 2011-2022 走看看