全面了解Mysql(二)Thread

  1. Master Thread工作方式
    每秒一次的操作有将日志缓存刷新到日志文件,即使事务没提交,每秒判断当前一秒内发生的IO次数是否小于5次,如果小于就进行合并插入缓冲,每秒判断当前缓冲池中脏页的比例是否超过innodb_max_dirty_pages_pct配置值,超过就刷新100个脏页到磁盘,下面代码就是示例
# coding=utf-8
import logging
import time
import threading


class MasterThread:
    def __init__(self):
        self.log = logging.getLogger(MasterThread.__name__)

    def main(self):
        while True:
            for i in range(10):
                self._per_second()

    # 判断io压力是否小 上一秒io小于5%io吞吐量
    def _is_io_low_pressure(self):
        self.log.debug('io压力小')
        return True

    # 当前缓冲池中脏页比例超过设置最大脏页阈值
    def _can_refresh_dirty_pages_to_disk(self):
        self.log.debug('可以刷新脏页')
        return True

    def _enable_adaptive_plush(self):
        self.log.debug('开启自适应刷新')
        return True

    def _not_user_activity(self):
        self.log.debug('没有用户活动')
        return True

    # 是否不在空闲
    def _not_idle(self):
        self.log.debug('不空闲')
        return True

    # 每秒运行
    def _per_second(self):
        time.sleep(1)
        self.log.debug('日志缓冲刷新到磁盘')
        if self._is_io_low_pressure():
            self.log.debug('合并5%io吞吐量条插入缓冲')
        if self._can_refresh_dirty_pages_to_disk():
            self.log.debug('刷新100%脏页')
        elif self._enable_adaptive_plush():
            self.log.debug('根据重做日志速度刷新脏页')
        if self._not_user_activity():
            self._background()

    # 后台刷新
    def _background(self):
        self.log.debug('刷新所有脏页')
        self.log.debug('合并100%插入缓冲')
        if self._not_idle():
            self.main()
        else:
            self._flush()

    # 刷新循环
    def _flush(self):
        self.log.debug('日志缓冲刷新到磁盘')
        if self._can_refresh_dirty_pages_to_disk():
            self._flush()

    # 暂停循环
    def _suspend(self):
        e = threading.Event()
        self.log.debug('等待事件激活')
        e.wait()
        self.main()
  1. IO Thread
    使用的AIO来处理IO请求,可以通过innodb_线程_io_threads来设置各种IO线程的数量
  2. Purge Thread
    当事务被提交后undo可能不需要了,该线程就是进行回收undo页的,使用innodb_purge_threads来设置开启独立线程数量
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值