storj/cmd/tools/segment-verify/nodealias.go
paul cannon 2feb49afc3 cmd/tools/segment-verify: don't cache offline status forever
Because it was originally intended to work on only a few pieces from
each segment at a time, and would frequently have reset its list of
online nodes, segment-verify has been taking nodes out of its
onlineNodes set and never putting them back. This means that over a long
run in Check=0 mode, we end up treating more and more nodes as offline,
permanently. This trend obfuscates the number of missing pieces that
each segment really has, because we don't check pieces on offline nodes.

This commit changes the onlineNodes set to an "offlineNodes" set, with
an expiration time on the offline-ness quality. So nodes are added to
the offlineNodes set when we see they are offline, and then we only
treat them as offline for the next 30 minutes (configurable). After that
point, we will try connecting to them again.

Change-Id: I14f0332de25cdc6ef655f923739bcb4df71e079e
2023-01-03 23:11:42 +00:00

80 lines
1.9 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"time"
"storj.io/storj/satellite/metabase"
)
// NodeAliasSet is a set containing node aliases.
type NodeAliasSet map[metabase.NodeAlias]struct{}
// Contains checks whether v is in the set.
func (set NodeAliasSet) Contains(v metabase.NodeAlias) bool {
_, ok := set[v]
return ok
}
// Add v to the set.
func (set NodeAliasSet) Add(v metabase.NodeAlias) {
set[v] = struct{}{}
}
// Remove v from the set.
func (set NodeAliasSet) Remove(v metabase.NodeAlias) {
delete(set, v)
}
// RemoveAll xs from the set.
func (set NodeAliasSet) RemoveAll(xs NodeAliasSet) {
for x := range xs {
delete(set, x)
}
}
type nodeAliasExpiringSet struct {
nowFunc func() time.Time
aliasesAndExpiryTimes map[metabase.NodeAlias]time.Time
timeToExpire time.Duration
}
func newNodeAliasExpiringSet(timeToExpire time.Duration) *nodeAliasExpiringSet {
return &nodeAliasExpiringSet{
nowFunc: time.Now,
aliasesAndExpiryTimes: make(map[metabase.NodeAlias]time.Time),
timeToExpire: timeToExpire,
}
}
// Contains checks whether v was added to the set since the last timeToExpire.
func (expiringSet nodeAliasExpiringSet) Contains(v metabase.NodeAlias) bool {
expiry, ok := expiringSet.aliasesAndExpiryTimes[v]
if ok {
if expiringSet.nowFunc().Before(expiry) {
return true
}
delete(expiringSet.aliasesAndExpiryTimes, v)
}
return false
}
// Add adds v to the set.
func (expiringSet nodeAliasExpiringSet) Add(v metabase.NodeAlias) {
expiringSet.aliasesAndExpiryTimes[v] = expiringSet.nowFunc().Add(expiringSet.timeToExpire)
}
// Remove removes v from the set.
func (expiringSet nodeAliasExpiringSet) Remove(v metabase.NodeAlias) {
delete(expiringSet.aliasesAndExpiryTimes, v)
}
// AddAll adds all xs to the set.
func (expiringSet nodeAliasExpiringSet) AddAll(xs NodeAliasSet) {
for x := range xs {
expiringSet.Add(x)
}
}