Enqueue Locks

Many of Oracle's locks are called enqueue locks. To enqueue a lock request is to place that request on the queue for its resource. So although the word "enqueue"is strictly speaking a verb, it is used adjectivally in the term enqueue lock. It is also used as a noun when referring to a particular enqueue resource, such as the CF (control file) enqueue.Oracle uses two classes of local locks—those for which the lock and resource data structures are dynamically allocated in the shared pool, and those that use fixed arrays for the lock and resource data structures. Although almost all types of lock requests may be enqueued, the term enqueue should be taken to refer exclusively to those locks that use the fixed arrays for the lock and resource data structures,unless otherwise qualified.

(许多oracle’s locks 称为enqueue locks. 锁请求排队就是放置该锁请求到资源的队列中。Oracle使用两种类型的local locks,对于这些lockslock and resource data structuresshared pool中动态的分配,并且对lock and resource data structures使用fixed arrays。因此术语Enqueue应专指这些lockslock and resource data structures使用fixed arrays。)


4.3.1 Enqueue Resources

The fixed array for enqueue resources is sized by the ENQUEUE_RESOURCES parameter. The number of slots in this array that are in use varies from time to time, and these can be seen in V$RESOURCE . Each row in V$RESOURCE represents a resource that is currently locked in any mode by one or more sessions. V$RESOURCE 中的行表示当前锁住的资源,也就是hash table中的hash buckethash chain),参数ENQUEUE_RESOURCES决定hash table的大小)These resources are not persistent in that they are no longer defined once all locks on the resource have been released.(这些resources不是persistent,因为资源上的所有锁释放了,不再定义这些资源)

Rows in V$RESOURCE are identified by a two-character code representing the type of resource, and two numeric fields used to encode either the resource identity or the activities protected by locks on the resource, depending on the resource type. For example, resources of type TX represent entries in the

transaction table of a rollback segment. The high-order two bytes of the first

identifier contain the rollback segment number, and the low-order two bytes

contain the transaction table slot number, while the second identifier contains

the rollback segment wrap or sequence number.

All enqueue operations access the enqueue resource structure via a hash table.

The hash value is based on the resource type and the numeric identifiers. The

length of the enqueue hash table is set by the _ENQUEUE_HASH (其实是决定hash table的长度,由procerss参数决定)parameter.

The default value of this parameter is derived directly from the PROCESSES

parameter, as follows:

45 + 2 * ( PROCESSES + PROCESSES/10 )

Because _ENQUEUE_HASH is derived directly from PROCESSES rather than from ENQUEUE_RESOURCES, it may be necessary to tune _ENQUEUE_HASH explicitly if ENQUEUE_RESOURCES has been raised significantly from its default value. Otherwise lengthy enqueue hash chains may develop. _ENQUEUE_HASH processes参数决定,而不是ENQUEUE_RESOURCES ,如果ENQUEUE_RESOURCES 增加很多,有必要调整_ENQUEUE_HASH,否则形成过长的hash链)

As with all hash tables, if you have cause to tune the number of buckets, you should make it a prime number (see Hash Tables and Prime Numbers). The enqueue hash chains are accessed under the protection of the enqueue hash chains latches. The number of child enqueue hash chains latches is set by the

_ENQUEUE_HASH_CHAIN_LATCHES parameter, which defaults to the

CPU_COUNT. In a high concurrency environment, sleeps may be recorded

against the enqueue hash chains latches if the hash chains are allowed to become unduly long. However, sleeps against these latches should normally be regarded as a secondary result of contention for a higher-level latch, rather than attributed to long hash chains.

Hash Tables and Prime Numbers

Oracle uses hash tables internally so that objects can be located

efficiently. For example, a hash table is used to locate database blocks in

the buffer cache, and another hash table is used to locate named objects

in the library cache.

To locate an object via a hash table, Oracle uses an algorithm to convert

the object's name or identifier into a number. That number may be

much larger than the size of the hash table, so it is converted to an index

into the hash table using a simple modulus function.

Multiple objects may map to the same hash table entry. This is called a

hash collision. Oracle normally resolves hash collisions using collision

chains. This means that objects that map to the same hash table entry

are linked together using a chain of pointers. These objects are said to

fall into the same hash bucket.

The performance of hash-based access is sensitive to the length of the

hash chains because they must be searched linearly. Therefore hash

tables must be large enough to ensure that the average hash chain

length remains short.

Long hash chains can also develop if the distribution of objects to hash

buckets is uneven. This happens if there is any pattern in the names of

the objects being hashed that the hash function is not able to randomize.

This is surprisingly common.

By making the number of hash buckets a prime number, you can greatly

