Commit d217a925 authored by Jan Lindström's avatar Jan Lindström

MDEV-24923 : Port selected Galera conflict resolution changes from 10.6

Add condition on trx->state == TRX_STATE_COMMITTED_IN_MEMORY in order to
avoid unnecessary work. If a transaction has already been committed or
rolled back, it will release its locks in lock_release() and let
the waiting thread(s) continue execution.

Let BF wait on lock_rec_has_to_wait and if necessary other BF
is replayed.

wsrep_trx_order_before
  If BF is not even replicated yet then they are ordered
  correctly.

bg_wsrep_kill_trx
  Make sure victim_trx is found and check also its state. If
  state is TRX_STATE_COMMITTED_IN_MEMORY transaction is
  already committed or rolled back and will release it locks
  soon.

wsrep_assert_no_bf_bf_wait
  Transaction requesting new record lock should be TRX_STATE_ACTIVE
  Conflicting transaction can be in states TRX_STATE_ACTIVE,
  TRX_STATE_COMMITTED_IN_MEMORY or in TRX_STATE_PREPARED.
  If conflicting transaction is already committed in memory or
  prepared we should wait. When transaction is committed in memory we
  held trx mutex, but not lock_sys->mutex. Therefore, we
  could end here before transaction has time to do lock_release()
  that is protected with lock_sys->mutex.

lock_rec_has_to_wait
  We very well can let bf to wait normally as other BF will be
  replayed in case of conflict. For debug builds we will do
  additional sanity checks to catch unsupported bf wait if any.

wsrep_kill_victim
  Check is victim already in TRX_STATE_COMMITTED_IN_MEMORY state and
  if it is we can return.

lock_rec_dequeue_from_page
lock_rec_unlock
  Remove unnecessary wsrep_assert_no_bf_bf_wait function calls.
  We can very well let BF wait here.
