In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to deal with read-write locks in MySQL. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Create a lock
The creation of a lock actually initializes a RW structure (rw_lock_t). The actual calling function is as follows:
# define rw_lock_create (K, L, level)\
Rw_lock_create_func ((L), # L)
There are three parameters on rw_lock_create, and only the second parameter is used in the actual scene lock
Where K indicates that mysql_pfs_key_t,level shows the current type of operation (at least it seems to be, defined in the file sync0sync.h), it appears that k is prepared for performance schema, and k represents the level of the current operation.
For example, the read-write lock creation of the purge thread:
Rw_lock_create (trx_purge_latch_key
& purge_sys- > latch,SYNC_PURGE_LATCH)
Let's go into rw_lock_create_func and see how it was created.
You can see that the logic of this function is actually very simple:
Lock- > lock_word = Xencrypted Lockbox DECR; / / key field
To limit the maximum number of concurrency of read-write locks, the comments in the code are as follows:
/ * We decrement lock_word by this amountfor each x_lock. It is also the
Start value for the lock_word, meaning thatit limits the maximum number
Of concurrent read locks before the rw_lockbreaks. The current value of
0x00100000 allows 1048575 concurrentreaders and 2047 recursive writers.*/
Rw_lock_lock_word_decr is called to reduce lock_word when trying to add a lock
After initializing a series of variables, execute:
Lock- > event = os_event_create (NULL)
Lock- > wait_ex_event = os_event_create (NULL)
Os_event_create is used to create a system signal, which is actually a mutex (os_fast_mutex_init (& (event- > os_mutex)); and a condition variable (os_cond_init (& (event- > cond_var));)
Finally, lock is added to the global linked list rw_lock_list
two。 Add lock
The locking function is defined by a macro, and the actual calling function is:
1) write lock
# define rw_lock_x_lock (M)\
Rw_lock_x_lock_func ((M), 0, _ _ FILE__, _ _ LINE__)
When applying for a write lock, perform the following steps:
(1)。 Call the rw_lock_x_lock_low function to acquire the lock. If the lock is obtained, it will be returned directly after rw_x_spin_round_count + = I. If the lock is not available, continue execution.
(2) rw_x_spin_wait_count++ is executed only once in the .loop process.
(3)。 Loop waits many times at millisecond level
While (I)
< SYNC_SPIN_ROUNDS && lock->Lock_word n_cells)
(6)。 Call the rw_lock_x_lock_low function again to attempt to acquire the lock. If the lock is obtained successfully, it returns
(7)。 Call sync_array_wait_event to wait for the condition variable, and then return 1 to continue loop
The specific locking function (rw_lock_x_lock_low) will be analyzed later.
2) read lock
# define rw_lock_s_lock (M)\
Rw_lock_s_lock_func ((M), 0, _ _ FILE__, _ _ LINE__)
This function is defined in sync0rw.ic, and the function is very simple, as follows:
If (rw_lock_s_lock_low (lock, pass, file_name, line)) {
Return; / * Success * /
} else {
/ * Did not succeed, try spin wait * /
Rw_lock_s_lock_spin (lock, pass, file_name, line)
Return
}
Here, first call rw_lock_s_lock_low to add the lock, and if the locking is not successful, call rw_lock_s_lock_spin to wait. The code logic of rw_lock_s_lock_spin is similar to that of rw_lock_x_lock_func, so I won't repeat it here.
The rw_lock_s_lock_low function will be called recursively in rw_lock_s_lock_spin
It seems that the actual locking and unlocking operations are controlled by the counter.
(1) in the function rw_lock_s_lock_low
Rw_lock_lock_word_decr (lock, 1), subtract 1 from lock- > lock_word
True is returned if the subtraction is successful, otherwise false is returned.
The logic of this part is still very simple.
(2) in the function rw_lock_x_lock_low, call:
Rw_lock_lock_word_decr (lock, X_LOCK_DECR), subtract X_LOCK_DECR from lock- > lock_word
After the subtraction is successful, execute:
Rw_lock_set_writer_id_and_recursion_flag (lock,pass? FALSE: TRUE) to set:
Lock- > writer_thread = s_thread_get_curr_id ()
Lock- > recursive = TRUE
The rw_lock_x_lock_wait function is then called to wait for lock- > lock_word=0, that is, to wait for all read locks to exit.
See a more interesting phenomenon, in the .ic code to see the use of macros
INNODB_RW_LOCKS_USE_ATOMICS, which is related to the version of gcc, implements atomic operations by using the built-in function of gcc.
3. Unlock
Unlock operations include unlocking read locks (# definerw_lock_ s_unlock (L) rw_lock_s_unlock_gen (L, 0)) and unlocking write locks (# definerw_lock_x_unlock (L) rw_lock_x_unlock_gen (L, 0))
The actual calling functions are rw_lock_s_unlock_func and rw_lock_x_unlock_func
1) release the read lock (rw_lock_s_unlock_func)
Increase the count rw_lock_lock_word_incr (lock, 1)
2) unlock the write (rw_lock_x_unlock_func)
Do the following
(1) if it is the last thread that recursively invokes the lock, set the comments in the lock- > recursive= FALSE; code as follows:
/ * lock- > recursive flag also indicatesif lock- > writer_thread is
Valid or stale. If we are the last of the recursive callers
Then we must unset lock- > recursive flag to indicate that the
Lock- > writer_thread is now stale.
Note that since we still hold the x-lock we can safely read the
Lock_word. , /
(2) increase the count rw_lock_lock_word_incr (lock,X_LOCK_DECR) = = X_LOCK_DECR, when you need to send a signal to the thread waiting for the lock:
If (lock- > waiters) {
Rw_lock_reset_waiter_flag (lock)
Os_event_set (lock- > event)
Sync_array_object_signalled (sync_primary_wait_array)
}
The os_event_set function sends a pthread_cond_broadcast to the waiting thread
4. Monitor read-write lock
In order to prevent mysqld from waiting for rw locks for a long time caused by hang, the error monitoring thread will monitor the threads that have been waiting for a long time. This thread loop every second
(os_event_wait_time_low (srv_error_event, 1000000, sig_count);
Function entry: srv_error_monitor_thread
The function sync_array_print_long_waits () is used to handle threads waiting for semaphores for a long time. The process is as follows:
1. View all waiting threads in the sync_primary_wait_array array.
-> when it is longer than 240s, output a warning to the error log and set noticed = TRUE
-> set fatal = TRUE when it is longer than 600s
two。 When noticed is true, print out innodb monitoring information, and then sleep30 seconds
3. Returns the fatal value
When the function sync_primary_wait_array returns true, there will be ten more opportunities for the same waiting thread, that is, 300 + 1cm 10 (monitoring thread loop sleep 1s per second) seconds; if not, the monitoring thread will execute an assertion failure:
If (fatal_cnt > 10) {
Fprintf (stderr
"InnoDB:Error: semaphore wait has lasted"
">% lu seconds\ n"
"InnoDB:We intentionally crash the server,"
"because it appears to be hung."
(ulong) srv_fatal_semaphore_wait_threshold)
Ut_error
}
Ut_error is a macro:
# define ut_error assert (0)
Assertion failure resulted in mysqld crash
An interesting parameter, srv_kill_idle_transaction, is found in the function srv_error_monitor_thread, and the corresponding system variable is innodb_kill_idle_transaction, which is used to clean up idle transactions over a period of time. This variable specifies the maximum amount of time for idle transactions.
The above is how to deal with read-write locks in the MySQL shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.