1、AST简介

DLM进程(LMON、LMD)之间的跨实例通信是使用高速互联上的IPC层实现的。为了传递锁资源的状态,DLM使用了异步陷阱(AST),它在操作系统处理程序例程中实现为中断。纯粹主义者可能同意AST的确切含义以及它的实现方式(中断或其他阻塞机制),但对于OPS或Oracle RAC来说,它就是中断。
AST可以是一个"阻塞AST",也可以是一个"获取AST"。当一个进程请求一个资源上的锁时,DLM向当前对同一资源拥有锁的全部进程发出一个阻塞异步陷阱(BAST)。在可能和必要时,这个锁的拥有者会放弃这个锁,允许请求者获取对该资源的访问。DLM将向请求者发送一个获取AST(AAST),通知其现在可以拥有这个资源(和这个锁)。通常将AAST看作进程的"唤醒呼叫"。 
DLM使用两个队列跟踪所有的lock 请求,并用两个ASTs(asynchronous traps)来完成请求的发送和响应,实际就是异步中断(interrupt)或者陷阱(trap)。下图显示的是资源和队列的关系,granted queue中记录的是所有已经获得的lock的进程,而convert queue记录时是所有等待lock的进程。

进程1和进程2拥有数据块S模式的锁,因此在granted queue 中有记录,假设现在进程2要获得X模式的锁,进程2必须先向DLM提出请求;请求提交给DLM后,DLM就要把进程2放在convert queue中。向拥有不兼容模式锁的进程1发送一个blocking  ASTs,这是一个异步请求,所以DLM不必等待响应。当进程1接受到这个BAST之后,就会把这个lock降级为null模式,DLM把进程2的锁模式转换为x模式,如下图所示:

然后,DLM发送一个acquisition ASTn(AAST)给进程2,并把进程2放到Granted queue中,如下图所示,进程2就可以继续处理了:

2、在DLM中如何授予锁
为了说明锁定在OPS的DLM中是如何工作的,请考虑一个由两个节点组成的示例集群,它有一个共享磁盘阵列:
(1) 进程p1需要修改实例1上的一个数据块。p1需要检查这个数据块上是否存在锁,然后才能将它读入实例1上的缓冲区缓存中。
(2) 这个数据块中可能存在锁,也可能不存在,因此LCK进程检查SGA结构,以验证缓冲区锁状态。如果存在锁,那么LCK必须请求DLM对这个锁进行降级。
(3) 如果不存在锁,那么LCK必须在本地实例中创建锁元素(LE),其角色是本地的。
(4) LCK必须以独占模式向DLM请求这个LE。如果该资源由实例1主控,那么LM继续处理。否则,必须将这个请求发送到集群中的主控DLM。
(5) 假定这个锁由实例1主控,则这个实例上的DLM在其DLM数据库中进行本地缓存查询,发现实例2上的一个进程已经对同一数据块拥有独占(EX)锁。
(6) 实例1上的DLM向实例2上的DLM发出一个BAST,请求对此锁进行降级。实例2上的DLM向同一实例上的LCK发出另一个BAST,将这个锁由EX降级为NULL。
(7) 实例2上的进程可能已经更新了这个锁,并且可能还没有提交修改。"脏缓冲区写入器"(DBWR)得到信号,将这个数据块写到磁盘上。在写入确认之后,实例2上的LCK将这个锁降级为NULL,向同一实例上的DLM发送一个AAST。
(8) 实例2上的DLM针对锁状态的修改对本地DLM数据库进行更新,并向实例1上的DLM发送一个AAST。
(9) 实例1上的主控DLM更新主控DLM数据库中这个锁(EX)的状态,现在可以将这个锁授予其实例上的进程。DLM自身将这个锁升级到EX。
(10) 实例1上的DLM现在向本地LCK进程发送另一个AAST,向它通知有关锁授予的情况,而且现在可以从磁盘读取该数据块。

Lock modes

A process running within a VMSCluster may obtain a lock on a resource. There are six lock modes that can be granted, and these determine the level of exclusivity being granted, it is possible to convert the lock to a higher or lower level of lock mode. When all processes have unlocked a resource, the system's information about the resource is destroyed.

  • Null (NL). Indicates interest in the resource, but does not prevent other processes from locking it. It has the advantage that the resource and its lock value block are preserved, even when no processes are locking it.
  • Concurrent Read (CR). Indicates a desire to read (but not update) the resource. It allows other processes to read or update the resource, but prevents others from having exclusive access to it. This is usually employed on high-level resources, in order that more restrictive locks can be obtained on subordinate resources.
  • Concurrent Write (CW). Indicates a desire to read and update the resource. It also allows other processes to read or update the resource, but prevents others from having exclusive access to it. This is also usually employed on high-level resources, in order that more restrictive locks can be obtained on subordinate resources.
  • Protected Read (PR). This is the traditional share lock, which indicates a desire to read the resource but prevents other from updating it. Others can however also read the resource.
  • Protected Write (PW). This is the traditional update lock, which indicates a desire to read and update the resource and prevents others from updating it. Others with Concurrent Read access can however read the resource.
  • Exclusive (EX). This is the traditional exclusive lock which allows read and update access to the resource, and prevents others from having any access to it.

The following truth table shows the compatibility of each lock mode with the others:

Mode NL CR CW PR PW EX
NL Yes Yes Yes Yes Yes Yes
CR Yes Yes Yes Yes Yes No
CW Yes Yes Yes No No No
PR Yes Yes No Yes No No
PW Yes Yes No No No No
EX Yes No No No No No

Obtaining a lock

A process can obtain a lock on a resource by enqueueing a lock request. This is similar to the QIO technique that is used to perform I/O. The enqueue lock request can either complete synchronously, in which case the process waits until the lock is granted, or asynchronously, in which case an AST occurs when the lock has been obtained.

It is also possible to establish a blocking AST, which is triggered when a process has obtained a lock that is preventing access to the resource by another process. The original process can then optionally take action to allow the other access (e.g. by demoting or releasing the lock).

Lock value block

A lock value block is associated with each resource. This can be read by any process that has obtained a lock on the resource (other than a null lock) and can be updated by a process that has obtained a protected update or exclusive lock on it.

It can be used to hold any information about the resource that the application designer chooses. A typical use is to hold a version number of the resource. Each time the associated entity (e.g. a database record) is updated, the holder of the lock increments the lock value block. When another process wishes to read the resource, it obtains the appropriate lock and compares the current lock value with the value it had last time the process locked the resource. If the value is the same, the process knows that the associated entity has not been updated since last time it read it, and therefore it is unnecessary to read it again. Hence, this technique can be used to implement various types of cache in a database or similar application.

Deadlock detection

When one or more processes have obtained locks on resources, it is possible to produce a situation where each is preventing another from obtaining a lock, and none of them can proceed. This is known as a deadlock (E. W. Dijkstra originally called it a deadly embrace).[1]

A simple example is when Process 1 has obtained an exclusive lock on Resource A, and Process 2 has obtained an exclusive lock on Resource B. If Process 1 then tries to lock Resource B, it will have to wait for Process 2 to release it. But if Process 2 then tries to lock Resource A, both processes will wait forever for each other.

The OpenVMS DLM periodically checks for deadlock situations. In the example above, the second lock enqueue request of one of the processes would return with a deadlock status. It would then be up to this process to take action to resolve the deadlock—in this case by releasing the first lock it obtained.

