Recently, I encountered a problem where an online machine waited for the semaphore for a long time. The mysql monitoring thread thought that mysqld had been hang, so it committed suicide and restarted. Here involves 1
Recently, I encountered a problem where an online machine waited for the semaphore for a long time. The mysql monitoring thread thought that mysqld had been hang, so it committed suicide and restarted. Here involves 1
Recently, I encountered a problem where an online machine waited for the semaphore for a long time. The mysql monitoring thread thought that mysqld had been hang at this time, so it committed suicide and restarted. This involves an interesting question, that is, how mysql handles read/write locks.
It consists of three parts:
1. Lock Creation
2. Lock
3. Unlock
4. Monitoring lock
The following content is analyzed based on Percona5.5.18
1. Create a lock
The creation of the lock is actually to initialize a RW struct (rw_lock_t). The actual call function is as follows:
[Cpp]
# Define rw_lock_create (K, L, level )\
Rw_lock_create_func (L), # L)
There are three parameters in rw_lock_create. In actual scenarios, only 2nd parameters are used.
K indicates mysql_pfs_key_t, and level indicates the current operation type (at least it looks like yes, in the file sync0sync. h). It seems that k is prepared for performance schema, and k represents the level of the current operation.
For example, create a read/write lock for the purge thread:
[Html]
Rw_lock_create (trx_purge_latch_key,
Let's go to rw_lock_create_func to see how it was created.
We can see that the logic of this function is actually very simple:
Lock-> lock_word = X_LOCK_DECR; // key field
Used to limit the maximum number of concurrent read/write locks. The comments in the Code are as follows:
[Cpp]
When trying to lock, rw_lock_lock_word_decr will be called to reduce lock_word
After initializing a series of variables, execute:
[Cpp]
Lock-> event = OS _event_create (NULL );
Lock-> wait_ex_event = OS _event_create (NULL );
OS _event_create is used to create a system signal. The mutex is actually created (OS _fast_mutex_init (& (event-> OS _mutex )); and condition variables (OS _cond_init (& (event-> cond_var ));)
Add the lock to the global linked list rw_lock_list.