Skip to content
Closed
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
79 changes: 67 additions & 12 deletions crates/datastore/src/locking_tx_datastore/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,21 @@ impl<'cs> ReplayCommittedState<'cs> {
// Replaying a whole record again could repeat rows already applied before the failure.
fn with_eviction_retry<T>(&mut self, mut operation: impl FnMut(&mut Self) -> Result<T>) -> Result<T> {
match operation(self) {
Err(error) if error.is_resident_memory_limit() => {
self.trim_resident()?;
operation(self)
}
result => result,
Err(error) if error.is_resident_memory_limit() => {}
result => return result,
}
self.trim_resident()?;
match operation(self) {
Err(error) if error.is_resident_memory_limit() => {}
result => return result,
}
// Replayed transactions were already committed, so they must apply
// even when nothing else can be evicted, e.g. if the limit was lowered
// below the size of tables that must stay in memory. Failing here would
// stop the database from starting at all. The limit still applies to
// new transactions once replay is done.
let _suspension = self.storage.suspend_resident_hard_limit();
operation(self)
}

/// The purpose of this is to rebuild the state of the datastore
Expand Down Expand Up @@ -1305,6 +1314,45 @@ mod tests {
Ok(())
}

#[test]
fn replay_exceeds_memory_limit_when_nothing_can_be_evicted() -> ResultTest<()> {
const LIMIT: u64 = 16 * 1024 * 1024;
let budget = ByteBudget::new(ByteBudgetConfig {
tier: CacheTier::Memory,
low_water_bytes: 0,
soft_limit_bytes: LIMIT,
hard_limit_bytes: LIMIT,
})?;
let mut config = TieredStorageConfig::new_for_test();
config.resident_budget = budget.clone();
let datastore = Locking::bootstrap(Identity::ZERO, config)?;
let mut tx = begin_mut_tx(&datastore);
let mut schema = TableSchema::from_product_type([AlgebraicType::U64].into());
schema.table_name = TableName::for_test("in_memory");
schema.cache_policy = TableCachePolicy::RequiredInMemory;
let table = tx.create_table(schema)?;
commit(&datastore, tx)?;
let row = product![42_u64];

{
let mut replay = ReplayCommittedState::new(datastore.committed_state.write());
let schema = replay.schema_for_table(table)?;
// Fill the budget. Every resident page belongs to a table that must
// stay in memory, so eviction cannot make room for the replayed row.
let reservation = budget.acquire(LIMIT - budget.usage().accounted_bytes)?;
replay.replay_insert(table, &schema, &row)?;
assert!(budget.usage().accounted_bytes > LIMIT);
// The limit is enforced again after the replayed operation.
assert!(budget.acquire(0).is_err());
drop(reservation);
replay.rebuild_state_after_replay()?;
}
let tx = begin_mut_tx(&datastore);
assert_eq!(all_rows(&datastore, &tx, table), [row]);
let _ = datastore.rollback_mut_tx(tx);
Ok(())
}

fn append_committed_transaction(log: &mut MemoryCommitlog, data: &TxData) -> ResultTest<()> {
use spacetimedb_commitlog::payload::txdata::{Mutations, Ops};

Expand Down Expand Up @@ -1764,18 +1812,25 @@ mod tests {
let state = datastore.committed_state.write();
let mut state = ReplayCommittedState::new(state);
let schema = state.schema_for_table(table_id)?;
assert!(state.replay_insert(table_id, &schema, &row).is_err());
// Replay retries with the memory limit suspended, so the insert
// succeeds, but only after the same pinning failure twice.
state.replay_insert(table_id, &schema, &row)?;
// Remove memory pressure before inspecting state, so subsequent
// reads can reveal leaked rows instead of failing to load them.
drop(reservation);
let table = &state.tables[&table_id];
assert_eq!(table.num_rows(), 0);
assert!(table.scan_all_row_ptrs()?.is_empty());
assert_eq!(state.storage.blobs().num_blobs(), 0);
// A subsequent replay operation must not see a ghost row from
// the failed operation, or inherit its blob references.
// The failed attempts must leave neither a ghost row nor extra
// blob references behind.
assert_eq!(table.num_rows(), 1);
assert_eq!(table.scan_all_row_ptrs()?.len(), 1);
let mut blob_uses = Vec::new();
state.storage.blobs().try_for_each::<ObjectStoreError>(|_, uses, _| {
blob_uses.push(uses.get() as u64);
Ok(())
})?;
assert_eq!(blob_uses, [1, 1]);
state.replay_insert(table_id, &schema, &product!["small", "row"])?;
assert_eq!(state.tables[&table_id].num_rows(), 1);
assert_eq!(state.tables[&table_id].num_rows(), 2);
} else {
// An insertion error must undo its own physical writes while
// the transaction remains open. Rolling back here would hide
Expand Down
71 changes: 69 additions & 2 deletions crates/table/src/tiered/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ impl ByteBudget {

pub fn new(config: ByteBudgetConfig) -> Result<Self, ConfigError> {
config.validate_then(|config| Self {
state: Arc::new(Mutex::new(ByteBudgetState { accounted_bytes: 0 })),
state: Arc::new(Mutex::new(ByteBudgetState {
accounted_bytes: 0,
hard_limit_suspensions: 0,
})),
config,
metrics: Default::default(),
})
Expand All @@ -58,7 +61,7 @@ impl ByteBudget {
pub fn acquire(&self, bytes: u64) -> Result<BudgetPermit, BudgetExceeded> {
let mut state = self.state.lock().unwrap();
if let Some(accounted_bytes) = state.accounted_bytes.checked_add(bytes)
&& accounted_bytes <= self.config.hard_limit_bytes
&& (accounted_bytes <= self.config.hard_limit_bytes || state.hard_limit_suspensions > 0)
{
state.accounted_bytes = accounted_bytes;
Ok(BudgetPermit {
Expand Down Expand Up @@ -86,6 +89,38 @@ impl ByteBudget {
bytes,
}
}

/// Let [`Self::acquire`] exceed the hard limit until the returned guard is dropped.
///
/// Bytes acquired meanwhile still count toward usage,
/// so the limit applies again to acquisitions after the guard is dropped.
pub fn suspend_hard_limit(&self) -> HardLimitSuspension {
let mut state = self.state.lock().unwrap();
state.hard_limit_suspensions = state
.hard_limit_suspensions
.checked_add(1)
.expect("hard limit suspensions overflow");
HardLimitSuspension {
state: self.state.clone(),
}
}
}

/// Guard returned by [`ByteBudget::suspend_hard_limit`].
#[derive(Debug)]
#[must_use = "the hard limit is enforced again as soon as the suspension is dropped"]
pub struct HardLimitSuspension {
state: Arc<Mutex<ByteBudgetState>>,
}

impl Drop for HardLimitSuspension {
fn drop(&mut self) {
let mut state = self.state.lock().unwrap();
state.hard_limit_suspensions = state
.hard_limit_suspensions
.checked_sub(1)
.expect("hard limit suspensions underflow");
}
}

impl TryFrom<ByteBudgetConfig> for ByteBudget {
Expand Down Expand Up @@ -133,6 +168,8 @@ impl ByteBudgetConfig {
#[derive(Debug)]
pub struct ByteBudgetState {
accounted_bytes: u64,
/// Number of live [`HardLimitSuspension`]s.
hard_limit_suspensions: u32,
}

impl ByteBudgetUsage {
Expand Down Expand Up @@ -316,5 +353,35 @@ mod tests {
drop(permit);
prop_assert_eq!(budget.usage().accounted_bytes(), 0);
}

#[test]
fn suspended_hard_limit_admits_acquisitions_and_is_enforced_after_drop(
hard_limit_bytes in 0_u64..1_000_000,
excess_bytes in 1_u64..1_000_000,
) {
let budget = ByteBudget::new(ByteBudgetConfig {
tier: CacheTier::Memory,
low_water_bytes: 0,
soft_limit_bytes: hard_limit_bytes,
hard_limit_bytes,
}).unwrap();
let over_limit_bytes = hard_limit_bytes + excess_bytes;
prop_assert!(budget.acquire(over_limit_bytes).is_err());

let outer = budget.suspend_hard_limit();
let inner = budget.suspend_hard_limit();
drop(inner);
// The outer suspension is still live.
let permit = budget.acquire(over_limit_bytes).unwrap();
prop_assert_eq!(budget.usage().accounted_bytes(), over_limit_bytes);
drop(outer);

// Usage acquired while suspended still counts against the limit.
let error = budget.acquire(0).unwrap_err();
prop_assert_eq!(error.accounted_bytes, over_limit_bytes);
drop(permit);
prop_assert_eq!(budget.usage().accounted_bytes(), 0);
prop_assert!(budget.acquire(hard_limit_bytes).is_ok());
}
}
}
7 changes: 6 additions & 1 deletion crates/table/src/tiered/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod storage;

pub use self::{
blob_set::{BlobHandle, BlobMap, BlobReadSet, BlobSet},
budget::{BudgetExceeded, BudgetPermit, ByteBudget, ByteBudgetConfig, ByteBudgetUsage},
budget::{BudgetExceeded, BudgetPermit, ByteBudget, ByteBudgetConfig, ByteBudgetUsage, HardLimitSuspension},
error::{BlobError, ObjectStoreError, PageError, TrimError},
map::ObjectKey,
mem::ResidentObjects,
Expand Down Expand Up @@ -157,6 +157,11 @@ impl TieredDatabaseStorage {
self.cache.metrics()
}

/// Let resident pages and blobs exceed the memory limit until the returned guard is dropped.
pub fn suspend_resident_hard_limit(&self) -> HardLimitSuspension {
self.cache.budget().suspend_hard_limit()
}

pub fn pages(&self) -> &Arc<PageManager> {
&self.pages
}
Expand Down
Loading