forked from Kyuubi2709/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
203 lines (174 loc) · 4.35 KB
/
worker.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
package das
import (
"context"
"errors"
"fmt"
"sync"
"time"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/share/p2p/shrexsub"
)
const (
catchupJob jobType = "catchup"
recentJob jobType = "recent"
retryJob jobType = "retry"
)
type worker struct {
lock sync.Mutex
state workerState
getter libhead.Getter[*header.ExtendedHeader]
sampleFn sampleFn
broadcast shrexsub.BroadcastFn
metrics *metrics
}
// workerState contains important information about the state of a
// current sampling routine.
type workerState struct {
result
curr uint64
}
type jobType string
// job represents headers interval to be processed by worker
type job struct {
id int
jobType jobType
from uint64
to uint64
// header is set only for recentJobs, avoiding an unnecessary call to the header store
header *header.ExtendedHeader
}
func newWorker(j job,
getter libhead.Getter[*header.ExtendedHeader],
sample sampleFn,
broadcast shrexsub.BroadcastFn,
metrics *metrics,
) worker {
return worker{
getter: getter,
sampleFn: sample,
broadcast: broadcast,
metrics: metrics,
state: workerState{
curr: j.from,
result: result{
job: j,
failed: make(map[uint64]int),
},
},
}
}
func (w *worker) run(ctx context.Context, timeout time.Duration, resultCh chan<- result) {
jobStart := time.Now()
log.Debugw("start sampling worker", "from", w.state.from, "to", w.state.to)
for curr := w.state.from; curr <= w.state.to; curr++ {
err := w.sample(ctx, timeout, curr)
if errors.Is(err, context.Canceled) {
// sampling worker will resume upon restart
return
}
w.setResult(curr, err)
}
if w.state.jobType != recentJob {
log.Infow(
"finished sampling headers",
"type", w.state.jobType,
"from", w.state.from,
"to", w.state.curr,
"errors", len(w.state.failed),
"finished (s)", time.Since(jobStart),
)
}
select {
case resultCh <- w.state.result:
case <-ctx.Done():
}
}
func (w *worker) sample(ctx context.Context, timeout time.Duration, height uint64) error {
h, err := w.getHeader(ctx, height)
if err != nil {
return err
}
start := time.Now()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err = w.sampleFn(ctx, h)
w.metrics.observeSample(ctx, h, time.Since(start), w.state.jobType, err)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Debugw(
"failed to sample header",
"type", w.state.jobType,
"height", h.Height(),
"hash", h.Hash(),
"square width", len(h.DAH.RowRoots),
"data root", h.DAH.String(),
"err", err,
"finished (s)", time.Since(start),
)
}
return err
}
logout := log.Debugw
// notify network about availability of new block data (note: only full nodes can notify)
if w.state.job.jobType == recentJob {
err = w.broadcast(ctx, shrexsub.Notification{
DataHash: h.DataHash.Bytes(),
Height: h.Height(),
})
if err != nil {
log.Warn("failed to broadcast availability message",
"height", h.Height(), "hash", h.Hash(), "err", err)
}
logout = log.Infow
}
logout(
"sampled header",
"type", w.state.jobType,
"height", h.Height(),
"hash", h.Hash(),
"square width", len(h.DAH.RowRoots),
"data root", h.DAH.String(),
"finished (s)", time.Since(start),
)
return nil
}
func (w *worker) getHeader(ctx context.Context, height uint64) (*header.ExtendedHeader, error) {
if w.state.header != nil {
return w.state.header, nil
}
// TODO: get headers in batches
start := time.Now()
h, err := w.getter.GetByHeight(ctx, height)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Errorw("failed to get header from header store", "height", height,
"finished (s)", time.Since(start))
}
return nil, err
}
w.metrics.observeGetHeader(ctx, time.Since(start))
log.Debugw(
"got header from header store",
"height", h.Height(),
"hash", h.Hash(),
"square width", len(h.DAH.RowRoots),
"data root", h.DAH.String(),
"finished (s)", time.Since(start),
)
return h, nil
}
func (w *worker) setResult(curr uint64, err error) {
w.lock.Lock()
defer w.lock.Unlock()
if err != nil {
w.state.failed[curr]++
w.state.err = errors.Join(w.state.err, fmt.Errorf("height: %d, err: %w", curr, err))
}
w.state.curr = curr
}
func (w *worker) getState() workerState {
w.lock.Lock()
defer w.lock.Unlock()
return w.state
}