innodb 源码分析 --锁
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);
}
innodb 源码分析 --锁的更多相关文章
- InnoDB源码分析--缓冲池(三)
转载请附原文链接:http://www.cnblogs.com/wingsless/p/5582063.html 昨天写到了InnoDB缓冲池的预读:<InnoDB源码分析--缓冲池(二)> ...
- InnoDB源码分析--缓冲池(二)
转载请附原文链接:http://www.cnblogs.com/wingsless/p/5578727.html 上一篇中我简单的分析了一下InnoDB缓冲池LRU算法的相关源码,其实说不上是分析,应 ...
- InnoDB源码分析--事务日志(一)
原创文章,转载请注明原文链接(http://www.cnblogs.com/wingsless/p/5705314.html) 在之前的文章<InnoDB的WAL方式学习>(http:// ...
- innoDB源码分析--缓冲池
最开始学Oracle的时候,有个概念叫SGA和PGA,是非常重要的概念,其实就是内存中的缓冲池.InnoDB的设计类似于Oracle,也会在内存中开辟一片缓冲池.众所周知,CPU的速度和磁盘的IO速度 ...
- JAVA源码分析------锁(1)
http://870604904.iteye.com/blog/2258604 第一次写博客,也就是记录一些自己对于JAVA的一些理解,不足之处,请大家指出,一起探讨. 这篇博文我打算说一下JAVA中 ...
- InnoDB源码分析--事务日志(二)
原创文章,转载请标明原文链接:http://www.cnblogs.com/wingsless/p/5708992.html 昨天写了有关事务日志的一些基本点(http://www.cnblogs.c ...
- MySQL系列:innodb源码分析之内存管理
http://blog.csdn.net/yuanrxdu/article/details/40985363 http://book.2cto.com/201402/40307.html 从MySQL ...
- MySQL系列:innodb源码分析 图 ---zerok的专栏
http://blog.csdn.net/yuanrxdu/article/details/40985363
- AtomicInteger源码分析——基于CAS的乐观锁实现
AtomicInteger源码分析——基于CAS的乐观锁实现 1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时 ...
随机推荐
- springmvc 返回汉字乱码
1.删除配置文件中的<mvc:annotation-driven /> 2.添加如下配置 <bean class="org.springframework.web.ser ...
- VHDL实例化过程
第二步:建立一个名为MUX_0的乘法器 第三步:在程序中例化,看以下程序. -- 该程序用来实现复数的乘法,端口分别定义的复数的 -- 输入的实部和虚部和输出的实部和虚部 LIBRARY IEEE; ...
- Java 窗体居中 通用代码
Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 Dimension screenSize = kit.getScreenSize() ...
- Perf -- Linux下的系统性能调优工具,第 1 部分
Perf 简介 Perf 是用来进行软件性能分析的工具. 通过它,应用程序可以利用 PMU,tracepoint 和内核中的特殊计数器来进行性能统计.它不但可以分析指定应用程序的性能问题 (per t ...
- spark ml 的例子
一.关于spark ml pipeline与机器学习 一个典型的机器学习构建包含若干个过程 1.源数据ETL 2.数据预处理 3.特征选取 4.模型训练与验证 以上四个步骤可以抽象为一个包括多个步骤的 ...
- java web eclipse中项目的加载过程
java web eclipse中项目的加载过程: Tomcat默认从WEB-INF/目录下加载资源,Eclipse在发布程序的时候,并没有把User Libraries的相关资源拷贝到WEB-INF ...
- Python学习-31.Python中集合的一些操作
add方法: s = {1,2,3} s.add(4) print(s)# {1, 2, 3, 4} 同list的append方法,若调用s.add(3),则不会有任何影响.这点与C#中的HashSe ...
- Elasticsearch 在 windows 和 ubuntu 下详细安装过程
1. 前言 作为一名 .NET 平台开发者,选择开发框架时总会面临更多的局限性,不过对于搜索这种刚需服务来说,开源框架可供选择的余地还是比较大的.笔者之前用的是 Lucene.net ,现在深感其使用 ...
- 【转】PHP操作MongoDB【NoSQL】
原文:http://blog.sina.com.cn/s/blog_4b67d3240101519b.html 一.MongoDB简介 MongoDB (名称来自"humongous&quo ...
- 前端项目打包工具weexpack的安装
最下面是本人安装时候的系统环境,本篇文章只限于参考,不一定非得是这样,原因你懂得. 打包的过程中出现的问题 1.执行到weexpack run android的时候,到了resolving class ...