Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/api/error-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1392,11 +1392,26 @@
"message": "SQLite database is closed.",
"statuses": []
},
{
"code": "deferred_commits_unsupported",
"message": "SQLite deferred commits are unavailable.",
"statuses": []
},
{
"code": "durability_failed",
"message": "SQLite commits could not be made durable.",
"statuses": []
},
{
"code": "invalid_bind_parameter",
"message": "Invalid SQLite bind parameter.",
"statuses": []
},
{
"code": "invalid_commit_ticket",
"message": "Invalid SQLite commit ticket.",
"statuses": []
},
{
"code": "invalid_compile_options",
"message": "SQLite requires SQLITE_ENABLE_BATCH_ATOMIC_WRITE and SQLITE_DIRECT_OVERFLOW_READ=0. Rebuild with LIBSQLITE3_FLAGS=\"SQLITE_ENABLE_BATCH_ATOMIC_WRITE SQLITE_DIRECT_OVERFLOW_READ=0\".",
Expand Down Expand Up @@ -1432,6 +1447,11 @@
"message": "Remote SQLite is unavailable.",
"statuses": []
},
{
"code": "statement_failed",
"message": "SQLite statement failed.",
"statuses": []
},
{
"code": "storage_overloaded",
"message": "SQLite storage is overloaded.",
Expand All @@ -1442,6 +1462,11 @@
"message": "SQLite storage timed out.",
"statuses": []
},
{
"code": "sync_unsupported",
"message": "Synchronous SQLite is unavailable.",
"statuses": []
},
{
"code": "transaction_closed",
"message": "SQLite transaction coordinator is closed.",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions engine/artifacts/errors/sqlite.durability_failed.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions engine/artifacts/errors/sqlite.invalid_commit_ticket.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions engine/artifacts/errors/sqlite.statement_failed.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions engine/artifacts/errors/sqlite.sync_unsupported.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions engine/packages/sqlite-embedded/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;
use std::ffi::{CStr, CString};
use std::fmt;
use std::os::raw::c_char;
use std::os::raw::{c_char, c_int};
use std::ptr;

use anyhow::{Result, anyhow};
Expand Down Expand Up @@ -29,9 +29,14 @@ pub struct SqliteQueryProfile {

#[derive(Debug)]
pub struct SqliteStatementError {
/// SQLite's extended result code, or -1 when the error did not come from SQLite.
pub code: i32,
pub statement_index: u32,
pub message: String,
/// SQLite's own message, without the operation that failed.
pub detail: String,
/// Byte offset into the statement's SQL of the token SQLite blamed, when it named one.
pub offset: Option<u32>,
}

impl fmt::Display for SqliteStatementError {
Expand Down Expand Up @@ -262,6 +267,8 @@ pub fn execute_single_statement(
code: -1,
statement_index: 0,
message: "sqlite execute only supports a single statement".to_owned(),
detail: "sqlite execute only supports a single statement".to_owned(),
offset: None,
}
.into());
}
Expand Down Expand Up @@ -331,6 +338,8 @@ pub fn execute_single_statement_profiled(
code: -1,
statement_index: 0,
message: "sqlite execute only supports a single statement".to_owned(),
detail: "sqlite execute only supports a single statement".to_owned(),
offset: None,
}
.into());
}
Expand Down Expand Up @@ -877,23 +886,32 @@ fn has_non_whitespace_tail(tail: *const c_char) -> bool {
bytes.iter().any(|byte| !byte.is_ascii_whitespace())
}

// The bundled SQLite has `sqlite3_error_offset`, but libsqlite3-sys's default bindings target an
// older SQLite that predates it.
unsafe extern "C" {
fn sqlite3_error_offset(db: *mut sqlite3) -> c_int;
}

