mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-18 17:08:15 +02:00
45-use-statistical-testing
Keepalive is based on per mesh and not per node. Using total ordering mechanism similar to paxos to elect a leader if leader doesn't update it's timestamp within 3 * keepAlive then give the leader a gravestone and elect the next leader. Leader is bassed on lexicographically ordered public key.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"net"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -245,6 +246,31 @@ func (m *TwoPhaseStoreMeshManager) UpdateTimeStamp(nodeId string) error {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
// Sort nodes by their public key
|
||||
peers := m.GetPeers()
|
||||
slices.Sort(peers)
|
||||
|
||||
if len(peers) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
peerToUpdate := peers[0]
|
||||
|
||||
if uint64(time.Now().Unix())-m.store.Clock.GetTimestamp(peerToUpdate) > 3*uint64(m.conf.KeepAliveTime) {
|
||||
m.store.Mark(peerToUpdate)
|
||||
|
||||
if len(peers) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
peerToUpdate = peers[1]
|
||||
}
|
||||
|
||||
if peerToUpdate != nodeId {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Refresh causing node to update it's time stamp
|
||||
node := m.store.Get(nodeId)
|
||||
node.Timestamp = time.Now().Unix()
|
||||
m.store.Put(nodeId, node)
|
||||
|
Reference in New Issue
Block a user