- 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()
- IO Thread
使用的AIO来处理IO请求,可以通过innodb_线程_io_threads来设置各种IO线程的数量 - Purge Thread
当事务被提交后undo可能不需要了,该线程就是进行回收undo页的,使用innodb_purge_threads来设置开启独立线程数量