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
@@ -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