MySQL源码-GTID模式事务提交

GTID 配置

GTID 模式

GTID可以工作在4种模式下:

  1. 从Master视角看,GTID_MODE_OFF和GTID_MODE_OFF_PERMISSIVE模式下,产生匿名事务;GTID_MODE_ON_PERMISSIVE和GTID_MODE_ON模式下,产生GTID事务。
  2. 从Slave视角看,GTID_MODE_OFF模式下,只能复制匿名事务;GTID_MODE_OFF_PERMISSIVE和GTID_MODE_ON_PERMISSIVE模式下,可以同时复制匿名事务和GTID事务;GTID_MODE_ON模式下,只能复制GTID事务。
enum enum_gtid_mode {
  GTID_MODE_OFF= 0,
  GTID_MODE_OFF_PERMISSIVE= 1,
  GTID_MODE_ON_PERMISSIVE= 2,
  GTID_MODE_ON= 3
};

GTID 一致性模式

  1. OFF模式下,所有事务允许违反GTID一致性;
  2. WARN模式下,所有事务允许违反GTID一致性,但会产生警告;
  3. ON模式下,所有事务不允许违反GTID一致性。
enum enum_gtid_consistency_mode
{
  GTID_CONSISTENCY_MODE_OFF= 0,
  GTID_CONSISTENCY_MODE_ON= 1,
  GTID_CONSISTENCY_MODE_WARN= 2
};

GTID 组类型

GTID 组类型默认为AUTOMATIC_GROUP。

  1. AUTOMATIC_GROUP表示Master在事务提交时才产生GTID;
  2. GTID_GROUP表示Slave在日志复制时,next_gtid被赋值GTID;
  3. ANONYMOUS_GROUP表示事务永远不会被赋值一个GTID;
  4. UNDEFINED_GROUP会让Slave主动产生报错,是为了解决Slave复制时,遇到事务中同时更新事务和非事务表场景;
  5. NOT_YET_DETERMINED_GROUP解决Slave接到到的事务组第一个事件不是Gtid_log_event的场景。
enum enum_group_type
{
  AUTOMATIC_GROUP= 0,
  GTID_GROUP,
  ANONYMOUS_GROUP,
  UNDEFINED_GROUP,
  NOT_YET_DETERMINED_GROUP
};

GTID 事务提交

Master事务提交在ordered_commit中分3大步骤进行:

  • FLUSH_STAGE:flush缓存的事务到binlog(不落盘);
  • SYNC_STAGE: 将binlog文件fsync到磁盘;
  • COMMIT_STAGE:提交事务。
int MYSQL_BIN_LOG::ordered_commit(THD *thd, bool all, bool skip_commit)

下面重点看下GTID相关的FLUSH_STAGE和COMMIT_STAGE阶段。

GTID 事务Flush

int MYSQL_BIN_LOG::process_flush_stage_queue(my_off_t *total_bytes_var, bool *rotate_var, THD **out_queue_var)
{
   assign_automatic_gtids_to_flush_group(first_seen);  //产生GTID
    /* Flush thread caches to binary log. */
  for (THD *head= first_seen ; head ; head = head->next_to_commit)
  {
    std::pair result= flush_thread_caches(head); //cache刷到文件
    no_flushes++;
  }
}

处于AUTOMATIC_GROUP状态下的事务赋予一个GTID。

