Repository navigation
MDEV-31956 SSD based InnoDB buffer pool extension - #6197
Open
mariadb-andrzejjarzabek wants to merge 6 commits into
Open
mariadb-andrzejjarzabek wants to merge 6 commits into
mariadb-andrzejjarzabek wants to merge 6 commits into
Conversation
|
|
|
|
mariadb-andrzejjarzabek
marked this pull request as draft
November 26, 2026 00:29
mariadb-andrzejjarzabek
force-pushed
the
main-MDEV-31956
branch
2 times, most recently
from
November 26, 2026 02:56
f5fea0f to
e579caa
Compare
In one of the practical cloud MariaDB setups, a server node accesses its datadir over the network, but also has a fast local SSD storage for temporary data. The content of such temporary storage is lost when the server container is destroyed. The commit uses this ephemeral fast local storage (SSD) as an extension of the portion of InnoDB buffer pool (DRAM) that caches persistent data pages. This cache is separated from the persistent storage of data files and ib_logfile0 and ignored during backup. The following system variables were introduced: innodb_extended_buffer_pool_size - the size of external buffer pool file, if it equals to 0, external buffer pool will not be used; innodb_extended_buffer_pool_path - the path to external buffer pool file. If innodb_extended_buffer_pool_size is not equal to 0, external buffer pool file will be created on startup. Only clean pages will be flushed to external buffer pool file. There is no need to flush dirty pages, as such pages will become clean after flushing, and then will be evicted when they reach the tail of LRU list. The general idea of this commit is to flush clean pages to external buffer pool file when they are evicted. A page can be evicted either by transaction thread or by background thread of page cleaner. In some cases transaction thread is waiting for page cleaner thread to finish its job. We can't do flushing in external buffer pool file when transaction threads are waithing for eviction, that would heart performance. That's why the only case for flushing is when page cleaner thread evicts pages in background and there are no waiters. For this purprose buf_pool_t::done_flush_list_waiters_count variable was introduced, we flush evicted clean pages only if the variable is zeroed. Clean pages are evicted in buf_flush_LRU_list_batch() to keep some amount of pages in buffer pool's free list. That's why we flush every second page to external buffer pool file, otherwise there could be not enought amount of pages in free list to let transaction threads to allocate buffer pool pages without page cleaner waiting. This might be not a good solution, but this is enought for prototyping. External buffer pool page is introduced to store information in buffer pool page hash about the certain page can be read from external buffer pool file. The first several members of such page must be the same as the members of internal page. External page frame must be equal to the certain value to disthinguish external page from internal one. External buffer pages are preallocated on startup in external pages array. We could get rid of the frame in external page, and check if the page's address belongs to the array to distinguish external and internal pages. There are also external pages free and LRU lists. When some internal page is decided to be flushed in external buffer pool file, a new external page is allocated eighter from the head of external free list, or from the tail of external LRU list. Both lists are protected with buf_pool.mutex. It makes sense, because a page is removed from internal LRU list during eviction under buf_pool.mutex. Then internal page is locked and the allocated external page is attached to io request for external buffer pool file, and when write request is completed, the internal page is replaced with external one in page hash, external page is pushed to the head of external LRU list and internal page is unlocked. After internal page was removed from external free list, it was not placed in external LRU, and placed there only after write completion, so the page can't be used by the other threads until write is completed. Page hash chain get element function has additional template parameter, which notifies the function if external pages must be ignored or not. We don't ignore external pages in page hash in two cases, when some page is initialized for read and when one is reinitialized for new page creating. When an internal page is initialized for read and external page with the same page id is found in page hash, the internal page is locked, the external page in replaced with newly initialized internal page in the page hash chain, the external page is removed from external LRU list and attached to io request to external buffer pool file. When the io request is completed, external page is returned to external free list, internal page is unlocked. So during read external page absents in both external LRU and free lists and can't be reused. When an internal page is initialized for new page creating and external pages with the same page id is found in page hash, we just remove external page from the page hash chain and external LRU list and push it to the head of external free list. So the external page can be used for future flushing. The pages are flushed to and read from external buffer pool file with the same manner as they are flushed to their spaces, i.e. compressed and encrypted pages stay compressed and encrypted in external buffer pool file. Reviewed by: Marko Mäkelä
The extended buffer pool (EBP) file was created with OS_FILE_OPEN_OR_CREATE as ext_buffer_pool in innodb_extended_buffer_pool_path or the data directory, locked, and deleted in fil_system_t::close(). If the server was killed, the file was left behind. It is now created with create_temp_file() and MY_TEMPORARY, so that the operating system removes it when the server closes it or exits, and no stale file is left behind after a crash. innodb_extended_buffer_pool_path is now the directory in which the file is created; the data directory is used if it is not set. For this purpose the body of row_merge_file_create_mode() was generalized into pfs_create_temp_file() in fil0fil.cc, which creates the file and registers it with Performance Schema, so that both the merge sort files and the EBP file are created by the same function. The merge sort files keep being created with O_BINARY | O_SEQUENTIAL, as before. OS_FILE_OPEN_OR_CREATE is removed, because it is no longer used. MTR test innodb.ext_buf_pool no longer lists the EBP file, because the file has no fixed name and is unlinked at creation on POSIX systems.
buf_pool_stat_t::n_pages_read_from_ebp and n_pages_written_to_ebp were Atomic_counter<ulint> only in debug builds, and plain ulint fields otherwise. They are incremented without holding buf_pool.mutex, from page cleaner and AIO completion threads, which is a data race and can lose counts. Declare them as Atomic_counter<ulint> in all builds, like n_page_gets. The SHOW_VAR entries keep pointing at the fields, as the layout is the same as that of ulint. buf_read_page_low(): do the debug-only (ib_ext_bp_count_io_only_for_t) counting before space is released, instead of after, because it reads the tablespace name. The set of counted events is unchanged.
buf_flush_LRU_list_batch() wrote every second eligible evicted clean page to the extended buffer pool. Replace that with a dynamic percentage, innodb_extended_buffer_pool_admission_pct (0..100, default 100), so that the write load on the extended buffer pool device can be tuned. 0 stops new admissions; pages already in the extended buffer pool can still be read. buf_pool_t::ext_bp_admit_next() spreads the admissions evenly with a Bresenham-style accumulator: add the percentage, admit and subtract 100 when the sum reaches 100. It costs one add, compare and subtract per candidate and keeps one integer of state. The accumulator is protected by buf_pool.mutex, which the caller already holds; the variable is updated under the same mutex via innodb_buf_pool_update, like innodb_lru_scan_depth. The debug-only innodb_force_LRU_eviction no longer bypasses the admission decision; it only forces the LRU scan. innodb.ext_buf_pool sets the percentage to 100 to keep its previous behaviour. New tests: innodb.ext_buf_pool_admission_pct checks exact admission counts at 100, 80, 50, 30 and 0 percent over 10 candidate pages, and sys_vars.innodb_extended_buffer_pool_admission_pct_basic covers the variable itself.
Fix several problems in the extended buffer pool (EBP) that showed up on buildbot: - When an EBP write completed after its tablespace was dropped, IORequest::write_complete() passed the written copy to buf_LRU_free_page(), which registers it in buf_pool.page_hash under the page id of the dropped tablespace, and leaked it if the page could not be evicted. The new buf_page_ext_write_discard() releases the write-fix and U-latch, returns the EBP page to the free list and tries to evict the page. - buf_page_write_complete() leaked the EBP page slot when the write failed, and when the page could not be evicted after a successful write (so the copy was never registered in buf_pool.page_hash). Both cases now return the slot to the free list. free_ext_page() resets the debug in_page_hash flag, because such slots are freed without having been inserted into page_hash. - On Windows the EBP file was opened through mysys create_temp_file(), without FILE_FLAG_OVERLAPPED and without being bound to srv_thread_pool, so the first asynchronous EBP write crashed in StartThreadpoolIo(). create_ext_file() now creates the file with CreateFile(FILE_FLAG_OVERLAPPED | FILE_FLAG_DELETE_ON_CLOSE) and binds it. The new close_ext_file() closes the file the right way on each platform and is also used when setting the file size fails. - buf_flush_LRU_list_batch() counted a page in n->evicted without having evicted it when the write to the EBP could not be started. It invoked buf_LRU_free_page() while the page was still U-latched, so can_relocate() was false and the call silently did nothing, leaving the page in the buffer pool. Release the U-latch before the call, like the path that handles a tablespace that is stopping writes. - The debug variable innodb_force_LRU_eviction returned before the forced eviction had finished: it waited for two done_flush_list signals, which other flushing can also send, and did not wait for the asynchronous writes. It now waits until two page cleaner LRU batches (counted by the new debug-only buf_pool.n_LRU_batches) have completed with the flag set, and then for all pending writes. This fixes the wrong counts seen in innodb.ext_buf_pool and innodb.ext_buf_pool_admission_pct. That wait could never end when no page cleaner is running, because only buf_flush_page_cleaner() increments n_LRU_batches and srv_start() only starts it when !srv_read_only_mode, which innodb_force_recovery=6 implies as well. The update function now does nothing in read only mode, and stops waiting if the page cleaner is no longer running; the page cleaner clears buf_page_cleaner_is_active and broadcasts done_flush_list while holding buf_pool.flush_list_mutex, so the transition cannot be missed. The wait also no longer holds LOCK_global_system_variables, like the other InnoDB update functions that wait, so that it cannot keep other connections from being established. - The debug flag ib_ext_bp_disable_LRU_eviction_for_t never skipped anything, because its continue statement applied to the do ... while (0) of DBUG_EXECUTE_IF() and not to the page loop. Set a variable inside the DBUG_EXECUTE_IF() and act on it after the macro, so that the pages of test/t are really left alone while innodb.ext_buf_pool and innodb.ext_buf_pool_admission_pct are still filling the table. Without this, those pages can be admitted to the EBP and evicted while the tests are inserting, and the exact counts that the tests expect after the forced eviction depend on how the page cleaner happened to be scheduled. - The debug flag ib_ext_bp_count_io_only_for_t offered more pages to the admission selector than it counted. It let a page through when fil_space_t::get() failed, while buf_page_write_complete() counts only the pages of the current test/t. The pages of the test/t that an earlier part of the test dropped can still be resident after their fil_space_t is gone, so a run of innodb.ext_buf_pool_admission_pct could put more than its 10 pages before the selector. Those extra pages consumed admissions without being counted, and because the selector's accumulator only returns to its previous value when the number of candidates times the percentage is a multiple of 100, the error also carried over into the following runs. Offer exactly the pages that are counted. Run innodb.ext_buf_pool and innodb.ext_buf_pool_admission_pct with innodb_stats_persistent=0. The inserts make the background statistics thread recalculate the statistics of test/t asynchronously. When it samples the index after the forced eviction, it reads the pages back from the EBP, and when it does so while the forced batch is running, it changes how many pages that batch admits. This made the tests randomly fail on Windows with too many reads, and with 8 or 10 instead of 9 pages flushed. Add a case to innodb.ext_buf_pool for the n->evicted fix. The new debug flag ib_ext_bp_alloc_fail makes every EBP page allocation fail, so that buf_page_t::flush(space, true) returns false with the page still U-latched. Also name the buf_page_t::space_type enumerators in buf_page_write_complete() instead of casting (oldest_modification() == 2) to the enumeration, which needed static_assert()s on the values of TEMPORARY and PERSISTENT. Also re-record sys_vars.sysvars_innodb,32bit for innodb_extended_buffer_pool_size.
innodb_extended_buffer_pool_size stays dynamic, but it is now validated: - If the server was started without the extended buffer pool (EBP), the value cannot be changed. - Otherwise, it can only be set to 0, which stops using the EBP, or to its startup size, which resumes using it. As at startup, a value is rounded down to a multiple of innodb_page_size, so any value that rounds down to the startup size is accepted, with a truncation warning if it was rounded. - buf_pool.extended_pages, the size of the EBP page array, no longer changes; setting 0 used to set it to 0 and break the bounds assertions on the EBP pages that were still in use. - ext_bp_size is now only the storage of the system variable, instead of a union of an atomic and a non-atomic member. fil_system_t::ext_buf_pool_enabled() reads the new atomic flag ext_bp_active instead, because it is also called without holding buf_pool.mutex. srv_start() uses fil_system_t::ext_bp_set_size() to keep the EBP unused until the startup is complete. An I/O error, a page that fails validation (checksum, page id, decryption) or a read past the end of the EBP file now disables the EBP for good with buf_pool_t::ext_disable(), instead of setting innodb_extended_buffer_pool_size to 0 with fil_system_t::ext_buf_pool_disable(), which SET GLOBAL could undo. It discards every EBP page from buf_pool.page_hash, so that nothing more is read from the file, and SET GLOBAL innodb_extended_buffer_pool_size can no longer enable it. This also happens if the size was set to 0 when the error occurs. Reads that fail fall back to reading the page from the data file, instead of flagging the data file as corrupted. If that read fails too, the page is reported to recovery with recv_sys_t::free_corrupted_page(), as for other failed page reads. A failed EBP write is completed by buf_page_ext_write_discard(), which now also evicts the page. This includes a write that cannot be submitted, which buf_page_t::flush() used to ignore, leaving the page write-fixed and latched for good. An asynchronous EBP read that returns fewer bytes than requested, as at the end of the file, is now treated as an I/O error instead of a successful read. os_file_io() named the data file of an EBP request with IORequest::node(), which is invalid for EBP requests; it and os_file_read_func() now use os_io_file_name(). The messages of buf_page_decrypt_after_read() and buf_page_check_corrupt() name the EBP file for pages read from it. buf_page_write_complete() is split into helper functions, and the error paths of EBP reads and writes are marked as unlikely. The EBP path itself is not, because it is the common one while the EBP is in use. The debug flag ib_ext_bp_count_io_only_for_t is now evaluated by buf_page_ext_count_io() for both the EBP admission in buf_flush_LRU_list_batch() and the EBP read and write counters, so that they always agree. It matches the tablespace name test/t exactly. The EBP status variables are moved to their alphabetical place in innodb_status_variables[]. MTR test innodb.ext_buf_pool no longer hides its queries from the result file. Its test of an I/O error on write completion now uses the debug injection ib_ext_bp_write_error of the new test innodb.ext_buf_pool_io_error, and expects innodb_extended_buffer_pool_size to keep its value. The debug injections ib_ext_bp_read_io_error and ib_ext_bp_write_io_error are replaced by those of innodb.ext_buf_pool_io_error.
mariadb-andrzejjarzabek
force-pushed
the
main-MDEV-31956
branch
from
November 26, 2026 13:08
e579caa to
fea99ca
Compare
mariadb-andrzejjarzabek
marked this pull request as ready for review
November 26, 2026 14:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The initial commit was reviewed as PR #4510 - but in this PR it's ported to main.
Subsequent changes:
Crash and leak fixes:
Clearer messages: error messages name the EBP file for EBP I/O.
Debug and test infrastructure:
innodb_force_LRU_eviction now waits for the eviction to actually finish.
The test/t debug filters are fixed and agree with the counters.
New error-injection points.
MTR tests added for ext_buf_pool_admission_pct and IO error handling
ext_buf_pool is extended, stabilized (persistent statistics off) and shows its queries again.
The sysvar results are re-recorded.