innodb 源码分析 --锁

时间:2023-03-08 16:02:02

innodb引擎中的锁分两种

1)针对数据结构, 如链表

互斥锁

读写锁

http://mysqllover.com/?p=425

http://www.cnblogs.com/justfortaste/p/3668935.html

2)针对数据表中记录

行锁

表锁

innodb中的互斥锁是依据操作系统中的spin lock自旋锁,进行一些修改而成的

修改方面:

  1.当获得不到锁时, 一直在CPU高速缓冲区中读取值,避免与内存打交道,以免造成总线风暴

2.当自旋超过20us后,将线程放入wait array,待时机成熟后再唤醒,而不是放入系统的等待队列,避免上下文切换

详情请见 http://www.cnblogs.com/taek/p/4809685.html

这里主要分析下innodb的行锁

当执行sql 如 update user set name='xx' where id=1;时

innodb引擎会针对id=1这条记录加一个LOCK_REC行锁

这个锁并没有附在物理记录本身,而是存储于单独一个结构体中

对于行锁,先尝试加快锁,如果不行,再加慢锁

/*********************************************************************//**
Tries to lock the specified record in the mode requested. If not immediately
possible, enqueues a waiting lock request. This is a low-level function
which does NOT look at implicit locks! Checks lock compatibility within
explicit locks. This function sets a normal next-key lock, or in the case
of a page supremum record, a gap type lock.
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, DB_LOCK_WAIT, DB_DEADLOCK,
or DB_QUE_THR_SUSPENDED */ /**尝试用某种请求的锁,去锁住指定的行记录,如果不能立即上锁,将等待的锁入队列。
该函数级别较低,不能看做隐式锁,检查该锁与显示锁的相容性。该功能设置常规的next-key lock,
或为页面的supremum record设置gap lock
*/
static
enum db_err
lock_rec_lock(
/*==========*/
ibool impl, /*!< in: if TRUE, no lock is set
if no wait is necessary: we
assume that the caller will
set an implicit lock */
ulint mode, /*!< in: lock mode: LOCK_X or LOCK_S possibly ORed to either
          LOCK_GAP or LOCK_REC_NOT_GAP */
const buf_block_t* block, /*!< in: buffer block containing the record */ //记录所在的block块
ulint heap_no, /*!< in: heap number of record */
dict_index_t* index, /*!< in: index of record */
que_thr_t* thr) /*!< in: query thread */
{
/* We try a simplified and faster subroutine for the most
common cases */
switch (lock_rec_lock_fast(impl, mode, block, heap_no, index, thr)) {
case LOCK_REC_SUCCESS:
return(DB_SUCCESS);
case LOCK_REC_SUCCESS_CREATED:
return(DB_SUCCESS_LOCKED_REC);
case LOCK_REC_FAIL:
return(lock_rec_lock_slow(impl, mode, block,
heap_no, index, thr));
}
return(DB_ERROR);
}

加快锁

UNIV_INLINE
enum lock_rec_req_status
lock_rec_lock_fast(
/*===============*/
ibool impl, /*!< in: if TRUE, no lock is set
if no wait is necessary: we
assume that the caller will
set an implicit lock */
ulint mode, /*!< in: lock mode: LOCK_X or
LOCK_S possibly ORed to either
LOCK_GAP or LOCK_REC_NOT_GAP */
const buf_block_t* block, /*!< in: buffer block containing
the record */
ulint heap_no,/*!< in: heap number of record */
dict_index_t* index, /*!< in: index of record */
que_thr_t* thr) /*!< in: query thread */
{
lock_t* lock; //详见 结构体
trx_t* trx; //详见 结构体
   lock = lock_rec_get_first_on_page(block); //根据block,取出space,page_no构成hash,取出相应值,详见 trx = thr_get_trx(thr); if (lock == NULL) {
if (!impl) {//显示锁
lock_rec_create(mode, block, heap_no, index, trx); //如果为空,则创建锁, 并返回加锁成功,详见
} return(LOCK_REC_SUCCESS_CREATED);
}

