pub struct Batch {
pub shard_id: u16,
pub events: Vec<InternalEvent>,
pub sequence_start: u64,
pub process_nonce: u64,
}Expand description
A batch of events for adapter dispatch.
Batches are formed by shard workers and contain strictly ordered events from a single shard.
Fields§
§shard_id: u16The shard this batch belongs to.
events: Vec<InternalEvent>Events in insertion order.
sequence_start: u64Sequence number of the first event in this batch. Used for idempotent retry handling.
process_nonce: u64Per-process nonce sampled once at process start. Adapters
that persist (shard_id, sequence_start) for dedup
(JetStream Nats-Msg-Id, Redis stream MAXLEN keys, etc.)
must include this in the dedup key — otherwise a producer
that restarts within the backend’s dedup window collides
with its prior incarnation on (shard, 0, 0) and the new
batches are silently discarded as duplicates.
BatchWorker::next_sequence is process-local and resets to
zero on restart; the nonce is the global discriminator that
makes the composite key globally unique across restarts
even though the per-process counter is not durable.
Implementations§
Source§impl Batch
impl Batch
Sourcepub fn new(
shard_id: u16,
events: Vec<InternalEvent>,
sequence_start: u64,
) -> Self
pub fn new( shard_id: u16, events: Vec<InternalEvent>, sequence_start: u64, ) -> Self
Create a new batch using the per-process nonce
(batch_process_nonce). Convenience for tests and for
callers that don’t thread a custom producer nonce through.
Production paths constructed via the bus go through
Self::with_nonce with the bus’s loaded
producer_nonce_path value so retries dedup across
process restart.
Sourcepub fn with_nonce(
shard_id: u16,
events: Vec<InternalEvent>,
sequence_start: u64,
producer_nonce: u64,
) -> Self
pub fn with_nonce( shard_id: u16, events: Vec<InternalEvent>, sequence_start: u64, producer_nonce: u64, ) -> Self
Create a new batch with an explicit producer nonce. Used by
the bus’s BatchWorker and remove_shard_internal’s
stranded-flush so adapters keying dedup on
(producer_nonce, shard, sequence_start, i) see the same
nonce across process restart when the bus is configured
with a producer_nonce_path.
A producer_nonce == 0 is coerced to 1 to preserve the
non-zero invariant that batch_process_nonce and
dedup_state::PersistentProducerNonce::create_new already
uphold (each generates non-zero u64s and re-rolls on the
astronomical 1-in-2^64 zero draw).
The zero coercion is defense-in-depth against future
codecs: a downstream caller that constructs a
Batch::with_nonce(..., 0) directly (e.g. tests, hand-built
fixtures) would otherwise emit dedup_id keys starting
0: — collision-prone with any future codec that reserves
0 as “no nonce, use the legacy path.” Today’s
adapter/jetstream.rs::on_batch just formats
process_nonce as {:x} with no special-casing, so the
hazard is latent rather than active. Coercing to 1 keeps
the invariant that every shipped batch has a non-zero
producer nonce regardless of caller hygiene.