-
Notifications
You must be signed in to change notification settings - Fork 15
/
statsmanager.go
671 lines (563 loc) · 20.3 KB
/
statsmanager.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
package enginenetx
//
// Code to keep statistics about the TLS dialing
// tactics that work and the ones that don't
//
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"sort"
"sync"
"time"
"github.com/ooni/probe-engine/pkg/model"
"github.com/ooni/probe-engine/pkg/runtimex"
)
// nullStatsManager is the "null" [httpsDialerEventsHandler].
type nullStatsManager struct{}
var _ httpsDialerEventsHandler = &nullStatsManager{}
// OnStarting implements httpsDialerEventsHandler.
func (*nullStatsManager) OnStarting(tactic *httpsDialerTactic) {
// nothing
}
// OnSuccess implements httpsDialerEventsHandler.
func (*nullStatsManager) OnSuccess(tactic *httpsDialerTactic) {
// nothing
}
// OnTCPConnectError implements httpsDialerEventsHandler.
func (*nullStatsManager) OnTCPConnectError(ctx context.Context, tactic *httpsDialerTactic, err error) {
// nothing
}
// OnTLSHandshakeError implements httpsDialerEventsHandler.
func (*nullStatsManager) OnTLSHandshakeError(ctx context.Context, tactic *httpsDialerTactic, err error) {
// nothing
}
// OnTLSVerifyError implements httpsDialerEventsHandler.
func (*nullStatsManager) OnTLSVerifyError(tactic *httpsDialerTactic, err error) {
// nothing
}
// statsTactic keeps stats about an [*httpsDialerTactic].
type statsTactic struct {
// CountStarted counts the number of operations we started.
CountStarted int64
// CountTCPConnectError counts the number of TCP connect errors.
CountTCPConnectError int64
// CountTCPConnectInterrupt counts the number of interrupted TCP connect attempts.
CountTCPConnectInterrupt int64
// CountTLSHandshakeError counts the number of TLS handshake errors.
CountTLSHandshakeError int64
// CountTLSHandshakeInterrupt counts the number of interrupted TLS handshakes.
CountTLSHandshakeInterrupt int64
// CountTLSVerificationError counts the number of TLS verification errors.
CountTLSVerificationError int64
// CountSuccess counts the number of successes.
CountSuccess int64
// HistoTCPConnectError contains an histogram of TCP connect errors.
HistoTCPConnectError map[string]int64
// HistoTLSHandshakeError contains an histogram of TLS handshake errors.
HistoTLSHandshakeError map[string]int64
// HistoTLSVerificationError contains an histogram of TLS verification errors.
HistoTLSVerificationError map[string]int64
// LastUpdated is the last time we updated this record.
LastUpdated time.Time
// Tactic is the underlying tactic.
Tactic *httpsDialerTactic
}
// statsNilSafeSuccessRate is a convenience function for computing the success rate
// which returns zero as the success rate if CountStarted is zero.
//
// For robustness, be paranoid about nils here because the stats are
// written on the disk and a user could potentially edit them.
func statsNilSafeSuccessRate(t *statsTactic) (rate float64) {
if t != nil && t.CountStarted > 0 {
rate = float64(t.CountSuccess) / float64(t.CountStarted)
}
return
}
// statsNilSafeLastUpdated is a convenience function for getting the .LastUpdated
// field that takes into account the case where t is nil.
func statsNilSafeLastUpdated(t *statsTactic) (output time.Time) {
if t != nil {
output = t.LastUpdated
}
return
}
// statsNilSafeCountSuccess is a convenience function for getting the .CountSuccess
// counter that takes into account the case where t is nil.
func statsNilSafeCountSuccess(t *statsTactic) (output int64) {
if t != nil {
output = t.CountSuccess
}
return
}
// statsDefensivelySortTacticsByDescendingSuccessRateWithAcceptPredicate sorts the input list
// by success rate taking into account that several entries could be malformed, and then
// filters the sorted list using the given boolean predicate to accept elements.
//
// The sorting criteria takes into account:
//
// 1. the success rate; or
//
// 2. the last updated time; or
//
// 3. the number of successes.
//
// The predicate allows to further restrict the returned list.
//
// This function operates on a deep copy of the input list, so it does not create data races.
func statsDefensivelySortTacticsByDescendingSuccessRateWithAcceptPredicate(
input []*statsTactic, acceptfunc func(*statsTactic) bool) []*statsTactic {
// first let's create a working list such that we don't modify
// the input in place thus avoiding any data race
work := []*statsTactic{}
for _, t := range input {
if t != nil && t.Tactic != nil {
work = append(work, t.Clone()) // DEEP COPY!
}
}
// now let's sort work in place
sort.SliceStable(work, func(i, j int) bool {
if statsNilSafeSuccessRate(work[i]) > statsNilSafeSuccessRate(work[j]) {
return true
}
if statsNilSafeCountSuccess(work[i]) > statsNilSafeCountSuccess(work[j]) {
return true
}
if statsNilSafeLastUpdated(work[i]).Sub(statsNilSafeLastUpdated(work[j])) > 0 {
return true
}
return false
})
// finally let's apply the predicate to produce output
output := []*statsTactic{}
for _, t := range work {
if acceptfunc(t) {
output = append(output, t)
}
}
return output
}
func statsMaybeCloneMapStringInt64(input map[string]int64) (output map[string]int64) {
// distinguish and preserve nil versus empty
if input == nil {
return
}
output = make(map[string]int64)
for key, value := range input {
output[key] = value
}
return
}
func statsMaybeCloneTactic(input *httpsDialerTactic) (output *httpsDialerTactic) {
if input != nil {
output = input.Clone()
}
return
}
// Clone clones a given [*statsTactic]
func (st *statsTactic) Clone() *statsTactic {
// Implementation note: a time.Time consists of an uint16, an int64 and
// a pointer to a location which is typically immutable, so it's perfectly
// fine to copy the LastUpdate field by assignment.
//
// here we're using a bunch of robustness aware mechanisms to clone
// considering that the struct may be edited by the user
return &statsTactic{
CountStarted: st.CountStarted,
CountTCPConnectError: st.CountTCPConnectError,
CountTCPConnectInterrupt: st.CountTCPConnectInterrupt,
CountTLSHandshakeError: st.CountTLSHandshakeError,
CountTLSHandshakeInterrupt: st.CountTLSHandshakeInterrupt,
CountTLSVerificationError: st.CountTLSVerificationError,
CountSuccess: st.CountSuccess,
HistoTCPConnectError: statsMaybeCloneMapStringInt64(st.HistoTCPConnectError),
HistoTLSHandshakeError: statsMaybeCloneMapStringInt64(st.HistoTLSHandshakeError),
HistoTLSVerificationError: statsMaybeCloneMapStringInt64(st.HistoTLSVerificationError),
LastUpdated: st.LastUpdated,
Tactic: statsMaybeCloneTactic(st.Tactic),
}
}
// statsDomainEndpoint contains stats associated with a domain endpoint.
type statsDomainEndpoint struct {
Tactics map[string]*statsTactic
}
// statsDomainEndpointPruneEntries returns a DEEP COPY of a [*statsDomainEndpoint] with old
// and excess entries removed, such that the overall size is not unbounded.
func statsDomainEndpointPruneEntries(input *statsDomainEndpoint) *statsDomainEndpoint {
tactics := []*statsTactic{}
now := time.Now()
// if .Tactics is empty here we're just going to do nothing
for summary, tactic := range input.Tactics {
// we serialize stats to disk, so we cannot rule out the case where the user
// explicitly edits the stats to include a malformed entry
if summary == "" || tactic == nil || tactic.Tactic == nil {
continue
}
tactics = append(tactics, tactic)
}
// oneWeek is a constant representing one week of data.
const oneWeek = 7 * 24 * time.Hour
// maxEntriesPerDomainEndpoint is the maximum number of entries per
// domain endpoint that we would like to keep overall.
const maxEntriesPerDomainEndpoint = 10
// Sort by descending success rate and cut all the entries that are older than
// a given threshold. Note that we need to be defensive here because we are dealing
// with data stored on disk that might have been modified to crash us.
//
// Note that statsDefensivelySortTacticsByDescendingSuccessRateWithAcceptPredicate
// operates on and returns a DEEP COPY of the original list.
tactics = statsDefensivelySortTacticsByDescendingSuccessRateWithAcceptPredicate(
tactics, func(st *statsTactic) bool {
// When .LastUpdated is the zero time.Time value, the check is going to fail
// exactly like the time was 1 or 5 or 10 years ago instead.
//
// See https://go.dev/play/p/HGQT17ueIkq where we show that the zero time
// is handled exactly like any time in the past (it was kinda obvious, but
// sometimes it also make sense to double check assumptions!)
delta := now.Sub(statsNilSafeLastUpdated(st))
return delta < oneWeek
})
// Cut excess entries, if needed
if len(tactics) > maxEntriesPerDomainEndpoint {
tactics = tactics[:maxEntriesPerDomainEndpoint]
}
// return a new statsDomainEndpoint to the caller
output := &statsDomainEndpoint{
Tactics: map[string]*statsTactic{},
}
for _, t := range tactics {
output.Tactics[t.Tactic.tacticSummaryKey()] = t
}
return output
}
// statsContainerVersion is the current version of [statsContainer].
const statsContainerVersion = 5
// statsContainer is the root container for the stats.
//
// The zero value is invalid; construct using [newStatsContainer].
type statsContainer struct {
// DomainEndpoints maps a domain endpoint to its tactics.
DomainEndpoints map[string]*statsDomainEndpoint
// Version is the version of the container data format.
Version int
}
// statsContainerPruneEntries returns a DEEP COPY of a [*statsContainer] with old entries removed.
func statsContainerPruneEntries(input *statsContainer) (output *statsContainer) {
output = newStatsContainer()
// if .DomainEndpoints is nil here we're just going to do nothing
for domainEpnt, inputStats := range input.DomainEndpoints {
// We serialize this data to disk, so we need to account for the case
// where a user has manually edited the JSON to add a nil value
if domainEpnt == "" || inputStats == nil || len(inputStats.Tactics) <= 0 {
continue
}
prunedStats := statsDomainEndpointPruneEntries(inputStats)
// We don't want to include an entry when it's empty because all the
// stats inside it have just been pruned
if len(prunedStats.Tactics) <= 0 {
continue
}
output.DomainEndpoints[domainEpnt] = prunedStats
}
return
}
// GetStatsTacticLocked returns the tactic record for the given [*statsTactic] instance.
//
// As the name implies, this function MUST be called while holding the [*statsManager] mutex.
func (c *statsContainer) GetStatsTacticLocked(tactic *httpsDialerTactic) (*statsTactic, bool) {
domainEpntRecord, found := c.DomainEndpoints[tactic.domainEndpointKey()]
if !found || domainEpntRecord == nil {
return nil, false
}
tacticRecord, found := domainEpntRecord.Tactics[tactic.tacticSummaryKey()]
return tacticRecord, found
}
// SetStatsTacticLocked sets the tactic record for the given the given [*statsTactic] instance.
//
// As the name implies, this function MUST be called while holding the [*statsManager] mutex.
func (c *statsContainer) SetStatsTacticLocked(tactic *httpsDialerTactic, record *statsTactic) {
domainEpntRecord, found := c.DomainEndpoints[tactic.domainEndpointKey()]
if !found {
domainEpntRecord = &statsDomainEndpoint{
Tactics: map[string]*statsTactic{},
}
// make sure the map is initialized -- not a void concern given that we're
// reading this structure from the disk
if len(c.DomainEndpoints) <= 0 {
c.DomainEndpoints = make(map[string]*statsDomainEndpoint)
}
c.DomainEndpoints[tactic.domainEndpointKey()] = domainEpntRecord
// fallthrough
}
domainEpntRecord.Tactics[tactic.tacticSummaryKey()] = record
}
// newStatsContainer creates a new empty [*statsContainer].
func newStatsContainer() *statsContainer {
return &statsContainer{
DomainEndpoints: map[string]*statsDomainEndpoint{},
Version: statsContainerVersion,
}
}
// statsManager implements [httpsDialerEventsHandler] by storing the
// relevant statistics in a [model.KeyValueStore].
//
// The zero value of this structure is not ready to use; please, use the
// [newStatsManager] factory to create a new instance.
type statsManager struct {
// cancel allows canceling the background stats pruner.
cancel context.CancelFunc
// closeOnce gives .Close a "once" semantics
closeOnce sync.Once
// container is the container container for stats
container *statsContainer
// kvStore is the key-value store we're using
kvStore model.KeyValueStore
// logger is the logger to use.
logger model.Logger
// mu provides mutual exclusion when accessing the stats.
mu sync.Mutex
// pruned is a channel pruned on a best effort basis
// by the background goroutine that prunes.
pruned chan any
// wg tells us when the background goroutine joined.
wg *sync.WaitGroup
}
// statsKey is the key used in the key-value store to access the state.
const statsKey = "httpsdialerstats.state"
// errStatsContainerWrongVersion means that the stats container document has the wrong version number.
var errStatsContainerWrongVersion = errors.New("wrong stats container version")
// loadStatsContainer loads a stats container from the given [model.KeyValueStore].
func loadStatsContainer(kvStore model.KeyValueStore) (*statsContainer, error) {
// load data from the kvstore
data, err := kvStore.Get(statsKey)
if err != nil {
return nil, err
}
// parse as JSON
var container statsContainer
if err := json.Unmarshal(data, &container); err != nil {
return nil, err
}
// make sure the version is OK
if container.Version != statsContainerVersion {
err := fmt.Errorf(
"%s: %w: expected=%d got=%d",
statsKey,
errStatsContainerWrongVersion,
statsContainerVersion,
container.Version,
)
return nil, err
}
// make sure we prune the data structure
pruned := statsContainerPruneEntries(&container)
return pruned, nil
}
// newStatsManager constructs a new instance of [*statsManager].
func newStatsManager(kvStore model.KeyValueStore, logger model.Logger, trimInterval time.Duration) *statsManager {
runtimex.Assert(trimInterval > 0, "passed non-positive trimInterval")
root, err := loadStatsContainer(kvStore)
if err != nil {
root = newStatsContainer()
}
ctx, cancel := context.WithCancel(context.Background())
mt := &statsManager{
cancel: cancel,
closeOnce: sync.Once{},
container: root,
kvStore: kvStore,
logger: logger,
mu: sync.Mutex{},
pruned: make(chan any),
wg: &sync.WaitGroup{},
}
// run a background goroutine that trims the stats by removing excessive
// entries until the programmer calls (*statsManager).Close
mt.wg.Add(1)
go func() {
defer mt.wg.Done()
mt.trim(ctx, trimInterval)
}()
return mt
}
var _ httpsDialerEventsHandler = &statsManager{}
// OnStarting implements httpsDialerEventsHandler.
func (mt *statsManager) OnStarting(tactic *httpsDialerTactic) {
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// get the record
record, found := mt.container.GetStatsTacticLocked(tactic)
if !found {
record = &statsTactic{
CountStarted: 0,
CountTCPConnectError: 0,
CountTCPConnectInterrupt: 0,
CountTLSHandshakeError: 0,
CountTLSHandshakeInterrupt: 0,
CountTLSVerificationError: 0,
CountSuccess: 0,
HistoTCPConnectError: map[string]int64{},
HistoTLSHandshakeError: map[string]int64{},
HistoTLSVerificationError: map[string]int64{},
LastUpdated: time.Time{},
Tactic: tactic.Clone(), // avoid storing the original
}
mt.container.SetStatsTacticLocked(tactic, record)
}
// update stats
record.CountStarted++
record.LastUpdated = time.Now()
}
func statsSafeIncrementMapStringInt64(input *map[string]int64, value string) {
runtimex.Assert(input != nil, "passed nil pointer to a map")
if *input == nil {
*input = make(map[string]int64)
}
(*input)[value]++
}
// OnTCPConnectError implements httpsDialerEventsHandler.
func (mt *statsManager) OnTCPConnectError(ctx context.Context, tactic *httpsDialerTactic, err error) {
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// get the record
record, found := mt.container.GetStatsTacticLocked(tactic)
if !found {
mt.logger.Warnf("statsManager.OnTCPConnectError: not found: %+v", tactic)
return
}
// update stats
record.LastUpdated = time.Now()
if ctx.Err() != nil {
record.CountTCPConnectInterrupt++
return
}
runtimex.Assert(err != nil, "OnTCPConnectError passed a nil error")
record.CountTCPConnectError++
statsSafeIncrementMapStringInt64(&record.HistoTCPConnectError, err.Error())
}
// OnTLSHandshakeError implements httpsDialerEventsHandler.
func (mt *statsManager) OnTLSHandshakeError(ctx context.Context, tactic *httpsDialerTactic, err error) {
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// get the record
record, found := mt.container.GetStatsTacticLocked(tactic)
if !found {
mt.logger.Warnf("statsManager.OnTLSHandshakeError: not found: %+v", tactic)
return
}
// update stats
record.LastUpdated = time.Now()
if ctx.Err() != nil {
record.CountTLSHandshakeInterrupt++
return
}
runtimex.Assert(err != nil, "OnTLSHandshakeError passed a nil error")
record.CountTLSHandshakeError++
statsSafeIncrementMapStringInt64(&record.HistoTLSHandshakeError, err.Error())
}
// OnTLSVerifyError implements httpsDialerEventsHandler.
func (mt *statsManager) OnTLSVerifyError(tactic *httpsDialerTactic, err error) {
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// get the record
record, found := mt.container.GetStatsTacticLocked(tactic)
if !found {
mt.logger.Warnf("statsManager.OnTLSVerificationError: not found: %+v", tactic)
return
}
// update stats
runtimex.Assert(err != nil, "OnTLSVerifyError passed a nil error")
record.CountTLSVerificationError++
statsSafeIncrementMapStringInt64(&record.HistoTLSVerificationError, err.Error())
record.LastUpdated = time.Now()
}
// OnSuccess implements httpsDialerEventsHandler.
func (mt *statsManager) OnSuccess(tactic *httpsDialerTactic) {
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// get the record
record, found := mt.container.GetStatsTacticLocked(tactic)
if !found {
mt.logger.Warnf("statsManager.OnSuccess: not found: %+v", tactic)
return
}
// update stats
record.CountSuccess++
record.LastUpdated = time.Now()
}
// Close implements io.Closer
func (mt *statsManager) Close() (err error) {
mt.closeOnce.Do(func() {
// interrupt the background goroutine
mt.cancel()
func() {
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// make sure we remove the unneeded entries one last time before saving them
container := statsContainerPruneEntries(mt.container)
// write updated stats into the underlying key-value store
err = mt.kvStore.Set(statsKey, runtimex.Try1(json.Marshal(container)))
}()
// wait for background goroutine to join
mt.wg.Wait()
})
return
}
// trim runs in the background and trims the mt.container struct
func (mt *statsManager) trim(ctx context.Context, interval time.Duration) {
// Note: we already manage mt.wg when we start this goroutine so there's NO NEED to do it here!
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
// get exclusive access and edit the container
mt.mu.Lock()
mt.container = statsContainerPruneEntries(mt.container)
mt.mu.Unlock()
// notify whoever's concerned that we pruned
// and do that best effort because it may be that nobody is concerned
select {
case mt.pruned <- true:
default:
}
}
}
}
// LookupTacticsStats returns stats about tactics for a given domain and port. The returned
// list is a clone of the one stored by [*statsManager] so, it can easily be modified.
func (mt *statsManager) LookupTactics(domain string, port string) ([]*statsTactic, bool) {
out := []*statsTactic{}
// get exclusive access
defer mt.mu.Unlock()
mt.mu.Lock()
// check whether we have information on this endpoint
//
// Note: in case mt.container.DomainEndpoints is nil, this access pattern
// will return to us a nil pointer and false
//
// we also protect against the case where a user has configured a nil
// domainEpnts value inside the serialized JSON to crash us
domainEpnts, good := mt.container.DomainEndpoints[net.JoinHostPort(domain, port)]
if !good || domainEpnts == nil {
return out, false
}
// return a copy of each entry
//
// Note: if Tactics here is nil, we're just not going to have
// anything to include into the out list
for _, entry := range domainEpnts.Tactics {
out = append(out, entry.Clone())
}
return out, len(out) > 0
}