VectorClock

class VectorClock<NodeId : Any, NodeTime : Comparable<NodeTime>>(localNodeId: NodeId, initialTime: VectorTimestamp<NodeId, NodeTime>, incrementNodeTime: (NodeTime) -> NodeTime, onNewTime: suspend (VectorTimestamp<NodeId, NodeTime>) -> Unit = {}) : LogicalClock<VectorTimestamp<NodeId, NodeTime>>

A generic vector clock.

Vector clocks generate timestamps with precise information about which events were known at the time that an event occurred. Their disadvantage is that they are large and computationally expensive: their size, as well as the computational complexity of comparing two instances, are proportional to the number of nodes in the system.

This implementation is thread-safe.

Example instantiation where node IDs are of type String and logical time is expressed as a Long:

fun stringLongVectorClock(
localNodeId: String,
initialTime: VectorTimestamp<String, Long>,
onNewTime: suspend (VectorTimestamp<String, Long>) -> Unit = {}
): VectorClock<String, Long> = VectorClock(
localNodeId,
initialTime,
Long::inc,
onNewTime
)

Constructors

Link copied to clipboard
constructor(localNodeId: NodeId, initialTime: VectorTimestamp<NodeId, NodeTime>, incrementNodeTime: (NodeTime) -> NodeTime, onNewTime: suspend (VectorTimestamp<NodeId, NodeTime>) -> Unit = {})

Properties

Link copied to clipboard

The current logical timestamp of this clock.

Functions

Link copied to clipboard
open suspend override fun tick(): VectorTimestamp<NodeId, NodeTime>

Returns a logical timestamp that is greater than the current logical timestamp of this clock and updates the clock's internal state accordingly.

Link copied to clipboard
open suspend override fun tock(externalTime: VectorTimestamp<NodeId, NodeTime>): VectorTimestamp<NodeId, NodeTime>

Updates the internal state of the clock to reflect the occurrence of an external event with the given logical timestamp.