2019-09-09 13:40:24 +03:00
|
|
|
/*
|
2024-03-13 14:57:38 +03:00
|
|
|
* Copyright 2017-2024 Leonid Yuriev <leo@yuriev.ru>
|
2017-03-30 18:54:57 +03:00
|
|
|
* and other libmdbx authors: please see AUTHORS file.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
|
2022-11-08 16:17:14 +03:00
|
|
|
#include "test.h++"
|
2017-03-30 18:54:57 +03:00
|
|
|
|
2021-03-15 20:52:18 +03: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 15:42:23 +03: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 18:41:11 +03:00
|
|
|
bool testcase_jitter::run() {
|
2020-01-06 01:42:31 +03: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 11:04:00 +03: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 18:41:11 +03:00
|
|
|
while (should_continue()) {
|
|
|
|
jitter_delay();
|
|
|
|
db_open();
|
|
|
|
|
2020-12-17 15:42:23 +03: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 11:04:00 +03: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 15:42:23 +03: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 17:11:45 +03: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 15:42:23 +03: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 17:11:45 +03:00
|
|
|
v = {(void *)"v002", 4};
|
|
|
|
err = mdbx_put(txn_guard.get(), dbi, &k, &v, MDBX_UPSERT);
|
2020-12-17 15:42:23 +03: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 11:04:00 +03: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 15:42:23 +03:00
|
|
|
check_dbi_error(MDBX_SUCCESS, "dropped-recreated-aborted+reopened");
|
2022-02-02 17:11:45 +03:00
|
|
|
v = {(void *)"v003", 4};
|
|
|
|
err = mdbx_put(txn_guard.get(), dbi, &k, &v, MDBX_UPSERT);
|
2020-12-17 15:42:23 +03:00
|
|
|
if (err != MDBX_SUCCESS)
|
|
|
|
failure_perror("jitter.put-3", err);
|
|
|
|
txn_end(false);
|
|
|
|
}
|
|
|
|
|
2019-12-07 01:35:05 +03: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 01:42:31 +03:00
|
|
|
if (flipcoin()) {
|
|
|
|
jitter_delay();
|
|
|
|
txn_begin(true);
|
|
|
|
fetch_canary();
|
2024-03-03 23:07:45 +03: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 01:42:31 +03:00
|
|
|
jitter_delay();
|
|
|
|
txn_end(flipcoin());
|
|
|
|
}
|
2019-12-07 01:35:05 +03:00
|
|
|
|
2020-01-06 01:42:31 +03:00
|
|
|
const bool coin4size = flipcoin();
|
2017-04-21 18:41:11 +03:00
|
|
|
jitter_delay();
|
|
|
|
txn_begin(mode_readonly());
|
|
|
|
jitter_delay();
|
|
|
|
if (!mode_readonly()) {
|
2017-04-23 19:00:28 +03:00
|
|
|
fetch_canary();
|
|
|
|
update_canary(1);
|
2022-01-20 23:38:27 +03: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 00:31:37 +03:00
|
|
|
err != MDBX_MAP_FULL && err != MDBX_TOO_LARGE && err != MDBX_EPERM)
|
2022-01-20 23:38:27 +03:00
|
|
|
failure_perror("mdbx_env_set_geometry-1", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txn_end(flipcoin());
|
|
|
|
|
|
|
|
if (global::config::geometry_jitter) {
|
2020-01-06 01:42:31 +03:00
|
|
|
err = mdbx_env_set_geometry(
|
|
|
|
db_guard.get(), -1, -1,
|
2022-01-20 23:38:27 +03:00
|
|
|
!coin4size ? upper_limit * 2 / 3 : upper_limit * 3 / 2, -1, -1, -1);
|
2020-02-28 17:39:52 +03:00
|
|
|
if (err != MDBX_SUCCESS && err != MDBX_UNABLE_EXTEND_MAPSIZE &&
|
2022-03-29 00:31:37 +03:00
|
|
|
err != MDBX_MAP_FULL && err != MDBX_TOO_LARGE && err != MDBX_EPERM)
|
2022-01-20 23:38:27 +03:00
|
|
|
failure_perror("mdbx_env_set_geometry-2", err);
|
2017-04-21 18:41:11 +03:00
|
|
|
}
|
2019-12-07 01:35:05 +03:00
|
|
|
|
2017-04-21 18:41:11 +03:00
|
|
|
if (flipcoin()) {
|
|
|
|
jitter_delay();
|
2017-10-26 21:14:29 +03:00
|
|
|
txn_begin(true);
|
2017-04-21 18:41:11 +03:00
|
|
|
jitter_delay();
|
2017-04-23 19:00:28 +03:00
|
|
|
txn_end(flipcoin());
|
2017-04-21 18:41:11 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 23:38:27 +03: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 00:31:37 +03:00
|
|
|
err != MDBX_MAP_FULL && err != MDBX_TOO_LARGE && err != MDBX_EPERM)
|
2022-01-20 23:38:27 +03:00
|
|
|
failure_perror("mdbx_env_set_geometry-3", err);
|
|
|
|
}
|
2019-12-07 01:35:05 +03:00
|
|
|
|
2017-04-21 18:41:11 +03:00
|
|
|
db_close();
|
2018-09-16 22:01:56 +03: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 18:41:11 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|