-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathstats.go
284 lines (243 loc) · 7.52 KB
/
stats.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
// Copyright 2021 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pan
import (
"sync"
"time"
)
var stats pathStatsDB
func init() {
stats = newPathStatsDB()
}
type PathStats struct {
// Was notified down at the recorded time (0 for never notified down)
IsNotifiedDown time.Time
}
type PathInterfaceStats struct {
// Was notified down at the recorded time (0 for never notified down)
IsNotifiedDown time.Time
}
type DestinationStats struct {
Latency map[PathFingerprint]StatsLatencySamples
}
type StatsLatencySamples []StatsLatencySample
type StatsLatencySample struct {
Time time.Time
Value time.Duration
}
type pathDownNotifyee interface {
PathDown(PathFingerprint, PathInterface)
}
type pathStatsDB struct {
mutex sync.RWMutex
// TODO: this needs a fixed/max capacity and least-recently-used spill over
// Possibly use separate, explicitly controlled table for paths in dialed connections.
paths map[PathFingerprint]PathStats
// TODO: this should rather be "link" or "hop" stats, i.e. identified by two
// consecutive (unordered?) interface IDs.
interfaces map[PathInterface]PathInterfaceStats
// TODO: cleanup of this map on connection end, reference counted handle?
destinations map[scionAddr]DestinationStats
notifier pathDownNotifier
}
func newPathStatsDB() pathStatsDB {
return pathStatsDB{
paths: make(map[PathFingerprint]PathStats),
interfaces: make(map[PathInterface]PathInterfaceStats),
destinations: make(map[scionAddr]DestinationStats),
}
}
func (s *pathStatsDB) RecordLatency(dst scionAddr, p PathFingerprint, latency time.Duration) {
s.mutex.Lock()
defer s.mutex.Unlock()
dstStats := s.destinations[dst]
if dstStats.Latency == nil {
dstStats.Latency = make(map[PathFingerprint]StatsLatencySamples)
}
dstStats.Latency[p] = dstStats.Latency[p].insert(latency)
s.destinations[dst] = dstStats
}
// LowestLatency returns the index of the path with lowest recorded latency.
// In case of ties, lower index paths are preferred.
// Path liveness is taken into account; latency records not younger than a
// recorded down notification for the corresponding path are ignored. Paths
// with no recorded latency value, or with down notifications, are treated with
// least priority.
func (s *pathStatsDB) LowestLatency(dst scionAddr, paths []*Path) int {
if len(paths) == 0 {
return -1
}
s.mutex.RLock()
defer s.mutex.RUnlock()
dstStats := s.destinations[dst]
best := -1
bestDown := maxTime
bestLatency := maxDuration
for i, p := range paths {
down := s.newestDownNotification(p)
latencyStats := dstStats.Latency[p.Fingerprint]
latency := maxDuration
if len(latencyStats) > 0 && latencyStats[0].Time.After(down) {
latency = latencyStats[0].Value
}
if latency < bestLatency {
best = i
bestDown = down
bestLatency = latency
} else if latency == bestLatency && down.Before(bestDown) {
best = i
bestDown = down
}
}
return best
}
// FirstMoreAlive returns the index of the first path in paths that is strictly "more
// alive" than p, or -1 if there is none.
// A path is considered to be more alive if it does not contain any of p's interfaces that
// are considered down
func (s *pathStatsDB) FirstMoreAlive(p *Path, paths []*Path) int {
s.mutex.RLock()
defer s.mutex.RUnlock()
for i, pc := range paths {
if s.IsMoreAlive(pc, p) {
return i
}
}
return -1
}
// IsMoreAlive checks if a is strictly "less down" / "more alive" than b.
// Returns true if a does not have any recent down notifications and b does, or
// (more generally) if all down notifications for a are strictly older
// than any down notification for b.
func (s *pathStatsDB) IsMoreAlive(a, b *Path) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
newestA := s.newestDownNotification(a)
oldestB := s.oldestDownNotification(b)
return newestA.Before(oldestB.Add(-pathDownNotificationTimeout)) // XXX: what is this value, what does it mean?
}
// newestDownNotification returns the time of the newest relevant down
// notification for path p.
func (s *pathStatsDB) newestDownNotification(p *Path) time.Time {
newest := s.paths[p.Fingerprint].IsNotifiedDown
if p.Metadata != nil {
for _, pi := range p.Metadata.Interfaces {
if v, ok := s.interfaces[pi]; ok {
if v.IsNotifiedDown.After(newest) {
newest = v.IsNotifiedDown
}
}
}
}
return newest
}
// oldestDownNotification returns the time of the oldest relevant down
// notification for path p. Returns 0 if no relevant down notifications were
// recorded.
func (s *pathStatsDB) oldestDownNotification(p *Path) time.Time {
t0 := time.Time{}
oldest := s.paths[p.Fingerprint].IsNotifiedDown
if p.Metadata != nil {
for _, pi := range p.Metadata.Interfaces {
if v, ok := s.interfaces[pi]; ok && v.IsNotifiedDown != t0 {
if oldest == t0 || v.IsNotifiedDown.Before(oldest) {
oldest = v.IsNotifiedDown
}
}
}
}
return oldest
}
func (s *pathStatsDB) subscribe(subscriber pathDownNotifyee) {
s.notifier.subscribe(subscriber)
}
func (s *pathStatsDB) unsubscribe(subscriber pathDownNotifyee) {
s.notifier.unsubscribe(subscriber)
}
func (s *pathStatsDB) NotifyPathDown(pf PathFingerprint, pi PathInterface) {
s.recordPathDown(pf, pi)
s.notifier.notifyAsync(pf, pi)
}
func (s *pathStatsDB) recordPathDown(pf PathFingerprint, pi PathInterface) {
s.mutex.Lock()
defer s.mutex.Unlock()
now := time.Now()
ps := s.paths[pf]
ps.IsNotifiedDown = now
s.paths[pf] = ps
pis := s.interfaces[pi]
pis.IsNotifiedDown = now
s.interfaces[pi] = pis
}
func (s StatsLatencySamples) insert(latency time.Duration) StatsLatencySamples {
if len(s) < statsNumLatencySamples {
s = append(s, StatsLatencySample{})
}
n := len(s)
copy(s[1:n], s[0:n-1])
s[0] = StatsLatencySample{
Time: time.Now(),
Value: latency,
}
return s
}
type pathDownNotifier struct {
mutex sync.Mutex
subscribers []pathDownNotifyee
runOnce sync.Once
notifications chan<- pathDownNotification
}
func (n *pathDownNotifier) subscribe(subscriber pathDownNotifyee) {
n.mutex.Lock()
defer n.mutex.Unlock()
n.subscribers = append(n.subscribers, subscriber)
}
func (n *pathDownNotifier) unsubscribe(subscriber pathDownNotifyee) {
n.mutex.Lock()
defer n.mutex.Unlock()
idx := -1
for i, v := range n.subscribers {
if subscriber == v {
idx = i
break
}
}
if idx >= 0 {
n.subscribers = append(n.subscribers[:idx], n.subscribers[idx+1:]...)
}
}
func (n *pathDownNotifier) run() {
notifications := make(chan pathDownNotification, pathDownNotificationChannelCapacity)
n.notifications = notifications
go func() {
for notification := range notifications {
n.notify(notification.Fingerprint, notification.Interface)
}
}()
}
func (n *pathDownNotifier) notifyAsync(pf PathFingerprint, pi PathInterface) {
n.runOnce.Do(n.run)
n.notifications <- pathDownNotification{Fingerprint: pf, Interface: pi}
}
func (n *pathDownNotifier) notify(pf PathFingerprint, pi PathInterface) {
n.mutex.Lock()
defer n.mutex.Unlock()
for _, s := range n.subscribers {
s.PathDown(pf, pi)
}
}
type pathDownNotification struct {
Fingerprint PathFingerprint
Interface PathInterface
}