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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `POST /delegate` and `POST /revoke` check the signature (and, for a renewal lease, the signature of the stored original it renews) before taking any lock. A forged or slow-to-verify request no longer holds the delegation's chain guards, its spaces' write order or a database transaction while it is checked; it is refused at once. A renewal lease whose original is not stored is also refused before any lock. Inside the transaction only the signed time window is checked again. A resubmitted delegation that is already stored is now also signature-checked before it is answered as existing. Details: TC-2986.
- Add opt-in PostgreSQL checkpoint parts for SQL persistence, with explicit artifact/account capacities and streamed hydration. A database stored in parts loads in bounded statements and is not held to the single-row read limit (`storage.sql.max_artifact_read_bytes`); a load that keeps the whole database in memory (a DuckDB export) still waits for one of the large-transfer slots. Details: TC-2917.
- Policy/v3: minting a session and registering a policy no longer re-verify signatures or re-run the claim, presentation, credential admission and allow evaluations while holding their spaces' write order and row locks. Under the locks they check that the challenge, registration and roots are unchanged; those decisions were made before the locks, at the same time, from the same inputs. Refusal codes are unchanged, except that `credential-evaluation-changed` and `policy-evaluation-changed` can no longer occur. Details: TC-2986.
- DuckDB saves a complete checkpoint when a write changes its main file, and uses incremental WAL persistence only when that file still matches the stored checkpoint, so acknowledged writes remain recoverable after restart. Details: TC-2990.
- SQLite stamp replay deadlines now use the same timestamp encoding as cleanup (TC-2920).
- PostgreSQL database artifacts use uncompressed external storage for new writes to avoid repeated decompression during chunked reads (TC-2917). Existing large artifacts require an operator re-toast; verify both storage settings after rollout and follow `scripts/tc2917-README.md` if lock contention deferred the migration.
- Encryption: v2 column format with key ids, per-purpose keys and recorded format metadata. Optional strict reads are off by default. Details: TC-2516, TC-2517.
Expand Down
85 changes: 84 additions & 1 deletion tinycloud-core/src/duckdb/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! result formatting, native connection open/close and filesystem work do not.

use std::collections::BTreeSet;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
Expand All @@ -12,6 +13,7 @@ use dashmap::DashMap;
use sqlparser::ast::Statement;
use tokio::sync::{mpsc, oneshot};

use crate::hash::Blake3Hasher;
use crate::request_telemetry::{self, QueryTimer, RequestContext};

use super::{
Expand All @@ -38,6 +40,11 @@ enum DbMessage {
Wal {
response_tx: oneshot::Sender<Result<Option<Vec<u8>>, DuckDbError>>,
},
WalForCheckpoint {
checkpoint_size: u64,
checkpoint_hash: String,
response_tx: oneshot::Sender<Result<Option<Vec<u8>>, DuckDbError>>,
},
Shutdown {
response_tx: oneshot::Sender<()>,
},
Expand Down Expand Up @@ -107,6 +114,30 @@ impl DatabaseHandle {
.map_err(|_| DuckDbError::Internal("Database actor dropped response".to_string()))?
}

/// Copy a WAL only when its local main file still matches the durable
/// checkpoint. Both operations run under the same actor fence.
pub async fn wal_for_checkpoint(
&self,
checkpoint_size: u64,
checkpoint_hash: String,
) -> Result<Option<Vec<u8>>, DuckDbError> {
let (response_tx, response_rx) = oneshot::channel();
self.tx
.send((
request_telemetry::current(),
DbMessage::WalForCheckpoint {
checkpoint_size,
checkpoint_hash,
response_tx,
},
))
.await
.map_err(|_| DuckDbError::Internal("Database actor not available".into()))?;
response_rx
.await
.map_err(|_| DuckDbError::Internal("Database actor dropped response".into()))?
}

/// Stop the actor and close its database before the cache is discarded.
pub(crate) async fn shutdown(&self) -> Result<(), DuckDbError> {
let (response_tx, response_rx) = oneshot::channel();
Expand Down Expand Up @@ -175,7 +206,8 @@ pub fn spawn_actor(
e
))));
}
DbMessage::Wal { response_tx } => {
DbMessage::Wal { response_tx }
| DbMessage::WalForCheckpoint { response_tx, .. } => {
let _ = response_tx.send(Err(DuckDbError::Internal(format!(
"Failed to open: {}",
e
Expand Down Expand Up @@ -257,6 +289,19 @@ pub fn spawn_actor(
let result = handle_wal(&mode, &file_path);
let _ = response_tx.send(result);
}
DbMessage::WalForCheckpoint {
checkpoint_size,
checkpoint_hash,
response_tx,
} => {
let result = handle_wal_for_checkpoint(
&mode,
&file_path,
checkpoint_size,
&checkpoint_hash,
);
let _ = response_tx.send(result);
}
DbMessage::Shutdown { response_tx } => {
shutdown = Some(response_tx);
break;
Expand Down Expand Up @@ -284,6 +329,44 @@ pub fn spawn_actor(
DatabaseHandle { id, tx }
}

fn handle_wal_for_checkpoint(
mode: &StorageMode,
file_path: &Path,
checkpoint_size: u64,
checkpoint_hash: &str,
) -> Result<Option<Vec<u8>>, DuckDbError> {
if !matches!(mode, StorageMode::File(_)) {
return Ok(None);
}
let mut file =
std::fs::File::open(file_path).map_err(|e| DuckDbError::Internal(e.to_string()))?;
if file
.metadata()
.map_err(|e| DuckDbError::Internal(e.to_string()))?
.len()
!= checkpoint_size
{
return Ok(None);
}
// DuckDB can place new blocks in the main file while recording only their
// references in the WAL. Such a WAL cannot replay on the older checkpoint.
let mut hasher = Blake3Hasher::new();
let mut buffer = [0u8; 64 * 1024];
loop {
let read = file
.read(&mut buffer)
.map_err(|e| DuckDbError::Internal(e.to_string()))?;
if read == 0 {
break;
}
hasher.update(&buffer[..read]);
}
if hasher.finalize().to_cid(0x55).to_string() != checkpoint_hash {
return Ok(None);
}
handle_wal(mode, file_path)
}

fn handle_wal(mode: &StorageMode, file_path: &Path) -> Result<Option<Vec<u8>>, DuckDbError> {
if !matches!(mode, StorageMode::File(_)) {
return Ok(None);
Expand Down
Loading