recently began to study Twemproxy first of the knowledge points summarized. As a series of knowledge points.
I. Introduction of TwemproxyTwemproxy is an agent for Memcache and Redis, developed by Twitter and currently open source. Studying this is very helpful for understanding network communication. Highlights include the following: 1.twemproxy you create and maintain a long connection to the backend server (that is, the Reids instance), ensuring that long connections are reused from different clients but to the same server. 2. The server that automatically identifies the exception status ensures that subsequent requests are not forwarded to the exception server. However, if this server returns to normal, Twemproxy can recognize this server and resume normal access. , 3. Provide specialized status monitoring end statements external tools get status monitoring information. 4. Real multi-stage processing multi-request, its IO model is worth learning, using single-threaded packet delivery, based on the Epoll event-driven model. 5, Support zero copy technology, by passing the message pointer between 3 queues.
Second, Twemproxy entrance main function analysis
Twemproxy is written in C, and its overall entry is in the main function.
grep "Main", we can see the main function in the file NC.C. At the beginning we can see that a variable is defined,
struct instance NCI;
Let's look at the definition of this instance:
struct instance { struct context *ctx; int log_level; Char *log_filename; Char *conf_filename; uint16_t Stats_port; int stats_interval; Char *stats_addr; Char Hostname[nc_maxhostnamelen]; size_t mbuf_chunk_size; pid_t pid; Char *pid_filename; unsigned pidfile:1; };
This instance is equivalent to a twemproxy instance, a twemproxy process corresponding to a instance, the whole program after the initialization of many will be used. Note that there is a
The instance corresponds to a context, while a context maintains a list of server pool. This means that a instance can contain more than one server pool.The next call is Nc_set_default_options (), which is the default value used to set the parameters of the instance instance NCI above. Then there is nc_get_options (), which reads the command-line arguments, overwriting the default parameters set by the previous function, if any.
If the-t option is configured, nc_test_conf: Used to check the configuration file to ensure that the configuration file format is correct.
Then call Nc_pre_run, before starting to do some preprocessing, including: Initialize the log level and log file, set whether to run in the background, initialize the signal, whether to create a PID file.
Next, call Nc_run to start the proxy; the function is to call Core_start to create the context and then go into the dead loop call Core_loop start the entire event loop processing, accept the request and process. Of course, Core_start and Core_loop also contain a lot of processing work, including configuration file parsing and reading, initialization of related components (Server_pool,conf,context), and so on, which are described in detail behind them.
In general, the start-up process is these steps, such as:
recently began to study Twemproxy first of the knowledge points summarized. As a series of knowledge points.
I. Introduction of TwemproxyTwemproxy is an agent for Memcache and Redis, developed by Twitter and currently open source. Studying this is very helpful for understanding network communication. Highlights include the following: 1.twemproxy you create and maintain a long connection to the backend server (that is, the Reids instance), ensuring that long connections are reused from different clients but to the same server. 2. The server that automatically identifies the exception status ensures that subsequent requests are not forwarded to the exception server. However, if this server returns to normal, Twemproxy can recognize this server and resume normal access. , 3. Provide specialized status monitoring end statements external tools get status monitoring information. 4. Real multi-stage processing multi-request, its IO model is worth learning, using single-threaded packet delivery, based on the Epoll event-driven model. 5, Support zero copy technology, by passing the message pointer between 3 queues.
Second, Twemproxy entrance main function analysis
Twemproxy is written in C, and its overall entry is in the main function.
grep "Main", we can see the main function in the file NC.C. At the beginning we can see that a variable is defined,
struct instance NCI;
Let's look at the definition of this instance:
struct instance { struct context *ctx; int log_level; Char *log_filename; Char *conf_filename; uint16_t Stats_port; int stats_interval; Char *stats_addr; Char Hostname[nc_maxhostnamelen]; size_t mbuf_chunk_size; pid_t pid; Char *pid_filename; unsigned pidfile:1; };
This instance is equivalent to a twemproxy instance, a twemproxy process corresponding to a instance, the whole program after the initialization of many will be used. Note that there is a
The instance corresponds to a context, while a context maintains a list of server pool. This means that a instance can contain more than one server pool.The next call is Nc_set_default_options (), which is the default value used to set the parameters of the instance instance NCI above. Then there is nc_get_options (), which reads the command-line arguments, overwriting the default parameters set by the previous function, if any.
If the-t option is configured, nc_test_conf: Used to check the configuration file to ensure that the configuration file format is correct.
Then call Nc_pre_run, before starting to do some preprocessing, including: Initialize the log level and log file, set whether to run in the background, initialize the signal, whether to create a PID file.
Next, call Nc_run to start the proxy; the function is to call Core_start to create the context and then go into the dead loop call Core_loop start the entire event loop processing, accept the request and process. Of course, Core_start and Core_loop also contain a lot of processing work, including configuration file parsing and reading, initialization of related components (Server_pool,conf,context), and so on, which are described in detail behind them.
In general, the start-up process is these steps, such as:
The next chapter will focus on Analysis Nc_run the next chapter will focus on analyzing Nc_run
Original: Twemproxy source analysis One of the entry function and startup process