-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtasks.go
More file actions
207 lines (177 loc) · 6.83 KB
/
Copy pathtasks.go
File metadata and controls
207 lines (177 loc) · 6.83 KB
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
package pdpnode
import (
"context"
"github.com/snadrus/must"
"golang.org/x/xerrors"
"github.com/filecoin-project/curio/alertmanager"
"github.com/filecoin-project/curio/cuhttp/servicedeps"
"github.com/filecoin-project/curio/harmony/harmonytask"
"github.com/filecoin-project/curio/harmony/resources/ffigpu"
"github.com/filecoin-project/curio/harmony/taskhelp"
"github.com/filecoin-project/curio/lib/chainsched"
"github.com/filecoin-project/curio/tasks/dbmaint"
"github.com/filecoin-project/curio/tasks/gc"
"github.com/filecoin-project/curio/tasks/indexing"
"github.com/filecoin-project/curio/tasks/message"
"github.com/filecoin-project/curio/tasks/pay"
"github.com/filecoin-project/curio/tasks/pdp"
"github.com/filecoin-project/curio/tasks/pdpv0"
)
// TaskResult is the output of RegisterTasks.
type TaskResult struct {
Engine *harmonytask.TaskEngine
AlertTask *alertmanager.AlertTask
ServiceDeps servicedeps.Deps
EthSender *message.SenderETH
}
type pdpTaskBundle struct {
tasks []harmonytask.TaskInterface
amTask *alertmanager.AlertTask
ethSender *message.SenderETH
}
func buildPDPTasks(ctx context.Context, d *Deps, chainSched *chainsched.CurioChainSched, pdponly bool) (*pdpTaskBundle, error) {
cfg := d.Cfg
db := d.DB
var tasks []harmonytask.TaskInterface
if pdponly {
_, sendTask := message.NewSender(d.Chain, d.Chain, db, cfg.Fees.MaximizeFeeCap)
tasks = append(tasks, sendTask)
}
// Obtain eth client once; PDP is entirely non-functional without it.
ethClient, err := d.EthClient.Val()
if err != nil {
return nil, xerrors.Errorf("eth client required for PDP: %w", err)
}
senderEth, ethSenderTask := message.NewSenderETH(ethClient, db)
tasks = append(tasks, ethSenderTask, message.NewMessageWaitsEthGCTask(db, ethClient))
pdp.NewWatcherDataSetCreate(db, ethClient, chainSched)
pdp.NewWatcherPieceAdd(db, chainSched, ethClient)
pdp.NewWatcherDelete(db, chainSched)
pdp.NewWatcherPieceDelete(db, chainSched)
tasks = append(tasks,
pdp.NewPDPNotifyTask(db),
pdp.NewProveTask(chainSched, db, ethClient, d.Chain, senderEth, d.CachedPieceReader, d.IndexStore),
pdp.NewNextProvingPeriodTask(db, ethClient, d.Chain, chainSched, senderEth),
pdp.NewInitProvingPeriodTask(db, ethClient, d.Chain, chainSched, senderEth),
pdp.NewPDPCommpTask(db, d.PieceIO, cfg.Subsystems.CommPMaxTasks),
pdp.NewPDPTaskAddPiece(db, senderEth, ethClient),
pdp.NewPDPTaskAddDataSet(db, senderEth, ethClient, d.Chain),
pdp.NewAggregatePDPDealTask(db, d.PieceIO),
pdp.NewTaskPDPSaveCache(db, d.CachedPieceReader, d.IndexStore),
pdp.NewPDPTaskDeletePiece(db, senderEth, ethClient),
pdp.NewPDPTaskDeleteDataSet(db, senderEth, ethClient, d.Chain),
)
w := pdpv0.NewPDPv0Watcher(db, ethClient, chainSched, d.Al)
pdpv0.NewDataSetWatch(w)
pay.NewSettleWatcher(w)
pdpv0.NewDataSetDeleteWatcher(w)
pdpv0.NewCleanupPiecesWatcher(w)
pdpv0.NewProcessDeletionsWatcher(w)
pdpv0.NewProvingPeriodWatcher(w)
pdpv0.NewTerminateServiceWatcher(w)
tasks = append(tasks,
pdpv0.NewProveTask(db, ethClient, d.Chain, w, senderEth, d.CachedPieceReader, d.IndexStore),
pdpv0.NewNextProvingPeriodTask(db, ethClient, d.Chain, w, senderEth),
pdpv0.NewInitProvingPeriodTask(db, ethClient, d.Chain, w, senderEth),
pdpv0.NewProcessDeletionsTask(db, ethClient, d.Chain, w, senderEth),
pdpv0.NewPDPNotifyTask(ctx, db),
pdpv0.NewPDPPullPieceTask(ctx, db, d.PieceIO, cfg.Subsystems.PDPPullPieceMaxTasks),
pdpv0.NewTerminateServiceTask(db, ethClient, senderEth),
pdpv0.NewDeleteDataSetTask(db, ethClient, senderEth),
pdpv0.NewCleanupPiecesTask(db, ethClient, senderEth),
pdpv0.NewTaskChainSync(db, ethClient, senderEth),
pay.NewSettleTask(db, ethClient, senderEth, d.Al), // Move this to a common section once PDP v1 is live
pdpv0.NewTaskPDPSaveCache(db, d.CachedPieceReader, d.IndexStore),
pdpv0.NewPieceGCTask(&cfg.HTTP, db, d.IndexStore, ethClient, cfg.Subsystems.PDPUnclaimedUploadKeepHours),
pdpv0.NewReorgCheckTask(db, ethClient, d.Chain),
pdpv0.NewDatasetIdxTask(db, ethClient),
pdpv0.NewRepairMissingPiecesTask(db, ethClient, d.PieceIO),
)
// Start PDP watcher after all internal watcher are created
w.Run(ctx)
idxMax := taskhelp.Max(cfg.Subsystems.IndexingMaxTasks)
tasks = append(tasks,
indexing.NewPDPIndexingTask(db, d.IndexStore, d.CachedPieceReader, cfg, idxMax),
indexing.NewPDPIPNITask(db, cfg, idxMax, d.IndexStore),
indexing.NewPDPV0IndexingTask(db, d.IndexStore, d.CachedPieceReader, cfg, idxMax),
indexing.NewPDPV0IPNITask(db, cfg, idxMax, d.IndexStore),
)
if pdponly {
amTask := alertmanager.NewAlertTask(d.Chain, db, cfg.Alerting)
tasks = append(tasks, amTask, gc.NewPieceCleanupTask(db, d.IndexStore))
if cfg.Subsystems.EnableDBAnalyze {
tasks = append(tasks, dbmaint.NewDBAnalyzeTask(db))
}
return &pdpTaskBundle{
tasks: tasks,
amTask: amTask,
ethSender: senderEth,
}, nil
}
return &pdpTaskBundle{
tasks: tasks,
amTask: nil,
ethSender: senderEth,
}, nil
}
// RegisterTasks wires PDP harmony tasks and returns the task engine.
func RegisterTasks(ctx context.Context, d *Deps) (*TaskResult, error) {
if d.DB.ReadOnly() {
log.Info("readonly database mode: skipping background tasks")
return &TaskResult{}, nil
}
chainSched := chainsched.New(d.Chain)
bundle, err := buildPDPTasks(ctx, d, chainSched, true)
if err != nil {
return nil, err
}
ht, err := harmonytask.New(d.DB, bundle.tasks, d.MachineHost, nil, ffigpu.Inspector{})
if err != nil {
return nil, err
}
d.MachineID = int64(ht.ResourcesAvailable().MachineID)
ethClient := must.One(d.EthClient.Val())
// NewMessageWatcherEth registers itself with ht for side effects; no
// external handle is needed after construction.
watcherEth, err := message.NewMessageWatcherEth(d.DB, ht, chainSched, ethClient)
if err != nil {
return nil, xerrors.Errorf("eth message watcher: %w", err)
}
_ = watcherEth
err = message.NewMessageReplacer(ctx, message.ReplacerConfig{
DB: d.DB,
ChainSched: chainSched,
Eth: &message.EthReplacerConfig{
Client: ethClient,
},
})
if err != nil {
return nil, xerrors.Errorf("message replacer: %w", err)
}
go chainSched.Run(ctx)
return &TaskResult{
Engine: ht,
AlertTask: bundle.amTask,
EthSender: bundle.ethSender,
ServiceDeps: servicedeps.Deps{
EthSender: bundle.ethSender,
AlertTask: bundle.amTask,
},
}, nil
}
// AppendTasks adds PDP tasks to a curio task list.
func AppendTasks(ctx context.Context, d *Deps, chainSched *chainsched.CurioChainSched, active *[]harmonytask.TaskInterface) (*servicedeps.Deps, error) {
if d.DB.ReadOnly() {
log.Info("readonly database mode: skipping background tasks")
return &servicedeps.Deps{}, nil
}
bundle, err := buildPDPTasks(ctx, d, chainSched, false)
if err != nil {
return nil, err
}
*active = append(*active, bundle.tasks...)
return &servicedeps.Deps{
EthSender: bundle.ethSender,
AlertTask: bundle.amTask,
}, nil
}