From 5a72a2158f341e0cc9ffc6e2264f5bf6d8b5e9c3 Mon Sep 17 00:00:00 2001 From: Liang Xu <24490353+lx3133584@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:47:03 +0800 Subject: [PATCH] graph: keep a batch's valid blocks when an append is refused `Batch::append_inner` moved `block_ptr`, `block_times` and `firehose_cursor` onto `self` before appending the row groups, and only `mods.append` can refuse the batch. A refused append therefore left the batch pointing at the block it had rejected while holding only the changes of the blocks before it. `append` then set `self.error`, and the write queue refuses to write a batch that carries one, so the valid blocks were dropped and the failure poisoned the queue. A failed deployment's head depended on the batching settings rather than on its data: with batching on it stayed at the start of the batch, with `GRAPH_STORE_WRITE_BATCH_DURATION=0` it reached the block before the failure. The row groups now go first, before any other field moves, so a refused append leaves the batch exactly as it was. With nothing half-applied there is no indeterminate state to guard against, so `append` no longer records the error and the batch stays writable: writing it afterwards records the blocks that were valid and the head stops where the failure did. The test appends an immutable insert across two blocks, which `append_row` refuses, and asserts the pointer stays on the first block and that the batch carries no error. Both assertions fail without the change. Signed-off-by: Liang Xu <24490353+lx3133584@users.noreply.github.com> --- graph/src/components/store/write.rs | 63 ++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/graph/src/components/store/write.rs b/graph/src/components/store/write.rs index 1a29714e51e..029941a4ff2 100644 --- a/graph/src/components/store/write.rs +++ b/graph/src/components/store/write.rs @@ -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); @@ -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 } @@ -1054,8 +1056,10 @@ 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, @@ -1063,8 +1067,9 @@ mod test { 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])]) { @@ -1341,6 +1346,46 @@ mod test { } } + fn make_batch(number: BlockNumber, mods: Vec) -> 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());