Commit c2a756dd authored by Rich Prohaska's avatar Rich Prohaska

FT-633 test case and bug fix for the data race between the lock request start...

FT-633 test case and bug fix for the data race between the lock request start and retry functions that results in a stall in the condition var broadcast
parent 254d71b5
......@@ -113,12 +113,19 @@ void lock_request::create(void) {
m_complete_r = 0;
m_state = state::UNINITIALIZED;
m_info = nullptr;
toku_cond_init(&m_wait_cond, nullptr);
m_start_test_callback = nullptr;
m_retry_test_callback = nullptr;
}
// destroy a lock request.
void lock_request::destroy(void) {
invariant(m_state != state::PENDING);
invariant(m_state != state::DESTROYED);
m_state = state::DESTROYED;
toku_destroy_dbt(&m_left_key_copy);
toku_destroy_dbt(&m_right_key_copy);
toku_cond_destroy(&m_wait_cond);
......@@ -135,7 +142,7 @@ void lock_request::set(locktree *lt, TXNID txnid, const DBT *left_key, const DBT
toku_destroy_dbt(&m_right_key_copy);
m_type = lock_type;
m_state = state::INITIALIZED;
m_info = lt->get_lock_request_info();
m_info = lt ? lt->get_lock_request_info() : nullptr;
m_big_txn = big_txn;
}
......@@ -223,15 +230,18 @@ int lock_request::start(void) {
insert_into_lock_requests();
if (deadlock_exists(conflicts)) {
remove_from_lock_requests();
complete(DB_LOCK_DEADLOCK);
r = DB_LOCK_DEADLOCK;
}
toku_mutex_unlock(&m_info->mutex);
} else {
if (m_start_test_callback) m_start_test_callback(); // test callback
}
if (r != DB_LOCK_NOTGRANTED) {
complete(r);
}
conflicts.destroy();
return m_state == state::COMPLETE ? m_complete_r : r;
return r;
}
// sleep on the lock request until it becomes resolved or the wait time has elapsed.
......@@ -292,8 +302,8 @@ int lock_request::wait(uint64_t wait_time_ms, uint64_t killed_time_ms, int (*kil
// complete this lock request with the given return value
void lock_request::complete(int complete_r) {
m_state = state::COMPLETE;
m_complete_r = complete_r;
m_state = state::COMPLETE;
}
const DBT *lock_request::get_left_key(void) const {
......@@ -331,6 +341,7 @@ int lock_request::retry(void) {
if (r == 0) {
remove_from_lock_requests();
complete(r);
if (m_retry_test_callback) m_retry_test_callback(); // test callback
toku_cond_broadcast(&m_wait_cond);
}
......@@ -416,7 +427,8 @@ void lock_request::remove_from_lock_requests(void) {
uint32_t idx;
lock_request *request;
int r = m_info->pending_lock_requests.find_zero<TXNID, find_by_txnid>(m_txnid, &request, &idx);
invariant_zero(r && request == this);
invariant_zero(r);
invariant(request == this);
r = m_info->pending_lock_requests.delete_at(idx);
invariant_zero(r);
}
......@@ -432,4 +444,12 @@ int lock_request::find_by_txnid(lock_request * const &request, const TXNID &txni
}
}
void lock_request::set_start_test_callback(void (*f)(void)) {
m_start_test_callback = f;
}
void lock_request::set_retry_test_callback(void (*f)(void)) {
m_retry_test_callback = f;
}
} /* namespace toku */
......@@ -164,6 +164,8 @@ class lock_request {
// The rest remain pending.
static void retry_all_lock_requests(locktree *lt);
void set_start_test_callback(void (*f)(void));
void set_retry_test_callback(void (*f)(void));
private:
enum state {
......@@ -171,6 +173,7 @@ class lock_request {
INITIALIZED,
PENDING,
COMPLETE,
DESTROYED,
};
// The keys for a lock request are stored "unowned" in m_left_key
......@@ -236,6 +239,9 @@ class lock_request {
static int find_by_txnid(lock_request * const &request, const TXNID &txnid);
void (*m_start_test_callback)(void);
void (*m_retry_test_callback)(void);
friend class lock_request_unit_test;
};
ENSURE_POD(lock_request);
......
......@@ -152,6 +152,7 @@ void locktree::create(locktree_manager *mgr, DICTIONARY_ID dict_id, const compar
void locktree::destroy(void) {
invariant(m_reference_count == 0);
invariant(m_lock_request_info.pending_lock_requests.size() == 0);
m_cmp.destroy();
m_rangetree->destroy();
toku_free(m_rangetree);
......
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*
COPYING CONDITIONS NOTICE:
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation, and provided that the
following conditions are met:
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below).
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below) in the documentation and/or other materials
provided with the distribution.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
COPYRIGHT NOTICE:
TokuFT, Tokutek Fractal Tree Indexing Library.
Copyright (C) 2007-2013 Tokutek, Inc.
DISCLAIMER:
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
UNIVERSITY PATENT NOTICE:
The technology is licensed by the Massachusetts Institute of
Technology, Rutgers State University of New Jersey, and the Research
Foundation of State University of New York at Stony Brook under
United States of America Serial No. 11/760379 and to the patents
and/or patent applications resulting from it.
PATENT MARKING NOTICE:
This software is covered by US Patent No. 8,185,551.
This software is covered by US Patent No. 8,489,638.
PATENT RIGHTS GRANT:
"THIS IMPLEMENTATION" means the copyrightable works distributed by
Tokutek as part of the Fractal Tree project.
"PATENT CLAIMS" means the claims of patents that are owned or
licensable by Tokutek, both currently or in the future; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
"PATENT CHALLENGE" shall mean a challenge to the validity,
patentability, enforceability and/or non-infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
Tokutek hereby grants to you, for the term and geographical scope of
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and
otherwise run, modify, and propagate the contents of THIS
IMPLEMENTATION, where such license applies only to the PATENT
CLAIMS. This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION. If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any rights
granted to you under this License shall terminate as of the date
such litigation is filed. If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE, then Tokutek may terminate any rights granted to you
under this License.
*/
#ident "Copyright (c) 2014 Tokutek Inc. All rights reserved."
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
#include "test.h"
#include "locktree.h"
#include "lock_request.h"
// Test FT-633, the data race on the lock request between ::start and ::retry
// This test is non-deterministic. It uses sleeps at 2 critical places to
// expose the data race on the lock requests state.
namespace toku {
struct locker_arg {
locktree *_lt;
TXNID _id;
const DBT *_key;
locker_arg(locktree *lt, TXNID id, const DBT *key) : _lt(lt), _id(id), _key(key) {
}
};
static void locker_callback(void) {
usleep(10000);
}
static void run_locker(locktree *lt, TXNID txnid, const DBT *key) {
int i;
for (i = 0; i < 1000; i++) {
lock_request request;
request.create();
request.set(lt, txnid, key, key, lock_request::type::WRITE, false);
// set the test callbacks
request.set_start_test_callback(locker_callback);
request.set_retry_test_callback(locker_callback);
// try to acquire the lock
int r = request.start();
if (r == DB_LOCK_NOTGRANTED) {
// wait for the lock to be granted
r = request.wait(10 * 1000);
}
if (r == 0) {
// release the lock
range_buffer buffer;
buffer.create();
buffer.append(key, key);
lt->release_locks(txnid, &buffer);
buffer.destroy();
// retry pending lock requests
lock_request::retry_all_lock_requests(lt);
}
request.destroy();
memset(&request, 0xab, sizeof request);
toku_pthread_yield();
if ((i % 10) == 0)
printf("%lu %d\n", toku_pthread_self(), i);
}
}
static void *locker(void *v_arg) {
locker_arg *arg = static_cast<locker_arg *>(v_arg);
run_locker(arg->_lt, arg->_id, arg->_key);
return arg;
}
} /* namespace toku */
int main(void) {
int r;
toku::locktree lt;
DICTIONARY_ID dict_id = { 1 };
lt.create(nullptr, dict_id, toku::dbt_comparator);
const DBT *one = toku::get_dbt(1);
const int n_workers = 2;
toku_pthread_t ids[n_workers];
for (int i = 0; i < n_workers; i++) {
toku::locker_arg *arg = new toku::locker_arg(&lt, i, one);
r = toku_pthread_create(&ids[i], nullptr, toku::locker, arg);
assert_zero(r);
}
for (int i = 0; i < n_workers; i++) {
void *ret;
r = toku_pthread_join(ids[i], &ret);
assert_zero(r);
toku::locker_arg *arg = static_cast<toku::locker_arg *>(ret);
delete arg;
}
lt.release_reference();
lt.destroy();
return 0;
}
......@@ -305,6 +305,7 @@ void toku_db_grab_write_lock (DB *db, DBT *key, TOKUTXN tokutxn) {
int r = request.start();
invariant_zero(r);
db_txn_note_row_lock(db, txn_anc, key, key);
request.destroy();
}
void toku_db_release_lt_key_ranges(DB_TXN *txn, txn_lt_key_ranges *ranges) {
......
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