Commit 054633c6 authored by Rich Prohaska's avatar Rich Prohaska

#173 allow lock tree waits to be killable

parent d93fe66d
...@@ -451,7 +451,7 @@ static void print_db_env_struct (void) { ...@@ -451,7 +451,7 @@ static void print_db_env_struct (void) {
"int (*set_lk_max_memory) (DB_ENV *env, uint64_t max)", "int (*set_lk_max_memory) (DB_ENV *env, uint64_t max)",
"int (*get_lk_max_memory) (DB_ENV *env, uint64_t *max)", "int (*get_lk_max_memory) (DB_ENV *env, uint64_t *max)",
"void (*set_update) (DB_ENV *env, int (*update_function)(DB *, const DBT *key, const DBT *old_val, const DBT *extra, void (*set_val)(const DBT *new_val, void *set_extra), void *set_extra))", "void (*set_update) (DB_ENV *env, int (*update_function)(DB *, const DBT *key, const DBT *old_val, const DBT *extra, void (*set_val)(const DBT *new_val, void *set_extra), void *set_extra))",
"int (*set_lock_timeout) (DB_ENV *env, uint64_t lock_wait_time_msec, uint64_t (*get_lock_wait_time_cb)(uint64_t default_lock_wait_time))", "int (*set_lock_timeout) (DB_ENV *env, uint64_t default_lock_wait_time_msec, uint64_t (*get_lock_wait_time_cb)(uint64_t default_lock_wait_time))",
"int (*get_lock_timeout) (DB_ENV *env, uint64_t *lock_wait_time_msec)", "int (*get_lock_timeout) (DB_ENV *env, uint64_t *lock_wait_time_msec)",
"int (*set_lock_timeout_callback) (DB_ENV *env, lock_timeout_callback callback)", "int (*set_lock_timeout_callback) (DB_ENV *env, lock_timeout_callback callback)",
"int (*txn_xa_recover) (DB_ENV*, TOKU_XA_XID list[/*count*/], long count, /*out*/ long *retp, uint32_t flags)", "int (*txn_xa_recover) (DB_ENV*, TOKU_XA_XID list[/*count*/], long count, /*out*/ long *retp, uint32_t flags)",
...@@ -463,6 +463,7 @@ static void print_db_env_struct (void) { ...@@ -463,6 +463,7 @@ static void print_db_env_struct (void) {
"int (*iterate_pending_lock_requests) (DB_ENV *env, iterate_requests_callback callback, void *extra)", "int (*iterate_pending_lock_requests) (DB_ENV *env, iterate_requests_callback callback, void *extra)",
"void (*set_loader_memory_size)(DB_ENV *env, uint64_t (*get_loader_memory_size_callback)(void))", "void (*set_loader_memory_size)(DB_ENV *env, uint64_t (*get_loader_memory_size_callback)(void))",
"uint64_t (*get_loader_memory_size)(DB_ENV *env)", "uint64_t (*get_loader_memory_size)(DB_ENV *env)",
"void (*set_killed_callback)(DB_ENV *env, uint64_t default_killed_time_msec, uint64_t (*get_killed_time_callback)(uint64_t default_killed_time_msec), int (*killed_callback)(void))",
NULL}; NULL};
sort_and_dump_fields("db_env", true, extra); sort_and_dump_fields("db_env", true, extra);
......
...@@ -234,38 +234,50 @@ int lock_request::start(void) { ...@@ -234,38 +234,50 @@ int lock_request::start(void) {
return m_state == state::COMPLETE ? m_complete_r : r; return m_state == state::COMPLETE ? m_complete_r : r;
} }
void lock_request::calculate_cond_wakeup_time(struct timespec *ts, uint64_t wait_time) { // sleep on the lock request until it becomes resolved or the wait time has elapsed.
struct timeval now; int lock_request::wait(uint64_t wait_time_ms) {
int r = gettimeofday(&now, NULL); return wait(wait_time_ms, 0, nullptr);
invariant_zero(r);
int64_t sec = now.tv_sec + (wait_time / 1000);
int64_t usec = now.tv_usec + ((wait_time % 1000) * 1000);
int64_t d_sec = usec / 1000000;
int64_t d_usec = usec % 1000000;
ts->tv_sec = sec + d_sec;
ts->tv_nsec = d_usec * 1000;
} }
// sleep on the lock request until it becomes resolved or the wait time has elapsed. int lock_request::wait(uint64_t wait_time_ms, uint64_t killed_time_ms, int (*killed_callback)(void)) {
int lock_request::wait(uint64_t wait_time) { uint64_t t_now = toku_current_time_microsec();
uint64_t t_start = toku_current_time_microsec(); uint64_t t_start = t_now;
uint64_t t_end = t_start + wait_time_ms * 1000;
toku_mutex_lock(&m_info->mutex); toku_mutex_lock(&m_info->mutex);
while (m_state == state::PENDING) { while (m_state == state::PENDING) {
struct timespec ts;
calculate_cond_wakeup_time(&ts, wait_time); // compute next wait time
uint64_t t_wait;
if (killed_time_ms == 0) {
t_wait = t_end;
} else {
t_wait = t_now + killed_time_ms * 1000;
if (t_wait > t_end)
t_wait = t_end;
}
struct timespec ts = {};
ts.tv_sec = t_wait / 1000000;
ts.tv_nsec = (t_wait % 1000000) * 1000;
int r = toku_cond_timedwait(&m_wait_cond, &m_info->mutex, &ts); int r = toku_cond_timedwait(&m_wait_cond, &m_info->mutex, &ts);
invariant(r == 0 || r == ETIMEDOUT); invariant(r == 0 || r == ETIMEDOUT);
if (r == ETIMEDOUT && m_state == state::PENDING) {
t_now = toku_current_time_microsec();
if (m_state == state::PENDING && (t_now >= t_end || (killed_callback && killed_callback()))) {
m_info->counters.timeout_count += 1; m_info->counters.timeout_count += 1;
// if we're still pending and we timed out, then remove our // if we're still pending and we timed out, then remove our
// request from the set of lock requests and fail. // request from the set of lock requests and fail.
remove_from_lock_requests(); remove_from_lock_requests();
// complete sets m_state to COMPLETE, breaking us out of the loop // complete sets m_state to COMPLETE, breaking us out of the loop
complete(DB_LOCK_NOTGRANTED); complete(DB_LOCK_NOTGRANTED);
} }
} }
uint64_t t_end = toku_current_time_microsec();
uint64_t duration = t_end - t_start; uint64_t t_real_end = toku_current_time_microsec();
uint64_t duration = t_real_end - t_start;
m_info->counters.wait_count += 1; m_info->counters.wait_count += 1;
m_info->counters.wait_time += duration; m_info->counters.wait_time += duration;
if (duration >= 1000000) { if (duration >= 1000000) {
......
...@@ -143,7 +143,8 @@ public: ...@@ -143,7 +143,8 @@ public:
// effect: Sleeps until either the request is granted or the wait time expires. // effect: Sleeps until either the request is granted or the wait time expires.
// returns: The return code of locktree::acquire_[write,read]_lock() // returns: The return code of locktree::acquire_[write,read]_lock()
// or simply DB_LOCK_NOTGRANTED if the wait time expired. // or simply DB_LOCK_NOTGRANTED if the wait time expired.
int wait(uint64_t wait_time); int wait(uint64_t wait_time_ms);
int wait(uint64_t wait_time_ms, uint64_t killed_time_ms, int (*killed_callback)(void));
// return: left end-point of the lock range // return: left end-point of the lock range
const DBT *get_left_key(void) const; const DBT *get_left_key(void) const;
...@@ -235,8 +236,6 @@ private: ...@@ -235,8 +236,6 @@ private:
void copy_keys(void); void copy_keys(void);
void calculate_cond_wakeup_time(struct timespec *ts, uint64_t wait_time);
static int find_by_txnid(lock_request * const &request, const TXNID &txnid); static int find_by_txnid(lock_request * const &request, const TXNID &txnid);
friend class lock_request_unit_test; friend class lock_request_unit_test;
......
...@@ -258,10 +258,6 @@ public: ...@@ -258,10 +258,6 @@ public:
int set_max_lock_memory(size_t max_lock_memory); int set_max_lock_memory(size_t max_lock_memory);
uint64_t get_lock_wait_time(void);
void set_lock_wait_time(uint64_t lock_wait_time, uint64_t (*get_lock_wait_time_cb)(uint64_t default_lock_wait_time));
// effect: Get a locktree from the manager. If a locktree exists with the given // effect: Get a locktree from the manager. If a locktree exists with the given
// dict_id, it is referenced and then returned. If one did not exist, it // dict_id, it is referenced and then returned. If one did not exist, it
// is created. It will use the given descriptor and comparison function // is created. It will use the given descriptor and comparison function
...@@ -353,7 +349,6 @@ public: ...@@ -353,7 +349,6 @@ public:
private: private:
static const uint64_t DEFAULT_MAX_LOCK_MEMORY = 64L * 1024 * 1024; static const uint64_t DEFAULT_MAX_LOCK_MEMORY = 64L * 1024 * 1024;
static const uint64_t DEFAULT_LOCK_WAIT_TIME = 0;
// tracks the current number of locks and lock memory // tracks the current number of locks and lock memory
uint64_t m_max_lock_memory; uint64_t m_max_lock_memory;
...@@ -362,10 +357,6 @@ public: ...@@ -362,10 +357,6 @@ public:
struct lt_counters m_lt_counters; struct lt_counters m_lt_counters;
// lock wait time for blocking row locks, in ms
uint64_t m_lock_wait_time_ms;
uint64_t (*m_get_lock_wait_time_cb)(uint64_t);
// the create and destroy callbacks for the locktrees // the create and destroy callbacks for the locktrees
lt_create_cb m_lt_create_callback; lt_create_cb m_lt_create_callback;
lt_destroy_cb m_lt_destroy_callback; lt_destroy_cb m_lt_destroy_callback;
......
...@@ -103,7 +103,6 @@ namespace toku { ...@@ -103,7 +103,6 @@ namespace toku {
void locktree::manager::create(lt_create_cb create_cb, lt_destroy_cb destroy_cb, lt_escalate_cb escalate_cb, void *escalate_extra) { void locktree::manager::create(lt_create_cb create_cb, lt_destroy_cb destroy_cb, lt_escalate_cb escalate_cb, void *escalate_extra) {
m_max_lock_memory = DEFAULT_MAX_LOCK_MEMORY; m_max_lock_memory = DEFAULT_MAX_LOCK_MEMORY;
m_current_lock_memory = 0; m_current_lock_memory = 0;
m_lock_wait_time_ms = DEFAULT_LOCK_WAIT_TIME;
m_mem_tracker.set_manager(this); m_mem_tracker.set_manager(this);
m_locktree_map.create(); m_locktree_map.create();
...@@ -153,19 +152,6 @@ int locktree::manager::set_max_lock_memory(size_t max_lock_memory) { ...@@ -153,19 +152,6 @@ int locktree::manager::set_max_lock_memory(size_t max_lock_memory) {
return r; return r;
} }
uint64_t locktree::manager::get_lock_wait_time(void) {
uint64_t wait_time = m_lock_wait_time_ms;
if (m_get_lock_wait_time_cb) {
wait_time = m_get_lock_wait_time_cb(wait_time);
}
return wait_time;
}
void locktree::manager::set_lock_wait_time(uint64_t lock_wait_time_ms, uint64_t (*get_lock_wait_time_cb)(uint64_t)) {
m_lock_wait_time_ms = lock_wait_time_ms;
m_get_lock_wait_time_cb = get_lock_wait_time_cb;
}
int locktree::manager::find_by_dict_id(locktree *const &lt, const DICTIONARY_ID &dict_id) { int locktree::manager::find_by_dict_id(locktree *const &lt, const DICTIONARY_ID &dict_id) {
if (lt->m_dict_id.dictid < dict_id.dictid) { if (lt->m_dict_id.dictid < dict_id.dictid) {
return -1; return -1;
......
/* -*- 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:
TokuDB, 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."
// test the kill callback. the lock wait is killed 1/2 of the way through the wait.
#include "lock_request_unit_test.h"
namespace toku {
const uint64_t my_lock_wait_time = 10 * 1000; // 10 seconds
const uint64_t my_killed_time = 1 * 1000;
static int killed_calls = 0;
static uint64_t t_last_kill;
static uint64_t t_do_kill;
static int my_killed_callback(void) {
uint64_t t_now = toku_current_time_microsec();
assert(t_now >= t_last_kill);
assert(t_now - t_last_kill >= my_killed_time * 1000 / 2); // div by 2 for valgrind which is not very accurate
t_last_kill = t_now;
killed_calls++;
if (t_now >= t_do_kill)
return 1;
else
return 0;
}
// make sure deadlocks are detected when a lock request starts
void lock_request_unit_test::test_wait_time_callback(void) {
int r;
locktree::manager mgr;
locktree *lt;
mgr.create(nullptr, nullptr, nullptr, nullptr);
DICTIONARY_ID dict_id = { 1 };
lt = mgr.get_lt(dict_id, nullptr, compare_dbts, nullptr);
TXNID txnid_a = 1001;
lock_request request_a;
request_a.create();
TXNID txnid_b = 2001;
lock_request request_b;
request_b.create();
const DBT *one = get_dbt(1);
// a locks 'one'
request_a.set(lt, txnid_a, one, one, lock_request::type::WRITE, false);
r = request_a.start();
assert_zero(r);
// b tries to lock 'one'
request_b.set(lt, txnid_b, one, one, lock_request::type::WRITE, false);
r = request_b.start();
assert(r == DB_LOCK_NOTGRANTED);
uint64_t t_start = toku_current_time_microsec();
t_last_kill = t_start;
t_do_kill = t_start + my_lock_wait_time * 1000 / 2;
r = request_b.wait(my_lock_wait_time, my_killed_time, my_killed_callback);
assert(r == DB_LOCK_NOTGRANTED);
uint64_t t_end = toku_current_time_microsec();
assert(t_end > t_start);
uint64_t t_delta = t_end - t_start;
// fprintf(stderr, "delta=%" PRIu64 "\n", t_delta);
assert(t_delta >= my_lock_wait_time / 2);
// fprintf(stderr, "killed_calls=%d\n", killed_calls);
assert(killed_calls > 0);
request_b.destroy();
release_lock_and_retry_requests(lt, txnid_a, one, one);
request_a.destroy();
mgr.release_lt(lt);
mgr.destroy();
}
} /* namespace toku */
int main(void) {
toku::lock_request_unit_test test;
test.test_wait_time_callback();
return 0;
}
/* -*- 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:
TokuDB, 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."
// test the kill callback. the kill callback never kills the lock wait in this test.
// the test verifies that the kill callback is called close to its requested frequency.
#include "lock_request_unit_test.h"
namespace toku {
const uint64_t my_lock_wait_time = 10 * 1000; // 10 seconds
const uint64_t my_killed_time = 1 * 1000;
static int killed_calls = 0;
static uint64_t t_last_kill;
static int my_killed_callback(void) {
uint64_t t_now = toku_current_time_microsec();
assert(t_now >= t_last_kill);
assert(t_now - t_last_kill >= my_killed_time * 1000 / 2); // div by 2 for valgrind which is not very accurate
t_last_kill = t_now;
killed_calls++;
return 0;
}
// make sure deadlocks are detected when a lock request starts
void lock_request_unit_test::test_wait_time_callback(void) {
int r;
locktree::manager mgr;
locktree *lt;
mgr.create(nullptr, nullptr, nullptr, nullptr);
DICTIONARY_ID dict_id = { 1 };
lt = mgr.get_lt(dict_id, nullptr, compare_dbts, nullptr);
TXNID txnid_a = 1001;
lock_request request_a;
request_a.create();
TXNID txnid_b = 2001;
lock_request request_b;
request_b.create();
const DBT *one = get_dbt(1);
// a locks 'one'
request_a.set(lt, txnid_a, one, one, lock_request::type::WRITE, false);
r = request_a.start();
assert_zero(r);
// b tries to lock 'one'
request_b.set(lt, txnid_b, one, one, lock_request::type::WRITE, false);
r = request_b.start();
assert(r == DB_LOCK_NOTGRANTED);
uint64_t t_start = toku_current_time_microsec();
t_last_kill = t_start;
r = request_b.wait(my_lock_wait_time, my_killed_time, my_killed_callback);
assert(r == DB_LOCK_NOTGRANTED);
uint64_t t_end = toku_current_time_microsec();
assert(t_end > t_start);
uint64_t t_delta = t_end - t_start;
// fprintf(stderr, "delta=%" PRIu64 "\n", t_delta);
assert(t_delta >= my_lock_wait_time);
// fprintf(stderr, "killed_calls=%d\n", killed_calls);
assert(killed_calls > 0);
request_b.destroy();
release_lock_and_retry_requests(lt, txnid_a, one, one);
request_a.destroy();
mgr.release_lt(lt);
mgr.destroy();
}
} /* namespace toku */
int main(void) {
toku::lock_request_unit_test test;
test.test_wait_time_callback();
return 0;
}
...@@ -93,12 +93,7 @@ PATENT RIGHTS GRANT: ...@@ -93,12 +93,7 @@ PATENT RIGHTS GRANT:
namespace toku { namespace toku {
static int my_calls = 0; static const uint64_t my_lock_wait_time = 10 * 1000; // 10 sec
static uint64_t my_lock_wait_time_callback(uint64_t default_lock_wait_time UU()) {
my_calls++;
return 1000;
}
// make sure deadlocks are detected when a lock request starts // make sure deadlocks are detected when a lock request starts
void lock_request_unit_test::test_wait_time_callback(void) { void lock_request_unit_test::test_wait_time_callback(void) {
...@@ -107,7 +102,6 @@ void lock_request_unit_test::test_wait_time_callback(void) { ...@@ -107,7 +102,6 @@ void lock_request_unit_test::test_wait_time_callback(void) {
locktree *lt; locktree *lt;
mgr.create(nullptr, nullptr, nullptr, nullptr); mgr.create(nullptr, nullptr, nullptr, nullptr);
mgr.set_lock_wait_time(10*1000, my_lock_wait_time_callback);
DICTIONARY_ID dict_id = { 1 }; DICTIONARY_ID dict_id = { 1 };
lt = mgr.get_lt(dict_id, nullptr, compare_dbts, nullptr); lt = mgr.get_lt(dict_id, nullptr, compare_dbts, nullptr);
...@@ -131,15 +125,13 @@ void lock_request_unit_test::test_wait_time_callback(void) { ...@@ -131,15 +125,13 @@ void lock_request_unit_test::test_wait_time_callback(void) {
request_b.set(lt, txnid_b, one, two, lock_request::type::WRITE, false); request_b.set(lt, txnid_b, one, two, lock_request::type::WRITE, false);
r = request_b.start(); r = request_b.start();
assert(r == DB_LOCK_NOTGRANTED); assert(r == DB_LOCK_NOTGRANTED);
assert(my_calls == 0);
uint64_t t_start = toku_current_time_microsec(); uint64_t t_start = toku_current_time_microsec();
r = request_b.wait(mgr.get_lock_wait_time()); r = request_b.wait(my_lock_wait_time);
uint64_t t_end = toku_current_time_microsec(); uint64_t t_end = toku_current_time_microsec();
assert(r == DB_LOCK_NOTGRANTED); assert(r == DB_LOCK_NOTGRANTED);
assert(my_calls == 1);
assert(t_end > t_start); assert(t_end > t_start);
uint64_t t_delta = t_end - t_start; uint64_t t_delta = t_end - t_start;
assert(1000000 <= t_delta && t_delta < 10000000); assert(t_delta >= my_lock_wait_time);
request_b.destroy(); request_b.destroy();
release_lock_and_retry_requests(lt, txnid_a, one, one); release_lock_and_retry_requests(lt, txnid_a, one, one);
......
...@@ -109,7 +109,6 @@ void manager_unit_test::test_create_destroy(void) { ...@@ -109,7 +109,6 @@ void manager_unit_test::test_create_destroy(void) {
invariant(mgr.m_escalation_count == 0); invariant(mgr.m_escalation_count == 0);
invariant(mgr.m_escalation_time == 0); invariant(mgr.m_escalation_time == 0);
invariant(mgr.m_escalation_latest_result == 0); invariant(mgr.m_escalation_latest_result == 0);
invariant(mgr.m_lock_wait_time_ms == locktree::manager::DEFAULT_LOCK_WAIT_TIME);
invariant(mgr.m_locktree_map.size() == 0); invariant(mgr.m_locktree_map.size() == 0);
invariant(mgr.m_lt_create_callback == create_callback); invariant(mgr.m_lt_create_callback == create_callback);
......
...@@ -103,10 +103,6 @@ void manager_unit_test::test_params(void) { ...@@ -103,10 +103,6 @@ void manager_unit_test::test_params(void) {
invariant(r == 0); invariant(r == 0);
invariant(mgr.get_max_lock_memory() == new_max_lock_memory); invariant(mgr.get_max_lock_memory() == new_max_lock_memory);
uint64_t new_lock_wait_time = 62345234;
mgr.set_lock_wait_time(new_lock_wait_time, nullptr);
invariant(mgr.get_lock_wait_time() == new_lock_wait_time);
mgr.m_current_lock_memory = 100000; mgr.m_current_lock_memory = 100000;
r = mgr.set_max_lock_memory(mgr.m_current_lock_memory - 1); r = mgr.set_max_lock_memory(mgr.m_current_lock_memory - 1);
invariant(r == EDOM); invariant(r == EDOM);
......
...@@ -183,6 +183,11 @@ struct __toku_db_env_internal { ...@@ -183,6 +183,11 @@ struct __toku_db_env_internal {
int logdir_lockfd; int logdir_lockfd;
int tmpdir_lockfd; int tmpdir_lockfd;
uint64_t (*get_loader_memory_size_callback)(void); uint64_t (*get_loader_memory_size_callback)(void);
uint64_t default_lock_timeout_msec;
uint64_t (*get_lock_timeout_callback)(uint64_t default_lock_timeout_msec);
uint64_t default_killed_time_msec;
uint64_t (*get_killed_time_callback)(uint64_t default_killed_time_msec);
int (*killed_callback)(void);
}; };
// test-only environment function for running lock escalation // test-only environment function for running lock escalation
......
...@@ -1723,15 +1723,17 @@ env_set_redzone(DB_ENV *env, int redzone) { ...@@ -1723,15 +1723,17 @@ env_set_redzone(DB_ENV *env, int redzone) {
return r; return r;
} }
static int static int env_get_lock_timeout(DB_ENV *env, uint64_t *lock_timeout_msec) {
env_get_lock_timeout(DB_ENV *env, uint64_t *lock_timeout_msec) { uint64_t t = env->i->default_lock_timeout_msec;
*lock_timeout_msec = env->i->ltm.get_lock_wait_time(); if (env->i->get_lock_timeout_callback)
t = env->i->get_lock_timeout_callback(t);
*lock_timeout_msec = t;
return 0; return 0;
} }
static int static int env_set_lock_timeout(DB_ENV *env, uint64_t default_lock_timeout_msec, uint64_t (*get_lock_timeout_callback)(uint64_t default_lock_timeout_msec)) {
env_set_lock_timeout(DB_ENV *env, uint64_t lock_timeout_msec, uint64_t (*get_lock_timeout_callback)(uint64_t default_lock_timeout_msec)) { env->i->default_lock_timeout_msec = default_lock_timeout_msec;
env->i->ltm.set_lock_wait_time(lock_timeout_msec, get_lock_timeout_callback); env->i->get_lock_timeout_callback = get_lock_timeout_callback;
return 0; return 0;
} }
...@@ -2452,6 +2454,12 @@ static uint64_t env_get_loader_memory_size(DB_ENV *env) { ...@@ -2452,6 +2454,12 @@ static uint64_t env_get_loader_memory_size(DB_ENV *env) {
return memory_size; return memory_size;
} }
static void env_set_killed_callback(DB_ENV *env, uint64_t default_killed_time_msec, uint64_t (*get_killed_time_callback)(uint64_t default_killed_time_msec), int (*killed_callback)(void)) {
env->i->default_killed_time_msec = default_killed_time_msec;
env->i->get_killed_time_callback = get_killed_time_callback;
env->i->killed_callback = killed_callback;
}
static int static int
toku_env_create(DB_ENV ** envp, uint32_t flags) { toku_env_create(DB_ENV ** envp, uint32_t flags) {
int r = ENOSYS; int r = ENOSYS;
...@@ -2527,6 +2535,7 @@ toku_env_create(DB_ENV ** envp, uint32_t flags) { ...@@ -2527,6 +2535,7 @@ toku_env_create(DB_ENV ** envp, uint32_t flags) {
USENV(change_fsync_log_period); USENV(change_fsync_log_period);
USENV(set_loader_memory_size); USENV(set_loader_memory_size);
USENV(get_loader_memory_size); USENV(get_loader_memory_size);
USENV(set_killed_callback);
#undef USENV #undef USENV
// unlocked methods // unlocked methods
......
...@@ -268,9 +268,14 @@ int toku_db_wait_range_lock(DB *db, DB_TXN *txn, toku::lock_request *request) { ...@@ -268,9 +268,14 @@ int toku_db_wait_range_lock(DB *db, DB_TXN *txn, toku::lock_request *request) {
DB_TXN *txn_anc = txn_oldest_ancester(txn); DB_TXN *txn_anc = txn_oldest_ancester(txn);
const DBT *left_key = request->get_left_key(); const DBT *left_key = request->get_left_key();
const DBT *right_key = request->get_right_key(); const DBT *right_key = request->get_right_key();
DB_ENV *env = db->dbenv;
uint64_t wait_time = txn->mgrp->i->ltm.get_lock_wait_time(); uint64_t wait_time_msec = env->i->default_lock_timeout_msec;
const int r = request->wait(wait_time); if (env->i->get_lock_timeout_callback)
wait_time_msec = env->i->get_lock_timeout_callback(wait_time_msec);
uint64_t killed_time_msec = env->i->default_killed_time_msec;
if (env->i->get_killed_time_callback)
killed_time_msec = env->i->get_killed_time_callback(killed_time_msec);
const int r = request->wait(wait_time_msec, killed_time_msec, env->i->killed_callback);
if (r == 0) { if (r == 0) {
db_txn_note_row_lock(db, txn_anc, left_key, right_key); db_txn_note_row_lock(db, txn_anc, left_key, right_key);
} else if (r == DB_LOCK_NOTGRANTED) { } else if (r == DB_LOCK_NOTGRANTED) {
......
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