Skip to content

Commit

Permalink
NET-507 (#2506)
Browse files Browse the repository at this point in the history
* NET-507

* Fixed server restart changing the node expiration date to the defaults.

* Removed expired nodes removal from zombie cleanup routine.

* Added a new expired nodes deletion routine which removes expired nodes every hour.

* NET-507 suggested changes

* Possible fix for zombie nodes upon node deletion from the UI.

* Suggested changes implemented for expired nodes deletion go routine.

* NET-507 typo fix

* typo fix for DeleteNode purge parameter
  • Loading branch information
uGiFarukh authored and mattkasun committed Aug 14, 2023
1 parent 3c86c9b commit b86bdc6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ func deleteHostFromNetwork(w http.ResponseWriter, r *http.Request) {
// unset all the relayed nodes
logic.SetRelayedNodes(false, node.ID.String(), node.RelayedNodes)
}
node.Action = models.NODE_DELETE
node.PendingDelete = true
logger.Log(1, "deleting node", node.ID.String(), "from host", currHost.Name)
logger.Log(1, "deleting node", node.ID.String(), "from host", currHost.Name)
if err := logic.DeleteNode(node, forceDelete); err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("failed to delete node"), "internal"))
return
}
node.Action = models.NODE_DELETE
node.PendingDelete = true
// notify node change
runUpdates(node, false)
go func() { // notify of peer change
Expand Down
31 changes: 31 additions & 0 deletions logic/nodes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logic

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
"github.com/gravitl/netmaker/validation"
"golang.org/x/exp/slog"
)

var (
Expand Down Expand Up @@ -444,6 +446,35 @@ func GetAllNodesAPI(nodes []models.Node) []models.ApiNode {
return apiNodes[:]
}

// DeleteExpiredNodes - goroutine which deletes nodes which are expired
func DeleteExpiredNodes(ctx context.Context, peerUpdate chan *models.Node) {
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Hour):
// Delete Expired Nodes Every Hour
allnodes, err := GetAllNodes()
if err != nil {
slog.Error("failed to retrieve all nodes", "error", err.Error())
return
}
for _, node := range allnodes {
if time.Now().After(node.ExpirationDateTime) {
if err := DeleteNode(&node, false); err != nil {
slog.Error("error deleting expired node", "nodeid", node.ID.String(), "error", err.Error())
continue
}
node.Action = models.NODE_DELETE
node.PendingDelete = true
peerUpdate <- &node
slog.Info("deleting expired node", "nodeid", node.ID.String())
}
}
}
}
}

// == PRO ==

func updateProNodeACLS(node *models.Node) error {
Expand Down
4 changes: 2 additions & 2 deletions logic/zombie.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func CheckZombies(newnode *models.Node) {
//skip self
continue
}
if node.HostID == newnode.HostID || time.Now().After(node.ExpirationDateTime) {
if node.HostID == newnode.HostID {
logger.Log(0, "adding ", node.ID.String(), " to zombie list")
newZombie <- node.ID
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
zombies = append(zombies[:i], zombies[i+1:]...)
continue
}
if time.Since(node.LastCheckIn) > time.Minute*ZOMBIE_DELETE_TIME || time.Now().After(node.ExpirationDateTime) {
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
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func runMessageQueue(wg *sync.WaitGroup, ctx context.Context) {
go func() {
peerUpdate := make(chan *models.Node)
go logic.ManageZombies(ctx, peerUpdate)
go logic.DeleteExpiredNodes(ctx, peerUpdate)
for nodeUpdate := range peerUpdate {
if err := mq.NodeUpdate(nodeUpdate); err != nil {
logger.Log(0, "failed to send peer update for deleted node: ", nodeUpdate.ID.String(), err.Error())
Expand Down
4 changes: 3 additions & 1 deletion models/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ func (node *Node) SetLastPeerUpdate() {

// Node.SetExpirationDateTime - sets node expiry time
func (node *Node) SetExpirationDateTime() {
node.ExpirationDateTime = time.Now().Add(TEN_YEARS_IN_SECONDS)
if node.ExpirationDateTime.IsZero() {
node.ExpirationDateTime = time.Now().Add(TEN_YEARS_IN_SECONDS)
}
}

// Node.SetDefaultName - sets a random name to node
Expand Down

0 comments on commit b86bdc6

Please sign in to comment.