Redis source code parsing (16) --- config configuration file

Source: Internet
Author: User

Every system will have a Config configuration file similar to that in the config file. All the content in the config file knows that it must be a fixed row of attribute code. Today we are looking at the config attribute in redis code, there are indeed a lot of things that have been pulled down, with a visual view of about 50 to 100 attributes. If each attribute in config is not my style, it must be boring. Therefore, my feature is to understand the ideas of programmers when writing this type of code in the code, and highlights in the code. We know that redis runs in many environments, such as Windows, Linux, and Mac OS. different operating systems, of course, cannot support some attributes, so config in redis. h The header file is pre-processed with the computer's operating system. For example, on Linux and other systems, you can modify the process name:

/* Check if we can use setproctitle (). * BSD systems have support for it, we provide an implementation for * Linux and OSX. * // * check whether setproctitle () can be called. This method is used to modify the process name in Linux * // * determine whether it is a BSD system, BSD is the abbreviation of Berkeley systems distrobution, is a UNIX version */# If (defined _ NetBSD _ | defined _ FreeBSD _ | defined _ OpenBSD __) # define use_setproctitle # endif # If (defined _ Linux | defined _ apple _) # define use_setproctitle # define extends spt_init (INT argc, char * argv []); void setproctitle (const char * FMT ,...); # endif
Of course, this is just one point I mentioned. That is to say, redis still has a very thorough consideration in processing heterogeneous systems. Let's take a look at the main config operation files. First, let's look at some of the APIS:

/* Config file API */INT yesnotoi (char * s)/* determines whether the character is yes */void appendserversaveparams (time_t seconds, int changes) /* append the server save parameter */void resetserversaveparams (void)/* reset the Save parameter of the server, that is, release serverparams */void loadserverconfigfromstring (char * config) of the server) /* load server attribute configuration from string */void loadserverconfig (char * filename, char * options)/* load server configuration from file */void configsetcommand (redisclient * C) /* Configure the server according to the parameters in redisclient */# define config_get_string_field (_ name, _ var)/* macro defines the method for obtaining the string value range */# define config_get_bool_field (_ name, _ var)/* macro defines the method to obtain the Boolean value range. The value here is yes or no */# define config_get_numerical_field (_ name, _ var) /* macro defines the method for obtaining numeric value ranges */void configgetcommand (redisclient * C)/* command for obtaining configuration information, how to replay the client */void rewriteconfigappendline (struct rewriteconfigstate * state, SDS line)/* Add the configuration string line */void rewriteconfigaddlinenumbertooption (struct rewriteconfigstate * state, SDS option, int linenum)/* Add dictionary line-option */void rewriteconfigmarkasprocessed (struct rewriteconfigstate * state, char * option) /* rewriteconfigstate override option */struct rewriteconfigstate * rewriteconfigreadoldfile (char * path)/* read information about the old configuration file. If the file is unreadable or does not exist, returns NULL */void rewriteconfigrewriteline (struct rewriteconfigstate * state, char * option, SDS line, int force)/* overwrite configline */INT rewriteconfigformatmemory (char * Buf, size_t Len, long long bytes)/* format the byte size display to avoid long-long display */void rewriteconfigbytesoption (struct rewriteconfigstate * state, char * option, long value, long long defvalue)/* write a type of configuration to config. The following methods are similar to */void rewriteconfigyesnooption (struct rewriteconfigstate * state, char * option, int value, int defvalue) /* same as */void rewriteconfigstringoption (struct rewriteconfigstate * state, char * option, char * value, char * defvalue)/* same as */void rewriteconfignumericaloption (struct rewriteconfigstate * state, char * option, long value, long defvalue)/* same as */void rewriteconfigoctaloption (struct rewriteconfigstate * state, char * option, int value, int defvalue) /* same as */void rewriteconfigenumoption (struct rewriteconfigstate * state, char * option, int value ,...) /* same as */void reset (struct rewriteconfigstate * State)/* same as */void rewriteconfigsaveoption (struct rewriteconfigstate * State)/* same as */void rewriteconfigdiroption) /* same as */void rewriteconfigslaveofoption (struct rewriteconfigstate * State)/* same as */void reset (struct rewriteconfigstate * State) /* same as above */void rewriteconfigbindoption (struct rewriteconfigstate * State)/* same as above */SDS rewriteconfiggetcontentfromstate (struct rewriteconfigstate * State) /* Get the configuration information string in consumstate */void rewriteconfigreleasestate (struct rewriteconfigstate * State)/* configstate release space */void rewriteconfigremoveorphaned (struct rewriteconfigstate * State) /* line configuration for empty state */INT rewriteconfigoverwritefile (char * configfile, SDS content)/* write string attribute overwrite source file */INT rewriteconfig (char * path) /* read the current attribute into the file. Step: (1 ). read the current server attribute into configstate (2 ). the configstate attribute is changed to a string (3 ). write the string to the file */void configcommand (redisclient * C)/* client config command call Method */
The number of Apis above is indeed a little scary. I will summarize the main operations in config. C:

