-
Notifications
You must be signed in to change notification settings - Fork 45
/
task.go
328 lines (277 loc) · 8.6 KB
/
task.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
package actorstate
import (
"context"
"fmt"
"os"
"strconv"
"sync"
"time"
"github.com/ipfs/go-cid"
"go.opencensus.io/tag"
"go.opentelemetry.io/otel"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lily/chain/actors/builtin"
"github.com/filecoin-project/lily/metrics"
"github.com/filecoin-project/lily/model"
visormodel "github.com/filecoin-project/lily/model/visor"
"github.com/filecoin-project/lily/tasks"
"github.com/filecoin-project/lotus/chain/types"
)
// A Task processes the extraction of actor state according the allowed types in its extractor map.
type Task struct {
node tasks.DataSource
extractorMap ActorExtractorMap
dataTransformer ActorDataTransformer
}
func NewTask(node tasks.DataSource, extractorMap ActorExtractorMap) *Task {
return &Task{
node: node,
extractorMap: extractorMap,
}
}
func NewTaskWithTransformer(node tasks.DataSource, extractorMap ActorExtractorMap, dataTransformer ActorDataTransformer) *Task {
return &Task{
node: node,
extractorMap: extractorMap,
dataTransformer: dataTransformer,
}
}
const actorChangeLimiterEnv = "LILY_ACTOR_CHANGE_LIMITER"
const defaultActorChangeLimit = 10000
// Skip task processing when actor state changes abnormally.
func (t *Task) exceedsActorChangeLimit(changes map[address.Address]tasks.ActorStateChange) bool {
// The 10,000 is a default threshold
limit := defaultActorChangeLimit
// Access the custom threshold set in the environment variable
s, ok := os.LookupEnv(actorChangeLimiterEnv)
if ok {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
limit = int(v)
}
}
return len(changes) > limit
}
func (t *Task) ProcessActors(ctx context.Context, current *types.TipSet, executed *types.TipSet, candidates tasks.ActorStateChangeDiff) (model.Persistable, *visormodel.ProcessingReport, error) {
ctx, span := otel.Tracer("").Start(ctx, "ProcessActors")
defer span.End()
report := &visormodel.ProcessingReport{
Height: int64(current.Height()),
StateRoot: current.ParentState().String(),
Status: visormodel.ProcessingStatusOK,
}
// Filter to just allowed actors
actors := make(map[address.Address]tasks.ActorStateChange)
for addr, ch := range candidates {
if t.extractorMap.Allow(ch.Actor.Code) {
actors[addr] = ch
}
}
ll := log.With("height", int64(current.Height()))
ll.Debug("processing actor state changes")
if len(actors) == 0 {
ll.Debug("no actor state changes found")
return model.PersistableList{}, report, nil
}
if t.exceedsActorChangeLimit(actors) {
err := fmt.Errorf("task skipped - max limit for handling actor state changes")
report.ErrorsDetected = err
return nil, report, nil
}
data := make(model.PersistableList, 0, len(actors))
errorsDetected := make([]*ActorStateError, 0, len(actors))
skippedActors := 0
start := time.Now()
ll.Debug("found actor state changes", "count", len(actors))
results := make(chan *ActorStateResult, len(actors))
// closes result when done, runs extraction for each actor in actors concurrently.
t.startActorStateExtraction(ctx, current, executed, actors, results)
// Gather results
for res := range results {
select {
case <-ctx.Done():
return data, report, ctx.Err()
default:
}
lla := log.With("height", int64(current.Height()), "actor", builtin.ActorNameByCode(res.Code), "address", res.Address)
if res.Error != nil {
lla.Errorw("actor returned with error", "code", builtin.ActorNameByCode(res.Code), "error", res.Error.Error())
errorsDetected = append(errorsDetected, &ActorStateError{
Code: res.Code.String(),
Name: builtin.ActorNameByCode(res.Code),
Head: res.Head.String(),
Address: res.Address.String(),
Error: res.Error.Error(),
})
continue
}
if res.SkippedParse {
lla.Warn("skipped actor without extractor", "code", builtin.ActorNameByCode(res.Code))
skippedActors++
}
data = append(data, res.Data)
}
if t.dataTransformer != nil {
td, err := t.dataTransformer.Transform(ctx, data)
if err != nil {
log.Warnw("failed to transform with error: ", err)
} else {
data = td
}
}
log.Debugw("completed processing actor state changes", "height", current.Height(), "success", len(actors)-len(errorsDetected)-skippedActors, "errors", len(errorsDetected), "skipped", skippedActors, "time", time.Since(start))
if skippedActors > 0 {
report.StatusInformation = fmt.Sprintf("did not parse %d actors", skippedActors)
}
if len(errorsDetected) != 0 {
report.ErrorsDetected = errorsDetected
}
return data, report, nil
}
func (t *Task) startActorStateExtraction(ctx context.Context, current, executed *types.TipSet, actors tasks.ActorStateChangeDiff, results chan *ActorStateResult) {
var wg sync.WaitGroup
// Setup the cache for robust address
err := t.node.SetIdRobustAddressMap(ctx, current.Key())
if err != nil {
log.Errorf("Error at setting IdRobustAddressMap: %v", err)
}
for addr, ac := range actors {
addr := addr
ac := ac
wg.Add(1)
go func() {
defer wg.Done()
ctx, _ = tag.New(ctx, tag.Upsert(metrics.ActorCode, builtin.ActorNameByCode(ac.Actor.Code)))
info := ActorInfo{
Actor: ac.Actor,
ChangeType: ac.ChangeType,
Address: addr,
Current: current,
Executed: executed,
}
ae, ok := t.extractorMap.GetExtractors(info.Actor.Code)
if !ok {
results <- &ActorStateResult{
Code: ac.Actor.Code,
Head: ac.Actor.Head,
Address: addr,
SkippedParse: true,
Data: nil,
}
} else {
for _, e := range ae {
e := e
wg.Add(1)
go func() {
defer wg.Done()
res := &ActorStateResult{
Code: ac.Actor.Code,
Head: ac.Actor.Head,
Address: addr,
}
stop := metrics.Timer(ctx, metrics.StateExtractionDuration)
defer stop()
data, err := e.Extract(ctx, info, t.node)
if err != nil {
res.Error = fmt.Errorf("failed to extract parsed actor state: %w", err)
}
res.Data = data
results <- res
}()
}
}
}()
}
go func() {
wg.Wait()
close(results)
}()
}
type ActorStateResult struct {
Code cid.Cid
Head cid.Cid
Address address.Address
Error error
SkippedParse bool
Data model.Persistable
}
type ActorStateError struct {
Code string
Name string
Head string
Address string
Error string
}
// An ActorExtractorMap controls which actor types may be extracted.
type ActorExtractorMap interface {
Allow(code cid.Cid) bool
GetExtractors(code cid.Cid) ([]ActorStateExtractor, bool)
}
type ActorExtractorFilter interface {
AllowAddress(addr string) bool
}
// A RawActorExtractorMap extracts all types of actors using basic actor extraction which only parses shallow state.
type RawActorExtractorMap struct {
extractors []ActorStateExtractor
}
func (r *RawActorExtractorMap) GetExtractors(_ cid.Cid) ([]ActorStateExtractor, bool) {
return r.extractors, true
}
func (r *RawActorExtractorMap) Register(ase ActorStateExtractor) {
r.extractors = append(r.extractors, ase)
}
func (r *RawActorExtractorMap) Allow(_ cid.Cid) bool {
return true
}
// A TypedActorExtractorMap extracts a single type of actor using full parsing of actor state
type TypedActorExtractorMap struct {
codes *cid.Set
extractors []ActorStateExtractor
}
func NewTypedActorExtractorMap(codes []cid.Cid, ase ...ActorStateExtractor) *TypedActorExtractorMap {
t := &TypedActorExtractorMap{
codes: cid.NewSet(),
extractors: ase,
}
for _, c := range codes {
t.codes.Add(c)
}
return t
}
func (t *TypedActorExtractorMap) Allow(code cid.Cid) bool {
return t.codes.Has(code)
}
func (t *TypedActorExtractorMap) GetExtractors(code cid.Cid) ([]ActorStateExtractor, bool) {
if !t.Allow(code) {
return nil, false
}
return t.extractors, true
}
// A CustomTypedActorExtractorMap extracts a single type of actor using full parsing of actor state
type CustomTypedActorExtractorMap struct {
codes *cid.Set
extractors map[cid.Cid][]ActorStateExtractor
}
func NewCustomTypedActorExtractorMap(extractors map[cid.Cid][]ActorStateExtractor) *CustomTypedActorExtractorMap {
t := &CustomTypedActorExtractorMap{
codes: cid.NewSet(),
extractors: extractors,
}
for c := range extractors {
t.codes.Add(c)
}
return t
}
func (c *CustomTypedActorExtractorMap) Allow(code cid.Cid) bool {
return c.codes.Has(code)
}
func (c *CustomTypedActorExtractorMap) GetExtractors(code cid.Cid) ([]ActorStateExtractor, bool) {
ex, ok := c.extractors[code]
if !ok {
return nil, false
}
return ex, true
}
type ActorDataTransformer interface {
Transform(ctx context.Context, data model.PersistableList) (model.PersistableList, error)
}