-
Notifications
You must be signed in to change notification settings - Fork 153
/
writer.go
378 lines (343 loc) · 11.9 KB
/
writer.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
// Copyright 2019 Anapaya Systems
//
// 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 beaconing
import (
"context"
"net"
"strconv"
"sync"
"time"
"github.com/scionproto/scion/go/cs/beacon"
"github.com/scionproto/scion/go/cs/ifstate"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/infra/modules/seghandler"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/metrics"
"github.com/scionproto/scion/go/lib/periodic"
"github.com/scionproto/scion/go/lib/prom"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/topology"
)
// Pather computes the remote address with a path based on the provided segment.
type Pather interface {
GetPath(svc addr.HostSVC, ps *seg.PathSegment) (*snet.SVCAddr, error)
}
// SegmentProvider provides segments to register for the specified type.
type SegmentProvider interface {
// SegmentsToRegister returns the segments that should be registered for the
// given segment type. The returned channel must not be nil if the returned
// error is nil.
SegmentsToRegister(ctx context.Context, segType seg.Type) (
<-chan beacon.BeaconOrErr, error)
}
// SegmentStore stores segments in the path database.
type SegmentStore interface {
StoreSegs(context.Context, []*seghandler.SegWithHP) (seghandler.SegStats, error)
}
// RPC registers the path segment with the remote.
type RPC interface {
RegisterSegment(ctx context.Context, meta seg.Meta, remote net.Addr) error
}
// WriteStats provides statistics about segment writing.
type WriteStats struct {
// Count is the number of successfully written segments.
Count int
// StartIAs lists the AS.
StartIAs map[addr.IA]struct{}
}
// Writer writes segments.
type Writer interface {
// Write must consume the segments channel until it's closed by the caller.
// The provided channel is always non-nil. Peers indicate the peering
// interface IDs of the local IA. The returned statistics should provide
// insights about how many segments have been successfully written. The
// method should return an error if the writing did fail.
Write(ctx context.Context, segments <-chan beacon.BeaconOrErr,
peers []common.IFIDType) (WriteStats, error)
}
var _ periodic.Task = (*WriteScheduler)(nil)
// WriteScheduler is used to periodically write path segments at the configured
// writer.
type WriteScheduler struct {
// Provider is used to query for segments.
Provider SegmentProvider
// Intfs gives access to the interfaces this CS beacons over.
Intfs *ifstate.Interfaces
// Type is the type of segments that should be queried from the Provider.
Type seg.Type
// Write is used to write the segments once the scheduling determines it is
// time to write.
Writer Writer
// Tick is mutable. It's used to determine when to call write.
Tick Tick
// lastWrite indicates the time of the last successful write.
lastWrite time.Time
}
// Name returns the tasks name.
func (r *WriteScheduler) Name() string {
// XXX(lukedirtwalker): this name is used in metrics and changing it would
// be a breaking change.
return "control_beaconing_registrar"
}
// Run writes path segments using the configured writer.
func (r *WriteScheduler) Run(ctx context.Context) {
r.Tick.SetNow(time.Now())
if err := r.run(ctx); err != nil {
log.FromCtx(ctx).Error("Unable to register", "seg_type", r.Type, "err", err)
}
r.Tick.UpdateLast()
}
func (r *WriteScheduler) run(ctx context.Context) error {
if r.Tick.Now().Sub(r.lastWrite) < r.Tick.Period() && !r.Tick.Passed() {
return nil
}
segments, err := r.Provider.SegmentsToRegister(ctx, r.Type)
if err != nil {
return err
}
peers := sortedIntfs(r.Intfs, topology.Peer)
stats, err := r.Writer.Write(ctx, segments, peers)
if err != nil {
return err
}
r.lastWrite = r.Tick.Now()
r.logSummary(log.FromCtx(ctx), &summary{count: stats.Count, srcs: stats.StartIAs})
if stats.Count > 0 {
r.lastWrite = r.Tick.Now()
}
return err
}
// RemoteWriter writes segments via an RPC to the source AS of a segment.
type RemoteWriter struct {
// InternalErrors counts errors that happened before being able to send a
// segment to a remote. This can be during terminating the segment, looking
// up the remote etc. If the counter is nil errors are not counted.
InternalErrors metrics.Counter
// Registered counts the amount of registered segments. A label is used to
// indicate the status of the registration.
Registered metrics.Counter
// Intfs gives access to the interfaces this CS beacons over.
Intfs *ifstate.Interfaces
// Extender is used to terminate the beacon.
Extender Extender
// Type is the type of segment that is handled by this writer.
Type seg.Type
// RPC is used to send the segment to a remote.
RPC RPC
// Pather is used to find paths to a remote.
Pather Pather
}
// Write writes the segment at the source AS of the segment.
func (r *RemoteWriter) Write(ctx context.Context, segments <-chan beacon.BeaconOrErr,
peers []common.IFIDType) (WriteStats, error) {
logger := log.FromCtx(ctx)
s := newSummary()
var expected int
var wg sync.WaitGroup
for bOrErr := range segments {
if bOrErr.Err != nil {
logger.Error("Unable to get beacon", "err", bOrErr.Err)
metrics.CounterInc(r.InternalErrors)
continue
}
if r.Intfs.Get(bOrErr.Beacon.InIfId) == nil {
continue
}
err := r.Extender.Extend(ctx, bOrErr.Beacon.Segment, bOrErr.Beacon.InIfId, 0, peers)
if err != nil {
logger.Error("Unable to terminate beacon", "beacon", bOrErr.Beacon, "err", err)
metrics.CounterInc(r.InternalErrors)
continue
}
expected++
s := remoteWriter{
writer: r,
rpc: r.RPC,
pather: r.Pather,
summary: s,
wg: &wg,
}
// Avoid head-of-line blocking when sending message to slow servers.
s.start(ctx, bOrErr.Beacon)
}
wg.Wait()
if expected > 0 && s.count <= 0 {
return WriteStats{}, serrors.New("no beacons registered", "candidates", expected)
}
return WriteStats{Count: s.count, StartIAs: s.srcs}, nil
}
// LocalWriter can be used to write segments in the SegmentStore.
type LocalWriter struct {
// InternalErrors counts errors that happened before being able to send a
// segment to a remote. This can for example be during the termination of
// the segment. If the counter is nil errors are not counted.
InternalErrors metrics.Counter
// Registered counts the amount of registered segments. A label is used to
// indicate the status of the registration.
Registered metrics.Counter
// Type is the type of segment that is handled by this writer.
Type seg.Type
// Store is used to store the terminated segments.
Store SegmentStore
// Extender is used to terminate the beacon.
Extender Extender
// Intfs gives access to the interfaces this CS beacons over.
Intfs *ifstate.Interfaces
}
// Write terminates the segments and registers them in the SegmentStore.
func (r *LocalWriter) Write(ctx context.Context, segments <-chan beacon.BeaconOrErr,
peers []common.IFIDType) (WriteStats, error) {
logger := log.FromCtx(ctx)
beacons := make(map[string]beacon.Beacon)
var toRegister []*seghandler.SegWithHP
for bOrErr := range segments {
if bOrErr.Err != nil {
logger.Error("Unable to get beacon", "err", bOrErr.Err)
metrics.CounterInc(r.InternalErrors)
continue
}
if r.Intfs.Get(bOrErr.Beacon.InIfId) == nil {
continue
}
err := r.Extender.Extend(ctx, bOrErr.Beacon.Segment, bOrErr.Beacon.InIfId, 0, peers)
if err != nil {
logger.Error("Unable to terminate beacon", "beacon", bOrErr.Beacon, "err", err)
metrics.CounterInc(r.InternalErrors)
continue
}
toRegister = append(toRegister, &seghandler.SegWithHP{
Seg: &seg.Meta{Type: r.Type, Segment: bOrErr.Beacon.Segment},
})
beacons[bOrErr.Beacon.Segment.GetLoggingID()] = bOrErr.Beacon
}
if len(toRegister) == 0 {
return WriteStats{}, nil
}
stats, err := r.Store.StoreSegs(ctx, toRegister)
if err != nil {
metrics.CounterInc(r.InternalErrors)
return WriteStats{}, err
}
r.updateMetricsFromStat(stats, beacons)
sum := summarizeStats(stats, beacons)
return WriteStats{Count: sum.count, StartIAs: sum.srcs}, nil
}
func (r *WriteScheduler) logSummary(logger log.Logger, s *summary) {
if r.Tick.Passed() {
logger.Debug("Registered beacons", "seg_type", r.Type, "count", s.count,
"start_isd_ases", len(s.srcs))
return
}
if s.count > 0 {
logger.Debug("Registered beacons after stale period",
"seg_type", r.Type, "count", s.count, "start_isd_ases", len(s.srcs))
}
}
// updateMetricsFromStat is used to update the metrics for local DB inserts.
func (r *LocalWriter) updateMetricsFromStat(s seghandler.SegStats, b map[string]beacon.Beacon) {
for _, id := range s.InsertedSegs {
metrics.CounterInc(metrics.CounterWith(r.Registered, writerLabels{
StartIA: b[id].Segment.FirstIA(),
Ingress: b[id].InIfId,
SegType: r.Type.String(),
Result: "ok_new",
}.Expand()...))
}
for _, id := range s.UpdatedSegs {
metrics.CounterInc(metrics.CounterWith(r.Registered, writerLabels{
StartIA: b[id].Segment.FirstIA(),
Ingress: b[id].InIfId,
SegType: r.Type.String(),
Result: "ok_updated",
}.Expand()...))
}
}
// remoteWriter registers one segment with the path server.
type remoteWriter struct {
writer *RemoteWriter
rpc RPC
pather Pather
summary *summary
wg *sync.WaitGroup
}
// start extends the beacon and starts a go routine that registers the beacon
// with the path server.
func (r *remoteWriter) start(ctx context.Context, bseg beacon.Beacon) {
logger := log.FromCtx(ctx)
addr, err := r.pather.GetPath(addr.SvcCS, bseg.Segment)
if err != nil {
logger.Error("Unable to choose server", "err", err)
metrics.CounterInc(r.writer.InternalErrors)
return
}
r.startSendSegReg(ctx, bseg, seg.Meta{Type: r.writer.Type, Segment: bseg.Segment}, addr)
}
// startSendSegReg adds to the wait group and starts a goroutine that sends the
// registration message to the peer.
func (r *remoteWriter) startSendSegReg(ctx context.Context, bseg beacon.Beacon,
reg seg.Meta, addr net.Addr) {
r.wg.Add(1)
go func() {
defer log.HandlePanic()
defer r.wg.Done()
labels := writerLabels{
StartIA: bseg.Segment.FirstIA(),
Ingress: bseg.InIfId,
SegType: r.writer.Type.String(),
}
logger := log.FromCtx(ctx)
if err := r.rpc.RegisterSegment(ctx, reg, addr); err != nil {
logger.Error("Unable to register segment",
"seg_type", r.writer.Type, "addr", addr, "err", err)
metrics.CounterInc(metrics.CounterWith(r.writer.Registered,
labels.WithResult(prom.ErrNetwork).Expand()...))
return
}
r.summary.AddSrc(bseg.Segment.FirstIA())
r.summary.Inc()
metrics.CounterInc(metrics.CounterWith(r.writer.Registered,
labels.WithResult(prom.Success).Expand()...))
logger.Debug("Successfully registered segment", "seg_type", r.writer.Type,
"addr", addr, "seg", bseg.Segment)
}()
}
func summarizeStats(s seghandler.SegStats, b map[string]beacon.Beacon) *summary {
sum := newSummary()
for _, id := range append(s.InsertedSegs, s.UpdatedSegs...) {
sum.AddSrc(b[id].Segment.FirstIA())
sum.Inc()
}
return sum
}
type writerLabels struct {
StartIA addr.IA
Ingress common.IFIDType
SegType string
Result string
}
func (l writerLabels) Expand() []string {
return []string{
"start_isd_as", l.StartIA.String(),
"ingress_interface", strconv.Itoa(int(l.Ingress)),
"seg_type", l.SegType,
prom.LabelResult, l.Result,
}
}
func (l writerLabels) WithResult(result string) writerLabels {
l.Result = result
return l
}