Skip to content

Commit

Permalink
Post-push fix for MDEV-12610 MariaDB start is slow
Browse files Browse the repository at this point in the history
fil_crypt_read_crypt_data(): Remove an unnecessary
acquisition of fil_system->mutex. Remove a duplicated condition
from the callers.
  • Loading branch information
dr-m committed Jun 12, 2017
1 parent 757339e commit 3005ceb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 84 deletions.
64 changes: 22 additions & 42 deletions storage/innobase/fil/fil0crypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/***********************************************************************
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<fil_space_t*>(space));
}

fil_space_crypt_t* crypt_data = space->crypt_data;
fil_crypt_read_crypt_data(const_cast<fil_space_t*>(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;
Expand Down
64 changes: 22 additions & 42 deletions storage/xtradb/fil/fil0crypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/***********************************************************************
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<fil_space_t*>(space));
}

fil_space_crypt_t* crypt_data = space->crypt_data;
fil_crypt_read_crypt_data(const_cast<fil_space_t*>(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;
Expand Down

0 comments on commit 3005ceb

Please sign in to comment.