|
@@ -4,7 +4,6 @@ import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
- "sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
@@ -25,8 +24,6 @@ type ServerStatus struct {
|
|
|
Failover map[string]bool `json:"is_failover_existed"`
|
|
|
}
|
|
|
|
|
|
-const batchSize = 50
|
|
|
-
|
|
|
// PublishPeerUpdate --- determines and publishes a peer update to all the hosts
|
|
|
func PublishPeerUpdate(replacePeers bool) error {
|
|
|
if !servercfg.IsMessageQueueBackend() {
|
|
@@ -43,58 +40,14 @@ func PublishPeerUpdate(replacePeers bool) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- var wg sync.WaitGroup
|
|
|
- hostLen := len(hosts)
|
|
|
- batch := batchSize
|
|
|
- div := hostLen / batch
|
|
|
- mod := hostLen % batch
|
|
|
-
|
|
|
- if div == 0 {
|
|
|
- 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()
|
|
|
- } else {
|
|
|
- for i := 0; i < div*batch; i += batch {
|
|
|
- wg.Add(batch)
|
|
|
- for j := 0; j < batch; j++ {
|
|
|
- host := hosts[i+j]
|
|
|
- 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)
|
|
|
+ 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())
|
|
|
}
|
|
|
- wg.Wait()
|
|
|
- }
|
|
|
- if mod != 0 {
|
|
|
- wg.Add(hostLen - (div * batch))
|
|
|
- for k := div * batch; k < hostLen; k++ {
|
|
|
- host := hosts[k]
|
|
|
- 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()
|
|
|
- }
|
|
|
+ }(host)
|
|
|
}
|
|
|
-
|
|
|
- // 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 err
|
|
|
}
|
|
|
|
|
@@ -115,55 +68,12 @@ func PublishDeletedNodePeerUpdate(delNode *models.Node) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- var wg sync.WaitGroup
|
|
|
- hostLen := len(hosts)
|
|
|
- batch := batchSize
|
|
|
- div := hostLen / batch
|
|
|
- mod := hostLen % batch
|
|
|
-
|
|
|
- if div == 0 {
|
|
|
- wg.Add(hostLen)
|
|
|
- for i := 0; i < hostLen; i++ {
|
|
|
- host := hosts[i]
|
|
|
- go func(host models.Host) {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, allNodes, delNode, nil, false, &wg); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }(host)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
- } else {
|
|
|
- for i := 0; i < div*batch; i += batch {
|
|
|
- wg.Add(batch)
|
|
|
- for j := 0; j < batch; j++ {
|
|
|
- host := hosts[i+j]
|
|
|
- go func(host models.Host) {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, allNodes, delNode, nil, false, &wg); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }(host)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
- }
|
|
|
- if mod != 0 {
|
|
|
- wg.Add(hostLen - (div * batch))
|
|
|
- for k := div * batch; k < hostLen; k++ {
|
|
|
- host := hosts[k]
|
|
|
- go func(host models.Host) {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, allNodes, delNode, nil, false, &wg); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }(host)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
+ for _, host := range hosts {
|
|
|
+ host := host
|
|
|
+ if err = PublishSingleHostPeerUpdate(&host, allNodes, delNode, nil, false); err != nil {
|
|
|
+ logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
}
|
|
|
}
|
|
|
- // for _, host := range hosts {
|
|
|
- // host := host
|
|
|
- // 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())
|
|
|
- // }
|
|
|
- // }
|
|
|
return err
|
|
|
}
|
|
|
|
|
@@ -184,72 +94,19 @@ func PublishDeletedClientPeerUpdate(delClient *models.ExtClient) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- var wg sync.WaitGroup
|
|
|
- hostLen := len(hosts)
|
|
|
- batch := batchSize
|
|
|
- div := hostLen / batch
|
|
|
- mod := hostLen % batch
|
|
|
-
|
|
|
- if div == 0 {
|
|
|
- wg.Add(hostLen)
|
|
|
- for i := 0; i < hostLen; i++ {
|
|
|
- host := hosts[i]
|
|
|
- go func(host models.Host) {
|
|
|
- if host.OS != models.OS_Types.IoT {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, nodes, nil, []models.ExtClient{*delClient}, false, &wg); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }
|
|
|
- }(host)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
- } else {
|
|
|
- for i := 0; i < div*batch; i += batch {
|
|
|
- wg.Add(batch)
|
|
|
- for j := 0; j < batch; j++ {
|
|
|
- host := hosts[i+j]
|
|
|
- go func(host models.Host) {
|
|
|
- if host.OS != models.OS_Types.IoT {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, nodes, nil, []models.ExtClient{*delClient}, false, &wg); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }
|
|
|
- }(host)
|
|
|
+ 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 {
|
|
|
+ logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
}
|
|
|
- wg.Wait()
|
|
|
- }
|
|
|
- if mod != 0 {
|
|
|
- wg.Add(hostLen - (div * batch))
|
|
|
- for k := div * batch; k < hostLen; k++ {
|
|
|
- host := hosts[k]
|
|
|
- go func(host models.Host) {
|
|
|
- if host.OS != models.OS_Types.IoT {
|
|
|
- if err = PublishSingleHostPeerUpdate(&host, nodes, nil, []models.ExtClient{*delClient}, false, &wg); err != nil {
|
|
|
- logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
|
|
|
- }
|
|
|
- }
|
|
|
- }(host)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // for _, host := range hosts {
|
|
|
- // host := host
|
|
|
- // if host.OS != models.OS_Types.IoT {
|
|
|
- // 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())
|
|
|
- // }
|
|
|
- // }
|
|
|
- // }
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
// 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, wg *sync.WaitGroup) error {
|
|
|
- if wg != nil {
|
|
|
- defer wg.Done()
|
|
|
- }
|
|
|
+func PublishSingleHostPeerUpdate(host *models.Host, allNodes []models.Node, deletedNode *models.Node, deletedClients []models.ExtClient, replacePeers bool) error {
|
|
|
peerUpdate, err := logic.GetPeerUpdateForHost("", host, allNodes, deletedNode, deletedClients)
|
|
|
if err != nil {
|
|
|
return err
|