parent c4427332
...@@ -2788,16 +2788,18 @@ extern "C" bool wsrep_thd_ignore_table(THD *thd) ...@@ -2788,16 +2788,18 @@ extern "C" bool wsrep_thd_ignore_table(THD *thd)
extern int extern int
wsrep_trx_order_before(THD *thd1, THD *thd2) wsrep_trx_order_before(THD *thd1, THD *thd2)
{ {
if (wsrep_thd_trx_seqno(thd1) < wsrep_thd_trx_seqno(thd2)) { const longlong trx1_seqno= wsrep_thd_trx_seqno(thd1);
WSREP_DEBUG("BF conflict, order: %lld %lld\n", const longlong trx2_seqno= wsrep_thd_trx_seqno(thd2);
(long long)wsrep_thd_trx_seqno(thd1), WSREP_DEBUG("BF conflict, order: %lld %lld\n",
(long long)wsrep_thd_trx_seqno(thd2)); trx1_seqno, trx2_seqno);
return 1;
} if (trx1_seqno == WSREP_SEQNO_UNDEFINED ||
WSREP_DEBUG("waiting for BF, trx order: %lld %lld\n", trx2_seqno == WSREP_SEQNO_UNDEFINED)
(long long)wsrep_thd_trx_seqno(thd1), return 1; /* trx is not yet replicated */
(long long)wsrep_thd_trx_seqno(thd2)); else if (trx1_seqno < trx2_seqno)
return 0; return 1;
return 0;
} }
......
...@@ -19596,21 +19596,32 @@ static void bg_wsrep_kill_trx( ...@@ -19596,21 +19596,32 @@ static void bg_wsrep_kill_trx(
DBUG_ENTER("bg_wsrep_kill_trx"); DBUG_ENTER("bg_wsrep_kill_trx");
if (thd) { if (thd) {
victim_trx = thd_to_trx(thd); victim_trx= thd_to_trx(thd);
lock_mutex_enter(); /* Victim trx might not exist e.g. on MDL-conflict. */
trx_mutex_enter(victim_trx); if (victim_trx) {
if (victim_trx->id != arg->trx_id) lock_mutex_enter();
{ trx_mutex_enter(victim_trx);
trx_mutex_exit(victim_trx); if (victim_trx->id != arg->trx_id ||
lock_mutex_exit(); victim_trx->state == TRX_STATE_COMMITTED_IN_MEMORY)
{
/* Victim was meanwhile rolled back or
committed */
trx_mutex_exit(victim_trx);
lock_mutex_exit();
wsrep_thd_UNLOCK(thd);
victim_trx= NULL;
}
} else {
/* find_thread_by_id locked
THD::LOCK_thd_data */
wsrep_thd_UNLOCK(thd); wsrep_thd_UNLOCK(thd);
victim_trx = NULL;
} }
} }
if (!victim_trx) { if (!victim_trx) {
/* it can happen that trx_id was meanwhile rolled back */ /* Victim trx might not exist (MDL-conflict) or victim
DBUG_PRINT("wsrep", ("no thd for conflicting lock")); was meanwhile rolled back or committed because of
a KILL statement or a disconnect. */
goto ret; goto ret;
} }
...@@ -19766,7 +19777,7 @@ static void bg_wsrep_kill_trx( ...@@ -19766,7 +19777,7 @@ static void bg_wsrep_kill_trx(
} }
ret_awake: ret_awake:
awake = true; awake= true;
ret_unlock: ret_unlock:
trx_mutex_exit(victim_trx); trx_mutex_exit(victim_trx);
......
...@@ -644,71 +644,81 @@ lock_rec_get_insert_intention( ...@@ -644,71 +644,81 @@ lock_rec_get_insert_intention(
return(lock->type_mode & LOCK_INSERT_INTENTION); return(lock->type_mode & LOCK_INSERT_INTENTION);
} }
#ifdef UNIV_DEBUG
#ifdef WITH_WSREP #ifdef WITH_WSREP
/** Check if both conflicting lock and other record lock are brute force /** Check if both conflicting lock transaction and other transaction
(BF). This case is a bug so report lock information and wsrep state. requesting record lock are brute force (BF). If they are check is
@param[in] lock_rec1 conflicting waiting record lock or NULL this BF-BF wait correct and if not report BF wait and assert.
@param[in] lock_rec2 other waiting record lock
@param[in] trx1 lock_rec1 can be NULL, trx @param[in] lock_rec other waiting record lock
@param[in] trx trx requesting conflicting record lock
*/ */
static void wsrep_assert_no_bf_bf_wait( static void wsrep_assert_no_bf_bf_wait(const lock_t *lock, const trx_t *trx)
const lock_t* lock_rec1,
const lock_t* lock_rec2,
const trx_t* trx1)
{ {
ut_ad(!lock_rec1 || lock_get_type_low(lock_rec1) == LOCK_REC); ut_ad(lock_get_type_low(lock) == LOCK_REC);
ut_ad(lock_get_type_low(lock_rec2) == LOCK_REC);
ut_ad(lock_mutex_own()); ut_ad(lock_mutex_own());
trx_t* lock_trx= lock->trx;
/* Note that we are holding lock_sys->mutex, thus we should /* Note that we are holding lock_sys->mutex, thus we should
not acquire THD::LOCK_thd_data mutex below to avoid mutexing not acquire THD::LOCK_thd_data mutex below to avoid mutexing
order violation. */ order violation. */
if (!trx1->is_wsrep() || !lock_rec2->trx->is_wsrep()) if (!trx->is_wsrep() || !lock_trx->is_wsrep())
return; return;
if (UNIV_LIKELY(!wsrep_thd_is_BF(trx1->mysql_thd, FALSE))) if (UNIV_LIKELY(!wsrep_thd_is_BF(trx->mysql_thd, FALSE))
return; || UNIV_LIKELY(!wsrep_thd_is_BF(lock_trx->mysql_thd, FALSE)))
if (UNIV_LIKELY(!wsrep_thd_is_BF(lock_rec2->trx->mysql_thd, FALSE)))
return; return;
/* if BF - BF order is honored, we can keep trx1 waiting for the lock */ ut_ad(trx->state == TRX_STATE_ACTIVE);
if (wsrep_trx_order_before(trx1->mysql_thd, lock_rec2->trx->mysql_thd))
trx_mutex_enter(lock_trx);
const trx_state_t trx2_state= lock_trx->state;
trx_mutex_exit(lock_trx);
/* If transaction is already committed in memory or
prepared we should wait. When transaction is committed in
memory we held trx mutex, but not lock_sys->mutex. Therefore,
we could end here before transaction has time to do
lock_release() that is protected with lock_sys->mutex. */
switch (trx2_state) {
case TRX_STATE_COMMITTED_IN_MEMORY:
case TRX_STATE_PREPARED:
return; return;
case TRX_STATE_ACTIVE:
break;
default:
ut_ad("invalid state" == 0);
}
/* avoiding BF-BF conflict assert, if victim is already aborting /* If BF - BF order is honored, i.e. trx already holding
or rolling back for replaying record lock should be ordered before this new lock request
*/ we can keep trx waiting for the lock. If conflicting
if (wsrep_trx_is_aborting(lock_rec2->trx->mysql_thd)) transaction is already aborting or rolling back for replaying
we can also let new transaction waiting. */
if (wsrep_trx_order_before(lock_trx->mysql_thd, trx->mysql_thd)
|| wsrep_trx_is_aborting(lock_trx->mysql_thd))
return; return;
mtr_t mtr; mtr_t mtr;
if (lock_rec1) {
ib::error() << "Waiting lock on table: "
<< lock_rec1->index->table->name
<< " index: "
<< lock_rec1->index->name()
<< " that has conflicting lock ";
lock_rec_print(stderr, lock_rec1, mtr);
}
ib::error() << "Conflicting lock on table: " ib::error() << "Conflicting lock on table: "
<< lock_rec2->index->table->name << lock->index->table->name
<< " index: " << " index: "
<< lock_rec2->index->name() << lock->index->name()
<< " that has lock "; << " that has lock ";
lock_rec_print(stderr, lock_rec2, mtr); lock_rec_print(stderr, lock, mtr);
ib::error() << "WSREP state: "; ib::error() << "WSREP state: ";
wsrep_report_bf_lock_wait(trx1->mysql_thd, wsrep_report_bf_lock_wait(trx->mysql_thd,
trx1->id); trx->id);
wsrep_report_bf_lock_wait(lock_rec2->trx->mysql_thd, wsrep_report_bf_lock_wait(lock_trx->mysql_thd,
lock_rec2->trx->id); lock_trx->id);
/* BF-BF wait is a bug */ /* BF-BF wait is a bug */
ut_error; ut_error;
} }
#endif /* WITH_WSREP */ #endif /* WITH_WSREP */
#endif /* UNIV_DEBUG */
/*********************************************************************//** /*********************************************************************//**
Checks if a lock request for a new lock has to wait for request lock2. Checks if a lock request for a new lock has to wait for request lock2.
...@@ -832,9 +842,11 @@ lock_rec_has_to_wait( ...@@ -832,9 +842,11 @@ lock_rec_has_to_wait(
return (FALSE); return (FALSE);
} }
/* There should not be two conflicting locks that are /* We very well can let bf to wait normally as other
brute force. If there is it is a bug. */ BF will be replayed in case of conflict. For debug
wsrep_assert_no_bf_bf_wait(NULL, lock2, trx); builds we will do additional sanity checks to catch
unsupported bf wait if any. */
ut_d(wsrep_assert_no_bf_bf_wait(lock2, trx));
#endif /* WITH_WSREP */ #endif /* WITH_WSREP */
return(TRUE); return(TRUE);
...@@ -1111,66 +1123,35 @@ lock_rec_other_has_expl_req( ...@@ -1111,66 +1123,35 @@ lock_rec_other_has_expl_req(
#endif /* UNIV_DEBUG */ #endif /* UNIV_DEBUG */
#ifdef WITH_WSREP #ifdef WITH_WSREP
static static void wsrep_kill_victim(const trx_t * const trx, const lock_t *lock)
void
wsrep_kill_victim(
/*==============*/
const trx_t * const trx,
const lock_t *lock)
{ {
ut_ad(lock_mutex_own()); ut_ad(lock_mutex_own());
ut_ad(trx_mutex_own(lock->trx)); ut_ad(trx->is_wsrep());
trx_t* lock_trx = lock->trx;
ut_ad(trx_mutex_own(lock_trx));
ut_ad(lock_trx != trx);
/* quit for native mysql */ if (!wsrep_thd_is_BF(trx->mysql_thd, FALSE))
if (!trx->is_wsrep()) return; return;
my_bool bf_this = wsrep_thd_is_BF(trx->mysql_thd, FALSE); if (lock_trx->state == TRX_STATE_COMMITTED_IN_MEMORY
my_bool bf_other = wsrep_thd_is_BF(lock->trx->mysql_thd, FALSE); || lock_trx->lock.was_chosen_as_deadlock_victim)
mtr_t mtr; return;
if ((bf_this && !bf_other) || my_bool bf_other = wsrep_thd_is_BF(lock_trx->mysql_thd, FALSE);
(bf_this && bf_other && wsrep_trx_order_before(
trx->mysql_thd, lock->trx->mysql_thd))) {
if (lock->trx->lock.que_state == TRX_QUE_LOCK_WAIT) { if (!bf_other
if (UNIV_UNLIKELY(wsrep_debug)) { || wsrep_trx_order_before(trx->mysql_thd,
ib::info() << "WSREP: BF victim waiting\n"; lock_trx->mysql_thd)) {
}
if (lock_trx->lock.que_state == TRX_QUE_LOCK_WAIT) {
if (UNIV_UNLIKELY(wsrep_debug))
WSREP_INFO("BF victim waiting");
/* cannot release lock, until our lock /* cannot release lock, until our lock
is in the queue*/ is in the queue*/
} else if (lock->trx != trx) { } else {
if (wsrep_log_conflicts) { wsrep_innobase_kill_one_trx(trx->mysql_thd, trx,
if (bf_this) { lock_trx, true);
ib::info() << "*** Priority TRANSACTION:";
} else {
ib::info() << "*** Victim TRANSACTION:";
}
wsrep_trx_print_locking(stderr, trx, 3000);
if (bf_other) {
ib::info() << "*** Priority TRANSACTION:";
} else {
ib::info() << "*** Victim TRANSACTION:";
}
wsrep_trx_print_locking(stderr, lock->trx, 3000);
ib::info() << "*** WAITING FOR THIS LOCK TO BE GRANTED:";
if (lock_get_type(lock) == LOCK_REC) {
lock_rec_print(stderr, lock, mtr);
} else {
lock_table_print(stderr, lock);
}
ib::info() << " SQL1: "
<< wsrep_thd_query(trx->mysql_thd);
ib::info() << " SQL2: "
<< wsrep_thd_query(lock->trx->mysql_thd);
}
wsrep_innobase_kill_one_trx(trx->mysql_thd,
trx, lock->trx, TRUE);
} }
} }
} }
...@@ -2410,10 +2391,6 @@ static void lock_rec_dequeue_from_page(lock_t* in_lock) ...@@ -2410,10 +2391,6 @@ static void lock_rec_dequeue_from_page(lock_t* in_lock)
/* Grant the lock */ /* Grant the lock */
ut_ad(lock->trx != in_lock->trx); ut_ad(lock->trx != in_lock->trx);
lock_grant(lock); lock_grant(lock);
#ifdef WITH_WSREP
} else {
wsrep_assert_no_bf_bf_wait(c, lock, c->trx);
#endif /* WITH_WSREP */
} }
} }
} else { } else {
...@@ -4342,10 +4319,6 @@ lock_rec_unlock( ...@@ -4342,10 +4319,6 @@ lock_rec_unlock(
/* Grant the lock */ /* Grant the lock */
ut_ad(trx != lock->trx); ut_ad(trx != lock->trx);
lock_grant(lock); lock_grant(lock);
#ifdef WITH_WSREP
} else {
wsrep_assert_no_bf_bf_wait(c, lock, c->trx);
#endif /* WITH_WSREP */
} }
} }
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment