2021-08-29 17:48:31 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-10-23 12:05:02 +02:00
|
|
|
"fmt"
|
|
|
|
"runtime/debug"
|
2021-08-29 17:48:31 +02:00
|
|
|
"sync"
|
2023-11-16 18:21:52 +01:00
|
|
|
"time"
|
2023-08-18 19:23:11 +02:00
|
|
|
|
2024-10-23 12:05:02 +02:00
|
|
|
"github.com/r3labs/diff/v3"
|
2023-08-18 19:23:11 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2024-10-30 16:53:23 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/differs"
|
2023-11-16 18:21:52 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2021-08-29 17:48:31 +02:00
|
|
|
)
|
|
|
|
|
2022-08-27 12:57:03 +02:00
|
|
|
const channelBufferSize = 100
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
type UpdateMessage struct {
|
2024-10-23 12:05:02 +02:00
|
|
|
Update *proto.SyncResponse
|
|
|
|
NetworkMap *NetworkMap
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
type PeersUpdateManager struct {
|
2023-02-03 10:33:28 +01:00
|
|
|
// peerChannels is an update channel indexed by Peer.ID
|
2021-08-29 17:48:31 +02:00
|
|
|
peerChannels map[string]chan *UpdateMessage
|
2024-10-23 12:05:02 +02:00
|
|
|
// peerNetworkMaps is the UpdateMessage indexed by Peer.ID.
|
|
|
|
peerUpdateMessage map[string]*UpdateMessage
|
2023-11-16 18:21:52 +01:00
|
|
|
// channelsMux keeps the mutex to access peerChannels
|
2024-10-23 12:05:02 +02:00
|
|
|
channelsMux *sync.RWMutex
|
2023-11-16 18:21:52 +01:00
|
|
|
// metrics provides method to collect application metrics
|
|
|
|
metrics telemetry.AppMetrics
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeersUpdateManager returns a new instance of PeersUpdateManager
|
2023-11-16 18:21:52 +01:00
|
|
|
func NewPeersUpdateManager(metrics telemetry.AppMetrics) *PeersUpdateManager {
|
2021-08-29 17:48:31 +02:00
|
|
|
return &PeersUpdateManager{
|
2024-10-23 12:05:02 +02:00
|
|
|
peerChannels: make(map[string]chan *UpdateMessage),
|
|
|
|
peerUpdateMessage: make(map[string]*UpdateMessage),
|
|
|
|
channelsMux: &sync.RWMutex{},
|
|
|
|
metrics: metrics,
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendUpdate sends update message to the peer's channel
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) SendUpdate(ctx context.Context, peerID string, update *UpdateMessage) {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
var found, dropped bool
|
|
|
|
|
2024-10-23 12:05:02 +02:00
|
|
|
// skip sending sync update to the peer if there is no change in update message,
|
|
|
|
// it will not check on turn credential refresh as we do not send network map or client posture checks
|
|
|
|
if update.NetworkMap != nil {
|
|
|
|
updated := p.handlePeerMessageUpdate(ctx, peerID, update)
|
|
|
|
if !updated {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
p.channelsMux.Lock()
|
2024-10-23 12:05:02 +02:00
|
|
|
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountSendUpdateDuration(time.Since(start), found, dropped)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-10-23 12:05:02 +02:00
|
|
|
if update.NetworkMap != nil {
|
|
|
|
lastSentUpdate := p.peerUpdateMessage[peerID]
|
|
|
|
if lastSentUpdate != nil && lastSentUpdate.Update.NetworkMap.GetSerial() > update.Update.NetworkMap.GetSerial() {
|
|
|
|
log.WithContext(ctx).Debugf("peer %s new network map serial: %d not greater than last sent: %d, skip sending update",
|
|
|
|
peerID, update.Update.NetworkMap.GetSerial(), lastSentUpdate.Update.NetworkMap.GetSerial())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.peerUpdateMessage[peerID] = update
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
2023-11-16 18:21:52 +01:00
|
|
|
found = true
|
2022-08-27 12:57:03 +02:00
|
|
|
select {
|
|
|
|
case channel <- update:
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("update was sent to channel for peer %s", peerID)
|
2022-08-27 12:57:03 +02:00
|
|
|
default:
|
2023-11-16 18:21:52 +01:00
|
|
|
dropped = true
|
2024-09-06 16:28:19 +02:00
|
|
|
log.WithContext(ctx).Warnf("channel for peer %s is %d full or closed", peerID, len(channel))
|
2022-08-27 12:57:03 +02:00
|
|
|
}
|
2023-10-03 16:46:58 +02:00
|
|
|
} else {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("peer %s has no channel", peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateChannel creates a go channel for a given peer used to deliver updates relevant to the peer.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) CreateChannel(ctx context.Context, peerID string) chan *UpdateMessage {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
closed := false
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountCreateChannelDuration(time.Since(start), closed)
|
|
|
|
}
|
|
|
|
}()
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
2023-11-16 18:21:52 +01:00
|
|
|
closed = true
|
2023-02-03 10:33:28 +01:00
|
|
|
delete(p.peerChannels, peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
close(channel)
|
2024-10-23 12:05:02 +02:00
|
|
|
delete(p.peerUpdateMessage, peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
2023-08-18 19:23:11 +02:00
|
|
|
// mbragin: todo shouldn't it be more? or configurable?
|
2022-08-27 12:57:03 +02:00
|
|
|
channel := make(chan *UpdateMessage, channelBufferSize)
|
2023-02-03 10:33:28 +01:00
|
|
|
p.peerChannels[peerID] = channel
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("opened updates channel for a peer %s", peerID)
|
2023-11-16 18:21:52 +01:00
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) closeChannel(ctx context.Context, peerID string) {
|
2023-02-03 10:33:28 +01:00
|
|
|
if channel, ok := p.peerChannels[peerID]; ok {
|
|
|
|
delete(p.peerChannels, peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
close(channel)
|
2024-10-23 12:05:02 +02:00
|
|
|
delete(p.peerUpdateMessage, peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("closed updates channel of a peer %s", peerID)
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
2022-10-16 13:33:46 +02:00
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
// CloseChannels closes updates channel for each given peer
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) CloseChannels(ctx context.Context, peerIDs []string) {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountCloseChannelsDuration(time.Since(start), len(peerIDs))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
for _, id := range peerIDs {
|
2024-07-03 11:33:02 +02:00
|
|
|
p.closeChannel(ctx, id)
|
2023-02-27 16:44:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseChannel closes updates channel of a given peer
|
2024-07-03 11:33:02 +02:00
|
|
|
func (p *PeersUpdateManager) CloseChannel(ctx context.Context, peerID string) {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountCloseChannelDuration(time.Since(start))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
p.closeChannel(ctx, peerID)
|
2023-02-27 16:44:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
// GetAllConnectedPeers returns a copy of the connected peers map
|
|
|
|
func (p *PeersUpdateManager) GetAllConnectedPeers() map[string]struct{} {
|
2023-11-16 18:21:52 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
p.channelsMux.Lock()
|
2023-11-16 18:21:52 +01:00
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
m := make(map[string]struct{})
|
2023-11-16 18:21:52 +01:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountGetAllConnectedPeersDuration(time.Since(start), len(m))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
for ID := range p.peerChannels {
|
|
|
|
m[ID] = struct{}{}
|
2022-10-16 13:33:46 +02:00
|
|
|
}
|
2023-11-16 18:21:52 +01:00
|
|
|
|
2022-10-16 13:33:46 +02:00
|
|
|
return m
|
|
|
|
}
|
2023-12-05 14:17:56 +01:00
|
|
|
|
|
|
|
// HasChannel returns true if peers has channel in update manager, otherwise false
|
|
|
|
func (p *PeersUpdateManager) HasChannel(peerID string) bool {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
p.channelsMux.Lock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.channelsMux.Unlock()
|
|
|
|
if p.metrics != nil {
|
|
|
|
p.metrics.UpdateChannelMetrics().CountHasChannelDuration(time.Since(start))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, ok := p.peerChannels[peerID]
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
2024-10-23 12:05:02 +02:00
|
|
|
|
|
|
|
// handlePeerMessageUpdate checks if the update message for a peer is new and should be sent.
|
|
|
|
func (p *PeersUpdateManager) handlePeerMessageUpdate(ctx context.Context, peerID string, update *UpdateMessage) bool {
|
|
|
|
p.channelsMux.RLock()
|
|
|
|
lastSentUpdate := p.peerUpdateMessage[peerID]
|
|
|
|
p.channelsMux.RUnlock()
|
|
|
|
|
|
|
|
if lastSentUpdate != nil {
|
2024-10-30 16:53:23 +01:00
|
|
|
updated, err := isNewPeerUpdateMessage(ctx, lastSentUpdate, update, p.metrics)
|
2024-10-23 12:05:02 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("error checking for SyncResponse updates: %v", err)
|
2024-10-30 16:53:23 +01:00
|
|
|
return true
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
|
|
|
if !updated {
|
|
|
|
log.WithContext(ctx).Debugf("peer %s network map is not updated, skip sending update", peerID)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// isNewPeerUpdateMessage checks if the given current update message is a new update that should be sent.
|
2024-10-30 16:53:23 +01:00
|
|
|
func isNewPeerUpdateMessage(ctx context.Context, lastSentUpdate, currUpdateToSend *UpdateMessage, metric telemetry.AppMetrics) (isNew bool, err error) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2024-10-23 12:05:02 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.WithContext(ctx).Panicf("comparing peer update messages. Trace: %s", debug.Stack())
|
|
|
|
isNew, err = true, nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if lastSentUpdate.Update.NetworkMap.GetSerial() > currUpdateToSend.Update.NetworkMap.GetSerial() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
differ, err := diff.NewDiffer(
|
|
|
|
diff.CustomValueDiffers(&differs.NetIPAddr{}),
|
|
|
|
diff.CustomValueDiffers(&differs.NetIPPrefix{}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to create differ: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSentFiles := getChecksFiles(lastSentUpdate.Update.Checks)
|
|
|
|
currFiles := getChecksFiles(currUpdateToSend.Update.Checks)
|
|
|
|
|
|
|
|
changelog, err := differ.Diff(lastSentFiles, currFiles)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to diff checks: %v", err)
|
|
|
|
}
|
|
|
|
if len(changelog) > 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
changelog, err = differ.Diff(lastSentUpdate.NetworkMap, currUpdateToSend.NetworkMap)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to diff network map: %v", err)
|
|
|
|
}
|
2024-10-30 16:53:23 +01:00
|
|
|
|
|
|
|
if metric != nil {
|
|
|
|
metric.UpdateChannelMetrics().CountNetworkMapDiffDurationMicro(time.Since(startTime))
|
|
|
|
}
|
|
|
|
|
2024-10-23 12:05:02 +02:00
|
|
|
return len(changelog) > 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getChecksFiles returns a list of files from the given checks.
|
|
|
|
func getChecksFiles(checks []*proto.Checks) []string {
|
|
|
|
files := make([]string, 0, len(checks))
|
|
|
|
for _, check := range checks {
|
|
|
|
files = append(files, check.GetFiles()...)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|