bool MYSQL_BIN_LOG::assign_automatic_gtids_to_flush_group(THD *first_seen)
{
  for (THD *head= first_seen ; head ; head = head->next_to_commit)
  {
    DBUG_ASSERT(head->variables.gtid_next.type != UNDEFINED_GROUP);

    /* Generate GTID */
    if (head->variables.gtid_next.type == AUTOMATIC_GROUP)
    {
      if (!is_global_sid_locked)
      {
        global_sid_lock->rdlock();
        is_global_sid_locked= true;
      }
      if (gtid_state->generate_automatic_gtid(head,  //产生GTID
              head->get_transaction()->get_rpl_transaction_ctx()->get_sidno(),
              head->get_transaction()->get_rpl_transaction_ctx()->get_gno(),
              &locked_sidno)
              != RETURN_STATUS_OK)
      {
        head->commit_error= THD::CE_FLUSH_ERROR;
        error= true;
      }
    }
    else
    {
      if (head->variables.gtid_next.type == GTID_GROUP)
        DBUG_ASSERT(head->owned_gtid.sidno > 0);  //slave,binlog复制时会设置gtid_next
      else
      {
        DBUG_ASSERT(head->variables.gtid_next.type == ANONYMOUS_GROUP);  //不产生gtid
        DBUG_ASSERT(head->owned_gtid.sidno == THD::OWNED_SIDNO_ANONYMOUS);
      }
    }
  }
  DBUG_RETURN(error);
}

对于ON和GTID_MODE_ON_PERMISSIVE模式下的事务,生成一个GTID。

enum_return_status Gtid_state::generate_automatic_gtid(THD *thd,
                                                       rpl_sidno specified_sidno,
                                                       rpl_gno specified_gno,
                                                       rpl_sidno *locked_sidno)
{
  DBUG_ASSERT(thd->variables.gtid_next.type == AUTOMATIC_GROUP);

  // If GTID_MODE = ON_PERMISSIVE or ON, generate a new GTID
  if (get_gtid_mode(GTID_MODE_LOCK_SID) >= GTID_MODE_ON_PERMISSIVE)
  {
    Gtid automatic_gtid= { specified_sidno, specified_gno };

    if (automatic_gtid.sidno == 0)  //master为0
      automatic_gtid.sidno= get_server_sidno();

    if (automatic_gtid.gno == 0)
    {
      automatic_gtid.gno= get_automatic_gno(automatic_gtid.sidno);
      if (automatic_gtid.sidno == get_server_sidno() &&
          automatic_gtid.gno != -1)
        next_free_gno= automatic_gtid.gno + 1;
    }
  }
  else
  {
    // 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");
  }
  DBUG_RETURN(ret);
}

flush_thread_caches方法中调用链为:

flush() -> write_gtid()

bool MYSQL_BIN_LOG::write_gtid(THD *thd, binlog_cache_data *cache_data, Binlog_event_writer *writer)
{
  int64 sequence_number, last_committed;
  /* Generate logical timestamps for MTS */
  m_dependency_tracker.get_dependency(thd, sequence_number, last_committed);

  Gtid_log_event gtid_event(thd, cache_data->is_trx_cache(), last_committed, sequence_number, cache_data->may_have_sbr_stmts());
  uchar buf[Gtid_log_event::MAX_EVENT_LENGTH];
  uint32 buf_len= gtid_event.write_to_memory(buf);
  bool ret= writer->write_full_event(buf, buf_len);
}
  1. get_dependency中设置sequence_number和last_committed;
  2. 写入gtid_event事件。

GTID 事务Commit

事务提交调用链如下:

process_commit_stage_queue() -> update_commit_group() -> update_gtids_impl_own_gtid()

update_gtids_impl_own_gtid中主要做2件事:将线程中事务分配的GTID从owned_gtids集合中删除;executed_gtids中添加该GTID。

void Gtid_state::update_gtids_impl_own_gtid(THD *thd, bool is_commit)
{
  owned_gtids.remove_gtid(thd->owned_gtid, thd->thread_id());
  if (is_commit)
  {
    executed_gtids._add_gtid(thd->owned_gtid);
  }
  else
  {
    if (thd->owned_gtid.sidno == server_sidno &&
        next_free_gno > thd->owned_gtid.gno)
      next_free_gno= thd->owned_gtid.gno;
  }

  thd->clear_owned_gtids();
}

你可能感兴趣的:(MySQL源码-GTID模式事务提交)