Skip to content
Permalink
Browse files

MDEV-12610: MariaDB start is slow

Problem appears to be that the function fsp_flags_try_adjust()
is being unconditionally invoked on every .ibd file on startup.
Based on performance investigation also the top function
fsp_header_get_crypt_offset() needs to addressed.

Ported implementation of fsp_header_get_encryption_offset()
function from 10.2 to fsp_header_get_crypt_offset().

Introduced a new function fil_crypt_read_crypt_data()
to read page 0 if it is not yet read.

fil_crypt_find_space_to_rotate(): Now that page 0 for every .ibd
file is not read on startup we need to check has page 0 read
from space that we investigate for key rotation, if it is not read
we read it.

fil_space_crypt_get_status(): Now that page 0 for every .ibd
file is not read on startup here also we need to read page 0
if it is not yet read it. This is needed
as tests use IS query to wait until background encryption
or decryption has finished and this function is used to
produce results.

fil_crypt_thread(): Add is_stopping condition for tablespace
so that we do not rotate pages if usage of tablespace should
be stopped. This was needed for failure seen on regression
testing.

fil_space_create: Remove page_0_crypt_read and extra
unnecessary info output.

fil_open_single_table_tablespace(): We call fsp_flags_try_adjust
only when when no errors has happened and server was not started
on read only mode and tablespace validation was requested or
flags contain other table options except low order bits to
FSP_FLAGS_POS_PAGE_SSIZE position.

fil_space_t::page_0_crypt_read removed.

Added test case innodb-first-page-read to test startup when
encryption is on and when encryption is off to check that not
for all tables page 0 is read on startup.
  • Loading branch information...
