Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f86b4a6

Browse files
committedMar 6, 2020
refactor(lang): remove dependency on runtime
The lang package has been refactored to refer to the runtime using the `flux.Runtime` interface instead of direct references. This prevents it from have a dependency on the runtime and allows the structs to be used without incurring a dependency on the runtime. The `flux.Runtime` interface has been created to facilitate access to the runtime. It is a wrapper around the existing runtime struct that is in the `runtime` package. The `complete` package has also had its reference to the runtime removed. It was only used by one convenience method so that method has been removed.
1 parent ba4d75b commit f86b4a6

24 files changed

+152
-126
lines changed
 

‎cmd/flux/cmd/compile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
_ "github.com/influxdata/flux/builtin"
1010
"github.com/influxdata/flux/lang"
11+
"github.com/influxdata/flux/runtime"
1112
"github.com/spf13/cobra"
1213
)
1314

@@ -45,7 +46,7 @@ func compile(cmd *cobra.Command, args []string) error {
4546
Query: script,
4647
}
4748

48-
spec, err := c.Compile(context.Background())
49+
spec, err := c.Compile(context.Background(), runtime.Default)
4950
if err != nil {
5051
return err
5152
}

‎compiler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// Compiler produces a specification for the query.
1212
type Compiler interface {
1313
// Compile produces a specification for the query.
14-
Compile(ctx context.Context) (Program, error)
14+
Compile(ctx context.Context, runtime Runtime) (Program, error)
1515
CompilerType() CompilerType
1616
}
1717

‎complete/complete.go

-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"sort"
88

9-
"github.com/influxdata/flux/runtime"
109
"github.com/influxdata/flux/semantic"
1110
"github.com/influxdata/flux/values"
1211
)
@@ -100,11 +99,6 @@ func (c Completer) FunctionSuggestion(name string) (FunctionSuggestion, error) {
10099
return s, nil
101100
}
102101

103-
// DefaultCompleter creates a completer with builtin scope
104-
func DefaultCompleter() Completer {
105-
return NewCompleter(runtime.Prelude())
106-
}
107-
108102
func isFunction(v values.Value) bool {
109103
return v.Type().Nature() == semantic.Function
110104
}

‎internal/cmd/refactortests/cmd/refactortests.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func executeScript(pkg *ast.Package) (string, string, error) {
178178
}
179179

180180
ctx := flux.NewDefaultDependencies().Inject(context.Background())
181-
program, err := c.Compile(ctx)
181+
program, err := c.Compile(ctx, runtime.Default)
182182
if err != nil {
183183
fmt.Println(ast.Format(testPkg))
184184
return "", "", errors.Wrap(err, codes.Inherit, "error during compilation, check your script and retry")

‎internal/cmd/test_rewriter/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/influxdata/flux/lang"
1616
"github.com/influxdata/flux/memory"
1717
"github.com/influxdata/flux/parser"
18+
"github.com/influxdata/flux/runtime"
1819
"github.com/spf13/cobra"
1920
)
2021

@@ -78,7 +79,7 @@ func runQuery(query string) (flux.ResultIterator, error) {
7879
deps.Deps.FilesystemService = filesystem.SystemFS
7980

8081
ctx := deps.Inject(context.Background())
81-
program, err := c.Compile(ctx)
82+
program, err := c.Compile(ctx, runtime.Default)
8283
if err != nil {
8384
return nil, err
8485
}

‎internal/spec/build.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/influxdata/flux"
99
"github.com/influxdata/flux/interpreter"
10-
"github.com/influxdata/flux/runtime"
1110
"github.com/opentracing/opentracing-go"
1211
)
1312

