Skip to content

Commit df44e75

Browse files
committed
Minor clean-up of purge code
purge_sys_t::n_submitted: Document that it is only accessed by srv_purge_coordinator_thread. purge_sys_t::n_completed: Exclusively use my_atomic access. srv_task_execute(): Simplify the code. srv_purge_coordinator_thread(): Test the cheaper condition first. trx_purge(): Atomically access purge_sys.n_completed. Remove some code duplication. trx_purge_wait_for_workers_to_complete(): Atomically access purge_sys.n_completed. Remove an unnecessary local variable. trx_purge_stop(): Remove a redundant assignment.
1 parent 0f6186c commit df44e75

File tree

3 files changed

+34
-67
lines changed

3 files changed

+34
-67
lines changed

storage/innobase/include/trx0purge.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,12 @@ class purge_sys_t
424424
MY_ALIGNED(CACHE_LINE_SIZE)
425425
ReadView view; /*!< The purge will not remove undo logs
426426
which are >= this view (purge view) */
427-
ulint n_submitted; /*!< Count of total tasks submitted
428-
to the task queue */
429-
ulint n_completed; /*!< Count of total tasks completed */
427+
/** Total number of tasks submitted by srv_purge_coordinator_thread.
428+
Not accessed by other threads. */
429+
ulint n_submitted;
430+
/** Number of completed tasks. Accessed by srv_purge_coordinator
431+
and srv_worker_thread by my_atomic. */
432+
ulint n_completed;
430433

431434
/** Iterator to the undo log records of committed transactions */
432435
struct iterator

