From 3005cebc96ef65dc54bf6caaa5e089d52d52e7ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Mon, 12 Jun 2017 17:10:56 +0300 Subject: Post-push fix for MDEV-12610 MariaDB start is slow fil_crypt_read_crypt_data(): Remove an unnecessary acquisition of fil_system->mutex. Remove a duplicated condition from the callers. --- storage/innobase/fil/fil0crypt.cc | 64 ++++++++++++++------------------------- storage/xtradb/fil/fil0crypt.cc | 64 ++++++++++++++------------------------- 2 files changed, 44 insertions(+), 84 deletions(-) (limited to 'storage') diff --git a/storage/innobase/fil/fil0crypt.cc b/storage/innobase/fil/fil0crypt.cc index 70d8558ede2..df5c250df90 100644 --- a/storage/innobase/fil/fil0crypt.cc +++ b/storage/innobase/fil/fil0crypt.cc @@ -1116,40 +1116,33 @@ fil_crypt_needs_rotation( } /** Read page 0 and possible crypt data from there. -@param[in] space Tablespace */ +@param[in,out] 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); - } + if (space->crypt_data || space->size) { + /* The encryption metadata has already been read, or + the tablespace is not encrypted and the file has been + opened already. */ + return; + } - mutex_exit(&fil_system->mutex); + 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); + if (buf_block_t* block = buf_page_get(space->id, zip_size, 0, + RW_S_LATCH, &mtr)) { + mutex_enter(&fil_system->mutex); + if (!space->crypt_data) { + space->crypt_data = fil_space_read_crypt_data( + space->id, block->frame, offset); } - - mtr_commit(&mtr); - } else { mutex_exit(&fil_system->mutex); } + + mtr_commit(&mtr); } /*********************************************************************** @@ -1656,12 +1649,7 @@ 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); - } + fil_crypt_read_crypt_data(state->space); if (fil_crypt_space_needs_rotation(state, key_state, recheck)) { ut_ad(key_state->key_id); @@ -2589,18 +2577,10 @@ 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(space)); - } - - fil_space_crypt_t* crypt_data = space->crypt_data; + fil_crypt_read_crypt_data(const_cast(space)); status->space = space->id; - if (crypt_data != NULL) { + if (fil_space_crypt_t* crypt_data = space->crypt_data) { mutex_enter(&crypt_data->mutex); status->scheme = crypt_data->type; status->keyserver_requests = crypt_data->keyserver_requests; diff --git a/storage/xtradb/fil/fil0crypt.cc b/storage/xtradb/fil/fil0crypt.cc index e24278dd102..6d3c2e98010 100644 --- a/storage/xtradb/fil/fil0crypt.cc +++ b/storage/xtradb/fil/fil0crypt.cc @@ -1116,40 +1116,33 @@ fil_crypt_needs_rotation( } /** Read page 0 and possible crypt data from there. -@param[in] space Tablespace */ +@param[in,out] 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); - } + if (space->crypt_data || space->size) { + /* The encryption metadata has already been read, or + the tablespace is not encrypted and the file has been + opened already. */ + return; + } - mutex_exit(&fil_system->mutex); + 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); + if (buf_block_t* block = buf_page_get(space->id, zip_size, 0, + RW_S_LATCH, &mtr)) { + mutex_enter(&fil_system->mutex); + if (!space->crypt_data) { + space->crypt_data = fil_space_read_crypt_data( + space->id, block->frame, offset); } - - mtr_commit(&mtr); - } else { mutex_exit(&fil_system->mutex); } + + mtr_commit(&mtr); } /*********************************************************************** @@ -1656,12 +1649,7 @@ 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); - } + fil_crypt_read_crypt_data(state->space); if (fil_crypt_space_needs_rotation(state, key_state, recheck)) { ut_ad(key_state->key_id); @@ -2590,18 +2578,10 @@ 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(space)); - } - - fil_space_crypt_t* crypt_data = space->crypt_data; + fil_crypt_read_crypt_data(const_cast(space)); status->space = space->id; - if (crypt_data != NULL) { + if (fil_space_crypt_t* crypt_data = space->crypt_data) { mutex_enter(&crypt_data->mutex); status->scheme = crypt_data->type; status->keyserver_requests = crypt_data->keyserver_requests; -- cgit v1.2.1