被动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));

}

……