Redis源码阅读【8-命令处理生命周期-3】

Redis源码阅读【1-简单动态字符串】
Redis源码阅读【2-跳跃表】
Redis源码阅读【3-Redis编译与GDB调试】
Redis源码阅读【4-压缩列表】
Redis源码阅读【5-字典】
Redis源码阅读【6-整数集合】
Redis源码阅读【7-quicklist】
Redis源码阅读【8-命令处理生命周期-1】
Redis源码阅读【8-命令处理生命周期-2】
Redis源码阅读【8-命令处理生命周期-3】
Redis源码阅读【8-命令处理生命周期-4】
Redis源码阅读【番外篇-Redis的多线程】
Redis源码阅读【9-持久化】
建议搭配源码阅读:源码地址

文章目录

  • 1、介绍
  • 2、server初始化
    • 2.1、初始化配置
    • 2.2、加载并解析配置文件
    • 2.3、初始化服务器内部变量
    • 2.4、创建事件循环eventLoop
  • 3、监听端口
    • 3.1、创建socket并启动监听
    • 3.2、创建文件事件与时间事件
    • 3.3、开启事件循环
      • 3.3.1、过期键删除
      • 3.3.2、创建client
  • 4、总结

1、介绍

  • 上一节我们讲述了客户端,服务端,事件处理等基础知识,下面开启学习Redis服务器的启动过程,这里主要分为server的初始化监听端口以及等待命令3阶段。

2、server初始化

服务器初始化主流程可以分为7个步骤:

  • 【1】、初始化配置,包括用户可配置的参数,以及命令表的初始化;
  • 【2】、加载并解析配置文件;
  • 【3】、初始化服务端内部变量;
  • 【4】、创建事件循环aeEventLoop;
  • 【5】、创建socket并启动监听;
  • 【6】、创建文件事件与时间事件;
  • 【7】、开启循环;

整体流程如下图:
Redis源码阅读【8-命令处理生命周期-3】_第1张图片

2.1、初始化配置

步骤初始化配置,由函数initServerConfig实现,具体的操作内容就是给配置参数赋值,代码如下:

void initServerConfig(void) {
     
    int j;
    //初始化缓存时间
    updateCachedTime(1);
    getRandomHexChars(server.runid,CONFIG_RUN_ID_SIZE);
    server.runid[CONFIG_RUN_ID_SIZE] = '\0';
    changeReplicationId();
    clearReplicationId2();
    server.timezone = getTimeZone(); /* Initialized by tzset(). */
    server.configfile = NULL;
    server.executable = NULL;
    server.arch_bits = (sizeof(long) == 8) ? 64 : 32;
    server.bindaddr_count = 0;
    server.unixsocketperm = CONFIG_DEFAULT_UNIX_SOCKET_PERM;
    server.ipfd_count = 0;
    server.tlsfd_count = 0;
    server.sofd = -1;
    server.active_expire_enabled = 1;
    server.client_max_querybuf_len = PROTO_MAX_QUERYBUF_LEN;
    server.saveparams = NULL;
    server.loading = 0;
    server.logfile = zstrdup(CONFIG_DEFAULT_LOGFILE);
    //AOF相关配置
    server.aof_state = AOF_OFF;
    server.aof_rewrite_base_size = 0;
    server.aof_rewrite_scheduled = 0;
    server.aof_flush_sleep = 0;
    server.aof_last_fsync = time(NULL);
    server.aof_rewrite_time_last = -1;
    server.aof_rewrite_time_start = -1;
    server.aof_lastbgrewrite_status = C_OK;
    server.aof_delayed_fsync = 0;
    server.aof_fd = -1;
    server.aof_selected_db = -1; /* Make sure the first time will not match */
    server.aof_flush_postponed_start = 0;
    server.pidfile = NULL;
    server.active_defrag_running = 0;
    server.notify_keyspace_events = 0;
    server.blocked_clients = 0;
    memset(server.blocked_clients_by_type,0,
           sizeof(server.blocked_clients_by_type));
    server.shutdown_asap = 0;
    server.cluster_configfile = zstrdup(CONFIG_DEFAULT_CLUSTER_CONFIG_FILE);
    server.cluster_module_flags = CLUSTER_MODULE_FLAG_NONE;
    server.migrate_cached_sockets = dictCreate(&migrateCacheDictType,NULL);
    server.next_client_id = 1; /* Client IDs, start from 1 .*/
    server.loading_process_events_interval_bytes = (1024*1024*2);
    //获取LRU的时间戳
    server.lruclock = getLRUClock();
    resetServerSaveParams();

    appendServerSaveParams(60*60,1);  /* save after 1 hour and 1 change */
    appendServerSaveParams(300,100);  /* save after 5 minutes and 100 changes */
    appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */

    /* Replication related */
    //主从同步配置
    server.masterauth = NULL;
    server.masterhost = NULL;
    server.masterport = 6379;
    server.master = NULL;
    server.cached_master = NULL;
    server.master_initial_offset = -1;
    server.repl_state = REPL_STATE_NONE;
    server.repl_transfer_tmpfile = NULL;
    server.repl_transfer_fd = -1;
    server.repl_transfer_s = NULL;
    server.repl_syncio_timeout = CONFIG_REPL_SYNCIO_TIMEOUT;
    server.repl_down_since = 0; /* Never connected, repl is down since EVER. */
    server.master_repl_offset = 0;
    /* Replication partial resync backlog */
    server.repl_backlog = NULL;
    server.repl_backlog_histlen = 0;
    server.repl_backlog_idx = 0;
    server.repl_backlog_off = 0;
    server.repl_no_slaves_since = time(NULL);

    /* Client output buffer limits */
    //客户端响应缓冲区空间
    for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++)
        server.client_obuf_limits[j] = clientBufferLimitsDefaults[j];

    /* Double constants initialization */
    R_Zero = 0.0;
    R_PosInf = 1.0/R_Zero;
    R_NegInf = -1.0/R_Zero;
    R_Nan = R_Zero/R_Zero;

    /* Command table -- we initiialize it here as it is part of the
     * initial configuration, since command names may be changed via
     * redis.conf using the rename-command directive. */
    //创建命令字典
    server.commands = dictCreate(&commandTableDictType,NULL);
    //创建兼容旧版本命令字典
    server.orig_commands = dictCreate(&commandTableDictType,NULL);
    //预热缓存常用命令
    populateCommandTable();
    server.delCommand = lookupCommandByCString("del");
    server.multiCommand = lookupCommandByCString("multi");
    server.lpushCommand = lookupCommandByCString("lpush");
    server.lpopCommand = lookupCommandByCString("lpop");
    server.rpopCommand = lookupCommandByCString("rpop");
    server.zpopminCommand = lookupCommandByCString("zpopmin");
    server.zpopmaxCommand = lookupCommandByCString("zpopmax");
    server.sremCommand = lookupCommandByCString("srem");
    server.execCommand = lookupCommandByCString("exec");
    server.expireCommand = lookupCommandByCString("expire");
    server.pexpireCommand = lookupCommandByCString("pexpire");
    server.xclaimCommand = lookupCommandByCString("xclaim");
    server.xgroupCommand = lookupCommandByCString("xgroup");

    /* Debugging */
    //debug配置
    server.assert_failed = "";
    server.assert_file = "";
    server.assert_line = 0;
    server.bug_report_start = 0;
    server.watchdog_period = 0;

    /* By default we want scripts to be always replicated by effects
     * (single commands executed by the script), and not by sending the
     * script to the slave / AOF. This is the new way starting from
     * Redis 5. However it is possible to revert it via redis.conf. */
    server.lua_always_replicate_commands = 1;
    //初始化其它默认配置
    initConfigValues();
}
  • 注意除了以上的配置最底下还有一个initConfigValues函数(定义实现在config.c文件中),里面有一个循环,在循环里面初始胡配置,其中循环的结构体是standardConfig,其代码如下:
void initConfigValues() {
     
    for (standardConfig *config = configs; config->name != NULL; config++) {
     
        config->interface.init(config->data);
    }
}

循环的standardConfig本身是一个含义默认值的数组,在config.c文件中还有一个configs数组,里面放置着各种各样的默认配置以及,对应读取的配置字段,通过上面的循环代码,一一将配置初始化,内容太多了,这里就不一个个介绍了,大家基本上都能根据配置名称判断出,配置的内容:

standardConfig configs[] = {
     
    /* Bool configs */
    createBoolConfig("rdbchecksum", NULL, IMMUTABLE_CONFIG, server.rdb_checksum, 1, NULL, NULL),
    createBoolConfig("daemonize", NULL, IMMUTABLE_CONFIG, server.daemonize, 0, NULL, NULL),
    createBoolConfig("io-threads-do-reads", NULL, IMMUTABLE_CONFIG, server.io_threads_do_reads, 0,NULL, NULL), /* Read + parse from threads? */
    createBoolConfig("lua-replicate-commands", NULL, IMMUTABLE_CONFIG, server.lua_always_replicate_commands, 1, NULL, NULL),
    createBoolConfig("always-show-logo", NULL, IMMUTABLE_CONFIG, server.always_show_logo, 0, NULL, NULL),
    createBoolConfig("protected-mode", NULL, MODIFIABLE_CONFIG, server.protected_mode, 1, NULL, NULL),
    createBoolConfig("rdbcompression", NULL, MODIFIABLE_CONFIG, server.rdb_compression, 1, NULL, NULL),
    createBoolConfig("activerehashing", NULL, MODIFIABLE_CONFIG, server.activerehashing, 1, NULL, NULL),
    createBoolConfig("stop-writes-on-bgsave-error", NULL, MODIFIABLE_CONFIG, server.stop_writes_on_bgsave_err, 1, NULL, NULL),
    createBoolConfig("dynamic-hz", NULL, MODIFIABLE_CONFIG, server.dynamic_hz, 1, NULL, NULL), /* Adapt hz to # of clients.*/
    createBoolConfig("lazyfree-lazy-eviction", NULL, MODIFIABLE_CONFIG, server.lazyfree_lazy_eviction, 0, NULL, NULL),
    createBoolConfig("lazyfree-lazy-expire", NULL, MODIFIABLE_CONFIG, server.lazyfree_lazy_expire, 0, NULL, NULL),
    createBoolConfig("lazyfree-lazy-server-del", NULL, MODIFIABLE_CONFIG, server.lazyfree_lazy_server_del, 0, NULL, NULL),
    createBoolConfig("repl-disable-tcp-nodelay", NULL, MODIFIABLE_CONFIG, server.repl_disable_tcp_nodelay, 0, NULL, NULL),
    createBoolConfig("repl-diskless-sync", NULL, MODIFIABLE_CONFIG, server.repl_diskless_sync, 0, NULL, NULL),
    createBoolConfig("gopher-enabled", NULL, MODIFIABLE_CONFIG, server.gopher_enabled, 0, NULL, NULL),
    createBoolConfig("aof-rewrite-incremental-fsync", NULL, MODIFIABLE_CONFIG, server.aof_rewrite_incremental_fsync, 1, NULL, NULL),
    createBoolConfig("no-appendfsync-on-rewrite", NULL, MODIFIABLE_CONFIG, server.aof_no_fsync_on_rewrite, 0, NULL, NULL),
    createBoolConfig("cluster-require-full-coverage", NULL, MODIFIABLE_CONFIG, server.cluster_require_full_coverage, 1, NULL, NULL),
    createBoolConfig("rdb-save-incremental-fsync", NULL, MODIFIABLE_CONFIG, server.rdb_save_incremental_fsync, 1, NULL, NULL),
    createBoolConfig("aof-load-truncated", NULL, MODIFIABLE_CONFIG, server.aof_load_truncated, 1, NULL, NULL),
    createBoolConfig("aof-use-rdb-preamble", NULL, MODIFIABLE_CONFIG, server.aof_use_rdb_preamble, 1, NULL, NULL),
    createBoolConfig("cluster-replica-no-failover", "cluster-slave-no-failover", MODIFIABLE_CONFIG, server.cluster_slave_no_failover, 0, NULL, NULL), /* Failover by default. */
    createBoolConfig("replica-lazy-flush", "slave-lazy-flush", MODIFIABLE_CONFIG, server.repl_slave_lazy_flush, 0, NULL, NULL),
    createBoolConfig("replica-serve-stale-data", "slave-serve-stale-data", MODIFIABLE_CONFIG, server.repl_serve_stale_data, 1, NULL, NULL),
    createBoolConfig("replica-read-only", "slave-read-only", MODIFIABLE_CONFIG, server.repl_slave_ro, 1, NULL, NULL),
    createBoolConfig("replica-ignore-maxmemory", "slave-ignore-maxmemory", MODIFIABLE_CONFIG, server.repl_slave_ignore_maxmemory, 1, NULL, NULL),
    createBoolConfig("jemalloc-bg-thread", NULL, MODIFIABLE_CONFIG, server.jemalloc_bg_thread, 1, NULL, updateJemallocBgThread),
    createBoolConfig("activedefrag", NULL, MODIFIABLE_CONFIG, server.active_defrag_enabled, 0, isValidActiveDefrag, NULL),
    createBoolConfig("syslog-enabled", NULL, IMMUTABLE_CONFIG, server.syslog_enabled, 0, NULL, NULL),
    createBoolConfig("cluster-enabled", NULL, IMMUTABLE_CONFIG, server.cluster_enabled, 0, NULL, NULL),
    createBoolConfig("appendonly", NULL, MODIFIABLE_CONFIG, server.aof_enabled, 0, NULL, updateAppendonly),

    /* String Configs */
    createStringConfig("aclfile", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.acl_filename, "", NULL, NULL),
    createStringConfig("unixsocket", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.unixsocket, NULL, NULL, NULL),
    createStringConfig("pidfile", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.pidfile, NULL, NULL, NULL),
    createStringConfig("replica-announce-ip", "slave-announce-ip", MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.slave_announce_ip, NULL, NULL, NULL),
    createStringConfig("masteruser", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.masteruser, NULL, NULL, NULL),
    createStringConfig("masterauth", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.masterauth, NULL, NULL, NULL),
    createStringConfig("cluster-announce-ip", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.cluster_announce_ip, NULL, NULL, NULL),
    createStringConfig("syslog-ident", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.syslog_ident, "redis", NULL, NULL),
    createStringConfig("dbfilename", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.rdb_filename, "dump.rdb", isValidDBfilename, NULL),
    createStringConfig("appendfilename", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.aof_filename, "appendonly.aof", isValidAOFfilename, NULL),

    /* Enum Configs */
    createEnumConfig("supervised", NULL, IMMUTABLE_CONFIG, supervised_mode_enum, server.supervised_mode, SUPERVISED_NONE, NULL, NULL),
    createEnumConfig("syslog-facility", NULL, IMMUTABLE_CONFIG, syslog_facility_enum, server.syslog_facility, LOG_LOCAL0, NULL, NULL),
    createEnumConfig("repl-diskless-load", NULL, MODIFIABLE_CONFIG, repl_diskless_load_enum, server.repl_diskless_load, REPL_DISKLESS_LOAD_DISABLED, NULL, NULL),
    createEnumConfig("loglevel", NULL, MODIFIABLE_CONFIG, loglevel_enum, server.verbosity, LL_NOTICE, NULL, NULL),
    createEnumConfig("maxmemory-policy", NULL, MODIFIABLE_CONFIG, maxmemory_policy_enum, server.maxmemory_policy, MAXMEMORY_NO_EVICTION, NULL, NULL),
    createEnumConfig("appendfsync", NULL, MODIFIABLE_CONFIG, aof_fsync_enum, server.aof_fsync, AOF_FSYNC_EVERYSEC, NULL, NULL),

    /* Integer configs */
    createIntConfig("databases", NULL, IMMUTABLE_CONFIG, 1, INT_MAX, server.dbnum, 16, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("port", NULL, IMMUTABLE_CONFIG, 0, 65535, server.port, 6379, INTEGER_CONFIG, NULL, NULL), /* TCP port. */
    createIntConfig("io-threads", NULL, IMMUTABLE_CONFIG, 1, 512, server.io_threads_num, 1, INTEGER_CONFIG, NULL, NULL), /* Single threaded by default */
    createIntConfig("auto-aof-rewrite-percentage", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.aof_rewrite_perc, 100, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("cluster-replica-validity-factor", "cluster-slave-validity-factor", MODIFIABLE_CONFIG, 0, INT_MAX, server.cluster_slave_validity_factor, 10, INTEGER_CONFIG, NULL, NULL), /* Slave max data age factor. */
    createIntConfig("list-max-ziplist-size", NULL, MODIFIABLE_CONFIG, INT_MIN, INT_MAX, server.list_max_ziplist_size, -2, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("tcp-keepalive", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.tcpkeepalive, 300, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("cluster-migration-barrier", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.cluster_migration_barrier, 1, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("active-defrag-cycle-min", NULL, MODIFIABLE_CONFIG, 1, 99, server.active_defrag_cycle_min, 1, INTEGER_CONFIG, NULL, NULL), /* Default: 1% CPU min (at lower threshold) */
    createIntConfig("active-defrag-cycle-max", NULL, MODIFIABLE_CONFIG, 1, 99, server.active_defrag_cycle_max, 25, INTEGER_CONFIG, NULL, NULL), /* Default: 25% CPU max (at upper threshold) */
    createIntConfig("active-defrag-threshold-lower", NULL, MODIFIABLE_CONFIG, 0, 1000, server.active_defrag_threshold_lower, 10, INTEGER_CONFIG, NULL, NULL), /* Default: don't defrag when fragmentation is below 10% */
    createIntConfig("active-defrag-threshold-upper", NULL, MODIFIABLE_CONFIG, 0, 1000, server.active_defrag_threshold_upper, 100, INTEGER_CONFIG, NULL, NULL), /* Default: maximum defrag force at 100% fragmentation */
    createIntConfig("lfu-log-factor", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.lfu_log_factor, 10, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("lfu-decay-time", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.lfu_decay_time, 1, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("replica-priority", "slave-priority", MODIFIABLE_CONFIG, 0, INT_MAX, server.slave_priority, 100, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("repl-diskless-sync-delay", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.repl_diskless_sync_delay, 5, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("maxmemory-samples", NULL, MODIFIABLE_CONFIG, 1, INT_MAX, server.maxmemory_samples, 5, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("timeout", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.maxidletime, 0, INTEGER_CONFIG, NULL, NULL), /* Default client timeout: infinite */
    createIntConfig("replica-announce-port", "slave-announce-port", MODIFIABLE_CONFIG, 0, 65535, server.slave_announce_port, 0, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("tcp-backlog", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.tcp_backlog, 511, INTEGER_CONFIG, NULL, NULL), /* TCP listen backlog. */
    createIntConfig("cluster-announce-bus-port", NULL, MODIFIABLE_CONFIG, 0, 65535, server.cluster_announce_bus_port, 0, INTEGER_CONFIG, NULL, NULL), /* Default: Use +10000 offset. */
    createIntConfig("cluster-announce-port", NULL, MODIFIABLE_CONFIG, 0, 65535, server.cluster_announce_port, 0, INTEGER_CONFIG, NULL, NULL), /* Use server.port */
    createIntConfig("repl-timeout", NULL, MODIFIABLE_CONFIG, 1, INT_MAX, server.repl_timeout, 60, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("repl-ping-replica-period", "repl-ping-slave-period", MODIFIABLE_CONFIG, 1, INT_MAX, server.repl_ping_slave_period, 10, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("list-compress-depth", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.list_compress_depth, 0, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("rdb-key-save-delay", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.rdb_key_save_delay, 0, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("key-load-delay", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.key_load_delay, 0, INTEGER_CONFIG, NULL, NULL),
    createIntConfig("tracking-table-max-fill", NULL, MODIFIABLE_CONFIG, 0, 100, server.tracking_table_max_fill, 10, INTEGER_CONFIG, NULL, NULL), /* Default: 10% tracking table max fill. */
    createIntConfig("active-expire-effort", NULL, MODIFIABLE_CONFIG, 1, 10, server.active_expire_effort, 1, INTEGER_CONFIG, NULL, NULL), /* From 1 to 10. */
    createIntConfig("hz", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.config_hz, CONFIG_DEFAULT_HZ, INTEGER_CONFIG, NULL, updateHZ),
    createIntConfig("min-replicas-to-write", "min-slaves-to-write", MODIFIABLE_CONFIG, 0, INT_MAX, server.repl_min_slaves_to_write, 0, INTEGER_CONFIG, NULL, updateGoodSlaves),
    createIntConfig("min-replicas-max-lag", "min-slaves-max-lag", MODIFIABLE_CONFIG, 0, INT_MAX, server.repl_min_slaves_max_lag, 10, INTEGER_CONFIG, NULL, updateGoodSlaves),

    /* Unsigned int configs */
    createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, server.maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients),

    /* Unsigned Long configs */
    createULongConfig("active-defrag-max-scan-fields", NULL, MODIFIABLE_CONFIG, 1, LONG_MAX, server.active_defrag_max_scan_fields, 1000, INTEGER_CONFIG, NULL, NULL), /* Default: keys with more than 1000 fields will be processed separately */
    createULongConfig("slowlog-max-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.slowlog_max_len, 128, INTEGER_CONFIG, NULL, NULL),

    /* Long Long configs */
    createLongLongConfig("lua-time-limit", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.lua_time_limit, 5000, INTEGER_CONFIG, NULL, NULL),/* milliseconds */
    createLongLongConfig("cluster-node-timeout", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, server.cluster_node_timeout, 15000, INTEGER_CONFIG, NULL, NULL),
    createLongLongConfig("slowlog-log-slower-than", NULL, MODIFIABLE_CONFIG, -1, LLONG_MAX, server.slowlog_log_slower_than, 10000, INTEGER_CONFIG, NULL, NULL),
    createLongLongConfig("latency-monitor-threshold", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, server.latency_monitor_threshold, 0, INTEGER_CONFIG, NULL, NULL),
    createLongLongConfig("proto-max-bulk-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.proto_max_bulk_len, 512ll*1024*1024, MEMORY_CONFIG, NULL, NULL), /* Bulk request max size */
    createLongLongConfig("stream-node-max-entries", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.stream_node_max_entries, 100, INTEGER_CONFIG, NULL, NULL),
    createLongLongConfig("repl-backlog-size", NULL, MODIFIABLE_CONFIG, 1, LONG_MAX, server.repl_backlog_size, 1024*1024, MEMORY_CONFIG, NULL, updateReplBacklogSize), /* Default: 1mb */

    /* Unsigned Long Long configs */
    createULongLongConfig("maxmemory", NULL, MODIFIABLE_CONFIG, 0, ULLONG_MAX, server.maxmemory, 0, MEMORY_CONFIG, NULL, updateMaxmemory),

    /* Size_t configs */
    createSizeTConfig("hash-max-ziplist-entries", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.hash_max_ziplist_entries, 512, INTEGER_CONFIG, NULL, NULL),
    createSizeTConfig("set-max-intset-entries", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.set_max_intset_entries, 512, INTEGER_CONFIG, NULL, NULL),
    createSizeTConfig("zset-max-ziplist-entries", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.zset_max_ziplist_entries, 128, INTEGER_CONFIG, NULL, NULL),
    createSizeTConfig("active-defrag-ignore-bytes", NULL, MODIFIABLE_CONFIG, 1, LONG_MAX, server.active_defrag_ignore_bytes, 100<<20, MEMORY_CONFIG, NULL, NULL), /* Default: don't defrag if frag overhead is below 100mb */
    createSizeTConfig("hash-max-ziplist-value", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.hash_max_ziplist_value, 64, MEMORY_CONFIG, NULL, NULL),
    createSizeTConfig("stream-node-max-bytes", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.stream_node_max_bytes, 4096, MEMORY_CONFIG, NULL, NULL),
    createSizeTConfig("zset-max-ziplist-value", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.zset_max_ziplist_value, 64, MEMORY_CONFIG, NULL, NULL),
    createSizeTConfig("hll-sparse-max-bytes", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.hll_sparse_max_bytes, 3000, MEMORY_CONFIG, NULL, NULL),

    /* Other configs */
    createTimeTConfig("repl-backlog-ttl", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.repl_backlog_time_limit, 60*60, INTEGER_CONFIG, NULL, NULL), /* Default: 1 hour */
    createOffTConfig("auto-aof-rewrite-min-size", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, server.aof_rewrite_min_size, 64*1024*1024, MEMORY_CONFIG, NULL, NULL),

#ifdef USE_OPENSSL
    createIntConfig("tls-port", NULL, IMMUTABLE_CONFIG, 0, 65535, server.tls_port, 0, INTEGER_CONFIG, NULL, NULL), /* TCP port. */
    createBoolConfig("tls-cluster", NULL, MODIFIABLE_CONFIG, server.tls_cluster, 0, NULL, NULL),
    createBoolConfig("tls-replication", NULL, MODIFIABLE_CONFIG, server.tls_replication, 0, NULL, NULL),
    createBoolConfig("tls-auth-clients", NULL, MODIFIABLE_CONFIG, server.tls_auth_clients, 1, NULL, NULL),
    createBoolConfig("tls-prefer-server-ciphers", NULL, MODIFIABLE_CONFIG, server.tls_ctx_config.prefer_server_ciphers, 0, NULL, updateTlsCfgBool),
    createStringConfig("tls-cert-file", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.cert_file, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-key-file", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.key_file, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-dh-params-file", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.dh_params_file, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-ca-cert-file", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.ca_cert_file, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-ca-cert-dir", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.ca_cert_dir, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-protocols", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.protocols, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-ciphers", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.ciphers, NULL, NULL, updateTlsCfg),
    createStringConfig("tls-ciphersuites", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.tls_ctx_config.ciphersuites, NULL, NULL, updateTlsCfg),
#endif

    /* NULL Terminator */
    {
     NULL}
};

2.2、加载并解析配置文件

-当初始化完成配置后,就会进行下一个步骤:加载并解析配置文件,入口函数为loadServerConfig,函数声明如下:

void loadServerConfig(char *filename, char *options)
  • 输入参数filename表示配置文件全路径名称,options表示命令行输入的配置参数,例如我们通常以如下命令启动Redis服务器:
/home/redis/redis-server /home/redis/redis.conf -p 4000
  • Redis的配置语法是,每一行是一条配置,格式如:配置参数1【参数2】【............】,加载配置文件只需要一行一行的将文件内容读取到内存中即可。加完成后就会开始解析配置,loadServerConfig的实现如下:
void loadServerConfig(char *filename, char *options) {
     
    sds config = sdsempty();
    char buf[CONFIG_MAX_LINE+1];

    /* Load the file content */
    if (filename) {
     
        FILE *fp;
        //对文件名称校验
        if (filename[0] == '-' && filename[1] == '\0') {
     
            fp = stdin;
        } else {
     
            //打开配置件
            if ((fp = fopen(filename,"r")) == NULL) {
     
                serverLog(LL_WARNING,
                    "Fatal error, can't open config file '%s'", filename);
                exit(1);
            }
        }//依次将文件中的每行配置写入 config
        while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)
            config = sdscat(config,buf);
        if (fp != stdin) fclose(fp);
    }
    /* Append the additional options */
    if (options) {
     
        config = sdscat(config,"\n");
        config = sdscat(config,options);
    }
    //开始从读取出来的字符串解析配置
    loadServerConfigFromString(config);
    //释放
    sdsfree(config);
}
  • 当配置读取完成的时候我们会调用loadServerConfigFromString,从字符中解析相对于的配置,代码如下:
void loadServerConfigFromString(char *config) {
     
    char *err = NULL;
    int linenum = 0, totlines, i;
    int slaveof_linenum = 0;
    sds *lines;
    //将字符串分割为多行
    lines = sdssplitlen(config,strlen(config),"\n",1,&totlines);
    //一行行解析
    for (i = 0; i < totlines; i++) {
     
        sds *argv;
        int argc;
        linenum = i+1;
        lines[i] = sdstrim(lines[i]," \t\r\n");
        //跳过注释与空行
        if (lines[i][0] == '#' || lines[i][0] == '\0') continue;
        /* Split into arguments */
        //解析配置参数,来判定当前是什么配置
        argv = sdssplitargs(lines[i],&argc);
        //参数解析失败,中止初始化
        if (argv == NULL) {
     
            err = "Unbalanced quotes in configuration line";
            goto loaderr;
        }
        ...............后面都是 if-else 判断配置并初始化..............
  • 上面的代码中,函数首先将输入配置字符串以\n,为字符分隔符划分为多行,totlines记录总行数,lines数组存储分割后的配置,数组元素类型为字符串SDS;for循环遍历所有配置行,解析配置参数,并根据参数内容设置结构体server各字段。并且开头为 # 或者 \0 的字符标记本行为注释内容,直接跳过。

2.3、初始化服务器内部变量

  • 完成配置加载和解析后,需要对服务器内部的变量进行一个初始化,比如客户端链表数据库全局变量共享对象等等,入口函数为initServer,由于里面很多重复性内容,这里讲解主要内容,函数逻辑如下:
void initServer(void) {
     
................................................
    //serverCron函数执行频率,默认为10
    server.hz = server.config_hz;
    //当前线程PID
    server.pid = getpid();
  
    //初始化客户端链表
    server.clients = listCreate();

	//创建共享对象
	createSharedObjects();
	
	//创建eventLoop事件循环
 	server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR);
 ....................................................
    //分配数据库空间默认16个数据库
    server.db = zmalloc(sizeof(redisDb)*server.dbnum);

    /* Open the TCP listening socket for the user commands. */
    //监听TCP端口
    if (server.port != 0 &&
        listenToPort(server.port,server.ipfd,&server.ipfd_count) == C_ERR)
        exit(1);
    //监听TLS
    if (server.tls_port != 0 &&
        listenToPort(server.tls_port,server.tlsfd,&server.tlsfd_count) == C_ERR)
        exit(1);

    //初始化每个db
    for (j = 0; j < server.dbnum; j++) {
     
        server.db[j].dict = dictCreate(&dbDictType,NULL);
        server.db[j].expires = dictCreate(&keyptrDictType,NULL);
        server.db[j].expires_cursor = 0;
        server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].ready_keys = dictCreate(&objectKeyPointerValueDictType,NULL);
        server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].id = j;
        server.db[j].avg_ttl = 0;
        server.db[j].defrag_later = listCreate();
        //释放用的函数钩子
        listSetFreeMethod(server.db[j].defrag_later,(void (*)(void*))sdsfree);
    }
.....................................................
     //限制32位最大内存使用空间位3GB 强行设置策略为 MAXMEMORY_NO_EVICTION
    if (server.arch_bits == 32 && server.maxmemory == 0) {
     
        serverLog(LL_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.");
        server.maxmemory = 3072LL*(1024*1024); /* 3 GB */
        server.maxmemory_policy = MAXMEMORY_NO_EVICTION;
    }
.....................................................
}
  • 注意数据库字典的dictType指向的是结构体dbDictType,其定义如下:
dictType dbDictType = {
     
    dictSdsHash,               //当前字典键散列函数 /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCompare,         //当前字典键的比较函数 /* key compare */
    dictSdsDestructor,         //当前字典键析构函数 /* key destructor */
    dictObjectDestructor       //当前字典对象析构函数
};
  • 数据库键都是SDS类型,键散列函数为dictSdsHash,键比较函数为dictSdsKeyCompare,键析构函数为dictSdsDestructor;数据库对象是robj对象,值析构函数为dictObjectDestructor;键和值的内存赋值函数都为NULL

  • 除了以上内容之外,还会调用createSharedObjects函数初始化共享对象,例如前面提到的 robjrefcount 字段。执行命令时 Redis 会返回一些字符串回复,这些字符串对象同样在服务器初始化时创建,且永远不会尝试释放这类对象。共享对象都存储在全局结构体变量shared中,下面来看看shared对应的结构体:

struct sharedObjectsStruct {
     
    robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
    *colon, *queued, *null[4], *nullarray[4], *emptymap[4], *emptyset[4],
    *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
    *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr,
    *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,
    *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
    *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
    *rpop, *lpop, *lpush, *rpoplpush, *zpopmin, *zpopmax, *emptyscan,
    *select[PROTO_SHARED_SELECT_CMDS],
    *integers[OBJ_SHARED_INTEGERS],
    *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*\r\n" */
    *bulkhdr[OBJ_SHARED_BULKHDR_LEN];  /* "$\r\n" */
    sds minstring, maxstring;
};

void createSharedObjects(void) {
     
    int j;

    shared.crlf = createObject(OBJ_STRING,sdsnew("\r\n"));
    shared.ok = createObject(OBJ_STRING,sdsnew("+OK\r\n"));
    shared.err = createObject(OBJ_STRING,sdsnew("-ERR\r\n"));
    shared.emptybulk = createObject(OBJ_STRING,sdsnew("$0\r\n\r\n"));
    shared.czero = createObject(OBJ_STRING,sdsnew(":0\r\n"));
    shared.cone = createObject(OBJ_STRING,sdsnew(":1\r\n"));
    shared.emptyarray = createObject(OBJ_STRING,sdsnew("*0\r\n"));
    shared.pong = createObject(OBJ_STRING,sdsnew("+PONG\r\n"));
    shared.queued = createObject(OBJ_STRING,sdsnew("+QUEUED\r\n"));
    shared.emptyscan = createObject(OBJ_STRING,sdsnew("*2\r\n$1\r\n0\r\n*0\r\n"));
.........................................................

  • 从上面可以看出,大部分共享对象都是共享指令,且都是RESP协议格式,我们知道Redis是基于RESP协议的,通过缓存RESP的指令,方便快速响应客户端,以及减少内存开销。

2.4、创建事件循环eventLoop

  • 初始化完成内部变量后,就是创建事件循环eventLoop,即分配结构体所需内存,并且初始化结构体各字段;epoll就是在这个时候创建的,在initServer函数里面,执行完createSharedObjects创建共享对象之后,代码如下:
void initServer(void) {
     
..................................................
	//创建事件循环
	server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR);
..................................................
}

//创建eventLoop
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;
    eventLoop->aftersleep = NULL;
    eventLoop->flags = 0;
    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;
}
  • 在创建eventLoop的时候,输入参数setsize理论上等于用户配置的最大客户端数目即可,但是为了确保安全,这里设置setsize等于最大客户端数目加128。函数aeApiCreate内部调用epoll_create创建epoll,并初始化结构体eventLoop的字段apidata可以参考前面的文章关于eventLoop的创建

3、监听端口

前面已经介绍了:1、初始化配置,2、加载并解析配置文件,3、初始化服务端内部变量,4、创建事件循环aeEventLoop。当完成这些操作后,Redis将创建socket并启动监听,同时创建对应的文件事件与时间事件并开始事件循环。下面将介绍以下几个步骤:

  • 【5】、创建socket并启动监听;
  • 【6】、创建文件事件与时间事件;
  • 【7】、开启循环;

3.1、创建socket并启动监听

用户可以通过指令port配置socket的端口号,指令bind配置socket绑定IP地址;注意指令bind可配置多个IP地址,中间用空格隔开;创建socket时只需要循环所有IP地址即可。函数实现如下:

//入参分别是 端口 fds是数组指向所有socket文件描述符 count存储socket数量
int listenToPort(int port, int *fds, int *count) {
     
    int j;

    /* Force binding of 0.0.0.0 if no bind address is specified, always
     * entering the loop if j == 0. */
    //如果没有指定 IP ,设置为 NULL
    if (server.bindaddr_count == 0) server.bindaddr[0] = NULL;
    //遍历所有需要bind的 ip
    for (j = 0; j < server.bindaddr_count || j == 0; j++) {
     
        //如果为空,用户没有指定,使用默认值,即绑定IPV4 又 绑定 IPV6
        if (server.bindaddr[j] == NULL) {
     
            int unsupported = 0;        
            //创建socket并启动监听,文件描述符存储在fds数组作为返回参数(IPV6)
            fds[*count] = anetTcp6Server(server.neterr,port,NULL,
                server.tcp_backlog);
            if (fds[*count] != ANET_ERR) {
     
                //设置socket 为非阻塞
                anetNonBlock(NULL,fds[*count]);
                (*count)++;
            } else if (errno == EAFNOSUPPORT) {
     
                unsupported++;
                serverLog(LL_WARNING,"Not listening to IPv6: unsupported");
            }

            if (*count == 1 || unsupported) {
                   
                //创建socket并启动监听,文件描述符存储在fds数组作为返回参数(IPV4)
                fds[*count] = anetTcpServer(server.neterr,port,NULL,
                    server.tcp_backlog);
                if (fds[*count] != ANET_ERR) {
     
                    //设置socket 为非阻塞
                    anetNonBlock(NULL,fds[*count]);
                    (*count)++;
                } else if (errno == EAFNOSUPPORT) {
     
                    unsupported++;
                    serverLog(LL_WARNING,"Not listening to IPv4: unsupported");
                }
            }
                 
            if (*count + unsupported == 2) break;
        } else if (strchr(server.bindaddr[j],':')) {
     
            //IP里面有带 : 肯定是IPV6
            //创建socket并启动监听,文件描述符存储在fds数组作为返回参数(IPV6)
            fds[*count] = anetTcp6Server(server.neterr,port,server.bindaddr[j],
                server.tcp_backlog);
        } else {
     
            //如果没有 : 而且还有 IP 那就是 IPV4
            /* Bind IPv4 address. */
            //创建socket并启动监听,文件描述符存储在fds数组作为返回参数(IPV4)
            fds[*count] = anetTcpServer(server.neterr,port,server.bindaddr[j],
                server.tcp_backlog);
        }
        if (fds[*count] == ANET_ERR) {
     
            serverLog(LL_WARNING,
                "Could not create server TCP listening socket %s:%d: %s",
                server.bindaddr[j] ? server.bindaddr[j] : "*",
                port, server.neterr);
                if (errno == ENOPROTOOPT     || errno == EPROTONOSUPPORT ||
                    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
                    errno == EAFNOSUPPORT    || errno == EADDRNOTAVAIL)
                    continue;
            return C_ERR;
        }
        //设置socket 为非阻塞
        anetNonBlock(NULL,fds[*count]);
        (*count)++;
    }
    return C_OK;
}

输入的port表示用户配置的端口号,server结构体的bindaddr_count字段存储用户配置的IP地址数目,bindaddr字段存储用户配置的所有IP地址。函数aneTcpServer实现了 socket 的创建,绑定,监听流程。参数fdscount可以用作输出参数,fds数组存储创建的所有 socket 文件描述符,count存储 socket 数目。

  • 注意:所有创建的socket都会设置为非阻塞模式,原因在于Redis使用了IO多路复用模式,其要求socket独写必须是非阻塞的,函数anetNonBlock通过系统调用fcntl设置socket为非阻塞模式。

3.2、创建文件事件与时间事件

上面已经完成了socket的创建以及端口的监听,但是还需要创建相应的事件进行监听,下面将会介绍文件事件和时间事件的创建。

	 //对IP socket 创建文件事件
    for (j = 0; j < server.ipfd_count; j++) {
     
        if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL) == AE_ERR)
            {
     
                serverPanic(
                    "Unrecoverable error creating server.ipfd file event.");
            }
    }
    
     //对 tls 创建文件事件
    for (j = 0; j < server.tlsfd_count; j++) {
     
        if (aeCreateFileEvent(server.el, server.tlsfd[j], AE_READABLE,
            acceptTLSHandler,NULL) == AE_ERR)
            {
     
                serverPanic(
                    "Unrecoverable error creating server.tlsfd file event.");
            }
    }
  • server结构体的ipfd_count字段存储创建的监听socket数目,ipfd数组存储的是所有监听socket文件描述符,需要遍历所有的监听socket,为其创建对应的文件事件。可以看到监听事件的处理函数为acceptTcpHandler(后面的指令处理和这个函数有关),实现了socket连接请求的accept,以及客户端对象的创建。
	//创建时间事件
    if (aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL) == AE_ERR) {
     
        serverPanic("Can't create event loop timers.");
        exit(1);
    }
  • 在《Redis5源码阅读【8-命令处理生命周期-中】》中提到,定时任务被抽象为时间事件,且Redis只创建了一个时间事件,在服务端初始化时创建。此时间事件的处理函数为serverCron,初次创建时1毫秒后就会被触发。

3.3、开启事件循环

前面几个步骤已经完成了服务端的初始化工作,并且在指定的ipport开启socket监听等待客户端连接请求,同时创建了文件事件时间事件;此时只需要开启事件循环等待事件即可。

/**
 * 事件驱动的循环主入口
 * @param eventLoop
 */
void aeMain(aeEventLoop *eventLoop) {
     
    eventLoop->stop = 0; //设置停止标记为(不停止)
    while (!eventLoop->stop) {
      //除非停止标志被设置,不然循环不会停止
        if (eventLoop->beforesleep != NULL)
            eventLoop->beforesleep(eventLoop);//函数不为空,先执行阻塞函数
        aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP);//执行事件
    }
}

事件处理主函数aeProcessEvents在《Redis5源码阅读【8-命令处理生命周期-中】》已经介绍过,这里需要重点关注函数beforesleep,它在每次事件循环开始,即Redis阻塞等待文件事件之前执行。函数beforesleep会执行一些不是很浪费时间的操作,如:集群相关操作过期键的删除操作向客户端返回命令回复等。

3.3.1、过期键删除

前面提到,在aeMain->beforesleep中会触发多种短时间操作,过期键操作就是其中一个。Redis过期键删除有两种策略:

  • 1、访问数据库时,校验该键是否过期,如果过期则删除;
  • 2、周期性删除过期键,beforesleep函数与serverCron函数都会执行。

server结构体的active_expire_enabled字段表示是否开启周期性删除过期键策略,用户可通过set active-expire 指令配置;masterhost字段存储当前Redis服务器的master服务器的域名,如果为NULL说明当前服务器不是某个服务器的slave。注意到这里依然是调用函数activeExpireCycle执行过期键删除,只是参数传递的是ACTIVE_EXPIRE_CYCLE_FAST表示快速过期删除。

回顾一下前面activeExpireCycle的实现,函数计算出timelimit即函数最大执行时间,循环删除过期键时会校验函数执行时间是否超过此限制,超出则结束循环。显然快速过期键删除时只需要缩短timelimit即可,函数实现如下:

void activeExpireCycle(int type) {
     
// 省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
 	static int timelimit_exit = 0;      /* Time limit hit in previous call? */
    static long long last_fast_cycle = 0; /* When last fast cycle ran. */
//省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
	 //上次activeExpirCycle函数是否已经执行完毕(时间用完之间返回)
     if (!timelimit_exit &&
         server.stat_expired_stale_perc < config_cycle_acceptable_stale)
         return;
     //当前时间距离上次执行快速过期删除是否已经超过2000微秒
     if (start < last_fast_cycle + (long long)config_cycle_fast_duration*2)
         return;
//省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
   //快速过期键删除时,函数执行时间不超过1000微秒
     if (type == ACTIVE_EXPIRE_CYCLE_FAST)
         timelimit = config_cycle_fast_duration; /* in microseconds. */

}

执行快速过期删除有很多限制,当函数activeExpireCycle正在执行时直接返回;当上次执行快速过期键删除的时间小于2000微秒时直接返回。timelimit_exit是声明为statictimelimit_exit的值为1的时候,由此便可通过变量timelimit_exit判断函数activeExpireCycle是否正在执行。变量last_fast_cycle也是声明为static。同时可以看到当执行快速过期删除时,设置函数activeExpireCycle的最大执行时间为1000微秒。

3.3.2、创建client

函数aeProcessEvents为事件处理主函数,它首先查找最近发生的时间事件,调用epoll_wait阻塞等待文件事件的发生并设置超时事件;待epoll_wait返回时,处理触发的文件事件;最后处理时间事件。步骤6中已经创建了文件事件,为监听socket的读事件,事件处理函为acceptTcpHandler,即当客户端发起socket连接请求时,服务端会执行函数acceptTcpHandler处理。acceptTcpHandler函数主要做了以下三件事情:

  • 1、接受(accept)客户端的连接请求;
  • 2、创建客户端对象,并初始化对象各个字段;
  • 3、创建文件事件;

其中创建客户端事件是通过函数createClient实现,conn为接受客户端连接请求后的socket封装的connection

client *createClient(connection *conn) {
     
    client *c = zmalloc(sizeof(client));

    /* passing NULL as conn it is possible to create a non connected client.
     * This is useful since all the commands needs to be executed
     * in the context of a client. When commands are executed in other
     * contexts (for instance a Lua script) we need a non connected client. */
    if (conn) {
     
        //设置为非阻塞
        connNonBlock(conn);
        //设置TCP_NODELAY
        connEnableTcpNoDelay(conn);
        //如果服务端配置了tcpkeepalive,则设置SO_KEEPALIVE
        if (server.tcpkeepalive)
            connKeepAlive(conn,server.tcpkeepalive);
        connSetReadHandler(conn, readQueryFromClient);
        connSetPrivateData(conn, c);
    }
 //初始化client结构体各字段
 }

为了能使用I/O多路复用,这里一样使用connNonBlock设置socket为非阻塞模式。
为了提升网络效率,服务端会使用Nagle算法,当应用层调用write函数发生数据的时候,TCP并不会一定立马把数据发送响应给客户端,根据Nagle算法的规定:

  • 1、当数据包大于一定限制的时候会立即发送;
  • 2、如果数据包中有FIN(TCP断开连接)字段,则立即发送;
  • 3、如果设置了TCP_NODELAY后,则立即发送;

否则数据包将会等待200毫秒才进行发送,这里为了提高Redis响应客户端的效率设置了TCP_NODELAY。同时为了避免TCP握手带来的开销,这里设置TCP的SO_KEEPALIVE,即TCP长连接。接收到客户端请求后,服务端需要创建文件事件等待客户端的命令请求,可以看到这里的文件事件处理函数为readQueryFromClient,当服务器接收到客户端的命令请求时,会执行此函数。

4、总结

本篇文章基本围绕着,服务端,初始化配置,读取配置文件并启动,直到启动完成等待客户端连接的一系列过程,可以用下图表示整个流程过程:
Redis源码阅读【8-命令处理生命周期-3】_第2张图片
其中服务器运行部分的本质其实是一个单线程的while循环,在每一个的循环都会取出相应的事件进行处理,围绕着aeProcessEvents函数,有beforesleepaftersleep两个函数,分别对于事件处理前的准备和事件处理后的后续工作,大家也可以思考一下,哪些逻辑内容应该放在beforesleep哪些内容应该放在aftersleep中。

你可能感兴趣的:(redis,源码阅读)