BUGFIX: Hashing datastore to work out changes

Changed hashing implementation to work out if there are changes
in the data store
This commit is contained in:
Tim Beatham 2023-11-30 15:58:26 +00:00
parent b9ba836ae3
commit 3ef1b68ba5
5 changed files with 22 additions and 9 deletions

View File

@ -222,13 +222,13 @@ func (m *TwoPhaseStoreMeshManager) GetDevice() (*wgtypes.Device, error) {
// HasChanges returns true if we have changes since last time we synced // HasChanges returns true if we have changes since last time we synced
func (m *TwoPhaseStoreMeshManager) HasChanges() bool { func (m *TwoPhaseStoreMeshManager) HasChanges() bool {
clockValue := m.store.GetClock() clockValue := m.store.GetHash()
return clockValue != m.LastClock return clockValue != m.LastClock
} }
// Record that we have changes and save the corresponding changes // Record that we have changes and save the corresponding changes
func (m *TwoPhaseStoreMeshManager) SaveChanges() { func (m *TwoPhaseStoreMeshManager) SaveChanges() {
clockValue := m.store.GetClock() clockValue := m.store.GetHash()
m.LastClock = clockValue m.LastClock = clockValue
} }

View File

@ -128,16 +128,19 @@ func (m *TwoPhaseMap[K, D]) incrementClock() uint64 {
return maxClock return maxClock
} }
func (m *TwoPhaseMap[K, D]) GetClock() uint64 { // GetHash: Get the hash of the current state of the map
maxClock := uint64(0) // Sums the current values of the vectors. Provides good approximation
// of increasing numbers
func (m *TwoPhaseMap[K, D]) GetHash() uint64 {
m.lock.RLock() m.lock.RLock()
for _, value := range m.vectors { sum := lib.Reduce(uint64(0), lib.MapValues(m.vectors), func(sum uint64, current uint64) uint64 {
maxClock = max(maxClock, value) return current + sum
} })
m.lock.RUnlock() m.lock.RUnlock()
return maxClock
return sum
} }
// GetState: get the current vector clock of the add and remove // GetState: get the current vector clock of the add and remove

View File

@ -125,7 +125,6 @@ func (t *TwoPhaseSyncer) RecvMessage(msg []byte) error {
func (t *TwoPhaseSyncer) Complete() { func (t *TwoPhaseSyncer) Complete() {
logging.Log.WriteInfof("SYNC COMPLETED") logging.Log.WriteInfof("SYNC COMPLETED")
t.manager.SaveChanges()
} }
func NewTwoPhaseSyncer(manager *TwoPhaseStoreMeshManager) *TwoPhaseSyncer { func NewTwoPhaseSyncer(manager *TwoPhaseStoreMeshManager) *TwoPhaseSyncer {

View File

@ -76,3 +76,13 @@ func Contains[V any](list []V, proposition func(V) bool) bool {
return false return false
} }
func Reduce[A any, V any](start A, values []V, reduce func(A, V) A) A {
accum := start
for _, elem := range values {
accum = reduce(accum, elem)
}
return accum
}

View File

@ -89,6 +89,7 @@ func (s *SyncerImpl) Sync(meshId string) error {
logging.Log.WriteInfof("SYNC COUNT: %d", s.syncCount) logging.Log.WriteInfof("SYNC COUNT: %d", s.syncCount)
s.infectionCount = ((s.conf.InfectionCount + s.infectionCount - 1) % s.conf.InfectionCount) s.infectionCount = ((s.conf.InfectionCount + s.infectionCount - 1) % s.conf.InfectionCount)
s.manager.GetMesh(meshId).SaveChanges()
return nil return nil
} }