Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Delete obsolete Hubs from database
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Jan 18, 2022
1 parent ce9632c commit 06df0be
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
21 changes: 18 additions & 3 deletions hub/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,24 @@ func (hub *Hub) Save() error {
return db.Put(hub)
}

// RemoveHub deletes a Hub from the database.
func RemoveHub(mapName string, hubID string) error {
return db.Delete(MakeHubDBKey(mapName, hubID))
// RemoveHubAndMsgs deletes a Hub and it's saved messages from the database.
func RemoveHubAndMsgs(mapName string, hubID string) (err error) {
err = db.Delete(MakeHubDBKey(mapName, hubID))
if err != nil {
return fmt.Errorf("failed to delete hub: %w", err)
}

err = db.Delete(MakeHubMsgDBKey(mapName, MsgTypeAnnouncement, hubID))
if err != nil {
return fmt.Errorf("failed to delete hub announcement: %w", err)
}

err = db.Delete(MakeHubMsgDBKey(mapName, MsgTypeStatus, hubID))
if err != nil {
return fmt.Errorf("failed to delete hub status: %w", err)
}

return nil
}

// HubMsg stores raw Hub messages.
Expand Down
35 changes: 35 additions & 0 deletions hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const (
ScopeTest Scope = 0xFF
)

const (
obsoleteValidAfter = 30 * 24 * time.Hour
obsoleteInvalidAfter = 7 * 24 * time.Hour
)

type MsgType string

const (
Expand Down Expand Up @@ -203,6 +208,36 @@ func (h *Hub) getName() string {
}
}

// Obsolete returns if the Hub is obsolete and may be deleted.
func (h *Hub) Obsolete() bool {
h.Lock()
defer h.Unlock()

// Check if Hub is valid.
var valid bool
switch {
case h.InvalidInfo:
case h.InvalidStatus:
case h.Status.Version == VersionOffline:
// Treat offline as invalid.
default:
valid = true
}

// Check when Hub was last seen.
lastSeen := h.FirstSeen
if h.Status.Timestamp != 0 {
lastSeen = time.Unix(h.Status.Timestamp, 0)
}

// Check if Hub is obsolete.
if valid {
return time.Now().Add(-obsoleteValidAfter).After(lastSeen)
} else {
return time.Now().Add(-obsoleteInvalidAfter).After(lastSeen)
}
}

// Equal returns whether the given Announcements are equal.
func (a *Announcement) Equal(b *Announcement) bool {
switch {
Expand Down
40 changes: 24 additions & 16 deletions navigator/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,22 @@ func (m *Map) RemoveHub(id string) {
}
delete(m.all, id)

// Remove lanes from removed Pin.
for id, _ := range pin.ConnectedTo {
// Remove Lane from peer.
peer, ok := m.all[id]
if ok {
delete(peer.ConnectedTo, pin.Hub.ID)
peer.pushChanges.Set()
}
}

// Push update to subscriptions.
export := pin.Export()
export.Meta().Delete()
mapDBController.PushUpdate(export)
// Push lane changes.
m.PushPinChanges()
}

// UpdateHub updates a Hub on the Map.
Expand Down Expand Up @@ -337,34 +349,30 @@ func (m *Map) updateHubLane(pin *Pin, lane *hub.Lane, peer *Pin) {
}

func (m *Map) updateStates(ctx context.Context, task *modules.Task) error {
now := time.Now()
oneMonthAgo := now.Add(-33 * 24 * time.Hour).Unix()
m.RLock()
defer m.RUnlock()

now := time.Now()
for _, pin := range m.all {
// Update StateFailing
// Update StateFailing.
if pin.State.has(StateFailing) && now.After(pin.FailingUntil) {
pin.removeStates(StateFailing)
}

// Delete stale Hubs that haven't been updated in over a month.
if pin.Hub.Info.Timestamp > 0 &&
pin.Hub.Info.Timestamp < oneMonthAgo &&
pin.Hub.Status.Timestamp < oneMonthAgo {
if pin.Hub.KeyIsSet() {
err := db.Delete(pin.Hub.Key())
if err != nil {
log.Warningf("spn/navigator: failed to delete stale %s: %s", pin.Hub, err)
}
} else {
m.RemoveHub(pin.Hub.ID)
// Delete obsolete Hubs.
if pin.State.hasNoneOf(StateActive) && pin.Hub.Obsolete() {
err := hub.RemoveHubAndMsgs(m.Name, pin.Hub.ID)
if err != nil {
log.Warningf("navigator: failed to delete obsolete %s", pin.Hub)
}
m.RemoveHub(pin.Hub.ID)
}
}

// Update StateActive
// Update StateActive.
m.updateActiveHubs()

// Update StateReachable
// Update StateReachable.
return m.recalculateReachableHubs()
}

Expand Down

0 comments on commit 06df0be

Please sign in to comment.