-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
data_source.go
186 lines (160 loc) · 5.78 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
package ocrcommon
import (
"context"
"math/big"
"sync"
"time"
"github.com/pkg/errors"
"github.com/smartcontractkit/libocr/commontypes"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting/types"
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median"
"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. Additionally, it converts the result to an
// ocrtypes.Observation (*big.Int), as expected by the offchain reporting library.
type inMemoryDataSource struct {
pipelineRunner pipeline.Runner
jb job.Job
spec pipeline.Spec
lggr logger.Logger
current bridges.BridgeMetaData
mu sync.RWMutex
monitoringEndpoint commontypes.MonitoringEndpoint
}
// dataSource uses inMemoryDataSource and implements capturing the result to be stored in the DB
type dataSource struct {
inMemoryDataSource
runResults chan<- pipeline.Run
}
// dataSourceV2 implements dataSource with the proper Observe return type
type dataSourceV2 struct {
dataSource
}
func (ds *dataSourceV2) Observe(ctx context.Context) (*big.Int, error) {
return ds.dataSource.Observe(ctx)
}
func NewDataSourceV1(pr pipeline.Runner, jb job.Job, spec pipeline.Spec, lggr logger.Logger, runResults chan<- pipeline.Run, endpoint commontypes.MonitoringEndpoint) ocrtypes.DataSource {
return &dataSource{
inMemoryDataSource: inMemoryDataSource{
pipelineRunner: pr,
jb: jb,
spec: spec,
lggr: lggr,
monitoringEndpoint: endpoint,
},
runResults: runResults,
}
}
func NewDataSourceV2(pr pipeline.Runner, jb job.Job, spec pipeline.Spec, lggr logger.Logger, runResults chan<- pipeline.Run, endpoint commontypes.MonitoringEndpoint) median.DataSource {
return &dataSourceV2{
dataSource: dataSource{
inMemoryDataSource: inMemoryDataSource{
pipelineRunner: pr,
jb: jb,
spec: spec,
lggr: lggr,
monitoringEndpoint: endpoint,
},
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 _ ocrtypes.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) (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 pipeline.Run{}, 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)
collectEATelemetry(ds, &trrs, &finalResult)
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) (*big.Int, error) {
_, finalResult, err := ds.executeRun(ctx)
if err != nil {
return nil, err
}
return ds.parse(finalResult)
}
// Observe with saving to DB
func (ds *dataSource) Observe(ctx context.Context) (ocrtypes.Observation, error) {
run, finalResult, err := ds.inMemoryDataSource.executeRun(ctx)
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)
}