w_rc_t bt_cursor_t::_refix_current_key(btree_page_h &p) { while (true) { w_rc_t fix_rt = p.refix_direct(_pid_bfidx.idx(), LATCH_SH); if (!fix_rt.is_error()) { break; // mostly no error. } if(fix_rt.err_num() != eBF_DIRECTFIX_SWIZZLED_PTR) { return fix_rt; // unexpected error code } W_DO(btree_impl::_ux_traverse(_store, _key, btree_impl::t_fence_contain, LATCH_SH, p)); _slot = _forward ? 0 : p.nrecs() - 1; _set_current_page(p); // now let's re-locate the key } return RCOK; }
rc_t btree_impl::_ux_lock_key( const StoreID& store, btree_page_h& leaf, const void* keystr, size_t keylen, latch_mode_t latch_mode, const okvl_mode& lock_mode, bool check_only ) { // Callers: // 1. Top level _ux_lock_key() - I/U/D and search operations, lock conflict is possible // 2. _ux_lock_range() - lock conflict is possible // // Lock conflict: // 1. Deadlock - the asking lock is held by another transaction currently, and the // current transaction is holding other locks already, failed // 2. Timeout - the asking lock is held by another transaction currently, but the // current transaction does not hold other locks, okay to retry // For restart operation using lock re-acquisition: // 1. On_demand or mixed UNDO - when lock conflict. it triggers UNDO transaction rollback // this is a blocking operation, meaning the other concurrent // transactions asking for the same lock are blocked, no deadlock // 2. Traditional UNDO - original behavior, either deadlock error or timeout and retry lockid_t lid (store, (const unsigned char*) keystr, keylen); // first, try conditionally. we utilize the inserted lock entry even if it fails RawLock* entry = nullptr; // The lock request does the following: // If the lock() failed to acquire lock (trying to acquire lock while holding the latch) and // if the transaction doesn't have any other locks, because 'condition' is true, lock() // returns immediatelly with eCONDLOCKTIMEOUT which indicates it failed to // acquire lock but no deadlock worry and the lock entry has been created already. // In this case caller (this function) releases latch and try again using retry_lock() // which is a blocking operation, this is because it is safe to forever retry without // risking deadlock // If the lock() returns eDEADLOCK, it means lock acquisition failed and // the current transaction already held other locks, it is not safe to retry (will cause // further deadlocks) therefore caller must abort the current transaction rc_t lock_rc = lm->lock(lid.hash(), lock_mode, true /*check */, false /* wait */, !check_only /* acquire */, smthread_t::xct(),timeout_t::WAIT_IMMEDIATE, &entry); if (!lock_rc.is_error()) { // lucky! we got it immediately. just return. return RCOK; } else { // if it caused deadlock and it was chosen to be victim, give up! (not retry) if (lock_rc.err_num() == eDEADLOCK) { // The user transaction will abort and rollback itself upon deadlock detection. // Because Express does not have a deadlock monitor and policy to determine // which transaction to rollback during a deadlock (should abort the cheaper // transaction), the user transaction which detects deadlock will be aborted. w_assert1(entry == nullptr); return lock_rc; } // couldn't immediately get it. then we unlatch the page and wait. w_assert1(lock_rc.err_num() == eCONDLOCKTIMEOUT); w_assert1(entry != nullptr); // we release the latch here. However, we increment the pin count before that // to prevent the page from being evicted. pin_for_refix_holder pin_holder(leaf.pin_for_refix()); // automatically releases the pin lsn_t prelsn = leaf.get_page_lsn(); // to check if it's modified after this unlatch leaf.unfix(); // then, we try it unconditionally (this will block) W_DO(lm->retry_lock(&entry, !check_only /* acquire */)); // now we got the lock.. but it might be changed because we unlatched. w_rc_t refix_rc = leaf.refix_direct(pin_holder._idx, latch_mode); if (refix_rc.is_error() || leaf.get_page_lsn() != prelsn) { // release acquired lock if (entry != nullptr) { w_assert1(!check_only); lm->unlock(entry); } else { w_assert1(check_only); } if (refix_rc.is_error()) { return refix_rc; } else { w_assert1(leaf.get_page_lsn() != prelsn); // unluckily, it's the case return RC(eLOCKRETRY); // retry! } } return RCOK; } }