Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NET-507 #2506

Merged
merged 3 commits into from
Aug 10, 2023
Merged

NET-507 #2506

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
abhishek9686 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading