cockroach: github.com/cockroachdb/cockroach/pkg/util/hlc Index | Examples | Files

package hlc

import "github.com/cockroachdb/cockroach/pkg/util/hlc"

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

Examples

Package Files

hlc.go legacy_timestamp.pb.go timestamp.go timestamp.pb.go

Variables

var (
    ErrInvalidLengthLegacyTimestamp = fmt.Errorf("proto: negative length found during unmarshaling")
    ErrIntOverflowLegacyTimestamp   = fmt.Errorf("proto: integer overflow")
)
var (
    // MaxTimestamp is the max value allowed for Timestamp.
    MaxTimestamp = Timestamp{WallTime: math.MaxInt64, Logical: math.MaxInt32}
    // MinTimestamp is the min value allowed for Timestamp.
    MinTimestamp = Timestamp{WallTime: 0, Logical: 1}
)

Timestamp constant values.

var (
    ErrInvalidLengthTimestamp = fmt.Errorf("proto: negative length found during unmarshaling")
    ErrIntOverflowTimestamp   = fmt.Errorf("proto: integer overflow")
)

func UnixNano Uses

func UnixNano() int64

UnixNano returns the local machine's physical nanosecond unix epoch timestamp as a convenience to create a HLC via c := hlc.NewClock(hlc.UnixNano, ...).

type Clock Uses

type Clock struct {
    // 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 Uses

func NewClock(physicalClock func() int64, maxOffset time.Duration) *Clock

NewClock creates a new hybrid logical clock associated with the given physical clock. The logical ts is initialized to 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.

A value of 0 for maxOffset means that clock skew checking, if performed on this clock by RemoteClockMonitor, is disabled.

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.

Code:

// Initialize a new clock, using the local
// physical clock.
c := NewClock(UnixNano, time.Nanosecond)
// Update the state of the hybrid clock.
s := c.Now()
time.Sleep(50 * time.Nanosecond)
t := Timestamp{WallTime: UnixNano()}
// The sanity checks below will usually never be triggered.

if s.Less(t) || !t.Less(s) {
    log.Fatalf(context.Background(), "The later timestamp is smaller than the earlier one")
}

if t.WallTime-s.WallTime > 0 {
    log.Fatalf(context.Background(), "HLC timestamp %d deviates from physical clock %d", s, t)
}

if s.Logical > 0 {
    log.Fatalf(context.Background(), "Trivial timestamp has logical component")
}

fmt.Printf("The Unix Epoch is now approximately %dns old.\n", t.WallTime)

func (*Clock) MaxOffset Uses

func (c *Clock) MaxOffset() time.Duration

MaxOffset returns the maximal clock offset to any node in the cluster.

A value of 0 means offset checking is disabled.

func (*Clock) Now Uses

func (c *Clock) Now() Timestamp

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 Uses

func (c *Clock) PhysicalNow() int64

PhysicalNow returns the local wall time.

func (*Clock) PhysicalTime Uses

func (c *Clock) PhysicalTime() time.Time

PhysicalTime returns a time.Time struct using the local wall time.

func (*Clock) RefreshHLCUpperBound Uses

func (c *Clock) RefreshHLCUpperBound(persistFn func(int64) error, delta int64) error

RefreshHLCUpperBound persists the HLC upper bound and updates the in memory value if the persist succeeds. delta is used to compute the upper bound.

func (*Clock) ResetHLCUpperBound Uses

func (c *Clock) ResetHLCUpperBound(persistFn func(int64) error) error

ResetHLCUpperBound persists a value of 0 as the HLC upper bound which disables upper bound validation

func (*Clock) StartMonitoringForwardClockJumps Uses

func (c *Clock) StartMonitoringForwardClockJumps(
    forwardClockJumpCheckEnabledCh <-chan bool,
    tickerFn func(d time.Duration) *time.Ticker,
    tickCallback func(),
) error

StartMonitoringForwardClockJumps starts a goroutine to update the clock's forwardClockJumpCheckEnabled based on the values pushed in forwardClockJumpCheckEnabledCh.

This also keeps lastPhysicalTime up to date to avoid spurious jump errors.

A nil channel or a value of false pushed in forwardClockJumpCheckEnabledCh disables checking clock jumps between two successive reads of the physical clock.

This should only be called once per clock, and will return an error if called more than once

tickerFn is used to create a new ticker

tickCallback is called whenever maxForwardClockJumpCh or a ticker tick is processed

func (*Clock) Update Uses

func (c *Clock) Update(rt Timestamp) Timestamp

Update takes a hybrid timestamp, usually originating from an event received from another member of a distributed system. The clock is updated and the clock's updated hybrid timestamp is returned. If the remote timestamp exceeds the wall clock time by more than the maximum clock offset, the update is still processed, but a warning is logged. To receive an error response instead of forcing the update in case the remote timestamp is too far into the future, use UpdateAndCheckMaxOffset() instead.

func (*Clock) UpdateAndCheckMaxOffset Uses

func (c *Clock) UpdateAndCheckMaxOffset(rt Timestamp) (Timestamp, error)

UpdateAndCheckMaxOffset is similar to Update, except it returns an error instead of logging a warning and updating the clock's timestamp, in the event that the supplied remote timestamp exceeds the wall clock time by more than the maximum clock offset.

func (*Clock) WallTimeUpperBound Uses

func (c *Clock) WallTimeUpperBound() int64

WallTimeUpperBound returns the in memory value of upper bound to wall time

type LegacyTimestamp Uses

type LegacyTimestamp struct {
    // Holds a wall time, typically a unix epoch time expressed in
    // nanoseconds.
    WallTime int64 `protobuf:"varint,1,opt,name=wall_time,json=wallTime" json:"wall_time"`
    // The logical component captures causality for events whose wall
    // times are equal. It is effectively bounded by (maximum clock
    // skew)/(minimal ns between events) and nearly impossible to
    // overflow.
    Logical int32 `protobuf:"varint,2,opt,name=logical" json:"logical"`
}

LegacyTimestamp is convertible to hlc.Timestamp, but uses the legacy encoding as it is encoded "below raft".

func NewPopulatedLegacyTimestamp Uses

func NewPopulatedLegacyTimestamp(r randyLegacyTimestamp, easy bool) *LegacyTimestamp

func (*LegacyTimestamp) Descriptor Uses

func (*LegacyTimestamp) Descriptor() ([]byte, []int)

func (*LegacyTimestamp) Equal Uses

func (this *LegacyTimestamp) Equal(that interface{}) bool

func (LegacyTimestamp) Less Uses

func (t LegacyTimestamp) Less(s LegacyTimestamp) bool

Less compares two timestamps.

func (*LegacyTimestamp) Marshal Uses

func (m *LegacyTimestamp) Marshal() (dAtA []byte, err error)

func (*LegacyTimestamp) MarshalTo Uses

func (m *LegacyTimestamp) MarshalTo(dAtA []byte) (int, error)

func (*LegacyTimestamp) ProtoMessage Uses

func (*LegacyTimestamp) ProtoMessage()

func (*LegacyTimestamp) Reset Uses

func (m *LegacyTimestamp) Reset()

func (*LegacyTimestamp) Size Uses

func (m *LegacyTimestamp) Size() (n int)

func (LegacyTimestamp) String Uses

func (t LegacyTimestamp) String() string

func (*LegacyTimestamp) Unmarshal Uses

func (m *LegacyTimestamp) Unmarshal(dAtA []byte) error

func (*LegacyTimestamp) XXX_DiscardUnknown Uses

func (m *LegacyTimestamp) XXX_DiscardUnknown()

func (*LegacyTimestamp) XXX_Marshal Uses

func (m *LegacyTimestamp) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*LegacyTimestamp) XXX_Merge Uses

func (dst *LegacyTimestamp) XXX_Merge(src proto.Message)

func (*LegacyTimestamp) XXX_Size Uses

func (m *LegacyTimestamp) XXX_Size() int

func (*LegacyTimestamp) XXX_Unmarshal Uses

func (m *LegacyTimestamp) XXX_Unmarshal(b []byte) error

type ManualClock Uses

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.

func NewManualClock Uses

func NewManualClock(nanos int64) *ManualClock

NewManualClock returns a new instance, initialized with specified timestamp.

func (*ManualClock) Increment Uses

func (m *ManualClock) Increment(incr int64)

Increment atomically increments the manual clock's timestamp.

func (*ManualClock) Set Uses

func (m *ManualClock) Set(nanos int64)

Set atomically sets the manual clock's timestamp.

func (*ManualClock) UnixNano Uses

func (m *ManualClock) UnixNano() int64

UnixNano returns the underlying manual clock's timestamp.

type Timestamp Uses

type Timestamp struct {
    // Holds a wall time, typically a unix epoch time expressed in
    // nanoseconds.
    WallTime int64 `protobuf:"varint,1,opt,name=wall_time,json=wallTime,proto3" json:"wall_time,omitempty"`
    // The logical component captures causality for events whose wall
    // times are equal. It is effectively bounded by (maximum clock
    // skew)/(minimal ns between events) and nearly impossible to
    // overflow.
    Logical int32 `protobuf:"varint,2,opt,name=logical,proto3" json:"logical,omitempty"`
}

Timestamp represents a state of the hybrid logical clock.

func NewPopulatedTimestamp Uses

func NewPopulatedTimestamp(r randyTimestamp, easy bool) *Timestamp

func (Timestamp) Add Uses

func (t Timestamp) Add(wallTime int64, logical int32) Timestamp

Add returns a timestamp with the WallTime and Logical components increased. wallTime is expressed in nanos.

func (Timestamp) AsOfSystemTime Uses

func (t Timestamp) AsOfSystemTime() string

AsOfSystemTime returns a string to be used in an AS OF SYSTEM TIME query.

func (*Timestamp) Backward Uses

func (t *Timestamp) Backward(s Timestamp)

Backward updates the timestamp from the one given, if that moves it backwards in time.

func (Timestamp) Clone Uses

func (t Timestamp) Clone() *Timestamp

Clone return a new timestamp that has the same contents as the receiver.

func (*Timestamp) Descriptor Uses

func (*Timestamp) Descriptor() ([]byte, []int)

func (*Timestamp) Equal Uses

func (this *Timestamp) Equal(that interface{}) bool

func (Timestamp) FloorPrev Uses

func (t Timestamp) FloorPrev() Timestamp

FloorPrev returns a timestamp earlier than the current timestamp. If it can subtract a logical tick without wrapping around, it does so. Otherwise it subtracts a nanosecond from the walltime.

func (*Timestamp) Forward Uses

func (t *Timestamp) Forward(s Timestamp) bool

Forward updates the timestamp from the one given, if that moves it forwards in time. Returns true if the timestamp was adjusted and false otherwise.

func (Timestamp) GoTime Uses

func (t Timestamp) GoTime() time.Time

GoTime converts the timestamp to a time.Time.

func (Timestamp) IsEmpty Uses

func (t Timestamp) IsEmpty() bool

IsEmpty retruns true if t is an empty Timestamp.

func (Timestamp) Less Uses

func (t Timestamp) Less(s Timestamp) bool

Less compares two timestamps.

func (*Timestamp) Marshal Uses

func (m *Timestamp) Marshal() (dAtA []byte, err error)

func (*Timestamp) MarshalTo Uses

func (m *Timestamp) MarshalTo(dAtA []byte) (int, error)

func (Timestamp) Next Uses

func (t Timestamp) Next() Timestamp

Next returns the timestamp with the next later timestamp.

func (Timestamp) Prev Uses

func (t Timestamp) Prev() Timestamp

Prev returns the next earliest timestamp.

func (*Timestamp) ProtoMessage Uses

func (*Timestamp) ProtoMessage()

func (*Timestamp) Reset Uses

func (m *Timestamp) Reset()

func (*Timestamp) Size Uses

func (m *Timestamp) Size() (n int)

func (Timestamp) String Uses

func (t Timestamp) String() string

func (*Timestamp) Unmarshal Uses

func (m *Timestamp) Unmarshal(dAtA []byte) error

func (*Timestamp) XXX_DiscardUnknown Uses

func (m *Timestamp) XXX_DiscardUnknown()

func (*Timestamp) XXX_Marshal Uses

func (m *Timestamp) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Timestamp) XXX_Merge Uses

func (dst *Timestamp) XXX_Merge(src proto.Message)

func (*Timestamp) XXX_Size Uses

func (m *Timestamp) XXX_Size() int

func (*Timestamp) XXX_Unmarshal Uses

func (m *Timestamp) XXX_Unmarshal(b []byte) error

Package hlc imports 11 packages (graph) and is imported by 138 packages. Updated 2019-07-09. Refresh now. Tools for package owners.