Skip to content

Commit 4e359eb

Browse files
committed
Cleanup: Reduce trx_t::mutex hold time
1 parent 814bc21 commit 4e359eb

File tree

5 files changed

+49
-85
lines changed

5 files changed

+49
-85
lines changed

storage/innobase/lock/lock0lock.cc

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,8 +1872,6 @@ lock_rec_cancel(
18721872
/*============*/
18731873
lock_t* lock) /*!< in: waiting record lock request */
18741874
{
1875-
que_thr_t* thr;
1876-
18771875
ut_ad(lock_mutex_own());
18781876
ut_ad(lock_get_type_low(lock) == LOCK_REC);
18791877

@@ -1884,17 +1882,13 @@ lock_rec_cancel(
18841882

18851883
lock_reset_lock_and_trx_wait(lock);
18861884

1887-
/* The following function releases the trx from lock wait */
1888-
1889-
trx_mutex_enter(lock->trx);
1890-
1891-
thr = que_thr_end_lock_wait(lock->trx);
1892-
1893-
if (thr != NULL) {
1885+
/* The following releases the trx from lock wait */
1886+
trx_t *trx = lock->trx;
1887+
trx_mutex_enter(trx);
1888+
if (que_thr_t* thr = que_thr_end_lock_wait(trx)) {
18941889
lock_wait_release_thread_if_suspended(thr);
18951890
}
1896-
1897-
trx_mutex_exit(lock->trx);
1891+
trx_mutex_exit(trx);
18981892
}
18991893

19001894
/** Remove a record lock request, waiting or granted, from the queue and
@@ -3755,7 +3749,6 @@ lock_rec_unlock(
37553749
heap_no = page_rec_get_heap_no(rec);
37563750

37573751
lock_mutex_enter();
3758-
trx_mutex_enter(trx);
37593752

37603753
first_lock = lock_rec_get_first(&lock_sys.rec_hash, block, heap_no);
37613754

@@ -3770,7 +3763,6 @@ lock_rec_unlock(
37703763
}
37713764

37723765
lock_mutex_exit();
3773-
trx_mutex_exit(trx);
37743766

37753767
{
37763768
ib::error err;
@@ -3809,7 +3801,6 @@ lock_rec_unlock(
38093801
}
38103802

38113803
lock_mutex_exit();
3812-
trx_mutex_exit(trx);
38133804
}
38143805

38153806
#ifdef UNIV_DEBUG
@@ -3925,7 +3916,8 @@ lock_trx_table_locks_remove(
39253916
ut_ad(lock_mutex_own());
39263917

39273918
/* It is safe to read this because we are holding the lock mutex */
3928-
if (!trx->lock.cancel) {
3919+
const bool have_mutex = trx->lock.cancel;
3920+
if (!have_mutex) {
39293921
trx_mutex_enter(trx);
39303922
} else {
39313923
ut_ad(trx_mutex_own(trx));
@@ -3942,18 +3934,14 @@ lock_trx_table_locks_remove(
39423934
if (lock == lock_to_remove) {
39433935
*it = NULL;
39443936

3945-
if (!trx->lock.cancel) {
3937+
if (!have_mutex) {
39463938
trx_mutex_exit(trx);
39473939
}
39483940

39493941
return;
39503942
}
39513943
}
39523944

3953-
if (!trx->lock.cancel) {
3954-
trx_mutex_exit(trx);
3955-
}
3956-
39573945
/* Lock must exist in the vector. */
39583946
ut_error;
39593947
}

storage/innobase/lock/lock0prdt.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -920,20 +920,14 @@ lock_place_prdt_page_lock(
920920
trx_t* trx = thr_get_trx(thr);
921921

922922
if (lock != NULL) {
923-
924-
trx_mutex_enter(trx);
925-
926923
/* Find a matching record lock owned by this transaction. */
927924

928925
while (lock != NULL && lock->trx != trx) {
929-
930926
lock = lock_rec_get_next_on_page_const(lock);
931927
}
932928

933929
ut_ad(lock == NULL || lock->type_mode == (mode | LOCK_REC));
934930
ut_ad(lock == NULL || lock_rec_get_n_bits(lock) != 0);
935-
936-
trx_mutex_exit(trx);
937931
}
938932

939933
if (lock == NULL) {

storage/innobase/lock/lock0wait.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,24 +459,21 @@ lock_wait_check_and_cancel(
459459

460460
lock_mutex_enter();
461461

462-
trx_mutex_enter(trx);
463-
464462
if (trx->lock.wait_lock != NULL) {
465-
466463
ut_a(trx->lock.que_state == TRX_QUE_LOCK_WAIT);
467464

468465
#ifdef WITH_WSREP
469466
if (!wsrep_is_BF_lock_timeout(trx)) {
470467
#endif /* WITH_WSREP */
468+
mutex_enter(&trx->mutex);
471469
lock_cancel_waiting_and_release(trx->lock.wait_lock);
470+
mutex_exit(&trx->mutex);
472471
#ifdef WITH_WSREP
473472
}
474473
#endif /* WITH_WSREP */
475474
}
476475

477476
lock_mutex_exit();
478-
479-
trx_mutex_exit(trx);
480477
}
481478
}
482479

storage/innobase/que/que0que.cc

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -590,22 +590,18 @@ que_thr_node_step(
590590
return(thr);
591591
}
592592

593-
trx_mutex_enter(thr_get_trx(thr));
593+
auto mutex = &thr->graph->trx->mutex;
594594

595-
if (que_thr_peek_stop(thr)) {
595+
mutex_enter(mutex);
596596

597-
trx_mutex_exit(thr_get_trx(thr));
598-
599-
return(thr);
597+
if (!que_thr_peek_stop(thr)) {
598+
/* Thread execution completed */
599+
thr->state = QUE_THR_COMPLETED;
600+
thr = NULL;
600601
}
601602

602-
/* Thread execution completed */
603-
604-
thr->state = QUE_THR_COMPLETED;
605-
606-
trx_mutex_exit(thr_get_trx(thr));
607-
608-
return(NULL);
603+
mutex_exit(mutex);
604+
return(thr);
609605
}
610606

611607
/**********************************************************************//**
@@ -728,27 +724,24 @@ que_thr_stop_for_mysql(
728724
trx_mutex_enter(trx);
729725

730726
if (thr->state == QUE_THR_RUNNING) {
731-
732-
if (trx->error_state != DB_SUCCESS
733-
&& trx->error_state != DB_LOCK_WAIT) {
734-
735-
/* Error handling built for the MySQL interface */
727+
switch (trx->error_state) {
728+
default:
729+
/* Error handling built for the MariaDB interface */
736730
thr->state = QUE_THR_COMPLETED;
737-
} else {
731+
break;
732+
case DB_SUCCESS:
733+
case DB_LOCK_WAIT:
738734
/* It must have been a lock wait but the lock was
739735
already released, or this transaction was chosen
740736
as a victim in selective deadlock resolution */
741-
742-
trx_mutex_exit(trx);
743-
744-
return;
737+
goto func_exit;
745738
}
746739
}
747740

748741
ut_ad(thr->is_active);
749742
ut_d(thr->set_active(false));
750743
thr->is_active= false;
751-
744+
func_exit:
752745
trx_mutex_exit(trx);
753746
}
754747

@@ -1009,28 +1002,23 @@ que_run_threads_low(
10091002
next_thr = que_thr_step(thr);
10101003
/*-------------------------*/
10111004

1012-
trx_mutex_enter(trx);
1013-
1014-
ut_a(next_thr == NULL || trx->error_state == DB_SUCCESS);
1015-
1016-
if (next_thr != thr) {
1017-
ut_a(next_thr == NULL);
1018-
1005+
if (next_thr) {
1006+
ut_a(trx->error_state == DB_SUCCESS);
1007+
ut_a(next_thr == thr);
1008+
} else {
10191009
/* This can change next_thr to a non-NULL value
10201010
if there was a lock wait that already completed. */
10211011

1012+
mutex_enter(&trx->mutex);
10221013
que_thr_dec_refer_count(thr, &next_thr);
1014+
mutex_exit(&trx->mutex);
10231015

10241016
if (next_thr != NULL) {
1025-
10261017
thr = next_thr;
10271018
}
10281019
}
10291020

10301021
ut_ad(trx == thr_get_trx(thr));
1031-
1032-
trx_mutex_exit(trx);
1033-
10341022
} while (next_thr != NULL);
10351023
}
10361024

@@ -1049,6 +1037,12 @@ que_run_threads(
10491037
que_run_threads_low(thr);
10501038

10511039
switch (thr->state) {
1040+
default:
1041+
ut_error;
1042+
case QUE_THR_COMPLETED:
1043+
case QUE_THR_COMMAND_WAIT:
1044+
/* Do nothing */
1045+
break;
10521046

10531047
case QUE_THR_RUNNING:
10541048
/* There probably was a lock wait, but it already ended
@@ -1058,30 +1052,21 @@ que_run_threads(
10581052

10591053
case QUE_THR_LOCK_WAIT:
10601054
lock_wait_suspend_thread(thr);
1055+
trx_t* trx = thr->graph->trx;
10611056

1062-
trx_mutex_enter(thr_get_trx(thr));
1063-
1064-
ut_a(thr_get_trx(thr)->id != 0);
1065-
1066-
if (thr_get_trx(thr)->error_state != DB_SUCCESS) {
1057+
trx_mutex_enter(trx);
1058+
ut_ad(trx->id);
1059+
const dberr_t err = trx->error_state;
1060+
if (err != DB_SUCCESS) {
10671061
/* thr was chosen as a deadlock victim or there was
10681062
a lock wait timeout */
1069-
10701063
que_thr_dec_refer_count(thr, NULL);
1071-
trx_mutex_exit(thr_get_trx(thr));
1072-
break;
10731064
}
1065+
trx_mutex_exit(trx);
10741066

1075-
trx_mutex_exit(thr_get_trx(thr));
1076-
goto loop;
1077-
1078-
case QUE_THR_COMPLETED:
1079-
case QUE_THR_COMMAND_WAIT:
1080-
/* Do nothing */
1081-
break;
1082-
1083-
default:
1084-
ut_error;
1067+
if (err == DB_SUCCESS) {
1068+
goto loop;
1069+
}
10851070
}
10861071
}
10871072

storage/innobase/trx/trx0roll.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,14 +959,14 @@ trx_rollback_step(
959959

960960
trx = thr_get_trx(thr);
961961

962-
trx_mutex_enter(trx);
963-
964962
node->state = ROLL_NODE_WAIT;
965963

966964
ut_a(node->undo_thr == NULL);
967965

968966
roll_limit = node->savept ? node->savept->least_undo_no : 0;
969967

968+
trx_mutex_enter(trx);
969+
970970
trx_commit_or_rollback_prepare(trx);
971971

972972
node->undo_thr = trx_rollback_start(trx, roll_limit);

0 commit comments

Comments
 (0)