************************************************************************************************************
/*-----------------------------------------------------------------------------
* Config file name-value maps. 配置文件 键 和 值 的 映射
*----------------------------------------------------------------------------*/
定义键值映射结构体
typedef struct configEnum {
const char *name; 枚举的类型
const int val; 枚举类型对应的值
} configEnum;
定义内存策略枚举数组
configEnum maxmemory_policy_enum[] = {
{"volatile-lru", MAXMEMORY_VOLATILE_LRU},
{"volatile-lfu", MAXMEMORY_VOLATILE_LFU},
{"volatile-random",MAXMEMORY_VOLATILE_RANDOM},
{"volatile-ttl",MAXMEMORY_VOLATILE_TTL},
{"allkeys-lru",MAXMEMORY_ALLKEYS_LRU},
{"allkeys-lfu",MAXMEMORY_ALLKEYS_LFU},
{"allkeys-random",MAXMEMORY_ALLKEYS_RANDOM},
{"noeviction",MAXMEMORY_NO_EVICTION},
{NULL, 0}
};
定义系统设施数组
configEnum syslog_facility_enum[] = {
{"user", LOG_USER},
{"local0", LOG_LOCAL0},
{"local1", LOG_LOCAL1},
{"local2", LOG_LOCAL2},
{"local3", LOG_LOCAL3},
{"local4", LOG_LOCAL4},
{"local5", LOG_LOCAL5},
{"local6", LOG_LOCAL6},
{"local7", LOG_LOCAL7},
{NULL, 0}
};
定义日志等级数组
configEnum loglevel_enum[] = {
{"debug", LL_DEBUG},
{"verbose", LL_VERBOSE},
{"notice", LL_NOTICE},
{"warning", LL_WARNING},
{NULL,0}
};
定义redis启动方式
configEnum supervised_mode_enum[] = {
{"upstart", SUPERVISED_UPSTART},
{"systemd", SUPERVISED_SYSTEMD},
{"auto", SUPERVISED_AUTODETECT},
{"no", SUPERVISED_NONE},
{NULL, 0}
};
定义aof文件同步模式
configEnum aof_fsync_enum[] = {
{"everysec", AOF_FSYNC_EVERYSEC},
{"always", AOF_FSYNC_ALWAYS},
{"no", AOF_FSYNC_NO},
{NULL, 0}
};
无盘加载模式
configEnum repl_diskless_load_enum[] = {
{"disabled", REPL_DISKLESS_LOAD_DISABLED},
{"on-empty-db", REPL_DISKLESS_LOAD_WHEN_DB_EMPTY},
{"swapdb", REPL_DISKLESS_LOAD_SWAPDB},
{NULL, 0}
};
无盘加载,默认disabled。不使用无盘加载,即先将rdb文件存储到磁盘。
可选配置项:
"on-empty-db":只有在数据库为空的情况下才使用无盘加载
"swapdb":使用无盘加载,即直接使用来自网络的RDB,同时将本机内存中备份当前数据(内存要确保足够)
/* Output buffer limits presets. */ 输出缓冲区预设的限制大小
clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT] = {
{0, 0, 0}, /* normal */
{1024*1024*256, 1024*1024*64, 60}, /* slave */
{1024*1024*32, 1024*1024*8, 60} /* pubsub */
};
***************************************************************************************************************************
/* Generic config infrastructure function pointers 通用配置基础结构函数指针
* int is_valid_fn(val, err) 判断函数指针是否有效
* Return 1 when val is valid, and 0 when invalid. 返回1如果val是有效的,无效就返回0
* Optionally set err to a static error string. 可以选择将err设置为静态错误字符串
* int update_fn(val, prev, err) 更新函数指针
* This function is called only for CONFIG SET command (not at config file parsing)
仅对CONFIG SET命令调用此函数(在配置文件解析时不调用)
* It is called after the actual config is applied,在实际的配置文件被应用之后才调用它(更新之前的设置使用)
* Return 1 for success, and 0 for failure. 成功返回1,失败返回0
* Optionally set err to a static error string. 可以选择将err设置为静态错误字符串
* On failure the config change will be reverted. 失败的情况下原值被保留
*/
/* Configuration values that require no special handling to set, get, load or
* rewrite. */ 无需特殊处理即可设置、获取、加载或重写的配置值
布尔配置
typedef struct boolConfigData {
int *config; /* The pointer to the server config this value is stored in */指向存储在服务器配置中值的指针
const int default_value; /* The default value of the config on rewrite */重写时默认的配置
int (*is_valid_fn)(int val, char **err); /* Optional function to check validity of new value (generic doc above) */
检查新值有效性的可选函数(见上面is_valid_fn的说明)
int (*update_fn)(int val, int prev, char **err); /* Optional function to apply new value at runtime (generic doc above) */
在运行时设置新值的可选函数(见上面update_fn的说明)
} boolConfigData;
字符串配置
typedef struct stringConfigData {
char **config; /* Pointer to the server config this value is stored in. */ 同上
const char *default_value; /* Default value of the config on rewrite. */
int (*is_valid_fn)(char* val, char **err); /* Optional function to check validity of new value (generic doc above) */
int (*update_fn)(char* val, char* prev, char **err); /* Optional function to apply new value at runtime (generic doc above) */
int convert_empty_to_null; /* Boolean indicating if empty strings should
be stored as a NULL value. */ 布尔值,指示是否应将空字符串存储为空值
} stringConfigData;
枚举配置
typedef struct enumConfigData {
int *config; /* The pointer to the server config this value is stored in */
configEnum *enum_value; /* The underlying enum type this data represents */ 此数据表示的基本枚举类型
const int default_value; /* The default value of the config on rewrite */
int (*is_valid_fn)(int val, char **err); /* Optional function to check validity of new value (generic doc above) */
int (*update_fn)(int val, int prev, char **err); /* Optional function to apply new value at runtime (generic doc above) */
} enumConfigData;
数值类型
typedef enum numericType {
NUMERIC_TYPE_INT,
NUMERIC_TYPE_UINT,
NUMERIC_TYPE_LONG,
NUMERIC_TYPE_ULONG,
NUMERIC_TYPE_LONG_LONG,
NUMERIC_TYPE_ULONG_LONG,
NUMERIC_TYPE_SIZE_T,
NUMERIC_TYPE_SSIZE_T,
NUMERIC_TYPE_OFF_T,
NUMERIC_TYPE_TIME_T,
} numericType;
数值配置信息
typedef struct numericConfigData {
union { 用联合体来表示任意一种数值类型实际的值
int *i;
unsigned int *ui;
long *l;
unsigned long *ul;
long long *ll;
unsigned long long *ull;
size_t *st;
ssize_t *sst;
off_t *ot;
time_t *tt;
} config; /* The pointer to the numeric config this value is stored in */
int is_memory; /* Indicates if this value can be loaded as a memory value */ 表示此值是否可以作为内存值加载
numericType numeric_type; /* An enum indicating the type of this value */ 表示此值类型的枚举类型
long long lower_bound; /* The lower bound of this numeric value */ 此数值的下限
long long upper_bound; /* The upper bound of this numeric value */ 此数值的上限
const long long default_value; /* The default value of the config on rewrite */
int (*is_valid_fn)(long long val, char **err); /* Optional function to check validity of new value (generic doc above) */
int (*update_fn)(long long val, long long prev, char **err); /* Optional function to apply new value at runtime (generic doc above) */
} numericConfigData;
定义各种类型的联合,此联合体适应各种数据类型情况
typedef union typeData {
boolConfigData yesno; 布尔类型
stringConfigData string; 字符串类型
enumConfigData enumd; 枚举类型
numericConfigData numeric; 数值类型
} typeData;
定义数据类型接口
typedef struct typeInterface {
/* Called on server start, to init the server with default value */
在服务器启动时调用,使用默认值初始化服务器中的参数
void (*init)(typeData data);
/* Called on server start, should return 1 on success, 0 on error and should set err */
在服务器启动时调用,使用传入的参数初始化值,成功返回1,错误返回0,并应设置错误信息
int (*load)(typeData data, sds *argc, int argv, char **err);
/* Called on server startup and CONFIG SET, returns 1 on success, 0 on error
* and can set a verbose err string, update is true when called from CONFIG SET */
在服务器启动和重新设置时调用,成功返回1,错误返回0,并且可以设置详细的错误信息,如果调用的是重新设置时,update为true
int (*set)(typeData data, sds value, int update, char **err);
/* Called on CONFIG GET, required to add output to the client */
获取配置信息的调用,需要将输出添加到客户端
void (*get)(client *c, typeData data);
/* Called on CONFIG REWRITE, required to rewrite the config state */
在配置信息重写时调用,需要重写配置状态
void (*rewrite)(typeData data, const char *name, struct rewriteConfigState *state);
} typeInterface;
标准的配置信息
typedef struct standardConfig {
const char *name; /* The user visible name of this config */ 此配置的对外可见名称
const char *alias; /* An alias that can also be used for this config */ 别名
const int modifiable; /* Can this value be updated by CONFIG SET? */ 是否允许运行时修改
typeInterface interface; /* The function pointers that define the type interface */ 定义类型接口的函数指针
typeData data; /* The type specific data exposed used by the interface */ 接口对外使用的特定类型的数据
} standardConfig;
***************************************************************************************************************************
standardConfig configs[]; 定义初始化值的数组
/*-----------------------------------------------------------------------------
* Enum access functions 枚举存取函数
*----------------------------------------------------------------------------*/
/* Get enum value from name. If there is no match INT_MIN is returned. */
通过名称获取枚举值。如果不匹配,则返回最小整型值
int configEnumGetValue(configEnum *ce, char *name) {
while(ce->name != NULL) {
if (!strcasecmp(ce->name,name)) return ce->val;
ce++;
}
return INT_MIN;
}
***************************************************************************************************************************
/* Get enum name from value. If no match is found NULL is returned. */
通过值获取枚举名称。如果不匹配,则返回null
const char *configEnumGetName(configEnum *ce, int val) {
while(ce->name != NULL) {
if (ce->val == val) return ce->name;
ce++;
}
return NULL;
}
***************************************************************************************************************************
/* Wrapper for configEnumGetName() returning "unknown" instead of NULL if
* there is no match. */
封装函数configEnumGetName,当不匹配返回null时,用字符串"unknown"代替
const char *configEnumGetNameOrUnknown(configEnum *ce, int val) {
const char *name = configEnumGetName(ce,val);
return name ? name : "unknown";
}
***************************************************************************************************************************
/* Used for INFO generation. */用于生成信息
根据配置数值获取内存策略的名字
const char *evictPolicyToString(void) {
return configEnumGetNameOrUnknown(maxmemory_policy_enum,server.maxmemory_policy);
}
***************************************************************************************************************************
/*-----------------------------------------------------------------------------
* Config file parsing 配置文件解析
*----------------------------------------------------------------------------*/
将yes no 转化为整数的1 和 0
int yesnotoi(char *s) {
if (!strcasecmp(s,"yes")) return 1;
else if (!strcasecmp(s,"no")) return 0;
else return -1;
}
***************************************************************************************************************************
添加服务器保存条件的参数
struct saveparam {
time_t seconds;
int changes;
};
void appendServerSaveParams(time_t seconds, int changes) {
server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1)); 先分配空间
server.saveparams[server.saveparamslen].seconds = seconds; 时间
server.saveparams[server.saveparamslen].changes = changes; 改变元素数量
server.saveparamslen++; 保存条件多1
}
***************************************************************************************************************************
重新设置服务器保存条件参数
void resetServerSaveParams(void) {
zfree(server.saveparams); 释放原来的保存条件
server.saveparams = NULL; 指向null
server.saveparamslen = 0; 长度为0
}
***************************************************************************************************************************
struct moduleLoadQueueEntry {
sds path;
int argc; 输入参数数量
robj **argv; 输入参数具体的对象
};
加载模块
void queueLoadModule(sds path, sds *argv, int argc) {
int i;
struct moduleLoadQueueEntry *loadmod;
loadmod = zmalloc(sizeof(struct moduleLoadQueueEntry));
loadmod->argv = zmalloc(sizeof(robj*)*argc);
loadmod->path = sdsnew(path); 模块代码存放的路径
loadmod->argc = argc;
for (i = 0; i < argc; i++) { 加载每个模块
loadmod->argv[i] = createRawStringObject(argv[i],sdslen(argv[i]));
}
listAddNodeTail(server.loadmodule_queue,loadmod); 添加到模块列表
-- list *loadmodule_queue; /* List of modules to load at startup. */ 初始化的模块列表
}
***************************************************************************************************************************
初始化配置变量
void initConfigValues() {
for (standardConfig *config = configs; config->name != NULL; config++) { 遍历每个定义在数组中的变量
config->interface.init(config->data); 调用不同类型变量的初始化函数
}
}
***************************************************************************************************************************
从字符串加载服务器配置
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); 根据\n(换行)分割字符串
for (i = 0; i < totlines; i++) { 分割出来的字符串数组
sds *argv;
int argc;
linenum = i+1; 行数
lines[i] = sdstrim(lines[i]," \t\r\n"); 去掉制表和换行字符
/* Skip comments and blank lines */ 跳过评论和空白行
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;
}
/* Skip this line if the resulting command vector is empty. */ 跳过这行 如果结果数组是空的
if (argc == 0) {
sdsfreesplitres(argv,argc);
continue;
}
sdstolower(argv[0]); 转化为小写字母
/* Iterate the configs that are standard */ 迭代初始化时候的配置
int match = 0;
for (standardConfig *config = configs; config->name != NULL; config++) {
if ((!strcasecmp(argv[0],config->name) ||
(config->alias && !strcasecmp(argv[0],config->alias)))) 是否存在同名的 包括正式名和别称
{
if (argc != 2) { 参数不是两个,格式错误 正常为 键->值 模式
err = "wrong number of arguments";
goto loaderr;
}
if (!config->interface.set(config->data, argv[1], 0, &err)) { 重新设置参数
goto loaderr;
}
match = 1; 设置成功
break; 跳出循环,操作下一个
}
}
if (match) { 存在匹配,释放, 跳到下一个
sdsfreesplitres(argv,argc);
continue;
}
/* Execute config directives */ 直接执行的配置命令
if (!strcasecmp(argv[0],"bind") && argc >= 2) { 如果是bind命令 参数超过两个 即绑定多个地址
int j, addresses = argc-1;
-- #define CONFIG_BINDADDR_MAX 16 最大绑定地址为16个
if (addresses > CONFIG_BINDADDR_MAX) {
err = "Too many bind addresses specified"; goto loaderr;
}
/* Free old bind addresses */ 释放旧的绑定地址
for (j = 0; j < server.bindaddr_count; j++) {
zfree(server.bindaddr[j]);
}
for (j = 0; j < addresses; j++) 添加新的绑定地址
server.bindaddr[j] = zstrdup(argv[j+1]);
server.bindaddr_count = addresses; 绑定地址个数
} else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) {
指定 UNIX socket permission 的权限 unixsocketperm 755
errno = 0;
server.unixsocketperm = (mode_t)strtol(argv[1], NULL, 8); 转化为8进制
if (errno || server.unixsocketperm > 0777) { 超出777的范围
err = "Invalid socket file permissions"; goto loaderr;
}
} else if (!strcasecmp(argv[0],"save")) { 保存命令
if (argc == 3) { 需要三个参数
int seconds = atoi(argv[1]); 经过多少时间
int changes = atoi(argv[2]); 与多少元素变化
if (seconds < 1 || changes < 0) { 时间少于1妙 或者变动为负 这是不合理的值
err = "Invalid save parameters"; goto loaderr;
}
appendServerSaveParams(seconds,changes); 存储保存条件到内部数组
} else if (argc == 2 && !strcasecmp(argv[1],"")) { 两个参数 第二个为空,重设保存条件参数
resetServerSaveParams();
}
} else if (!strcasecmp(argv[0],"dir") && argc == 2) {
指定rdb/AOF文件的目录位置,只能为文件夹不能为文件 dir /var/lib/redis
if (chdir(argv[1]) == -1) { 检测是否是目录
serverLog(LL_WARNING,"Can't chdir to '%s': %s",
argv[1], strerror(errno));
exit(1);
}
} else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
指定日志文件名。此外,可以使用空字符串强制Redis登录标准输出。
请注意,如果使用标准输出进行日志记录,但使用守护进程,则日志将发送到/dev/null
例如: logfile "/soft/myredis/log/redis.log"
FILE *logfp;
zfree(server.logfile); 释放原来的
server.logfile = zstrdup(argv[1]); 新建
if (server.logfile[0] != '\0') { 文件非空
/* Test if we are able to open the file. The server will not
* be able to abort just for this problem later... */
测试文件是否可以打开,免得后面服务器因为这个原因退出
logfp = fopen(server.logfile,"a");
if (logfp == NULL) {
err = sdscatprintf(sdsempty(),
"Can't open the log file: %s", strerror(errno));
goto loaderr;
}
fclose(logfp);
}
} else if (!strcasecmp(argv[0],"include") && argc == 2) { 说明参见redis.conf文档 INCLUDES部分
loadServerConfig(argv[1],NULL); 加载文件配置
} else if ((!strcasecmp(argv[0],"client-query-buffer-limit")) && argc == 2) { 客户端查询最大缓存 说明参见redis.conf文档
server.client_max_querybuf_len = memtoll(argv[1],NULL);
} else if ((!strcasecmp(argv[0],"slaveof") ||
!strcasecmp(argv[0],"replicaof")) && argc == 3) { 配置主机复制的主ip和端口
slaveof_linenum = linenum;
server.masterhost = sdsnew(argv[1]); 主ip
server.masterport = atoi(argv[2]); 主port
server.repl_state = REPL_STATE_CONNECT;
-- #define REPL_STATE_CONNECT 1 /* Must connect to master */
} else if (!strcasecmp(argv[0],"requirepass") && argc == 2) { 密码验证
if (strlen(argv[1]) > CONFIG_AUTHPASS_MAX_LEN) { #define CONFIG_AUTHPASS_MAX_LEN 512 最长密码长度
err = "Password is longer than CONFIG_AUTHPASS_MAX_LEN";
goto loaderr;
}
/* The old "requirepass" directive just translates to setting
* a password to the default user. The only thing we do
* additionally is to remember the cleartext password in this
* case, for backward compatibility with Redis <= 5. */
旧的“requirepass”指令只是将密码设置为默认用户。另外,我们要做的就是记住明文密码,以便向后兼容Redis<=5
ACLSetUser(DefaultUser,"resetpass",-1); 取消无需密码登录 ,清空现有密码
sds aclop = sdscatprintf(sdsempty(),">%s",argv[1]); 获取输入密码
ACLSetUser(DefaultUser,aclop,sdslen(aclop)); 设置默认密码
sdsfree(aclop);
sdsfree(server.requirepass);
server.requirepass = sdsnew(argv[1]);
} else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
/* DEAD OPTION */ 假的选择项
} else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
/* DEAD OPTION */
} else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
struct redisCommand *cmd = lookupCommand(argv[1]); 查找命令
int retval;
if (!cmd) {
err = "No such command in rename-command";
goto loaderr;
}
/* If the target command name is the empty string we just
* remove it from the command table. */
如果目标命令为空字符串,我们只需要从命令表重移除即可
retval = dictDelete(server.commands, argv[1]);
serverAssert(retval == DICT_OK);
/* Otherwise we re-add the command under a different name. */
否则,我们将以不同的名称重新添加命令
if (sdslen(argv[2]) != 0) {
sds copy = sdsdup(argv[2]); 赋值重命名的名字
retval = dictAdd(server.commands, copy, cmd); 增加新名字
if (retval != DICT_OK) {
sdsfree(copy);
err = "Target command name already exists"; goto loaderr;
}
}
} else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) { 集群节点配置文件
zfree(server.cluster_configfile);
server.cluster_configfile = zstrdup(argv[1]);
} else if (!strcasecmp(argv[0],"client-output-buffer-limit") && 客户端输出缓存限制,详细见redis.conf
argc == 5)
{
int class = getClientTypeByName(argv[1]); 通过客户端名字获取类型
unsigned long long hard, soft;
int soft_seconds;
if (class == -1 || class == CLIENT_TYPE_MASTER) { 如果是不可识别类型 或者是 主机 不需要缓存限制
err = "Unrecognized client limit class: the user specified "
"an invalid one, or 'master' which has no buffer limits.";
goto loaderr;
}
hard = memtoll(argv[2],NULL); 将内存字符串表示转化为长整型,总限制大小
soft = memtoll(argv[3],NULL); 持续一段时间限制大小,时间为下面的秒数
soft_seconds = atoi(argv[4]); 输入的时间秒数
if (soft_seconds < 0) { 输入的时间为负
err = "Negative number of seconds in soft limit is invalid";
goto loaderr;
}
server.client_obuf_limits[class].hard_limit_bytes = hard;
server.client_obuf_limits[class].soft_limit_bytes = soft;
server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
} else if (!strcasecmp(argv[0],"notify-keyspace-events") && argc == 2) { Key过期事件的通知给订阅的客户
int flags = keyspaceEventsStringToFlags(argv[1]); 将通知事件的字符串表示转化为整型
if (flags == -1) { 无效事件
err = "Invalid event class character. Use 'g$lshzxeA'.";
goto loaderr;
}
server.notify_keyspace_events = flags;
} else if (!strcasecmp(argv[0],"user") && argc >= 2) { 添加用户
int argc_err;
if (ACLAppendUserForLoading(argv,argc,&argc_err) == C_ERR) { 设置acl列表的用户
char buf[1024];
char *errmsg = ACLSetUserStringError();
snprintf(buf,sizeof(buf),"Error in user declaration '%s': %s",
argv[argc_err],errmsg);
err = buf;
goto loaderr;
}
} else if (!strcasecmp(argv[0],"loadmodule") && argc >= 2) { 加载模块
queueLoadModule(argv[1],&argv[2],argc-2);
} else if (!strcasecmp(argv[0],"sentinel")) {
/* argc == 1 is handled by main() as we need to enter the sentinel
* mode ASAP. */
当argc为1时候,由主函数处理,因为我们需要尽快进入sentinel模式
if (argc != 1) {
if (!server.sentinel_mode) {
err = "sentinel directive while not in sentinel mode";
goto loaderr;
}
err = sentinelHandleConfiguration(argv+1,argc-1); 配置sentinel的配置文件
if (err) goto loaderr;
}
} else {
err = "Bad directive or wrong number of arguments"; goto loaderr;
}
sdsfreesplitres(argv,argc); 释放掉申请的内存空间
}
/* Sanity checks. */
if (server.cluster_enabled && server.masterhost) { 主从模式和集群模式不兼容
linenum = slaveof_linenum;
i = linenum-1;
err = "replicaof directive not allowed in cluster mode";
goto loaderr;
}
sdsfreesplitres(lines,totlines); 释放为输入的参数分配的数组
return;
loaderr:
fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR (Redis %s) ***\n",
REDIS_VERSION);
fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
fprintf(stderr, ">>> '%s'\n", lines[i]);
fprintf(stderr, "%s\n", err);
exit(1); 加载配置文件出错直接退出服务器
}
***************************************************************************************************************************
/* Load the server configuration from the specified filename.
* The function appends the additional configuration directives stored
* in the 'options' string to the config file before loading.
*
* Both filename and options can be NULL, in such a case are considered
* empty. This way loadServerConfig can be used to just load a file or
* just load a string. */
从指定的文件名加载服务器配置。该函数在加载之前将存储在“options”字符串中的其他配置指令附加到配置文件。
文件名和options都可以为空,在这种情况下加载配置就是空的。
这个loadServerConfig函数可以用来加载一个文件或一个字符串。
void loadServerConfig(char *filename, char *options) {
sds config = sdsempty();
char buf[CONFIG_MAX_LINE+1]; --#define CONFIG_MAX_LINE 1024 定义行的最大字节数
/* 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_MAX_LINE个字节的内容,碰到行结尾或者文件结尾提前返回
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); 释放配置的内存空间
}
/*-----------------------------------------------------------------------------
* CONFIG SET implementation 配置设置命令实现
*----------------------------------------------------------------------------*/
设置布尔域的值
#define config_set_bool_field(_name,_var) \
} else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
int yn = yesnotoi(o->ptr); \
if (yn == -1) goto badfmt; \
_var = yn;
设置数值类型的值
#define config_set_numerical_field(_name,_var,min,max) \
} else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
if (getLongLongFromObject(o,&ll) == C_ERR) goto badfmt; \
if (min != LLONG_MIN && ll < min) goto badfmt; \
if (max != LLONG_MAX && ll > max) goto badfmt; \
_var = ll;
设置内存类型的值
#define config_set_memory_field(_name,_var) \
} else if (!strcasecmp(c->argv[2]->ptr,_name)) { \
ll = memtoll(o->ptr,&err); \
if (err || ll < 0) goto badfmt; \
_var = ll;
设置特殊类型的域
#define config_set_special_field(_name) \
} else if (!strcasecmp(c->argv[2]->ptr,_name)) {
用别名设置特殊类型的域
#define config_set_special_field_with_alias(_name1,_name2) \
} else if (!strcasecmp(c->argv[2]->ptr,_name1) || \
!strcasecmp(c->argv[2]->ptr,_name2)) {
#define config_set_else } else
************************************************************************************************************************
void configSetCommand(client *c) {
robj *o;
long long ll;
int err;
char *errstr = NULL;
serverAssertWithInfo(c,c->argv[2],sdsEncodedObject(c->argv[2])); 确认传入参数编码类型是字符串
serverAssertWithInfo(c,c->argv[3],sdsEncodedObject(c->argv[3]));
o = c->argv[3];
/* Iterate the configs that are standard */ 迭代标准的配置参数
for (standardConfig *config = configs; config->name != NULL; config++) {
if(config->modifiable && (!strcasecmp(c->argv[2]->ptr,config->name) || 是否可修改 名字匹配
(config->alias && !strcasecmp(c->argv[2]->ptr,config->alias)))) 有别名清下别名匹配
{
if (!config->interface.set(config->data,o->ptr,1,&errstr)) { 重新设置新值
goto badfmt;
}
addReply(c,shared.ok);
return;
}
}
if (0) { /* this starts the config_set macros else-if chain. */
这个条件判断是用来执行 else-if部分的内容,注意看上面的类似 config_set_special_field 的宏定义
/* Special fields that can't be handled with general macros. */ 特殊的域不能用一般的宏定义来处理
config_set_special_field("requirepass") {
if (sdslen(o->ptr) > CONFIG_AUTHPASS_MAX_LEN) goto badfmt; 大于最大密码长度
/* The old "requirepass" directive just translates to setting
* a password to the default user. The only thing we do
* additionally is to remember the cleartext password in this
* case, for backward compatibility with Redis <= 5. */
旧的“requirepass”指令只是将密码设置为默认用户。另外,我们要做的就是记住明文密码,以便向后兼容Redis<=5
ACLSetUser(DefaultUser,"resetpass",-1);
sds aclop = sdscatprintf(sdsempty(),">%s",(char*)o->ptr); 获取输入密码
ACLSetUser(DefaultUser,aclop,sdslen(aclop));
sdsfree(aclop);
sdsfree(server.requirepass);
server.requirepass = sdsnew(o->ptr); 设置默认密码
} config_set_special_field("save") {
int vlen, j;
sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
/* Perform sanity check before setting the new config: 在设置新配置之前执行合法性检查
* - Even number of args 参数数量是偶数
* - Seconds >= 1, changes >= 0 */ 秒数大于1 变化键大于等于0
if (vlen & 1) { 奇数,参数个数不对
sdsfreesplitres(v,vlen);
goto badfmt;
}
for (j = 0; j < vlen; j++) {
char *eptr;
long val;
val = strtoll(v[j], &eptr, 10);
if (eptr[0] != '\0' || 值不为空
((j & 1) == 0 && val < 1) || 参数对的一个参数 秒数
((j & 1) == 1 && val < 0)) { 参数对的第二个参数 变化键
sdsfreesplitres(v,vlen);
goto badfmt;
}
}
/* Finally set the new config */ 最终设置新配置
resetServerSaveParams();
for (j = 0; j < vlen; j += 2) {
time_t seconds;
int changes;
seconds = strtoll(v[j],NULL,10);
changes = strtoll(v[j+1],NULL,10);
appendServerSaveParams(seconds, changes);
}
sdsfreesplitres(v,vlen);
} config_set_special_field("dir") {
指定rdb/AOF文件的目录位置,只能为文件夹不能为文件 dir /var/lib/redis
if (chdir((char*)o->ptr) == -1) { 只能为文件夹
addReplyErrorFormat(c,"Changing directory: %s", strerror(errno));
return;
}
} config_set_special_field("client-output-buffer-limit") {
int vlen, j;
sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
/* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */ 按照格式 我们需要4个一组的数据
if (vlen % 4) { 非四个一组,格式有问题
sdsfreesplitres(v,vlen);
goto badfmt;
}
/* Sanity check of single arguments, so that we either refuse the
* whole configuration string or accept it all, even if a single
* error in a single client class is present. */
对单个参数进行合法性检查,从而拒绝所有配置字符串或全部接受,即使单个客户机类中存在单个错误,也全部拒绝
for (j = 0; j < vlen; j++) { 检查所有参数
long val;
if ((j % 4) == 0) {
int class = getClientTypeByName(v[j]); 获取类型
if (class == -1 || class == CLIENT_TYPE_MASTER) {
sdsfreesplitres(v,vlen);
goto badfmt;
}
} else {
val = memtoll(v[j], &err); 是否可以转化为数值
if (err || val < 0) {
sdsfreesplitres(v,vlen);
goto badfmt;
}
}
}
/* Finally set the new config */ 最终设置新配置
for (j = 0; j < vlen; j += 4) {
int class;
unsigned long long hard, soft;
int soft_seconds;
class = getClientTypeByName(v[j]);
hard = memtoll(v[j+1],NULL);
soft = memtoll(v[j+2],NULL);
soft_seconds = strtoll(v[j+3],NULL,10);
server.client_obuf_limits[class].hard_limit_bytes = hard;
server.client_obuf_limits[class].soft_limit_bytes = soft;
server.client_obuf_limits[class].soft_limit_seconds = soft_seconds;
}
sdsfreesplitres(v,vlen);
} config_set_special_field("notify-keyspace-events") { Key过期事件的通知给订阅的客户
int flags = keyspaceEventsStringToFlags(o->ptr);
if (flags == -1) goto badfmt;
server.notify_keyspace_events = flags;
/* Numerical fields. 数值域
* config_set_numerical_field(name,var,min,max) */
} config_set_numerical_field(
"watchdog-period",ll,0,INT_MAX) { 设置看门狗的时间,超过这个时间就人为是慢操作 单位毫秒
if (ll)
enableWatchdog(ll);
else
disableWatchdog();
/* Memory fields. 内存域
* config_set_memory_field(name,var) */
} config_set_memory_field(
"client-query-buffer-limit",server.client_max_querybuf_len) { 设置客户端查询缓存限制
/* Everything else is an error... */ 其它情况下都时错误的
} config_set_else {
addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
(char*)c->argv[2]->ptr);
return;
}
/* On success we just return a generic OK for all the options. */
对所有可选择项,成功的情况下我们只返回一般的OK
addReply(c,shared.ok);
return;
badfmt: /* Bad format errors */ 错误格式错误
if (errstr) {
addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s' - %s",
(char*)o->ptr,
(char*)c->argv[2]->ptr,
errstr);
} else {
addReplyErrorFormat(c,"Invalid argument '%s' for CONFIG SET '%s'",
(char*)o->ptr,
(char*)c->argv[2]->ptr);
}
}
/*-----------------------------------------------------------------------------
* CONFIG GET implementation 配置参数获取实现
*----------------------------------------------------------------------------*/
获取字符串
#define config_get_string_field(_name,_var) do { \
if (stringmatch(pattern,_name,1)) { \
addReplyBulkCString(c,_name); \
addReplyBulkCString(c,_var ? _var : ""); \
matches++; \
} \
} while(0);
获取布尔类型
#define config_get_bool_field(_name,_var) do { \
if (stringmatch(pattern,_name,1)) { \
addReplyBulkCString(c,_name); \
addReplyBulkCString(c,_var ? "yes" : "no"); \
matches++; \
} \
} while(0);
获取数值类型
#define config_get_numerical_field(_name,_var) do { \
if (stringmatch(pattern,_name,1)) { \
ll2string(buf,sizeof(buf),_var); \
addReplyBulkCString(c,_name); \
addReplyBulkCString(c,buf); \
matches++; \
} \
} while(0);
******************************************************************************************************************
void configGetCommand(client *c) {
robj *o = c->argv[2];
void *replylen = addReplyDeferredLen(c);
char *pattern = o->ptr;
char buf[128];
int matches = 0;
serverAssertWithInfo(c,o,sdsEncodedObject(o));
/* Iterate the configs that are standard */ 迭代所有标准的配置信息
for (standardConfig *config = configs; config->name != NULL; config++) {
if (stringmatch(pattern,config->name,1)) { 模式匹配
addReplyBulkCString(c,config->name);
config->interface.get(c,config->data); 获取配置信息
matches++;
}
if (config->alias && stringmatch(pattern,config->alias,1)) { 别名匹配
addReplyBulkCString(c,config->alias);
config->interface.get(c,config->data);
matches++;
}
}
/* String values */ 获取字符串值 日志文件
config_get_string_field("logfile",server.logfile);
/* Numerical values */ 获取数值
config_get_numerical_field("client-query-buffer-limit",server.client_max_querybuf_len); 客户端查询缓存限制
config_get_numerical_field("watchdog-period",server.watchdog_period); 看门狗定时
/* Everything we can't handle with macros follows. */ 下面是所有不能用宏定义处理的事情
if (stringmatch(pattern,"dir",1)) { 指定rdb/AOF文件的目录位置,只能为文件夹不能为文件 dir /var/lib/redis
char buf[1024];
if (getcwd(buf,sizeof(buf)) == NULL) 获取当前目录的绝对路径
buf[0] = '\0';
addReplyBulkCString(c,"dir");
addReplyBulkCString(c,buf);
matches++;
}
if (stringmatch(pattern,"save",1)) { 出发保存的条件参数
sds buf = sdsempty();
int j;
for (j = 0; j < server.saveparamslen; j++) {
buf = sdscatprintf(buf,"%jd %d",
(intmax_t)server.saveparams[j].seconds,
server.saveparams[j].changes);
if (j != server.saveparamslen-1)
buf = sdscatlen(buf," ",1);
}
addReplyBulkCString(c,"save");
addReplyBulkCString(c,buf);
sdsfree(buf);
matches++;
}
if (stringmatch(pattern,"client-output-buffer-limit",1)) { 客户端输出缓存限制
sds buf = sdsempty();
int j;
#define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output
buffer configuration. Just the first
three: normal, slave, pubsub. */
要公开到输出缓冲区配置的客户端类型。只有前3种:normal, slave, pubsub.
for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++) {
buf = sdscatprintf(buf,"%s %llu %llu %ld",
getClientTypeName(j),
server.client_obuf_limits[j].hard_limit_bytes,
server.client_obuf_limits[j].soft_limit_bytes,
(long) server.client_obuf_limits[j].soft_limit_seconds);
if (j != CLIENT_TYPE_OBUF_COUNT-1)
buf = sdscatlen(buf," ",1);
}
addReplyBulkCString(c,"client-output-buffer-limit");
addReplyBulkCString(c,buf);
sdsfree(buf);
matches++;
}
if (stringmatch(pattern,"unixsocketperm",1)) { 指定 UNIX socket permission 的权限 unixsocketperm 755
char buf[32];
snprintf(buf,sizeof(buf),"%o",server.unixsocketperm);
addReplyBulkCString(c,"unixsocketperm");
addReplyBulkCString(c,buf);
matches++;
}
if (stringmatch(pattern,"slaveof",1) || 配置主机复制的主ip和端口
stringmatch(pattern,"replicaof",1))
{
char *optname = stringmatch(pattern,"slaveof",1) ?
"slaveof" : "replicaof";
char buf[256];
addReplyBulkCString(c,optname);
if (server.masterhost)
snprintf(buf,sizeof(buf),"%s %d",
server.masterhost, server.masterport);
else
buf[0] = '\0';
addReplyBulkCString(c,buf);
matches++;
}
if (stringmatch(pattern,"notify-keyspace-events",1)) { Key过期事件的通知给订阅的客户
sds flags = keyspaceEventsFlagsToString(server.notify_keyspace_events);
addReplyBulkCString(c,"notify-keyspace-events");
addReplyBulkSds(c,flags);
matches++;
}
if (stringmatch(pattern,"bind",1)) { 绑定地址
sds aux = sdsjoin(server.bindaddr,server.bindaddr_count," ");
addReplyBulkCString(c,"bind");
addReplyBulkCString(c,aux);
sdsfree(aux);
matches++;
}
if (stringmatch(pattern,"requirepass",1)) {
获取默认用户的密码
addReplyBulkCString(c,"requirepass");
sds password = server.requirepass;
if (password) {
addReplyBulkCBuffer(c,password,sdslen(password));
} else {
addReplyBulkCString(c,"");
}
matches++;
}
setDeferredMapLen(c,replylen,matches); 设置回复参数
}
/*-----------------------------------------------------------------------------
* CONFIG REWRITE implementation 配置信息重写实现
*----------------------------------------------------------------------------*/
#define REDIS_CONFIG_REWRITE_SIGNATURE "# Generated by CONFIG REWRITE"
/* We use the following dictionary type to store where a configuration
* option is mentioned in the old configuration file, so it's
* like "maxmemory" -> list of line numbers (first line is zero). */
我们使用下面的字典类型来保存旧配置文件中提到的配置选项,
所以它就像“maxmemory”->行号列表(第一行是零)
uint64_t dictSdsCaseHash(const void *key);
int dictSdsKeyCaseCompare(void *privdata, const void *key1, const void *key2);
void dictSdsDestructor(void *privdata, void *val);
void dictListDestructor(void *privdata, void *val);
/* Sentinel config rewriting is implemented inside sentinel.c by
* rewriteConfigSentinelOption(). */
Sentinel配置的重写实现是在文件sentinel.c里面的函数rewriteConfigSentinelOption
void rewriteConfigSentinelOption(struct rewriteConfigState *state);
dictType optionToLineDictType = {
dictSdsCaseHash, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
dictSdsKeyCaseCompare, /* key compare */
dictSdsDestructor, /* key destructor */
dictListDestructor /* val destructor */
};
dictType optionSetDictType = {
dictSdsCaseHash, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
dictSdsKeyCaseCompare, /* key compare */
dictSdsDestructor, /* key destructor */
NULL /* val destructor */
};
/* The config rewrite state. */ 配置重写状态
struct rewriteConfigState {
dict *option_to_line; /* Option -> list of config file lines map */ 配置项->配置文件函数 的映射
dict *rewritten; /* Dictionary of already processed options */ 已经处理过的配置项的字典
int numlines; /* Number of lines in current config */ 当前配置项的行数
sds *lines; /* Current lines as an array of sds strings */ 当前行是 sds字符串数组
int has_tail; /* True if we already added directives that were
not present in the original config file. */ 如果我们已经添加了原始配置文件中不存在的指令,则为True
};
***************************************************************************************************
/* Append the new line to the current configuration state. */ 将新行添加到当前配置状态
void rewriteConfigAppendLine(struct rewriteConfigState *state, sds line) {
state->lines = zrealloc(state->lines, sizeof(char*) * (state->numlines+1));
state->lines[state->numlines++] = line;
}
***************************************************************************************************
/* Populate the option -> list of line numbers map. */填充选项->行号列表映射
void rewriteConfigAddLineNumberToOption(struct rewriteConfigState *state, sds option, int linenum) {
list *l = dictFetchValue(state->option_to_line,option); 查找是否存在
if (l == NULL) { 不存在新建
l = listCreate();
dictAdd(state->option_to_line,sdsdup(option),l); 字典中增加新键值
}
listAddNodeTail(l,(void*)(long)linenum); 列表后面添加行号,一个键值可能存在对应多个行
}
***************************************************************************************************
/* Add the specified option to the set of processed options.
* This is useful as only unused lines of processed options will be blanked
* in the config file, while options the rewrite process does not understand
* remain untouched. */
将指定的选项添加到已处理的选项集中。
这很有用,因为在配置文件中只有未使用的已处理选项行将被清空,而重写过程不理解(就是不在处理过的字典中的)的选项将保持不变。
void rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option) {
sds opt = sdsnew(option);
if (dictAdd(state->rewritten,opt,NULL) != DICT_OK) sdsfree(opt);
}
***************************************************************************************************
/* Read the old file, split it into lines to populate a newly created
* config rewrite state, and return it to the caller.
读取旧文件,将其拆分为行以填充新创建的配置重写状态,并将其返回给调用者。
* If it is impossible to read the old file, NULL is returned.
* If the old file does not exist at all, an empty state is returned. */
如果无法读取旧文件,则返回空。
如果旧文件根本不存在,则返回空状态
struct rewriteConfigState *rewriteConfigReadOldFile(char *path) {
FILE *fp = fopen(path,"r"); 打开就配置文件
if (fp == NULL && errno != ENOENT) return NULL;
char buf[CONFIG_MAX_LINE+1];
int linenum = -1;
struct rewriteConfigState *state = zmalloc(sizeof(*state));
state->option_to_line = dictCreate(&optionToLineDictType,NULL); 创建 选择项 和 行数 对应的字典
state->rewritten = dictCreate(&optionSetDictType,NULL); 创建处理过的选择项字典
state->numlines = 0; 初始的时候函数为0
state->lines = NULL; 当前行为空
state->has_tail = 0; 不是新项,原配置没有
if (fp == NULL) return state;
/* Read the old file line by line, populate the state. */ 一行一行读取原配置文件,填充配置状态结构
while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL) {
int argc;
sds *argv;
sds line = sdstrim(sdsnew(buf),"\r\n\t "); 去除各种空字符
linenum++; /* Zero based, so we init at -1 */ 从0开始,所以我们初始化为-1
/* Handle comments and empty lines. */ 处理评论和空行
if (line[0] == '#' || line[0] == '\0') {
if (!state->has_tail && !strcmp(line,REDIS_CONFIG_REWRITE_SIGNATURE))
state->has_tail = 1;
rewriteConfigAppendLine(state,line);
continue;
}
/* Not a comment, split into arguments. */ 非评论,分割为参数数组
argv = sdssplitargs(line,&argc);
if (argv == NULL) {
/* Apparently the line is unparsable for some reason, for
* instance it may have unbalanced quotes. Load it as a
* comment. */
显然,由于某些原因,这行是不可解析的,例如它可能有不平衡的引号。将其作为注释加载
sds aux = sdsnew("# ??? ");
aux = sdscatsds(aux,line);
sdsfree(line);
rewriteConfigAppendLine(state,aux);
continue;
}
sdstolower(argv[0]); /* We only want lowercase config directives. */ 我们值需要小写的配置指令
/* Now we populate the state according to the content of this line.
* Append the line and populate the option -> line numbers map. */
现在我们根据此行的内容填充状态。追加行并填充选项->行号映射。
rewriteConfigAppendLine(state,line);
/* Translate options using the word "slave" to the corresponding name
* "replica", before adding such option to the config name -> lines
* mapping. */
在将选项添加到配置名称->行映射之前,使用单词“slave”将选项转换为相应的名称“replica”
char *p = strstr(argv[0],"slave");
if (p) {
sds alt = sdsempty();
alt = sdscatlen(alt,argv[0],p-argv[0]);;
alt = sdscatlen(alt,"replica",7);
alt = sdscatlen(alt,p+5,strlen(p+5));
sdsfree(argv[0]);
argv[0] = alt;
}
rewriteConfigAddLineNumberToOption(state,argv[0],linenum); 添加行号到 选择项
sdsfreesplitres(argv,argc);
}
fclose(fp);
return state;
}
***************************************************************************************************
/* Rewrite the specified configuration option with the new "line".
* It progressively uses lines of the file that were already used for the same
* configuration option in the old version of the file, removing that line from
* the map of options -> line numbers.
用新的“行”重写指定的配置选项。它逐步代替那些已经在旧版本的文件中用于相同的配置选项的行,从选项->行号映射中删除该行
* If there are lines associated with a given configuration option and
* "force" is non-zero, the line is appended to the configuration file.
* Usually "force" is true when an option has not its default value, so it
* must be rewritten even if not present previously.
如果存在与给定配置选项关联的行并且“force”为非零,则该行将附加到配置文件中。
当一个选项没有它的默认值时,通常“force”是真的,所以即使它以前不存在,也必须重写它
* The first time a line is appended into a configuration file, a comment
* is added to show that starting from that point the config file was generated
* by CONFIG REWRITE.
第一次在配置文件中追加一行时,会添加一条注释,表明从该点开始,配置文件是由config REWRITE生成的
* "line" is either used, or freed, so the caller does not need to free it
* in any way. */ “line”要么被使用,要么被释放,因此调用者不需要以任何方式释放它
void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force) {
sds o = sdsnew(option);
list *l = dictFetchValue(state->option_to_line,o); 能够在已有配置中找到这个参数项
rewriteConfigMarkAsProcessed(state,option); 写入到已处理参数字典
if (!l && !force) { 如果原来配置中没有这个参数 非强制写
/* Option not used previously, and we are not forced to use it. */
选择项以前没有使用过,所以我们也不强制的使用它
sdsfree(line);
sdsfree(o);
return;
}
if (l) { 原配置项存在这个参数
listNode *ln = listFirst(l); 找到列表的头节点
int linenum = (long) ln->value; 获取行数
/* There are still lines in the old configuration file we can reuse
* for this option. Replace the line with the new one. */
在旧的配置文件中仍然有行我们可以重用此选项。更换成新行
listDelNode(l,ln); 删除这个节点
if (listLength(l) == 0) dictDelete(state->option_to_line,o); 如果只有一个节点(即一行),那么从中字典删除这对映射值
sdsfree(state->lines[linenum]); 释放这行原来的值
state->lines[linenum] = line;
} else { 原配置项不存在这个参数 ,因为上面的判断,这里的force非零,所以要写入配置文件
/* Append a new line. */ 添加这配置项到新的一行
if (!state->has_tail) { 如果还没有添加过新行,第一次需要添加一个说明
rewriteConfigAppendLine(state,
sdsnew(REDIS_CONFIG_REWRITE_SIGNATURE));
state->has_tail = 1;
}
rewriteConfigAppendLine(state,line); 添加新行
}
sdsfree(o); 释放函数内部分配的空间
}
***************************************************************************************************
/* Write the long long 'bytes' value as a string in a way that is parsable
* inside redis.conf. If possible uses the GB, MB, KB notation. */
以redis.conf配置文件中可解析的方式将长字节值写入字符串。可以使用GB,MB,KB表示法。
int rewriteConfigFormatMemory(char *buf, size_t len, long long bytes) {
int gb = 1024*1024*1024;
int mb = 1024*1024;
int kb = 1024;
if (bytes && (bytes % gb) == 0) {
return snprintf(buf,len,"%lldgb",bytes/gb); G
} else if (bytes && (bytes % mb) == 0) {
return snprintf(buf,len,"%lldmb",bytes/mb); M
} else if (bytes && (bytes % kb) == 0) {
return snprintf(buf,len,"%lldkb",bytes/kb); K
} else {
return snprintf(buf,len,"%lld",bytes); 没有使用单位
}
}
***************************************************************************************************
/* Rewrite a simple "option-name <bytes>" configuration option. */
重写一个简单的 选项 名字<字节数> 配置选项
void rewriteConfigBytesOption(struct rewriteConfigState *state, const char *option, long long value, long long defvalue) {
char buf[64];
int force = value != defvalue; 新旧值不等的时候force的值为1
sds line;
rewriteConfigFormatMemory(buf,sizeof(buf),value); 转化成好看的内存大小格式
line = sdscatprintf(sdsempty(),"%s %s",option,buf);
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite a yes/no option. */
重写一个 yes/no 选择项
void rewriteConfigYesNoOption(struct rewriteConfigState *state, const char *option, int value, int defvalue) {
int force = value != defvalue; 新旧值不等的时候force的值为1
sds line = sdscatprintf(sdsempty(),"%s %s",option, 格式为 (选择项空格参数)
value ? "yes" : "no"); value 为真yes 假no
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite a string option. */
重写一个字符串选择项
void rewriteConfigStringOption(struct rewriteConfigState *state, const char *option, char *value, const char *defvalue) {
int force = 1;
sds line;
/* String options set to NULL need to be not present at all in the
* configuration file to be set to NULL again at the next reboot. */
设置为空的字符串选项 不需要表现在配置文件中, 这样下次重启的首又会设置为空
if (value == NULL) { 如果值为空
rewriteConfigMarkAsProcessed(state,option); 只需要记录处理过即可
return;
}
/* Set force to zero if the value is set to its default. */
如果使用的是默认值,设置变量force为0
if (defvalue && strcmp(value,defvalue) == 0) force = 0;
line = sdsnew(option); 前面的键
line = sdscatlen(line, " ", 1); 中间的空格
line = sdscatrepr(line, value, strlen(value)); 后面的值
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite a numerical (long long range) option. */
重写一个在长整形范围内的数值选项参数
void rewriteConfigNumericalOption(struct rewriteConfigState *state, const char *option, long long value, long long defvalue) {
int force = value != defvalue;
sds line = sdscatprintf(sdsempty(),"%s %lld",option,value);
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite a octal option. */
重写一个八进制选项
void rewriteConfigOctalOption(struct rewriteConfigState *state, char *option, int value, int defvalue) {
int force = value != defvalue;
sds line = sdscatprintf(sdsempty(),"%s %o",option,value);
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite an enumeration option. It takes as usually state and option name,
* and in addition the enumeration array and the default value for the
* option. */
重写一个枚举型的选项。它通常采用状态和选项名,此外还采用枚举数组和选项的默认值
void rewriteConfigEnumOption(struct rewriteConfigState *state, const char *option, int value, configEnum *ce, int defval) {
sds line;
const char *name = configEnumGetNameOrUnknown(ce,value); 从值获取枚举名
int force = value != defval;
line = sdscatprintf(sdsempty(),"%s %s",option,name);
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite the save option. */
重写保存参数
void rewriteConfigSaveOption(struct rewriteConfigState *state) {
int j;
sds line;
/* Note that if there are no save parameters at all, all the current
* config line with "save" will be detected as orphaned and deleted,
* resulting into no RDB persistence as expected. */
注意,如果根本没有save参数,那么所有带有“save”的当前配置行都将被检测为孤立并被删除,从而导致没有预期的RDB持久化文件产生
for (j = 0; j < server.saveparamslen; j++) {
line = sdscatprintf(sdsempty(),"save %ld %d",
(long) server.saveparams[j].seconds, server.saveparams[j].changes);
rewriteConfigRewriteLine(state,"save",line,1);
}
/* Mark "save" as processed in case server.saveparamslen is zero. */
如果server.saveparamslen为零,则将“save”标记为已处理
rewriteConfigMarkAsProcessed(state,"save");
}
***************************************************************************************************
/* Rewrite the user option. */
重写用户参数
void rewriteConfigUserOption(struct rewriteConfigState *state) {
/* If there is a user file defined we just mark this configuration
* directive as processed, so that all the lines containing users
* inside the config file gets discarded. */
如果定义了一个用户文件,我们只需将此配置指令标记为已处理,
这样配置文件中包含用户的所有行都将被丢弃
if (server.acl_filename[0] != '\0') {
rewriteConfigMarkAsProcessed(state,"user");
return;
}
/* Otherwise scan the list of users and rewrite every line. Note that
* in case the list here is empty, the effect will just be to comment
* all the users directive inside the config file. */
否则,扫描用户列表并重写每一行。注意,如果这里的列表是空的,效果就是等于注释配置文件中的所有用户
raxIterator ri;
raxStart(&ri,Users);
raxSeek(&ri,"^",NULL,0);
while(raxNext(&ri)) { 遍历用户的基树
user *u = ri.data;
sds line = sdsnew("user ");
line = sdscatsds(line,u->name);
line = sdscatlen(line," ",1);
sds descr = ACLDescribeUser(u); 获取该用户的描述
line = sdscatsds(line,descr);
sdsfree(descr);
rewriteConfigRewriteLine(state,"user",line,1);
}
raxStop(&ri);
/* Mark "user" as processed in case there are no defined users. */
标记用户当做处理过了,即使没有定义用户这个参数
rewriteConfigMarkAsProcessed(state,"user");
}
***************************************************************************************************
/* Rewrite the dir option, always using absolute paths.*/
重写目录参数,总是使用绝对路径
void rewriteConfigDirOption(struct rewriteConfigState *state) {
char cwd[1024];
if (getcwd(cwd,sizeof(cwd)) == NULL) { 获取当前工作目录的绝对路径
rewriteConfigMarkAsProcessed(state,"dir"); 当做处理过了
return; /* no rewrite on error. */ 错误时候不需要重写
}
rewriteConfigStringOption(state,"dir",cwd,NULL); 正常情况写到配置文件
}
***************************************************************************************************
/* Rewrite the slaveof option. */
重写slaveof参数
void rewriteConfigSlaveofOption(struct rewriteConfigState *state, char *option) {
sds line;
/* If this is a master, we want all the slaveof config options
* in the file to be removed. Note that if this is a cluster instance
* we don't want a slaveof directive inside redis.conf. */
如果这是主服务器,我们希望删除文件中的所有slaveof配置选项。
注意,如果这是一个集群实例,我们不希望redis.conf中有slaveof指令(非主从模式)
if (server.cluster_enabled || server.masterhost == NULL) { 是集群模式 或者是主服务器
rewriteConfigMarkAsProcessed(state,option); 标记处理过,不需要重写配置
return;
}
line = sdscatprintf(sdsempty(),"%s %s %d", option,
server.masterhost, server.masterport);
rewriteConfigRewriteLine(state,option,line,1); 否则就需要重写配置
}
***************************************************************************************************
/* Rewrite the notify-keyspace-events option. */
重写键事情通知参数
void rewriteConfigNotifykeyspaceeventsOption(struct rewriteConfigState *state) {
int force = server.notify_keyspace_events != 0; 表示不为0时需要强制设置
char *option = "notify-keyspace-events";
sds line, flags;
flags = keyspaceEventsFlagsToString(server.notify_keyspace_events); 将事件的标志转化为字符串
line = sdsnew(option);
line = sdscatlen(line, " ", 1);
line = sdscatrepr(line, flags, sdslen(flags));
sdsfree(flags);
rewriteConfigRewriteLine(state,option,line,force);
}
***************************************************************************************************
/* Rewrite the client-output-buffer-limit option. */
重写客户端输入缓存限制参数
void rewriteConfigClientoutputbufferlimitOption(struct rewriteConfigState *state) {
int j;
char *option = "client-output-buffer-limit";
for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++) {
int force = (server.client_obuf_limits[j].hard_limit_bytes !=
clientBufferLimitsDefaults[j].hard_limit_bytes) ||
(server.client_obuf_limits[j].soft_limit_bytes !=
clientBufferLimitsDefaults[j].soft_limit_bytes) ||
(server.client_obuf_limits[j].soft_limit_seconds !=
clientBufferLimitsDefaults[j].soft_limit_seconds); 存在不一样就需要重写
sds line;
char hard[64], soft[64];
rewriteConfigFormatMemory(hard,sizeof(hard),
server.client_obuf_limits[j].hard_limit_bytes); 总量
rewriteConfigFormatMemory(soft,sizeof(soft),
server.client_obuf_limits[j].soft_limit_bytes); 区间量
char *typename = getClientTypeName(j);
if (!strcmp(typename,"slave")) typename = "replica"; slave 和 replica 表示同样的意思
line = sdscatprintf(sdsempty(),"%s %s %s %s %ld",
option, typename, hard, soft,
(long) server.client_obuf_limits[j].soft_limit_seconds); 区间(即时间的长度)
rewriteConfigRewriteLine(state,option,line,force);
}
}
***************************************************************************************************
/* Rewrite the bind option. */ 重写绑定地址参数
void rewriteConfigBindOption(struct rewriteConfigState *state) {
int force = 1;
sds line, addresses;
char *option = "bind";
/* Nothing to rewrite if we don't have bind addresses. */
如果没有绑定地址就不需要重写
if (server.bindaddr_count == 0) {
rewriteConfigMarkAsProcessed(state,option); 只需要标记处理过即可
return;
}
/* Rewrite as bind <addr1> <addr2> ... <addrN> */ 重写如下 bind 地址1 地址2...地址N
addresses = sdsjoin(server.bindaddr,server.bindaddr_count," "); 拼接数组中的地址
line = sdsnew(option);
line = sdscatlen(line, " ", 1);
line = sdscatsds(line, addresses);
sdsfree(addresses);
rewriteConfigRewriteLine(state,option,line,force); 重写配置参数
}
***************************************************************************************************
/* Rewrite the requirepass option. */ 重写 默认用户的密码 参数
void rewriteConfigRequirepassOption(struct rewriteConfigState *state, char *option) {
int force = 1;
sds line;
sds password = server.requirepass; 原密码
/* If there is no password set, we don't want the requirepass option
* to be present in the configuration at all. */
如果没有设置密码,我们根本不希望配置中出现requirepass选项
if (password == NULL) {
rewriteConfigMarkAsProcessed(state,option); 标记已处理,跳过
return;
}
line = sdsnew(option);
line = sdscatlen(line, " ", 1);
line = sdscatsds(line, password);
rewriteConfigRewriteLine(state,option,line,force); 重写配置参数
}
***************************************************************************************************
/* Glue together the configuration lines in the current configuration
* rewrite state into a single string, stripping multiple empty lines. */
将当前配置重写状态下的配置行粘合到一个字符串中,去掉多行的空行(只保留一行)
sds rewriteConfigGetContentFromState(struct rewriteConfigState *state) {
sds content = sdsempty();
int j, was_empty = 0;
for (j = 0; j < state->numlines; j++) {
/* Every cluster of empty lines is turned into a single empty line. */
多个连续空行变成一个空行
if (sdslen(state->lines[j]) == 0) {
if (was_empty) continue;
was_empty = 1;
} else {
was_empty = 0;
}
content = sdscatsds(content,state->lines[j]);
content = sdscatlen(content,"\n",1);设置换行符
}
return content;
}
***************************************************************************************************
/* Free the configuration rewrite state. */ 释放配置重写状态
void rewriteConfigReleaseState(struct rewriteConfigState *state) {
sdsfreesplitres(state->lines,state->numlines); 释放数组
dictRelease(state->option_to_line); 释放字典
dictRelease(state->rewritten);
zfree(state); 释放结构本身
}
***************************************************************************************************
/* At the end of the rewrite process the state contains the remaining
* map between "option name" => "lines in the original config file".
* Lines used by the rewrite process were removed by the function
* rewriteConfigRewriteLine(), all the other lines are "orphaned" and
* should be replaced by empty lines.
在重写过程结束时,重写配置状态 包含“选项参数”=>“原始配置文件中的行”之间的剩余映射。
重写过程使用的行已被函数rewriteConfigRewriteLine删除,所有其他的行均为“孤立”行,应替换为空行
* This function does just this, iterating all the option names and
* blanking all the lines still associated. */
这个函数完成如下内容,迭代所有选项参数,并清空所有关联的行
void rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) {
dictIterator *di = dictGetIterator(state->option_to_line); 获取参数项和文件中的行号映射
dictEntry *de;
while((de = dictNext(di)) != NULL) {
list *l = dictGetVal(de); 参数对应的值
sds option = dictGetKey(de);参数项
/* Don't blank lines about options the rewrite process
* don't understand. */ 对于重写过程中不理解的行,不置空
if (dictFind(state->rewritten,option) == NULL) {
serverLog(LL_DEBUG,"Not rewritten option: %s", option);
continue;
}
while(listLength(l)) { 遍历参数对应的值
listNode *ln = listFirst(l); 第一个元素
int linenum = (long) ln->value; 获取行号
sdsfree(state->lines[linenum]); 释放该行号对应的内容
state->lines[linenum] = sdsempty();置空
listDelNode(l,ln); 删除头节点
}
}
dictReleaseIterator(di);
}
***************************************************************************************************
/* This function overwrites the old configuration file with the new content.
这个函数使用新的配置内容修改旧的配置
* 1) The old file length is obtained.
获取旧文件的长度
* 2) If the new content is smaller, padding is added.
如果新内容较小,则添加填充
* 3) A single write(2) call is used to replace the content of the file.
调用一次write(2)函数用于替换文件的内容
* 4) Later the file is truncated to the length of the new content.
稍后,该文件将被截断为新内容的长度
* This way we are sure the file is left in a consistent state even if the
* process is stopped between any of the four operations.
这样,即使进程在四个操作之间停止,我们也可以确保文件保持一致状态
* The function returns 0 on success, otherwise -1 is returned and errno
* set accordingly. */
这个函数成功返回0,失败返回-1.并且会根据情况设置错误值
int rewriteConfigOverwriteFile(char *configfile, sds content) {
int retval = 0;
int fd = open(configfile,O_RDWR|O_CREAT,0644); 打开一个文件
int content_size = sdslen(content), padding = 0;
struct stat sb;
sds content_padded;
/* 1) Open the old file (or create a new one if it does not
* exist), get the size. */ 打开一个文件(如果没有就新建一个文件),获取文件大小
if (fd == -1) return -1; /* errno set by open(). */
if (fstat(fd,&sb) == -1) { 获取文件大小,如果失败
close(fd);
return -1; /* errno set by fstat(). */ 错误信息由函数stat设置
}
/* 2) Pad the content at least match the old file size. */ 填充内容至少和原文件一样大小
content_padded = sdsdup(content);
if (content_size < sb.st_size) { 如果内容大小小于原文件大小
/* If the old file was bigger, pad the content with
* a newline plus as many "#" chars as required. */
如果原文件更大,填充一个由足够多的符号#组成的新行
padding = sb.st_size - content_size; 总填充个数
content_padded = sdsgrowzero(content_padded,sb.st_size); 给content_padded增加空间
content_padded[content_size] = '\n';换行
memset(content_padded+content_size+1,'#',padding-1); 剩余的个数由#填充
}
/* 3) Write the new content using a single write(2). */ 通过函数write将新内容写入到文件中
if (write(fd,content_padded,strlen(content_padded)) == -1) {
retval = -1;
goto cleanup;
}
/* 4) Truncate the file to the right length if we used padding. */
如果我们有添加,需要截断回到原文件长度
if (padding) {
if (ftruncate(fd,content_size) == -1) { 截取出现问题
/* Non critical error... */ 非致命错误,就是多了一行#号
}
}
cleanup:
sdsfree(content_padded); 释放内存
close(fd); 释放文件句柄
return retval;
}
***************************************************************************************************
/* Rewrite the configuration file at "path".
重写在路径下的配置文件
* If the configuration file already exists, we try at best to retain comments
* and overall structure.
如果配置文件已经存在,我们尝试最好的保持原来注释和总体结构
* Configuration parameters that are at their default value, unless already
* explicitly included in the old configuration file, are not rewritten.
除非已明确包含在旧配置文件中,否则不会重写处于默认值的配置参数
* On error -1 is returned and errno is set accordingly, otherwise 0. */
在出现错误时,返回-1并相应地设置errno,否则返回0
int rewriteConfig(char *path) {
struct rewriteConfigState *state;
sds newcontent;
int retval;
/* Step 1: read the old config into our rewrite state. */ 读取原配置文件到我们的重写状态结构
if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1;
/* Step 2: rewrite every single option, replacing or appending it inside
* the rewrite state. */
重写每个选项,在“重写”状态中替换或附加它
/* Iterate the configs that are standard */ 迭代标准配置,写入到新配置
for (standardConfig *config = configs; config->name != NULL; config++) {
config->interface.rewrite(config->data, config->name, state);
}
非标准参数的写入
rewriteConfigBindOption(state);
rewriteConfigOctalOption(state,"unixsocketperm",server.unixsocketperm,CONFIG_DEFAULT_UNIX_SOCKET_PERM);
rewriteConfigStringOption(state,"logfile",server.logfile,CONFIG_DEFAULT_LOGFILE);
rewriteConfigSaveOption(state);
rewriteConfigUserOption(state);
rewriteConfigDirOption(state);
rewriteConfigSlaveofOption(state,"replicaof");
rewriteConfigRequirepassOption(state,"requirepass");
rewriteConfigBytesOption(state,"client-query-buffer-limit",server.client_max_querybuf_len,PROTO_MAX_QUERYBUF_LEN);
rewriteConfigStringOption(state,"cluster-config-file",server.cluster_configfile,CONFIG_DEFAULT_CLUSTER_CONFIG_FILE);
rewriteConfigNotifykeyspaceeventsOption(state);
rewriteConfigClientoutputbufferlimitOption(state);
/* Rewrite Sentinel config if in Sentinel mode. */ 如果是集群模式,写入集群配置
if (server.sentinel_mode) rewriteConfigSentinelOption(state);
/* Step 3: remove all the orphaned lines in the old file, that is, lines
* that were used by a config option and are no longer used, like in case
* of multiple "save" options or duplicated options. */
删除旧文件中的所有孤立行,即配置文件中原来使用的行,但是现在不再使用,例如 在多个“保存”选项或重复选项的情况下
rewriteConfigRemoveOrphaned(state);
/* Step 4: generate a new configuration file from the modified state
* and write it into the original file. */
从修改后的状态生成新的配置文件,并将其写入原始文件
newcontent = rewriteConfigGetContentFromState(state); 拼接新配置文件内容
retval = rewriteConfigOverwriteFile(server.configfile,newcontent); 覆盖旧配置文件
sdsfree(newcontent);
rewriteConfigReleaseState(state);
return retval;
}
***************************************************************************************************
/*-----------------------------------------------------------------------------
* Configs that fit one of the major types and require no special handling
配置适合其中一种主要类型且无需特殊处理的
*----------------------------------------------------------------------------*/
#define LOADBUF_SIZE 256
static char loadbuf[LOADBUF_SIZE];
#define MODIFIABLE_CONFIG 1
#define IMMUTABLE_CONFIG 0
设置 名字 别名 是否可以修改
#define embedCommonConfig(config_name, config_alias, is_modifiable) \
.name = (config_name), \
.alias = (config_alias), \
.modifiable = (is_modifiable),
设置 相关处理函数
#define embedConfigInterface(initfn, setfn, getfn, rewritefn) .interface = { \
.init = (initfn), \
.set = (setfn), \
.get = (getfn), \
.rewrite = (rewritefn) \
},
/* What follows is the generic config types that are supported. To add a new
* config with one of these types, add it to the standardConfig table with
* the creation macro for each type.
下面是受支持的通用配置类型。要使用这些类型之一添加新配置,请使用每种类型的创建宏将其添加到standardConfig表中
* Each type contains the following:
每种类型包含如下内容:
* * A function defining how to load this type on startup.
* * A function defining how to update this type on CONFIG SET.
* * A function defining how to serialize this type on CONFIG SET.
* * A function defining how to rewrite this type on CONFIG REWRITE.
* * A Macro defining how to create this type.
*/
**定义如何在启动时加载此类型的函数。
**定义如何在配置集上更新此类型的函数。
**定义如何在配置集中序列化此类型的函数。
**定义如何在配置重写时重写此类型的函数。
**定义如何创建此类型的宏
***************************************************************************************************
/* Bool Configs */ 布尔配置
初始化默认值
static void boolConfigInit(typeData data) {
*data.yesno.config = data.yesno.default_value;
}
***************************************************************************************************
设置布尔变量
static int boolConfigSet(typeData data, sds value, int update, char **err) {
int yn = yesnotoi(value);
if (yn == -1) {
*err = "argument must be 'yes' or 'no'";
return 0;
}
if (data.yesno.is_valid_fn && !data.yesno.is_valid_fn(yn, err)) 存在有效判别函数 并且判别为假
return 0;
int prev = *(data.yesno.config);
*(data.yesno.config) = yn;
if (update && data.yesno.update_fn && !data.yesno.update_fn(yn, prev, err)) { 存在更新函数,更新失败
*(data.yesno.config) = prev; 恢复原值
return 0;
}
return 1;
}
***************************************************************************************************
获取配置的布尔值
static void boolConfigGet(client *c, typeData data) {
addReplyBulkCString(c, *data.yesno.config ? "yes" : "no");
}
***************************************************************************************************
设置配置的布尔值
static void boolConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {
rewriteConfigYesNoOption(state, name,*(data.yesno.config), data.yesno.default_value);
}
***************************************************************************************************
创建布尔值
#define createBoolConfig(name, alias, modifiable, config_addr, default, is_valid, update) { \
embedCommonConfig(name, alias, modifiable) \
embedConfigInterface(boolConfigInit, boolConfigSet, boolConfigGet, boolConfigRewrite) \
.data.yesno = { \
.config = &(config_addr), \
.default_value = (default), \
.is_valid_fn = (is_valid), \
.update_fn = (update), \
} \
}
***************************************************************************************************
/* String Configs */ 字符串配置
初始化字符串
static void stringConfigInit(typeData data) {
if (data.string.convert_empty_to_null) { 空字符串要转NULL
*data.string.config = data.string.default_value ? zstrdup(data.string.default_value) : NULL;
} else {
*data.string.config = zstrdup(data.string.default_value);
}
}
***************************************************************************************************
static int stringConfigSet(typeData data, sds value, int update, char **err) {
if (data.string.is_valid_fn && !data.string.is_valid_fn(value, err)) 存在有效判别函数 并且判别为假
return 0;
char *prev = *data.string.config;
if (data.string.convert_empty_to_null) { 空字符串要转NULL
*data.string.config = value[0] ? zstrdup(value) : NULL;
} else {
*data.string.config = zstrdup(value);
}
if (update && data.string.update_fn && !data.string.update_fn(*data.string.config, prev, err)) {
存在更新函数,更新失败
zfree(*data.string.config);
*data.string.config = prev;
return 0;
}
zfree(prev);
return 1;
}
***************************************************************************************************
获取字符串配置内容
static void stringConfigGet(client *c, typeData data) {
addReplyBulkCString(c, *data.string.config ? *data.string.config : "");
}
***************************************************************************************************
重写字符串配置
static void stringConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {
rewriteConfigStringOption(state, name,*(data.string.config), data.string.default_value);
}
***************************************************************************************************
#define ALLOW_EMPTY_STRING 0
#define EMPTY_STRING_IS_NULL 1
创建字符串配置
#define createStringConfig(name, alias, modifiable, empty_to_null, config_addr, default, is_valid, update) { \
embedCommonConfig(name, alias, modifiable) \
embedConfigInterface(stringConfigInit, stringConfigSet, stringConfigGet, stringConfigRewrite) \
.data.string = { \
.config = &(config_addr), \
.default_value = (default), \
.is_valid_fn = (is_valid), \
.update_fn = (update), \
.convert_empty_to_null = (empty_to_null), \
} \
}
***************************************************************************************************
/* Enum configs */ 枚举配置
初始化枚举值
static void enumConfigInit(typeData data) {
*data.enumd.config = data.enumd.default_value;
}
static int enumConfigSet(typeData data, sds value, int update, char **err) {
int enumval = configEnumGetValue(data.enumd.enum_value, value);
if (enumval == INT_MIN) {
sds enumerr = sdsnew("argument must be one of the following: ");
configEnum *enumNode = data.enumd.enum_value;
while(enumNode->name != NULL) { 拼接存在的所有枚举值
enumerr = sdscatlen(enumerr, enumNode->name,
strlen(enumNode->name));
enumerr = sdscatlen(enumerr, ", ", 2);
enumNode++;
}
sdsrange(enumerr,0,-3); /* Remove final ", ". */ 去掉最后的分格符号
strncpy(loadbuf, enumerr, LOADBUF_SIZE); 按照给定的长度截断
loadbuf[LOADBUF_SIZE - 1] = '\0';
sdsfree(enumerr);
*err = loadbuf;
return 0;
}
if (data.enumd.is_valid_fn && !data.enumd.is_valid_fn(enumval, err))
return 0;
int prev = *(data.enumd.config);
*(data.enumd.config) = enumval;
if (update && data.enumd.update_fn && !data.enumd.update_fn(enumval, prev, err)) {
*(data.enumd.config) = prev;
return 0;
}
return 1;
}
***************************************************************************************************
static void enumConfigGet(client *c, typeData data) {
addReplyBulkCString(c, configEnumGetNameOrUnknown(data.enumd.enum_value,*data.enumd.config));
}
***************************************************************************************************
static void enumConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {
rewriteConfigEnumOption(state, name,*(data.enumd.config), data.enumd.enum_value, data.enumd.default_value);
}
***************************************************************************************************
#define createEnumConfig(name, alias, modifiable, enum, config_addr, default, is_valid, update) { \
embedCommonConfig(name, alias, modifiable) \
embedConfigInterface(enumConfigInit, enumConfigSet, enumConfigGet, enumConfigRewrite) \
.data.enumd = { \
.config = &(config_addr), \
.default_value = (default), \
.is_valid_fn = (is_valid), \
.update_fn = (update), \
.enum_value = (enum), \
} \
}
***************************************************************************************************
/* Gets a 'long long val' and sets it into the union, using a macro to get
* compile time type check. */
获取一个长整型的值,根据其值赋值到联合体中,使用宏获取编译时类型检查
#define SET_NUMERIC_TYPE(val) \
if (data.numeric.numeric_type == NUMERIC_TYPE_INT) { \
*(data.numeric.config.i) = (int) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_UINT) { \
*(data.numeric.config.ui) = (unsigned int) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG) { \
*(data.numeric.config.l) = (long) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG) { \
*(data.numeric.config.ul) = (unsigned long) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG_LONG) { \
*(data.numeric.config.ll) = (long long) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG_LONG) { \
*(data.numeric.config.ull) = (unsigned long long) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_SIZE_T) { \
*(data.numeric.config.st) = (size_t) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_SSIZE_T) { \
*(data.numeric.config.sst) = (ssize_t) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_OFF_T) { \
*(data.numeric.config.ot) = (off_t) val; \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_TIME_T) { \
*(data.numeric.config.tt) = (time_t) val; \
}
***************************************************************************************************
/* Gets a 'long long val' and sets it with the value from the union, using a
* macro to get compile time type check. */
获取一个长整型的值,并使用联合体中的值设置它,使用宏获取编译时类型检查
#define GET_NUMERIC_TYPE(val) \
if (data.numeric.numeric_type == NUMERIC_TYPE_INT) { \
val = *(data.numeric.config.i); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_UINT) { \
val = *(data.numeric.config.ui); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG) { \
val = *(data.numeric.config.l); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG) { \
val = *(data.numeric.config.ul); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG_LONG) { \
val = *(data.numeric.config.ll); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG_LONG) { \
val = *(data.numeric.config.ull); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_SIZE_T) { \
val = *(data.numeric.config.st); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_SSIZE_T) { \
val = *(data.numeric.config.sst); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_OFF_T) { \
val = *(data.numeric.config.ot); \
} else if (data.numeric.numeric_type == NUMERIC_TYPE_TIME_T) { \
val = *(data.numeric.config.tt); \
}
***************************************************************************************************
/* Numeric configs */ 数值配置
static void numericConfigInit(typeData data) {
SET_NUMERIC_TYPE(data.numeric.default_value)
}
***************************************************************************************************
数值类型边界检测
static int numericBoundaryCheck(typeData data, long long ll, char **err) {
if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG_LONG ||
data.numeric.numeric_type == NUMERIC_TYPE_UINT ||
data.numeric.numeric_type == NUMERIC_TYPE_SIZE_T) {
/* Boundary check for unsigned types */ 对无符号数进行边界检测
unsigned long long ull = ll;
unsigned long long upper_bound = data.numeric.upper_bound;
unsigned long long lower_bound = data.numeric.lower_bound;
if (ull > upper_bound || ull < lower_bound) {
snprintf(loadbuf, LOADBUF_SIZE,
"argument must be between %llu and %llu inclusive",
lower_bound,
upper_bound);
*err = loadbuf;
return 0;
}
} else {
/* Boundary check for signed types */ 检测有符号数的边界
if (ll > data.numeric.upper_bound || ll < data.numeric.lower_bound) {
snprintf(loadbuf, LOADBUF_SIZE,
"argument must be between %lld and %lld inclusive",
data.numeric.lower_bound,
data.numeric.upper_bound);
*err = loadbuf;
return 0;
}
}
return 1;
}
***************************************************************************************************
设置配置的数值参数
static int numericConfigSet(typeData data, sds value, int update, char **err) {
long long ll, prev = 0;
if (data.numeric.is_memory) { 是内存类型表示
int memerr;
ll = memtoll(value, &memerr);
if (memerr || ll < 0) {
*err = "argument must be a memory value";
return 0;
}
} else { 字符串表示
if (!string2ll(value, sdslen(value),&ll)) {
*err = "argument couldn't be parsed into an integer" ;
return 0;
}
}
if (!numericBoundaryCheck(data, ll, err)) 边界检测
return 0;
if (data.numeric.is_valid_fn && !data.numeric.is_valid_fn(ll, err)) 有效函数检测
return 0;
GET_NUMERIC_TYPE(prev)
SET_NUMERIC_TYPE(ll)
if (update && data.numeric.update_fn && !data.numeric.update_fn(ll, prev, err)) { 更新函数检测
SET_NUMERIC_TYPE(prev) 失败设置回原值
return 0;
}
return 1;
}
***************************************************************************************************
获取配置数值类型内容
static void numericConfigGet(client *c, typeData data) {
char buf[128];
long long value = 0;
GET_NUMERIC_TYPE(value)
ll2string(buf, sizeof(buf), value); 数转字符串
addReplyBulkCString(c, buf);
}
***************************************************************************************************
重写数值类型的配置
static void numericConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {
long long value = 0;
GET_NUMERIC_TYPE(value)
if (data.numeric.is_memory) {
rewriteConfigBytesOption(state, name, value, data.numeric.default_value);
} else {
rewriteConfigNumericalOption(state, name, value, data.numeric.default_value);
}
}
***************************************************************************************************
#define INTEGER_CONFIG 0
#define MEMORY_CONFIG 1
#define embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) { \
embedCommonConfig(name, alias, modifiable) \
embedConfigInterface(numericConfigInit, numericConfigSet, numericConfigGet, numericConfigRewrite) \
.data.numeric = { \
.lower_bound = (lower), \
.upper_bound = (upper), \
.default_value = (default), \
.is_valid_fn = (is_valid), \
.update_fn = (update), \
.is_memory = (memory),
***************************************************************************************************
#define createIntConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_INT, \
.config.i = &(config_addr) \
} \
}
***************************************************************************************************
#define createUIntConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_UINT, \
.config.ui = &(config_addr) \
} \
}
***************************************************************************************************
#define createLongConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_LONG, \
.config.l = &(config_addr) \
} \
}
***************************************************************************************************
#define createULongConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_ULONG, \
.config.ul = &(config_addr) \
} \
}
***************************************************************************************************
#define createLongLongConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_LONG_LONG, \
.config.ll = &(config_addr) \
} \
}
#define createULongLongConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_ULONG_LONG, \
.config.ull = &(config_addr) \
} \
}
***************************************************************************************************
#define createSizeTConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_SIZE_T, \
.config.st = &(config_addr) \
} \
}
***************************************************************************************************
#define createSSizeTConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_SSIZE_T, \
.config.sst = &(config_addr) \
} \
}
***************************************************************************************************
#define createTimeTConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_TIME_T, \
.config.tt = &(config_addr) \
} \
}
***************************************************************************************************
#define createOffTConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
embedCommonNumericalConfig(name, alias, modifiable, lower, upper, config_addr, default, memory, is_valid, update) \
.numeric_type = NUMERIC_TYPE_OFF_T, \
.config.ot = &(config_addr) \
} \
}
***************************************************************************************************
是否存在在线磁盘整理函数
static int isValidActiveDefrag(int val, char **err) {
#ifndef HAVE_DEFRAG
if (val) {
*err = "Active defragmentation cannot be enabled: it "
"requires a Redis server compiled with a modified Jemalloc "
"like the one shipped by default with the Redis source "
"distribution";
return 0;
}
#else
UNUSED(val);
UNUSED(err);
#endif
return 1;
}
***************************************************************************************************
是否存在有效db文件
static int isValidDBfilename(char *val, char **err) {
if (!pathIsBaseName(val)) {
*err = "dbfilename can't be a path, just a filename";
return 0;
}
return 1;
}
***************************************************************************************************
是否存在有效AOF文件
static int isValidAOFfilename(char *val, char **err) {
if (!pathIsBaseName(val)) {
*err = "appendfilename can't be a path, just a filename";
return 0;
}
return 1;
}
***************************************************************************************************
更新中断频率
static int updateHZ(long long val, long long prev, char **err) {
UNUSED(prev);
UNUSED(err);
/* Hz is more an hint from the user, so we accept values out of range
* but cap them to reasonable values. */
Hz更多的是来自用户的提示,因此我们接受超出范围的值,但将其限制为合理值
server.config_hz = val;
if (server.config_hz < CONFIG_MIN_HZ) server.config_hz = CONFIG_MIN_HZ;
if (server.config_hz > CONFIG_MAX_HZ) server.config_hz = CONFIG_MAX_HZ;
server.hz = server.config_hz;
return 1;
}
***************************************************************************************************
更新为后台线程
static int updateJemallocBgThread(int val, int prev, char **err) {
UNUSED(prev);
UNUSED(err);
set_jemalloc_bg_thread(val);
return 1;
}
***************************************************************************************************
更新repl_backlog_size的大小
设置backlog的大小,backlog是一个缓冲区,在slave端失连时存放要同步到slave的数据
static int updateReplBacklogSize(long long val, long long prev, char **err) {
/* resizeReplicationBacklog sets server.repl_backlog_size, and relies on
* being able to tell when the size changes, so restore prev becore calling it. */
UNUSED(err);
server.repl_backlog_size = prev;
resizeReplicationBacklog(val);
return 1;
}
***************************************************************************************************
更新最大的内存
static int updateMaxmemory(long long val, long long prev, char **err) {
UNUSED(prev);
UNUSED(err);
if (val) {
size_t used = zmalloc_used_memory()-freeMemoryGetNotCountedMemory();
if ((unsigned long long)val < used) {
serverLog(LL_WARNING,"WARNING: the new maxmemory value set via CONFIG SET (%llu) is smaller than the current memory usage (%zu). This will result in key eviction and/or the inability to accept new write commands depending on the maxmemory-policy.", server.maxmemory, used);
}
freeMemoryIfNeededAndSafe();
}
return 1;
}
***************************************************************************************************
更新活着的从服务器
static int updateGoodSlaves(long long val, long long prev, char **err) {
UNUSED(val);
UNUSED(prev);
UNUSED(err);
refreshGoodSlavesCount();
return 1;
}
***************************************************************************************************
更新添加模式
static int updateAppendonly(int val, int prev, char **err) {
UNUSED(prev);
if (val == 0 && server.aof_state != AOF_OFF) { 不允许添加模式AOF
stopAppendOnly();
} else if (val && server.aof_state == AOF_OFF) {
if (startAppendOnly() == C_ERR) { 开始添加模式AOF失败
*err = "Unable to turn on AOF. Check server logs.";
return 0;
}
}
return 1;
}
***************************************************************************************************
更新最大客户端数目
static int updateMaxclients(long long val, long long prev, char **err) {
/* Try to check if the OS is capable of supporting so many FDs. */
尝试检查操作系统是否能够支持如此多的文件句柄。
if (val > prev) {
adjustOpenFilesLimit();
if (server.maxclients != val) {
static char msg[128];
sprintf(msg, "The operating system is not able to handle the specified number of clients, try with %d", server.maxclients);
*err = msg;
if (server.maxclients > prev) {
server.maxclients = prev;
adjustOpenFilesLimit();
}
return 0;
}
if ((unsigned int) aeGetSetSize(server.el) <
server.maxclients + CONFIG_FDSET_INCR)
{ 当前ae监控事件小于新要求的值,尝试设置新配置的值
if (aeResizeSetSize(server.el,
server.maxclients + CONFIG_FDSET_INCR) == AE_ERR)
{
*err = "The event loop API used by Redis is not able to handle the specified number of clients";
return 0;
}
}
}
return 1;
}
***************************************************************************************************
#ifdef USE_OPENSSL 使用ssl
更新tls的配置参数
static int updateTlsCfg(char *val, char *prev, char **err) {
UNUSED(val);
UNUSED(prev);
UNUSED(err);
if (tlsConfigure(&server.tls_ctx_config) == C_ERR) {
*err = "Unable to configure tls-cert-file. Check server logs.";
return 0;
}
return 1;
}
更新TLS参数的布尔值
static int updateTlsCfgBool(int val, int prev, char **err) {
UNUSED(val);
UNUSED(prev);
return updateTlsCfg(NULL, NULL, err);
}
#endif /* USE_OPENSSL */
***************************************************************************************************
默认的初始化内容
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, MODIFIABLE_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("rdb-del-sync-files", NULL, MODIFIABLE_CONFIG, server.rdb_del_sync_files, 0, 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("lazyfree-lazy-user-del", NULL, MODIFIABLE_CONFIG, server.lazyfree_lazy_user_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),
createBoolConfig("cluster-allow-reads-when-down", NULL, MODIFIABLE_CONFIG, server.cluster_allow_reads_when_down, 0, NULL, NULL),
/* 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),
createStringConfig("server_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.server_cpulist, NULL, NULL, NULL),
createStringConfig("bio_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.bio_cpulist, NULL, NULL, NULL),
createStringConfig("aof_rewrite_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.aof_rewrite_cpulist, NULL, NULL, NULL),
createStringConfig("bgsave_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.bgsave_cpulist, NULL, NULL, 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, 128, 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, IMMUTABLE_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("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),
createULongConfig("acllog-max-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.acllog_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, LLONG_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, LLONG_MAX, server.stream_node_max_entries, 100, INTEGER_CONFIG, NULL, NULL),
createLongLongConfig("repl-backlog-size", NULL, MODIFIABLE_CONFIG, 1, LLONG_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, LLONG_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),
createSizeTConfig("tracking-table-max-keys", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, server.tracking_table_max_keys, 1000000, INTEGER_CONFIG, NULL, NULL), /* Default: 1 million keys max. */
/* 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}
};
/*-----------------------------------------------------------------------------
* CONFIG command entry point 配置命令入口点
*----------------------------------------------------------------------------*/
void configCommand(client *c) {
/* Only allow CONFIG GET while loading. */ 加载时仅允许配置获取
if (server.loading && strcasecmp(c->argv[1]->ptr,"get")) {
addReplyError(c,"Only CONFIG GET is allowed during loading");
return;
}
if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) { 展示帮助内容
const char *help[] = {
"GET <pattern> -- Return parameters matching the glob-like <pattern> and their values.",
"SET <parameter> <value> -- Set parameter to value.",
"RESETSTAT -- Reset statistics reported by INFO.",
"REWRITE -- Rewrite the configuration file.",
NULL
};
addReplyHelp(c, help);
} else if (!strcasecmp(c->argv[1]->ptr,"set") && c->argc == 4) { 设置配置参数
configSetCommand(c);
} else if (!strcasecmp(c->argv[1]->ptr,"get") && c->argc == 3) { 获取配置参数
configGetCommand(c);
} else if (!strcasecmp(c->argv[1]->ptr,"resetstat") && c->argc == 2) { 重置配置参数
resetServerStats();
resetCommandTableStats();
addReply(c,shared.ok);
} else if (!strcasecmp(c->argv[1]->ptr,"rewrite") && c->argc == 2) { 重写配置参数
if (server.configfile == NULL) {
addReplyError(c,"The server is running without a config file");
return;
}
if (rewriteConfig(server.configfile) == -1) {
serverLog(LL_WARNING,"CONFIG REWRITE failed: %s", strerror(errno));
addReplyErrorFormat(c,"Rewriting config file: %s", strerror(errno));
} else {
serverLog(LL_WARNING,"CONFIG REWRITE executed with success.");
addReply(c,shared.ok);
}
} else { 其它情况,格式不对或不支持
addReplySubcommandSyntaxError(c);
return;
}
}
************************************************************************************************************