go.temporal.io/server@v1.23.0/common/clock/hybrid_logical_clock/hybrid_logical_clock.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package hybrid_logical_clock 26 27 import ( 28 "time" 29 30 clockpb "go.temporal.io/server/api/clock/v1" 31 commonclock "go.temporal.io/server/common/clock" 32 ) 33 34 type Clock = clockpb.HybridLogicalClock 35 36 // Next generates the next clock timestamp given the current clock. 37 // HybridLogicalClock requires the previous clock to ensure that time doesn't move backwards and the next clock is 38 // monotonically increasing. 39 func Next(prior *Clock, source commonclock.TimeSource) *Clock { 40 wallclock := source.Now().UnixMilli() 41 // Ensure time does not move backwards 42 if wallclock < prior.GetWallClock() { 43 wallclock = prior.GetWallClock() 44 } 45 // Ensure timestamp is monotonically increasing 46 var version int32 47 if wallclock == prior.GetWallClock() { 48 version = prior.GetVersion() + 1 49 } 50 51 return &Clock{WallClock: wallclock, Version: version, ClusterId: prior.ClusterId} 52 } 53 54 // Zero generates a zeroed logical clock for the cluster ID. 55 func Zero(clusterID int64) *Clock { 56 return &Clock{WallClock: 0, Version: 0, ClusterId: clusterID} 57 } 58 59 func sign[T int64 | int32](x T) int { 60 if x > 0 { 61 return 1 62 } 63 if x < 0 { 64 return -1 65 } 66 return 0 67 } 68 69 // Compare 2 Clocks, returns 0 if a == b, -1 if a > b, 1 if a < b 70 func Compare(a *Clock, b *Clock) int { 71 if a.WallClock == b.WallClock { 72 if a.Version == b.Version { 73 return sign(b.ClusterId - a.ClusterId) 74 } 75 return sign(b.Version - a.Version) 76 } 77 return sign(b.WallClock - a.WallClock) 78 } 79 80 // Greater returns true if a is greater than b 81 func Greater(a *Clock, b *Clock) bool { 82 return Compare(b, a) > 0 83 } 84 85 // Greater returns true if a is greater than b 86 func Less(a *Clock, b *Clock) bool { 87 return Compare(a, b) > 0 88 } 89 90 // Max returns the maximum of two Clocks 91 func Max(a *Clock, b *Clock) *Clock { 92 if Compare(a, b) > 0 { 93 return b 94 } 95 return a 96 } 97 98 // Min returns the minimum of two Clocks 99 func Min(a *Clock, b *Clock) *Clock { 100 if Compare(a, b) > 0 { 101 return a 102 } 103 return b 104 } 105 106 // Equal returns whether two Clocks are equal 107 func Equal(a *Clock, b *Clock) bool { 108 return Compare(a, b) == 0 109 } 110 111 // UTC returns a Time from a Clock in millisecond resolution. The Time's Location is set to UTC. 112 func UTC(c *Clock) time.Time { 113 if c == nil { 114 return time.Unix(0, 0).UTC() 115 } 116 return time.Unix(c.WallClock/1000, c.WallClock%1000*1000000).UTC() 117 } 118 119 // Since returns time.Since(UTC(c)) 120 func Since(c *Clock) time.Duration { 121 return time.Since(UTC(c)) 122 }