DLM分布式锁的实现机制的更多相关文章

  1. python使用redis实现协同控制的分布式锁

    python使用redis实现协同控制的分布式锁 上午的时候,有个腾讯的朋友问我,关于用zookeeper分布式锁的设计,他的需求其实很简单,就是节点之间的协同合作. 我以前用redis写过一个网络锁 ...

  2. ZooKeeper-3.5.6分布式锁

    原理 基本方案是基于ZooKeeper的临时节点与和watch机制.当要获取锁时在某个目录下创建一个临时节点,创建成功则表示获取锁成功,创建失败则表示获取锁失败,此时watch该临时节点,当该临时节点 ...

  3. 循序渐进 Redis 分布式锁(以及何时不用它)

    场景 假设我们有个批处理服务,实现逻辑大致是这样的: 用户在管理后台向批处理服务投递任务: 批处理服务将该任务写入数据库,立即返回: 批处理服务有启动单独线程定时从数据库获取一批未处理(或处理失败)的 ...

  4. etcd实现分布式锁

    转载自:etcd实现分布式锁 当并发的访问共享资源的时候,如果没有加锁的话,无法保证共享资源安全性和正确性.这个时候就需要用到锁 1.需要具备的特性 需要保证互斥访问(分布式环境需要保证不同节点.不同 ...

  5. 【连载】redis库存操作,分布式锁的四种实现方式[三]--基于Redis watch机制实现分布式锁

    一.redis的事务介绍 1. Redis保证一个事务中的所有命令要么都执行,要么都不执行.如果在发送EXEC命令前客户端断线了,则Redis会清空事务队列,事务中的所有命令都不会执行.而一旦客户端发 ...

  6. redis事务机制和分布式锁

    Redis事务机制 严格意义来讲,Redis的事务和我们理解的传统数据库(如mysql)的事务是不一样的:Redis的事务实质上是命令的集合,在一个事务中要么所有命令都被执行,要么所有事物都不执行.  ...

  7. ETCD分布式锁实现选主机制(Golang实现)

    ETCD分布式锁实现选主机制(Golang) 为什么要写这篇文章 做架构的时候,涉及到系统的一个功能,有一个服务必须在指定的节点执行,并且需要有个节点来做任务分发,想了半天,那就搞个主节点做这事呗,所 ...

  8. Zookeeper分布式协调即分布式锁机制

    主要用到的Zookeeper机制: 临时+有序节点,节点watch机制 过程: 发生分布式锁竞争时,参与竞争的各个客户端服务都到Zookeeper的同一父节点(代表着同一把锁)下建立自己的临时+有序子 ...

  9. 图解Janusgraph系列-并发安全:锁机制(本地锁+分布式锁)分析

    图解Janusgraph系列-并发安全:锁机制(本地锁+分布式锁)分析 大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 图数据库文章总目录: 整理所有图相关文章,请移步(超链):图数据 ...

随机推荐

  1. android 拖拽图片&拖动浮动按钮到处跑

    来自老外: 拖拽图片效果 方法一: 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  2. AOP分析

    cglib动态代理 Waiter target = new NaiveWaiter();//一个实现了Waiter接口的类 BeforeAdvice advice = new GreetingBefo ...

  3. 1、gitlab的理论知识

    2.1 svn与git对比 . svn git 分布式 不是 是 在线阅读 不支持 不仅支持,而且可以在线编辑 存储方式 按文件 按元数据 完整性 一般 优 离线工作 日志都没法看 完全没问题 分支 ...

  4. 3、kvm配置vnc

    配置kvm通过vnc访问 virsh edit privi-server 添加如下配置: <graphics type='vnc' port='5901' autoport='no' liste ...

  5. js 中的 for 循环。。。

    for (var i in data){ data[i] } 和for (var i=0;i< data.length; i++){ data[i] } 第一种可能会有bug...

  6. BufferedReader readLine

    import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.digest.DigestUtils; In ...

  7. CollectionUtils.select 集合筛选

    import org.apache.commons.collections.CollectionUtils;import org.apache.commons.collections.Predicat ...

  8. spark_运行spark-shell报错_javax.jdo.JDOFatalDataStoreException: Unable to open a test connection to the given database.

    error: # ./spark-shell Caused by: javax.jdo.JDOFatalDataStoreException: Unable to open a test connec ...

  9. Microsoft JDBC Driver 使用 getParameterMetaData 会报错?

    不知道为何使用 Microsoft JDBC Driver for SQL Server 驱动时,sql语句不带参数没有问题,但是如果带参数且使用 getParameterMetaData 就会提示某 ...

  10. js中的focus()聚焦

    document.getElementById("vin").focus();document.form1.name.focus() $(document).ready(funct ...