//不清楚为什么要取下一个lock记录
if (lock_rec_get_next_on_page(lock)) { return(LOCK_REC_FAIL);
}
  
/**
*锁的事务不是当前事务
*或者锁的类型不是 mode|LOCK_REC (不包含行锁)
*lock_rec_get_n_bits返回lock->un_member.rec_lock.n_bits,这个n_bits可以理解为page中记录个数,对应的byte是n_bits/8+1
*例如 page中只有5条记录 5/8+1=1 只需要1+64=65字节即可表示这此记录
     *如果heap_no大于上面的n_bits,意味着 heap_no不能存放到由n_bits构造的二维数组中(该数组其实是n_bits/8+1)
*返回failed
*/
if (lock->trx != trx
|| lock->type_mode != (mode | LOCK_REC)
|| lock_rec_get_n_bits(lock) <= heap_no) { //详见 return(LOCK_REC_FAIL);
} if (!impl) {
/* If the nth bit of the record lock is already set then we
do not set a new lock bit, otherwise we do set */ if (!lock_rec_get_nth_bit(lock, heap_no)) {
lock_rec_set_nth_bit(lock, heap_no);
return(LOCK_REC_SUCCESS_CREATED);
}
} return(LOCK_REC_SUCCESS);
}

慢加锁

static
enum db_err
lock_rec_lock_slow(
/*===============*/
ibool impl, /*!< in: if TRUE, no lock is set
if no wait is necessary: we
assume that the caller will
set an implicit lock */
ulint mode, /*!< in: lock mode: LOCK_X or
LOCK_S possibly ORed to either
LOCK_GAP or LOCK_REC_NOT_GAP */
const buf_block_t* block, /*!< in: buffer block containing
the record */
ulint heap_no,/*!< in: heap number of record */
dict_index_t* index, /*!< in: index of record */
que_thr_t* thr) /*!< in: query thread */
{
trx_t* trx;
lock_t* lock; trx = thr_get_trx(thr);
/**
*根据block计算出space_id和page_no,再根据这两个值计算出hash值
*通过hash值在lock_sys->rec_hash这个HashTable中查询,当找到的头结点不符号条件,则遍历
*如果HashTable中某一个锁权限大于预加锁的权限时,直接返回

*
*如果HashTable中的某一个锁权限小于预加锁权限时,再判断兼容性
*例如在HashTable中找到一个lock,其mode为S
*预加锁的mode为X,S强度要小于X
*
*锁之间兼容性判断
*预加锁的权限 是否 兼容 HashTable中根据条件找到的lock的权限
*例如:
*预加锁权限 LOCK_X
*找到的lock 权限 LOCK_S
*经查找兼容矩阵:
* 不兼容:新建lock,其mode为 LOCK_X|LOCK_WAIT
* 兼容: 新建lock,其mode为LOCK_X|LOCK_REC */
lock = lock_rec_has_expl(mode, block, heap_no, trx); //函数实现    if (lock) {
if (lock->type_mode & LOCK_CONV_BY_OTHER) { //#define LOCK_CONV_BY_OTHER 4096
/* This lock or lock waiting was created by the other
transaction, not by the transaction (trx) itself.
So, the transaction (trx) should treat it collectly
according as whether granted or not. */ if (lock->type_mode & LOCK_WAIT) { //#define LOCK_WAIT 256
/* This lock request was not granted yet.
Should wait for granted. */ goto enqueue_waiting;
} else {
/* This lock request was already granted.
Just clearing the flag. */ lock->type_mode &= ~LOCK_CONV_BY_OTHER;
}
} /* The trx already has a strong enough lock on rec: do
nothing */
} else if (lock_rec_other_has_conflicting(mode, block, heap_no, trx)) {
//如果出现冲突,即不兼容,则创建一个新lock,且锁模式为 预加锁模式+ lock_wait
/* If another transaction has a non-gap conflicting request in
the queue, as this transaction does not have a lock strong
enough already granted on the record, we have to wait. */ ut_ad(lock == NULL);
enqueue_waiting:
return(lock_rec_enqueue_waiting(mode, block, heap_no,ock, index, thr)); //函数实现
} else if (!impl) {
/* Set the requested lock on the record */ lock_rec_add_to_queue(LOCK_REC | mode, block,heap_no, index, trx); //锁模式加上行锁 函数实现    return(DB_SUCCESS_LOCKED_REC);
} return(DB_SUCCESS);
}

