-
Notifications
You must be signed in to change notification settings - Fork 911
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
Versioning data merge algo and hybrid logical clock utils #4205
Merged
bergundy
merged 5 commits into
temporalio:worker-versioning
from
bergundy:versioning-data-merge
Apr 25, 2023
+653
−89
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
common/clock/hybrid_logical_clock/hybrid_logical_clock.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package hybrid_logical_clock | ||
|
||
import ( | ||
clockpb "go.temporal.io/server/api/clock/v1" | ||
commonclock "go.temporal.io/server/common/clock" | ||
) | ||
|
||
type Clock = clockpb.HybridLogicalClock | ||
|
||
// Next generates the next clock timestamp given the current clock. | ||
// HybridLogicalClock requires the previous clock to ensure that time doesn't move backwards and the next clock is | ||
// monotonically increasing. | ||
func Next(clock Clock, source commonclock.TimeSource) Clock { | ||
wallclock := source.Now().UnixMilli() | ||
// Ensure time does not move backwards | ||
if wallclock < clock.GetWallClock() { | ||
wallclock = clock.GetWallClock() | ||
} | ||
// Ensure timestamp is monotonically increasing | ||
if wallclock == clock.GetWallClock() { | ||
clock.Version = clock.GetVersion() + 1 | ||
} else { | ||
clock.Version = 0 | ||
clock.WallClock = wallclock | ||
} | ||
|
||
return Clock{WallClock: wallclock, Version: clock.Version, ClusterId: clock.ClusterId} | ||
} | ||
|
||
// Zero generates a zeroed logical clock for the cluster ID. | ||
func Zero(clusterID int64) Clock { | ||
return Clock{WallClock: 0, Version: 0, ClusterId: clusterID} | ||
} | ||
|
||
func sign[T int64 | int32](x T) int { | ||
if x > 0 { | ||
return 1 | ||
} | ||
if x < 0 { | ||
return -1 | ||
} | ||
return 0 | ||
} | ||
|
||
// Compare 2 clocks, returns 0 if a == b, -1 if a > b, 1 if a < b | ||
func Compare(a Clock, b Clock) int { | ||
if a.WallClock == b.WallClock { | ||
if a.Version == b.Version { | ||
return sign(b.ClusterId - a.ClusterId) | ||
} | ||
return sign(b.Version - a.Version) | ||
} | ||
return sign(b.WallClock - a.WallClock) | ||
} | ||
|
||
// Greater returns true if a is greater than b | ||
func Greater(a Clock, b Clock) bool { | ||
return Compare(b, a) > 0 | ||
} | ||
|
||
// Greater returns true if a is greater than b | ||
func Less(a Clock, b Clock) bool { | ||
return Compare(a, b) > 0 | ||
} | ||
|
||
// Max returns the maximum of two clocks | ||
func Max(a Clock, b Clock) Clock { | ||
if Compare(a, b) > 0 { | ||
return b | ||
} | ||
return a | ||
} | ||
|
||
// Equal returns whether two clocks are equal | ||
func Equal(a Clock, b Clock) bool { | ||
return Compare(a, b) == 0 | ||
} |
85 changes: 85 additions & 0 deletions
85
common/clock/hybrid_logical_clock/hybrid_logical_clock_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package hybrid_logical_clock | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
commonclock "go.temporal.io/server/common/clock" | ||
) | ||
|
||
func Test_Next_ReturnsGreaterClock(t *testing.T) { | ||
t0 := Zero(1) | ||
timesource := commonclock.NewEventTimeSource() | ||
|
||
// Same wallclock | ||
timesource.Update(time.Unix(0, 0).UTC()) | ||
t1 := Next(t0, timesource) | ||
assert.Equal(t, Compare(t0, t1), 1) | ||
// Greater wallclock | ||
timesource.Update(time.Unix(0, 1).UTC()) | ||
t2 := Next(t1, timesource) | ||
assert.Equal(t, Compare(t1, t2), 1) | ||
} | ||
|
||
func Test_Compare(t *testing.T) { | ||
var t0 Clock | ||
var t1 Clock | ||
|
||
t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} | ||
t1 = Clock{WallClock: 1, Version: 1, ClusterId: 1} | ||
assert.Equal(t, Compare(t0, t1), 0) | ||
assert.True(t, Equal(t0, t1)) | ||
|
||
t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} | ||
t1 = Clock{WallClock: 1, Version: 1, ClusterId: 2} | ||
assert.Equal(t, Compare(t0, t1), 1) | ||
// Let's get a -1 in there for sanity | ||
assert.Equal(t, Compare(t1, t0), -1) | ||
|
||
t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} | ||
t1 = Clock{WallClock: 1, Version: 2, ClusterId: 1} | ||
assert.Equal(t, Compare(t0, t1), 1) | ||
|
||
t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} | ||
t1 = Clock{WallClock: 2, Version: 1, ClusterId: 1} | ||
assert.Equal(t, Compare(t0, t1), 1) | ||
|
||
assert.True(t, Greater(t1, t0)) | ||
assert.True(t, Less(t0, t1)) | ||
} | ||
|
||
func Test_Max_ReturnsMaximum(t *testing.T) { | ||
t0 := Zero(1) | ||
t1 := Zero(2) | ||
|
||
max := Max(t0, t1) | ||
assert.Equal(t, max, t1) | ||
// Just in case it doesn't work in reverse order... | ||
max = Max(t1, t0) | ||
assert.Equal(t, max, t1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ import ( | |
persistencespb "go.temporal.io/server/api/persistence/v1" | ||
tokenspb "go.temporal.io/server/api/token/v1" | ||
"go.temporal.io/server/common" | ||
"go.temporal.io/server/common/clock" | ||
hlc "go.temporal.io/server/common/clock/hybrid_logical_clock" | ||
"go.temporal.io/server/common/cluster" | ||
"go.temporal.io/server/common/log" | ||
"go.temporal.io/server/common/log/tag" | ||
|
@@ -103,6 +105,7 @@ type ( | |
namespaceRegistry namespace.Registry | ||
keyResolver membership.ServiceResolver | ||
clusterMeta cluster.Metadata | ||
timeSource clock.TimeSource | ||
} | ||
) | ||
|
||
|
@@ -150,6 +153,7 @@ func NewEngine( | |
namespaceRegistry: namespaceRegistry, | ||
keyResolver: resolver, | ||
clusterMeta: clusterMeta, | ||
timeSource: clock.NewRealTimeSource(), // No need to mock this at the moment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we get this from fx instead? just for consistency. I can do it in another PR if you don't want to touch the fx stuff |
||
} | ||
} | ||
|
||
|
@@ -723,12 +727,12 @@ func (e *matchingEngineImpl) UpdateWorkerBuildIdCompatibility( | |
err = tqMgr.UpdateUserData(hCtx.Context, func(data *persistencespb.TaskQueueUserData) (*persistencespb.TaskQueueUserData, error) { | ||
clock := data.GetClock() | ||
if clock == nil { | ||
tmp := zeroHLC(e.clusterMeta.GetClusterID()) | ||
tmp := hlc.Zero(e.clusterMeta.GetClusterID()) | ||
clock = &tmp | ||
} | ||
|
||
updatedClock, versioningData, err := UpdateVersionSets( | ||
*clock, | ||
updatedClock := hlc.Next(*clock, e.timeSource) | ||
versioningData, err := UpdateVersionSets( | ||
updatedClock, | ||
data.GetVersioningData(), | ||
req.GetRequest(), | ||
e.config.VersionCompatibleSetLimitPerQueue(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this really deserve a new package? just put it in
common/clock
.. they're used together anyway so anyone importing this needs to import that too