janlindstrom committed Jun 8, 2017
1 parent fbeb948 commit 58c56dd7f8e787c48b6f09a4d03cddd7c922588b
@@ -0,0 +1,89 @@
SET GLOBAL innodb_file_format = `Barracuda`;
SET GLOBAL innodb_file_per_table = ON;
create database innodb_test;
use innodb_test;
create table innodb_normal(c1 bigint not null, b char(200)) engine=innodb;
create table innodb_compact(c1 bigint not null, b char(200)) engine=innodb row_format=compact;
create table innodb_dynamic(c1 bigint not null, b char(200)) engine=innodb row_format=dynamic;
create table innodb_compressed(c1 bigint not null, b char(200)) engine=innodb row_format=compressed;
create table innodb_compressed1(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=1;
create table innodb_compressed2(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=2;
create table innodb_compressed4(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=4;
create table innodb_compressed8(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=8;
create table innodb_compressed16(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=16;
create table innodb_redundant(c1 bigint not null, b char(200)) engine=innodb row_format=redundant;
create table innodb_pagecomp(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes;
create table innodb_pagecomp1(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=1;
create table innodb_pagecomp2(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=2;
create table innodb_pagecomp3(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=3;
create table innodb_pagecomp4(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=4;
create table innodb_pagecomp5(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=5;
create table innodb_pagecomp6(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=6;
create table innodb_pagecomp7(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=7;
create table innodb_pagecomp8(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=8;
create table innodb_pagecomp9(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=9;
create table innodb_datadir1(c1 bigint not null, b char(200)) engine=innodb DATA DIRECTORY='MYSQL_TMP_DIR';
create table innodb_datadir2(c1 bigint not null, b char(200)) engine=innodb row_format=compressed DATA DIRECTORY='MYSQL_TMP_DIR';
create table innodb_datadir3(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes DATA DIRECTORY='MYSQL_TMP_DIR';
begin;
insert into innodb_normal values (1,'secret');
insert into innodb_compact select * from innodb_normal;
insert into innodb_dynamic select * from innodb_normal;
insert into innodb_compressed select * from innodb_normal;
insert into innodb_compressed1 select * from innodb_normal;
insert into innodb_compressed2 select * from innodb_normal;
insert into innodb_compressed4 select * from innodb_normal;
insert into innodb_compressed8 select * from innodb_normal;
insert into innodb_compressed16 select * from innodb_normal;
insert into innodb_redundant select * from innodb_normal;
insert into innodb_pagecomp select * from innodb_normal;
insert into innodb_pagecomp1 select * from innodb_normal;
insert into innodb_pagecomp2 select * from innodb_normal;
insert into innodb_pagecomp3 select * from innodb_normal;
insert into innodb_pagecomp4 select * from innodb_normal;
insert into innodb_pagecomp5 select * from innodb_normal;
insert into innodb_pagecomp6 select * from innodb_normal;
insert into innodb_pagecomp7 select * from innodb_normal;
insert into innodb_pagecomp8 select * from innodb_normal;
insert into innodb_pagecomp9 select * from innodb_normal;
insert into innodb_datadir1 select * from innodb_normal;
insert into innodb_datadir2 select * from innodb_normal;
insert into innodb_datadir3 select * from innodb_normal;
commit;
# Restart server and see how many page 0's are read
# result should be less than actual number of tables
# i.e. < 23 + 3 = 26
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 17
use innodb_test;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 17
use test;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 17
set global innodb_encrypt_tables=OFF;
# wait until tables are decrypted
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 29
use innodb_test;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 29
use test;
# restart and see number read page 0
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 17
use innodb_test;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 17
use test;
drop database innodb_test;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 29
@@ -12,13 +12,13 @@ create database innodb_encrypted_1;
use innodb_encrypted_1;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 3
Innodb_pages0_read 1
set autocommit=0;
set autocommit=1;
commit work;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 3
Innodb_pages0_read 1
# should be 100
SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE NAME LIKE 'innodb_encrypted%';
COUNT(*)
@@ -88,47 +88,47 @@ Innodb_pages0_read 3
# Restart Success!
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
use test;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
use innodb_encrypted_1;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
use innodb_encrypted_2;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
use innodb_encrypted_3;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
use innodb_encrypted_1;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 1
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 101
use innodb_encrypted_2;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 101
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 201
use innodb_encrypted_3;
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 201
show status like 'innodb_pages0_read%';
Variable_name Value
Innodb_pages0_read 303
Innodb_pages0_read 301
SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION = 0 AND NAME LIKE 'innodb_encrypted%';
COUNT(*)
100
@@ -0,0 +1,5 @@
--innodb-encrypt-tables=ON
--innodb-encrypt-log=ON
--innodb-encryption-rotate-key-age=15
--innodb-encryption-threads=4
--innodb-tablespaces-encryption
@@ -0,0 +1,97 @@
-- source include/have_innodb.inc
-- source include/have_file_key_management_plugin.inc
-- source include/not_embedded.inc

--disable_warnings
SET GLOBAL innodb_file_format = `Barracuda`;
SET GLOBAL innodb_file_per_table = ON;
--enable_warnings

create database innodb_test;
use innodb_test;
create table innodb_normal(c1 bigint not null, b char(200)) engine=innodb;
create table innodb_compact(c1 bigint not null, b char(200)) engine=innodb row_format=compact;
create table innodb_dynamic(c1 bigint not null, b char(200)) engine=innodb row_format=dynamic;
create table innodb_compressed(c1 bigint not null, b char(200)) engine=innodb row_format=compressed;
create table innodb_compressed1(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=1;
create table innodb_compressed2(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=2;
create table innodb_compressed4(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=4;
create table innodb_compressed8(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=8;
create table innodb_compressed16(c1 bigint not null, b char(200)) engine=innodb row_format=compressed key_block_size=16;
create table innodb_redundant(c1 bigint not null, b char(200)) engine=innodb row_format=redundant;
create table innodb_pagecomp(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes;
create table innodb_pagecomp1(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=1;
create table innodb_pagecomp2(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=2;
create table innodb_pagecomp3(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=3;
create table innodb_pagecomp4(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=4;
create table innodb_pagecomp5(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=5;
create table innodb_pagecomp6(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=6;
create table innodb_pagecomp7(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=7;
create table innodb_pagecomp8(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=8;
create table innodb_pagecomp9(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes page_compression_level=9;

--replace_result $MYSQL_TMP_DIR MYSQL_TMP_DIR
eval create table innodb_datadir1(c1 bigint not null, b char(200)) engine=innodb DATA DIRECTORY='$MYSQL_TMP_DIR';
--replace_result $MYSQL_TMP_DIR MYSQL_TMP_DIR
eval create table innodb_datadir2(c1 bigint not null, b char(200)) engine=innodb row_format=compressed DATA DIRECTORY='$MYSQL_TMP_DIR';
--replace_result $MYSQL_TMP_DIR MYSQL_TMP_DIR
eval create table innodb_datadir3(c1 bigint not null, b char(200)) engine=innodb page_compressed=yes DATA DIRECTORY='$MYSQL_TMP_DIR';

begin;
insert into innodb_normal values (1,'secret');
insert into innodb_compact select * from innodb_normal;
insert into innodb_dynamic select * from innodb_normal;
insert into innodb_compressed select * from innodb_normal;
insert into innodb_compressed1 select * from innodb_normal;
insert into innodb_compressed2 select * from innodb_normal;
insert into innodb_compressed4 select * from innodb_normal;
insert into innodb_compressed8 select * from innodb_normal;
insert into innodb_compressed16 select * from innodb_normal;
insert into innodb_redundant select * from innodb_normal;
insert into innodb_pagecomp select * from innodb_normal;
insert into innodb_pagecomp1 select * from innodb_normal;
insert into innodb_pagecomp2 select * from innodb_normal;
insert into innodb_pagecomp3 select * from innodb_normal;
insert into innodb_pagecomp4 select * from innodb_normal;
insert into innodb_pagecomp5 select * from innodb_normal;
insert into innodb_pagecomp6 select * from innodb_normal;
insert into innodb_pagecomp7 select * from innodb_normal;
insert into innodb_pagecomp8 select * from innodb_normal;
insert into innodb_pagecomp9 select * from innodb_normal;
insert into innodb_datadir1 select * from innodb_normal;
insert into innodb_datadir2 select * from innodb_normal;
insert into innodb_datadir3 select * from innodb_normal;
commit;

--echo # Restart server and see how many page 0's are read
--source include/restart_mysqld.inc

--echo # result should be less than actual number of tables
--echo # i.e. < 23 + 3 = 26
show status like 'innodb_pages0_read%';
use innodb_test;
show status like 'innodb_pages0_read%';
use test;
show status like 'innodb_pages0_read%';

set global innodb_encrypt_tables=OFF;

--echo # wait until tables are decrypted
--let $wait_condition=SELECT COUNT(*) = 0 FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION <> 0
--source include/wait_condition.inc

show status like 'innodb_pages0_read%';
use innodb_test;
show status like 'innodb_pages0_read%';
use test;

--echo # restart and see number read page 0
-- source include/restart_mysqld.inc

show status like 'innodb_pages0_read%';
use innodb_test;
show status like 'innodb_pages0_read%';
use test;

drop database innodb_test;
show status like 'innodb_pages0_read%';
@@ -4549,6 +4549,7 @@ buf_page_check_corrupt(buf_page_t* bpage, fil_space_t* space)
!bpage->encrypted &&
fil_space_verify_crypt_checksum(dst_frame, zip_size,
space, bpage->offset));

if (!still_encrypted) {
/* If traditional checksums match, we assume that page is
not anymore encrypted. */
@@ -1115,6 +1115,43 @@ fil_crypt_needs_rotation(
return false;
}

/** Read page 0 and possible crypt data from there.
@param[in] space Tablespace */
static inline
void
fil_crypt_read_crypt_data(fil_space_t* space)
{
mutex_enter(&fil_system->mutex);

/* If space does not contain crypt data and space size is 0
we have not yet read first page of tablespace. We need to
read it to find out tablespace current encryption status. */
if (!space->crypt_data && space->size == 0) {
mtr_t mtr;
mtr_start(&mtr);
ulint zip_size = fsp_flags_get_zip_size(space->flags);
ulint offset = fsp_header_get_crypt_offset(zip_size);
mutex_exit(&fil_system->mutex);
if (buf_block_t* block = buf_page_get(space->id, zip_size, 0,
RW_X_LATCH, &mtr)) {
byte* frame = buf_block_get_frame(block);

mutex_enter(&fil_system->mutex);

if (!space->crypt_data) {
space->crypt_data = fil_space_read_crypt_data(space->id,
frame, offset);
}

mutex_exit(&fil_system->mutex);
}

mtr_commit(&mtr);
} else {
mutex_exit(&fil_system->mutex);
}
}

/***********************************************************************
Start encrypting a space
@param[in,out] space Tablespace
@@ -1125,6 +1162,7 @@ fil_crypt_start_encrypting_space(
fil_space_t* space)
{
bool recheck = false;

mutex_enter(&fil_crypt_threads_mutex);

fil_space_crypt_t *crypt_data = space->crypt_data;
@@ -1191,8 +1229,6 @@ fil_crypt_start_encrypting_space(
byte* frame = buf_block_get_frame(block);
crypt_data->type = CRYPT_SCHEME_1;
crypt_data->write_page0(frame, &mtr);


mtr_commit(&mtr);

/* record lsn of update */
@@ -1620,6 +1656,13 @@ fil_crypt_find_space_to_rotate(
}

while (!state->should_shutdown() && state->space) {
/* If there is no crypt data and we have not yet read
page 0 for this tablespace, we need to read it before
we can continue. */
if (!state->space->crypt_data) {
fil_crypt_read_crypt_data(state->space);
}

if (fil_crypt_space_needs_rotation(state, key_state, recheck)) {
ut_ad(key_state->key_id);
/* init state->min_key_version_found before
@@ -2314,8 +2357,10 @@ DECLARE_THREAD(fil_crypt_thread)(
while (!thr.should_shutdown() &&
fil_crypt_find_page_to_rotate(&new_state, &thr)) {

/* rotate a (set) of pages */
fil_crypt_rotate_pages(&new_state, &thr);
if (!thr.space->is_stopping()) {
/* rotate a (set) of pages */
fil_crypt_rotate_pages(&new_state, &thr);
}

/* If space is marked as stopping, release
space and stop rotation. */
@@ -2544,6 +2589,14 @@ fil_space_crypt_get_status(
memset(status, 0, sizeof(*status));

ut_ad(space->n_pending_ops > 0);

/* If there is no crypt data and we have not yet read
page 0 for this tablespace, we need to read it before
we can continue. */
if (!space->crypt_data) {
fil_crypt_read_crypt_data(const_cast<fil_space_t*>(space));
}

fil_space_crypt_t* crypt_data = space->crypt_data;
status->space = space->id;

0 comments on commit 58c56dd

Please sign in to comment.
You can’t perform that action at this time.