mysqld key functions and execution flow of MySQL8.0.11 source code analysis

Posted by dswain on Tue, 31 Dec 2019 22:00:41 +0100

mysqld is the SQL daemons (that is, the MySQL server). To use the client program, it must run because the client accesses the database by connecting to the server.

//Linux entry function

int mysqld_main(int argc, char **argv){

    ...... //Initialize operation

    //Cyclic connection

    mysqld_socket_acceptor->connection_event_loop();  

    ......//End operation

}

 

//Connection receiver loop to accept connections from clients

void connection_event_loop() {

    //Initialize connection handle
    Connection_handler_manager *mgr = Connection_handler_manager::get_instance();  

     while (!connection_events_loop_aborted()) {  

         //Listening for connection events

         Channel_info *channel_info = m_listener->listen_for_connection_event();        

         //Process a new connection

         if (channel_info != NULL) 
            mgr->process_new_connection(channel_info);     

    }

}

 

//Process new connections

void Connection_handler_manager::process_new_connection(Channel_info *channel_info) {

    if (connection_events_loop_aborted() || !check_and_incr_conn_count()) {

        channel_info->send_error_and_close_channel(ER_CON_COUNT_ERROR, 0, true);

        delete channel_info;

        return;

     }

      //Associate a new connection with a thread

     if (m_connection_handler->add_connection(channel_info)) {

         inc_aborted_connects();

        delete channel_info;

     }

}

 

//Add a connection. If no idle thread is available to occupy the new connection, create a new thread to handle the connection

bool Per_thread_connection_handler::add_connection(Channel_info *channel_info) {

    int error = 0;

    my_thread_handle id;

    //Check idle threads and queue connections

    if (!check_idle_thread_and_enqueue_connection(channel_info))

        DBUG_RETURN(false);

    //No idle threads are available for new connections. Create a new thread to handle the connection

    channel_info->set_prior_thr_create_utime();

    error =mysql_thread_create(key_thread_one_connection,&id,&connection_attrib,
        handle_connection,(void *)channel_info);

   .....

}

 

Topics: SQL MySQL Database Linux