MySQL源代码:如何对读写锁进行处理


4.监控读写锁

为了防止mysqld被hang住导致的长时间等待rw锁,error监控线程会对长时间等待的线程进行监控。这个线程每1秒loop一次

(os_event_wait_time_low(srv_error_event, 1000000, sig_count);)

函数入口:srv_error_monitor_thread

函数sync_array_print_long_waits()用于处理长时间等待信号量的线程,流程如下:

1. 查看sync_primary_wait_array数组中的所有等待线程。

->大于240秒时,向错误日志中输出警告,设置noticed = TRUE;

->大于600秒时,设置fatal =TRUE;

2.当noticed为true时,打印出innodb监控信息,然后sleep30秒

3. 返回fatal值

 

当函数sync_primary_wait_array返回true时,对于同一个等待线程还会有十次机会,也就是300 + 1*10(监控线程每次loop sleep 1s)秒的时间;如果挺不过去,监控线程就会执行一个断言失败:

[cpp]
  1. if (fatal_cnt > 10) {  
  2.                    fprintf(stderr,  
  3.                             "InnoDB:Error: semaphore wait has lasted"  
  4.                             "> %lu seconds\n"  
  5.                             "InnoDB:We intentionally crash the server,"  
  6.                             "because it appears to be hung.\n",  
  7.                              (ulong) srv_fatal_semaphore_wait_threshold);  
  8.    
  9.                             ut_error;  
  10.                    }  
ut_error是一个宏:

[cpp]
  1. #define ut_error      assert(0)  
断言失败导致mysqld crash

 在函数srv_error_monitor_thread里发现一个比较有意思的参数srv_kill_idle_transaction,对应的系统变量为innodb_kill_idle_transaction,用于清理在一段时间内的空闲事务。这个变量指定了空闲事务的最长时间。具体实现分析,且听下回分解 见

相关内容