Skip to main content

CdcBuffer

Struct CdcBuffer 

Source
pub struct CdcBuffer { /* private fields */ }
Expand description

CDC event buffer — circular buffer of change events.

Splits the “next LSN” counter (write-contended on every emit) from the event ring (short-hold push/pop) so that concurrent emitters don’t serialise on a single RwLock. The previous design used one RwLock<CdcState> that turned every insert into a write-lock acquire, capping 16-way concurrent writes at ~1000 ops/s (each writer paid ~1ms queueing for the same mutex even though the work it guarded was a one-line VecDeque push).

New layout:

  • LSN is an AtomicU64, assigned with fetch_add(1). Zero contention.
  • Events are guarded by a parking_lot::Mutex<VecDeque>. The critical section is pop_front (if full) + push_back — microseconds at most, parking-free at low contention.

Readers (poll, current_lsn, stats) take the same mutex briefly; they’re cold paths compared to the write hot path.

Implementations§

Source§

impl CdcBuffer

Source

pub fn new(max_size: usize) -> CdcBuffer

Create a new CDC buffer with maximum capacity.

Source

pub fn emit( &self, operation: ChangeOperation, collection: &str, entity_id: u64, entity_kind: &str, ) -> u64

Emit a change event into the buffer. changed_columns defaults to None for backwards compatibility; call sites that have a damage vector available should use Self::emit_with_columns instead.

Source

pub fn emit_with_columns( &self, operation: ChangeOperation, collection: &str, entity_id: u64, entity_kind: &str, changed_columns: Option<Vec<String>>, ) -> u64

Emit a change event with an optional list of column names that were affected. Use from update paths that have already computed a RowDamageVector so CDC consumers can filter by touched column without re-diffing.

Source

pub fn emit_batch_same_collection<I>( &self, operation: ChangeOperation, collection: &str, entity_kind: &str, entity_ids: I, ) -> Vec<u64>

Emit many same-collection events with one LSN reservation and one ring-buffer lock. This is used by bulk insert paths that do not need per-row logical-WAL records.

Source

pub fn emit_kv( &self, operation: ChangeOperation, collection: &str, key: &str, entity_id: u64, before: Option<Value>, after: Option<Value>, ) -> u64

Emit a committed logical KV event into the same CDC ring used by result-cache invalidation and /changes consumers.

Source

pub fn poll(&self, since_lsn: u64, max_count: usize) -> Vec<ChangeEvent>

Poll for events since a given LSN.

Source

pub fn current_lsn(&self) -> u64

Get the current (latest) LSN.

Source

pub fn set_current_lsn(&self, lsn: u64)

Restore the LSN cursor after process restart. Only advances; never rewinds. Under concurrent emit this is guarded by a compare-exchange loop.

Source

pub fn oldest_lsn(&self) -> Option<u64>

Get the oldest available LSN (or None if empty).

Source

pub fn stats(&self) -> CdcStats

Get buffer stats (single lock acquisition — no deadlock risk).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more