-
Notifications
You must be signed in to change notification settings - Fork 402
/
chore.go
74 lines (63 loc) · 2.37 KB
/
chore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package straynodes
import (
"context"
"time"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"storj.io/common/sync2"
"storj.io/storj/satellite/overlay"
)
var mon = monkit.Package()
// Config contains configurable values for stray nodes chore.
type Config struct {
EnableDQ bool `help:"whether nodes will be disqualified if they have not been contacted in some time" releaseDefault:"true" devDefault:"true"`
Interval time.Duration `help:"how often to check for and DQ stray nodes" releaseDefault:"168h" devDefault:"5m" testDefault:"1m"`
MaxDurationWithoutContact time.Duration `help:"length of time a node can go without contacting satellite before being disqualified" releaseDefault:"720h" devDefault:"7200h" testDefault:"5m"`
Limit int `help:"Max number of nodes to return in a single query. Chore will iterate until rows returned is less than limit" releaseDefault:"1000" devDefault:"1000"`
}
// Chore disqualifies stray nodes.
type Chore struct {
log *zap.Logger
cache *overlay.Service
maxDurationWithoutContact time.Duration
limit int
Loop *sync2.Cycle
}
// NewChore creates a new stray nodes Chore.
func NewChore(log *zap.Logger, cache *overlay.Service, config Config) *Chore {
return &Chore{
log: log,
cache: cache,
maxDurationWithoutContact: config.MaxDurationWithoutContact,
limit: config.Limit,
Loop: sync2.NewCycle(config.Interval),
}
}
// Run runs the chore.
func (chore *Chore) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return chore.Loop.Run(ctx, func(ctx context.Context) error {
var total int
for {
n, err := chore.cache.DQNodesLastSeenBefore(ctx, time.Now().UTC().Add(-chore.maxDurationWithoutContact), chore.limit)
if err != nil {
chore.log.Error("error disqualifying stray nodes", zap.Error(err))
mon.IntVal("stray_nodes_dq_count").Observe(int64(total))
return nil
}
total += n
if n < chore.limit {
break
}
}
mon.IntVal("stray_nodes_dq_count").Observe(int64(total))
return nil
})
}
// Close closes chore.
func (chore *Chore) Close() error {
chore.Loop.Close()
return nil
}