检查已存在的lock的模式是否与欲加锁的模式有冲突,返回true 说明有冲突

static
lock_t*
lock_rec_other_has_conflicting(
/*===========================*/
enum lock_mode mode, /*!< in: LOCK_S or LOCK_X,
possibly ORed to LOCK_GAP or
LOC_REC_NOT_GAP,
LOCK_INSERT_INTENTION */
const buf_block_t* block, /*!< in: buffer block containing
the record */
ulint heap_no,/*!< in: heap number of the record */
trx_t* trx) /*!< in: our transaction */
{
lock_t* lock;lock = lock_rec_get_first(block, heap_no);
if (UNIV_LIKELY_NULL(lock)) {
if (UNIV_UNLIKELY(heap_no == PAGE_HEAP_NO_SUPREMUM)) {
do {
if (lock_rec_has_to_wait(trx, mode, lock,
TRUE)) {
return(lock);
}
lock = lock_rec_get_next(heap_no, lock);
} while (lock);
} else {
do {
if (lock_rec_has_to_wait(trx, mode, lock,
FALSE)) {
return(lock);
}
lock = lock_rec_get_next(heap_no, lock);
} while (lock);
}
} return(NULL);
}
ibool
lock_rec_has_to_wait(
/*=================*/
const trx_t* trx, /*!< in: trx of new lock */
ulint type_mode,/*!< in: precise mode of the new lock
to set: LOCK_S or LOCK_X, possibly
ORed to LOCK_GAP or LOCK_REC_NOT_GAP,
LOCK_INSERT_INTENTION */
const lock_t* lock2, /*!< in: another record lock; NOTE that
it is assumed that this has a lock bit
set on the same record as in the new
lock we are setting */
ibool lock_is_on_supremum) /*!< in: TRUE if we are setting the
lock on the 'supremum' record of an
index page: we know then that the lock
request is really for a 'gap' type lock */
{
ut_ad(trx && lock2);
ut_ad(lock_get_type_low(lock2) == LOCK_REC); if (trx != lock2->trx
&& !lock_mode_compatible(LOCK_MODE_MASK & type_mode,lock_get_mode(lock2))) { //检查锁与锁之间的兼容性, 详情 /* We have somewhat complex rules when gap type record locks
cause waits */ if ((lock_is_on_supremum || (type_mode & LOCK_GAP))&& !(type_mode & LOCK_INSERT_INTENTION)) { /* Gap type locks without LOCK_INSERT_INTENTION flag
do not need to wait for anything. This is because
different users can have conflicting lock types
on gaps. */ return(FALSE);
} if (!(type_mode & LOCK_INSERT_INTENTION)
&& lock_rec_get_gap(lock2)) { /* Record lock (LOCK_ORDINARY or LOCK_REC_NOT_GAP
does not need to wait for a gap type lock */ return(FALSE);
} if ((type_mode & LOCK_GAP)
&& lock_rec_get_rec_not_gap(lock2)) { /* Lock on gap does not need to wait for
a LOCK_REC_NOT_GAP type lock */ return(FALSE);
} if (lock_rec_get_insert_intention(lock2)) { /* No lock request needs to wait for an insert
intention lock to be removed. This is ok since our
rules allow conflicting locks on gaps. This eliminates
a spurious deadlock caused by a next-key lock waiting
for an insert intention lock; when the insert
intention lock was granted, the insert deadlocked on
the waiting next-key lock. Also, insert intention locks do not disturb each
other. */ return(FALSE);
} return(TRUE);
} return(FALSE);
}