-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
data_source.go
223 lines (192 loc) · 6.93 KB
/
data_source.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
package ocrcommon
import (
"context"
"math/big"
"sync"
"time"
"github.com/pkg/errors"
ocr1types "github.com/smartcontractkit/libocr/offchainreporting/types"
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median"
ocr2types "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/chainlink/v2/core/bridges"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// inMemoryDataSource is an abstraction over the process of initiating a pipeline run
// and returning the result
type inMemoryDataSource struct {
pipelineRunner pipeline.Runner
jb job.Job
spec pipeline.Spec
lggr logger.Logger
current bridges.BridgeMetaData
mu sync.RWMutex
chEnhancedTelemetry chan<- EnhancedTelemetryData
}
type dataSourceBase struct {
inMemoryDataSource
runResults chan<- *pipeline.Run
}
// dataSource implements dataSourceBase with the proper Observe return type for ocr1
type dataSource struct {
dataSourceBase
}
// dataSourceV2 implements dataSourceBase with the proper Observe return type for ocr2
type dataSourceV2 struct {
dataSourceBase
}
// ObservationTimestamp abstracts ocr2types.ReportTimestamp and ocr1types.ReportTimestamp
type ObservationTimestamp struct {
Round uint8
Epoch uint32
ConfigDigest string
}
func NewDataSourceV1(pr pipeline.Runner, jb job.Job, spec pipeline.Spec, lggr logger.Logger, runResults chan<- *pipeline.Run, chEnhancedTelemetry chan EnhancedTelemetryData) ocr1types.DataSource {
return &dataSource{
dataSourceBase: dataSourceBase{
inMemoryDataSource: inMemoryDataSource{
pipelineRunner: pr,
jb: jb,
spec: spec,
lggr: lggr,
chEnhancedTelemetry: chEnhancedTelemetry,
},
runResults: runResults,
},
}
}
func NewDataSourceV2(pr pipeline.Runner, jb job.Job, spec pipeline.Spec, lggr logger.Logger, runResults chan<- *pipeline.Run, enhancedTelemChan chan EnhancedTelemetryData) median.DataSource {
return &dataSourceV2{
dataSourceBase: dataSourceBase{
inMemoryDataSource: inMemoryDataSource{
pipelineRunner: pr,
jb: jb,
spec: spec,
lggr: lggr,
chEnhancedTelemetry: enhancedTelemChan,
},
runResults: runResults,
},
}
}
func NewInMemoryDataSource(pr pipeline.Runner, jb job.Job, spec pipeline.Spec, lggr logger.Logger) median.DataSource {
return &inMemoryDataSource{
pipelineRunner: pr,
jb: jb,
spec: spec,
lggr: lggr,
}
}
var _ ocr1types.DataSource = (*dataSource)(nil)
func (ds *inMemoryDataSource) updateAnswer(a *big.Int) {
ds.mu.Lock()
defer ds.mu.Unlock()
ds.current = bridges.BridgeMetaData{
LatestAnswer: a,
UpdatedAt: big.NewInt(time.Now().Unix()),
}
}
func (ds *inMemoryDataSource) currentAnswer() (*big.Int, *big.Int) {
ds.mu.RLock()
defer ds.mu.RUnlock()
return ds.current.LatestAnswer, ds.current.UpdatedAt
}
// The context passed in here has a timeout of (ObservationTimeout + ObservationGracePeriod).
// Upon context cancellation, its expected that we return any usable values within ObservationGracePeriod.
func (ds *inMemoryDataSource) executeRun(ctx context.Context, timestamp ObservationTimestamp) (*pipeline.Run, pipeline.FinalResult, error) {
md, err := bridges.MarshalBridgeMetaData(ds.currentAnswer())
if err != nil {
ds.lggr.Warnw("unable to attach metadata for run", "err", err)
}
vars := pipeline.NewVarsFrom(map[string]interface{}{
"jb": map[string]interface{}{
"databaseID": ds.jb.ID,
"externalJobID": ds.jb.ExternalJobID,
"name": ds.jb.Name.ValueOrZero(),
},
"jobRun": map[string]interface{}{
"meta": md,
},
})
run, trrs, err := ds.pipelineRunner.ExecuteRun(ctx, ds.spec, vars, ds.lggr)
if err != nil {
return nil, pipeline.FinalResult{}, errors.Wrapf(err, "error executing run for spec ID %v", ds.spec.ID)
}
finalResult := trrs.FinalResult(ds.lggr)
promSetBridgeParseMetrics(ds, &trrs)
promSetFinalResultMetrics(ds, &finalResult)
if ShouldCollectEnhancedTelemetry(&ds.jb) {
EnqueueEnhancedTelem(ds.chEnhancedTelemetry, EnhancedTelemetryData{
TaskRunResults: trrs,
FinalResults: finalResult,
RepTimestamp: timestamp,
})
}
return run, finalResult, err
}
// parse uses the FinalResult into a big.Int and stores it in the bridge metadata
func (ds *inMemoryDataSource) parse(finalResult pipeline.FinalResult) (*big.Int, error) {
result, err := finalResult.SingularResult()
if err != nil {
return nil, errors.Wrapf(err, "error getting singular result for job ID %v", ds.spec.JobID)
}
if result.Error != nil {
return nil, result.Error
}
asDecimal, err := utils.ToDecimal(result.Value)
if err != nil {
return nil, errors.Wrap(err, "cannot convert observation to decimal")
}
ds.updateAnswer(asDecimal.BigInt())
return asDecimal.BigInt(), nil
}
// Observe without saving to DB
func (ds *inMemoryDataSource) Observe(ctx context.Context, timestamp ocr2types.ReportTimestamp) (*big.Int, error) {
_, finalResult, err := ds.executeRun(ctx, ObservationTimestamp{
Round: timestamp.Round,
Epoch: timestamp.Epoch,
ConfigDigest: timestamp.ConfigDigest.Hex(),
})
if err != nil {
return nil, err
}
return ds.parse(finalResult)
}
func (ds *dataSourceBase) observe(ctx context.Context, timestamp ObservationTimestamp) (*big.Int, error) {
run, finalResult, err := ds.inMemoryDataSource.executeRun(ctx, timestamp)
if err != nil {
return nil, err
}
// Do the database write in a non-blocking fashion
// so we can return the observation results immediately.
// This is helpful in the case of a blocking API call, where
// we reach the passed in context deadline and we want to
// immediately return any result we have and do not want to have
// a db write block that.
select {
case ds.runResults <- run:
default:
// If we're unable to enqueue a write, still return the value we have but warn.
ds.lggr.Warnf("unable to enqueue run save for job ID %d, buffer full", ds.inMemoryDataSource.spec.JobID)
return ds.inMemoryDataSource.parse(finalResult)
}
return ds.inMemoryDataSource.parse(finalResult)
}
// Observe with saving to DB, satisfies ocr1 interface
func (ds *dataSource) Observe(ctx context.Context, timestamp ocr1types.ReportTimestamp) (ocr1types.Observation, error) {
return ds.observe(ctx, ObservationTimestamp{
Round: timestamp.Round,
Epoch: timestamp.Epoch,
ConfigDigest: timestamp.ConfigDigest.Hex(),
})
}
// Observe with saving to DB, satisfies ocr2 interface
func (ds *dataSourceV2) Observe(ctx context.Context, timestamp ocr2types.ReportTimestamp) (*big.Int, error) {
return ds.observe(ctx, ObservationTimestamp{
Round: timestamp.Round,
Epoch: timestamp.Epoch,
ConfigDigest: timestamp.ConfigDigest.Hex(),
})
}