2024-05-19 22:07:58 +03:00
|
|
|
|
/// \author Леонид Юрьев aka Leonid Yuriev <leo@yuriev.ru> \date 2015-2024
|
|
|
|
|
/// \copyright SPDX-License-Identifier: Apache-2.0
|
2019-10-09 23:38:44 +03:00
|
|
|
|
|
2022-11-08 16:17:14 +03:00
|
|
|
|
#include "test.h++"
|
2019-10-09 23:38:44 +03:00
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2020-05-24 19:52:50 +03:00
|
|
|
|
/* LY: тест "эмуляцией time-to-live" с вложенными транзакциями:
|
|
|
|
|
* - организуется "скользящее окно", которое каждую транзакцию сдвигается
|
|
|
|
|
* вперед вдоль числовой оси.
|
|
|
|
|
* - по переднему краю "скользящего окна" записи добавляются в таблицу,
|
|
|
|
|
* а по заднему удаляются.
|
|
|
|
|
* - количество добавляемых/удаляемых записей псевдослучайно зависит
|
|
|
|
|
* от номера транзакции, но с экспоненциальным распределением.
|
|
|
|
|
* - размер "скользящего окна" также псевдослучайно зависит от номера
|
|
|
|
|
* транзакции с "отрицательным" экспоненциальным распределением
|
|
|
|
|
* MAX_WIDTH - exp(rnd(N)), при уменьшении окна сдвигается задний
|
|
|
|
|
* край и удаляются записи позади него.
|
|
|
|
|
* - групповое добавление данных в начало окна и групповое удаление в конце,
|
|
|
|
|
* преимущественно выполняются во вложенных транзакциях.
|
|
|
|
|
* - меньшая часть запускаемых вложенных транзакций отменяется, с последующим
|
|
|
|
|
* продолжением итераций с состояния предыдущиего коммита.
|
|
|
|
|
*
|
|
|
|
|
* Таким образом имитируется поведение таблицы с TTL: записи стохастически
|
|
|
|
|
* добавляются и удаляются, и изредка происходят массивные удаления. */
|
|
|
|
|
|
2021-03-15 20:52:18 +03:00
|
|
|
|
class testcase_nested : public testcase_ttl {
|
|
|
|
|
using inherited = testcase_ttl;
|
|
|
|
|
using FIFO = std::deque<std::pair<uint64_t, unsigned>>;
|
|
|
|
|
|
|
|
|
|
uint64_t serial{0};
|
|
|
|
|
unsigned clear_wholetable_passed{0};
|
|
|
|
|
unsigned clear_stepbystep_passed{0};
|
|
|
|
|
unsigned dbfull_passed{0};
|
|
|
|
|
bool keyspace_overflow{false};
|
|
|
|
|
FIFO fifo;
|
|
|
|
|
std::stack<std::tuple<scoped_txn_guard, uint64_t, FIFO, SET>> stack;
|
|
|
|
|
|
|
|
|
|
bool trim_tail(unsigned window_width);
|
|
|
|
|
bool grow_head(unsigned head_count);
|
|
|
|
|
bool pop_txn(bool abort);
|
|
|
|
|
bool pop_txn() {
|
|
|
|
|
return pop_txn(inherited::is_nested_txn_available() ? flipcoin_x3()
|
|
|
|
|
: flipcoin_x2());
|
|
|
|
|
}
|
|
|
|
|
void push_txn();
|
|
|
|
|
bool stochastic_breakable_restart_with_nested(bool force_restart = false);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
testcase_nested(const actor_config &config, const mdbx_pid_t pid)
|
|
|
|
|
: inherited(config, pid) {}
|
|
|
|
|
bool setup() override;
|
|
|
|
|
bool run() override;
|
|
|
|
|
bool teardown() override;
|
|
|
|
|
};
|
|
|
|
|
REGISTER_TESTCASE(nested);
|
|
|
|
|
|
2019-10-09 23:38:44 +03:00
|
|
|
|
bool testcase_nested::setup() {
|
|
|
|
|
if (!inherited::setup())
|
|
|
|
|
return false;
|
|
|
|
|
int err = db_open__begin__table_create_open_clean(dbi);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
|
|
|
|
log_notice("nested: bailout-prepare due '%s'", mdbx_strerror(err));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 17:38:23 +03:00
|
|
|
|
keyvalue_maker.setup(config.params, 0 /* thread_number */);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
key = keygen::alloc(config.params.keylen_max);
|
|
|
|
|
data = keygen::alloc(config.params.datalen_max);
|
|
|
|
|
serial = 0;
|
|
|
|
|
fifo.clear();
|
|
|
|
|
speculum.clear();
|
|
|
|
|
assert(stack.empty());
|
|
|
|
|
stack.emplace(nullptr, serial, fifo, speculum);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool testcase_nested::teardown() {
|
|
|
|
|
while (!stack.empty())
|
|
|
|
|
pop_txn(true);
|
|
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
if (dbi) {
|
|
|
|
|
if (config.params.drop_table && !mode_readonly()) {
|
|
|
|
|
txn_begin(false);
|
|
|
|
|
db_table_drop(dbi);
|
|
|
|
|
int err = breakable_commit();
|
2022-01-21 01:29:59 +03:00
|
|
|
|
if (unlikely(err != MDBX_SUCCESS) &&
|
|
|
|
|
(err != MDBX_MAP_FULL || !config.params.ignore_dbfull)) {
|
2019-10-09 23:38:44 +03:00
|
|
|
|
log_notice("nested: bailout-clean due '%s'", mdbx_strerror(err));
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
2020-10-21 00:36:36 +03:00
|
|
|
|
} else {
|
|
|
|
|
if (txn_guard)
|
|
|
|
|
txn_end(false);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
db_table_close(dbi);
|
2020-10-21 00:36:36 +03:00
|
|
|
|
}
|
2019-10-09 23:38:44 +03:00
|
|
|
|
dbi = 0;
|
|
|
|
|
}
|
|
|
|
|
return inherited::teardown() && ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testcase_nested::push_txn() {
|
2021-07-03 13:01:41 +00:00
|
|
|
|
MDBX_txn *nested_txn;
|
2020-11-17 15:00:51 +03:00
|
|
|
|
MDBX_txn_flags_t flags = MDBX_txn_flags_t(
|
|
|
|
|
prng32() & uint32_t(MDBX_TXN_NOSYNC | MDBX_TXN_NOMETASYNC));
|
2021-07-03 13:01:41 +00:00
|
|
|
|
int err = mdbx_txn_begin(db_guard.get(), txn_guard.get(), flags, &nested_txn);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
|
failure_perror("mdbx_txn_begin(nested)", err);
|
2021-07-03 13:01:41 +00:00
|
|
|
|
/* CLANG/LLVM C++ library could stupidly copy std::set<> item-by-item,
|
|
|
|
|
* i.e. with insertion(s) & comparison(s), which will cause null dereference
|
|
|
|
|
* during call mdbx_cmp() with zero txn. So it is the workaround for this:
|
|
|
|
|
* - explicitly set txn_guard with the new nested txn;
|
|
|
|
|
* - explicitly copy the `speculum` (an instance of std::set<>). */
|
|
|
|
|
scoped_txn_guard nested_txn_guard(nested_txn);
|
|
|
|
|
txn_guard.swap(nested_txn_guard);
|
|
|
|
|
SET speculum_snapshot(speculum);
|
|
|
|
|
stack.emplace(std::move(nested_txn_guard), serial, fifo,
|
|
|
|
|
std::move(speculum_snapshot));
|
2019-11-22 18:20:01 +03:00
|
|
|
|
log_verbose("begin level#%zu txn #%" PRIu64 ", flags 0x%x, serial %" PRIu64,
|
2021-07-03 13:01:41 +00:00
|
|
|
|
stack.size(), mdbx_txn_id(nested_txn), flags, serial);
|
2021-10-04 19:25:37 +03:00
|
|
|
|
if (!dbi && stack.size() == 1)
|
|
|
|
|
dbi = db_table_open(true);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool testcase_nested::pop_txn(bool abort) {
|
|
|
|
|
assert(txn_guard && !stack.empty());
|
|
|
|
|
bool should_continue = true;
|
|
|
|
|
MDBX_txn *txn = txn_guard.release();
|
2020-10-20 15:42:50 +03:00
|
|
|
|
bool committed = false;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
if (abort) {
|
2019-11-22 18:20:01 +03:00
|
|
|
|
log_verbose(
|
|
|
|
|
"abort level#%zu txn #%" PRIu64 ", undo serial %" PRIu64 " <- %" PRIu64,
|
|
|
|
|
stack.size(), mdbx_txn_id(txn), serial, std::get<1>(stack.top()));
|
2021-10-04 19:25:37 +03:00
|
|
|
|
if (dbi > 0 && stack.size() == 1 &&
|
|
|
|
|
is_handle_created_in_current_txn(dbi, txn))
|
|
|
|
|
dbi = 0;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
int err = mdbx_txn_abort(txn);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
|
failure_perror("mdbx_txn_abort()", err);
|
|
|
|
|
} else {
|
|
|
|
|
log_verbose("commit level#%zu txn, nested serial %" PRIu64 " -> %" PRIu64,
|
|
|
|
|
stack.size(), serial, std::get<1>(stack.top()));
|
|
|
|
|
int err = mdbx_txn_commit(txn);
|
|
|
|
|
if (likely(err == MDBX_SUCCESS))
|
2020-10-20 15:42:50 +03:00
|
|
|
|
committed = true;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
else {
|
|
|
|
|
should_continue = false;
|
|
|
|
|
if (err == MDBX_MAP_FULL && config.params.ignore_dbfull) {
|
|
|
|
|
err = mdbx_txn_abort(txn);
|
2019-10-10 15:24:39 +03:00
|
|
|
|
if (unlikely(err != MDBX_SUCCESS && err != MDBX_THREAD_MISMATCH &&
|
|
|
|
|
err != MDBX_BAD_TXN))
|
2019-10-09 23:38:44 +03:00
|
|
|
|
failure_perror("mdbx_txn_abort()", err);
|
|
|
|
|
} else
|
|
|
|
|
failure_perror("mdbx_txn_commit()", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::swap(txn_guard, std::get<0>(stack.top()));
|
2020-10-20 15:42:50 +03:00
|
|
|
|
if (!committed) {
|
2019-10-09 23:38:44 +03:00
|
|
|
|
serial = std::get<1>(stack.top());
|
|
|
|
|
std::swap(fifo, std::get<2>(stack.top()));
|
|
|
|
|
std::swap(speculum, std::get<3>(stack.top()));
|
|
|
|
|
}
|
|
|
|
|
stack.pop();
|
|
|
|
|
return should_continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool testcase_nested::stochastic_breakable_restart_with_nested(
|
|
|
|
|
bool force_restart) {
|
|
|
|
|
log_trace(">> stochastic_breakable_restart_with_nested%s",
|
|
|
|
|
force_restart ? ": force_restart" : "");
|
|
|
|
|
|
|
|
|
|
if (force_restart)
|
|
|
|
|
while (txn_guard)
|
|
|
|
|
pop_txn(true);
|
|
|
|
|
|
|
|
|
|
bool should_continue = true;
|
|
|
|
|
while (!stack.empty() &&
|
|
|
|
|
(flipcoin() || txn_underutilization_x256(txn_guard.get()) < 42))
|
|
|
|
|
should_continue &= pop_txn();
|
|
|
|
|
|
2021-12-03 17:04:10 +03:00
|
|
|
|
if (flipcoin_x3()) {
|
|
|
|
|
unsigned period;
|
|
|
|
|
int err = mdbx_env_get_syncperiod(db_guard.get(), &period);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
|
failure_perror("mdbx_env_get_syncperiod()", err);
|
|
|
|
|
|
|
|
|
|
size_t bytes;
|
|
|
|
|
err = mdbx_env_get_syncbytes(db_guard.get(), &bytes);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
|
failure_perror("mdbx_env_get_syncbytes()", err);
|
|
|
|
|
|
|
|
|
|
err = mdbx_env_set_syncperiod(db_guard.get(), period ^ 42);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS) && err != MDBX_BUSY)
|
|
|
|
|
failure_perror("mdbx_env_set_syncperiod()", err);
|
|
|
|
|
|
|
|
|
|
err = mdbx_env_set_syncbytes(db_guard.get(), bytes ^ 42000);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS) && err != MDBX_BUSY)
|
|
|
|
|
failure_perror("mdbx_env_set_syncbytes()", err);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 23:38:44 +03:00
|
|
|
|
if (should_continue)
|
|
|
|
|
while (stack.empty() ||
|
|
|
|
|
(is_nested_txn_available() && flipcoin() && stack.size() < 5))
|
|
|
|
|
push_txn();
|
|
|
|
|
|
|
|
|
|
log_trace("<< stochastic_breakable_restart_with_nested: should_continue=%s",
|
|
|
|
|
should_continue ? "yes" : "no");
|
|
|
|
|
return should_continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool testcase_nested::trim_tail(unsigned window_width) {
|
2020-05-23 01:29:58 +03:00
|
|
|
|
if (window_width || flipcoin()) {
|
|
|
|
|
clear_stepbystep_passed += window_width == 0;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
while (fifo.size() > window_width) {
|
|
|
|
|
uint64_t tail_serial = fifo.back().first;
|
|
|
|
|
const unsigned tail_count = fifo.back().second;
|
2021-08-27 15:02:15 +03:00
|
|
|
|
log_verbose("nested: trim-tail (serial %" PRIu64 ", count %u)",
|
2019-11-22 18:20:01 +03:00
|
|
|
|
tail_serial, tail_count);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
fifo.pop_back();
|
|
|
|
|
for (unsigned n = 0; n < tail_count; ++n) {
|
|
|
|
|
log_trace("nested: remove-tail %" PRIu64, tail_serial);
|
|
|
|
|
generate_pair(tail_serial);
|
|
|
|
|
int err = remove(key, data);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
|
|
|
|
if (err == MDBX_MAP_FULL && config.params.ignore_dbfull) {
|
|
|
|
|
log_notice("nested: tail-bailout due '%s'", mdbx_strerror(err));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
failure_perror("mdbx_del(tail)", err);
|
|
|
|
|
}
|
|
|
|
|
if (unlikely(!keyvalue_maker.increment(tail_serial, 1)))
|
|
|
|
|
failure("nested: unexpected key-space overflow on the tail");
|
|
|
|
|
}
|
2020-05-24 19:52:50 +03:00
|
|
|
|
report(tail_count);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
}
|
2019-11-22 18:20:01 +03:00
|
|
|
|
} else if (!fifo.empty()) {
|
|
|
|
|
log_verbose("nested: purge state %" PRIu64 " - %" PRIu64 ", fifo-items %zu",
|
|
|
|
|
fifo.front().first, fifo.back().first + fifo.back().second,
|
|
|
|
|
fifo.size());
|
2019-10-09 23:38:44 +03:00
|
|
|
|
db_table_clear(dbi, txn_guard.get());
|
|
|
|
|
fifo.clear();
|
2020-05-23 01:29:58 +03:00
|
|
|
|
clear_wholetable_passed += 1;
|
2020-05-19 14:18:26 +03:00
|
|
|
|
report(1);
|
2019-10-09 23:38:44 +03:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool testcase_nested::grow_head(unsigned head_count) {
|
2020-08-04 01:06:01 +03:00
|
|
|
|
const MDBX_put_flags_t insert_flags =
|
|
|
|
|
(config.params.table_flags & MDBX_DUPSORT)
|
|
|
|
|
? MDBX_NODUPDATA
|
|
|
|
|
: MDBX_NODUPDATA | MDBX_NOOVERWRITE;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
retry:
|
|
|
|
|
fifo.push_front(std::make_pair(serial, head_count));
|
|
|
|
|
for (unsigned n = 0; n < head_count; ++n) {
|
|
|
|
|
log_trace("nested: insert-head %" PRIu64, serial);
|
|
|
|
|
generate_pair(serial);
|
|
|
|
|
int err = insert(key, data, insert_flags);
|
|
|
|
|
if (unlikely(err != MDBX_SUCCESS)) {
|
|
|
|
|
if (err == MDBX_MAP_FULL && config.params.ignore_dbfull) {
|
|
|
|
|
log_notice("nested: head-insert skip due '%s'", mdbx_strerror(err));
|
|
|
|
|
head_count = n;
|
|
|
|
|
stochastic_breakable_restart_with_nested(true);
|
2020-05-23 01:29:58 +03:00
|
|
|
|
dbfull_passed += 1;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
goto retry;
|
|
|
|
|
}
|
|
|
|
|
failure_perror("mdbx_put(head)", err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unlikely(!keyvalue_maker.increment(serial, 1))) {
|
|
|
|
|
log_notice("nested: unexpected key-space overflow");
|
2020-05-24 13:43:56 +03:00
|
|
|
|
keyspace_overflow = true;
|
|
|
|
|
head_count = n;
|
|
|
|
|
stochastic_breakable_restart_with_nested(true);
|
|
|
|
|
goto retry;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool testcase_nested::run() {
|
2024-03-03 17:38:23 +03:00
|
|
|
|
uint64_t seed = prng64_map2_white(prng_state) + config.space_id;
|
2019-10-09 23:38:44 +03:00
|
|
|
|
|
2020-05-23 01:29:58 +03:00
|
|
|
|
clear_wholetable_passed = 0;
|
|
|
|
|
clear_stepbystep_passed = 0;
|
|
|
|
|
dbfull_passed = 0;
|
|
|
|
|
unsigned loops = 0;
|
|
|
|
|
while (true) {
|
2019-10-09 23:38:44 +03:00
|
|
|
|
const uint64_t salt = prng64_white(seed) /* mdbx_txn_id(txn_guard.get()) */;
|
2020-05-24 19:52:50 +03:00
|
|
|
|
const unsigned window_width =
|
|
|
|
|
(!should_continue() || flipcoin_x4()) ? 0 : edge2window(salt);
|
|
|
|
|
const unsigned head_count = edge2count(salt);
|
2020-05-25 00:50:06 +03:00
|
|
|
|
log_debug("nested: step #%" PRIu64 " (serial %" PRIu64
|
2019-10-09 23:38:44 +03:00
|
|
|
|
", window %u, count %u) salt %" PRIu64,
|
|
|
|
|
nops_completed, serial, window_width, head_count, salt);
|
|
|
|
|
|
|
|
|
|
if (!trim_tail(window_width))
|
|
|
|
|
return false;
|
|
|
|
|
if (!stochastic_breakable_restart_with_nested()) {
|
|
|
|
|
log_notice("nested: bailout at commit/restart after tail-trim");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!speculum_verify()) {
|
|
|
|
|
log_notice("nested: bailout after tail-trim");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 13:43:56 +03:00
|
|
|
|
if (!keyspace_overflow && (should_continue() || !clear_wholetable_passed ||
|
|
|
|
|
!clear_stepbystep_passed)) {
|
2020-05-23 01:29:58 +03:00
|
|
|
|
unsigned underutilization_x256 =
|
|
|
|
|
txn_underutilization_x256(txn_guard.get());
|
|
|
|
|
if (dbfull_passed > underutilization_x256) {
|
|
|
|
|
log_notice("nested: skip head-grow to avoid one more dbfull (was %u, "
|
|
|
|
|
"underutilization %.2f%%)",
|
|
|
|
|
dbfull_passed, underutilization_x256 / 2.560);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!grow_head(head_count))
|
|
|
|
|
return false;
|
|
|
|
|
if (!stochastic_breakable_restart_with_nested())
|
|
|
|
|
log_notice("nested: skip commit/restart after head-grow");
|
|
|
|
|
if (!speculum_verify()) {
|
|
|
|
|
log_notice("nested: bailout after head-grow");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
loops += 1;
|
|
|
|
|
} else if (fifo.empty()) {
|
2020-05-24 19:52:50 +03:00
|
|
|
|
log_notice("nested: done %u whole loops, %" PRIu64 " ops, %" PRIu64
|
|
|
|
|
" items",
|
|
|
|
|
loops, nops_completed, serial);
|
2020-05-23 01:29:58 +03:00
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
log_notice("nested: done, wait for empty, skip head-grow");
|
2019-10-09 23:38:44 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!stack.empty())
|
|
|
|
|
pop_txn(false);
|
|
|
|
|
|
2019-10-10 08:59:19 +03:00
|
|
|
|
return speculum_verify();
|
2019-10-09 23:38:44 +03:00
|
|
|
|
}
|