Skip to content

Commit

Permalink
Merge pull request #1709 from josephschorr/hlc-ints
Browse files Browse the repository at this point in the history
Switch HLC timestamps to be stored as an array of two int64s
  • Loading branch information
josephschorr authored Jan 12, 2024
2 parents dae2369 + 93345fe commit e2fc914
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 127 deletions.
8 changes: 4 additions & 4 deletions internal/datastore/common/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ func TestHLCOrdering(t *testing.T) {
ch := NewChanges(revisions.HLCKeyFunc, datastore.WatchRelationships|datastore.WatchSchema)
require.True(t, ch.IsEmpty())

rev1, err := revisions.HLCRevisionFromString("1.1")
rev1, err := revisions.HLCRevisionFromString("1.0000000001")
require.NoError(t, err)

rev0, err := revisions.HLCRevisionFromString("1.0")
rev0, err := revisions.HLCRevisionFromString("1")
require.NoError(t, err)

err = ch.AddRelationshipChange(ctx, rev1, tuple.MustParse("document:foo#viewer@user:tom"), core.RelationTupleUpdate_DELETE)
Expand Down Expand Up @@ -454,10 +454,10 @@ func TestHLCSameRevision(t *testing.T) {
ch := NewChanges(revisions.HLCKeyFunc, datastore.WatchRelationships|datastore.WatchSchema)
require.True(t, ch.IsEmpty())

rev0, err := revisions.HLCRevisionFromString("1.0")
rev0, err := revisions.HLCRevisionFromString("1")
require.NoError(t, err)

rev0again, err := revisions.HLCRevisionFromString("1.0")
rev0again, err := revisions.HLCRevisionFromString("1")
require.NoError(t, err)

err = ch.AddRelationshipChange(ctx, rev0, tuple.MustParse("document:foo#viewer@user:tom"), core.RelationTupleUpdate_TOUCH)
Expand Down
36 changes: 18 additions & 18 deletions internal/datastore/proxy/schemacaching/intervaltracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestIntervalTrackerBasic(t *testing.T) {
validate(t, tracker)

// Ensure that revisions 1-3 find the first.
for _, rv := range []string{"1", "1.1", "2", "2.6", "3", "3.9", "3.999"} {
for _, rv := range []string{"1", "1.0000000001", "2", "2.0000000006", "3", "3.0000000009", "3.0000000010"} {
value, found = tracker.lookup(rev(rv), rev("5"))
require.True(t, found)
require.Equal(t, "first", value)
Expand All @@ -68,7 +68,7 @@ func TestIntervalTrackerBasic(t *testing.T) {
require.Equal(t, "second", value)

// Ensure the entry is not found if the tracking revision is less than that specified.
_, found = tracker.lookup(rev("5.1"), rev("5"))
_, found = tracker.lookup(rev("5.0000000001"), rev("5"))
require.False(t, found)
}

Expand All @@ -85,7 +85,7 @@ func TestIntervalTrackerBeginningGap(t *testing.T) {
require.Equal(t, "first", value)

// Ensure the value is *not* found at revision 4.1 with max tracked 4.
_, found = tracker.lookup(rev("4.1"), rev("4"))
_, found = tracker.lookup(rev("4.0000000001"), rev("4"))
require.False(t, found)

// Ensure the value is found at revision 4.1 when maxed tracked is 5.
Expand All @@ -94,7 +94,7 @@ func TestIntervalTrackerBeginningGap(t *testing.T) {
require.Equal(t, "first", value)

// Make sure a request for revision 1-3 (exclusive) with the tracking revision less (since that "update" hasn't "arrived" yet)
for _, rv := range []string{"1", "1.1", "2", "2.6", "2.999"} {
for _, rv := range []string{"1", "1.0000000001", "2", "2.0000000006", "2.0000000009"} {
_, found = tracker.lookup(rev(rv), rev("3"))
require.False(t, found)
}
Expand All @@ -120,22 +120,22 @@ func TestIntervalTrackerOutOfOrderInsertion(t *testing.T) {
validate(t, tracker)

// Make sure a request for revision 1-2 (exclusive) refer to 'first'.
for _, rv := range []string{"1", "1.1", "1.999"} {
value, found := tracker.lookup(rev(rv), rev("4.1"))
for _, rv := range []string{"1", "1.0000000001", "1.0000000009"} {
value, found := tracker.lookup(rev(rv), rev("4.0000000001"))
require.True(t, found)
require.Equal(t, "first", value)
}

// Make sure a request for revision 2-4 (exclusive) refer to 'second'.
for _, rv := range []string{"2", "2.5", "3.999"} {
value, found := tracker.lookup(rev(rv), rev("4.1"))
for _, rv := range []string{"2", "2.0000000005", "3.0000000009"} {
value, found := tracker.lookup(rev(rv), rev("4.0000000001"))
require.True(t, found)
require.Equal(t, "second", value)
}

// Make sure a request for revision 4+ refers to 'four'
for _, rv := range []string{"4", "4.01", "4.05", "4.1"} {
value, found := tracker.lookup(rev(rv), rev("4.1"))
for _, rv := range []string{"4", "4.0000000001", "4.0000000005", "4.0000000009"} {
value, found := tracker.lookup(rev(rv), rev("4.0000000009"))
require.True(t, found)
require.Equal(t, "four", value)
}
Expand All @@ -145,15 +145,15 @@ func TestIntervalTrackerOutOfOrderInsertion(t *testing.T) {
validate(t, tracker)

// Make sure a request for revision 4-8 (exclusive) refers to 'four'
for _, rv := range []string{"4", "4.01", "4.05", "4.1", "7.999"} {
value, found := tracker.lookup(rev(rv), rev("8.1"))
for _, rv := range []string{"4", "4.0000000001", "4.0000000005", "4.0000000001", "7.0000000009"} {
value, found := tracker.lookup(rev(rv), rev("8.0000000001"))
require.True(t, found)
require.Equal(t, "four", value)
}

// Make sure a request for revision 8+ refers to 'eight'
for _, rv := range []string{"8", "8.01", "8.05", "8.1"} {
value, found := tracker.lookup(rev(rv), rev("8.1"))
for _, rv := range []string{"8", "8.0000000001", "8.0000000005", "8.0000000009"} {
value, found := tracker.lookup(rev(rv), rev("8.0000000009"))
require.True(t, found)
require.Equal(t, "eight", value)
}
Expand Down Expand Up @@ -291,23 +291,23 @@ func TestIntervalTrackerRealWorldUsage(t *testing.T) {
require.True(t, found)
require.Equal(t, "notfound5", value)

value, found = tracker.lookup(rev("5"), rev("3.5"))
value, found = tracker.lookup(rev("5"), rev("3.0000000005"))
require.True(t, found)
require.Equal(t, "notfound5", value)

value, found = tracker.lookup(rev("2"), rev("5"))
require.True(t, found)
require.Equal(t, "real2", value)

value, found = tracker.lookup(rev("2"), rev("3.5"))
value, found = tracker.lookup(rev("2"), rev("3.0000000005"))
require.True(t, found)
require.Equal(t, "real2", value)

value, found = tracker.lookup(rev("3.5"), rev("5"))
value, found = tracker.lookup(rev("3.0000000005"), rev("5"))
require.True(t, found)
require.Equal(t, "real2-again", value)

value, found = tracker.lookup(rev("3.5"), rev("3.5"))
value, found = tracker.lookup(rev("3.0000000005"), rev("3.0000000005"))
require.True(t, found)
require.Equal(t, "real2-again", value)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/datastore/proxy/schemacaching/watchingcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ func TestWatchingCacheBasicOperation(t *testing.T) {
fakeDS.disableReads()

// Read again, which should now be via the cache.
nsDef, _, err = wcache.SnapshotReader(rev("3.5")).ReadNamespaceByName(context.Background(), "somenamespace")
nsDef, _, err = wcache.SnapshotReader(rev("3.0000000005")).ReadNamespaceByName(context.Background(), "somenamespace")
require.NoError(t, err)
require.Equal(t, "somenamespace", nsDef.Name)

// Read via a lookup.
nsDefs, err := wcache.SnapshotReader(rev("3.5")).LookupNamespacesWithNames(context.Background(), []string{"somenamespace"})
nsDefs, err := wcache.SnapshotReader(rev("3.0000000005")).LookupNamespacesWithNames(context.Background(), []string{"somenamespace"})
require.NoError(t, err)
require.Equal(t, "somenamespace", nsDefs[0].Definition.Name)

// Delete the namespace at revision 5.
fakeDS.updateNamespace("somenamespace", nil, rev("5"))

// Re-read at an earlier revision.
nsDef, _, err = wcache.SnapshotReader(rev("3.5")).ReadNamespaceByName(context.Background(), "somenamespace")
nsDef, _, err = wcache.SnapshotReader(rev("3.0000000005")).ReadNamespaceByName(context.Background(), "somenamespace")
require.NoError(t, err)
require.Equal(t, "somenamespace", nsDef.Name)

Expand Down
7 changes: 7 additions & 0 deletions internal/datastore/revisions/commonrevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ type WithTimestampRevision interface {
TimestampNanoSec() int64
ConstructForTimestamp(timestampNanoSec int64) WithTimestampRevision
}

// WithIntegerRepresentation is an interface that can be implemented by a revision to
// provide an integer representation of the revision.
type WithIntegerRepresentation interface {
datastore.Revision
IntegerRepresentation() (int64, uint32)
}
36 changes: 18 additions & 18 deletions internal/datastore/revisions/commonrevision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func TestRevisionEqual(t *testing.T) {
true,
},
{
"1.1",
"1.0000000004",
"1",
false,
},
{
"1",
"1.1",
"1.0000000004",
false,
},
{
"1.1",
"1.1",
"1.0000000004",
"1.0000000004",
true,
},
}
Expand Down Expand Up @@ -97,18 +97,18 @@ func TestRevisionComparison(t *testing.T) {
false,
},
{
"1.1",
"1.0000000004",
"1",
true,
},
{
"1",
"1.1",
"1.0000000004",
false,
},
{
"1.1",
"1.1",
"1.0000000004",
"1.0000000004",
false,
},
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestRevisionComparison(t *testing.T) {

func TestRevisionBidirectionalParsing(t *testing.T) {
tcs := []string{
"1", "2", "42", "192747564535", "1.1", "1.2", "1.42", "-1235",
"1", "2", "42", "192747564535", "1.0000000004", "1.0000000002", "1.0000000042", "-1235",
}

for _, tc := range tcs {
Expand All @@ -178,7 +178,7 @@ func TestTimestampRevisionParsing(t *testing.T) {
"42": false,
"1257894000000000000": false,
"-1": false,
"1.23": true,
"1.0000000004": true,
}

for tc, expectError := range tcs {
Expand All @@ -203,7 +203,7 @@ func TestTransactionIDRevisionParsing(t *testing.T) {
"42": false,
"1257894000000000000": false,
"-1": true,
"1.23": true,
"1.0000000004": true,
}

for tc, expectError := range tcs {
Expand All @@ -223,13 +223,13 @@ func TestTransactionIDRevisionParsing(t *testing.T) {

func TestHLCRevisionParsing(t *testing.T) {
tcs := map[string]bool{
"1": false,
"2": false,
"42": false,
"1257894000000000000": false,
"-1": false,
"1.23": false,
"9223372036854775807.2": false,
"1": false,
"2": false,
"42": false,
"1257894000000000000": false,
"-1": false,
"1.0000000004": false,
"9223372036854775807.0000000004": false,
}

for tc, expectError := range tcs {
Expand Down
Loading

0 comments on commit e2fc914

Please sign in to comment.