2023-10-10 21:14:40 +02:00
|
|
|
// sync merges shared state between two nodes
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-10-23 19:13:08 +02:00
|
|
|
"io"
|
2023-10-10 21:14:40 +02:00
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/ctrlserver"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/mesh"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/rpc"
|
2023-10-10 21:14:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SyncServiceImpl struct {
|
2023-10-20 13:41:06 +02:00
|
|
|
rpc.UnimplementedSyncServiceServer
|
|
|
|
Server *ctrlserver.MeshCtrlServer
|
2023-10-10 21:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMesh: Gets a nodes local mesh configuration as a CRDT
|
|
|
|
func (s *SyncServiceImpl) GetConf(context context.Context, request *rpc.GetConfRequest) (*rpc.GetConfReply, error) {
|
2023-10-20 13:41:06 +02:00
|
|
|
mesh := s.Server.MeshManager.GetMesh(request.MeshId)
|
2023-10-10 21:14:40 +02:00
|
|
|
|
|
|
|
if mesh == nil {
|
|
|
|
return nil, errors.New("mesh does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
meshBytes := mesh.Save()
|
|
|
|
|
|
|
|
reply := rpc.GetConfReply{
|
|
|
|
Mesh: meshBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &reply, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync: Pings a node and syncs the mesh configuration with the other node
|
2023-10-23 19:13:08 +02:00
|
|
|
// SyncMesh: syncs the two streams changes
|
|
|
|
func (s *SyncServiceImpl) SyncMesh(stream rpc.SyncService_SyncMeshServer) error {
|
|
|
|
var meshId = ""
|
2023-10-26 17:53:12 +02:00
|
|
|
var syncer mesh.MeshSyncer = nil
|
2023-10-10 21:14:40 +02:00
|
|
|
|
2023-10-23 19:13:08 +02:00
|
|
|
for {
|
|
|
|
in, err := stream.Recv()
|
2023-10-10 21:14:40 +02:00
|
|
|
|
2023-10-23 19:13:08 +02:00
|
|
|
if err == io.EOF {
|
2023-10-24 17:00:46 +02:00
|
|
|
if syncer != nil {
|
|
|
|
syncer.Complete()
|
|
|
|
}
|
2023-10-23 19:13:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-10-10 21:14:40 +02:00
|
|
|
|
2023-10-23 19:13:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-10 21:14:40 +02:00
|
|
|
|
2023-10-23 19:13:08 +02:00
|
|
|
if len(meshId) == 0 {
|
|
|
|
meshId = in.MeshId
|
|
|
|
|
|
|
|
mesh := s.Server.MeshManager.GetMesh(meshId)
|
|
|
|
|
|
|
|
if mesh == nil {
|
|
|
|
return errors.New("mesh does not exist")
|
|
|
|
}
|
2023-10-10 21:14:40 +02:00
|
|
|
|
2023-10-23 19:13:08 +02:00
|
|
|
syncer = mesh.GetSyncer()
|
|
|
|
} else if meshId != in.MeshId {
|
2023-11-21 14:31:34 +01:00
|
|
|
return errors.New("differing meshids")
|
2023-10-23 19:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if syncer == nil {
|
2023-11-21 14:31:34 +01:00
|
|
|
return errors.New("syncer should not be nil")
|
2023-10-23 19:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg, moreMessages := syncer.GenerateMessage()
|
|
|
|
|
|
|
|
if err = stream.Send(&rpc.SyncMeshReply{Success: true, Changes: msg}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Changes) != 0 {
|
|
|
|
if err = syncer.RecvMessage(in.Changes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !moreMessages || err == io.EOF {
|
2023-10-24 17:00:46 +02:00
|
|
|
if syncer != nil {
|
|
|
|
syncer.Complete()
|
|
|
|
}
|
2023-10-31 11:34:09 +01:00
|
|
|
|
2023-10-23 19:13:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 11:34:09 +01:00
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
func NewSyncService(server *ctrlserver.MeshCtrlServer) *SyncServiceImpl {
|
|
|
|
return &SyncServiceImpl{Server: server}
|
|
|
|
}
|