Skip to content

Commit

Permalink
CBG-4440: make hlc public for use in blip client tester in sync gatew…
Browse files Browse the repository at this point in the history
…ay (#45)
  • Loading branch information
gregns1 authored Feb 26, 2025
1 parent c8f7248 commit 3b9ac15
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@ func (c *Collection) getLastCas(q queryable) (cas CAS, err error) {
}

// getLastTimestamp returns the last timestamp assigned to any doc in bucket. Returns 0 in the case of no cas values assigned.
func (bucket *Bucket) getLastTimestamp() timestamp {
var lastTimestamp timestamp
func (bucket *Bucket) getLastTimestamp() Timestamp {
var lastTimestamp Timestamp
row := bucket.db().QueryRow("SELECT lastCas FROM bucket")
_ = scan(row, &lastTimestamp)
return timestamp(lastTimestamp)
return Timestamp(lastTimestamp)
}

// Updates the collection's and the bucket's lastCas.
Expand Down
20 changes: 10 additions & 10 deletions hlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"time"
)

var hlc *hybridLogicalClock
var hlc *HybridLogicalClock

func init() {
hlc = NewHybridLogicalClock(0)
}

type timestamp uint64
type Timestamp uint64

// hybridLogicalClock is a hybrid logical clock implementation for rosmar that produces timestamps that will always be increasing regardless of clock changes.
type hybridLogicalClock struct {
// HybridLogicalClock is a hybrid logical clock implementation for rosmar that produces timestamps that will always be increasing regardless of clock changes.
type HybridLogicalClock struct {
clock clock
highestTime uint64
mutex sync.Mutex
Expand All @@ -42,23 +42,23 @@ func (c *systemClock) getTime() uint64 {
}

// NewHybridLogicalClock returns a new HLC from a previously initialized time.
func NewHybridLogicalClock(lastTime timestamp) *hybridLogicalClock {
return &hybridLogicalClock{
func NewHybridLogicalClock(lastTime Timestamp) *HybridLogicalClock {
return &HybridLogicalClock{
highestTime: uint64(lastTime),
clock: &systemClock{},
}
}

func (c *hybridLogicalClock) updateLatestTime(lastTime timestamp) {
func (c *HybridLogicalClock) updateLatestTime(lastTime Timestamp) {
c.mutex.Lock()
defer c.mutex.Unlock()
if uint64(lastTime) > c.highestTime {
c.highestTime = uint64(lastTime)
}
}

// Now returns the next time represented in nanoseconds. This can be the current timestamp, or if multiple occur in the same nanosecond, an increasing timestamp.
func (c *hybridLogicalClock) Now() timestamp {
// Now returns the next time represented in nanoseconds. This can be the current Timestamp, or if multiple occur in the same nanosecond, an increasing Timestamp.
func (c *HybridLogicalClock) Now() Timestamp {
c.mutex.Lock()
defer c.mutex.Unlock()
physicalTime := c.clock.getTime() &^ 0xFFFF // round to 48 bits
Expand All @@ -67,5 +67,5 @@ func (c *hybridLogicalClock) Now() timestamp {
} else {
c.highestTime = physicalTime
}
return timestamp(c.highestTime)
return Timestamp(c.highestTime)
}
32 changes: 16 additions & 16 deletions hlc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ import (
)

func TestHybridLogicalClockNow(t *testing.T) {
clock := hybridLogicalClock{clock: &systemClock{}}
clock := HybridLogicalClock{clock: &systemClock{}}
timestamp1 := clock.Now()
timestamp2 := clock.Now()
require.Greater(t, timestamp2, timestamp1)
}

func generateTimestamps(wg *sync.WaitGroup, clock *hybridLogicalClock, n int, result chan []timestamp) {
func generateTimestamps(wg *sync.WaitGroup, clock *HybridLogicalClock, n int, result chan []Timestamp) {
defer wg.Done()
timestamps := make([]timestamp, n)
timestamps := make([]Timestamp, n)
for i := 0; i < n; i++ {
timestamps[i] = clock.Now()
}
result <- timestamps
}

func TestHLCNowConcurrent(t *testing.T) {
clock := hybridLogicalClock{clock: &systemClock{}}
clock := HybridLogicalClock{clock: &systemClock{}}
goroutines := 100
timestampCount := 100

wg := sync.WaitGroup{}
results := make(chan []timestamp)
results := make(chan []Timestamp)
for i := 0; i < goroutines; i++ {
wg.Add(1)
go generateTimestamps(&wg, &clock, timestampCount, results)
Expand All @@ -50,7 +50,7 @@ func TestHLCNowConcurrent(t *testing.T) {
wg.Wait()
doneChan <- struct{}{}
}()
allTimestamps := make([]timestamp, 0, goroutines*timestampCount)
allTimestamps := make([]Timestamp, 0, goroutines*timestampCount)
loop:
for {
select {
Expand All @@ -60,7 +60,7 @@ loop:
break loop
}
}
uniqueTimestamps := make(map[timestamp]struct{})
uniqueTimestamps := make(map[Timestamp]struct{})
for _, timestamp := range allTimestamps {
if _, ok := uniqueTimestamps[timestamp]; ok {
t.Errorf("Timestamp %d is not unique", timestamp)
Expand All @@ -79,32 +79,32 @@ func (c *fakeClock) getTime() uint64 {

func TestHLCReverseTime(t *testing.T) {
clock := &fakeClock{}
hlc := hybridLogicalClock{clock: clock}
hlc := HybridLogicalClock{clock: clock}
startTime := uint64(1000000) // 1 second
clock.time = startTime
require.Equal(t, timestamp(0xf0000), hlc.Now())
require.Equal(t, timestamp(0xf0001), hlc.Now())
require.Equal(t, Timestamp(0xf0000), hlc.Now())
require.Equal(t, Timestamp(0xf0001), hlc.Now())

// reverse time no counter
clock.time = 0
require.Equal(t, timestamp(0xf0002), hlc.Now())
require.Equal(t, Timestamp(0xf0002), hlc.Now())

// reset time to normal
clock.time = startTime
require.Equal(t, timestamp(0xf0003), hlc.Now())
require.Equal(t, Timestamp(0xf0003), hlc.Now())

// reverse time again
clock.time = 1
require.Equal(t, timestamp(0xf0004), hlc.Now())
require.Equal(t, Timestamp(0xf0004), hlc.Now())

// jump to a value we had previously
clock.time = startTime * 2
require.Equal(t, timestamp(0x1e0000), hlc.Now())
require.Equal(t, timestamp(0x1e0001), hlc.Now())
require.Equal(t, Timestamp(0x1e0000), hlc.Now())
require.Equal(t, Timestamp(0x1e0001), hlc.Now())

// continue forward
clock.time *= 2
require.Equal(t, timestamp(0x3d0000), hlc.Now())
require.Equal(t, Timestamp(0x3d0000), hlc.Now())

}

Expand Down

0 comments on commit 3b9ac15

Please sign in to comment.