storage/innobase/srv/srv0srv.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,38 +2474,25 @@ srv_purge_should_exit(ulint n_purged)
24742474
/*********************************************************************//**
24752475
Fetch and execute a task from the work queue.
24762476
@return true if a task was executed */
2477-
static
2478-
bool
2479-
srv_task_execute(void)
2480-
/*==================*/
2477+
static bool srv_task_execute()
24812478
{
2482-
que_thr_t* thr = NULL;
2483-
24842479
ut_ad(!srv_read_only_mode);
2485-
ut_a(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
2480+
ut_ad(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
24862481

24872482
mutex_enter(&srv_sys.tasks_mutex);
24882483

2489-
if (UT_LIST_GET_LEN(srv_sys.tasks) > 0) {
2490-
2491-
thr = UT_LIST_GET_FIRST(srv_sys.tasks);
2492-
2484+
if (que_thr_t* thr = UT_LIST_GET_FIRST(srv_sys.tasks)) {
24932485
ut_a(que_node_get_type(thr->child) == QUE_NODE_PURGE);
2494-
24952486
UT_LIST_REMOVE(srv_sys.tasks, thr);
2496-
}
2497-
2498-
mutex_exit(&srv_sys.tasks_mutex);
2499-
2500-
if (thr != NULL) {
2501-
2487+
mutex_exit(&srv_sys.tasks_mutex);
25022488
que_run_threads(thr);
2503-
2504-
my_atomic_addlint(
2505-
&purge_sys.n_completed, 1);
2489+
my_atomic_addlint(&purge_sys.n_completed, 1);
2490+
return true;
25062491
}
25072492

2508-
return(thr != NULL);
2493+
ut_ad(UT_LIST_GET_LEN(srv_sys.tasks) == 0);
2494+
mutex_exit(&srv_sys.tasks_mutex);
2495+
return false;
25092496
}
25102497

25112498
/*********************************************************************//**
@@ -2781,8 +2768,8 @@ DECLARE_THREAD(srv_purge_coordinator_thread)(
27812768

27822769
if (srv_shutdown_state == SRV_SHUTDOWN_NONE
27832770
&& srv_undo_sources
2784-
&& (purge_sys.state == PURGE_STATE_STOP
2785-
|| n_total_purged == 0)) {
2771+
&& (n_total_purged == 0
2772+
|| purge_sys.state == PURGE_STATE_STOP)) {
27862773

27872774
srv_purge_coordinator_suspend(slot, rseg_history_len);
27882775
}

storage/innobase/trx/trx0purge.cc

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,10 +1510,9 @@ static
15101510
void
15111511
trx_purge_wait_for_workers_to_complete()
15121512
{
1513-
ulint n_submitted = purge_sys.n_submitted;
1514-
15151513
/* Ensure that the work queue empties out. */
1516-
while ((ulint) my_atomic_loadlint(&purge_sys.n_completed) != n_submitted) {
1514+
while (my_atomic_loadlint(&purge_sys.n_completed)
1515+
!= purge_sys.n_submitted) {
15171516

15181517
if (srv_get_task_queue_length() > 0) {
15191518
srv_release_threads(SRV_WORKER, 1);
@@ -1522,9 +1521,6 @@ trx_purge_wait_for_workers_to_complete()
15221521
os_thread_yield();
15231522
}
15241523

1525-
/* None of the worker threads should be doing any work. */
1526-
ut_a(purge_sys.n_submitted == purge_sys.n_completed);
1527-
15281524
/* There should be no outstanding tasks as long
15291525
as the worker threads are active. */
15301526
ut_a(srv_get_task_queue_length() == 0);
@@ -1548,7 +1544,8 @@ trx_purge(
15481544
srv_dml_needed_delay = trx_purge_dml_delay();
15491545

15501546
/* The number of tasks submitted should be completed. */
1551-
ut_a(purge_sys.n_submitted == purge_sys.n_completed);
1547+
ut_a(purge_sys.n_submitted
1548+
== my_atomic_loadlint(&purge_sys.n_completed));
15521549

15531550
rw_lock_x_lock(&purge_sys.latch);
15541551
trx_sys.clone_oldest_view();
@@ -1562,46 +1559,27 @@ trx_purge(
15621559

15631560
/* Fetch the UNDO recs that need to be purged. */
15641561
n_pages_handled = trx_purge_attach_undo_recs(n_purge_threads);
1562+
purge_sys.n_submitted += n_purge_threads;
15651563

1566-
/* Do we do an asynchronous purge or not ? */
1567-
if (n_purge_threads > 1) {
1568-
ulint i = 0;
1569-
1570-
/* Submit the tasks to the work queue. */
1571-
for (i = 0; i < n_purge_threads - 1; ++i) {
1572-
thr = que_fork_scheduler_round_robin(
1573-
purge_sys.query, thr);
1574-
1575-
ut_a(thr != NULL);
1576-
1577-
srv_que_task_enqueue_low(thr);
1578-
}
1579-
1564+
/* Submit tasks to workers queue if using multi-threaded purge. */
1565+
for (ulint i = n_purge_threads; --i; ) {
15801566
thr = que_fork_scheduler_round_robin(purge_sys.query, thr);
1581-
ut_a(thr != NULL);
1582-
1583-
purge_sys.n_submitted += n_purge_threads - 1;
1584-
1585-
goto run_synchronously;
1586-
1587-
/* Do it synchronously. */
1588-
} else {
1589-
thr = que_fork_scheduler_round_robin(purge_sys.query, NULL);
1590-
ut_ad(thr);
1567+
ut_a(thr);
1568+
srv_que_task_enqueue_low(thr);
1569+
}
15911570

1592-
run_synchronously:
1593-
++purge_sys.n_submitted;
1571+
thr = que_fork_scheduler_round_robin(purge_sys.query, thr);
15941572

1595-
que_run_threads(thr);
1573+
que_run_threads(thr);
15961574

1597-
my_atomic_addlint(&purge_sys.n_completed, 1);
1575+
my_atomic_addlint(&purge_sys.n_completed, 1);
15981576

1599-
if (n_purge_threads > 1) {
1600-
trx_purge_wait_for_workers_to_complete();
1601-
}
1577+
if (n_purge_threads > 1) {
1578+
trx_purge_wait_for_workers_to_complete();
16021579
}
16031580

1604-
ut_a(purge_sys.n_submitted == purge_sys.n_completed);
1581+
ut_a(purge_sys.n_submitted
1582+
== my_atomic_loadlint(&purge_sys.n_completed));
16051583

16061584
if (truncate) {
16071585
trx_purge_truncate_history();
@@ -1653,7 +1631,6 @@ trx_purge_stop(void)
16531631
case PURGE_STATE_STOP:
16541632
ut_ad(srv_n_purge_threads > 0);
16551633
++purge_sys.n_stop;
1656-
purge_sys.state = PURGE_STATE_STOP;
16571634
if (!purge_sys.running) {
16581635
goto unlock;
16591636
}

0 commit comments

Comments
 (0)