1. Read the configuration from the config file to the server attribute.

2. Write the settings of the current server to the configuration.

The other methods above serve the two requirements above. Therefore, redis designs a structure Role named rewriteconfigstate, which stores the string array of attribute configuration. A String Array represents a property setting. For example, to read the current configuration into the configuration file:

Int rewriteconfig (char * path)/* reads the current attribute into the file. Step: (1 ). read the current server attribute into configstate (2 ). the configstate attribute is changed to a string (3 ). write a string to a file */
Let's take a look at the structure of the configstate struct in redis code:

/* The Config rewrite state. */struct rewriteconfigstate {// The option ing dict * option_to_line of Option-line is stored here;/* option-> List of config file lines map */dict * rewritten; /* Dictionary of already processed options * // The number of rows in the current configuration file int numlines;/* number of lines in Current Config * // The current row string SDS * lines; /* current lines as an array of SDS strings */INT has_tail;/* true if we already added ctictives that were not present in the original config file. */};
Lines are specific attributes. The first opition-line ing refers to the row corresponding to the attribute name. The Config File also mentions the concept of "maxmemory", which can be understood as "maximum memory" in Chinese ':

/* 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 ). * // * Several dictionary types are defined below to save some information in the old configuration file, such as historical records, like "maxmemory "*/
That is to say, the old configuration file can exist in the redis file. The old attribute can be read from the old file and written into the new configuration again, so as to record the historical configuration records. The attributes read from the old configuration file also exist in the configstate structure in the middle:

/* 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. * // * read the information of the old configuration file. If the file cannot be read or does not exist, null */struct rewriteconfigstate * rewriteconfigreadoldfile (char * path) {file * fp = fopen (path, "R"); struct rewritec Onfigstate * State = zmalloc (sizeof (* State); char Buf [redis_configline_max + 1]; int linenum =-1; if (FP = NULL & errno! = Enoent) return NULL; State-> option_to_line = dictcreate (& optiontolinedicttype, null); State-> rewritten = dictcreate (& optionsetdicttype, null); State-> numlines = 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, redis_configline_max + 1, FP )! = NULL) {int argc; SDS * argv; SDS line = sdstrim (sdsnew (BUF), "\ r \ n \ t"); linenum ++;/* zero based, so we init at-1 * // * handle comments and empty lines. * // process comments and empty rows 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); // read the old Configuration Attribute into the configstate struct 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); rewriteconfigaddlinenumbertooption (State, argv [0], linenum); sdsfreesplitres (argv, argc);} fclose (FP); // return configstate, it records the configuration line information return state in some old configuration files ;}
The configstate struct is used again. Small config files also have unexpected designs.

Redis source code parsing (16) --- config configuration file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.