To study Atlas source, you first need to start from the beginning of the program. The main function of Atlas is in <mysql-proxy-cli.c>, and the actual entry function for UNIX systems or Windows is
int main_cmdline (int argc, char **argv)
{...}
Since Atlas is based on GLIBC-completed development, the first thing to do in the startup process is to validate the GLIBC version:
if (Chassis_frontend_init_glib ()) {/* init the thread, module, ... system *
/Goto_exit (exit_failure);
}
Next, configure the log output parameters:
Chassis_log *log = NULL;
Log = Chassis_log_new ();
LOG->MIN_LVL = G_log_level_message; /* Display messages while parsing or loading plugins
/G_log_set_default_handler (chassis_log_func, log);
Where Chassis_log is the following structure:
typedef struct {
gloglevelflags min_lvl;//log level
gchar *log_filename;//log file path
gint log_file_fd;//log file descriptor
Gboolean Use_syslog;
Gboolean rotate_logs;
Gstring *log_ts_str;
Gint log_ts_resolution/*<< Timestamp Resolution (sec, MS) * * gstring *last_msg
;
time_t last_msg_ts;
Guint last_msg_count;
} chassis_log;
Thus, the normalized log output can be achieved through functions such as G_log.
Subsequently, Atlas will perform the parsing of command-line parameters and configuration files. The parsed data is stored in the
chassis_frontend_t *frontend;
Its specific structure is as follows
typedef struct { int print_version;//print version int verbose_shutdown;//off output int da
emon_mode;//background run Gchar *user;//users Gchar *base_dir;//working directory int auto_base_dir;//whether to use the default directory Gchar *default_file;//Default
Profile gkeyfile *keyfile;//configuration file Object
Chassis_plugin *p;//plug-in goptionentry *config_entries;//command line configuration options
Gchar *pid_file;//pid File Storage office
Gchar *plugin_dir;//Plug-in directory Gchar **plugin_names;//plug-in name Guint invoke_db
G_on_crash; Gint MAx_files_number;//maximum number of files Gchar *log_level;//log level Gchar *log_path;//log store path int &NB Sp use_syslog;//whether to enable syslog Char *lua_path;//lua script store path char *lua_cpath;// CH Ar **lua_subdirs;//lua Foot Directory} chassis_frontend_t;
In fact, Atlas has a certain sequence of parsing the command line and configuration files, first getting two parameters frontend->print_version and frontend->default_file from the configuration item.
Chassis_frontend_init_base_options (Option_ctx, &ARGC, &ARGV, & (frontend->print_version),& ( Frontend->default_file), &gerr);
The configuration file is then loaded according to the path specified by Frontend->default_file:
Frontend->keyfile = Chassis_frontend_open_config_file (Frontend->default_file, &gerr);
Next, begin parsing the other configuration parameters in the command line and configuration file
chassis_options_t *opts = NULL;
opts = Chassis_options_new ();
Chassis_frontend_set_chassis_options (Frontend, opts);
Main_entries = Chassis_options_to_g_option_entries (opts);
G_option_context_add_main_entries (Option_ctx, main_entries, NULL);//Specify configuration commands to resolve
G_option_context_parse (Option_ctx, &ARGC, &ARGV, &gerr)//Resolve command line other parameters
Chassis_keyfile_to_options (Frontend->keyfile, "Mysql-proxy", main_entries); parsing parameters in a configuration file
At this point, the configuration loading work on Atlas is complete.
After the configuration load is complete, do the work:
1. Specify the Lua script directory
2. Check that the number of worker threads is configured correctly
3. Update the Atlas server configuration and complete part of the initialization (primarily the initialization required for the Lua script run, which is not quite understood at this point in part to the Lua script run)
Network_mysqld_init (SRV);
Next, it sets the corresponding handler function for the SIGSEGV signal.
memset (&sigsegv_sa, 0, sizeof (SIGSEGV_SA));
Sigsegv_sa.sa_handler = Sigsegv_handler;
Sigemptyset (&sigsegv_sa.sa_mask);
if (Frontend->invoke_dbg_on_crash &&! ( Running_on_valgrind)) {
sigaction (SIGSEGV, &sigsegv_sa, NULL);
}
Sigsegv_handler has two main tasks: tracking the wrong position and throwing SIGABRT signal.
Subsequently, Atlas began to prepare for the creation of log and PID files and reset the log level based on the configuration file, which is better understood and not discussed.
Next, Atlas starts loading plug-ins, MySQL, and proxy.
if (!frontend->plugin_names) {frontend->plugin_names = G_new (char *, 3);
Frontend->plugin_names[0] = g_strdup ("admin");
FRONTEND->PLUGIN_NAMES[1] = g_strdup ("proxy");
FRONTEND->PLUGIN_NAMES[2] = NULL; //Loading Plug-ins here, using the GLIBC method, this section can refer to http://www.ibm.com/developerworks/cn/linux/l-gtkplgin/if (chassis_frontend_load _plugins (Srv->modules, Frontend->plugin_dir, Frontend->plugin_names)) {G
Oto_exit (exit_failure);
///The main job here is to get the configuration options for the plug-in and read the partial configuration first (Chassis_frontend_init_plugins srv->modules, Option_ctx, &ARGC, &ARGV, Frontend->keyfile, "Mysql-proxy", sr V->base_dir, &gerr)) {g_critical ("%s:%s", G_strloc, gerr-
>message);
G_clear_error (&gerr);
Goto_exit (exit_failure); }
Once the plug-in is loaded, you can begin parsing some command-line arguments that were previously unknown in the command-line arguments:
G_option_context_parse (Option_ctx, &ARGC, &ARGV, &gerr);
Subsequently, Atlass was partially configured, including
1. Deamon mode operation
2. Modify the PID file
3. Set maximum file limit
Finally, it enters the server program main loop:
Chassis_mainloop (SRV);