Skip to content

Commit 1fe3dd0

Browse files
committed
MDEV-24426 fil_crypt_thread keep spinning even if innodb_encryption_rotate_key_age=0
After MDEV-15528, two modes of operation in the fil_crypt_thread remains, depending on whether innodb_encryption_rotate_key_age=0 (whether key rotation is disabled). If the key rotation is disabled, the fil_crypt_thread miss the opportunity to sleep, which will result in lots of wasted CPU usage. fil_crypt_return_iops(): Add a parameter to specify whether other fil_crypt_thread should be woken up. fil_system_t::keyrotate_next(): Return the special value fil_system.temp_space to indicate that no work is to be done. fil_space_t::next(): Propagage the special value fil_system.temp_space to the caller. fil_crypt_find_space_to_rotate(): If no work is to be done, do not wake up other threads.
1 parent 6bb3949 commit 1fe3dd0

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

mysql-test/suite/mariabackup/xb_compressed_encrypted.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--innodb-encryption-rotate-key-age=2
1+
--innodb-encryption-rotate-key-age=0
22
--innodb-encryption-threads=4
33
--innodb-tablespaces-encryption
44
--plugin-load-add=$FILE_KEY_MANAGEMENT_SO

storage/innobase/fil/fil0crypt.cc

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,13 +1402,10 @@ fil_crypt_realloc_iops(
14021402
fil_crypt_update_total_stat(state);
14031403
}
14041404

1405-
/***********************************************************************
1406-
Return allocated iops to global
1407-
@param[in,out] state Rotation state */
1408-
static
1409-
void
1410-
fil_crypt_return_iops(
1411-
rotate_thread_t *state)
1405+
/** Release excess allocated iops
1406+
@param state rotation state
1407+
@param wake whether to wake up other threads */
1408+
static void fil_crypt_return_iops(rotate_thread_t *state, bool wake= true)
14121409
{
14131410
if (state->allocated_iops > 0) {
14141411
uint iops = state->allocated_iops;
@@ -1424,7 +1421,9 @@ fil_crypt_return_iops(
14241421

14251422
n_fil_crypt_iops_allocated -= iops;
14261423
state->allocated_iops = 0;
1427-
os_event_set(fil_crypt_threads_event);
1424+
if (wake) {
1425+
os_event_set(fil_crypt_threads_event);
1426+
}
14281427
mutex_exit(&fil_crypt_threads_mutex);
14291428
}
14301429

@@ -1437,7 +1436,8 @@ fil_crypt_return_iops(
14371436
the encryption parameters were changed
14381437
@param encrypt expected state of innodb_encrypt_tables
14391438
@return the next tablespace to process (n_pending_ops incremented)
1440-
@retval NULL if this was the last */
1439+
@retval fil_system.temp_space if there is no work to do
1440+
@retval nullptr upon reaching the end of the iteration */
14411441
inline fil_space_t *fil_system_t::keyrotate_next(fil_space_t *space,
14421442
bool recheck, bool encrypt)
14431443
{
@@ -1472,15 +1472,20 @@ inline fil_space_t *fil_system_t::keyrotate_next(fil_space_t *space,
14721472
}
14731473
}
14741474

1475-
while (it != end)
1475+
if (it == end)
1476+
return temp_space;
1477+
1478+
do
14761479
{
14771480
space= &*it;
14781481
if (space->acquire_if_not_stopped(true))
14791482
return space;
1480-
while (++it != end && (!UT_LIST_GET_LEN(it->chain) || it->is_stopping()));
1483+
if (++it == end)
1484+
return nullptr;
14811485
}
1486+
while (!UT_LIST_GET_LEN(it->chain) || it->is_stopping());
14821487

1483-
return NULL;
1488+
return nullptr;
14841489
}
14851490

14861491
/** Determine the next tablespace for encryption key rotation.
@@ -1489,6 +1494,7 @@ inline fil_space_t *fil_system_t::keyrotate_next(fil_space_t *space,
14891494
encryption parameters were changed
14901495
@param encrypt expected state of innodb_encrypt_tables
14911496
@return the next tablespace
1497+
@retval fil_system.temp_space if there is no work to do
14921498
@retval nullptr upon reaching the end of the iteration */
14931499
inline fil_space_t *fil_space_t::next(fil_space_t *space, bool recheck,
14941500
bool encrypt)
@@ -1561,10 +1567,25 @@ static bool fil_crypt_find_space_to_rotate(
15611567
state->space = NULL;
15621568
}
15631569

1564-
state->space = fil_space_t::next(state->space, *recheck,
1565-
key_state->key_version != 0);
1570+
bool wake;
1571+
for (;;) {
1572+
state->space = fil_space_t::next(state->space, *recheck,
1573+
key_state->key_version != 0);
1574+
wake = state->should_shutdown();
1575+
if (wake) {
1576+
break;
1577+
}
1578+
1579+
if (state->space == fil_system.temp_space) {
1580+
goto done;
1581+
} else {
1582+
wake = true;
1583+
}
1584+
1585+
if (!state->space) {
1586+
break;
1587+
}
15661588

1567-
while (!state->should_shutdown() && state->space) {
15681589
/* If there is no crypt data and we have not yet read
15691590
page 0 for this tablespace, we need to read it before
15701591
we can continue. */
@@ -1579,18 +1600,16 @@ static bool fil_crypt_find_space_to_rotate(
15791600
state->min_key_version_found = key_state->key_version;
15801601
return true;
15811602
}
1582-
1583-
state->space = fil_space_t::next(state->space, *recheck,
1584-
key_state->key_version != 0);
15851603
}
15861604

15871605
if (state->space) {
15881606
state->space->release();
1607+
done:
15891608
state->space = NULL;
15901609
}
15911610

15921611
/* no work to do; release our allocation of I/O capacity */
1593-
fil_crypt_return_iops(state);
1612+
fil_crypt_return_iops(state, wake);
15941613

15951614
return false;
15961615

0 commit comments

Comments
 (0)