当前位置: 首页 > news >正文

mysql学习笔记4---mysql 复制---源代码

mysql:
c:底层
C++:相对上层
主备复制:主库通知备库来取


MYSQL复制源代码代码:SQL文件夹


int start_slave_thread(
#ifdef HAVE_PSI_INTERFACE
                       PSI_thread_key thread_key,
#endif
                       pthread_handler h_func, mysql_mutex_t *start_lock,
                       mysql_mutex_t *cond_lock,
                       mysql_cond_t *start_cond,
                       volatile uint *slave_running,
                       volatile ulong *slave_run_id,
                       Master_info* mi)
{
  pthread_t th;
  ulong start_id;
  int error;
  DBUG_ENTER("start_slave_thread");

  if (start_lock)
    mysql_mutex_lock(start_lock);
  if (!server_id)
  {
    if (start_cond)
      mysql_cond_broadcast(start_cond);
    if (start_lock)
      mysql_mutex_unlock(start_lock);
    sql_print_error("Server id not set, will not start slave");
    DBUG_RETURN(ER_BAD_SLAVE);
  }

  if (*slave_running)
  {
    if (start_cond)
      mysql_cond_broadcast(start_cond);
    if (start_lock)
      mysql_mutex_unlock(start_lock);
    DBUG_RETURN(ER_SLAVE_MUST_STOP);
  }
  start_id= *slave_run_id;
  DBUG_PRINT("info",("Creating new slave thread"));
  if ((error= mysql_thread_create(thread_key,
                          &th, &connection_attrib, h_func, (void*)mi)))
  {
    sql_print_error("Can't create slave thread (errno= %d).", error);
    if (start_lock)
      mysql_mutex_unlock(start_lock);
    DBUG_RETURN(ER_SLAVE_THREAD);
  }
  if (start_cond && cond_lock) // caller has cond_lock
  {
    THD* thd = current_thd;
    while (start_id == *slave_run_id && thd != NULL)
    {
      DBUG_PRINT("sleep",("Waiting for slave thread to start"));
      PSI_stage_info saved_stage= {0, "", 0};
      thd->ENTER_COND(start_cond, cond_lock,
                      & stage_waiting_for_slave_thread_to_start,
                      & saved_stage);
      /*
        It is not sufficient to test this at loop bottom. We must test
        it after registering the mutex in enter_cond(). If the kill
        happens after testing of thd->killed and before the mutex is
        registered, we could otherwise go waiting though thd->killed is
        set.
      */
      if (!thd->killed)
        mysql_cond_wait(start_cond, cond_lock);
      thd->EXIT_COND(& saved_stage);
      mysql_mutex_lock(cond_lock); // re-acquire it as exit_cond() released
      if (thd->killed)
      {
        if (start_lock)
          mysql_mutex_unlock(start_lock);
        DBUG_RETURN(thd->killed_errno());
      }
    }
  }
  if (start_lock)
    mysql_mutex_unlock(start_lock);
  DBUG_RETURN(0);
}




/**
  Slave IO thread entry point.

  @param arg Pointer to Master_info struct that holds information for
  the IO thread.

  @return Always 0.
*/

pthread_handler_t handle_slave_io(void *arg)
{
  THD *thd= NULL; // needs to be first for thread_stack
  bool thd_added= false;
  MYSQL *mysql;
  Master_info *mi = (Master_info*)arg;
  Relay_log_info *rli= mi->rli;
  char llbuff[22];
  uint retry_count;
  bool suppress_warnings;
  int ret;
  int binlog_version;
#ifndef DBUG_OFF
  uint retry_count_reg= 0, retry_count_dump= 0, retry_count_event= 0;
#endif
  // needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff
  my_thread_init();
  DBUG_ENTER("handle_slave_io");

..
}

/**
  Slave SQL thread entry point.

  @param arg Pointer to Relay_log_info object that holds information
  for the SQL thread.

  @return Always 0.
*/
pthread_handler_t handle_slave_sql(void *arg)
{
  THD *thd;                     /* needs to be first for thread_stack */
  bool thd_added= false;
  char llbuff[22],llbuff1[22];
  char saved_log_name[FN_REFLEN];
  char saved_master_log_name[FN_REFLEN];
  my_off_t saved_log_pos= 0;
  my_off_t saved_master_log_pos= 0;
  my_off_t saved_skip= 0
  
  
  
   if (exec_relay_log_event(thd,rli))

......................................
}