fn sqlite_error(db: *mut sqlite3, statement_index: u32, context: &str) -> anyhow::Error {
let (code, detail) = unsafe {
let (code, detail, offset) = unsafe {
if db.is_null() {
(-1, "unknown sqlite error".to_string())
(-1, "unknown sqlite error".to_string(), -1)
} else {
(
sqlite3_extended_errcode(db),
CStr::from_ptr(sqlite3_errmsg(db))
.to_string_lossy()
.into_owned(),
sqlite3_error_offset(db),
)
}
};
SqliteStatementError {
code,
statement_index,
message: format!("{context}: {detail}"),
detail,
offset: u32::try_from(offset).ok(),
}
.into()
}
7 changes: 7 additions & 0 deletions rivetkit-rust/packages/rivetkit-core/src/actor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ pub struct ActorConfig {
/// on the TS side). Gates the inspector database tab.
pub has_database: bool,
pub remote_sqlite: bool,
/// Commits SQLite writes locally and makes them durable in the background, so writes and
/// state saves resolve before they are durable. Only for embedders that gate every output
/// on `SqliteDb::when_durable`. Not user-facing. Requires local native SQLite.
pub sqlite_deferred_commits: bool,
pub sqlite_profiling: SqliteProfilingConfig,
pub tracing: Arc<ActorTracingConfig>,
/// Whether the user declared actor state (`state: ...` or `createState`).
Expand Down Expand Up @@ -430,6 +434,7 @@ pub struct ActorConfigInput {
pub icon: Option<String>,
pub has_database: Option<bool>,
pub remote_sqlite: Option<bool>,
pub sqlite_deferred_commits: Option<bool>,
pub sqlite_profiling: Option<SqliteProfilingConfigInput>,
pub tracing: Option<ActorTracingConfig>,
pub has_state: Option<bool>,
Expand Down Expand Up @@ -462,6 +467,7 @@ impl ActorConfig {
icon: config.icon,
has_database: config.has_database.unwrap_or(false),
remote_sqlite: config.remote_sqlite.unwrap_or(false),
sqlite_deferred_commits: config.sqlite_deferred_commits.unwrap_or(false),
sqlite_profiling: config
.sqlite_profiling
.map(SqliteProfilingConfig::from_input)
Expand Down Expand Up @@ -590,6 +596,7 @@ impl Default for ActorConfig {
icon: None,
has_database: false,
remote_sqlite: false,
sqlite_deferred_commits: false,
sqlite_profiling: SqliteProfilingConfig::default(),
tracing: Arc::default(),
has_state: false,
Expand Down
16 changes: 16 additions & 0 deletions rivetkit-rust/packages/rivetkit-core/src/actor/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ impl ActorContext {
self.next_schedule_timestamp(false).await?,
self.run_wake_at(),
);
self.wait_for_durable_schedule().await?;
self.sync_alarm_timestamp(next_alarm)
}

Expand All @@ -912,9 +913,24 @@ impl ActorContext {
self.next_schedule_timestamp(true).await?,
self.run_wake_at(),
);
self.wait_for_durable_schedule().await?;
self.sync_alarm_timestamp(next_alarm)
}

/// With deferred commits, the schedule rows the next alarm comes from can still be local.
/// Pushing the alarm first could leave the engine, after a crash, without the wakeup of a
/// durable schedule whose cancellation was lost. Awaited commits are durable already.
async fn wait_for_durable_schedule(&self) -> Result<()> {
if !self.0.schedule_dirty_since_push.load(Ordering::SeqCst) {
return Ok(());
}
let ticket = self.sql().commit_ticket();
if ticket == 0 {
return Ok(());
}
self.sql().when_durable(ticket).await
}

/// Pushes `next_alarm` to the engine unless it equals the last alarm this generation
/// pushed. A generation starts with no last pushed alarm, so its first sync pushes any
/// next alarm; see `force_next_alarm_push`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl SqliteDb {
return Ok(());
}
let transaction = self
.begin_named_transaction(Some(BOOTSTRAP_TRANSACTION_NAME), None)
.begin_internal_transaction(BOOTSTRAP_TRANSACTION_NAME)
.await
.context("begin deferred sqlite bootstrap")?;
transaction
Expand Down
Loading
Loading