forked from safing/spn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.go
313 lines (268 loc) · 7.47 KB
/
public.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
package captain
import (
"context"
"errors"
"fmt"
"time"
"github.com/safing/portbase/rng"
"github.com/safing/portbase/metrics"
"github.com/safing/spn/conf"
"github.com/safing/spn/docks"
"github.com/safing/spn/hub"
"github.com/safing/spn/navigator"
"github.com/safing/spn/terminal"
"github.com/safing/portbase/database"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/spn/cabin"
)
const (
maintainStatusRegularInterval = 15 * time.Minute
maintainStatusShortInterval = 1 * time.Minute
maintainStatusShortIntervalAddRandom = 2 * time.Minute
)
var (
publicIdentity *cabin.Identity
publicIdentityKey = "core:spn/public/identity"
publicIdentityUpdateTask *modules.Task
statusUpdateTask *modules.Task
)
func loadPublicIdentity() (err error) {
var changed bool
publicIdentity, changed, err = cabin.LoadIdentity(publicIdentityKey)
switch err {
case nil:
// load was successful
log.Infof("spn/captain: loaded public hub identity %s", publicIdentity.Hub.ID)
case database.ErrNotFound:
// does not exist, create new
publicIdentity, err = cabin.CreateIdentity(module.Ctx, conf.MainMapName)
if err != nil {
return fmt.Errorf("failed to create new identity: %w", err)
}
publicIdentity.SetKey(publicIdentityKey)
changed = true
log.Infof("spn/captain: created new public hub identity %s", publicIdentity.ID)
default:
// loading error, abort
return fmt.Errorf("failed to load public identity: %w", err)
}
// Save to database if the identity changed.
if changed {
err = publicIdentity.Save()
if err != nil {
return fmt.Errorf("failed to save new/updated identity to database: %w", err)
}
}
// Set available networks.
conf.SetHubNetworks(
publicIdentity.Hub.Info.IPv4 != nil,
publicIdentity.Hub.Info.IPv6 != nil,
)
// Set Home Hub before updating the hub on the map, as this would trigger a
// recalculation without a Home Hub.
ok := navigator.Main.SetHome(publicIdentity.ID, nil)
// Always update the navigator in any case in order to sync the reference to
// the active struct of the identity.
navigator.Main.UpdateHub(publicIdentity.Hub)
// Setting the Home Hub will have failed if the identidy was only just
// created - try again if it failed.
if !ok {
ok = navigator.Main.SetHome(publicIdentity.ID, nil)
if !ok {
return errors.New("failed to set self as home hub")
}
}
return nil
}
func prepPublicIdentityMgmt() error {
publicIdentityUpdateTask = module.NewTask(
"maintain public identity",
maintainPublicIdentity,
)
statusUpdateTask = module.NewTask(
"maintain public status",
maintainPublicStatus,
).Repeat(maintainStatusRegularInterval)
return module.RegisterEventHook(
"config",
"config change",
"update public identity from config",
func(_ context.Context, _ interface{}) error {
// trigger update in 5 minutes
publicIdentityUpdateTask.Schedule(time.Now().Add(5 * time.Minute))
return nil
},
)
}
func maintainPublicIdentity(ctx context.Context, task *modules.Task) error {
changed, err := publicIdentity.MaintainAnnouncement(false)
if err != nil {
return fmt.Errorf("failed to maintain announcement: %w", err)
}
if !changed {
return nil
}
// Update on map.
navigator.Main.UpdateHub(publicIdentity.Hub)
log.Debug("spn/captain: updated own hub on map after announcement change")
// export announcement
announcementData, err := publicIdentity.ExportAnnouncement()
if err != nil {
return fmt.Errorf("failed to export announcement: %w", err)
}
// forward to other connected Hubs
gossipRelayMsg("", GossipHubAnnouncementMsg, announcementData)
// manage docks in order to react to possibly changed transports
if managePiersTask != nil {
managePiersTask.Queue()
}
return nil
}
func maintainPublicStatus(ctx context.Context, task *modules.Task) error {
// Schedule maintenance soon again if something failed.
var tryAgainSoon bool
defer func() {
if tryAgainSoon {
maintainStatusSoon(
maintainStatusShortInterval,
maintainStatusShortIntervalAddRandom,
)
}
}()
// Maintain cranes.
cranes := docks.GetAllAssignedCranes()
for _, crane := range cranes {
// Ignore private or stopped cranes.
if !crane.Public() || crane.Stopped() {
continue
}
// Maintain crane.
tErr := maintainCrane(ctx, crane)
switch {
case tErr == nil:
// All good, continue.
case errors.Is(tErr, terminal.ErrStopping):
// Check if we should stop.
select {
case <-ctx.Done():
return nil
default:
}
case errors.Is(tErr, terminal.ErrTryAgainLater):
// Someone else is alredy running a test, try again later.
tryAgainSoon = true
case tErr.IsError():
// Some other error happened.
log.Errorf("captain: maintaining crane %s failed: %s", crane.ID, tErr)
}
}
// Get current lanes.
lanes := make([]*hub.Lane, 0, len(cranes))
for _, crane := range cranes {
// Ignore private or stopped cranes.
if !crane.Public() || crane.Stopped() {
continue
}
// Add crane lane.
lanes = append(lanes, &hub.Lane{
ID: crane.ConnectedHub.ID,
Capacity: crane.GetLaneCapacity(),
Latency: crane.GetLaneLatency(),
})
}
// Sort Lanes for comparing.
hub.SortLanes(lanes)
// Get system load and convert to fixed steps.
var load int
loadAvg, ok := metrics.LoadAvg15()
switch {
case !ok:
load = -1
case loadAvg >= 1:
load = 100
case loadAvg >= 0.95:
load = 95
case loadAvg >= 0.8:
load = 80
default:
load = 0
}
if loadAvg >= 0.8 {
log.Warningf("spn/captain: publishing 15m system load average of %.2f as %d", loadAvg, load)
}
// Run maintenance with the new data.
changed, err := publicIdentity.MaintainStatus(lanes, load, false)
if err != nil {
return fmt.Errorf("failed to maintain status: %w", err)
}
if !changed {
return nil
}
// Update on map.
navigator.Main.UpdateHub(publicIdentity.Hub)
log.Debug("spn/captain: updated own hub on map after status change")
// export status
statusData, err := publicIdentity.ExportStatus()
if err != nil {
return fmt.Errorf("failed to export status: %w", err)
}
// forward to other connected Hubs
gossipRelayMsg("", GossipHubStatusMsg, statusData)
log.Infof(
"spn/captain: updated status with load %d and current lanes: %v",
publicIdentity.Hub.Status.Load,
publicIdentity.Hub.Status.Lanes,
)
return nil
}
func maintainCrane(ctx context.Context, crane *docks.Crane) *terminal.Error {
// Don't maintain private or stopped cranes.
if !crane.Public() || crane.Stopped() {
return nil
}
// Measure latency if needed.
if time.Now().After(crane.LaneLatencyExpiresAt()) {
// Start op.
latOp, tErr := docks.NewLatencyTestOp(crane.Controller)
if tErr != nil {
return tErr
}
// Wait for op to complete.
select {
case tErr = <-latOp.Result():
if tErr.IsError() {
return tErr
}
case <-ctx.Done():
return terminal.ErrStopping
}
}
// Measure capacity if needed.
if time.Now().After(crane.LaneCapacityExpiresAt()) {
// Start op.
capOp, tErr := docks.NewCapacityTestOp(crane.Controller)
if tErr != nil {
return tErr
}
// Wait for op to complete.
select {
case tErr = <-capOp.Result():
if tErr.IsError() {
return tErr
}
case <-ctx.Done():
return terminal.ErrStopping
}
}
return nil
}
func maintainStatusSoon(tryIn, addRandom time.Duration) {
n, err := rng.Number(uint64(addRandom) * 2)
if err != nil {
log.Warningf("captain: failed to get random number for scheduling status maintenance: %s", err)
} else {
tryIn += time.Duration(n)
}
statusUpdateTask.Schedule(time.Now().Add(tryIn))
}