Skip to content
Open
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
63 changes: 54 additions & 9 deletions graph/src/components/store/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,14 @@ impl Batch {
));
}

// The row groups are the only part that can refuse this batch, so they
// go first: a batch whose append fails must keep describing the blocks
// it already holds, and advancing the pointer before the rows are
// accepted would leave it naming a block it has no changes for.
self.mods.append(batch.mods)?;
self.block_ptr = batch.block_ptr;
self.block_times.append(&mut batch.block_times);
self.firehose_cursor = batch.firehose_cursor;
self.mods.append(batch.mods)?;
self.data_sources.append(batch.data_sources);
self.deterministic_errors
.append(&mut batch.deterministic_errors);
Expand All @@ -860,14 +864,12 @@ impl Batch {
/// same effect as writing `self` first and then `batch` in separate
/// transactions.
///
/// When this method returns an `Err`, the batch is marked as not
/// healthy by setting `self.error` to `Some(_)` and must not be written
/// as it will be in an indeterminate state.
/// When this method returns an `Err`, nothing of `batch` is held: the row
/// groups are appended before any other field moves, so a refused batch
/// leaves this one exactly as it was and it can still be written. The
/// caller reports the failure and stops feeding this batch.
pub fn append(&mut self, batch: Batch) -> Result<(), StoreError> {
let res = self.append_inner(batch);
if let Err(e) = &res {
self.error = Some(e.clone());
}
self.weigh();
res
}
Expand Down Expand Up @@ -1054,17 +1056,20 @@ mod test {
use std::sync::Arc;

use crate::{
blockchain::{BlockPtr, BlockTime, block_stream::FirehoseCursor},
components::store::{
BlockNumber, EntityType, StoreError, write::EntityModification, write::EntityOp,
BlockHash, BlockNumber, EntityType, StoreError, write::EntityModification,
write::EntityOp,
},
data::{store::Id, value::Word},
entity,
prelude::DeploymentHash,
schema::InputSchema,
};
use lazy_static::lazy_static;
use slog::Logger;

use super::{LastMod, RowGroup};
use super::{Batch, LastMod, RowGroup};

#[track_caller]
fn check_runs(values: &[usize], blocks: &[BlockNumber], exp: &[(BlockNumber, &[usize])]) {
Expand Down Expand Up @@ -1341,6 +1346,46 @@ mod test {
}
}

fn make_batch(number: BlockNumber, mods: Vec<EntityModification>) -> Batch {
let hash = BlockHash(vec![number as u8].into());

Batch::new(
BlockPtr::new(hash, number),
BlockTime::NONE,
FirehoseCursor::None,
mods,
vec![],
vec![],
vec![],
false,
Logger::root(slog::Discard, slog::o!()),
)
.unwrap()
}

// A failed append must leave the batch describing the last block it actually
// holds. Its blocks are valid and must still be written; moving the pointer to
// the block whose append failed would make it claim changes it does not have.
#[test]
fn failed_append_keeps_the_block_pointer() {
let mut batch = make_batch(1, vec![make_insert(&IMM_THING_TYPE, "imm", 1)]);
assert_eq!(1, batch.block_ptr.number);

// Same immutable entity at block 2, which append_row refuses across blocks
let refused = make_batch(2, vec![make_insert(&IMM_THING_TYPE, "imm", 2)]);
assert!(batch.append(refused).is_err());

assert_eq!(
1, batch.block_ptr.number,
"the batch still holds block 1's changes, so its pointer must stay at 1"
);
assert!(
batch.error.is_none(),
"a batch that holds only valid blocks must stay writable: carrying an \
error makes the write queue poison itself instead of writing them"
);
}

#[test]
fn append_row_immutable_default_rejects_cross_block_duplicate() {
let mut group = RowGroup::new(IMM_THING_TYPE.clone());
Expand Down