forked from akash-network/provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
488 lines (406 loc) · 12.7 KB
/
manager.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
package manifest
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"time"
"github.com/boz/go-lifecycle"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
maniv2beta1 "github.com/akash-network/node/manifest/v2beta1"
"github.com/akash-network/node/pubsub"
"github.com/akash-network/node/sdl"
sdlutil "github.com/akash-network/node/sdl/util"
"github.com/akash-network/node/util/runner"
"github.com/akash-network/node/validation"
dtypes "github.com/akash-network/node/x/deployment/types/v1beta2"
mtypes "github.com/akash-network/node/x/market/types/v1beta2"
clustertypes "github.com/akash-network/provider/cluster/types/v1beta2"
"github.com/akash-network/provider/event"
"github.com/akash-network/provider/session"
)
const (
manifestLingerDuration = time.Minute * time.Duration(5)
)
var (
// ErrShutdownTimerExpired for a terminating deployment
ErrShutdownTimerExpired = errors.New("shutdown timer expired")
// ErrManifestVersion indicates that the given manifest's version does not
// match the blockchain Version value.
ErrManifestVersion = errors.New("manifest version validation failed")
ErrNoManifestForDeployment = errors.New("manifest not yet received for that deployment")
ErrNoLeaseForDeployment = errors.New("no lease for deployment")
errNoGroupForLease = errors.New("group not found")
errManifestRejected = errors.New("manifest rejected")
)
func newManager(h *service, daddr dtypes.DeploymentID) *manager {
session := h.session.ForModule("manifest-manager")
m := &manager{
daddr: daddr,
session: session,
bus: h.bus,
leasech: make(chan event.LeaseWon),
rmleasech: make(chan mtypes.LeaseID),
manifestch: make(chan manifestRequest),
updatech: make(chan []byte),
log: session.Log().With("deployment", daddr),
lc: lifecycle.New(),
config: h.config,
hostnameService: h.hostnameService,
}
go m.lc.WatchChannel(h.lc.ShuttingDown())
go m.run(h.managerch)
return m
}
// 'manager' facilitates operations around a configured Deployment.
type manager struct {
config ServiceConfig
daddr dtypes.DeploymentID
session session.Session
bus pubsub.Bus
leasech chan event.LeaseWon
rmleasech chan mtypes.LeaseID
manifestch chan manifestRequest
updatech chan []byte
data dtypes.QueryDeploymentResponse
requests []manifestRequest
pendingRequests []manifestRequest
manifests []*maniv2beta1.Manifest
versions [][]byte
localLeases []event.LeaseWon
fetched bool
fetchedAt time.Time
stoptimer *time.Timer
log log.Logger
lc lifecycle.Lifecycle
hostnameService clustertypes.HostnameServiceClient
}
func (m *manager) stop() {
m.lc.ShutdownAsync(nil)
}
func (m *manager) handleLease(ev event.LeaseWon) {
select {
case m.leasech <- ev:
case <-m.lc.ShuttingDown():
m.log.Error("not running: handle manifest", "lease", ev.LeaseID)
}
}
func (m *manager) removeLease(id mtypes.LeaseID) {
select {
case m.rmleasech <- id:
case <-m.lc.ShuttingDown():
m.log.Error("not running: remove lease", "lease", id)
}
}
func (m *manager) handleManifest(req manifestRequest) {
select {
case m.manifestch <- req:
case <-m.lc.ShuttingDown():
m.log.Error("not running: handle manifest")
req.ch <- ErrNotRunning
}
}
func (m *manager) handleUpdate(version []byte) {
select {
case m.updatech <- version:
case <-m.lc.ShuttingDown():
m.log.Error("not running: version update", "version", hex.EncodeToString(version))
}
}
func (m *manager) clearFetched() {
m.fetchedAt = time.Time{}
m.fetched = false
m.data = dtypes.QueryDeploymentResponse{}
m.localLeases = nil
}
func (m *manager) run(donech chan<- *manager) {
defer m.lc.ShutdownCompleted()
defer func() { donech <- m }()
var runch <-chan runner.Result
ctx, cancel := context.WithCancel(context.Background())
loop:
for {
var stopch <-chan time.Time
if m.stoptimer != nil {
stopch = m.stoptimer.C
}
select {
case err := <-m.lc.ShutdownRequest():
m.lc.ShutdownInitiated(err)
break loop
case <-stopch:
m.log.Error(ErrShutdownTimerExpired.Error())
m.lc.ShutdownInitiated(ErrShutdownTimerExpired)
break loop
case ev := <-m.leasech:
m.log.Info("new lease", "lease", ev.LeaseID)
m.clearFetched()
m.maybeScheduleStop()
runch = m.maybeFetchData(ctx, runch)
case id := <-m.rmleasech:
m.log.Info("lease removed", "lease", id)
m.clearFetched()
m.maybeScheduleStop()
case req := <-m.manifestch:
m.log.Info("manifest received")
m.requests = append(m.requests, req)
m.maybeScheduleStop()
if runch = m.maybeFetchData(ctx, runch); runch == nil {
m.validateRequests()
m.emitReceivedEvents()
}
case version := <-m.updatech:
m.log.Info("received version", "version", hex.EncodeToString(version))
m.versions = append(m.versions, version)
m.clearFetched()
case result := <-runch:
runch = nil
if err := result.Error(); err != nil {
m.log.Error("error fetching data", "err", err)
// Fetching data failed, all requests are now in an error state
m.fillAllRequests(err)
break
}
fetchResult := result.Value().(manifestManagerFetchDataResult)
m.fetched = true
m.fetchedAt = time.Now()
m.data = fetchResult.deployment
m.localLeases = fetchResult.leases
m.log.Info("data received", "version", hex.EncodeToString(m.data.Deployment.Version))
m.validateRequests()
m.emitReceivedEvents()
m.maybeScheduleStop()
}
}
cancel()
m.fillAllRequests(ErrNotRunning)
if m.stoptimer != nil {
if m.stoptimer.Stop() {
<-m.stoptimer.C
}
}
if runch != nil {
<-runch
}
}
// maybeFetchData try fetch deployment and lease data
// if there is cached result the function returns nil channel which signals caller to process events
func (m *manager) maybeFetchData(ctx context.Context, runch <-chan runner.Result) <-chan runner.Result {
if runch != nil {
return runch
}
if !m.fetched || time.Since(m.fetchedAt) > m.config.CachedResultMaxAge {
m.clearFetched()
return m.fetchData(ctx)
}
return runch
}
func (m *manager) fetchData(ctx context.Context) <-chan runner.Result {
return runner.Do(func() runner.Result {
// TODO: retry
return runner.NewResult(m.doFetchData(ctx))
})
}
type manifestManagerFetchDataResult struct {
deployment dtypes.QueryDeploymentResponse
leases []event.LeaseWon
}
func (m *manager) doFetchData(ctx context.Context) (manifestManagerFetchDataResult, error) {
subctx, cancel := context.WithTimeout(ctx, m.config.RPCQueryTimeout)
defer cancel()
deploymentResponse, err := m.session.Client().Query().Deployment(subctx, &dtypes.QueryDeploymentRequest{ID: m.daddr})
if err != nil {
return manifestManagerFetchDataResult{}, err
}
leasesResponse, err := m.session.Client().Query().Leases(subctx, &mtypes.QueryLeasesRequest{
Filters: mtypes.LeaseFilters{
Owner: m.daddr.Owner,
DSeq: m.daddr.DSeq,
GSeq: 0,
OSeq: 0,
Provider: m.session.Provider().GetOwner(),
State: mtypes.LeaseActive.String(),
},
Pagination: nil,
})
if err != nil {
return manifestManagerFetchDataResult{}, err
}
groups := make(map[uint32]dtypes.Group)
for _, g := range deploymentResponse.GetGroups() {
groups[g.ID().GSeq] = g
}
leases := make([]event.LeaseWon, len(leasesResponse.Leases))
for i, leaseEntry := range leasesResponse.Leases {
lease := leaseEntry.GetLease()
leaseID := lease.GetLeaseID()
groupForLease, foundGroup := groups[leaseID.GetGSeq()]
if !foundGroup {
return manifestManagerFetchDataResult{}, fmt.Errorf("%w: could not locate group %v ", errNoGroupForLease, leaseID)
}
ev := event.LeaseWon{
LeaseID: leaseID,
Group: &groupForLease,
Price: lease.GetPrice(),
}
leases[i] = ev
}
return manifestManagerFetchDataResult{
deployment: *deploymentResponse,
leases: leases,
}, nil
}
func (m *manager) maybeScheduleStop() bool { // nolint:golint,unparam
if len(m.localLeases) > 0 || len(m.manifests) > 0 {
if m.stoptimer != nil {
m.log.Info("stopping stop timer")
if m.stoptimer.Stop() {
<-m.stoptimer.C
}
m.stoptimer = nil
}
return false
}
if m.stoptimer != nil {
m.log.Info("starting stop timer", "duration", manifestLingerDuration)
m.stoptimer = time.NewTimer(manifestLingerDuration)
}
return true
}
func (m *manager) fillAllRequests(response error) {
for _, req := range m.pendingRequests {
req.ch <- response
}
m.pendingRequests = nil
for _, req := range m.requests {
req.ch <- response
}
m.requests = nil
}
func (m *manager) emitReceivedEvents() {
if !m.fetched || len(m.manifests) == 0 {
m.log.Debug("emit received events skipped", "data", m.data, "manifests", len(m.manifests))
return
}
if len(m.localLeases) == 0 {
m.log.Debug("emit received events skips due to no leases", "data", m.data, "manifests", len(m.manifests))
m.fillAllRequests(ErrNoLeaseForDeployment)
return
}
latestManifest := m.manifests[len(m.manifests)-1]
m.log.Debug("publishing manifest received", "num-leases", len(m.localLeases))
copyOfData := new(dtypes.QueryDeploymentResponse)
*copyOfData = m.data
for _, lease := range m.localLeases {
m.log.Debug("publishing manifest received for lease", "lease_id", lease.LeaseID)
if err := m.bus.Publish(event.ManifestReceived{
LeaseID: lease.LeaseID,
Group: lease.Group,
Manifest: latestManifest,
Deployment: copyOfData,
}); err != nil {
m.log.Error("publishing event", "err", err, "lease", lease.LeaseID)
}
}
// A manifest has been published, satisfy all pending requests
for _, req := range m.pendingRequests {
req.ch <- nil
}
m.pendingRequests = nil
}
func (m *manager) validateRequests() {
if !m.fetched || len(m.requests) == 0 {
return
}
manifests := make([]*maniv2beta1.Manifest, 0)
for _, req := range m.requests {
// If the request context is complete then skip processing it
select {
case <-req.ctx.Done():
continue
default:
}
if err := m.validateRequest(req); err != nil {
m.log.Error("invalid manifest", "err", err)
req.ch <- err
continue
}
manifests = append(manifests, &req.value.Manifest)
// The manifest has been grabbed from the request but not published yet, store this response
m.pendingRequests = append(m.pendingRequests, req)
}
m.requests = nil // all requests processed at this time
m.log.Debug("requests valid", "num-requests", len(manifests))
if len(manifests) > 0 {
// XXX: only one version means only one valid manifest
m.manifests = append(m.manifests, manifests[0])
}
}
func (m *manager) validateRequest(req manifestRequest) error {
select {
case <-req.ctx.Done():
return req.ctx.Err()
default:
}
// ensure that an uploaded manifest matches the hash declared on
// the Akash Deployment.Version
version, err := sdl.ManifestVersion(req.value.Manifest)
if err != nil {
return err
}
var versionExpected []byte
if len(m.versions) != 0 {
versionExpected = m.versions[len(m.versions)-1]
} else {
versionExpected = m.data.Deployment.Version
}
if !bytes.Equal(version, versionExpected) {
m.log.Info("deployment version mismatch", "expected", m.data.Deployment.Version, "got", version)
return ErrManifestVersion
}
if err = validation.ValidateManifest(req.value.Manifest); err != nil {
return err
}
if err = validation.ValidateManifestWithDeployment(&req.value.Manifest, m.data.Groups); err != nil {
return err
}
groupNames := make([]string, 0)
for _, lease := range m.localLeases {
groupNames = append(groupNames, lease.Group.GroupSpec.Name)
}
// Check that hostnames are not in use
if err = m.checkHostnamesForManifest(req.value.Manifest, groupNames); err != nil {
return err
}
return nil
}
func (m *manager) checkHostnamesForManifest(requestManifest maniv2beta1.Manifest, groupNames []string) error {
// Check if the hostnames are available. Do not block forever
ownerAddr, err := m.data.GetDeployment().DeploymentID.GetOwnerAddress()
if err != nil {
return err
}
allHostnames := make([]string, 0)
for _, mgroup := range requestManifest.GetGroups() {
for _, groupName := range groupNames {
// Only check leases with a matching deployment ID & group name
if groupName != mgroup.GetName() {
continue
}
allHostnames = append(allHostnames, sdlutil.AllHostnamesOfManifestGroup(mgroup)...)
if !m.config.HTTPServicesRequireAtLeastOneHost {
continue
}
// For each service that exposes via an Ingress, then require a hsotname
for _, service := range mgroup.Services {
for _, expose := range service.Expose {
if sdlutil.ShouldBeIngress(expose) && len(expose.Hosts) == 0 {
return fmt.Errorf("%w: service %q exposed on %d:%s must have a hostname", errManifestRejected, service.Name, sdlutil.ExposeExternalPort(expose), expose.Proto)
}
}
}
}
}
return m.hostnameService.CanReserveHostnames(allHostnames, ownerAddr)
}