InnoDB Buffer Pool缓存了表数据和二级索引在内存中,提高数据库效率,因此设置innodb_buffer_pool_size到合理数值对实例性能影响很大。当size设置偏小,会导致数据库大量直接磁盘的访问,而设置过大会导致实例占用内存太多,容易发生OOM。在MySQL 5.7之前innodb_buffer_pool_size的修改需要重启实例,在5.7后支持了动态修改innodb_buffer_pool_size。本文会根据源码介绍该特性。

innodb_buffer_pool_size 设置范围

innodb_buffer_pool_size默认值是128M,最小5M(当小于该值时会设置成5M),最大为LLONG_MAX。当innodb_buffer_pool_instances设置大于1的时候,buffer pool size最小为1GB。同时buffer pool size需要是innodb_buffer_pool_chunk_size*innodb_buffer_pool_instances的倍数。innodb_buffer_pool_chunk_size默认为128M,最小为1M,实例启动后为只读参数。

  1. static MYSQL_SYSVAR_LONGLONG(buffer_pool_size, innobase_buffer_pool_size,
  2. PLUGIN_VAR_RQCMDARG,
  3. "The size of the memory buffer InnoDB uses to cache data and indexes of its tables.",
  4. innodb_buffer_pool_size_validate,
  5. innodb_buffer_pool_size_update,
  6. static_cast<longlong>(srv_buf_pool_def_size),//128M
  7. static_cast<longlong>(srv_buf_pool_min_size),//5M
  8. LLONG_MAX, 1024*1024L);
  9. #define BUF_POOL_SIZE_THRESHOLD (1024 * 1024 * 1024) //1GB
  10. static
  11. int
  12. innodb_buffer_pool_size_validate(
  13. {
  14. ...
  15. //当srv_buf_pool_instances > 1,要求size不小于1GB。
  16. if (srv_buf_pool_instances > 1 && intbuf < BUF_POOL_SIZE_THRESHOLD) {
  17. buf_pool_mutex_exit_all();
  18. push_warning_printf(thd, Sql_condition::SL_WARNING,
  19. ER_WRONG_ARGUMENTS,
  20. "Cannot update innodb_buffer_pool_size"
  21. " to less than 1GB if"
  22. " innodb_buffer_pool_instances > 1.");
  23. return(1);
  24. }
  25. ...
  26. ulint requested_buf_pool_size
  27. = buf_pool_size_align(static_cast<ulint>(intbuf));
  28. }
  29. /** Calculate aligned buffer pool size based on srv_buf_pool_chunk_unit,
  30. if needed.
  31. @param[in] size size in bytes
  32. @return aligned size */
  33. UNIV_INLINE
  34. ulint
  35. buf_pool_size_align(
  36. ulint size)
  37. {
  38. const ulint m = srv_buf_pool_instances * srv_buf_pool_chunk_unit;
  39. size = ut_max(size, srv_buf_pool_min_size);
  40. if (size % m == 0) {
  41. return(size);
  42. } else {
  43. return((size / m + 1) * m);
  44. }
  45. }

buffer pool resize流程

  • 如果开启了AHI(adaptive hash index,自适应哈希索引)就关闭AHI,这里因为AHI是通过buffer pool中的B+树页构造而来。

  • 如果新设定的buffer pool size小于原来的size,就需要计算需要删除的chunk数目withdraw_target。

  • 遍历buffer pool instances,锁住buffer pool,收集free list中的chunk page到withdraw,直到withdraw_target或者遍历完,然后释放buffer pool锁。

  • 停止加载buffer pool。

  • 如果free list中没有收集到足够的chunk,则重复遍历收集,每次重复间隔时间会指数增加1s、2s、4s、8s…,以等待事务释放资源。

  • 锁住buffer pool,开始增减chunk。

  • 如果改变比较大,超过2倍,会重置page hash,改变桶大小。

  • 释放buffer_pool,page_hash锁。

  • 改变比较大时候,重新设置buffer pool大小相关的内存结构。

  • 开启AHI。

  1. /** Resize the buffer pool based on srv_buf_pool_size from
  2. srv_buf_pool_old_size. */
  3. void
  4. buf_pool_resize()
  5. {
  6. /* disable AHI if needed */
  7. btr_search_disable(true);
  8. /* set withdraw target */
  9. for (ulint i = 0; i < srv_buf_pool_instances; i++) {
  10. if (buf_pool->curr_size < buf_pool->old_size) {
  11. ...
  12. while (chunk < echunk) {
  13. withdraw_target += chunk->size;
  14. ++chunk;
  15. }
  16. ...
  17. }
  18. }
  19. /* wait for the number of blocks fit to the new size (if needed)*/
  20. for (ulint i = 0; i < srv_buf_pool_instances; i++) {
  21. buf_pool = buf_pool_from_array(i);
  22. if (buf_pool->curr_size < buf_pool->old_size) {
  23. should_retry_withdraw |=
  24. buf_pool_withdraw_blocks(buf_pool);
  25. }
  26. }
  27. ...
  28. if (should_retry_withdraw) {
  29. ib::info() << "Will retry to withdraw " << retry_interval
  30. << " seconds later.";
  31. os_thread_sleep(retry_interval * 1000000);
  32. if (retry_interval > 5) {
  33. retry_interval = 10;
  34. } else {
  35. retry_interval *= 2;
  36. }
  37. goto withdraw_retry;
  38. }
  39. ...
  40. /* add/delete chunks */
  41. for (ulint i = 0; i < srv_buf_pool_instances; ++i) {
  42. if (buf_pool->n_chunks_new < buf_pool->n_chunks) {
  43. /* delete chunks */
  44. /* discard withdraw list */
  45. }
  46. }
  47. /* reallocate buf_pool->chunks */
  48. if (buf_pool->n_chunks_new > buf_pool->n_chunks) {
  49. /* add chunks */
  50. }
  51. ...
  52. const bool new_size_too_diff
  53. = srv_buf_pool_base_size > srv_buf_pool_size * 2
  54. || srv_buf_pool_base_size * 2 < srv_buf_pool_size;
  55. /* Normalize page_hash and zip_hash,
  56. if the new size is too different */
  57. }

resize过程中的等待和阻塞

在支持动态修改innodb_buffer_pool_size之前,该值的修改需要修改配置项然后重启实例生效。而重启实例会导致用户连接强制断开,导致一段时间的实例不可用,如果有大事务在回滚就需要等待很长时间。

动态修改innodb_buffer_pool_size只有在收集回收块;查找持有block阻止buffer pool收集回收chunk的事务;resizing buffer pool操作时会阻塞用户写入。而这几部分操作都是内存操作,会较快完成。

如果对innodb_buffer_pool_size修改量很大,同时遇到page cleaner工作时间久,就可能导致一段时间的阻塞。例如下面一个较为极端的例子,innodb_buffer_pool_instances为1,innodb_buffer_pool_size由18GB改为5M,innodb_buffer_pool_chunk_size为1M,page cleaner loop花费近48s,导致收集回收块会花费很长时间,可以看到在测试机器上用时近48s。而这期间的写入操作也会被阻塞。

  1. 02:54:09.798912Z 0 [Note] InnoDB: Withdrawing blocks to be shrunken.
  2. 02:54:09.798935Z 0 [Note] InnoDB: buffer pool 0 : start to withdraw the last 1151680 blocks.
  3. 02:54:57.660725Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 47685ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
  4. 02:54:57.687189Z 0 [Note] InnoDB: buffer pool 0 : withdrawing blocks. (1151680/1151680)
  5. 02:54:57.687237Z 0 [Note] InnoDB: buffer pool 0 : withdrew 1151653 blocks from free list. Tried to relocate 27 pages (1151680/1151680).
  6. 02:54:57.753014Z 0 [Note] InnoDB: buffer pool 0 : withdrawn target 1151680 blocks.
  7. > insert into t values(10000001, 2);
  8. Query OK, 1 row affected (9.03 sec)

正常不需要等待时的内存操作会很快。

  1. 03:31:57.734231Z 0 [Note] InnoDB: Resizing buffer pool from 1887436800 to 5242880 (unit=1048576).
  2. 03:31:58.480061Z 0 [Note] InnoDB: Completed to resize buffer pool from 1887436800 to 5242880.
  3. ...
  4. 03:31:46.453250Z 10 [Note] InnoDB: Resizing buffer pool from 524288 (new size: 1887436800 bytes)
  5. 03:31:57.734231Z 0 [Note] InnoDB: Resizing buffer pool from 1887436800 to 5242880 (unit=1048576).

另一个方面,如果当前有事务占用大量buffer pool数据导致无法收集到足够的chunk,resize过程也会变久。下面极端测试中当执行xa rollback回滚大事务的时候,innodb_buffer_pool_chunk_size由16M改为5M,即等待了较久时间才完成回收chunk的收集。不过这段时间并不会完全阻塞用户的操作。

  1. > xa begin 'y';
  2. Query OK, 0 rows affected (0.00 sec)
  3. > update t set c2 = 2 where c2 =1;
  4. Query OK, 999996 rows affected (8.32 sec)
  5. Rows matched: 999996 Changed: 999996 Warnings: 0
  6. > xa end 'y';
  7. Query OK, 0 rows affected (0.00 sec)
  8. > xa prepare 'y';
  9. Query OK, 0 rows affected (0.11 sec)
  10. > xa rollback 'y';
  11. Query OK, 0 rows affected (10.10 sec)
  12. InnoDB: Resizing buffer pool from 16777216 to 5242880 (unit=1048576).
  13. InnoDB: Withdrawing blocks to be shrunken.
  14. InnoDB: buffer pool 0 : start to withdraw the last 704 blocks.
  15. InnoDB: buffer pool 0 : withdrew 239 blocks from free list. Tried to relocate 126 pages (689/704).
  16. InnoDB: buffer pool 0 : withdrew 0 blocks from free list. Tried to relocate 0 pages (689/704).
  17. ...
  18. InnoDB: Will retry to withdraw 1 seconds later.
  19. InnoDB: buffer pool 0 : start to withdraw the last 704 blocks.
  20. ...
  21. InnoDB: buffer pool 0 : will retry to withdraw later.
  22. InnoDB: Will retry to withdraw 2 seconds later.
  23. ...
  24. InnoDB: buffer pool 0 : will retry to withdraw later.
  25. InnoDB: Will retry to withdraw 4 seconds later.
  26. InnoDB: buffer pool 0 : start to withdraw the last 704 blocks.
  27. ...
  28. InnoDB: Will retry to withdraw 8 seconds later.
  29. InnoDB: buffer pool 0 : withdrawn target 704 blocks.

从上面可以看到innodb_buffer_pool_size的online修改相比重启对用户实例的影响降低了很多,但也最好选择业务低峰期和没有大事务操作时候进行,同时要修改MySQL配置文件,防止重启后恢复到原来的值。