Based on sorting out the relevant content of DB2 LOG, this chapter sorts out the contents of lock, comparing with log, lock The content is more related to applications, and all aspects of the content are more complex. The essential difference between a DBMS and a file system lies in the transactions supported by the DBMS system. If the introduction of log is to ensure persistence, then the introduction of LOCK is to ensure transaction serialization, solving the concurrency of transactions and bringing about resource RACE CONDITION. Different transaction scenarios define different concurrency requirements. For this purpose, four transaction isolation levels are defined. Different DBMS introduces different lock mechanisms to achieve this. The serialization mechanism of DB2 is mainly implemented through LOCK, LATCH, CLAIM/DRAIM. The specific LOCK related attributes and the impact of LOCK attributes on applications such as OBJECT, SIZE, MODE, DURATION, PARTICIPANTS, PARAMETER LOCATIONS are introduced. . When DBMS CLUSTER (DATA SHARING GROUP in DB2 and ORACEL RAC in ORACLE) was introduced, in order to handle data consistency between different MEMBERs, DB2 introduced PHYSICAL LOCK and CF LOCK STRTURE to implement global LOCK conflict detection. In a single SUBSYSTEM, the main conflicts are TIMEOUT and DEADLOCK. In the DATASHARING GROUP, new CONTENTION, XES CONTENTION, FALSE CONTENTION, and GLOBAL LOCK CONTENTION are introduced. The introduction of LOCK inevitably brings about a new OVERHEAD. DB2 mainly handles lock resource requests through IRLM, XES, CF, etc. In addition to address space lock requests, due to the unreasonable design of the application itself or some specific The scenario brings many problems, such as TEMEOUT, DEADLOCK, LOCK ESCALATION, etc. How to effectively avoid these problems requires the joint efforts of system operation and maintenance personnel and developers.
The context of this article is based on my personal level of understanding of DBMS LOCK. The logic and rationality of the writing, as well as the breadth and depth of knowledge in organizing the content, all need further consideration.
This blog lasted nearly 2 weeks from the time I started writing to the completion of the general framework. It should be the longest blog I have ever written. Even so, every time I check it, I still have something new to post. I will add it myself, and I will continue to add my own understanding later.
DBMS区别与文件系统的最本质区别:DBMS支持事务
DBMS :Allowing multiple users to access a database simultaneously without compromising data integrity.
A transaction (or unit of work)is a recoverable sequence of one or more SQL operations that are grouped together as a single unit, usually within an application process.
One of the mechanisms DB2 uses to keep data consistent is the transaction. A transaction or(otherwise known as a unit of work) is a recoverable sequence of one or more SQL operations that are grouped together as a single unit, usually within an application process. The initiation and termination of a single transaction defines points of data consistency within a database; either the effects of all SQL operations performed within a transaction are applied to the database and made permanent (committed), or the effects of all SQL operations performed are completely "undone" and thrown away (rolled back).
事务并发带来的问题,为了解决这些问题,DB2定义了4种事务隔离级别。
Occurs when two transactions read the same data, both attempt to update the data read, and one of the updates is lost
Transaction 1 and Transaction 2 read the same row of data and both calculate new values for that row based upon the original values read. If Transaction 1 updates the row with its new value and Transaction 2 then updates the same row, the update operation performed by Transaction 1 is lost
Occurs when a transaction reads data that has not yet been committed
Transaction 1 changes a row of data and Transaction 2 reads the changed row before Transaction 1 commits the change. If Transaction 1 rolls back the change, Transaction 2 will have read data that theoretically, never existed.
Occurs when a transaction executes the same query multiple times and gets different results with each execution
Transaction 1 reads a row of data, then Transaction 2 modifies or deletes that row and commits the change. When Transaction 1 attempts to reread the row, it will retrieve different data values
Occurs when a row of data that matches some search criteria is not seen initially
Transaction 1 retrieves a set of rows that satisfy some search criteria, then Transaction 2 inserts a new row that contains matching search criteria for Transaction 1’s query. If Transaction 1 re-executes the query that produced the original set of rows, a different set of rows will be retrieved – the new row added by Transaction 2 will now be included in the set of rows returned
这里的object是指锁所施加的对象,不同的锁所能适用的对象是不同的。如PAGE LOCK的对象肯定是PAGE.
OWNER 表示谁持有锁,有的是TRAN,有的是DB2 MEMBER
LOCK PARTICIPANTS
Locking is a complex interaction of many parts.
Page/Row locks are not compatible with tablespace locks The solution is "Intent Locking" Regardless of the locksize, DB2 will always start with a tablespace lock
S | U | X | |
---|---|---|---|
S | Y | Y | N |
U | Y | N | N |
X | N | N | N |
持有数据的时间:commit,across commit(with hold)
FROM:START OF FIRST USE
TO :COMMIT OR MOMENTIALIY
When modifying data (Insert, Update and Delete),DB2 determines all locking actions
每一种事务隔离级别锁所能解决的问题
DECLARE upd_cur CURSOR FOR SELECT data1, data2 FROM table FOR UPDATE OF colx
DECLARE ro_cur CURSOR FOR SELECT DEPTNO, AVG(SALARY) FROM EMP GROUP BY DEPTNO
DECLARE amb_cur CURSOR FOR SELECT data1, data2 FROM table
下面分别介绍了系统参数,应用参数如何影响LOCK行为
OBJECT DEFINITION,CREATE TABLESPACE:
NOFOR Option:NO FOR update clause mandatory for positioned updates
STDSQL(YES): Implies NOFOR
ZPARM | MEANING |
---|---|
URCHKTH | UR Checkpoint Frequency Threshold |
URLGWTH | UR Log Write Threshold |
LRDRTHLD | Long-Running Reader |
RELCURHL | Release Held Lock |
EVALUNC | Evaluate Uncommitted |
SKIPUNCI | Skip Uncommitted Inserts |
RRULOCK | U lock for RR/RS |
XLKUPDLT | X lock for searched U/D |
NUMLKTS | Locks per Table(space) |
NUMLKUS | Locks per User |
BETWEEN SQL AND SQL:DB2 USE LOCKS
BETWEEN UTILITY AND SQL:DB2 USE CLAIM AND DRAIN
BASE:
FOR EACH OBJECT(TABLESPACE,PARTITION,TABLE,IDNEXSPACE),THERE IS A CLAIM-COUNT
WHICH IS INCREASED 1 AT START AND DECREASE BY 1 AT COMMIT
A utility starts with a Drain
No new Claims are allowed (wait, maybe timeout)
If the Claim-count >0 the utility waits (maybe timeout)
When the Claim-count reaches zero, the utility can continue
Two important situations:
A claim is a notification to DB2 that a particular object is currently being accessed. Claims usually do not continue to exist beyond the commit point one exception being a Cursor with Hold. In order to access the DB2 object within the next unit of work, an application needs to make a new claim.Claims notify DB2 that there is current interest in or activity on a DB2 object.Even if Uncommitted Read doesn’t take any locks, a claim is taken. As long as there are any claims on a DB2 object, no drains may be taken on the object until those claims are released.
A drain is the action of obtaining access to a DB2 object, by:Preventing any new claims against the object.Waiting for all existing claims on the object to be released.
A drain on a DB2 object causes DB2 to quiesce all applications currently claiming that resource, by allowing them to reach a commit point but preventing them (or any other application process) from making a new claim.A drain lock also prevents conflicting processes from trying to drain the same object at the same time.
Utilities detect claimers are present and wait
Drain Write waits for all write claims to be released
Drain All waits for claims on all classes to be released
SHRLEVEL(CHANGE) Utilities are CLAIMers
Latches – managed by DB2
? BM page latching for index and data pages
? DB2 internal latching (many latches grouped into 32 latch classes)
? Latches – managed by IRLM
? Internal IRLM serialization
虽然标题是advanced topics,其实涉及的内容并没有本质的区别,只不过我们在看待问题,分析问题时需要从单个DB2 SUBSYSTEM的视角上升到DBMS CLUSTER的高度,即DATA SHARING GROUP的高度。为了处理不同的member之间共享数据的COHERENCY问题,DB2引入P-LOCK,区别主要在于单个DB2 SUBSYSTEM中的LOCK owner均为tran,而P-LOCK的owner为DB2 SUBSYSTEM.因此这里的内容按照这种分类进行介绍L-LOCK,P-LOCK,RETAINED LOCK,这几种锁之间的区别需要了解。
特点:
THE KIND YOU HAVE IN BOTH DATA SHARING AND NON DATA SHARING SUBSYTEM THEN CONTROL DATA CONCRRRENCY OF ACCESS TO OBJECTS THEN CAN BE LOCAL OR GLOBAL THEY ASSOTIATED WITH PROGRAMS
WHY IS HIERACHICAL
SO THE MEMBER DB2 SUBSYSTEM WILL NOT PROPAGATE L-LOCKS TO THE CF UNNECESSARILY
特点:
EXCLUSIVE TO DATA SHARING
ARE ONLY GLOBAL
USED TO MAINTAIN DATA COHERENCY IN DATA SHARING
ALSO USED TO MAINTAIN EDMPOOL CONSISTENCY AMONG MEMBERS
ASSOTIATED BY(OWNED BY) DB2 MEMBERS
P-LOCK CAN BE NEGOTIATTED BETWEEN DB2 MEMBERS
NO TIMEOUT OR DEADLOCK DETECTION
分类
<img src="http://www.bkjia.com/uploads/allimg/160227/11433Q233-1.png" style="max-width:90%" style="max-width:90%" alt="">The ONLY way to clear retained locks is by estarting the failed DB2 subsystem.
A LOCK THAT A DB2 SHARING DB2 MEMBERHAS TOMAKE KNOWN TO OTHERS MEMBERS OF THIS DATA SHARING GROUP
THE IRLM VIA XES,PROPAGATE TO THES LOCKS TO THE STRUCTURE IN THE CF
XCF,XES的内容在XES contention部分介绍。
HOW ARE GLOBAL LOCK REQUESTS MADE KNOWN TO OTHER DB2 MEMBERS?
THEY ARE PROPAGATED TO DB2 LOCK STRUCTURE
与APPLICATION LOCK不同的是,PHYSCIAL LOCK 可以做Page Set P-Lock Negotiation。
上图展示了INERDB2 READ/WRITE INTEREST场景下的PAGE SET P-LOCK NEGOTIATION。所谓的negotiation就是锁请求方以及锁申请方将锁请求均DOWNGRADE,从而达到compatible的目的。需要注意的是,PAGE SET P-LOCK STATE状态的变化需要涉及IRLM以及XCF通讯机制。PAGE SET P-LOCK对GLOBAL BUFFER POOL起一种标志作用,
对于READ的DB2 SUBSYSTEM, must register its
interest in read pages in the GBP directory
对于UPDATER的DB2 SUBSYSTEM,must write updated pages to the group buffer pool and register those updated pages in the GBP directory
DEVIDE INTO TWO PARTS OF CF LOCK STRUCTURE RATIO OF LAST TWO COMPONETS DEFAULT ROUGHLY 50%CAN BE ALTERED BY IRLM PARAMETER
OVERVIEW OF MRL STRUCTURE
ACTIVEstatus of active means that the DB2 system is running.
Retained, on the other hand, means that a DB2 and/or its associated IRLM has failed
CONTAINS ENTRIES FOR ALL CRURENTLY HELD MODIFY LOCKS
IN THE EVENT OF DB2 FAILTURE ENSURE THAT OTHER MEMBER OF DB2 KNOW WHICH LOCKS MUST BERETIANED
It is essentially a list structure that lists all the update or modify locks in the data sharing group
DISPLAY GROUP ; Display “LIST ENTRIES IN USE”
NUMBER LIST ENTRIES : 7353, LIST ENTRIES IN USE: 1834
WARNING: When the LIST ENTRIES IN USE catches up to the NUMBER LIST ENTRIES, new modify lock requests will not be granted, and transactions will begin to fail with -904s.
A 'POOL' OF HASH POINTERS
CONTAINS ENTRIES FOR ALL READ AND MODIFY LOCKS
USED FOR GLOBAL LOCK CONTENTION
It is very difficult to keep track of how full a randomly populated table is, so the only way to track its utilization is indirectly. In DB2 data sharing, that tracking mechanism is false contention
SIZE OF EACH INDIVIDUAL ENTRY SET BY MAXUSRS PARAMETER IN IRLMPROC OF FIRST IRLM JOIN THE GROUP
NUMBER OF MEMBER IN THE GROUP
其实主要的目的就是提供INTER-SUBSYTEM之间通讯的API,主要的功能有三个:
action | DB2 member interest page set p lock | STAE CHANGE |
---|---|---|
open | PP-LOCK S/IS | NONE->R/O |
PSEUDO OPEN | PP-LOCK IX/SIX/X/U | R/O->R/W |
PSEDUO CLOSE | PP-LOCK S/IS | R/W->R/O |
Physical CLOSE | NONE | R/O->NONE |
The root cause is that XES itself is designed to only support two lock types,
?XES Contention = XES-level resource contention as XES
only understands S or X
eg member 1 asking for IX and member 2 for IS
Big relief in V8
is mainly caused by the definition of DB2 lock table being too small or the hash algorithm not being discrete enough Well, conflicts resulting from different DB2 resources hashing to the same lock table hash class can be resolved by enlarging the CF lock table, so FALSE CONTENTION marks whether the size of the CF LOCK STRUCTURE is appropriate.
GLOBAL CONTENTIONGLOBAL CONTENTION itself includes XES CONTENTION, FALSE CONTENTION, and REAL CONTENTION. REAL CONTENTION is indeed a resource conflict, such as resource hotspot. The other two conflicts are due to the introduction of DATA SHARING's OVERHEAD
According to the above introduction, the following is a display of A DB2 Performance Tuning Roadmap-2data sharing BLOCK
The L-LOCK (SHARING) part shows the degree of data sharing
P/L XES CONTENTION indicates the proportion of P-LOCK and L-LOCK that need to be sent to CF. That