forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
registrar.go
253 lines (233 loc) · 7.42 KB
/
registrar.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
// 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"
"sync"
"time"
"github.com/scionproto/scion/go/beacon_srv/internal/beacon"
"github.com/scionproto/scion/go/beacon_srv/internal/metrics"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/infra/messenger"
"github.com/scionproto/scion/go/lib/infra/modules/itopo"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/periodic"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/snet/addrutil"
"github.com/scionproto/scion/go/proto"
)
// SegmentProvider provides segments to register for the specified type.
type SegmentProvider interface {
SegmentsToRegister(ctx context.Context, segType proto.PathSegType) (
<-chan beacon.BeaconOrErr, error)
}
var _ periodic.Task = (*Registrar)(nil)
// RegistrarConf is the configuration to create a new registrar.
type RegistrarConf struct {
Config ExtenderConf
SegProvider SegmentProvider
TopoProvider itopo.ProviderI
Msgr infra.Messenger
Period time.Duration
SegType proto.PathSegType
}
// Registrar is used to periodically register path segments with the appropriate
// path servers. Core and Up segments are registered with the local path server.
// Down segments are registered at the core.
type Registrar struct {
*segExtender
msgr infra.Messenger
segProvider SegmentProvider
topoProvider itopo.ProviderI
segType proto.PathSegType
// mutable fields
lastSucc time.Time
tick tick
}
// New creates a new segment regsitration task.
func (cfg RegistrarConf) New() (*Registrar, error) {
cfg.Config.task = "registrar"
extender, err := cfg.Config.new()
if err != nil {
return nil, err
}
r := &Registrar{
segProvider: cfg.SegProvider,
topoProvider: cfg.TopoProvider,
segType: cfg.SegType,
msgr: cfg.Msgr,
tick: tick{period: cfg.Period},
segExtender: extender,
}
return r, nil
}
// Name returns the tasks name.
func (r *Registrar) Name() string {
return "bs_beaconing_registrar"
}
// Run registers path segments for the specified type to path servers.
func (r *Registrar) Run(ctx context.Context) {
r.tick.now = time.Now()
if err := r.run(ctx); err != nil {
log.FromCtx(ctx).Error("[beaconing.Registrar] Unable to register",
"type", r.segType, "err", err)
}
metrics.Registrar.RuntimeWithType(r.segType.String()).Add(time.Since(r.tick.now).Seconds())
r.tick.updateLast()
}
func (r *Registrar) run(ctx context.Context) error {
if r.tick.now.Sub(r.lastSucc) < r.tick.period && !r.tick.passed() {
return nil
}
logger := log.FromCtx(ctx)
segments, err := r.segProvider.SegmentsToRegister(ctx, r.segType)
if err != nil {
return err
}
peers, nonActivePeers := sortedIntfs(r.cfg.Intfs, proto.LinkType_peer)
if len(nonActivePeers) > 0 {
logger.Debug("[beaconing.Registrar] Ignore non-active peer interfaces", "type", r.segType,
"intfs", nonActivePeers)
}
s := newSummary()
var expected int
var wg sync.WaitGroup
for bOrErr := range segments {
if bOrErr.Err != nil {
logger.Error("[beaconing.Registrar] Unable to get beacon", "err", err)
metrics.Registrar.InternalErrorsWithType(r.segType.String()).Inc()
continue
}
if !r.IntfActive(bOrErr.Beacon.InIfId) {
continue
}
expected++
s := segmentRegistrar{
Registrar: r,
beacon: bOrErr.Beacon,
peers: peers,
summary: s,
logger: logger,
}
// Avoid head-of-line blocking when sending message to slow servers.
s.start(ctx, &wg)
}
wg.Wait()
if expected == 0 {
return nil
}
if s.count <= 0 {
return common.NewBasicError("No beacons propagated", nil, "candidates", expected)
}
r.lastSucc = r.tick.now
r.logSummary(logger, s)
return nil
}
func (r *Registrar) logSummary(logger log.Logger, s *summary) {
if r.tick.passed() {
logger.Info("[beaconing.Registrar] Registered beacons", "type", r.segType, "count", s.count,
"startIAs", len(s.srcs))
return
}
if s.count > 0 {
logger.Info("[beaconing.Registrar] Registered beacons after stale period",
"type", r.segType, "count", s.count, "startIAs", len(s.srcs))
}
}
// segmentRegistrar registers one segment with the path server.
type segmentRegistrar struct {
*Registrar
beacon beacon.Beacon
peers []common.IFIDType
summary *summary
logger log.Logger
// mutable
reg *path_mgmt.SegReg
addr net.Addr
}
// start extends the beacon and starts a go routine that registers the beacon
// with the path server.
func (r *segmentRegistrar) start(ctx context.Context, wg *sync.WaitGroup) {
if err := r.setSegToRegister(); err != nil {
r.logger.Error("[beaconing.Registrar] Unable to create segment",
"type", r.segType, "err", err)
return
}
r.startSendSegReg(ctx, wg)
}
// setSegToRegister sets the segment to register and the address to send to.
func (r *segmentRegistrar) setSegToRegister() error {
if err := r.extend(r.beacon.Segment, r.beacon.InIfId, 0, r.peers); err != nil {
metrics.Registrar.InternalErrorsWithType(r.segType.String()).Inc()
return common.NewBasicError("Unable to terminate", err, "beacon", r.beacon)
}
r.reg = &path_mgmt.SegReg{
SegRecs: &path_mgmt.SegRecs{
Recs: []*seg.Meta{
{
Type: r.segType,
Segment: r.beacon.Segment,
},
},
},
}
var err error
r.addr, err = r.chooseServer(r.beacon.Segment)
if err != nil {
metrics.Registrar.InternalErrorsWithType(r.segType.String()).Inc()
return common.NewBasicError("Unable to choose server", err)
}
return nil
}
// startSendSegReg adds to the wait group and starts a goroutine that sends the
// registration message to the peer.
func (r *segmentRegistrar) startSendSegReg(ctx context.Context, wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer log.LogPanicAndExit()
defer wg.Done()
if err := r.msgr.SendSegReg(ctx, r.reg, r.addr, messenger.NextId()); err != nil {
r.logger.Error("[beaconing.Registrar] Unable to register segment",
"addr", r.addr, "err", err)
metrics.Registrar.InternalErrorsWithType(r.segType.String()).Inc()
return
}
r.onSuccess()
r.logger.Trace("[beaconing.Registrar] Successfully registered segment", "type", r.segType,
"addr", r.addr, "seg", r.beacon.Segment)
}()
}
func (r *segmentRegistrar) onSuccess() {
r.summary.AddSrc(r.beacon.Segment.FirstIA())
r.summary.Inc()
l := metrics.RegistrarLabels{
SegType: r.segType.String(),
StartIA: r.beacon.Segment.FirstIA(),
InIfID: r.beacon.InIfId,
Result: metrics.Success,
}
metrics.Registrar.Beacons(l).Inc()
}
func (r *segmentRegistrar) chooseServer(pseg *seg.PathSegment) (net.Addr, error) {
if r.segType != proto.PathSegType_down {
topo := r.topoProvider.Get()
return &snet.Addr{IA: topo.IA(), Host: addr.NewSVCUDPAppAddr(addr.SvcPS)}, nil
}
return addrutil.GetPath(addr.SvcPS, pseg, r.topoProvider)
}