reduce the risk of any pattern in the hash values resulting in hash

collisions once the modulus function has been applied.

4.3.2 Enqueue Locking

In addition to the enqueue resources, a second fixed array is used for enqueue

locking—namely, the enqueue locks themselves. The size of the enqueue locks

fixed array is set by the _ENQUEUE_LOCKS parameter, and the active rows can be seen in V$ENQUEUE_LOCK .(第二个fixed array用于enqueue

locks。该enqueue locks fixed array的大小由参数_ENQUEUE_LOCKS决定,并且active rows可以在V$ENQUEUE_LOCK中看到

An enqueue lock structure is used by each session waiting for or holding a lock on a resource. If one or more sessions are waiting for locks on a resource, then their enqueue lock structures are linked together into a two-way linked list, with the enqueue resource structure as the list header. This linked list is maintained and serviced in the order in which the locks were requested. For example, if a lock is held in shared mode, and the first waiter requires access to the resource in exclusive mode, then other sessions that require shared access must queue for the resource behind the first waiter, despite the fact that their requests are compatible with the mode in which the resource is currently locked.

Similar two-way linked lists are used to link together the enqueue lock structures for sessions holding a lock on the resource, and for sessions waiting to change the mode of the lock that they are holding.

The operation of changing the mode of a lock is called an enqueue conversion.

For example, if a transaction holds a lock on a particular table in sub-share mode, and needs to update a row of that table, then the enqueue lock must be converted to sub-exclusive mode. However, if the resource is currently locked in an incompatible mode by another session, then the conversion cannot proceed

immediately and the enqueue lock structure is placed in the conversion queue.

Enqueue conversions are serviced in order before new enqueue requests.

During enqueue operations, modifications to the enqueue resources and enqueue locks fixed array free lists (see the sidebar, "Fixed Array Free Lists") are made under the protection of the enqueues latch. There is only one enqueues latch, and it is often taken and released twice during the course of a single enqueue operation. However, the relevant enqueue hash chains latch is held for the duration of the operation.

4.3.3 Enqueue Waits

An enqueue wait occurs whenever an enqueue request or enqueue conversion

cannot be granted immediately because another session is holding a lock on the resource in an incompatible mode. The blocked process records an enqueue wait.(由于用不兼容的方式在资源上持有锁, enqueue request or enqueue conversion不能立即进行,发生enqueue wait。被诸塞得进程记录一个enqueue wait

The wait parameters are shown in Table 4.3.

Table 4.3. Wait Parameters (enqueue waits)

Parameter Description

p1

The high-order 2 bytes contain the ASCII codes for the resource type.(高位两个字节表示资源类型(resource type))

The low-order 2 bytes contain the mode in which a lock is required.

p2 The id1 identifier for the resource.(低位两个字节表示锁模式(lock mode

p3 The id2 identifier for the resource.

Whenever a session releases an enqueue lock, it examines the lock request and

conversion queues for the resource and, if appropriate, posts the next process

that will be able to acquire a lock on the resource.(当会话释放enqueue lock时,它检查该资源的lock request and conversion queues,如果合适,通知下一个进程获得该资源上的锁)

Processes waiting in an enqueue wait also set an alarm before they begin to wait.

The timeout duration is dependent on the type of resource. For most enqueues,the enqueue wait timeout is 3 seconds.

Consecutive waits during a single attempt to acquire an enqueue lock are

recorded as separate waits in the session and system wait statistics. However, the enqueue waits statistic in V$SYSSTAT is only incremented by one, after the lock has been acquired, as are the enqueue requests and enqueue conversions statistics. Note also that the enqueue timeouts statistic in V$SYSSTAT does not represent the number of enqueue wait timeouts. Rather, this statistic is incremented when an enqueue request or enqueue conversion is aborted entirely.(在enqueue wait等待的进程,在进程开始等待前,还要设置an alarmtimeout durationthe type of resource有关,对于大多数enqueuesenqueue wait timeout是三秒。during a single attempt to acquire an enqueue lock,在session and system wait statistics中,Consecutive waits作为separate waits记录。但是,在获得the lock后,V$SYSSTAT中的enqueue waits仅仅增加一次,enqueue requests and enqueue conversions statistics也类似。还要注意V$SYSSTAT中的enqueue timeouts表示he number of enqueue wait timeouts,相反,当enqueue request or enqueue conversion完全放弃后,该统计增加)

This can be due to a distributed transaction timeout, but usually relates to locks requested in no-wait mode.

Fixed Array Free Lists

The free slots in each of Oracle's fixed arrays are maintained on a free list.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9599/viewspace-472965/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9599/viewspace-472965/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值