mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-10 15:14:22 +08:00
* add additional mutex lock on node acls func * increase verbosity * disable acls on cloud emqx * add emqx creds creation to go routine * add debug log of mq client id * comment port check * uncomment port check * check for connection mq connection open * use username for client id * add write mutex on acl is allowed * add mq connection lost handler on server * spin off zombie init as go routine * get whole api path from config * Revert "get whole api path from config" This reverts commit392f5f4c5f
. * update extclient acls async * add additional mutex lock on node acls func (cherry picked from commit5325f0e7d7
) * increase verbosity (cherry picked from commit705b3cf0bf
) * add emqx creds creation to go routine (cherry picked from commitc8e65f4820
) * add debug log of mq client id (cherry picked from commit29c5d6ceca
) * comment port check (cherry picked from commitdb8d6d95ea
) * check for connection mq connection open (cherry picked from commit13b11033b0
) * use username for client id (cherry picked from commite90c7386de
) * add write mutex on acl is allowed (cherry picked from commit4cae1b0bb4
) * add mq connection lost handler on server (cherry picked from commitc82918ad35
) * spin off zombie init as go routine (cherry picked from commit6d65c44c43
) * update extclient acls async (cherry picked from commit6557ef1ebe
) * additionl logs for oauth user flow (cherry picked from commit61703038ae
) * add more debug logs (cherry picked from commit5980beacd1
) * add more debug logs (cherry picked from commit4d001f0d27
) * add set auth secret (cherry picked from commitf41cef5da5
) * fix fetch pass (cherry picked from commit825caf4b60
) * make sure auth secret is set only once (cherry picked from commitba33ed02aa
) * make sure auth secret is set only once (cherry picked from commit920ac4c507
) * comment usage of emqx acls * replace read lock with write lock on acls * replace read lock with write lock on acls (cherry picked from commit808d2135c8
) * use deadlock pkg for visibility * add additional mutex locks * remove race flag * on mq re-connecting donot exit if failed * on mq re-connecting donot exit if failed * revert mutex package change * set mq clean session * remove debug log * go mod tidy * revert on prem emqx acls del
170 lines
5 KiB
Go
170 lines
5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gravitl/netmaker/logger"
|
|
"github.com/gravitl/netmaker/models"
|
|
)
|
|
|
|
const (
|
|
// ZOMBIE_TIMEOUT - timeout in hours for checking zombie status
|
|
ZOMBIE_TIMEOUT = 6
|
|
// ZOMBIE_DELETE_TIME - timeout in minutes for zombie node deletion
|
|
ZOMBIE_DELETE_TIME = 10
|
|
)
|
|
|
|
var (
|
|
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 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) {
|
|
nodes, err := GetNetworkNodes(newnode.Network)
|
|
if err != nil {
|
|
logger.Log(1, "Failed to retrieve network nodes", newnode.Network, err.Error())
|
|
return
|
|
}
|
|
for _, node := range nodes {
|
|
if node.ID == newnode.ID {
|
|
//skip self
|
|
continue
|
|
}
|
|
if node.HostID == newnode.HostID {
|
|
logger.Log(0, "adding ", node.ID.String(), " to zombie list")
|
|
newZombie <- node.ID
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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")
|
|
go InitializeZombies()
|
|
|
|
// Zombie Nodes Cleanup Four Times a Day
|
|
ticker := time.NewTicker(time.Hour * ZOMBIE_TIMEOUT)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
ticker.Stop()
|
|
close(peerUpdate)
|
|
return
|
|
case id := <-newZombie:
|
|
zombies = append(zombies, id)
|
|
case id := <-newHostZombie:
|
|
hostZombies = append(hostZombies, id)
|
|
case <-ticker.C: // run this check 4 times a day
|
|
logger.Log(3, "checking for zombie nodes")
|
|
if len(zombies) > 0 {
|
|
for i := len(zombies) - 1; i >= 0; i-- {
|
|
node, err := GetNodeByID(zombies[i].String())
|
|
if err != nil {
|
|
logger.Log(1, "error retrieving zombie node", zombies[i].String(), err.Error())
|
|
logger.Log(1, "deleting ", node.ID.String(), " from zombie list")
|
|
zombies = append(zombies[:i], zombies[i+1:]...)
|
|
continue
|
|
}
|
|
if time.Since(node.LastCheckIn) > time.Minute*ZOMBIE_DELETE_TIME {
|
|
if err := DeleteNode(&node, true); err != nil {
|
|
logger.Log(1, "error deleting zombie node", zombies[i].String(), err.Error())
|
|
continue
|
|
}
|
|
node.PendingDelete = true
|
|
node.Action = models.NODE_DELETE
|
|
peerUpdate <- &node
|
|
logger.Log(1, "deleting zombie node", node.ID.String())
|
|
zombies = append(zombies[:i], zombies[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
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())
|
|
if host != nil {
|
|
logger.Log(1, "deleting ", host.ID.String(), " from zombie list")
|
|
}
|
|
hostZombies = append(hostZombies[:i], hostZombies[i+1:]...)
|
|
continue
|
|
}
|
|
if len(host.Nodes) == 0 {
|
|
if err := RemoveHost(host, true); err != nil {
|
|
logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
|
|
}
|
|
hostZombies = append(hostZombies[:i], hostZombies[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// InitializeZombies - populates the zombie quarantine list (should be called from initialization)
|
|
func InitializeZombies() {
|
|
nodes, err := GetAllNodes()
|
|
if err != nil {
|
|
logger.Log(1, "failed to retrieve nodes", err.Error())
|
|
return
|
|
}
|
|
for _, node := range nodes {
|
|
othernodes, err := GetNetworkNodes(node.Network)
|
|
if err != nil {
|
|
logger.Log(1, "failled to retrieve nodes for network", node.Network, err.Error())
|
|
continue
|
|
}
|
|
for _, othernode := range othernodes {
|
|
if node.ID == othernode.ID {
|
|
continue
|
|
}
|
|
if node.HostID == othernode.HostID {
|
|
if node.LastCheckIn.After(othernode.LastCheckIn) {
|
|
newZombie <- othernode.ID
|
|
logger.Log(1, "adding", othernode.ID.String(), "to zombie list")
|
|
} else {
|
|
newZombie <- node.ID
|
|
logger.Log(1, "adding", node.ID.String(), "to zombie list")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|