Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tokio-util = "0.7" # For CancellationToken
serde = { version = "1", features = ["derive"] }
serde_json = "1"
async-trait = "0.1"
uuid = { version = "1", features = ["v4"] }
tracing = { version = "0.1", features = ["std"] }
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter", "json"] }
semver = "1"
Expand Down
41 changes: 9 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2769,11 +2769,11 @@ impl OrchestrationContext {
}
}

/// Generate a new deterministic GUID.
/// Generate a new GUID.
///
/// This schedules a built-in activity that generates a unique identifier.
/// The GUID is deterministic across replays (the same value is returned
/// when the orchestration replays).
/// This schedules a built-in activity that generates a random version-4
/// UUID. The value is recorded in history, so the same GUID is returned
/// when the orchestration replays.
///
/// # Example
///
Expand Down Expand Up @@ -2871,36 +2871,13 @@ impl OrchestrationContext {
}
}

/// Generate a deterministic GUID for use in orchestrations.
/// Generate a GUID for use in orchestrations.
///
/// Uses timestamp + thread-local counter for uniqueness.
/// Returns a random version-4 UUID. The value is produced once inside the
/// built-in syscall activity and recorded in history, so the same value is
/// returned on every replay.
pub(crate) fn generate_guid() -> String {
use std::time::{SystemTime, UNIX_EPOCH};

let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);

// Thread-local counter for uniqueness within the same timestamp
thread_local! {
static COUNTER: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
}
let counter = COUNTER.with(|c| {
let val = c.get();
c.set(val.wrapping_add(1));
val
});

// Format as UUID-like string
format!(
"{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
(timestamp >> 96) as u32,
((timestamp >> 80) & 0xFFFF) as u16,
(counter & 0xFFFF) as u16,
((timestamp >> 64) & 0xFFFF) as u16,
(timestamp & 0xFFFFFFFFFFFF) as u64
)
uuid::Uuid::new_v4().to_string()
}

impl OrchestrationContext {
Expand Down
Loading