-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BCF-2630] Add pipeline runner wrapper (#11091)
- Loading branch information
1 parent
b655080
commit db64df9
Showing
8 changed files
with
247 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
core/services/ocr2/plugins/generic/pipeline_runner_adapter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package generic | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-relay/pkg/types" | ||
"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/store/models" | ||
) | ||
|
||
var _ types.PipelineRunnerService = (*PipelineRunnerAdapter)(nil) | ||
|
||
type pipelineRunner interface { | ||
ExecuteRun(ctx context.Context, spec pipeline.Spec, vars pipeline.Vars, l logger.Logger) (run *pipeline.Run, trrs pipeline.TaskRunResults, err error) | ||
} | ||
|
||
type PipelineRunnerAdapter struct { | ||
runner pipelineRunner | ||
job job.Job | ||
logger logger.Logger | ||
} | ||
|
||
func (p *PipelineRunnerAdapter) ExecuteRun(ctx context.Context, spec string, vars types.Vars, options types.Options) ([]types.TaskResult, error) { | ||
s := pipeline.Spec{ | ||
DotDagSource: spec, | ||
CreatedAt: time.Now(), | ||
MaxTaskDuration: models.Interval(options.MaxTaskDuration), | ||
JobID: p.job.ID, | ||
JobName: p.job.Name.ValueOrZero(), | ||
JobType: string(p.job.Type), | ||
} | ||
|
||
defaultVars := map[string]interface{}{ | ||
"jb": map[string]interface{}{ | ||
"databaseID": p.job.ID, | ||
"externalJobID": p.job.ExternalJobID, | ||
"name": p.job.Name.ValueOrZero(), | ||
}, | ||
} | ||
|
||
err := merge(defaultVars, vars.Vars) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
finalVars := pipeline.NewVarsFrom(defaultVars) | ||
_, trrs, err := p.runner.ExecuteRun(ctx, s, finalVars, p.logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
taskResults := make([]types.TaskResult, len(trrs)) | ||
for i, trr := range trrs { | ||
taskResults[i] = types.TaskResult{ | ||
ID: trr.ID.String(), | ||
Type: string(trr.Task.Type()), | ||
Value: trr.Result.Value, | ||
Error: trr.Result.Error, | ||
Index: int(trr.TaskRun.Index), | ||
} | ||
} | ||
return taskResults, nil | ||
} | ||
|
||
func NewPipelineRunnerAdapter(logger logger.Logger, job job.Job, runner pipelineRunner) *PipelineRunnerAdapter { | ||
return &PipelineRunnerAdapter{ | ||
logger: logger, | ||
job: job, | ||
runner: runner, | ||
} | ||
} | ||
|
||
// merge merges mapTwo into mapOne, modifying mapOne in the process. | ||
func merge(mapOne, mapTwo map[string]interface{}) error { | ||
for k, v := range mapTwo { | ||
// if `mapOne` doesn't have `k`, then nothing to do, just assign v to `mapOne`. | ||
if _, ok := mapOne[k]; !ok { | ||
mapOne[k] = v | ||
} else { | ||
vAsMap, vOK := v.(map[string]interface{}) | ||
mapOneVAsMap, moOK := mapOne[k].(map[string]interface{}) | ||
if vOK && moOK { | ||
merge(mapOneVAsMap, vAsMap) | ||
} else { | ||
mapOne[k] = v | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
144 changes: 144 additions & 0 deletions
144
core/services/ocr2/plugins/generic/pipeline_runner_adapter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package generic | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/shopspring/decimal" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/guregu/null.v4" | ||
|
||
"github.com/smartcontractkit/chainlink-relay/pkg/types" | ||
"github.com/smartcontractkit/chainlink/v2/core/bridges" | ||
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" | ||
_ "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" | ||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/job" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/keystore" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/pg" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline" | ||
"github.com/smartcontractkit/chainlink/v2/core/utils" | ||
) | ||
|
||
const spec = ` | ||
answer [type=sum values=<[ $(val), 2 ]>] | ||
answer; | ||
` | ||
|
||
func TestAdapter_Integration(t *testing.T) { | ||
logger := logger.TestLogger(t) | ||
cfg := configtest.NewTestGeneralConfig(t) | ||
url := cfg.Database().URL() | ||
db, err := pg.NewConnection(url.String(), cfg.Database().Dialect(), cfg.Database()) | ||
require.NoError(t, err) | ||
|
||
keystore := keystore.NewInMemory(db, utils.FastScryptParams, logger, cfg.Database()) | ||
pipelineORM := pipeline.NewORM(db, logger, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) | ||
bridgesORM := bridges.NewORM(db, logger, cfg.Database()) | ||
pr := pipeline.NewRunner( | ||
pipelineORM, | ||
bridgesORM, | ||
cfg.JobPipeline(), | ||
cfg.WebServer(), | ||
nil, | ||
keystore.Eth(), | ||
keystore.VRF(), | ||
logger, | ||
http.DefaultClient, | ||
http.DefaultClient, | ||
) | ||
pra := NewPipelineRunnerAdapter(logger, job.Job{}, pr) | ||
results, err := pra.ExecuteRun(context.Background(), spec, types.Vars{Vars: map[string]interface{}{"val": 1}}, types.Options{}) | ||
require.NoError(t, err) | ||
|
||
finalResult := results[0].Value.(decimal.Decimal) | ||
|
||
assert.True(t, decimal.NewFromInt(3).Equal(finalResult)) | ||
} | ||
|
||
func newMockPipelineRunner() *mockPipelineRunner { | ||
return &mockPipelineRunner{} | ||
} | ||
|
||
type mockPipelineRunner struct { | ||
results pipeline.TaskRunResults | ||
err error | ||
run *pipeline.Run | ||
spec pipeline.Spec | ||
vars pipeline.Vars | ||
} | ||
|
||
func (m *mockPipelineRunner) ExecuteRun(ctx context.Context, spec pipeline.Spec, vars pipeline.Vars, l logger.Logger) (*pipeline.Run, pipeline.TaskRunResults, error) { | ||
m.spec = spec | ||
m.vars = vars | ||
return m.run, m.results, m.err | ||
} | ||
|
||
func TestAdapter_AddsDefaultVars(t *testing.T) { | ||
logger := logger.TestLogger(t) | ||
mpr := newMockPipelineRunner() | ||
jobID, externalJobID, name := int32(100), uuid.New(), null.StringFrom("job-name") | ||
pra := NewPipelineRunnerAdapter(logger, job.Job{ID: jobID, ExternalJobID: externalJobID, Name: name}, mpr) | ||
|
||
_, err := pra.ExecuteRun(context.Background(), spec, types.Vars{}, types.Options{}) | ||
require.NoError(t, err) | ||
|
||
gotName, err := mpr.vars.Get("jb.name") | ||
require.NoError(t, err) | ||
assert.Equal(t, name.String, gotName) | ||
|
||
gotID, err := mpr.vars.Get("jb.databaseID") | ||
require.NoError(t, err) | ||
assert.Equal(t, jobID, gotID) | ||
|
||
gotExternalID, err := mpr.vars.Get("jb.externalJobID") | ||
require.NoError(t, err) | ||
assert.Equal(t, externalJobID, gotExternalID) | ||
} | ||
|
||
func TestPipelineRunnerAdapter_SetsVarsOnSpec(t *testing.T) { | ||
logger := logger.TestLogger(t) | ||
mpr := newMockPipelineRunner() | ||
jobID, externalJobID, name, jobType := int32(100), uuid.New(), null.StringFrom("job-name"), job.Type("generic") | ||
pra := NewPipelineRunnerAdapter(logger, job.Job{ID: jobID, ExternalJobID: externalJobID, Name: name, Type: jobType}, mpr) | ||
|
||
maxDuration := time.Duration(100 * time.Second) | ||
_, err := pra.ExecuteRun(context.Background(), spec, types.Vars{}, types.Options{MaxTaskDuration: maxDuration}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, jobID, mpr.spec.JobID) | ||
assert.Equal(t, name.ValueOrZero(), mpr.spec.JobName) | ||
assert.Equal(t, string(jobType), mpr.spec.JobType) | ||
assert.Equal(t, maxDuration, mpr.spec.MaxTaskDuration.Duration()) | ||
|
||
} | ||
|
||
func TestMerge(t *testing.T) { | ||
vars := map[string]interface{}{ | ||
"jb": map[string]interface{}{ | ||
"databaseID": "some-job-id", | ||
}, | ||
} | ||
addedVars := map[string]interface{}{ | ||
"jb": map[string]interface{}{ | ||
"some-other-var": "foo", | ||
}, | ||
"val": 0, | ||
} | ||
|
||
err := merge(vars, addedVars) | ||
require.NoError(t, err) | ||
|
||
assert.True(t, reflect.DeepEqual(vars, map[string]interface{}{ | ||
"jb": map[string]interface{}{ | ||
"databaseID": "some-job-id", | ||
"some-other-var": "foo", | ||
}, | ||
"val": 0, | ||
}), vars) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters