Documentation ¶
Overview ¶
Package hlc implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at http://www.cse.buffalo.edu/tech-reports/2014-04.pdf.
Index ¶
- func UnixNano() int64
- type Clock
- func (c *Clock) MaxOffset() time.Duration
- func (c *Clock) Now() (result proto.Timestamp)
- func (c *Clock) PhysicalNow() int64
- func (c *Clock) PhysicalTime() time.Time
- func (c *Clock) SetMaxOffset(delta time.Duration)
- func (c *Clock) Timestamp() proto.Timestamp
- func (c *Clock) Update(rt proto.Timestamp) (result proto.Timestamp, err error)
- type ManualClock
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Clock ¶
type Clock struct { // Clock contains a mutex used to lock the below // fields while methods operate on them. sync.Mutex // contains filtered or unexported fields }
Clock is a hybrid logical clock. Objects of this type model causality while maintaining a relation to physical time. Roughly speaking, timestamps consist of the largest wall clock time among all events, and a logical clock that ticks whenever an event happens in the future of the local physical clock. The data structure is thread safe and thus can safely be shared by multiple goroutines.
See NewClock for details.
func NewClock ¶
NewClock creates a new hybrid logical clock associated with the given physical clock, initializing both wall time and logical time with zero.
The physical clock is typically given by the wall time of the local machine in unix epoch nanoseconds, using hlc.UnixNano. This is not a requirement.
Example ¶
ExampleNewClock shows how to create a new hybrid logical clock based on the local machine's physical clock. The sanity checks in this example will, of course, not fail and the output will be the age of the Unix epoch in nanoseconds.
// Initialize a new clock, using the local // physical clock. c := NewClock(UnixNano) // Update the state of the hybrid clock. s := c.Now() time.Sleep(50 * time.Nanosecond) t := proto.Timestamp{WallTime: UnixNano()} // The sanity checks below will usually never be triggered. // Timestamp implements the util.Ordered interface. if s.Less(t) || !t.Less(s) { log.Fatalf("The later timestamp is smaller than the earlier one") } if t.WallTime-s.WallTime > 0 { log.Fatalf("HLC timestamp %d deviates from physical clock %d", s, t) } if s.Logical > 0 { log.Fatalf("Trivial timestamp has logical component") } fmt.Printf("The Unix Epoch is now approximately %dns old.\n", t.WallTime)
Output:
func (*Clock) MaxOffset ¶
MaxOffset returns the maximal offset allowed. A value of 0 means offset checking is disabled. See SetMaxOffset for details.
func (*Clock) Now ¶
Now returns a timestamp associated with an event from the local machine that may be sent to other members of the distributed network. This is the counterpart of Update, which is passed a timestamp received from another member of the distributed network.
func (*Clock) PhysicalNow ¶
PhysicalNow returns the local wall time. It corresponds to the physicalClock provided at instantiation. For a timestamp value, use Now() instead.
func (*Clock) PhysicalTime ¶
PhysicalTime returns a time.Time struct using the local wall time.
func (*Clock) SetMaxOffset ¶
SetMaxOffset sets the maximal offset of the physical clock from the cluster. It is used to set the max offset a call to Update may cause and to ensure an upperbound on timestamp WallTime in transactions. A well-chosen value is large enough to ignore a reasonable amount of clock skew but will prevent ill-configured nodes from dramatically skewing the wall time of the clock into the future.
A value of zero disables all safety features. The default value for a new instance is zero.
func (*Clock) Timestamp ¶
Timestamp returns a copy of the clock's current timestamp, without performing a clock adjustment.
func (*Clock) Update ¶
Update takes a hybrid timestamp, usually originating from an event received from another member of a distributed system. The clock is updated and the hybrid timestamp associated to the receipt of the event returned. An error may only occur if offset checking is active and the remote timestamp was rejected due to clock offset, in which case the state of the clock will not have been altered. To timestamp events of local origin, use Now instead.
type ManualClock ¶
type ManualClock struct {
// contains filtered or unexported fields
}
ManualClock is a convenience type to facilitate creating a hybrid logical clock whose physical clock is manually controlled. ManualClock is thread safe.
Example ¶
ExampleManualClock shows how a manual clock can be used as a physical clock. This is useful for testing.
m := NewManualClock(10) c := NewClock(m.UnixNano) c.Now() if c.Timestamp().WallTime != 10 { log.Fatalf("manual clock error") } m.Set(20) c.Now() if c.Timestamp().WallTime != 20 { log.Fatalf("manual clock error") }
Output:
func NewManualClock ¶
func NewManualClock(nanos int64) *ManualClock
NewManualClock returns a new instance, initialized with specified timestamp.
func (*ManualClock) Increment ¶
func (m *ManualClock) Increment(incr int64)
Increment atomically increments the manual clock's timestamp.
func (*ManualClock) Set ¶
func (m *ManualClock) Set(nanos int64)
Set atomically sets the manual clock's timestamp.
func (*ManualClock) UnixNano ¶
func (m *ManualClock) UnixNano() int64
UnixNano returns the underlying manual clock's timestamp.