2024-05-20 03:07:58 +08:00
|
|
|
/// \author Леонид Юрьев aka Leonid Yuriev <leo@yuriev.ru> \date 2015-2024
|
|
|
|
/// \copyright SPDX-License-Identifier: Apache-2.0
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2022-11-08 21:17:14 +08:00
|
|
|
#include "test.h++"
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2021-03-16 01:52:18 +08:00
|
|
|
class testcase_jitter : public testcase {
|
|
|
|
protected:
|
|
|
|
void check_dbi_error(int expect, const char *stage);
|
|
|
|
|
|
|
|
public:
|
|
|
|
testcase_jitter(const actor_config &config, const mdbx_pid_t pid)
|
|
|
|
: testcase(config, pid) {}
|
|
|
|
bool run() override;
|
|
|
|
};
|
|
|
|
REGISTER_TESTCASE(jitter);
|
|
|
|
|
2020-12-17 20:42:23 +08:00
|
|
|
void testcase_jitter::check_dbi_error(int expect, const char *stage) {
|
|
|
|
MDBX_stat stat;
|
|
|
|
int err = mdbx_dbi_stat(txn_guard.get(), dbi, &stat, sizeof(stat));
|
|
|
|
if (err != expect)
|
|
|
|
failure("unexpected result for %s dbi-handle: expect %d, got %d", stage,
|
|
|
|
expect, err);
|
|
|
|
}
|
|
|
|
|
2017-04-21 23:41:11 +08:00
|
|
|
bool testcase_jitter::run() {
|
2020-01-06 06:42:31 +08:00
|
|
|
int err;
|
|
|
|
size_t upper_limit = config.params.size_upper;
|
|
|
|
if (upper_limit < 1)
|
|
|
|
upper_limit = config.params.size_now * 2;
|
|
|
|
|
2023-11-01 16:04:00 +08:00
|
|
|
tablename_buf buffer;
|
|
|
|
const char *const tablename = db_tablename(buffer);
|
|
|
|
tablename_buf buffer_renamed;
|
|
|
|
const char *const tablename_renamed =
|
|
|
|
db_tablename(buffer_renamed, ".renamed");
|
|
|
|
|
2017-04-21 23:41:11 +08:00
|
|
|
while (should_continue()) {
|
|
|
|
jitter_delay();
|
|
|
|
db_open();
|
|
|
|
|
2020-12-17 20:42:23 +08:00
|
|
|
if (!dbi && !mode_readonly()) {
|
|
|
|
// create table
|
|
|
|
txn_begin(false);
|
|
|
|
dbi = db_table_open(true);
|
|
|
|
check_dbi_error(MDBX_SUCCESS, "created-uncommitted");
|
2023-11-01 16:04:00 +08:00
|
|
|
|
|
|
|
bool renamed = false;
|
|
|
|
if (flipcoin()) {
|
|
|
|
err = mdbx_dbi_rename(txn_guard.get(), dbi, tablename_renamed);
|
|
|
|
if (err != MDBX_SUCCESS)
|
|
|
|
failure_perror("jitter.rename-1", err);
|
|
|
|
renamed = true;
|
|
|
|
}
|
|
|
|
|
2020-12-17 20:42:23 +08:00
|
|
|
// note: here and below the 4-byte length keys and value are used
|
|
|
|
// to be compatible with any Db-flags given from command line.
|
2022-02-02 22:11:45 +08:00
|
|
|
MDBX_val k = {(void *)"k000", 4}, v = {(void *)"v001", 4};
|
|
|
|
err = mdbx_put(txn_guard.get(), dbi, &k, &v, MDBX_UPSERT);
|
2020-12-17 20:42:23 +08:00
|
|
|
if (err != MDBX_SUCCESS)
|
|
|
|
failure_perror("jitter.put-1", err);
|
|
|
|
txn_end(false);
|
|
|
|
|
|
|
|
// drop & re-create table, but abort txn
|
|
|
|
txn_begin(false);
|
|
|
|
check_dbi_error(MDBX_SUCCESS, "created-committed");
|
|
|
|
err = mdbx_drop(txn_guard.get(), dbi, true);
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
failure_perror("mdbx_drop(delete=true)", err);
|
|
|
|
check_dbi_error(MDBX_BAD_DBI, "dropped-uncommitted");
|
|
|
|
dbi = db_table_open(true);
|
|
|
|
check_dbi_error(MDBX_SUCCESS, "recreated-uncommitted");
|
|
|
|
txn_end(true);
|
|
|
|
|
|
|
|
// check after aborted txn
|
|
|
|
txn_begin(false);
|
2022-02-02 22:11:45 +08:00
|
|
|
v = {(void *)"v002", 4};
|
|
|
|
err = mdbx_put(txn_guard.get(), dbi, &k, &v, MDBX_UPSERT);
|
2020-12-17 20:42:23 +08:00
|
|
|
if (err != MDBX_BAD_DBI)
|
|
|
|
failure_perror("jitter.put-2", err);
|
|
|
|
check_dbi_error(MDBX_BAD_DBI, "dropped-recreated-aborted");
|
|
|
|
// restore DBI
|
2023-11-01 16:04:00 +08:00
|
|
|
dbi = db_table_open(false, renamed);
|
|
|
|
if (renamed) {
|
|
|
|
err = mdbx_dbi_open(
|
|
|
|
txn_guard.get(), tablename_renamed,
|
|
|
|
flipcoin() ? MDBX_DB_ACCEDE : config.params.table_flags, &dbi);
|
|
|
|
if (unlikely(err != MDBX_SUCCESS))
|
|
|
|
failure_perror("open-renamed", err);
|
|
|
|
err = mdbx_dbi_rename(txn_guard.get(), dbi, tablename);
|
|
|
|
if (err != MDBX_SUCCESS)
|
|
|
|
failure_perror("jitter.rename-2", err);
|
|
|
|
}
|
2020-12-17 20:42:23 +08:00
|
|
|
check_dbi_error(MDBX_SUCCESS, "dropped-recreated-aborted+reopened");
|
2022-02-02 22:11:45 +08:00
|
|
|
v = {(void *)"v003", 4};
|
|
|
|
err = mdbx_put(txn_guard.get(), dbi, &k, &v, MDBX_UPSERT);
|
2020-12-17 20:42:23 +08:00
|
|
|
if (err != MDBX_SUCCESS)
|
|
|
|
failure_perror("jitter.put-3", err);
|
|
|
|
txn_end(false);
|
|
|
|
}
|
|
|
|
|
2019-12-07 06:35:05 +08:00
|
|
|
if (upper_limit < 1) {
|
|
|
|
MDBX_envinfo info;
|
|
|
|
err = mdbx_env_info_ex(db_guard.get(), txn_guard.get(), &info,
|
|
|
|
sizeof(info));
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_env_info_ex()", err);
|
|
|
|
upper_limit = (info.mi_geo.upper < INTPTR_MAX)
|
|
|
|
? (intptr_t)info.mi_geo.upper
|
|
|
|
: INTPTR_MAX;
|
|
|
|
}
|
|
|
|
|
2020-01-06 06:42:31 +08:00
|
|
|
if (flipcoin()) {
|
|
|
|
jitter_delay();
|
|
|
|
txn_begin(true);
|
|
|
|
fetch_canary();
|
2024-03-04 04:07:45 +08:00
|
|
|
if (flipcoin()) {
|
|
|
|
MDBX_txn_info info;
|
|
|
|
err = mdbx_txn_reset(txn_guard.get());
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_reset()", err);
|
|
|
|
err = mdbx_txn_info(txn_guard.get(), &info, false);
|
|
|
|
if (err != MDBX_BAD_TXN)
|
|
|
|
failure_perror("mdbx_txn_info(MDBX_BAD_TXN)", err);
|
|
|
|
err = mdbx_txn_reset(txn_guard.get());
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_reset(again)", err);
|
|
|
|
err = mdbx_txn_break(txn_guard.get());
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_break()", err);
|
|
|
|
|
|
|
|
err = mdbx_txn_abort(txn_guard.get());
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_abort()", err);
|
|
|
|
txn_guard.release();
|
|
|
|
txn_begin(true);
|
|
|
|
err = mdbx_txn_reset(txn_guard.get());
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_reset()", err);
|
|
|
|
|
|
|
|
err = mdbx_txn_renew(txn_guard.get());
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_renew()", err);
|
|
|
|
err = mdbx_txn_info(txn_guard.get(), &info, false);
|
|
|
|
if (err)
|
|
|
|
failure_perror("mdbx_txn_info()", err);
|
|
|
|
}
|
2020-01-06 06:42:31 +08:00
|
|
|
jitter_delay();
|
|
|
|
txn_end(flipcoin());
|
|
|
|
}
|
2019-12-07 06:35:05 +08:00
|
|
|
|
2020-01-06 06:42:31 +08:00
|
|
|
const bool coin4size = flipcoin();
|
2017-04-21 23:41:11 +08:00
|
|
|
jitter_delay();
|
|
|
|
txn_begin(mode_readonly());
|
|
|
|
jitter_delay();
|
|
|
|
if (!mode_readonly()) {
|
2017-04-24 00:00:28 +08:00
|
|
|
fetch_canary();
|
|
|
|
update_canary(1);
|
2022-01-21 04:38:27 +08:00
|
|
|
if (global::config::geometry_jitter) {
|
|
|
|
err = mdbx_env_set_geometry(
|
|
|
|
db_guard.get(), -1, -1,
|
|
|
|
coin4size ? upper_limit * 2 / 3 : upper_limit * 3 / 2, -1, -1, -1);
|
|
|
|
if (err != MDBX_SUCCESS && err != MDBX_UNABLE_EXTEND_MAPSIZE &&
|
2022-03-29 05:31:37 +08:00
|
|
|
err != MDBX_MAP_FULL && err != MDBX_TOO_LARGE && err != MDBX_EPERM)
|
2022-01-21 04:38:27 +08:00
|
|
|
failure_perror("mdbx_env_set_geometry-1", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txn_end(flipcoin());
|
|
|
|
|
|
|
|
if (global::config::geometry_jitter) {
|
2020-01-06 06:42:31 +08:00
|
|
|
err = mdbx_env_set_geometry(
|
|
|
|
db_guard.get(), -1, -1,
|
2022-01-21 04:38:27 +08:00
|
|
|
!coin4size ? upper_limit * 2 / 3 : upper_limit * 3 / 2, -1, -1, -1);
|
2020-02-28 22:39:52 +08:00
|
|
|
if (err != MDBX_SUCCESS && err != MDBX_UNABLE_EXTEND_MAPSIZE &&
|
2022-03-29 05:31:37 +08:00
|
|
|
err != MDBX_MAP_FULL && err != MDBX_TOO_LARGE && err != MDBX_EPERM)
|
2022-01-21 04:38:27 +08:00
|
|
|
failure_perror("mdbx_env_set_geometry-2", err);
|
2017-04-21 23:41:11 +08:00
|
|
|
}
|
2019-12-07 06:35:05 +08:00
|
|
|
|
2017-04-21 23:41:11 +08:00
|
|
|
if (flipcoin()) {
|
|
|
|
jitter_delay();
|
2017-10-27 02:14:29 +08:00
|
|
|
txn_begin(true);
|
2017-04-21 23:41:11 +08:00
|
|
|
jitter_delay();
|
2017-04-24 00:00:28 +08:00
|
|
|
txn_end(flipcoin());
|
2017-04-21 23:41:11 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 04:38:27 +08:00
|
|
|
if (global::config::geometry_jitter) {
|
|
|
|
jitter_delay();
|
|
|
|
err = mdbx_env_set_geometry(db_guard.get(), -1, -1, upper_limit, -1, -1,
|
|
|
|
-1);
|
|
|
|
if (err != MDBX_SUCCESS && err != MDBX_UNABLE_EXTEND_MAPSIZE &&
|
2022-03-29 05:31:37 +08:00
|
|
|
err != MDBX_MAP_FULL && err != MDBX_TOO_LARGE && err != MDBX_EPERM)
|
2022-01-21 04:38:27 +08:00
|
|
|
failure_perror("mdbx_env_set_geometry-3", err);
|
|
|
|
}
|
2019-12-07 06:35:05 +08:00
|
|
|
|
2017-04-21 23:41:11 +08:00
|
|
|
db_close();
|
2018-09-17 03:01:56 +08:00
|
|
|
|
|
|
|
/* just 'align' nops with other tests with batching */
|
|
|
|
const auto batching =
|
|
|
|
std::max(config.params.batch_read, config.params.batch_write);
|
|
|
|
report(std::max(1u, batching / 2));
|
2017-04-21 23:41:11 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|