Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBG-4440: HLC implementation for blip tester client #7399

Merged
merged 7 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 additions & 0 deletions rest/utilities_testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"testing"

"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
"github.com/couchbaselabs/rosmar"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -333,3 +335,49 @@ func TestRequest(t *testing.T) {
_ = Request(http.MethodGet, "%", "")
})
}

func TestHybridLogicalClockNowConcurrent(t *testing.T) {

hlc := rosmar.NewHybridLogicalClock(0)

wg := sync.WaitGroup{}
wg.Add(100)
results := make(chan []rosmar.Timestamp)

for i := 0; i < 100; i++ {
go getTimestamps(&wg, results, hlc)
}

doneChan := make(chan struct{})
go func() {
wg.Wait()
doneChan <- struct{}{}
}()
allHLCTimestamps := make([]rosmar.Timestamp, 0, 10000)
loop:
for {
select {
case timestamps := <-results:
allHLCTimestamps = append(allHLCTimestamps, timestamps...)
case <-doneChan:
break loop
}
}

timestampMap := make(map[rosmar.Timestamp]bool)
for _, timestamp := range allHLCTimestamps {
if _, ok := timestampMap[timestamp]; ok {
t.Fatalf("timestamp %d is not unique", timestamp)
}
timestampMap[timestamp] = true
}
}

func getTimestamps(wg *sync.WaitGroup, results chan []rosmar.Timestamp, hlc *rosmar.HybridLogicalClock) {
defer wg.Done()
timestamps := make([]rosmar.Timestamp, 100)
for i := 0; i < 100; i++ {
timestamps[i] = hlc.Now()
}
results <- timestamps
}
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
Loading