Mysql 5.7中Gtid和Last_commt sequnce_number的生成机遇是什么
发布时间:2021-12-20 22:11:58 所属栏目:MySql教程 来源:互联网
导读:本篇内容主要讲解Mysql 5.7中Gtid和Last_commt/sequnce_number的生成时机是什么,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习Mysql 5.7中Gtid和Last_commt/sequnce_number的生成时机是什么吧! 一、Gtid生成类型
本篇内容主要讲解“Mysql 5.7中Gtid和Last_commt/sequnce_number的生成时机是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mysql 5.7中Gtid和Last_commt/sequnce_number的生成时机是什么”吧! 一、Gtid生成类型 这里首先使用源码的解释给出三种类型: AUTOMATIC_GROUP GTID_GROUP ANONYMOUS_GROUP 其中AUTOMATIC_GROUP通常用于主库开启Gtid的情况,GTID_GROUP通常用于备库和使用了GTID_NEXT的情况下。 源码中有详细解释如下: /** Specifies that the GTID has not been generated yet; it will be generated on commit. It will depend on the GTID_MODE: if GTID_MODE<=OFF_PERMISSIVE, then the transaction will be anonymous; if GTID_MODE>=ON_PERMISSIVE, then the transaction will be assigned a new GTID. This is the default value: thd->variables.gtid_next has this state when GTID_NEXT="AUTOMATIC". It is important that AUTOMATIC_GROUP==0 so that the default value for thd->variables->gtid_next.type is AUTOMATIC_GROUP. */ AUTOMATIC_GROUP= 0, /** Specifies that the transaction has been assigned a GTID (UUID:NUMBER). thd->variables.gtid_next has this state when GTID_NEXT="UUID:NUMBER". This is the state of GTID-transactions replicated to the slave. */ GTID_GROUP, /** Specifies that the transaction is anonymous, i.e., it does not have a GTID and will never be assigned one. thd->variables.gtid_next has this state when GTID_NEXT="ANONYMOUS". This is the state of any transaction generated on a pre-GTID server, or on a server with GTID_MODE==OFF. */ ANONYMOUS_GROUP 二、Gtid和Last_commt/sequnce_number的生成时机 Gtid其实是在commit的时候调用MYSQL_BIN_LOG::ordered_commit执行到flush 阶段产生Gtid event的时候才生成,生成后会将这个Gtid 加入到Gtid_state的Owned_gtids中,实际上这个过程不仅要生成Gtid还会生成sequence_number和last_commit并且会构造Gtid_event写入到binlog cache最后将binlog cache写入到binlog file,下面是binlog_cache_data::flush函数的片段: if (!error) if ((error= mysql_bin_log.write_gtid(thd, this, &writer))) //生成Gtid和Last_commt/sequnce_number构造好Gtid event并且写入到到binlog cache中 thd->commit_error= THD::CE_FLUSH_ERROR; if (!error) error= mysql_bin_log.write_cache(thd, this, &writer); //将binlog cache写入到文件 下面是mysql_bin_log.write_gtid中生成Gtid和Last_commt/sequnce_number的代码片段: if (thd->variables.gtid_next.type == AUTOMATIC_GROUP)//如果过是非指定的Gtid则需要自动生成调用generate_automatic_gtid生成 { if (gtid_state->generate_automatic_gtid(thd, thd->get_transaction()->get_rpl_transaction_ctx()->get_sidno(), thd->get_transaction()->get_rpl_transaction_ctx()->get_gno()) != RETURN_STATUS_OK) DBUG_RETURN(true); } ..... //下面生成sequence_number和last_committed int64 relative_sequence_number= trn_ctx->sequence_number - clock.get_offset(); int64 relative_last_committed= trn_ctx->last_committed <= clock.get_offset() ? SEQ_UNINIT : trn_ctx->last_committed - clock.get_offset(); 三、Gtid_state::generate_automatic_gtid逻辑 // If GTID_MODE = ON_PERMISSIVE or ON, generate a new GTID if (get_gtid_mode(GTID_MODE_LOCK_SID) >= GTID_MODE_ON_PERMISSIVE)//如果GTID_MODE是ON_PERMISSIVE和ON则生成GTID { Gtid automatic_gtid= { specified_sidno, specified_gno }; if (automatic_gtid.sidno == 0)//如果是备库则sidno>0,如果是主库sidno==0,因为主库的Gtid这个时候才生成,但是备库则是使用GTID_GROUP指定生成 automatic_gtid.sidno= get_server_sidno();//此处返回本server的sidno lock_sidno(automatic_gtid.sidno);//此处对并发生成GNO的多个线程进行控制 if (automatic_gtid.gno == 0)//如果是备库则gno>0,如果是主库gno == 0,因为主库的Gtid这个时候才生成,但是备库则是使用GTID_GROUP指定生成 automatic_gtid.gno= get_automatic_gno(automatic_gtid.sidno);//此处返回最后指定sidno的end gno if (automatic_gtid.gno != -1) acquire_ownership(thd, automatic_gtid);//此处将这个gtid 及上面的SIDNO:gno加入到owned_gtids中 并且赋予给线程 经过本步骤 可以显示 else ret= RETURN_STATUS_REPORTED_ERROR; unlock_sidno(automatic_gtid.sidno);//分配完成其他线程可以分配 } else //如果是OFF_PERMISSIVE或者OFF状态如何处理 这里不做讨论了 { // If GTID_MODE = OFF or OFF_PERMISSIVE, just mark this thread as // using an anonymous transaction. thd->owned_gtid.sidno= THD::OWNED_SIDNO_ANONYMOUS; thd->owned_gtid.gno= 0; acquire_anonymous_ownership(); thd->owned_gtid.dbug_print(NULL, "set owned_gtid (anonymous) in generate_automatic_gtid"); } sid_lock->unlock();//释放读写锁 接下来看看gno的生成逻辑Gtid_state::get_automatic_gno。 (编辑:站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |