|
@@ -2,7 +2,6 @@ package logic
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "net"
|
|
|
"time"
|
|
|
|
|
|
"github.com/google/uuid"
|
|
@@ -18,15 +17,16 @@ const (
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- zombies []uuid.UUID
|
|
|
- removeZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
|
- newZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
|
+ zombies []uuid.UUID
|
|
|
+ hostZombies []uuid.UUID
|
|
|
+ newZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
|
+ newHostZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
|
)
|
|
|
|
|
|
-// CheckZombies - checks if new node has same macaddress as existing node
|
|
|
+// CheckZombies - checks if new node has same hostid as existing node
|
|
|
// if so, existing node is added to zombie node quarantine list
|
|
|
// also cleans up nodes past their expiration date
|
|
|
-func CheckZombies(newnode *models.Node, mac net.HardwareAddr) {
|
|
|
+func CheckZombies(newnode *models.Node) {
|
|
|
nodes, err := GetNetworkNodes(newnode.Network)
|
|
|
if err != nil {
|
|
|
logger.Log(1, "Failed to retrieve network nodes", newnode.Network, err.Error())
|
|
@@ -44,6 +44,35 @@ func CheckZombies(newnode *models.Node, mac net.HardwareAddr) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// checkForZombieHosts - checks if new host has the same macAddress as an existing host
|
|
|
+// if true, existing host is added to host zombie collection
|
|
|
+func checkForZombieHosts(h *models.Host) {
|
|
|
+ hosts, err := GetAllHosts()
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(3, "errror retrieving all hosts", err.Error())
|
|
|
+ }
|
|
|
+ for _, existing := range hosts {
|
|
|
+ if existing.ID == h.ID {
|
|
|
+ //probably an unnecessary check as new host should not be in database yet, but just in case
|
|
|
+ //skip self
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if existing.MacAddress.String() == h.MacAddress.String() {
|
|
|
+ //add to hostZombies
|
|
|
+ newHostZombie <- existing.ID
|
|
|
+ //add all nodes belonging to host to zombile list
|
|
|
+ for _, node := range existing.Nodes {
|
|
|
+ id, err := uuid.Parse(node)
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(3, "error parsing uuid from host.Nodes", err.Error())
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ newHostZombie <- id
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// ManageZombies - goroutine which adds/removes/deletes nodes from the zombie node quarantine list
|
|
|
func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
|
|
logger.Log(2, "Zombie management started")
|
|
@@ -51,24 +80,12 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
|
|
for {
|
|
|
select {
|
|
|
case <-ctx.Done():
|
|
|
+ close(peerUpdate)
|
|
|
return
|
|
|
case id := <-newZombie:
|
|
|
- logger.Log(1, "adding", id.String(), "to zombie quaratine list")
|
|
|
zombies = append(zombies, id)
|
|
|
- case id := <-removeZombie:
|
|
|
- found := false
|
|
|
- if len(zombies) > 0 {
|
|
|
- for i := len(zombies) - 1; i >= 0; i-- {
|
|
|
- if zombies[i] == id {
|
|
|
- logger.Log(1, "removing zombie from quaratine list", zombies[i].String())
|
|
|
- zombies = append(zombies[:i], zombies[i+1:]...)
|
|
|
- found = true
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if !found {
|
|
|
- logger.Log(3, "no zombies found")
|
|
|
- }
|
|
|
+ case id := <-newHostZombie:
|
|
|
+ hostZombies = append(hostZombies, id)
|
|
|
case <-time.After(time.Second * ZOMBIE_TIMEOUT):
|
|
|
logger.Log(3, "checking for zombie nodes")
|
|
|
if len(zombies) > 0 {
|
|
@@ -92,6 +109,23 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if len(hostZombies) > 0 {
|
|
|
+ logger.Log(3, "checking host zombies")
|
|
|
+ for i := len(hostZombies) - 1; i >= 0; i-- {
|
|
|
+ host, err := GetHost(hostZombies[i].String())
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(1, "error retrieving zombie host", err.Error())
|
|
|
+ logger.Log(1, "deleting ", host.ID.String(), " from zombie list")
|
|
|
+ zombies = append(zombies[:i], zombies[i+1:]...)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if len(host.Nodes) == 0 {
|
|
|
+ if err := RemoveHost(host); err != nil {
|
|
|
+ logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -115,10 +149,10 @@ func InitializeZombies() {
|
|
|
}
|
|
|
if node.HostID == othernode.HostID {
|
|
|
if node.LastCheckIn.After(othernode.LastCheckIn) {
|
|
|
- zombies = append(zombies, othernode.ID)
|
|
|
+ newZombie <- othernode.ID
|
|
|
logger.Log(1, "adding", othernode.ID.String(), "to zombie list")
|
|
|
} else {
|
|
|
- zombies = append(zombies, node.ID)
|
|
|
+ newZombie <- node.ID
|
|
|
logger.Log(1, "adding", node.ID.String(), "to zombie list")
|
|
|
}
|
|
|
}
|