当前位置 博文首页 > m0_37389157的博客:MySQL源码学习-GTID

    m0_37389157的博客:MySQL源码学习-GTID

    作者:[db:作者] 时间:2021-08-06 21:59

    GTID的组成

    gtid一般又server_uuid+gno组成

    1.server_uuid重要的函数

    /*
    函数位置:mysqld.cc
    */
    init_server_auto_options() //mysql启动的时候会调用该函数读取auto.cnf文件获取server_uuid
    generate_server_uuid() //如果auto.cnf文件不存在,则会调用该函数生成一个新的server_uuid
    

    1.1generate_server_uuid()函数部分代码理解

    /*
    这部分代码是server_uuid的主要初始化过程,该函数首先会分配一个临时的变量thd,来进行过程中的计算
    为了保证在新启动mysqld时uuid的不重复,我们可以看到uuid和以下三个方面有关
    1.数据库的启动时间
    2.轻量级现成的id
    3.随机内存地址
    */
    sql_print_information("Salting uuid generator variables, current_pid: %lu, "
                            "server_start_time: %lu, bytes_sent: %llu, ",
                            current_pid,
                            (ulong)server_start_time, thd->status_var.bytes_sent);
    
      const time_t save_server_start_time= server_start_time;
      server_start_time+= ((ulonglong)current_pid << 48) + current_pid;
      thd->status_var.bytes_sent= (ulonglong)thd;
    
      lex_start(thd);
      func_uuid= new (thd->mem_root) Item_func_uuid();
      func_uuid->fixed= 1;
      func_uuid->val_str(&uuid);
    
      sql_print_information("Generated uuid: '%s', "
                            "server_start_time: %lu, bytes_sent: %llu",
                            uuid.c_ptr(),
                            (ulong)server_start_time, thd->status_var.bytes_sent);
      // Restore global variables used for salting
      server_start_time = save_server_start_time;
    

    2.gno相关的函数

    /*
    函数位置:rpl_gtid_state.cc
    */
    rpl_gno Gtid_state::get_automatic_gno()//gno生成以及gtid的生成
    
    cs