Sqlite: WAL frame admission and automatic rollback reporting (#47)
This commit is contained in:
1 parent
b3c566ed4d
commit
a40882522a
5 files changed
+364
-28
No files matched your search
@@ -140,6 +140,12 @@ namespace Void::Sqlite
|
||||
}
|
||||
return Void::Result::fail(std::move(c).error());
|
||||
}
|
||||
} else if (tx->state() != TransactionState::Committed) {
|
||||
// SQLite may end the transaction on its own (FULL, interrupt,
|
||||
// IO error). Never report the callback's success as durable.
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::misuse,
|
||||
"AsyncPool: transaction ended before commit (automatically rolled back)"));
|
||||
}
|
||||
return res;
|
||||
},
|
||||
|
||||
@@ -181,7 +181,8 @@ namespace Void::Sqlite
|
||||
};
|
||||
|
||||
/// RAII transaction retaining shared ConnectionState. Never marks Committed
|
||||
/// before COMMIT succeeds; inspects sqlite3_get_autocommit on error.
|
||||
/// before COMMIT succeeds; inspects sqlite3_get_autocommit on error and after
|
||||
/// statements so an automatic rollback is reported as RolledBack.
|
||||
class Transaction
|
||||
{
|
||||
public:
|
||||
@@ -192,11 +193,11 @@ namespace Void::Sqlite
|
||||
Transaction(const Transaction&) = delete;
|
||||
Transaction& operator=(const Transaction&) = delete;
|
||||
|
||||
[[nodiscard]] TransactionState state() const noexcept { return m_state; }
|
||||
[[nodiscard]] bool is_active() const noexcept
|
||||
{
|
||||
return m_state == TransactionState::Active;
|
||||
}
|
||||
/// Observes the native autocommit state; returns RolledBack when SQLite
|
||||
/// already ended this transaction (for example SQLITE_FULL or an
|
||||
/// interrupt inside the transaction).
|
||||
[[nodiscard]] TransactionState state() const noexcept;
|
||||
[[nodiscard]] bool is_active() const noexcept;
|
||||
|
||||
Result<void> commit();
|
||||
[[nodiscard]] Result<void> rollback_checked();
|
||||
@@ -204,10 +205,13 @@ namespace Void::Sqlite
|
||||
|
||||
private:
|
||||
friend class Database;
|
||||
explicit Transaction(std::shared_ptr<detail::ConnectionState> conn) noexcept;
|
||||
explicit Transaction(std::shared_ptr<detail::ConnectionState> conn,
|
||||
std::uint64_t id) noexcept;
|
||||
void refresh_state() const noexcept;
|
||||
|
||||
std::shared_ptr<detail::ConnectionState> m_conn{};
|
||||
TransactionState m_state{TransactionState::RolledBack};
|
||||
std::uint64_t m_id{0};
|
||||
mutable TransactionState m_state{TransactionState::RolledBack};
|
||||
};
|
||||
|
||||
/// Separately owned nested savepoint scope.
|
||||
|
||||
@@ -177,6 +177,8 @@ namespace Void::Sqlite
|
||||
std::size_t max_cached_statements{64};
|
||||
std::size_t max_cache_bytes{8ULL * 1024ULL * 1024ULL};
|
||||
std::chrono::milliseconds busy_timeout{5000};
|
||||
/// WAL thresholds are measured in un-checkpointed frames (converted to
|
||||
/// bytes with the database page size), not the raw on-disk WAL size.
|
||||
std::uint64_t wal_checkpoint_threshold_bytes{16ULL * 1024ULL * 1024ULL};
|
||||
std::uint64_t wal_warn_threshold_bytes{64ULL * 1024ULL * 1024ULL};
|
||||
std::uint64_t wal_stop_writes_threshold_bytes{256ULL * 1024ULL * 1024ULL};
|
||||
@@ -335,6 +337,8 @@ namespace Void::Sqlite
|
||||
struct CheckpointReport {
|
||||
int log_frames{0};
|
||||
int checkpointed_frames{0};
|
||||
/// Frames written to the WAL but not yet copied back into the database.
|
||||
int uncheckpointed_frames{0};
|
||||
bool busy{false};
|
||||
std::uint64_t wal_size_bytes{0};
|
||||
};
|
||||
@@ -364,6 +368,14 @@ namespace Void::Sqlite
|
||||
bool active_transaction{false};
|
||||
std::size_t open_savepoints{0};
|
||||
std::uint64_t wal_size_bytes{0};
|
||||
/// Whether the frame counts below were obtained from the WAL index. When
|
||||
/// false, `wal_uncheckpointed_bytes` is a conservative upper bound (the
|
||||
/// on-disk WAL size) and the frame counts are zero.
|
||||
bool wal_frames_known{false};
|
||||
int wal_log_frames{0};
|
||||
int wal_checkpointed_frames{0};
|
||||
int wal_uncheckpointed_frames{0};
|
||||
std::uint64_t wal_uncheckpointed_bytes{0};
|
||||
bool wal_checkpoint_recommended{false};
|
||||
bool wal_warn_exceeded{false};
|
||||
bool wal_write_admission_blocked{false};
|
||||
|
||||
+164
-20
@@ -383,14 +383,25 @@ namespace Void::Sqlite
|
||||
namespace detail
|
||||
{
|
||||
struct ConnectionState {
|
||||
struct WalUsage {
|
||||
std::uint64_t size_bytes{0};
|
||||
std::uint64_t uncheckpointed_bytes{0};
|
||||
int log_frames{-1};
|
||||
int checkpointed_frames{-1};
|
||||
bool frames_known{false};
|
||||
};
|
||||
|
||||
sqlite3* db{nullptr};
|
||||
std::filesystem::path path{};
|
||||
OpenOptions options{};
|
||||
EffectiveCapabilities caps{};
|
||||
int page_size{0};
|
||||
bool closing{false};
|
||||
bool closed{false};
|
||||
std::size_t live_statements{0};
|
||||
bool active_transaction{false};
|
||||
std::uint64_t active_transaction_id{0};
|
||||
std::uint64_t next_transaction_id{0};
|
||||
std::size_t open_savepoints{0};
|
||||
|
||||
std::vector<std::unique_ptr<ScalarFunctionBox>> scalar_functions{};
|
||||
@@ -422,6 +433,62 @@ namespace Void::Sqlite
|
||||
return ec ? 0ULL : static_cast<std::uint64_t>(sz);
|
||||
}
|
||||
|
||||
/// Measures WAL usage with an un-checkpointed-frame count. A passive
|
||||
/// checkpoint is only attempted once the on-disk WAL reaches
|
||||
/// `probe_threshold`; below that the file size is already a safe upper
|
||||
/// bound and no native checkpoint work is done. Passive checkpoints never
|
||||
/// block writers and never truncate, so this only advances nBackfill when
|
||||
/// no reader pins the frames. If frame information is unavailable the
|
||||
/// on-disk size is returned as a conservative fallback.
|
||||
[[nodiscard]] WalUsage wal_usage(std::uint64_t probe_threshold) noexcept
|
||||
{
|
||||
WalUsage usage;
|
||||
usage.size_bytes = current_wal_size_bytes();
|
||||
if (usage.size_bytes == 0) {
|
||||
return usage;
|
||||
}
|
||||
if (db == nullptr || usage.size_bytes < probe_threshold) {
|
||||
usage.uncheckpointed_bytes = usage.size_bytes;
|
||||
return usage;
|
||||
}
|
||||
|
||||
int log_frames = -1;
|
||||
int checkpointed_frames = -1;
|
||||
const int rc = sqlite3_wal_checkpoint_v2(
|
||||
db, "main", SQLITE_CHECKPOINT_PASSIVE, &log_frames, &checkpointed_frames);
|
||||
if ((rc == SQLITE_OK || rc == SQLITE_BUSY) && log_frames >= 0 &&
|
||||
checkpointed_frames >= 0) {
|
||||
usage.log_frames = log_frames;
|
||||
usage.checkpointed_frames = checkpointed_frames;
|
||||
usage.frames_known = true;
|
||||
const auto remaining = static_cast<std::uint64_t>(
|
||||
std::max(0, log_frames - checkpointed_frames));
|
||||
if (page_size > 0) {
|
||||
usage.uncheckpointed_bytes =
|
||||
std::min(usage.size_bytes,
|
||||
remaining * static_cast<std::uint64_t>(page_size));
|
||||
} else {
|
||||
usage.uncheckpointed_bytes = usage.size_bytes;
|
||||
}
|
||||
return usage;
|
||||
}
|
||||
|
||||
usage.uncheckpointed_bytes = usage.size_bytes;
|
||||
return usage;
|
||||
}
|
||||
|
||||
/// Clears wrapper transaction bookkeeping when SQLite ended the
|
||||
/// transaction on its own (autocommit is back on). The original SQLite
|
||||
/// error is never replaced by this observation.
|
||||
void observe_autocommit_transition() noexcept
|
||||
{
|
||||
if (db != nullptr && active_transaction &&
|
||||
sqlite3_get_autocommit(db) != 0) {
|
||||
active_transaction = false;
|
||||
active_transaction_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
~ConnectionState()
|
||||
{
|
||||
if (db != nullptr) {
|
||||
@@ -890,16 +957,20 @@ namespace Void::Sqlite
|
||||
++m_row_generation;
|
||||
m_has_row = false;
|
||||
|
||||
if (!m_conn->active_transaction &&
|
||||
sqlite3_stmt_readonly(m_stmt) == 0 &&
|
||||
m_conn->current_wal_size_bytes() >=
|
||||
if (!m_conn->active_transaction && sqlite3_stmt_readonly(m_stmt) == 0) {
|
||||
const auto wal = m_conn->wal_usage(
|
||||
m_conn->options.limits.wal_stop_writes_threshold_bytes);
|
||||
if (wal.uncheckpointed_bytes >=
|
||||
m_conn->options.limits.wal_stop_writes_threshold_bytes) {
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::overloaded,
|
||||
"Sqlite: WAL size reached write-admission stop threshold; checkpoint required"));
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::overloaded,
|
||||
"Sqlite: un-checkpointed WAL frames reached write-admission stop "
|
||||
"threshold; checkpoint required"));
|
||||
}
|
||||
}
|
||||
|
||||
const int rc = sqlite3_step(m_stmt);
|
||||
m_conn->observe_autocommit_transition();
|
||||
if (rc == SQLITE_ROW) {
|
||||
m_has_row = true;
|
||||
return true;
|
||||
@@ -1251,13 +1322,44 @@ namespace Void::Sqlite
|
||||
|
||||
// ── Transaction ──────────────────────────────────────────────────────────
|
||||
|
||||
Transaction::Transaction(std::shared_ptr<detail::ConnectionState> conn) noexcept
|
||||
: m_conn(std::move(conn)), m_state(TransactionState::Active)
|
||||
Transaction::Transaction(
|
||||
std::shared_ptr<detail::ConnectionState> conn,
|
||||
std::uint64_t id) noexcept
|
||||
: m_conn(std::move(conn)), m_id(id), m_state(TransactionState::Active)
|
||||
{
|
||||
}
|
||||
|
||||
void Transaction::refresh_state() const noexcept
|
||||
{
|
||||
if (m_state != TransactionState::Active || !m_conn || m_conn->db == nullptr) {
|
||||
return;
|
||||
}
|
||||
const bool owns_connection_transaction =
|
||||
m_conn->active_transaction && m_conn->active_transaction_id == m_id;
|
||||
if (!owns_connection_transaction ||
|
||||
sqlite3_get_autocommit(m_conn->db) != 0) {
|
||||
m_state = TransactionState::RolledBack;
|
||||
if (m_conn->active_transaction_id == m_id) {
|
||||
m_conn->active_transaction = false;
|
||||
m_conn->active_transaction_id = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TransactionState Transaction::state() const noexcept
|
||||
{
|
||||
refresh_state();
|
||||
return m_state;
|
||||
}
|
||||
|
||||
bool Transaction::is_active() const noexcept
|
||||
{
|
||||
return state() == TransactionState::Active;
|
||||
}
|
||||
|
||||
Transaction::~Transaction()
|
||||
{
|
||||
refresh_state();
|
||||
if (m_state == TransactionState::Active) {
|
||||
auto r = rollback_checked();
|
||||
if (!r && m_conn) {
|
||||
@@ -1268,6 +1370,7 @@ namespace Void::Sqlite
|
||||
|
||||
Transaction::Transaction(Transaction&& o) noexcept
|
||||
: m_conn(std::move(o.m_conn)),
|
||||
m_id(std::exchange(o.m_id, 0)),
|
||||
m_state(std::exchange(o.m_state, TransactionState::RolledBack))
|
||||
{
|
||||
}
|
||||
@@ -1275,6 +1378,7 @@ namespace Void::Sqlite
|
||||
Transaction& Transaction::operator=(Transaction&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
refresh_state();
|
||||
if (m_state == TransactionState::Active) {
|
||||
auto r = rollback_checked();
|
||||
if (!r && m_conn) {
|
||||
@@ -1282,6 +1386,7 @@ namespace Void::Sqlite
|
||||
}
|
||||
}
|
||||
m_conn = std::move(o.m_conn);
|
||||
m_id = std::exchange(o.m_id, 0);
|
||||
m_state = std::exchange(o.m_state, TransactionState::RolledBack);
|
||||
}
|
||||
return *this;
|
||||
@@ -1289,10 +1394,13 @@ namespace Void::Sqlite
|
||||
|
||||
Result<void> Transaction::commit()
|
||||
{
|
||||
refresh_state();
|
||||
if (m_state != TransactionState::Active) {
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::misuse,
|
||||
"Sqlite: commit() called on non-active transaction"));
|
||||
m_state == TransactionState::RolledBack
|
||||
? "Sqlite: commit() called on a transaction that was already rolled back"
|
||||
: "Sqlite: commit() called on non-active transaction"));
|
||||
}
|
||||
if (!m_conn || m_conn->db == nullptr) {
|
||||
m_state = TransactionState::RecoveryRequired;
|
||||
@@ -1309,9 +1417,11 @@ namespace Void::Sqlite
|
||||
if (sqlite3_get_autocommit(m_conn->db) == 0) {
|
||||
m_state = TransactionState::Active;
|
||||
m_conn->active_transaction = true;
|
||||
m_conn->active_transaction_id = m_id;
|
||||
} else {
|
||||
m_state = TransactionState::RolledBack;
|
||||
m_conn->active_transaction = false;
|
||||
m_conn->active_transaction_id = 0;
|
||||
}
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
map_sqlite_rc(fault.fail_next_commit_rc),
|
||||
@@ -1325,6 +1435,7 @@ namespace Void::Sqlite
|
||||
if (rc == SQLITE_OK) {
|
||||
m_state = TransactionState::Committed;
|
||||
m_conn->active_transaction = false;
|
||||
m_conn->active_transaction_id = 0;
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -1338,9 +1449,11 @@ namespace Void::Sqlite
|
||||
if (sqlite3_get_autocommit(m_conn->db) == 0) {
|
||||
m_state = TransactionState::Active;
|
||||
m_conn->active_transaction = true;
|
||||
m_conn->active_transaction_id = m_id;
|
||||
} else {
|
||||
m_state = TransactionState::RolledBack;
|
||||
m_conn->active_transaction = false;
|
||||
m_conn->active_transaction_id = 0;
|
||||
}
|
||||
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
@@ -1352,6 +1465,7 @@ namespace Void::Sqlite
|
||||
|
||||
Result<void> Transaction::rollback_checked()
|
||||
{
|
||||
refresh_state();
|
||||
if (m_state == TransactionState::RolledBack) {
|
||||
return {};
|
||||
}
|
||||
@@ -1375,6 +1489,7 @@ namespace Void::Sqlite
|
||||
if (rc == SQLITE_OK || sqlite3_get_autocommit(m_conn->db) != 0) {
|
||||
m_state = TransactionState::RolledBack;
|
||||
m_conn->active_transaction = false;
|
||||
m_conn->active_transaction_id = 0;
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -1547,6 +1662,10 @@ namespace Void::Sqlite
|
||||
state->caps.in_memory = is_memory_path(path);
|
||||
state->caps.read_only = (state->options.mode == OpenMode::ReadOnly) ||
|
||||
(sqlite3_db_readonly(raw_db, "main") > 0);
|
||||
if (auto page_size = query_single_int_pragma(raw_db, "PRAGMA page_size");
|
||||
page_size && *page_size > 0) {
|
||||
state->page_size = *page_size;
|
||||
}
|
||||
|
||||
// Disable extension loading by default.
|
||||
int ext_enabled = 0;
|
||||
@@ -1675,7 +1794,9 @@ namespace Void::Sqlite
|
||||
SqliteErrorCode::invalid_argument, "Sqlite: SQL contains embedded NUL byte"));
|
||||
}
|
||||
const std::string s{sql};
|
||||
return exec_raw_sql(m_conn->db, s, "exec");
|
||||
auto result = exec_raw_sql(m_conn->db, s, "exec");
|
||||
m_conn->observe_autocommit_transition();
|
||||
return result;
|
||||
}
|
||||
|
||||
Result<Statement> Database::prepare(std::string_view sql)
|
||||
@@ -1753,12 +1874,16 @@ namespace Void::Sqlite
|
||||
SqliteErrorCode::misuse,
|
||||
"Sqlite: nested begin() rejected; use savepoint() for nested scopes"));
|
||||
}
|
||||
if (mode != BeginMode::Deferred &&
|
||||
m_conn->current_wal_size_bytes() >=
|
||||
if (mode != BeginMode::Deferred) {
|
||||
const auto wal = m_conn->wal_usage(
|
||||
m_conn->options.limits.wal_stop_writes_threshold_bytes);
|
||||
if (wal.uncheckpointed_bytes >=
|
||||
m_conn->options.limits.wal_stop_writes_threshold_bytes) {
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::overloaded,
|
||||
"Sqlite: WAL size reached write-admission stop threshold; checkpoint required"));
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::overloaded,
|
||||
"Sqlite: un-checkpointed WAL frames reached write-admission stop "
|
||||
"threshold; checkpoint required"));
|
||||
}
|
||||
}
|
||||
|
||||
const char* sql = "BEGIN";
|
||||
@@ -1771,7 +1896,11 @@ namespace Void::Sqlite
|
||||
return Void::Result::fail(std::move(r).error());
|
||||
}
|
||||
m_conn->active_transaction = true;
|
||||
return Transaction{m_conn};
|
||||
m_conn->active_transaction_id = ++m_conn->next_transaction_id;
|
||||
if (m_conn->active_transaction_id == 0) {
|
||||
m_conn->active_transaction_id = ++m_conn->next_transaction_id;
|
||||
}
|
||||
return Transaction{m_conn, m_conn->active_transaction_id};
|
||||
}
|
||||
|
||||
Result<Savepoint> Database::savepoint(std::string_view name)
|
||||
@@ -1912,16 +2041,29 @@ namespace Void::Sqlite
|
||||
{
|
||||
ConnectionHealth out;
|
||||
if (!m_conn) return out;
|
||||
m_conn->observe_autocommit_transition();
|
||||
out.live_statements = m_conn->live_statements;
|
||||
out.active_transaction = m_conn->active_transaction;
|
||||
out.open_savepoints = m_conn->open_savepoints;
|
||||
out.wal_size_bytes = m_conn->current_wal_size_bytes();
|
||||
const auto wal = m_conn->wal_usage(
|
||||
m_conn->options.limits.wal_checkpoint_threshold_bytes);
|
||||
out.wal_size_bytes = wal.size_bytes;
|
||||
out.wal_frames_known = wal.frames_known;
|
||||
out.wal_log_frames = std::max(0, wal.log_frames);
|
||||
out.wal_checkpointed_frames = std::max(0, wal.checkpointed_frames);
|
||||
out.wal_uncheckpointed_frames =
|
||||
wal.frames_known
|
||||
? std::max(0, wal.log_frames - wal.checkpointed_frames)
|
||||
: 0;
|
||||
out.wal_uncheckpointed_bytes = wal.uncheckpointed_bytes;
|
||||
out.wal_checkpoint_recommended =
|
||||
out.wal_size_bytes >= m_conn->options.limits.wal_checkpoint_threshold_bytes;
|
||||
wal.uncheckpointed_bytes >=
|
||||
m_conn->options.limits.wal_checkpoint_threshold_bytes;
|
||||
out.wal_warn_exceeded =
|
||||
out.wal_size_bytes >= m_conn->options.limits.wal_warn_threshold_bytes;
|
||||
wal.uncheckpointed_bytes >= m_conn->options.limits.wal_warn_threshold_bytes;
|
||||
out.wal_write_admission_blocked =
|
||||
out.wal_size_bytes >= m_conn->options.limits.wal_stop_writes_threshold_bytes;
|
||||
wal.uncheckpointed_bytes >=
|
||||
m_conn->options.limits.wal_stop_writes_threshold_bytes;
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -2212,6 +2354,8 @@ namespace Void::Sqlite
|
||||
CheckpointReport rep;
|
||||
rep.log_frames = std::max(0, log_frames);
|
||||
rep.checkpointed_frames = std::max(0, ckpt_frames);
|
||||
rep.uncheckpointed_frames =
|
||||
std::max(0, rep.log_frames - rep.checkpointed_frames);
|
||||
rep.busy = (rc == SQLITE_BUSY) || (rep.log_frames > rep.checkpointed_frames);
|
||||
rep.wal_size_bytes = m_conn->current_wal_size_bytes();
|
||||
return rep;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
@@ -467,3 +468,172 @@ Void::Testing::Test sqlite_checkpoint_wal_admission_backup_restore_and_safe_call
|
||||
Void::Testing::expect(!writer.exec("DROP TABLE logs").has_value());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test sqlite_wal_admission_counts_uncheckpointed_frames()
|
||||
{
|
||||
TempSqliteDir dir;
|
||||
const auto db_path = dir.path / "wal-frames.db";
|
||||
const auto wal_path = std::filesystem::path{db_path.string() + "-wal"};
|
||||
|
||||
OpenOptions opts;
|
||||
opts.limits.busy_timeout = std::chrono::milliseconds{20};
|
||||
opts.limits.wal_checkpoint_threshold_bytes = 4096;
|
||||
opts.limits.wal_warn_threshold_bytes = 8192;
|
||||
opts.limits.wal_stop_writes_threshold_bytes = 16384;
|
||||
|
||||
auto writer_r = open_database(db_path, opts);
|
||||
Void::Testing::require(writer_r.has_value());
|
||||
auto& writer = *writer_r;
|
||||
Void::Testing::require(
|
||||
writer.exec("CREATE TABLE t(id INTEGER PRIMARY KEY, payload BLOB)").has_value());
|
||||
|
||||
// Grow the WAL past the stop threshold.
|
||||
{
|
||||
auto tx = writer.begin(BeginMode::Immediate);
|
||||
Void::Testing::require(tx.has_value());
|
||||
auto ins = writer.prepare("INSERT INTO t(payload) VALUES(?)");
|
||||
Void::Testing::require(ins.has_value());
|
||||
const std::string blob(4096, 'W');
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
Void::Testing::require(ins->bind_checked(1, ValueView{blob}).has_value());
|
||||
Void::Testing::require(ins->step().has_value());
|
||||
Void::Testing::require(ins->reset_checked().has_value());
|
||||
}
|
||||
Void::Testing::require(tx->commit().has_value());
|
||||
}
|
||||
Void::Testing::require(std::filesystem::exists(wal_path));
|
||||
Void::Testing::expect(std::filesystem::file_size(wal_path) >= 16384);
|
||||
|
||||
// Pin a reader at the current snapshot.
|
||||
OpenOptions ropts;
|
||||
ropts.mode = OpenMode::ReadOnly;
|
||||
auto reader_r = open_database(db_path, ropts);
|
||||
Void::Testing::require(reader_r.has_value());
|
||||
auto& reader = *reader_r;
|
||||
auto rtx = reader.begin(BeginMode::Deferred);
|
||||
Void::Testing::require(rtx.has_value());
|
||||
{
|
||||
auto sel = reader.prepare("SELECT COUNT(*) FROM t");
|
||||
Void::Testing::require(sel.has_value() && *sel->step());
|
||||
}
|
||||
|
||||
// A passive checkpoint can backfill every frame the reader has already seen.
|
||||
// The WAL file itself stays large (passive checkpoints never truncate).
|
||||
auto ckpt = writer.checkpoint(CheckpointMode::Passive);
|
||||
Void::Testing::require(ckpt.has_value());
|
||||
Void::Testing::expect(ckpt->uncheckpointed_frames == 0);
|
||||
Void::Testing::expect(std::filesystem::file_size(wal_path) >= 16384);
|
||||
|
||||
const auto h = writer.health();
|
||||
Void::Testing::expect(h.wal_size_bytes >= 16384);
|
||||
Void::Testing::expect(h.wal_frames_known);
|
||||
Void::Testing::expect(h.wal_uncheckpointed_frames == 0);
|
||||
Void::Testing::expect(!h.wal_checkpoint_recommended);
|
||||
Void::Testing::expect(!h.wal_write_admission_blocked);
|
||||
|
||||
// Admission uses un-checkpointed frames, not the large but checkpointed file.
|
||||
auto admitted = writer.begin(BeginMode::Immediate);
|
||||
Void::Testing::require(admitted.has_value());
|
||||
{
|
||||
auto ins = writer.prepare("INSERT INTO t(payload) VALUES(?)");
|
||||
Void::Testing::require(ins.has_value());
|
||||
const std::string blob(4096, 'N');
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
Void::Testing::require(ins->bind_checked(1, ValueView{blob}).has_value());
|
||||
Void::Testing::require(ins->step().has_value());
|
||||
Void::Testing::require(ins->reset_checked().has_value());
|
||||
}
|
||||
}
|
||||
Void::Testing::require(admitted->commit().has_value());
|
||||
|
||||
// New frames cannot be checkpointed while the reader pins its snapshot.
|
||||
const auto h2 = writer.health();
|
||||
Void::Testing::expect(h2.wal_frames_known);
|
||||
Void::Testing::expect(h2.wal_uncheckpointed_frames > 0);
|
||||
Void::Testing::expect(h2.wal_write_admission_blocked);
|
||||
|
||||
auto rejected = writer.begin(BeginMode::Immediate);
|
||||
Void::Testing::expect(!rejected.has_value());
|
||||
Void::Testing::expect(rejected.error().code().value ==
|
||||
static_cast<std::int32_t>(SqliteErrorCode::overloaded));
|
||||
|
||||
// Releasing the reader allows a truncating checkpoint to restore admission.
|
||||
Void::Testing::require(rtx->commit().has_value());
|
||||
auto ok_ckpt = writer.checkpoint(CheckpointMode::Truncate);
|
||||
Void::Testing::require(ok_ckpt.has_value());
|
||||
Void::Testing::expect(!writer.health().wal_write_admission_blocked);
|
||||
}
|
||||
|
||||
Void::Testing::Test sqlite_transaction_reports_automatic_rollback()
|
||||
{
|
||||
TempSqliteDir dir;
|
||||
const auto db_path = dir.path / "auto-rollback.db";
|
||||
|
||||
auto db_r = open_database(db_path);
|
||||
Void::Testing::require(db_r.has_value());
|
||||
auto& db = *db_r;
|
||||
Void::Testing::require(
|
||||
db.exec("CREATE TABLE t(id INTEGER PRIMARY KEY, payload BLOB)").has_value());
|
||||
|
||||
// sqlite3_interrupt during an INSERT inside an explicit transaction makes
|
||||
// SQLite roll the whole transaction back.
|
||||
{
|
||||
auto tx = db.begin(BeginMode::Immediate);
|
||||
Void::Testing::require(tx.has_value());
|
||||
Void::Testing::require(
|
||||
db.exec("INSERT INTO t(payload) VALUES(randomblob(1024))").has_value());
|
||||
|
||||
int progress_calls = 0;
|
||||
db.begin_leased_operation(
|
||||
7, 1, [&progress_calls] { return ++progress_calls > 20; });
|
||||
auto stmt = db.prepare(
|
||||
"INSERT INTO t(payload) SELECT randomblob(512) FROM ("
|
||||
"WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL "
|
||||
"SELECT x + 1 FROM c WHERE x < 10000000) SELECT x FROM c)");
|
||||
Void::Testing::require(stmt.has_value());
|
||||
auto interrupted = stmt->step();
|
||||
db.end_leased_operation(7);
|
||||
|
||||
Void::Testing::expect(!interrupted.has_value());
|
||||
Void::Testing::expect(interrupted.error().code().value ==
|
||||
static_cast<std::int32_t>(SqliteErrorCode::cancelled));
|
||||
Void::Testing::expect(sqlite3_get_autocommit(db.handle()) != 0);
|
||||
Void::Testing::expect(tx->state() == TransactionState::RolledBack);
|
||||
Void::Testing::expect(!tx->is_active());
|
||||
|
||||
// The wrapper must not let the stale handle commit a later transaction.
|
||||
auto stale_commit = tx->commit();
|
||||
Void::Testing::expect(!stale_commit.has_value());
|
||||
Void::Testing::expect(stale_commit.error().code().value ==
|
||||
static_cast<std::int32_t>(SqliteErrorCode::misuse));
|
||||
Void::Testing::require(tx->rollback_checked().has_value());
|
||||
}
|
||||
|
||||
// SQLITE_FULL during a statement is another automatic-rollback path.
|
||||
{
|
||||
auto page_count_stmt = db.prepare("PRAGMA page_count");
|
||||
Void::Testing::require(page_count_stmt.has_value() && *page_count_stmt->step());
|
||||
const auto page_count = *page_count_stmt->try_column_int(0);
|
||||
|
||||
auto tx = db.begin(BeginMode::Immediate);
|
||||
Void::Testing::require(tx.has_value());
|
||||
Void::Testing::require(
|
||||
db.exec(std::format("PRAGMA max_page_count={}", page_count)).has_value());
|
||||
|
||||
auto ins = db.prepare("INSERT INTO t(payload) VALUES(?)");
|
||||
Void::Testing::require(ins.has_value());
|
||||
std::vector<std::byte> huge(4U * 1024U * 1024U, std::byte{0x5A});
|
||||
Void::Testing::require(
|
||||
ins->bind_checked(1, ValueView{std::span<const std::byte>{huge}}).has_value());
|
||||
auto full = ins->step();
|
||||
Void::Testing::expect(!full.has_value());
|
||||
Void::Testing::expect(full.error().code().value ==
|
||||
static_cast<std::int32_t>(SqliteErrorCode::full));
|
||||
Void::Testing::expect(sqlite3_get_autocommit(db.handle()) != 0);
|
||||
Void::Testing::expect(tx->state() == TransactionState::RolledBack);
|
||||
Void::Testing::expect(!tx->is_active());
|
||||
|
||||
// Restore a growth-friendly page limit for the remaining tests.
|
||||
Void::Testing::require(db.exec("PRAGMA max_page_count=1073741823").has_value());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user