Skip to main content

Module clock

Module clock 

Source
Expand description

Deterministic generation clock.

Synthetic generation must be byte-identical across runs with the same (config, seed). Many records stamp created_at / updated_at / generated_at with wall-clock time, which breaks that guarantee. This module provides a process-global deterministic epoch: while it is set, now returns a fixed, seed-derived timestamp instead of the wall clock, so every timestamp that routes through now becomes reproducible.

The epoch is stored in a global atomic (not a thread_local!, unlike crate::serde_decimal) because generation fans out across worker threads under parallel = true; a thread-local set on the main thread would not be visible to those workers.

§Usage

The orchestrator wraps a generation run in a DeterministicClockGuard built from the run’s seed; on drop the wall clock is restored. Outside a guard, now is exactly Utc::now().

§Limitation

The epoch is process-global, so two generation runs executing concurrently in the same process would share it. CLI generation is sequential; callers that generate concurrently in-process (e.g. a server) should serialize the deterministic-clock scope or accept wall-clock timestamps.

Structs§

DeterministicClockGuard
RAII guard: sets the deterministic epoch on construction and restores the wall clock on drop. Hold it for the lifetime of a generation run.

Functions§

epoch_from_seed
Derive a deterministic epoch from a run seed and the config start date.
is_deterministic
Whether a deterministic epoch is currently active.
next_document_id
A document id for a model constructor: a seeded sequential UUID when the deterministic context is active, otherwise Uuid::now_v7(). Use this in place of Uuid::now_v7() in generation model constructors.
now
The current timestamp: the deterministic epoch if one is set, else the real wall clock. Use this in generation code instead of Utc::now().
set_deterministic_epoch
Set (or clear) the process-global deterministic epoch.
set_deterministic_uuids
Activate (or clear) the deterministic UUID fallback used by model constructors. Some(seed) resets the counter and makes next_document_id return seeded sequential UUIDs; None restores Uuid::now_v7().