innodb之change buffer被動merge,innodbbuffermerge
被動merge情景一,二級索引頁空間不足:ibuf0ibuf.cc:: ibuf_insert_low
1、當嘗試緩衝插入操作時,如果預估二級索引page的空間不足,可能導致索引分裂,則定位到嘗試緩衝的page no在ibuf tree中的位置,最多merge 8個page,merge方式為非同步,即發起非同步讀取索引頁請求。
說明:
Buffered:當前二級索引頁已經緩衝的記錄
entry_size:待插入的二級索引記錄大小
page_dir_calc_reserved_space(1):待插入記錄的slot大小
ibuf_index_page_calc_free_from_bits(zip_size,bits)):待插入二級索引頁面剩餘空間大小
…
/* Find out the volume of already bufferedinserts for the same index page */
min_n_recs= 0;
buffered= ibuf_get_volume_buffered(&pcur, space, page_no,
op == IBUF_OP_DELETE
? &min_n_recs
: NULL, &mtr);
……
do_merge = FALSE;
……
//開啟待插入ibuf記錄在ibuf tree中的前一個記錄位置
btr_pcur_open(ibuf->index, ibuf_entry,PAGE_CUR_LE, mode, &pcur, &mtr);
……
if (op == IBUF_OP_INSERT){
ulint bits = ibuf_bitmap_page_get_bits(
bitmap_page,page_no, zip_size, IBUF_BITMAP_FREE,&bitmap_mtr);
if(buffered + entry_size + page_dir_calc_reserved_space(1)
>ibuf_index_page_calc_free_from_bits(zip_size, bits)) {
…
do_merge= TRUE;
ibuf_get_merge_page_nos(FALSE,
btr_pcur_get_rec(&pcur),&mtr,
space_ids,space_versions,
page_nos,&n_stored);
……
}
}
……
if(do_merge) {
buf_read_ibuf_merge_pages(false, space_ids,space_versions,
page_nos, n_stored);
}
問題:
1、buffered指什嗎?page_dir_calc_reserved_space(1)是什嗎?
指二級索引頁插入二級索引時,已經緩衝的記錄的個數。當插入二級索引時,如果二級索引頁發生分裂,ibuf樹種記錄的頁面資訊就會部分時效。因此每次插入時需要對bitmap進行判斷。
2、如何判讀merge了8頁?
IBUF_MAX_N_PAGES_MERGED值是8.
Merge頁的個數是8和當前buffer pool大小的1/4最小值
Ibuf0ibuf.cc::ibuf_get_merge_page_nos_func
limit = ut_min(IBUF_MAX_N_PAGES_MERGED, buf_pool_get_curr_size() / 4);
while (*n_stored < limit) {
……
page_nos[*n_stored] = prev_page_no;
……
}
3、merge的哪8個頁?
遞增的8個頁。。。,這8個頁指的是索引頁,還是ibuftree的頁?
首先定位到待插入記錄的前一個記錄的位置,根據前一個記錄依次向前推8個記錄。這8個記錄裡的space id和page no就是需要被merge的二級索引頁的標記。
1、buf0rea.cc::buf_read_ibuf_merge_pages
輸入參數:
Bool Sync:true if the caller wantsthis function to wait for the highest address page to get read in, before this functionreturns
const ulint* space_ids:space id數組
const ib_int64_t* space_versions:the spaces musthave this version number (timestamp), otherwise we discard the read; we usethis to cancel reads if DISCARD + IMPORT may have changed the tablespace size
const ulint* page_nos:讀入的頁號數組
ulint n_stored:頁號數組元素個數
說明:
將數組頁讀入記憶體,merge相關頁
Issues read requests for pages which theibuf module wants to read in, in order to contract the insert buffer tree.Technically, this function is like a read-ahead function
for (i = 0; i < n_stored; i++) {
……
buf_read_page_low(&err, sync && (i + 1 == n_stored),
BUF_READ_ANY_PAGE, space_ids[i],
zip_size, TRUE,space_versions[i],
page_nos[i]);
if (sync) {
/* The i/o is already completed when wearrive from fil_read */
if (!buf_page_io_complete(bpage)) {
return(0);
}
}
……
}
Buf0buf.cc::buf_page_io_complete:非同步讀
…
if (io_type ==BUF_IO_READ){
…
if (uncompressed&& !recv_no_ibuf_operations) {
ibuf_merge_or_delete_for_page(
(buf_block_t*)bpage, bpage->space,
bpage->offset,buf_page_get_zip_size(bpage),
TRUE);
}
}
……
2、ibuf0ibuf.cc:: ibuf_merge_or_delete_for_page
輸入參數:
block: 如果page已經從磁碟讀取,指向pagex-latched
space:index page的space id
page_no:index page的頁號
zip_size:compressed page大小
update_ibuf_bitmap:通常情況下設true。如果已經或者正在刪除tablespace,設false
說明:
當一個二級索引頁從磁碟讀到buffer pool時,應用這個函數,將該頁對應的所有操作merge,刪除insert buffer中的entries。如果這個頁沒有讀,但在buffer pool中建立,那麼刪除他緩衝的操作記錄。
被動merge情景二,insert buffer過大:ibuf0ibuf.cc:: ibuf_insert_low
如果insert buffer大小過大,contract insert buffer,並且放棄本次緩衝。
1、 ibuf->size大於ibuf->max_size+10(單位頁)執行一次同步ibuf merge(ibuf_contract),merge的page no是隨機定位的記錄的page no,最多一次merge 8頁,同時放棄本次緩衝
2、 其中ibuf_max_size=25%*bufferpool size,百分比由innodb_change_buffer_max_size控制,可動態調整。
…
if (ibuf->size >= ibuf->max_size +IBUF_CONTRACT_DO_NOT_INSERT) {
/*Insert buffer is now too big, contract it but do not try to insert */
/* Use synchronous contract (== TRUE) */
ibuf_contract(TRUE);
return(ibuf_merge(0, &n_pages, sync));
return(ibuf_merge_pages(n_pages,sync));
return(DB_STRONG_FAIL);
}
被動merge情景三,insert 操作可能產生ibuf btree分裂:ibuf0ibuf.cc:: ibuf_insert_low
說明:
1、 當ibuf->size<ibuf->max_size時(IBUF_CONTRACT_ON_INSERT_NON_SYNC=0)不做任何操作
2、 當ibuf->size>= ibuf->max_size+5時,ibuf_contract(true),同步一次ibuf merge,位置隨機
3、 當ibuf->max_size< ibuf->size <= ibuf->max_size+5時,ibuf_contract(false),非同步merge,位置隨機。
4、 Merge至少8頁,ut_min(IBUF_MAX_N_PAGES_MERGED, buf_pool_get_curr_size() / 4);
……
if (err == DB_SUCCESS && mode ==BTR_MODIFY_TREE) {
ibuf_contract_after_insert(entry_size);
if (size < max_size +IBUF_CONTRACT_ON_INSERT_NON_SYNC) {
return;
}
sync = (size >= max_size +IBUF_CONTRACT_ON_INSERT_SYNC);
size = ibuf_contract(sync);
return(ibuf_merge(0,&n_pages, sync));
return(ibuf_merge_pages(n_pages,sync));
}
……
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。