- 論壇徽章:
- 0
|
Lockwt工具:
使用Esql/C編寫的工具lockwt非常適合用來分析鎖等待的情況。為了能使用該工具,你需要安裝Informix Client SDK及C編譯器,然后編譯該工具。該工具會搜索sysmaster數(shù)據(jù)庫里面的系統(tǒng)表以找到出現(xiàn)的鎖等待情況信息。
該工具將顯示每個會話所占有鎖資源的信息,及哪些會話在等待這些鎖資源釋放。執(zhí)行l(wèi)ockwt -r <#sec> 命令可以按照指定的時間間隔重復收集信息(類似于onstat -r命令)。
該工具可以實時監(jiān)控復雜的鎖資源等待情況,同時以簡單易讀的形式將相關(guān)信息顯示出來。
Listing 5. Lockwt - Description of the output format
Output from lockwt:
-------------------
(0) (1) (2) (3) (4) (5) (6) (7) ( (9)
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
WAIT SID ID PROCNAME USERNAME LKTYPE DATABASE:TABLENAME LKOBJ
0 - 13900:12303 workprocess3 dbuser X rome rders row
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
1 W 53600:23613 batchp12 dbuser rome rders
Colno Purpose
(0) Sequence number
(1) Waiting or not waiting, possible values are:
"-" - this session is the holder of the lock and is always listed first.
"W" - this session(s) is(are) waiting for the above session.
(2) Session ID of this session in the database server
(3) Process ID of the UNIX process, remote connections have pid -1
(4) Process name of the UNIX process. If it is a remote connection
(pid = -1), no process name will be available.
(5) UNIX username of this session
(6) Type of lock, possible values are:
"X" - Exclusive Lock
"S" - Shared Lock
"U" - Update Lock
For additional lock types, execute the following sql-statement:
-> select txt from sysmaster:flags_text where tabname = "syslcktab"
(7) Database name
( Table name, the lock is on. If it is an index lock and the index is detached
from the table (has it's own partition number), the name of that index
is shown here.
(9) Type of object locked, possible values are:
"table" - this is a table lock
"idx" - this is an index key lock
"page" - this is a page lock
"row" - this is a row lock
Listing 6. Lockwt - Lock wait situation I
Output from lockwt:
-------------------
WAIT SID ID PROCNAME USERNAME LKTYPE DATABASE:TABLENAME LKOBJ
0 - 13900:12303 workprocess3 dbuser X rome rders row
1 W 53600:23613 batchp12 dbuser rome rders
本例中,會話13900 (process "workprocess3" 是表orders上鎖資源的擁有者,會話53600正在等待這些鎖資源的釋放,因此你需要通過執(zhí)行onstat -g ses 13900命令去檢查會話13900以證實其運行是否正常。
Listing 7. Lockwt - Lock wait situation II
Output from lockwt:
-------------------
WAIT SID ID PROCNAME USERNAME LKTYPE DATABASE:TABLENAME LKOBJ
0 W 3894: -1 (remote) eherber1 X rome :status row
1 W 17048: 3140 batchp3 dbuser rome :status
0 - 63296: -1 (remote) eherber1 X rome :customer_order row
1 W 3894: -1 (remote) eherber1 rome :customer_order
本例是一個交復雜的情況。會話17048正在等待會話3894設(shè)置在表status上的鎖資源釋放。但是通過檢查下一部分可以發(fā)現(xiàn),會話3894本身也在等待會話63296釋放相應(yīng)的鎖資源。這是一個典型的鎖等待傳遞案例,因為會話3894占據(jù)了別的會話需要使用的鎖資源,而它本身又在等待被其他會話所擁有的鎖資源。因此我們應(yīng)該通過onstat -g ses 63296去具體分析會話63296是否在正常執(zhí)行。
從工具lockwt的源代碼中,你可以抽取那些與sysmaster相關(guān)的查詢語句以形成你自己的查詢語句。
打開游標的問題:
可能你在修改表的時候曾經(jīng)碰到過類似的問題。即使你能夠在該表上設(shè)置排他鎖,你仍然不能對該表進行修改。以下為示例的整個過程:
Listing 8. Non-exclusive access on a table
Output from dbaccess -e stores_demo <script.sql>:
--------------------------------------------------
begin;
Started transaction.
lock table customer in exclusive mode;
Table locked.
alter table customer add (mycol integer);
242: Could not open database table (informix.customer).
106: ISAM error: non-exclusive access.
出現(xiàn)這種情況有可能是由于有其他的用戶在表customer上使用了游標進行數(shù)據(jù)讀取。由于游標并不在具體的數(shù)據(jù)記錄上放置任何鎖,否則我們就不可能能夠?qū)⒃摫硪耘潘姆绞芥i住,但是它卻能防止其他用戶對表的partition信息進行修改。
1、要解決該問題,首先需要找到該表的16進制partnum:
Select hex(partnum) from systables where tabname = "customer".
2、如果partnum為0,那么這可能是一個分片表。你需要執(zhí)行以下命令來找到分片表的partnum:
Select st.tabname, dbinfo("dbspace", sf.partn), hex(sf.partn) from systables st, sysfragments sf, where st.tabid = sf.tabid and sf.fragtype = "T"and st.tabname = "customer".
3、用找到的partnum結(jié)合onstat命令搜索當前打開表的信息:
onstat -g opn | grep -i <hex_partnum>
4、從rstcb字段,它表明了相關(guān)會話線程的內(nèi)存地址信息,據(jù)此信息使用onstat -u命令進行搜索:
onstat -u | grep <rstcb_without_leading_0x>
在確定與此相關(guān)的會話以后,你可以通過onmode -z <sessid>終止相關(guān)的會話。
如果你使用IDS 7.31.xD5, 9.40 or 10,你可以使用環(huán)境變量IFX_DIRTY_WAIT。該環(huán)境變量可以被設(shè)置在服務(wù)器端或客戶端。該變量指定了在執(zhí)行DDL命令時將等待數(shù)據(jù)庫完成修改操作所需的時間。如果指定的時間超時,數(shù)據(jù)庫將返回與沒有設(shè)置該變量時同樣的錯誤。
總結(jié):
在一個擁有眾多并發(fā)事物正在運行的環(huán)境下,實時分析鎖沖突的情況是一個比較艱巨的任務(wù)。但是通過本文對數(shù)據(jù)庫鎖機制的介紹,將有助于大家在分析鎖沖突的時候縮短所需時間。
下載lockwt工具: http://www.herber-consulting.de/ ... .pl?action=IfmxUtil
本文作者:Eric Herber |
|