-
Notifications
You must be signed in to change notification settings - Fork 18
/
scanner.go
340 lines (285 loc) · 8.1 KB
/
scanner.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package autopilot
import (
"context"
"errors"
"strings"
"sync"
"sync/atomic"
"time"
rhpv2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/autopilot/contractor"
"go.sia.tech/renterd/internal/utils"
"go.uber.org/zap"
)
const (
scannerTimeoutInterval = 10 * time.Minute
scannerTimeoutMinTimeout = 10 * time.Second
trackerMinDataPoints = 25
trackerNumDataPoints = 1000
trackerTimeoutPercentile = 99
)
type (
scanner struct {
// TODO: use the actual bus and worker interfaces when they've consolidated
// a bit, we currently use inline interfaces to avoid having to update the
// scanner tests with every interface change
bus interface {
SearchHosts(ctx context.Context, opts api.SearchHostOptions) ([]api.Host, error)
HostsForScanning(ctx context.Context, opts api.HostsForScanningOptions) ([]api.HostAddress, error)
RemoveOfflineHosts(ctx context.Context, minRecentScanFailures uint64, maxDowntime time.Duration) (uint64, error)
}
tracker *tracker
logger *zap.SugaredLogger
ap *Autopilot
wg sync.WaitGroup
scanBatchSize uint64
scanThreads uint64
scanMinInterval time.Duration
timeoutMinInterval time.Duration
timeoutMinTimeout time.Duration
mu sync.Mutex
scanning bool
scanningLastStart time.Time
timeout time.Duration
timeoutLastUpdate time.Time
interruptScanChan chan struct{}
}
scanWorker interface {
RHPScan(ctx context.Context, hostKey types.PublicKey, hostIP string, timeout time.Duration) (api.RHPScanResponse, error)
}
scanReq struct {
hostKey types.PublicKey
hostIP string
}
scanResp struct {
hostKey types.PublicKey
settings rhpv2.HostSettings
err error
}
tracker struct {
threshold uint64
percentile float64
mu sync.Mutex
count uint64
timings []float64
}
)
func newTracker(threshold, total uint64, percentile float64) *tracker {
return &tracker{
threshold: threshold,
percentile: percentile,
timings: make([]float64, total),
}
}
func (t *tracker) addDataPoint(duration time.Duration) {
if duration == 0 {
return
}
t.mu.Lock()
defer t.mu.Unlock()
t.timings[t.count%uint64(len(t.timings))] = float64(duration.Milliseconds())
// NOTE: we silently overflow and disregard the threshold being reapplied
// when we overflow entirely, since we only ever increment the count with 1
// it will never happen
t.count += 1
}
func (t *tracker) timeout() time.Duration {
t.mu.Lock()
defer t.mu.Unlock()
if t.count < uint64(t.threshold) {
return 0
}
percentile, err := percentile(t.timings, t.percentile)
if err != nil {
return 0
}
return time.Duration(percentile) * time.Millisecond
}
func newScanner(ap *Autopilot, scanBatchSize, scanThreads uint64, scanMinInterval, timeoutMinInterval, timeoutMinTimeout time.Duration) (*scanner, error) {
if scanBatchSize == 0 {
return nil, errors.New("scanner batch size has to be greater than zero")
}
if scanThreads == 0 {
return nil, errors.New("scanner threads has to be greater than zero")
}
return &scanner{
bus: ap.bus,
tracker: newTracker(
trackerMinDataPoints,
trackerNumDataPoints,
trackerTimeoutPercentile,
),
logger: ap.logger.Named("scanner"),
ap: ap,
interruptScanChan: make(chan struct{}),
scanBatchSize: scanBatchSize,
scanThreads: scanThreads,
scanMinInterval: scanMinInterval,
timeoutMinInterval: timeoutMinInterval,
timeoutMinTimeout: timeoutMinTimeout,
}, nil
}
func (s *scanner) Status() (bool, time.Time) {
s.mu.Lock()
defer s.mu.Unlock()
return s.scanning, s.scanningLastStart
}
func (s *scanner) isInterrupted() bool {
select {
case <-s.interruptScanChan:
return true
default:
return false
}
}
func (s *scanner) tryPerformHostScan(ctx context.Context, w scanWorker, force bool) {
if s.ap.isStopped() {
return
}
scanType := "host scan"
if force {
scanType = "forced scan"
}
s.mu.Lock()
if force {
close(s.interruptScanChan)
s.mu.Unlock()
s.logger.Infof("waiting for ongoing scan to complete")
s.wg.Wait()
s.mu.Lock()
s.interruptScanChan = make(chan struct{})
} else if s.scanning || !s.isScanRequired() {
s.mu.Unlock()
return
}
s.scanningLastStart = time.Now()
s.scanning = true
s.mu.Unlock()
s.logger.Infof("%s started", scanType)
s.wg.Add(1)
go func(st string) {
defer s.wg.Done()
for resp := range s.launchScanWorkers(ctx, w, s.launchHostScans()) {
if s.isInterrupted() || s.ap.isStopped() {
break
}
if resp.err != nil && !strings.Contains(resp.err.Error(), "connection refused") {
s.logger.Error(resp.err)
}
}
s.mu.Lock()
s.scanning = false
s.logger.Infof("%s finished after %v", st, time.Since(s.scanningLastStart))
s.mu.Unlock()
}(scanType)
}
func (s *scanner) PruneHosts(ctx context.Context, cfg api.HostsConfig) {
maxDowntime := time.Duration(cfg.MaxDowntimeHours) * time.Hour
minRecentScanFailures := cfg.MinRecentScanFailures
if maxDowntime > 0 {
s.logger.Debugf("removing hosts that have been offline for more than %v and have failed at least %d scans", maxDowntime, minRecentScanFailures)
removed, err := s.bus.RemoveOfflineHosts(ctx, minRecentScanFailures, maxDowntime)
if err != nil {
s.logger.Errorf("error occurred while removing offline hosts, err: %v", err)
} else if removed > 0 {
s.logger.Infof("removed %v offline hosts", removed)
}
}
}
func (s *scanner) tryUpdateTimeout() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.isTimeoutUpdateRequired() {
return
}
updated := s.tracker.timeout()
if updated < s.timeoutMinTimeout {
s.logger.Infof("updated timeout is lower than min timeout, %v<%v", updated, s.timeoutMinTimeout)
updated = s.timeoutMinTimeout
}
if s.timeout != updated {
s.logger.Infof("updated timeout %v->%v", s.timeout, updated)
s.timeout = updated
}
s.timeoutLastUpdate = time.Now()
}
func (s *scanner) launchHostScans() chan scanReq {
reqChan := make(chan scanReq, s.scanBatchSize)
s.ap.wg.Add(1)
go func() {
defer s.ap.wg.Done()
defer close(reqChan)
var offset int
var exhausted bool
cutoff := time.Now().Add(-s.scanMinInterval)
for !s.ap.isStopped() && !exhausted {
// fetch next batch
hosts, err := s.bus.HostsForScanning(s.ap.shutdownCtx, api.HostsForScanningOptions{
MaxLastScan: api.TimeRFC3339(cutoff),
Offset: offset,
Limit: int(s.scanBatchSize),
})
if err != nil {
s.logger.Errorf("could not get hosts for scanning, err: %v", err)
break
}
if len(hosts) == 0 {
break
}
if len(hosts) < int(s.scanBatchSize) {
exhausted = true
}
s.logger.Infof("scanning %d hosts in range %d-%d", len(hosts), offset, offset+int(s.scanBatchSize))
offset += int(s.scanBatchSize)
// add batch to scan queue
for _, h := range hosts {
select {
case <-s.ap.shutdownCtx.Done():
return
case reqChan <- scanReq{
hostKey: h.PublicKey,
hostIP: h.NetAddress,
}:
}
}
}
}()
return reqChan
}
func (s *scanner) launchScanWorkers(ctx context.Context, w scanWorker, reqs chan scanReq) chan scanResp {
respChan := make(chan scanResp, s.scanThreads)
liveThreads := s.scanThreads
for i := uint64(0); i < s.scanThreads; i++ {
go func() {
for req := range reqs {
if s.ap.isStopped() {
break // shutdown
}
scan, err := w.RHPScan(ctx, req.hostKey, req.hostIP, s.currentTimeout())
if err != nil {
break // abort
} else if !utils.IsErr(errors.New(scan.ScanError), contractor.ErrIOTimeout) && scan.Ping > 0 {
s.tracker.addDataPoint(time.Duration(scan.Ping))
}
respChan <- scanResp{req.hostKey, scan.Settings, err}
}
if atomic.AddUint64(&liveThreads, ^uint64(0)) == 0 {
close(respChan)
}
}()
}
return respChan
}
func (s *scanner) isScanRequired() bool {
return s.scanningLastStart.IsZero() || time.Since(s.scanningLastStart) > s.scanMinInterval/20 // check 20 times per minInterval, so every 30 minutes
}
func (s *scanner) isTimeoutUpdateRequired() bool {
return s.timeoutLastUpdate.IsZero() || time.Since(s.timeoutLastUpdate) > s.timeoutMinInterval
}
func (s *scanner) currentTimeout() time.Duration {
s.mu.Lock()
defer s.mu.Unlock()
return s.timeout
}