|
@@ -4,6 +4,7 @@ import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
@@ -13,6 +14,9 @@ import (
|
|
|
"golang.org/x/exp/slog"
|
|
|
)
|
|
|
|
|
|
+var batchSize = servercfg.GetPeerUpdateBatchSize()
|
|
|
+var batchUpdate = servercfg.GetBatchPeerUpdate()
|
|
|
+
|
|
|
// PublishPeerUpdate --- determines and publishes a peer update to all the hosts
|
|
|
func PublishPeerUpdate(replacePeers bool) error {
|
|
|
if !servercfg.IsMessageQueueBackend() {
|
|
@@ -28,15 +32,37 @@ func PublishPeerUpdate(replacePeers bool) error {
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- for _, host := range hosts {
|
|
|
- host := host
|
|
|
- go func(host models.Host) {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, allNodes, nil, nil, replacePeers); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }(host)
|
|
|
+
|
|
|
+ //if batch peer update disabled
|
|
|
+ if !batchUpdate {
|
|
|
+ for _, host := range hosts {
|
|
|
+ host := host
|
|
|
+ go func(host models.Host) {
|
|
|
+ if err = PublishSingleHostPeerUpdate(&host, allNodes, nil, nil, replacePeers, nil); err != nil {
|
|
|
+ logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
+ }
|
|
|
+ }(host)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|
|
|
- return err
|
|
|
+
|
|
|
+ //if batch peer update enabled
|
|
|
+ batchHost := BatchItems(hosts, batchSize)
|
|
|
+ var wg sync.WaitGroup
|
|
|
+ for _, v := range batchHost {
|
|
|
+ hostLen := len(v)
|
|
|
+ wg.Add(hostLen)
|
|
|
+ for i := 0; i < hostLen; i++ {
|
|
|
+ host := hosts[i]
|
|
|
+ go func(host models.Host) {
|
|
|
+ if err = PublishSingleHostPeerUpdate(&host, allNodes, nil, nil, replacePeers, &wg); err != nil {
|
|
|
+ logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
+ }
|
|
|
+ }(host)
|
|
|
+ }
|
|
|
+ wg.Wait()
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// PublishDeletedNodePeerUpdate --- determines and publishes a peer update
|
|
@@ -57,7 +83,7 @@ func PublishDeletedNodePeerUpdate(delNode *models.Node) error {
|
|
|
}
|
|
|
for _, host := range hosts {
|
|
|
host := host
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, allNodes, delNode, nil, false); err != nil {
|
|
|
+ if err = PublishSingleHostPeerUpdate(&host, allNodes, delNode, nil, false, nil); err != nil {
|
|
|
logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
}
|
|
|
}
|
|
@@ -83,7 +109,7 @@ func PublishDeletedClientPeerUpdate(delClient *models.ExtClient) error {
|
|
|
for _, host := range hosts {
|
|
|
host := host
|
|
|
if host.OS != models.OS_Types.IoT {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, nodes, nil, []models.ExtClient{*delClient}, false); err != nil {
|
|
|
+ if err = PublishSingleHostPeerUpdate(&host, nodes, nil, []models.ExtClient{*delClient}, false, nil); err != nil {
|
|
|
logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
}
|
|
|
}
|
|
@@ -92,7 +118,10 @@ func PublishDeletedClientPeerUpdate(delClient *models.ExtClient) error {
|
|
|
}
|
|
|
|
|
|
// PublishSingleHostPeerUpdate --- determines and publishes a peer update to one host
|
|
|
-func PublishSingleHostPeerUpdate(host *models.Host, allNodes []models.Node, deletedNode *models.Node, deletedClients []models.ExtClient, replacePeers bool) error {
|
|
|
+func PublishSingleHostPeerUpdate(host *models.Host, allNodes []models.Node, deletedNode *models.Node, deletedClients []models.ExtClient, replacePeers bool, wg *sync.WaitGroup) error {
|
|
|
+ if wg != nil {
|
|
|
+ defer wg.Done()
|
|
|
+ }
|
|
|
peerUpdate, err := logic.GetPeerUpdateForHost("", host, allNodes, deletedNode, deletedClients)
|
|
|
if err != nil {
|
|
|
return err
|