-
Notifications
You must be signed in to change notification settings - Fork 10
/
engine.go
410 lines (353 loc) Β· 10.7 KB
/
engine.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
package engine
import (
"context"
"math"
"os/exec"
"sync"
"time"
"github.com/benbjohnson/clock"
"github.com/filecoin-project/dealbot/controller/client"
"github.com/filecoin-project/dealbot/lotus"
"github.com/filecoin-project/dealbot/tasks"
"github.com/filecoin-project/lotus/api"
"github.com/google/uuid"
"github.com/urfave/cli/v2"
logging "github.com/ipfs/go-log/v2"
)
const (
maxTaskRunTime = 24 * time.Hour
noTasksWait = 5 * time.Second
popTaskTimeout = 2 * time.Second
)
var log = logging.Logger("engine")
type apiClient interface {
GetTask(ctx context.Context, uuid string) (tasks.Task, error)
UpdateTask(ctx context.Context, uuid string, r tasks.UpdateTask) (tasks.Task, error)
PopTask(ctx context.Context, r tasks.PopTask) (tasks.Task, error)
ResetWorker(ctx context.Context, worker string) error
}
type taskExecutor interface {
MakeStorageDeal(ctx context.Context, config tasks.NodeConfig, node api.FullNode, task tasks.StorageTask, updateStage tasks.UpdateStage, log tasks.LogStatus, stageTimeouts map[string]time.Duration, releaseWorker func()) error
MakeRetrievalDeal(ctx context.Context, config tasks.NodeConfig, node api.FullNode, task tasks.RetrievalTask, updateStage tasks.UpdateStage, log tasks.LogStatus, stageTimeouts map[string]time.Duration, releaseWorker func()) error
}
type defaultTaskExecutor struct{}
func (defaultTaskExecutor) MakeStorageDeal(ctx context.Context, config tasks.NodeConfig, node api.FullNode, task tasks.StorageTask, updateStage tasks.UpdateStage, log tasks.LogStatus, stageTimeouts map[string]time.Duration, releaseWorker func()) error {
return tasks.MakeStorageDeal(ctx, config, node, task, updateStage, log, stageTimeouts, releaseWorker)
}
func (defaultTaskExecutor) MakeRetrievalDeal(ctx context.Context, config tasks.NodeConfig, node api.FullNode, task tasks.RetrievalTask, updateStage tasks.UpdateStage, log tasks.LogStatus, stageTimeouts map[string]time.Duration, releaseWorker func()) error {
return tasks.MakeRetrievalDeal(ctx, config, node, task, updateStage, log, stageTimeouts, releaseWorker)
}
type Engine struct {
shutdown chan struct{}
stopped chan struct{}
queueIsEmpty chan struct{}
host string
tags []string
stageTimeouts map[string]time.Duration
// depedencies
node api.FullNode
nodeConfig tasks.NodeConfig
closer lotus.NodeCloser
client apiClient
clock clock.Clock
taskExecutor taskExecutor
}
func New(ctx context.Context, cliCtx *cli.Context) (*Engine, error) {
workers := cliCtx.Int("workers")
if workers < 1 {
workers = 1
}
host_id := cliCtx.String("id")
if host_id == "" {
host_id = uuid.New().String()[:8]
}
stageTimeouts, err := tasks.ParseStageTimeouts(cliCtx.StringSlice("stage-timeout"))
if err != nil {
return nil, err
}
client := client.New(cliCtx)
nodeConfig, node, closer, err := lotus.SetupClient(ctx, cliCtx)
if err != nil {
return nil, err
}
clock := clock.New()
tags := cliCtx.StringSlice("tags")
engine, err := new(ctx, host_id, stageTimeouts, tags, node, nodeConfig, closer, client, clock, defaultTaskExecutor{}, nil)
if err != nil {
return nil, err
}
go engine.run(ctx, workers)
return engine, nil
}
// used for testing
func new(
ctx context.Context,
host string,
stageTimeouts map[string]time.Duration,
tags []string,
node api.FullNode,
nodeConfig tasks.NodeConfig,
closer lotus.NodeCloser,
client apiClient,
clock clock.Clock,
taskExecutor taskExecutor,
queueIsEmpty chan struct{},
) (*Engine, error) {
v, err := node.Version(ctx)
if err != nil {
return nil, err
}
log.Infof("remote version: %s", v.Version)
// before we do anything, reset all this workers tasks
err = client.ResetWorker(ctx, host)
if err != nil {
// for now, just log an error if this happens... seems like there are scenarios
// where we want to get the dealbot up and running even though reset worker failed for
// whatever eason
log.Errorf("error resetting tasks for worker: %s", err)
}
return &Engine{
client: client,
nodeConfig: nodeConfig,
node: node,
closer: closer,
host: host,
shutdown: make(chan struct{}),
stopped: make(chan struct{}),
tags: tags,
stageTimeouts: stageTimeouts,
clock: clock,
taskExecutor: taskExecutor,
queueIsEmpty: queueIsEmpty,
}, nil
}
func (e *Engine) run(ctx context.Context, workers int) {
defer close(e.stopped)
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(ctx)
e.taskLoop(ctx, wg, workers)
cancel()
// Stop workers and wait for all workers to exit
wg.Wait()
}
func (e *Engine) taskLoop(ctx context.Context, wg sync.WaitGroup, workers int) {
// super annoying -- make a new timer that is already stopped
popTimer := e.clock.Timer(math.MinInt64)
if !popTimer.Stop() {
<-popTimer.C
}
ready := make(chan struct{}, 1)
// we start in a ready state
ready <- struct{}{}
released := make(chan struct{}, workers)
active := 0
for {
// insure at most one operation runs before a quit
select {
case <-e.shutdown:
return
default:
}
select {
case <-e.shutdown:
return
case <-ready:
// stop and drain timer if not already drained
if !popTimer.Stop() {
select {
case <-popTimer.C:
default:
}
}
task := e.tryPopTask()
if task != nil {
active++
wg.Add(1)
go func() {
defer wg.Done()
e.runTask(ctx, task, 1, released)
}()
if active < workers {
ready <- struct{}{}
}
} else {
popTimer.Reset(noTasksWait)
// only used for testing
if e.queueIsEmpty != nil {
e.queueIsEmpty <- struct{}{}
}
}
case <-popTimer.C:
// ready to queue next task if not otherwise queued
select {
case ready <- struct{}{}:
default:
}
case <-released:
active--
// ready to queue next task if not otherwise queued
select {
case ready <- struct{}{}:
default:
}
}
}
}
func (e *Engine) Close(ctx context.Context) {
close(e.shutdown) // signal to stop workers
<-e.stopped // wait for workers to stop
e.closer()
}
func (e *Engine) tryPopTask() tasks.Task {
if !e.apiGood() {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), popTaskTimeout)
defer cancel()
// pop a task
task, err := e.client.PopTask(ctx, tasks.Type.PopTask.Of(e.host, tasks.InProgress, e.tags...))
if err != nil {
log.Warnw("pop-task returned error", "err", err)
return nil
}
if task == nil {
return nil // no task available
}
if task.WorkedBy.Must().String() != e.host {
log.Warnw("pop-task returned task that is not for this host", "err", err)
return nil
}
log.Infow("successfully acquired task", "taskID", task.UUID.String())
return task // found a runable task
}
func (e *Engine) runTask(ctx context.Context, task tasks.Task, runCount int, released chan<- struct{}) {
var err error
log.Infow("worker running task", "taskID", task.UUID.String(), "run_count", runCount)
var releaseOnce sync.Once
releaseWorker := func() {
releaseOnce.Do(func() {
released <- struct{}{}
})
}
// Define function to update task stage. Use shutdown context, not task
updateStage := func(ctx context.Context, stage string, stageDetails tasks.StageDetails) error {
_, err := e.client.UpdateTask(ctx, task.UUID.String(),
tasks.Type.UpdateTask.OfStage(
e.host,
tasks.InProgress,
"",
stage,
stageDetails,
runCount,
))
return err
}
finalStatus := tasks.Successful
finalErrorMessage := ""
tlog := log.With("taskID", task.UUID.String())
// Start deals
if task.RetrievalTask.Exists() {
err = e.taskExecutor.MakeRetrievalDeal(ctx, e.nodeConfig, e.node, task.RetrievalTask.Must(), updateStage, log.Infow, e.stageTimeouts, releaseWorker)
if err != nil {
if err == context.Canceled {
// Engine closed, do not update final state
tlog.Warn("task abandoned for shutdown")
return
}
finalStatus = tasks.Failed
finalErrorMessage = err.Error()
tlog.Errorw("retrieval task returned error", "err", err)
} else {
tlog.Info("successfully retrieved data")
}
} else if task.StorageTask.Exists() {
err = e.taskExecutor.MakeStorageDeal(ctx, e.nodeConfig, e.node, task.StorageTask.Must(), updateStage, log.Infow, e.stageTimeouts, releaseWorker)
if err != nil {
if err == context.Canceled {
// Engine closed, do not update final state
tlog.Warn("task abandoned for shutdown")
return
}
finalStatus = tasks.Failed
finalErrorMessage = err.Error()
tlog.Errorw("storage task returned error", "err", err)
} else {
tlog.Info("successfully stored data")
}
} else {
tlog.Error("task has unknown deal type")
return
}
// Fetch task with all the updates from deal execution
task, err = e.client.GetTask(ctx, task.UUID.String())
if err != nil {
tlog.Errorw("cannot get updated task to finalize", "err", err)
}
// if we haven't already released the queue to run more jobs, release it now
releaseWorker()
// Update task final status. Do not use task context.
var stageDetails tasks.StageDetails
if task.CurrentStageDetails.Exists() {
stageDetails = task.CurrentStageDetails.Must()
} else {
stageDetails = tasks.BlankStage()
}
_, err = e.client.UpdateTask(ctx, task.UUID.String(),
tasks.Type.UpdateTask.OfStage(
e.host,
finalStatus,
finalErrorMessage,
task.Stage.String(),
stageDetails,
runCount,
))
if err != nil {
if err == context.Canceled {
tlog.Warn("task abandoned for shutdown")
return
}
tlog.Errorw("error updating final status", "err", err)
}
if e.nodeConfig.PostHook != "" {
err := exec.Command("/bin/bash", e.nodeConfig.PostHook, task.GetUUID()).Run()
if err != nil {
tlog.Errorw("could not run post hook", "err", err)
}
}
}
// apiGood returns true if the api can be reached and reports sufficient fil/cap to process tasks.
func (e *Engine) apiGood() bool {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
localBalance, err := e.node.WalletBalance(ctx, e.nodeConfig.WalletAddress)
if err != nil {
log.Errorw("could not query api for wallet balance", "err", err)
return false
}
if localBalance.LessThan(e.nodeConfig.MinWalletBalance) {
log.Warnf("Not accepting task. local balance below min")
return false
}
head, err := e.node.ChainHead(ctx)
if err != nil {
log.Errorw("could not query api for head", "err", err)
return false
}
localCap, err := e.node.StateVerifiedClientStatus(ctx, e.nodeConfig.WalletAddress, head.Key())
if err != nil {
log.Errorw("could not query api for datacap", "err", err)
return false
}
log.Infow("local datacap", "datacap", localCap)
if e.nodeConfig.MinWalletCap.Int64() < 0 {
return true
}
if localCap == nil {
return e.nodeConfig.MinWalletCap.NilOrZero()
}
if localCap.LessThan(e.nodeConfig.MinWalletCap) {
log.Warnf("Not accepting task. local datacap below min")
return false
}
return true
}