码迷,mamicode.com
首页 > 数据库 > 详细

innodb之change buffer被动merge

时间:2015-08-08 01:20:37      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

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

}

……

版权声明:本文为博主原创文章,未经博主允许不得转载。

innodb之change buffer被动merge

标签:

原文地址:http://blog.csdn.net/yanzongshuai/article/details/47349111

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!