1
0
forked from extern/smegmesh
smegmesh/pkg/sync/syncer.go

126 lines
3.1 KiB
Go
Raw Normal View History

2023-10-10 21:14:40 +02:00
package sync
import (
"io"
2023-11-03 16:24:18 +01:00
"math/rand"
"time"
2023-11-03 16:24:18 +01:00
"github.com/tim-beatham/wgmesh/pkg/conf"
"github.com/tim-beatham/wgmesh/pkg/conn"
"github.com/tim-beatham/wgmesh/pkg/lib"
logging "github.com/tim-beatham/wgmesh/pkg/log"
"github.com/tim-beatham/wgmesh/pkg/mesh"
)
2023-10-10 21:14:40 +02:00
// Syncer: picks random nodes from the mesh
type Syncer interface {
Sync(meshId string) error
SyncMeshes() error
}
type SyncerImpl struct {
2023-11-05 19:03:58 +01:00
manager mesh.MeshManager
2023-11-03 16:24:18 +01:00
requester SyncRequester
infectionCount int
syncCount int
cluster conn.ConnCluster
conf *conf.WgMeshConfiguration
2023-10-10 21:14:40 +02:00
}
// Sync: Sync random nodes
func (s *SyncerImpl) Sync(meshId string) error {
if !s.manager.HasChanges(meshId) && s.infectionCount == 0 {
logging.Log.WriteInfof("No changes for %s", meshId)
return nil
}
logging.Log.WriteInfof("UPDATING WG CONF")
s.manager.GetRouteManager().UpdateRoutes()
err := s.manager.ApplyConfig()
2023-11-05 19:03:58 +01:00
if err != nil {
logging.Log.WriteInfof("Failed to update config %w", err)
2023-11-05 19:03:58 +01:00
}
publicKey := s.manager.GetPublicKey()
logging.Log.WriteInfof(publicKey.String())
nodeNames := s.manager.GetMesh(meshId).GetPeers()
neighbours := s.cluster.GetNeighbours(nodeNames, publicKey.String())
2023-11-03 16:24:18 +01:00
randomSubset := lib.RandomSubsetOfLength(neighbours, s.conf.BranchRate)
for _, node := range randomSubset {
logging.Log.WriteInfof("Random node: %s", node)
}
before := time.Now()
if len(nodeNames) > s.conf.ClusterSize && rand.Float64() < s.conf.InterClusterChance {
2023-11-03 16:24:18 +01:00
logging.Log.WriteInfof("Sending to random cluster")
randomSubset[len(randomSubset)-1] = s.cluster.GetInterCluster(nodeNames, publicKey.String())
2023-11-03 16:24:18 +01:00
}
var succeeded bool = false
2023-10-23 19:13:08 +02:00
// Do this synchronously to conserve bandwidth
for _, node := range randomSubset {
correspondingPeer := s.manager.GetNode(meshId, node)
if correspondingPeer == nil {
logging.Log.WriteErrorf("node %s does not exist", node)
}
err = s.requester.SyncMesh(meshId, correspondingPeer)
if err == nil || err == io.EOF {
succeeded = true
} else {
// If the synchronisation operation has failed them mark a gravestone
// preventing the peer from being re-contacted until it has updated
// itself
s.manager.GetMesh(meshId).Mark(node)
}
}
2023-11-03 16:24:18 +01:00
s.syncCount++
logging.Log.WriteInfof("SYNC TIME: %v", time.Since(before))
2023-11-03 16:24:18 +01:00
logging.Log.WriteInfof("SYNC COUNT: %d", s.syncCount)
2023-11-03 16:24:18 +01:00
s.infectionCount = ((s.conf.InfectionCount + s.infectionCount - 1) % s.conf.InfectionCount)
if !succeeded {
// If could not gossip with anyone then repeat.
s.infectionCount++
}
s.manager.GetMesh(meshId).SaveChanges()
s.manager.Prune()
return nil
2023-10-10 21:14:40 +02:00
}
// SyncMeshes: Sync all meshes
2023-10-10 21:14:40 +02:00
func (s *SyncerImpl) SyncMeshes() error {
for meshId := range s.manager.GetMeshes() {
err := s.Sync(meshId)
if err != nil {
return err
}
}
return nil
2023-10-10 21:14:40 +02:00
}
2023-11-05 19:03:58 +01:00
func NewSyncer(m mesh.MeshManager, conf *conf.WgMeshConfiguration, r SyncRequester) Syncer {
2023-11-03 16:24:18 +01:00
cluster, _ := conn.NewConnCluster(conf.ClusterSize)
return &SyncerImpl{
manager: m,
conf: conf,
requester: r,
infectionCount: 0,
syncCount: 0,
cluster: cluster}
}