Merge issue-47-sqlite-wal: WAL frame admission and rollback reporting (closes #47)
This commit is contained in:
commit
a535f352d6
5 files changed
+364
-28
No files matched your search
+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;
|
||||
|
||||
Reference in new issue
Block a user