Skip to content

Commit

Permalink
CBG-4440: HLC implementation for blip tester client (#7399)
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Newman-Smith <[email protected]>
  • Loading branch information
gregns1 authored Feb 26, 2025
1 parent 0d4a5b7 commit f2939ae
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/couchbase/sg-bucket v0.0.0-20241018143914-45ef51a0c1be
github.com/couchbaselabs/go-fleecedelta v0.0.0-20220909152808-6d09efa7a338
github.com/couchbaselabs/gocbconnstr v1.0.5
github.com/couchbaselabs/rosmar v0.0.0-20250219003541-c8f724802783
github.com/couchbaselabs/rosmar v0.0.0-20250226134616-3b9ac157a3cd
github.com/elastic/gosigar v0.14.3
github.com/felixge/fgprof v0.9.5
github.com/go-jose/go-jose/v4 v4.0.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/couchbaselabs/gocbconnstr v1.0.5 h1:e0JokB5qbcz7rfnxEhNRTKz8q1svoRvDo
github.com/couchbaselabs/gocbconnstr v1.0.5/go.mod h1:KV3fnIKMi8/AzX0O9zOrO9rofEqrRF1d2rG7qqjxC7o=
github.com/couchbaselabs/gocbconnstr/v2 v2.0.0-20240607131231-fb385523de28 h1:lhGOw8rNG6RAadmmaJAF3PJ7MNt7rFuWG7BHCYMgnGE=
github.com/couchbaselabs/gocbconnstr/v2 v2.0.0-20240607131231-fb385523de28/go.mod h1:o7T431UOfFVHDNvMBUmUxpHnhivwv7BziUao/nMl81E=
github.com/couchbaselabs/rosmar v0.0.0-20250219003541-c8f724802783 h1:V6N77//HHB1ypgaD0iuvZuSMz7tkeB7LSvSUbXtL/6c=
github.com/couchbaselabs/rosmar v0.0.0-20250219003541-c8f724802783/go.mod h1:suZBurj14d2YtLOW8pBc8mjQN8MhPFHHgPbqX1fDDlE=
github.com/couchbaselabs/rosmar v0.0.0-20250226134616-3b9ac157a3cd h1:kW8rCwAesoUTaTPu/wK5/XhcAAncKKir5zdj0eeHOpE=
github.com/couchbaselabs/rosmar v0.0.0-20250226134616-3b9ac157a3cd/go.mod h1:suZBurj14d2YtLOW8pBc8mjQN8MhPFHHgPbqX1fDDlE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
11 changes: 9 additions & 2 deletions rest/utilities_testing_blip_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/couchbase/go-blip"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
"github.com/couchbaselabs/rosmar"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -78,6 +79,8 @@ type BlipTesterClient struct {

collectionClients []*BlipTesterCollectionClient
nonCollectionAwareClient *BlipTesterCollectionClient

hlc *rosmar.HybridLogicalClock
}

// getClientDocForSeq returns the clientDoc for the given sequence number, if it exists.
Expand Down Expand Up @@ -310,6 +313,8 @@ type BlipTesterCollectionClient struct {

attachmentsLock sync.RWMutex // lock for _attachments map
_attachments map[string][]byte // Client's local store of _attachments - Map of digest to bytes

hlc *rosmar.HybridLogicalClock
}

// GetDoc returns the latest revision of a document stored on the client.
Expand Down Expand Up @@ -1017,6 +1022,7 @@ func (btcRunner *BlipTestClientRunner) NewBlipTesterClientOptsWithRT(rt *RestTes
BlipTesterClientOpts: *opts,
rt: rt,
id: id.ID(),
hlc: rosmar.NewHybridLogicalClock(0),
}
btcRunner.clients[client.id] = client
client.createBlipTesterReplications()
Expand Down Expand Up @@ -1097,6 +1103,7 @@ func (btc *BlipTesterClient) createBlipTesterReplications() {
_seqCond: sync.NewCond(&l),
_attachments: make(map[string][]byte),
parent: btc,
hlc: btc.hlc,
}
}

Expand All @@ -1116,6 +1123,7 @@ func (btc *BlipTesterClient) initCollectionReplication(collection string, collec
_seqFromDocID: make(map[string]clientSeq),
_attachments: make(map[string][]byte),
parent: btc,
hlc: btc.hlc,
}

btcReplicator.collection = collection
Expand Down Expand Up @@ -1560,8 +1568,7 @@ func (btc *BlipTesterCollectionClient) upsertDoc(docID string, parentVersion *Do

var docVersion DocVersion
if btc.UseHLV() {
// TODO: CBG-4440 Construct a HLC for Value - UnixNano is not accurate enough on Windows to generate unique values, and seq is not comparable across clients.
newVersion := db.Version{SourceID: btc.parent.SourceID, Value: uint64(time.Now().UnixNano())}
newVersion := db.Version{SourceID: btc.parent.SourceID, Value: uint64(btc.hlc.Now())}
require.NoError(btc.TB(), hlv.AddVersion(newVersion))
docVersion = DocVersion{CV: *hlv.ExtractCurrentVersionFromHLV()}
} else {
Expand Down
13 changes: 5 additions & 8 deletions topologytest/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fmt"
"iter"
"maps"
"runtime"
"slices"
"testing"
"time"
Expand Down Expand Up @@ -447,13 +446,11 @@ func TestPeerImplementation(t *testing.T) {
resurrectionBody := []byte(`{"op": "resurrection"}`)
resurrectionVersion := peer.WriteDocument(collectionName, docID, resurrectionBody)
require.NotEmpty(t, resurrectionVersion.docMeta.CV(t))
// FIXME: CBG-4440 - Windows timestamp resolution not good enough for this test
if runtime.GOOS != "windows" {
// need to switch to a HLC so we can have unique versions even in the same timestamp window
require.NotEqual(t, resurrectionVersion.docMeta.CV(t), deleteVersion.CV(t))
require.NotEqual(t, resurrectionVersion.docMeta.CV(t), updateVersion.docMeta.CV(t))
require.NotEqual(t, resurrectionVersion.docMeta.CV(t), createVersion.docMeta.CV(t))
}

require.NotEqual(t, resurrectionVersion.docMeta.CV(t), deleteVersion.CV(t))
require.NotEqual(t, resurrectionVersion.docMeta.CV(t), updateVersion.docMeta.CV(t))
require.NotEqual(t, resurrectionVersion.docMeta.CV(t), createVersion.docMeta.CV(t))

if tc.peerOption.Type == PeerTypeSyncGateway {
require.NotEmpty(t, resurrectionVersion.docMeta.RevTreeID)
require.NotEqual(t, resurrectionVersion.docMeta.RevTreeID, createVersion.docMeta.RevTreeID)
Expand Down

0 comments on commit f2939ae

Please sign in to comment.