在前面的blog中,我們知道,redo entries寫入log buffer大致的過程如下:
在PGA中生產Redo Entry -> 服務進程擷取Redo Copy latch(存在多個---CPU_COUNT*2) -> 服務進程擷取redo allocation latch(僅1個) -> 分配log buffer ->
釋放redo allocation latch -> 將Redo Entry寫入Log Buffer -> 釋放Redo Copy latch
由於log buffer是一塊“共用”記憶體,為了避免衝突,它是受到redo allocation latch保護的,每個server process需要先擷取到該latch才能分配redo buffer。因此,在OLTP系統中,我們通常可以觀察到redo allocation latch的等待事件。
oracle引入shared strand和private strand來實現並行redo buffer分配機制,藉此避免高並發下的redo allocation latch等待事件。
1 shared strand
為了減少redo allocation latch等待事件,oracle引入了log buffer的並行機制。其基本原理是,將log buffer劃分為多個小的buffer,這些小的buffer被稱作shared strand。每一個shared strand受到一個單獨的redo allocation latch的保護。多個shared strand的出現,使得原來序列化的redo buffer分配變成了並行的過程,從而減少了redo
allocation latch的等待。
shared strand由一些隱藏參數控制:
09:39:59 sys@ORCL (^ω^) col name for a2509:42:11 sys@ORCL (^ω^) col value for a1009:42:11 sys@ORCL (^ω^) col description for a5509:42:11 sys@ORCL (^ω^) select a.ksppinm name,b.ksppstvl value,a.ksppdesc description09:42:11 2 from x$ksppi a,x$ksppcv b09:42:11 3 where a.indx = b.indx09:42:11 4 and a.ksppinm like '%_log_parallelism%'09:42:13 5 /NAME VALUE DESCRIPTION------------------------- ---------- -------------------------------------------------------_log_parallelism 1 Number of log buffer strands_log_parallelism_max 2 Maximum number of log buffer strands_log_parallelism_dynamic TRUE Enable dynamic strands --控制是否允許shared strand數量在_log_parallelism和_log_parallelism_max之間動態變化
每一個shared_strand的大小=log_buffer/(shared_strand的數量):
12:55:40 sys@ORCL (^ω^) select indx,strand_size_kcrfa from x$kcrfstrand where last_buf_kcrfa != '00'; INDX STRAND_SIZE_KCRFA---------- ----------------- 0 3512320 1 351232012:58:46 sys@ORCL (^ω^) show parameter log_bufferNAME_COL_PLUS_S TYPE VALUE_COL_PLUS_--------------- --------------- ---------------log_buffer integer 702464012:58:57 sys@ORCL (^ω^) select 7024640/2 from dual; 7024640/2---------- 3512320
關於shared strand的數量設定,16個cpu之內最大預設為2,當系統中存在redo allocation latch等待時,每增加16個cpu可以考慮增加1個strand,最大不應該超過8。
並且_log_parallelism_max不允許大於cpu_count。
2 private strand
為了進一步降低redo buffer的衝突,10g引入了private strand機制,這是從shared pool中分配出來的一塊記憶體空間。每一個Private strand受到一個單獨的redo allocation latch保護,每個Private strand作為“私人的”strand只會服務於一個活動事務。擷取到了Private strand的使用者事務不是在PGA中而是在Private strand產生Redo。當flush
private strand或者commit時,Private strand被批量寫入log檔案中。如果新事務申請不到Private strand的redo allocation latch,則會繼續遵循舊的redo buffer機制,申請寫入shared strand中。對於未能擷取到Private strand的redo allocation latch的事務,在事務結束前,即使已經有其它事務釋放了Private strand,也不會再申請Private strand了。
整個事務過程大致如下:
事務開始 -> 申請Private strand的redo allocation latch (申請失敗則申請Shared Strand的redo allocation latch) -> 在Private strand中生產Redo Enrey ->
Flush/Commit ->申請Redo Copy Latch -> 服務進程將Redo Entry批量寫入Log File -> 釋放Redo Copy Latch -> 釋放Private strand的redo allocation latch
12:59:30 sys@ORCL (^ω^) select * from v$sgastat where name like '%strand%';POOL NAME BYTES------------------------ --------------- ----------shared pool private strands 119808013:12:31 sys@ORCL (^ω^) select indx,strand_size_kcrfa from x$kcrfstrand where last_buf_kcrfa = '00'; INDX STRAND_SIZE_KCRFA---------- ----------------- 2 66560 3 66560 4 66560 5 66560 6 66560 7 66560 8 66560 9 66560 10 66560 11 66560 12 66560 13 66560 14 66560 15 66560 16 66560 17 66560 18 66560 19 66560已選擇18行。14:53:42 sys@ORCL (^ω^) select 66560*18 from dual; 66560*18---------- 1198080
每個Private strand的大小為65K。Private strand的數量受到2個方面的影響:min(logfile的大小,活躍事務數量)
2.1 logfile的大小
參數_log_private_mul指定了使用多少logfile空間預分配給Private strand,預設為5。我們可以根據當前logfile的大小(要除去預分配給log buffer的空間)計算出這一約束條件下能夠預分配多少個Private strand。
15:27:25 sys@ORCL (^ω^) select bytes from v$log where status='CURRENT'; BYTES---------- 5242880015:32:30 sys@ORCL (^ω^) select trunc(((select bytes from v$log where status = 'CURRENT') - (select to_number(value) from v$parameter where name = 'log_buffer'))*15:35:16 2 (select to_number(val.KSPPSTVL) from sys.x$ksppi nam, sys.x$ksppsv val where nam.indx = val.indx AND nam.ksppinm = '_log_private_mul') / 100 / 66560)15:35:16 3 as "calculated private strands"15:35:16 4 from dual;calculated private strands-------------------------- 3415:35:22 sys@ORCL (^ω^) select count(1) "actual private strands" from x$kcrfstrand where last_buf_kcrfa = '00';actual private strands---------------------- 1815:36:03 sys@ORCL (^ω^) alter system switch logfile;系統已更改。15:36:22 sys@ORCL (^ω^) select bytes from v$log where status='CURRENT'; BYTES---------- 5242880015:36:46 sys@ORCL (^ω^) select trunc(((select bytes from v$log where status = 'CURRENT') - (select to_number(value) from v$parameter where name = 'log_buffer'))*15:36:59 2 (select to_number(val.KSPPSTVL) from sys.x$ksppi nam, sys.x$ksppsv val where nam.indx = val.indx AND nam.ksppinm = '_log_private_mul') / 100 / 66560)15:37:00 3 as "calculated private strands"15:37:00 4 from dual;calculated private strands-------------------------- 3415:37:01 sys@ORCL (^ω^) select count(1) "actual private strands" from x$kcrfstrand where last_buf_kcrfa = '00';actual private strands---------------------- 1815:37:19 sys@ORCL (^ω^)
2.2 活躍事務數量
參數_log_private_parallelism_mul用於推算活躍事務數量在最大事務數量中的百分比,預設為10。Private strand的數量不能大於活躍事務的數量。
15:37:19 sys@ORCL (^ω^) show parameter transactionsNAME_COL_PLUS_S TYPE VALUE_COL_PLUS_--------------- --------------- ---------------transactions integer 187transactions_pe integer 5r_rollback_segment15:42:47 sys@ORCL (^ω^) select trunc((select to_number(value) from v$parameter where name = 'transactions') *15:44:33 2 (select to_number(val.KSPPSTVL)15:44:33 3 from sys.x$ksppi nam, sys.x$ksppsv val15:44:33 4 where nam.indx = val.indx15:44:33 5 AND nam.ksppinm = '_log_private_parallelism_mul') / 100 )15:44:33 6 as "calculated private strands"15:44:33 7 from dual;calculated private strands-------------------------- 1815:44:35 sys@ORCL (^ω^) select count(1) "actual private strands" from x$kcrfstrand where last_buf_kcrfa = '00';actual private strands---------------------- 18