Skip to content

Commit

Permalink
follow-up
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Aug 27, 2023
1 parent e08268a commit 301e4ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
6 changes: 1 addition & 5 deletions xact/xreg/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ var (
// "best-effort ID" - to independently and locally generate globally unique xaction ID
func GenBEID(div uint64, tag string) (beid string) {
now := uint64(time.Now().UnixNano() - MyTime.Load() + PrimeTime.Load())
if div&1 == 0 {
div++
}
rem := now % div
val := now - rem
val := now / div
val ^= xxhash.ChecksumString64S(tag, val)

beid = cos.GenBEID(val)
Expand Down
49 changes: 25 additions & 24 deletions xact/xs/xaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package xs_test

import (
"errors"
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -282,36 +283,36 @@ func TestXactionQueryFinished(t *testing.T) {
}

func TestBeid(t *testing.T) {
num := 10000
const div = uint64(100 * time.Millisecond)
num := 100
if testing.Short() {
num = 10
}
results := make(map[string]uint64, num)
compare := make(map[uint64]string, num)
tags := []string{"tag1", "tag2"}
now := time.Now()
xreg.PrimeTime.Store(now.UnixNano())
xreg.MyTime.Store(now.Add(time.Second).UnixNano())

var (
ids = make(map[string]struct{}, num)
tags = []string{"tag1", "tag2"}
cnt int
)
for i := 0; i < num; i++ {
val := uint64(time.Now().UnixNano())
beid := xreg.GenBEID(val, tags[i%2])
compare[val] = beid
if _, ok := results[beid]; ok {
t.Fatalf("%s duplicated", beid)
beid := xreg.GenBEID(div, tags[i%2])
if _, ok := ids[beid]; ok {
t.Fatalf("%d: %s duplicated", i, beid)
}
results[beid] = val
ids[beid] = struct{}{}

time.Sleep(time.Millisecond)
}
if len(compare) != len(results) {
t.Fatalf("lengths differ %d != %d", len(compare), len(results))
}
// repro
for val, beid := range compare {
b1 := xreg.GenBEID(val, tags[0])
b2 := xreg.GenBEID(val, tags[1])
if b1 == beid && b2 != beid {
continue
}
if b2 == beid && b1 != beid {
continue
id := xreg.GenBEID(div, tags[i%2])
if beid != id {
cnt++
}
t.Fatalf("failed to repro for %x: %s, %s, %s", val, beid, b1, b2)

time.Sleep(time.Duration(div))
}
if cnt > 0 {
fmt.Printf("Warning: failed to reproduce %d time%s out of %d\n", cnt, cos.Plural(cnt), num)
}
}

0 comments on commit 301e4ef

Please sign in to comment.