diff options
author | sensssz <hjmsens@gmail.com> | 2016-10-25 18:57:03 -0400 |
---|---|---|
committer | sensssz <hjmsens@gmail.com> | 2016-10-25 18:57:03 -0400 |
commit | 74961760a4837d2deb33336329c28cf9ad9b4e9e (patch) | |
tree | 7bd2c4b26660c53fe4d7bd129a3130ef093c45cd /storage/xtradb/lock | |
parent | 183c02839f032e1d9057fd4e278806c26b016826 (diff) | |
download | mariadb-git-74961760a4837d2deb33336329c28cf9ad9b4e9e.tar.gz |
A few fixes for VATS in 10.1
Diffstat (limited to 'storage/xtradb/lock')
-rw-r--r-- | storage/xtradb/lock/lock0lock.cc | 423 |
1 files changed, 316 insertions, 107 deletions
diff --git a/storage/xtradb/lock/lock0lock.cc b/storage/xtradb/lock/lock0lock.cc index 2091f926153..e626821ed7e 100644 --- a/storage/xtradb/lock/lock0lock.cc +++ b/storage/xtradb/lock/lock0lock.cc @@ -383,6 +383,25 @@ struct lock_stack_t { ulint heap_no; /*!< heap number if rec lock */ }; +/*********************************************************************//** +Checks if a waiting record lock request still has to wait in a queue. +@return lock that is causing the wait */ +static +const lock_t* +lock_rec_has_to_wait_in_queue( +/*==========================*/ + const lock_t* wait_lock); /*!< in: waiting record lock */ + +/*************************************************************//** +Grants a lock to a waiting lock request and releases the waiting transaction. +The caller must hold lock_sys->mutex. */ +static +void +lock_grant( +/*=======*/ + lock_t* lock, /*!< in/out: waiting lock request */ + bool owns_trx_mutex); /*!< in: whether lock->trx->mutex is owned */ + extern "C" void thd_report_wait_for(MYSQL_THD thd, MYSQL_THD other_thd); extern "C" int thd_need_wait_for(const MYSQL_THD thd); extern "C" @@ -2045,48 +2064,121 @@ has_higher_priority( } else if (!lock_get_wait(lock2)) { return false; } - return lock1->trx->start_time < lock2->trx->start_time; + return lock1->trx->start_time_micro <= lock2->trx->start_time_micro; } /*********************************************************************//** -Insert a lock to the hash list according to the mode (whether it is a wait lock) -and the age of the transaction the it is associated with. +Insert a lock to the hash list according to the mode (whether it is a wait +lock) and the age of the transaction the it is associated with. If the lock is not a wait lock, insert it to the head of the hash list. -Otherwise, insert it to the middle of the wait locks according to the age of the -transaciton. -*/ +Otherwise, insert it to the middle of the wait locks according to the age of +the transaciton. */ static -void +dberr_t lock_rec_insert_by_trx_age( - lock_t *in_lock, /*!< in: lock to be insert */ - bool wait) /*!< in: whether it's a wait lock */ -{ - ulint space; - ulint page_no; - ulint rec_fold; - hash_cell_t* cell; + lock_t *in_lock) /*!< in: lock to be insert */{ + ulint space; + ulint page_no; + ulint rec_fold; lock_t* node; lock_t* next; + hash_cell_t* cell; space = in_lock->un_member.rec_lock.space; page_no = in_lock->un_member.rec_lock.page_no; rec_fold = lock_rec_fold(space, page_no); cell = hash_get_nth_cell(lock_sys->rec_hash, - hash_calc_hash(rec_fold, lock_sys->rec_hash)); + hash_calc_hash(rec_fold, lock_sys->rec_hash)); node = (lock_t *) cell->node; // If in_lock is not a wait lock, we insert it to the head of the list. - if (node == NULL || !wait || has_higher_priority(in_lock, node)) { + if (node == NULL || !lock_get_wait(in_lock) || has_higher_priority(in_lock, node)) { cell->node = in_lock; in_lock->hash = node; - return; + if (lock_get_wait(in_lock)) { + lock_grant(in_lock, true); + return DB_SUCCESS_LOCKED_REC; + } + return DB_SUCCESS; } - while (node != NULL && has_higher_priority((lock_t *) node->hash, in_lock)) { + while (node != NULL && has_higher_priority((lock_t *) node->hash, + in_lock)) { node = (lock_t *) node->hash; } next = (lock_t *) node->hash; node->hash = in_lock; in_lock->hash = next; + + if (lock_get_wait(in_lock) && !lock_rec_has_to_wait_in_queue(in_lock)) { + lock_grant(in_lock, true); + if (cell->node != in_lock) { + // Move it to the front of the queue + node->hash = in_lock->hash; + next = (lock_t *) cell->node; + cell->node = in_lock; + in_lock->hash = next; + } + return DB_SUCCESS_LOCKED_REC; + } + + return DB_SUCCESS; +} + +static +bool +lock_queue_validate( + const lock_t *in_lock) /*!< in: lock whose hash list is to be validated */ +{ + ulint space; + ulint page_no; + ulint rec_fold; + hash_table_t* hash; + hash_cell_t* cell; + lock_t* next; + bool wait_lock = false; + + if (in_lock == NULL) { + return true; + } + + space = in_lock->un_member.rec_lock.space; + page_no = in_lock->un_member.rec_lock.page_no; + rec_fold = lock_rec_fold(space, page_no); + cell = hash_get_nth_cell(lock_sys->rec_hash, + hash_calc_hash(rec_fold, lock_sys->rec_hash)); + next = (lock_t *) cell->node; + while (next != NULL) { + // If this is a granted lock, check that there's no wait lock before it. + if (!lock_get_wait(next)) { + ut_ad(!wait_lock); + } else { + wait_lock = true; + } + next = (lock_t *) next->hash; + } + return true; +} + +static +void +lock_rec_insert_to_head( + lock_t *in_lock, /*!< in: lock to be insert */ + ulint rec_fold) /*!< in: rec_fold of the page */ +{ + hash_cell_t* cell; + lock_t* node; + + if (in_lock == NULL) { + return; + } + + cell = hash_get_nth_cell(lock_sys->rec_hash, + hash_calc_hash(rec_fold, lock_sys->rec_hash)); + node = (lock_t *) cell->node; + if (node != in_lock) { + cell->node = in_lock; + in_lock->hash = node; + } } /*********************************************************************//** @@ -2116,8 +2208,10 @@ lock_rec_create( lock_t* lock; ulint page_no; ulint space; + ulint rec_fold; ulint n_bits; ulint n_bytes; + bool wait_lock; const page_t* page; ut_ad(lock_mutex_own()); @@ -2144,6 +2238,8 @@ lock_rec_create( type_mode = type_mode & ~(LOCK_GAP | LOCK_REC_NOT_GAP); } + wait_lock = type_mode & LOCK_WAIT; + /* Make lock bitmap bigger by a safety margin */ n_bits = page_dir_get_n_heap(page) + LOCK_PAGE_BITMAP_MARGIN; n_bytes = 1 + n_bits / 8; @@ -2159,6 +2255,7 @@ lock_rec_create( lock->un_member.rec_lock.space = space; lock->un_member.rec_lock.page_no = page_no; lock->un_member.rec_lock.n_bits = n_bytes * 8; + rec_fold = lock_rec_fold(space, page_no); /* Reset to zero the bitmap which resides immediately after the lock struct */ @@ -2251,13 +2348,27 @@ lock_rec_create( return(lock); } trx_mutex_exit(c_lock->trx); + } else if (innodb_lock_schedule_algorithm == INNODB_LOCK_SCHEDULE_ALGORITHM_VATS + && !thd_is_replication_slave_thread(lock->trx->mysql_thd)) { + if (wait_lock) { + HASH_INSERT(lock_t, hash, lock_sys->rec_hash, rec_fold, lock); + } else { + lock_rec_insert_to_head(lock, rec_fold); + } } else { - HASH_INSERT(lock_t, hash, lock_sys->rec_hash, - lock_rec_fold(space, page_no), lock); + HASH_INSERT(lock_t, hash, lock_sys->rec_hash, rec_fold, lock); } #else - HASH_INSERT(lock_t, hash, lock_sys->rec_hash, - lock_rec_fold(space, page_no), lock); + if (innodb_lock_schedule_algorithm == INNODB_LOCK_SCHEDULE_ALGORITHM_VATS + && !thd_is_replication_slave_thread(lock->trx->mysql_thd)) { + if (wait_lock) { + HASH_INSERT(lock_t, hash, lock_sys->rec_hash, rec_fold, lock); + } else { + lock_rec_insert_to_head(lock, rec_fold); + } + } else { + HASH_INSERT(lock_t, hash, lock_sys->rec_hash, rec_fold, lock); + } #endif /* WITH_WSREP */ lock_sys->rec_num++; @@ -2316,6 +2427,9 @@ lock_rec_enqueue_waiting( trx_id_t victim_trx_id; ulint sec; ulint ms; + ulint space; + ulint page_no; + dberr_t err; ut_ad(lock_mutex_own()); @@ -2390,41 +2504,51 @@ lock_rec_enqueue_waiting( transaction as a victim, it is possible that we already have the lock now granted! */ - return(DB_SUCCESS_LOCKED_REC); - } - - // Move it only when it does not cause a deadlock. - if (innodb_lock_schedule_algorithm == INNODB_LOCK_SCHEDULE_ALGORITHM_VATS) { - HASH_DELETE(lock_t, hash, lock_sys->rec_hash, - lock_rec_fold(buf_block_get_space(block), buf_block_get_page_no(block)), lock); - lock_rec_insert_by_trx_age(lock, true); - } - - trx->lock.que_state = TRX_QUE_LOCK_WAIT; + err = DB_SUCCESS_LOCKED_REC; + } else { + trx->lock.que_state = TRX_QUE_LOCK_WAIT; - trx->lock.was_chosen_as_deadlock_victim = FALSE; - trx->lock.wait_started = ut_time(); + trx->lock.was_chosen_as_deadlock_victim = FALSE; + trx->lock.wait_started = ut_time(); - if (UNIV_UNLIKELY(trx->take_stats)) { - ut_usectime(&sec, &ms); - trx->lock_que_wait_ustarted = (ib_uint64_t)sec * 1000000 + ms; - } + if (UNIV_UNLIKELY(trx->take_stats)) { + ut_usectime(&sec, &ms); + trx->lock_que_wait_ustarted = (ib_uint64_t)sec * 1000000 + ms; + } - ut_a(que_thr_stop(thr)); + ut_a(que_thr_stop(thr)); #ifdef UNIV_DEBUG - if (lock_print_waits) { - fprintf(stderr, "Lock wait for trx " TRX_ID_FMT " in index ", - trx->id); - ut_print_name(stderr, trx, FALSE, index->name); - } + if (lock_print_waits) { + fprintf(stderr, "Lock wait for trx " TRX_ID_FMT " in index ", + trx->id); + ut_print_name(stderr, trx, FALSE, index->name); + } #endif /* UNIV_DEBUG */ - MONITOR_INC(MONITOR_LOCKREC_WAIT); + MONITOR_INC(MONITOR_LOCKREC_WAIT); - trx->n_rec_lock_waits++; + trx->n_rec_lock_waits++; - return(DB_LOCK_WAIT); + err = DB_LOCK_WAIT; + } + + // Move it only when it does not cause a deadlock. + if (err != DB_DEADLOCK + && innodb_lock_schedule_algorithm + == INNODB_LOCK_SCHEDULE_ALGORITHM_VATS + && !thd_is_replication_slave_thread(lock->trx->mysql_thd)) { + space = buf_block_get_space(block); + page_no = buf_block_get_page_no(block); + HASH_DELETE(lock_t, hash, lock_sys->rec_hash, + lock_rec_fold(space, page_no), lock); + dberr_t res = lock_rec_insert_by_trx_age(lock); + if (res != DB_SUCCESS) { + return res; + } + } + + return err; } /*********************************************************************//** @@ -2856,13 +2980,16 @@ static void lock_grant( /*=======*/ - lock_t* lock) /*!< in/out: waiting lock request */ + lock_t* lock, /*!< in/out: waiting lock request */ + bool owns_trx_mutex) /*!< in: whether lock->trx->mutex is owned */ { ut_ad(lock_mutex_own()); lock_reset_lock_and_trx_wait(lock); - trx_mutex_enter(lock->trx); + if (!owns_trx_mutex) { + trx_mutex_enter(lock->trx); + } if (lock_get_mode(lock) == LOCK_AUTO_INC) { dict_table_t* table = lock->un_member.tab_lock.table; @@ -2911,7 +3038,9 @@ lock_grant( lock->wait_time = (ulint)difftime(ut_time(), lock->requested_time); - trx_mutex_exit(lock->trx); + if (!owns_trx_mutex) { + trx_mutex_exit(lock->trx); + } } /*************************************************************//** @@ -2949,23 +3078,62 @@ lock_rec_cancel( trx_mutex_exit(lock->trx); } -/*************************************************************//** -Move the lock to the head of the hash list. */ static void -lock_rec_move_to_front( - lock_t *lock_to_move, /*!< in: lock to be moved */ - ulint rec_fold) /*!< in: rec fold of the lock */ +lock_grant_and_move_on_page( + ulint space, + ulint page_no) { - if (lock_to_move != NULL) - { - // Move the target lock to the head of the list - hash_cell_t* cell = hash_get_nth_cell(lock_sys->rec_hash, - hash_calc_hash(rec_fold, lock_sys->rec_hash)); - if (lock_to_move != cell->node) { - lock_t *next = (lock_t *) cell->node; - cell->node = lock_to_move; - lock_to_move->hash = next; + lock_t* lock; + lock_t* next; + lock_t* previous; + ulint rec_fold = lock_rec_fold(space, page_no); + + previous = (lock_t *) hash_get_nth_cell(lock_sys->rec_hash, + hash_calc_hash(rec_fold, lock_sys->rec_hash))->node; + if (previous == NULL) { + return; + } + if (previous->un_member.rec_lock.space == space && + previous->un_member.rec_lock.page_no == page_no) { + lock = previous; + } + else { + next = (lock_t *) previous->hash; + while (next && + (next->un_member.rec_lock.space != space || + next->un_member.rec_lock.page_no != page_no)) { + previous = next; + next = (lock_t *) previous->hash; + } + lock = (lock_t *) previous->hash; + } + + ut_ad(previous->hash == lock || previous == lock); + /* Grant locks if there are no conflicting locks ahead. + Move granted locks to the head of the list. */ + for (;lock != NULL;) { + /* If the lock is a wait lock on this page, and it does not need to wait. */ + if ((lock->un_member.rec_lock.space == space) + && (lock->un_member.rec_lock.page_no == page_no) + && lock_get_wait(lock) + && !lock_rec_has_to_wait_in_queue(lock)) { + + lock_grant(lock, false); + + if (previous != NULL) { + /* Move the lock to the head of the list. */ + HASH_GET_NEXT(hash, previous) = HASH_GET_NEXT(hash, lock); + lock_rec_insert_to_head(lock, rec_fold); + } else { + /* Already at the head of the list. */ + previous = lock; + } + /* Move on to the next lock. */ + lock = static_cast<lock_t *>(HASH_GET_NEXT(hash, previous)); + } else { + previous = lock; + lock = static_cast<lock_t *>(HASH_GET_NEXT(hash, lock)); } } } @@ -2987,9 +3155,7 @@ lock_rec_dequeue_from_page( { ulint space; ulint page_no; - ulint rec_fold; lock_t* lock; - lock_t* previous = NULL; trx_lock_t* trx_lock; ut_ad(lock_mutex_own()); @@ -3000,7 +3166,6 @@ lock_rec_dequeue_from_page( space = in_lock->un_member.rec_lock.space; page_no = in_lock->un_member.rec_lock.page_no; - rec_fold = lock_rec_fold(space, page_no); in_lock->index->table->n_rec_locks--; @@ -3013,7 +3178,9 @@ lock_rec_dequeue_from_page( MONITOR_INC(MONITOR_RECLOCK_REMOVED); MONITOR_DEC(MONITOR_NUM_RECLOCK); - if (innodb_lock_schedule_algorithm == INNODB_LOCK_SCHEDULE_ALGORITHM_FCFS) { + if (innodb_lock_schedule_algorithm + == INNODB_LOCK_SCHEDULE_ALGORITHM_FCFS || + thd_is_replication_slave_thread(in_lock->trx->mysql_thd)) { /* Check if waiting locks in the queue can now be granted: grant locks if there are no conflicting locks ahead. Stop at the first X lock that is waiting or has been granted. */ @@ -3027,38 +3194,11 @@ lock_rec_dequeue_from_page( /* Grant the lock */ ut_ad(lock->trx != in_lock->trx); - lock_grant(lock); + lock_grant(lock, false); } } } else { - /* Grant locks if there are no conflicting locks ahead. - Move granted locks to the head of the list. */ - for (lock = lock_rec_get_first_on_page_addr(space, page_no); - lock != NULL;) { - - /* If the lock is a wait lock on this page, and it does not need to wait. */ - if ((lock->un_member.rec_lock.space == space) - && (lock->un_member.rec_lock.page_no == page_no) - && lock_get_wait(lock) - && !lock_rec_has_to_wait_in_queue(lock)) { - - lock_grant(lock); - - if (previous != NULL) { - /* Move the lock to the head of the list. */ - HASH_GET_NEXT(hash, previous) = HASH_GET_NEXT(hash, lock); - lock_rec_move_to_front(lock, rec_fold); - } else { - /* Already at the head of the list. */ - previous = lock; - } - /* Move on to the next lock. */ - lock = static_cast<lock_t *>(HASH_GET_NEXT(hash, previous)); - } else { - previous = lock; - lock = static_cast<lock_t *>(HASH_GET_NEXT(hash, lock)); - } - } + lock_grant_and_move_on_page(space, page_no); } } @@ -5370,12 +5510,71 @@ lock_table_dequeue( /* Grant the lock */ ut_ad(in_lock->trx != lock->trx); - lock_grant(lock); + lock_grant(lock, false); } } } /*=========================== LOCK RELEASE ==============================*/ +static +void +lock_grant_and_move_on_rec( + lock_t* first_lock, + ulint heap_no) +{ + lock_t* lock; + lock_t* previous; + ulint space; + ulint page_no; + ulint rec_fold; + + space = first_lock->un_member.rec_lock.space; + page_no = first_lock->un_member.rec_lock.page_no; + rec_fold = lock_rec_fold(space, page_no); + + previous = (lock_t *) hash_get_nth_cell(lock_sys->rec_hash, + hash_calc_hash(rec_fold, lock_sys->rec_hash))->node; + if (previous == NULL) { + return; + } + if (previous == first_lock) { + lock = previous; + } else { + while (previous->hash && + previous->hash != first_lock) { + previous = (lock_t *) previous->hash; + } + lock = (lock_t *) previous->hash; + } + /* Grant locks if there are no conflicting locks ahead. + Move granted locks to the head of the list. */ + for (;lock != NULL;) { + + /* If the lock is a wait lock on this page, and it does not need to wait. */ + if (lock->un_member.rec_lock.space == space + && lock->un_member.rec_lock.page_no == page_no + && lock_rec_get_nth_bit(lock, heap_no) + && lock_get_wait(lock) + && !lock_rec_has_to_wait_in_queue(lock)) { + + lock_grant(lock, false); + + if (previous != NULL) { + /* Move the lock to the head of the list. */ + HASH_GET_NEXT(hash, previous) = HASH_GET_NEXT(hash, lock); + lock_rec_insert_to_head(lock, rec_fold); + } else { + /* Already at the head of the list. */ + previous = lock; + } + /* Move on to the next lock. */ + lock = static_cast<lock_t *>(HASH_GET_NEXT(hash, previous)); + } else { + previous = lock; + lock = static_cast<lock_t *>(HASH_GET_NEXT(hash, lock)); + } + } +} /*************************************************************//** Removes a granted record lock of a transaction from the queue and grants @@ -5439,17 +5638,24 @@ released: ut_a(!lock_get_wait(lock)); lock_rec_reset_nth_bit(lock, heap_no); - /* Check if we can now grant waiting lock requests */ + if (innodb_lock_schedule_algorithm + == INNODB_LOCK_SCHEDULE_ALGORITHM_FCFS || + thd_is_replication_slave_thread(lock->trx->mysql_thd)) { - for (lock = first_lock; lock != NULL; - lock = lock_rec_get_next(heap_no, lock)) { - if (lock_get_wait(lock) - && !lock_rec_has_to_wait_in_queue(lock)) { + /* Check if we can now grant waiting lock requests */ - /* Grant the lock */ - ut_ad(trx != lock->trx); - lock_grant(lock); + for (lock = first_lock; lock != NULL; + lock = lock_rec_get_next(heap_no, lock)) { + if (lock_get_wait(lock) + && !lock_rec_has_to_wait_in_queue(lock)) { + + /* Grant the lock */ + ut_ad(trx != lock->trx); + lock_grant(lock, false); + } } + } else { + lock_grant_and_move_on_rec(first_lock, heap_no); } lock_mutex_exit(); @@ -6505,6 +6711,9 @@ lock_rec_queue_validate( } } + ut_ad(innodb_lock_schedule_algorithm == INNODB_LOCK_SCHEDULE_ALGORITHM_FCFS || + lock_queue_validate(lock)); + func_exit: if (!locked_lock_trx_sys) { lock_mutex_exit(); |