编辑
2023-11-16
Redis源码阅读
00
请注意,本文编写于 540 天前,最后修改于 540 天前,其中某些信息可能已经过时。

目录

判断是否是哨兵模式
解析sentinel.conf

判断是否是哨兵模式

c
char *exec_name = strrchr(argv[0], '/'); if (exec_name == NULL) exec_name = argv[0]; server.sentinel_mode = checkForSentinelMode(argc,argv, exec_name); initServerConfig(); ACLInit(); /* The ACL subsystem must be initialized ASAP because the basic networking code and client creation depends on it. */ moduleInitModulesSystem(); connTypeInitialize(); ***********************************************************、 /* Returns 1 if there is --sentinel among the arguments or if * executable name contains "redis-sentinel". */ int checkForSentinelMode(int argc, char **argv, char *exec_name) { if (strstr(exec_name,"redis-sentinel") != NULL) return 1; for (int j = 1; j < argc; j++) if (!strcmp(argv[j],"--sentinel")) return 1; return 0; }

初始化哨兵模式

c
/* This function overwrites a few normal Redis config default with Sentinel * specific defaults. */ void initSentinelConfig(void) { server.port = REDIS_SENTINEL_PORT; server.protected_mode = 0; /* Sentinel must be exposed. */ } void freeSentinelLoadQueueEntry(void *item); /* Perform the Sentinel mode initialization. */ void initSentinel(void) { /* Initialize various data structures. */ sentinel.current_epoch = 0; sentinel.masters = dictCreate(&instancesDictType); sentinel.tilt = 0; sentinel.tilt_start_time = 0; sentinel.previous_time = mstime(); sentinel.running_scripts = 0; sentinel.scripts_queue = listCreate(); sentinel.announce_ip = NULL; sentinel.announce_port = 0; sentinel.simfailure_flags = SENTINEL_SIMFAILURE_NONE; sentinel.deny_scripts_reconfig = SENTINEL_DEFAULT_DENY_SCRIPTS_RECONFIG; sentinel.sentinel_auth_pass = NULL; sentinel.sentinel_auth_user = NULL; sentinel.resolve_hostnames = SENTINEL_DEFAULT_RESOLVE_HOSTNAMES; sentinel.announce_hostnames = SENTINEL_DEFAULT_ANNOUNCE_HOSTNAMES; memset(sentinel.myid,0,sizeof(sentinel.myid)); server.sentinel_config = NULL; }
c
/* Main state. */ struct sentinelState { char myid[CONFIG_RUN_ID_SIZE+1]; /* This sentinel ID. */ uint64_t current_epoch; /* Current epoch. */ dict *masters; /* Dictionary of master sentinelRedisInstances. Key is the instance name, value is the sentinelRedisInstance structure pointer. */ int tilt; /* Are we in TILT mode? */ int running_scripts; /* Number of scripts in execution right now. */ mstime_t tilt_start_time; /* When TITL started. */ mstime_t previous_time; /* Last time we ran the time handler. */ list *scripts_queue; /* Queue of user scripts to execute. */ char *announce_ip; /* IP addr that is gossiped to other sentinels if not NULL. */ int announce_port; /* Port that is gossiped to other sentinels if non zero. */ unsigned long simfailure_flags; /* Failures simulation. */ int deny_scripts_reconfig; /* Allow SENTINEL SET ... to change script paths at runtime? */ char *sentinel_auth_pass; /* Password to use for AUTH against other sentinel */ char *sentinel_auth_user; /* Username for ACLs AUTH against other sentinel. */ int resolve_hostnames; /* Support use of hostnames, assuming DNS is well configured. */ int announce_hostnames; /* Announce hostnames instead of IPs when we have them. */ } sentinel;

解析sentinel.conf

loadServerConfig

本文作者:yowayimono

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!