MySQL proxy Learning (1) Startup Process
Mysql-proxy relies heavily on libraries including libevent, glib2, and Lua. Therefore, you still need some knowledge to read the code.
1. Main libevent Interfaces
Event_base_new: Initialize an event_base
Event_set: sets event events, that is, initializes the struct event structure: type, file descriptor, callback function, and parameters.
Event_base_set: bind the event set above to the specified event_base. Each event must specify the event_base to which it belongs.
Event_add: registers Event Events.
Event_base_dispatch: blocks an event_base and monitors all FD instances bound to it.
2. MySQL proxy startup
/U01/Xiangzhong/MySQL-proxy/bin/MySQL-proxy -- daemon -- PID-file =/u01/Xiangzhong/MySQL-proxy/proxy. PID -- plugins = proxy -- proxy-address =: 4040 -- log-level = debug -- log-file =/u01/Xiangzhong/MySQL-proxy/proxy. log -- proxy-backend-addresses = 127.0.0.1: 4312 -- proxy-LUA-script =/u01/Xiangzhong/MySQL-proxy/proxy. lua
-- Event-threads = 5
This is one of our startup methods. We can see that there are many startup parameters. The most important among them are proxy-backend-addresses, which indicates the background server of the mysql-proxy.
Proxy-LUA-Script: indicates the callback function of the position it hooks during proxy running, including connect_server, read_handshake, read_auth, read_auth_result, read_query, read_query_result. For more information, see http://dev.mysql.com/doc/refman/5.1/en/mysql-proxy-scripting.html. The following describes the mysql-proxy startup process.
3. mysql-proxy Startup Process
Simply put, MySQL-proxy is a libevent model that supports multiple threads.
1. After mysql-proxy is started, it enters main_cmdline, initializes various structures, parses startup parameters, and so on;
2. chassis_mainloop, which first initializes the corresponding structure of the main thread;
3. load and start all plugins (MySQL-proxy can be composed of multiple INS: Admin and proxy. Here, each component is loaded, that is, the apply_config function of the corresponding component is called, for example, if the proxy-plugin function is callback, this function uses network_mysqld_proxy_connection_init (CON) to initialize the processing functions corresponding to the proxy status. These functions are network_mysqld_plugin_proto (***) the name of the function at the beginning. The *** value is proxy_init and proxy_read_auth.
Script and set the corresponding global tables, network_mysqld_lua_setup_global (Chas-> priv-> SC-> L, g); finally, monitor listen_sock-> FD, event_set (& (listen_sock-> event), listen_sock-> FD, ev_read | ev_persist, network_mysqld_con_accept, con); [The listen FD is monitored by the main thread ])
4. next, create other event_threads and start (the number specified by the startup parameter). chassis_event_threads_init_thread is used to initialize the thread structure, including creating event_base for each thread, then copy chassis_event_threads_t-> event_policy_fds [0] To the chassis_event_thread_t-> policy_fd of each thread, that is, [0] is readable, [1] is writable, and the current thread is responsible for writing, all threads Listen to the [0] readable event event_set (& (event_thread-> policy_fd_event ),
Event_thread-> policy_fd, ev_read | ev_persist, chassis_event_handle, event_thread );
5. All threads enter the main loop chassis_event_thread_loop, and then call event_base_dispatch to listen to their own event_base
That is to say, the main thread listens to two FD: Listen FD, chassis_event_thread_t-> policy_fd; other threads Listen to: chassis_event_thread_t-> policy_fd.