diff options
Diffstat (limited to 'storage/tokudb/ft-index')
-rw-r--r-- | storage/tokudb/ft-index/buildheader/make_tdb.cc | 1 | ||||
-rw-r--r-- | storage/tokudb/ft-index/ft/ft-internal.h | 1 | ||||
-rw-r--r-- | storage/tokudb/ft-index/ft/ft-ops.cc | 12 | ||||
-rw-r--r-- | storage/tokudb/ft-index/locktree/lock_request.cc | 32 | ||||
-rw-r--r-- | storage/tokudb/ft-index/locktree/lock_request.h | 6 | ||||
-rw-r--r-- | storage/tokudb/ft-index/locktree/locktree.cc | 1 | ||||
-rw-r--r-- | storage/tokudb/ft-index/locktree/tests/lock_request_start_retry_race.cc | 193 | ||||
-rw-r--r-- | storage/tokudb/ft-index/src/ydb_row_lock.cc | 1 | ||||
-rw-r--r-- | storage/tokudb/ft-index/src/ydb_txn.cc | 6 |
9 files changed, 243 insertions, 10 deletions
diff --git a/storage/tokudb/ft-index/buildheader/make_tdb.cc b/storage/tokudb/ft-index/buildheader/make_tdb.cc index 9890b8ed34b..88f8882df78 100644 --- a/storage/tokudb/ft-index/buildheader/make_tdb.cc +++ b/storage/tokudb/ft-index/buildheader/make_tdb.cc @@ -585,6 +585,7 @@ static void print_db_txn_struct (void) { "uint64_t (*id64) (DB_TXN*)", "void (*set_client_id)(DB_TXN *, uint64_t client_id)", "uint64_t (*get_client_id)(DB_TXN *)", + "bool (*is_prepared)(DB_TXN *)", NULL}; sort_and_dump_fields("db_txn", false, extra); } diff --git a/storage/tokudb/ft-index/ft/ft-internal.h b/storage/tokudb/ft-index/ft/ft-internal.h index 3cd39705571..88fc5dca686 100644 --- a/storage/tokudb/ft-index/ft/ft-internal.h +++ b/storage/tokudb/ft-index/ft/ft-internal.h @@ -616,6 +616,7 @@ typedef enum { FT_PRO_RIGHTMOST_LEAF_SHORTCUT_SUCCESS, FT_PRO_RIGHTMOST_LEAF_SHORTCUT_FAIL_POS, FT_PRO_RIGHTMOST_LEAF_SHORTCUT_FAIL_REACTIVE, + FT_CURSOR_SKIP_DELETED_LEAF_ENTRY, // how many deleted leaf entries were skipped by a cursor FT_STATUS_NUM_ROWS } ft_status_entry; diff --git a/storage/tokudb/ft-index/ft/ft-ops.cc b/storage/tokudb/ft-index/ft/ft-ops.cc index 481d80fdbe4..34c9c46f1c6 100644 --- a/storage/tokudb/ft-index/ft/ft-ops.cc +++ b/storage/tokudb/ft-index/ft/ft-ops.cc @@ -377,6 +377,8 @@ status_init(void) STATUS_INIT(FT_PRO_RIGHTMOST_LEAF_SHORTCUT_FAIL_POS, nullptr, PARCOUNT, "promotion: tried the rightmost leaf shorcut but failed (out-of-bounds)", TOKU_ENGINE_STATUS); STATUS_INIT(FT_PRO_RIGHTMOST_LEAF_SHORTCUT_FAIL_REACTIVE,nullptr, PARCOUNT, "promotion: tried the rightmost leaf shorcut but failed (child reactive)", TOKU_ENGINE_STATUS); + STATUS_INIT(FT_CURSOR_SKIP_DELETED_LEAF_ENTRY, CURSOR_SKIP_DELETED_LEAF_ENTRY, PARCOUNT, "cursor skipped deleted leaf entries", TOKU_ENGINE_STATUS|TOKU_GLOBAL_STATUS); + ft_status.initialized = true; } static void status_destroy(void) { @@ -3378,13 +3380,13 @@ ok: ; if (le_val_is_del(le, ftcursor->is_snapshot_read, ftcursor->ttxn)) { // Provisionally deleted stuff is gone. // So we need to scan in the direction to see if we can find something. - // Every 100 deleted leaf entries check if the leaf's key is within the search bounds. - for (uint n_deleted = 1; ; n_deleted++) { + // Every 64 deleted leaf entries check if the leaf's key is within the search bounds. + for (uint64_t n_deleted = 1; ; n_deleted++) { switch (search->direction) { case FT_SEARCH_LEFT: idx++; - if (idx >= bn->data_buffer.num_klpairs() || - ((n_deleted % 64) == 0 && !search_continue(search, key, keylen))) { + if (idx >= bn->data_buffer.num_klpairs() || ((n_deleted % 64) == 0 && !search_continue(search, key, keylen))) { + STATUS_INC(FT_CURSOR_SKIP_DELETED_LEAF_ENTRY, n_deleted); if (ftcursor->interrupt_cb && ftcursor->interrupt_cb(ftcursor->interrupt_cb_extra)) { return TOKUDB_INTERRUPTED; } @@ -3393,6 +3395,7 @@ ok: ; break; case FT_SEARCH_RIGHT: if (idx == 0) { + STATUS_INC(FT_CURSOR_SKIP_DELETED_LEAF_ENTRY, n_deleted); if (ftcursor->interrupt_cb && ftcursor->interrupt_cb(ftcursor->interrupt_cb_extra)) { return TOKUDB_INTERRUPTED; } @@ -3406,6 +3409,7 @@ ok: ; r = bn->data_buffer.fetch_klpair(idx, &le, &keylen, &key); assert_zero(r); // we just validated the index if (!le_val_is_del(le, ftcursor->is_snapshot_read, ftcursor->ttxn)) { + STATUS_INC(FT_CURSOR_SKIP_DELETED_LEAF_ENTRY, n_deleted); goto got_a_good_value; } } diff --git a/storage/tokudb/ft-index/locktree/lock_request.cc b/storage/tokudb/ft-index/locktree/lock_request.cc index 97fa780bb04..18f6051afdf 100644 --- a/storage/tokudb/ft-index/locktree/lock_request.cc +++ b/storage/tokudb/ft-index/locktree/lock_request.cc @@ -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 */ diff --git a/storage/tokudb/ft-index/locktree/lock_request.h b/storage/tokudb/ft-index/locktree/lock_request.h index d1a4c2822e0..c504961fcc0 100644 --- a/storage/tokudb/ft-index/locktree/lock_request.h +++ b/storage/tokudb/ft-index/locktree/lock_request.h @@ -164,6 +164,8 @@ public: // 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 @@ private: INITIALIZED, PENDING, COMPLETE, + DESTROYED, }; // The keys for a lock request are stored "unowned" in m_left_key @@ -236,6 +239,9 @@ private: 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); diff --git a/storage/tokudb/ft-index/locktree/locktree.cc b/storage/tokudb/ft-index/locktree/locktree.cc index 27e528db8e8..fc2470e98bd 100644 --- a/storage/tokudb/ft-index/locktree/locktree.cc +++ b/storage/tokudb/ft-index/locktree/locktree.cc @@ -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); diff --git a/storage/tokudb/ft-index/locktree/tests/lock_request_start_retry_race.cc b/storage/tokudb/ft-index/locktree/tests/lock_request_start_retry_race.cc new file mode 100644 index 00000000000..86ef2dd9cc5 --- /dev/null +++ b/storage/tokudb/ft-index/locktree/tests/lock_request_start_retry_race.cc @@ -0,0 +1,193 @@ +/* -*- 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 <iostream> +#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) + std::cout << toku_pthread_self() << " " << i << std::endl; + } +} + +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(<, 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; +} + diff --git a/storage/tokudb/ft-index/src/ydb_row_lock.cc b/storage/tokudb/ft-index/src/ydb_row_lock.cc index 5ca853d92d9..4dd527220ec 100644 --- a/storage/tokudb/ft-index/src/ydb_row_lock.cc +++ b/storage/tokudb/ft-index/src/ydb_row_lock.cc @@ -307,6 +307,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) { diff --git a/storage/tokudb/ft-index/src/ydb_txn.cc b/storage/tokudb/ft-index/src/ydb_txn.cc index dd428c4d502..ce06e78b23f 100644 --- a/storage/tokudb/ft-index/src/ydb_txn.cc +++ b/storage/tokudb/ft-index/src/ydb_txn.cc @@ -422,6 +422,11 @@ static int toku_txn_discard(DB_TXN *txn, uint32_t flags) { return 0; } +static bool toku_txn_is_prepared(DB_TXN *txn) { + TOKUTXN ttxn = db_txn_struct_i(txn)->tokutxn; + return toku_txn_get_state(ttxn) == TOKUTXN_PREPARING; +} + static inline void txn_func_init(DB_TXN *txn) { #define STXN(name) txn->name = locked_txn_ ## name STXN(abort); @@ -438,6 +443,7 @@ static inline void txn_func_init(DB_TXN *txn) { SUTXN(discard); #undef SUTXN txn->id64 = toku_txn_id64; + txn->is_prepared = toku_txn_is_prepared; } // |