@@ -128,7 +127,7 @@ func FromTableObject(ctx context.Context, to *flux.TableObject, now time.Time) (
128127
// FromScript returns a spec from a script expressed as a raw string.
129128
// This is duplicate logic for what happens when a flux.Program runs.
130129
// This function is used in tests that compare flux.Specs (e.g. in planner tests).
131-
func FromScript(ctx context.Context, now time.Time, script string) (*flux.Spec, error) {
130+
func FromScript(ctx context.Context, runtime flux.Runtime, now time.Time, script string) (*flux.Spec, error) {
132131
s, _ := opentracing.StartSpanFromContext(ctx, "parse")
133132
astPkg, err := runtime.Parse(script)
134133
if err != nil {
@@ -137,7 +136,7 @@ func FromScript(ctx context.Context, now time.Time, script string) (*flux.Spec,
137136
s.Finish()
138137

139138
s, cctx := opentracing.StartSpanFromContext(ctx, "eval")
140-
sideEffects, scope, err := runtime.EvalAST(cctx, astPkg, runtime.SetNowOption(now))
139+
sideEffects, scope, err := runtime.Eval(cctx, astPkg, flux.SetNowOption(now))
141140
if err != nil {
142141
return nil, err
143142
}

‎internal/spec/build_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "github.com/influxdata/flux/builtin"
99
"github.com/influxdata/flux/dependencies/dependenciestest"
1010
"github.com/influxdata/flux/internal/spec"
11+
"github.com/influxdata/flux/runtime"
1112
)
1213

1314
func Benchmark_FromScript(b *testing.B) {
@@ -35,7 +36,7 @@ check |> yield(name: "mean")
3536
ctx := dependenciestest.Default().Inject(context.Background())
3637
b.ReportAllocs()
3738
for i := 0; i < b.N; i++ {
38-
if _, err := spec.FromScript(ctx, time.Now(), query); err != nil {
39+
if _, err := spec.FromScript(ctx, runtime.Default, time.Now(), query); err != nil {
3940
b.Fatal(err)
4041
}
4142
}

‎lang/compiler.go

+13-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/influxdata/flux/internal/spec"
1515
"github.com/influxdata/flux/memory"
1616
"github.com/influxdata/flux/plan"
17-
"github.com/influxdata/flux/runtime"
1817
"github.com/influxdata/flux/semantic"
1918
"github.com/influxdata/flux/values"
2019
"github.com/opentracing/opentracing-go"
@@ -97,20 +96,21 @@ func Verbose(v bool) CompileOption {
9796

9897
// Compile evaluates a Flux script producing a flux.Program.
9998
// now parameter must be non-zero, that is the default now time should be set before compiling.
100-
func Compile(q string, now time.Time, opts ...CompileOption) (*AstProgram, error) {
99+
func Compile(q string, runtime flux.Runtime, now time.Time, opts ...CompileOption) (*AstProgram, error) {
101100
astPkg, err := runtime.Parse(q)
102101
if err != nil {
103102
return nil, err
104103
}
105-
return CompileAST(astPkg, now, opts...), nil
104+
return CompileAST(astPkg, runtime, now, opts...), nil
106105
}
107106

108107
// CompileAST evaluates a Flux AST and produces a flux.Program.
109108
// now parameter must be non-zero, that is the default now time should be set before compiling.
110-
func CompileAST(astPkg *ast.Package, now time.Time, opts ...CompileOption) *AstProgram {
109+
func CompileAST(astPkg *ast.Package, runtime flux.Runtime, now time.Time, opts ...CompileOption) *AstProgram {
111110
return &AstProgram{
112111
Program: &Program{
113-
opts: applyOptions(opts...),
112+
Runtime: runtime,
113+
opts: applyOptions(opts...),
114114
},
115115
Ast: astPkg,
116116
Now: now,
@@ -165,9 +165,9 @@ type FluxCompiler struct {
165165
Query string `json:"query"`
166166
}
167167

168-
func (c FluxCompiler) Compile(ctx context.Context) (flux.Program, error) {
168+
func (c FluxCompiler) Compile(ctx context.Context, runtime flux.Runtime) (flux.Program, error) {
169169
// Ignore context, it will be provided upon Program Start.
170-
return Compile(c.Query, c.Now, WithExtern(c.Extern))
170+
return Compile(c.Query, runtime, c.Now, WithExtern(c.Extern))
171171
}
172172

173173
func (c FluxCompiler) CompilerType() flux.CompilerType {
@@ -180,13 +180,13 @@ type ASTCompiler struct {
180180
Now time.Time
181181
}
182182

183-
func (c ASTCompiler) Compile(ctx context.Context) (flux.Program, error) {
183+
func (c ASTCompiler) Compile(ctx context.Context, runtime flux.Runtime) (flux.Program, error) {
184184
now := c.Now
185185
if now.IsZero() {
186186
now = time.Now()
187187
}
188188
// Ignore context, it will be provided upon Program Start.
189-
return CompileAST(c.AST, now), nil
189+
return CompileAST(c.AST, runtime, now), nil
190190
}
191191

192192
func (ASTCompiler) CompilerType() flux.CompilerType {
@@ -224,6 +224,7 @@ type LoggingProgram interface {
224224
type Program struct {
225225
Logger *zap.Logger
226226
PlanSpec *plan.Spec
227+
Runtime flux.Runtime
227228

228229
opts *compileOptions
229230
}
@@ -296,7 +297,7 @@ type AstProgram struct {
296297
Now time.Time
297298
}
298299

299-
func (p *AstProgram) getSpec(ctx context.Context, alloc *memory.Allocator) (*flux.Spec, values.Scope, error) {
300+
func (p *AstProgram) getSpec(ctx context.Context, runtime flux.Runtime, alloc *memory.Allocator) (*flux.Spec, values.Scope, error) {
300301
if p.opts == nil {
301302
p.opts = defaultOptions()
302303
}
@@ -314,7 +315,7 @@ func (p *AstProgram) getSpec(ctx context.Context, alloc *memory.Allocator) (*flu
314315
}
315316
ctx = deps.Inject(ctx)
316317
s, cctx := opentracing.StartSpanFromContext(ctx, "eval")
317-
sideEffects, scope, err := runtime.EvalAST(cctx, p.Ast, runtime.SetNowOption(p.Now))
318+
sideEffects, scope, err := runtime.Eval(cctx, p.Ast, flux.SetNowOption(p.Now))
318319
if err != nil {
319320
return nil, nil, err
320321
}
@@ -339,7 +340,7 @@ func (p *AstProgram) getSpec(ctx context.Context, alloc *memory.Allocator) (*flu
339340
}
340341

341342
func (p *AstProgram) Start(ctx context.Context, alloc *memory.Allocator) (flux.Query, error) {
342-
sp, scope, err := p.getSpec(ctx, alloc)
343+
sp, scope, err := p.getSpec(ctx, p.Runtime, alloc)
343344
if err != nil {
344345
return nil, err
345346
}
@@ -450,15 +451,3 @@ func getRules(plannerPkg values.Object, optionName string) ([]string, error) {
450451
})
451452
return rs, nil
452453
}
453-
454-
// WalkIR applies the function `f` to each operation in the compiled spec.
455-
// WARNING: this function evaluates the AST using an unlimited allocator.
456-
// In case of dynamic queries this could lead to unexpected memory usage.
457-
func WalkIR(ctx context.Context, astPkg *ast.Package, f func(o *flux.Operation) error) error {
458-
p := CompileAST(astPkg, time.Now())
459-
if sp, _, err := p.getSpec(ctx, new(memory.Allocator)); err != nil {
460-
return err
461-
} else {
462-
return sp.Walk(f)
463-
}
464-
}

‎lang/compiler_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestFluxCompiler(t *testing.T) {
133133
t.Errorf("compiler serialized/deserialized does not match: -want/+got:\n%v", diff)
134134
}
135135

136-
program, err := c.Compile(ctx)
136+
program, err := c.Compile(ctx, runtime.Default)
137137
if err != nil {
138138
if tc.err != "" {
139139
if !strings.Contains(err.Error(), tc.err) {
@@ -164,7 +164,7 @@ func TestFluxCompiler(t *testing.T) {
164164
}
165165

166166
func TestCompilationError(t *testing.T) {
167-
program, err := lang.Compile(`illegal query`, time.Unix(0, 0))
167+
program, err := lang.Compile(`illegal query`, runtime.Default, time.Unix(0, 0))
168168
if err != nil {
169169
// This shouldn't happen, has the script should be evaluated at program Start.
170170
t.Fatal(err)
@@ -293,7 +293,7 @@ csv.from(csv: "foo,bar") |> range(start: 2017-10-10T00:00:00Z)
293293
c.PrependFile(tc.file)
294294
}
295295

296-
program, err := c.Compile(context.Background())
296+
program, err := c.Compile(context.Background(), runtime.Default)
297297
if err != nil {
298298
t.Fatalf("failed to compile AST: %v", err)
299299
}
@@ -322,7 +322,7 @@ func TestCompileOptions(t *testing.T) {
322322

323323
opt := lang.WithLogPlanOpts(plan.OnlyLogicalRules(removeCount{}))
324324

325-
program, err := lang.Compile(src, now, opt)
325+
program, err := lang.Compile(src, runtime.Default, now, opt)
326326
if err != nil {
327327
t.Fatalf("failed to compile script: %v", err)
328328
}
@@ -654,7 +654,7 @@ option planner.disableLogicalRules = ["removeCountRule"]`},
654654
}
655655
}
656656

657-
program := lang.CompileAST(astPkg, nowFn())
657+
program := lang.CompileAST(astPkg, runtime.Default, nowFn())
658658
ctx := executetest.NewTestExecuteDependencies().Inject(context.Background())
659659
if _, err := program.Start(ctx, &memory.Allocator{}); err != nil {
660660
if tc.wantErr == "" {

‎lang/dependencies.go

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/influxdata/flux/memory"
7-
87
"go.uber.org/zap"
98
)
109

‎lang/query_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"github.com/influxdata/flux/execute/executetest"
1111
"github.com/influxdata/flux/lang"
1212
"github.com/influxdata/flux/memory"
13+
"github.com/influxdata/flux/runtime"
1314
)
1415

1516
func runQuery(script string) (flux.Query, error) {
16-
program, err := lang.Compile(script, time.Unix(0, 0))
17+
program, err := lang.Compile(script, runtime.Default, time.Unix(0, 0))
1718
if err != nil {
1819
return nil, err
1920
}

‎plan/logical_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/influxdata/flux/parser"
1515
"github.com/influxdata/flux/plan"
1616
"github.com/influxdata/flux/plan/plantest"
17+
"github.com/influxdata/flux/runtime"
1718
"github.com/influxdata/flux/semantic"
1819
"github.com/influxdata/flux/stdlib/influxdata/influxdb"
1920
"github.com/influxdata/flux/stdlib/kafka"
@@ -22,7 +23,7 @@ import (
2223
)
2324

2425
func compile(fluxText string, now time.Time) (*flux.Spec, error) {
25-
return spec.FromScript(dependenciestest.Default().Inject(context.Background()), now, fluxText)
26+
return spec.FromScript(dependenciestest.Default().Inject(context.Background()), runtime.Default, now, fluxText)
2627
}
2728

2829
func TestPlan_LogicalPlanFromSpec(t *testing.T) {

‎plan/rules_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/influxdata/flux/internal/spec"
1111
"github.com/influxdata/flux/plan"
1212
"github.com/influxdata/flux/plan/plantest"
13+
"github.com/influxdata/flux/runtime"
1314
"github.com/influxdata/flux/stdlib/influxdata/influxdb"
1415
)
1516

@@ -30,7 +31,7 @@ func TestRuleRegistration(t *testing.T) {
3031
plan.RegisterLogicalRules(&simpleRule)
3132

3233
now := time.Now().UTC()
33-
fluxSpec, err := spec.FromScript(dependenciestest.Default().Inject(context.Background()), now, `from(bucket: "telegraf") |> range(start: -5m)`)
34+
fluxSpec, err := spec.FromScript(dependenciestest.Default().Inject(context.Background()), runtime.Default, now, `from(bucket: "telegraf") |> range(start: -5m)`)
3435
if err != nil {
3536
t.Fatalf("could not compile very simple Flux query: %v", err)
3637
}

‎querytest/compile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/influxdata/flux"
1414
"github.com/influxdata/flux/dependencies/dependenciestest"
1515
"github.com/influxdata/flux/internal/spec"
16+
"github.com/influxdata/flux/runtime"
1617
"github.com/influxdata/flux/semantic/semantictest"
1718
"github.com/influxdata/flux/stdlib/universe"
1819
"github.com/influxdata/flux/values/valuestest"
@@ -38,7 +39,7 @@ func NewQueryTestHelper(t *testing.T, tc NewQueryTestCase) {
3839
t.Helper()
3940

4041
now := time.Now().UTC()
41-
got, err := spec.FromScript(dependenciestest.Default().Inject(context.Background()), now, tc.Raw)
42+
got, err := spec.FromScript(dependenciestest.Default().Inject(context.Background()), runtime.Default, now, tc.Raw)
4243
if (err != nil) != tc.WantErr {
4344
t.Errorf("error compiling spec error = %v, wantErr %v", err, tc.WantErr)
4445
return

‎querytest/execute.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"github.com/influxdata/flux"
88
"github.com/influxdata/flux/execute/executetest"
99
"github.com/influxdata/flux/memory"
10+
"github.com/influxdata/flux/runtime"
1011
)
1112

1213
type Querier struct{}
1314

1415
func (q *Querier) Query(ctx context.Context, w io.Writer, c flux.Compiler, d flux.Dialect) (int64, error) {
15-
program, err := c.Compile(ctx)
16+
program, err := c.Compile(ctx, runtime.Default)
1617
if err != nil {
1718
return 0, err
1819
}

‎repl/compiler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Compiler struct {
1616
Spec *flux.Spec `json:"spec"`
1717
}
1818

19-
func (c Compiler) Compile(ctx context.Context) (flux.Program, error) {
19+
func (c Compiler) Compile(ctx context.Context, runtime flux.Runtime) (flux.Program, error) {
2020
planner := plan.PlannerBuilder{}.Build()
2121
ps, err := planner.Plan(c.Spec)
2222
if err != nil {

‎repl/repl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (r *REPL) doQuery(ctx context.Context, spec *flux.Spec, deps flux.Dependenc
220220
Spec: spec,
221221
}
222222

223-
program, err := c.Compile(ctx)
223+
program, err := c.Compile(ctx, runtime.Default)
224224
if err != nil {
225225
return err
226226
}

0 commit comments

Comments
 (0)
Please sign in to comment.