mysql_execute_command(THD *thd)
{
    
    。。。。。。。。。。。。
    
    
    
  case SQLCOM_SLAVE_START:
  {
    mysql_mutex_lock(&LOCK_active_mi);
    if (active_mi != NULL)
      res= start_slave(thd, active_mi, 1 /* net report*/);
    else
      my_message(ER_SLAVE_CONFIGURATION, ER(ER_SLAVE_CONFIGURATION),
                 MYF(0));
    mysql_mutex_unlock(&LOCK_active_mi);
    break;
    
    。。。。。。。。。。。。。。。。。。。
  }

    
  /**
  Top-level function for executing the next event in the relay log.
  This is called from the SQL thread.

  This function reads the event from the relay log, executes it, and
  advances the relay log position.  It also handles errors, etc.

  This function may fail to apply the event for the following reasons:

   - The position specfied by the UNTIL condition of the START SLAVE
     command is reached.

   - It was not possible to read the event from the log.

   - The slave is killed.

   - An error occurred when applying the event, and the event has been
     tried slave_trans_retries times.  If the event has been retried
     fewer times, 0 is returned.

   - init_info or init_relay_log_pos failed. (These are called
     if a failure occurs when applying the event.)

   - An error occurred when updating the binlog position.

  @retval 0 The event was applied.

  @retval 1 The event was not applied.
*/
static int exec_relay_log_event(THD* thd, Relay_log_info* rli)
{
  DBUG_ENTER("exec_relay_log_event");

  /*
     We acquire this mutex since we need it for all operations except
     event execution. But we will release it in places where we will
     wait for something for example inside of next_event().
   */
  mysql_mutex_lock(&rli->data_lock);

  /*
    UNTIL_SQL_AFTER_GTIDS requires special handling since we have to check
    whether the until_condition is satisfied *before* the SQL threads goes on
    a wait inside next_event() for the re  
    
    
    。。。。。。。。。。。。
    }
    
    */
    exec_res= apply_event_and_update_pos(ptr_ev, thd, rli);
    
    apply_event_and_update_pos(Log_event** ptr_ev, THD* thd, Relay_log_info* rli)

    
 class Query_log_event: public Log_event
{
  LEX_STRING user;
  LEX_STRING host;
protected:
  Log_event::Byte* data_buf;
public:
  const char* query;
  const char* catalog;
  const char* db;
  /*
    If we already know the length of the query string
    we pass it with q_len, so we would not have to call strlen()
    otherwise, set it to 0, in which case, we compute it with strlen()
  */
  uint32 q_len;
  uint32 db_len;
  uint16 error_code;
  ulong thread_id;
  /*
    For events created by Query_log_event::do_apply_event (and
    Load_log_event::do_apply_event()) we need the *original* thread
    id, to be able to log the event with the original (=master's)
    thread id (fix for BUG#1686).
  */
  ulong slave_proxy_id;

  /*
    Binlog format 3 and 4 start to differ (as far as class members are
    concerned) from here.
  */

  uint catalog_len;            // <= 255 char; 0 means uninited

  /*
    We want to be able to store a variable number of N-bit status vars:
    (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
    of affected rows (for debugging) while another does not want to lose 4
    bytes in this.
    The storage on disk is the following:
    status_vars_len is part of the post-header,
    status_vars are in the variable-length part, after the post-header, before
    the db & query.
    status_vars on disk is a sequence of pairs (code, value) where 'code' means
    'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
    its first byte is its length. For now the order of status vars is:
    flags2 - sql_mode - catalog - autoinc - charset
    We should add the same thing to Load_log_event, but in fact
    LOAD DATA INFILE is going to be logged with a new type of event (logging of
    the plain text query), so Load_log_event would be frozen, so no need. The
    new way of logging LOAD DATA INFILE would use a derived class of
    Query_log_event, so automatically benefit from the work already done for
    status variables in Query_log_event.
 */
  uint16 status_vars_len;

  /*
    'flags2' is a second set of flags (on top of those in Log_event), for
    session variables. These are thd->options which is & against a mask
    (OPTIONS_WRITTEN_TO_BIN_LOG).
    flags2_inited helps make 
    。。
    
    }
    
    */
    
    
    
    主库/备库 挂了,数据不一致
    1062 :主健冲突,备库大于主库数据,,主库少了数据 ,主库挂了
    1032:备库比主库数据少
    
    set session sql_log_bin=0;

 

semi-sync安装---master

master 安装过程:
install plugin rpl_semi_sync_master soname 'semisync_master.so'    
show variables like 'rpl_semi_sync_master_enabled'
值为ON,表示开启;否则检查失败原因

set global rpl_semi_sync_master_timeout=100000(利于观察)
set global rpl_semi_sync_master_wait_no_slave=1
(是默认值,表示即使没有slave 也会以等待过期时间结束)
         

 

转载于:https://www.cnblogs.com/zengkefu/p/5542040.html

相关文章:

  • linux下安装LoadRunner LoadGenerator
  • return和finally的执行顺序
  • Java System.getProperty()
  • 构建之法读后感
  • 个人学习对UIView动画的总结
  • L2-001 紧急救援
  • Git Shell Warning
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • Network of Schools_POJ1236_Tarjan
  • 18121 排排坐看电影
  • 编辑中
  • html5 历史管理
  • fopen()函数以a+方式打开一个不存在的文件后读写出现问题
  • 第五章
  • Android Studio自定义注释模板
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • Java 最常见的 200+ 面试题:面试必备
  • PHP面试之三:MySQL数据库
  • React-redux的原理以及使用
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 读懂package.json -- 依赖管理
  • 检测对象或数组
  • 理清楚Vue的结构
  • 使用API自动生成工具优化前端工作流
  • 微服务入门【系列视频课程】
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • 协程
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 运行时添加log4j2的appender
  • Python 之网络式编程
  • ​linux启动进程的方式
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • # 计算机视觉入门
  • #LLM入门|Prompt#3.3_存储_Memory
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (强烈推荐)移动端音视频从零到上手(下)
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)Google的Objective-C编码规范
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • ..回顾17,展望18
  • .bat批处理出现中文乱码的情况
  • .net core 依赖注入的基本用发
  • .Net Redis的秒杀Dome和异步执行
  • .net 获取url的方法
  • .NET 自定义中间件 判断是否存在 AllowAnonymousAttribute 特性 来判断是否需要身份验证
  • /etc/sudoers (root权限管理)
  • @ModelAttribute使用详解
  • [2669]2-2 Time类的定义
  • [Android]使用Android打包Unity工程
  • [APUE]进程关系(下)
  • [BZOJ 1040] 骑士