2023-12-07 02:44:54 +01:00
|
|
|
package crdt
|
|
|
|
|
|
|
|
import (
|
2023-12-07 19:18:13 +01:00
|
|
|
"cmp"
|
|
|
|
"slices"
|
2023-12-07 02:44:54 +01:00
|
|
|
"sync"
|
2023-12-07 19:18:13 +01:00
|
|
|
"time"
|
2023-12-07 02:44:54 +01:00
|
|
|
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
|
|
|
)
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
type VectorBucket struct {
|
|
|
|
// clock current value of the node's clock
|
|
|
|
clock uint64
|
|
|
|
// lastUpdate we've seen
|
|
|
|
lastUpdate uint64
|
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
// Vector clock defines an abstract data type
|
|
|
|
// for a vector clock implementation
|
2023-12-07 19:18:13 +01:00
|
|
|
type VectorClock[K cmp.Ordered] struct {
|
|
|
|
vectors map[K]*VectorBucket
|
2023-12-07 02:44:54 +01:00
|
|
|
lock sync.RWMutex
|
|
|
|
processID K
|
2023-12-07 19:18:13 +01:00
|
|
|
staleTime uint64
|
|
|
|
hashFunc func(K) uint64
|
2023-12-07 02:44:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementClock: increments the node's value in the vector clock
|
|
|
|
func (m *VectorClock[K]) IncrementClock() uint64 {
|
|
|
|
maxClock := uint64(0)
|
|
|
|
m.lock.Lock()
|
|
|
|
|
|
|
|
for _, value := range m.vectors {
|
2023-12-07 19:18:13 +01:00
|
|
|
maxClock = max(maxClock, value.clock)
|
|
|
|
}
|
|
|
|
|
|
|
|
newBucket := VectorBucket{
|
|
|
|
clock: maxClock + 1,
|
|
|
|
lastUpdate: uint64(time.Now().Unix()),
|
2023-12-07 02:44:54 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
m.vectors[m.processID] = &newBucket
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
m.lock.Unlock()
|
|
|
|
return maxClock
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHash: gets the hash of the vector clock used to determine if there
|
|
|
|
// are any changes
|
|
|
|
func (m *VectorClock[K]) GetHash() uint64 {
|
|
|
|
m.lock.RLock()
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
hash := uint64(0)
|
|
|
|
|
|
|
|
sortedKeys := lib.MapKeys(m.vectors)
|
|
|
|
slices.Sort(sortedKeys)
|
|
|
|
|
|
|
|
for key, bucket := range m.vectors {
|
|
|
|
hash += m.hashFunc(key)
|
|
|
|
hash += bucket.clock
|
|
|
|
}
|
|
|
|
|
|
|
|
m.lock.RUnlock()
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStale: get all entries that are stale within the mesh
|
|
|
|
func (m *VectorClock[K]) getStale() []K {
|
|
|
|
m.lock.RLock()
|
|
|
|
maxTimeStamp := lib.Reduce(0, lib.MapValues(m.vectors), func(i uint64, vb *VectorBucket) uint64 {
|
|
|
|
return max(i, vb.lastUpdate)
|
2023-12-07 02:44:54 +01:00
|
|
|
})
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
toRemove := make([]K, 0)
|
|
|
|
|
|
|
|
for key, bucket := range m.vectors {
|
|
|
|
if maxTimeStamp-bucket.lastUpdate > m.staleTime {
|
|
|
|
toRemove = append(toRemove, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
m.lock.RUnlock()
|
2023-12-07 19:18:13 +01:00
|
|
|
return toRemove
|
2023-12-07 02:44:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *VectorClock[K]) Prune() {
|
2023-12-07 19:18:13 +01:00
|
|
|
stale := m.getStale()
|
2023-12-07 02:44:54 +01:00
|
|
|
|
|
|
|
m.lock.Lock()
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
for _, key := range stale {
|
|
|
|
delete(m.vectors, key)
|
2023-12-07 02:44:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
func (m *VectorClock[K]) GetTimestamp(processId K) uint64 {
|
|
|
|
return m.vectors[processId].lastUpdate
|
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
func (m *VectorClock[K]) Put(key K, value uint64) {
|
2023-12-07 19:18:13 +01:00
|
|
|
clockValue := uint64(0)
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
m.lock.Lock()
|
2023-12-07 19:18:13 +01:00
|
|
|
bucket, ok := m.vectors[key]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
clockValue = bucket.clock
|
|
|
|
}
|
|
|
|
|
|
|
|
if value > clockValue {
|
|
|
|
newBucket := VectorBucket{
|
|
|
|
clock: value,
|
|
|
|
lastUpdate: uint64(time.Now().Unix()),
|
|
|
|
}
|
|
|
|
m.vectors[key] = &newBucket
|
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
m.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *VectorClock[K]) GetClock() map[K]uint64 {
|
|
|
|
clock := make(map[K]uint64)
|
|
|
|
|
|
|
|
m.lock.RLock()
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
keys := lib.MapKeys(m.vectors)
|
|
|
|
slices.Sort(keys)
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
for key, value := range clock {
|
|
|
|
clock[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
m.lock.RUnlock()
|
|
|
|
return clock
|
|
|
|
}
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
func NewVectorClock[K cmp.Ordered](processID K, hashFunc func(K) uint64, staleTime uint64) *VectorClock[K] {
|
2023-12-07 02:44:54 +01:00
|
|
|
return &VectorClock[K]{
|
2023-12-07 19:18:13 +01:00
|
|
|
vectors: make(map[K]*VectorBucket),
|
2023-12-07 02:44:54 +01:00
|
|
|
processID: processID,
|
2023-12-07 19:18:13 +01:00
|
|
|
staleTime: staleTime,
|
|
|
|
hashFunc: hashFunc,
|
2023-12-07 02:44:54 +01:00
|
|
|
}
|
|
|
|
}
|