Repository navigation
Expand file tree
/
Copy pathexec.go
More file actions
576 lines (471 loc) · 14.1 KB
/
Copy pathexec.go
File metadata and controls
576 lines (471 loc) · 14.1 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package bytecode
import (
"context"
"fmt"
"sync"
"time"
"github.com/influxdata/flux"
bctypes "github.com/influxdata/flux/bytecode/types"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/internal/spec"
"github.com/influxdata/flux/metadata"
"github.com/influxdata/flux/plan"
"github.com/opentracing/opentracing-go"
"go.uber.org/zap"
"github.com/influxdata/flux/values/objects"
)
type stack struct {
arr []interface{}
}
func (s *stack) PanicIfNotEmpty() {
if len(s.arr) != 0 {
panic("bytecode execution stack was not empty")
}
}
func (s *stack) PushSideEffects(se []interpreter.SideEffect) {
s.arr = append(s.arr, se)
}
func (s *stack) PopSideEffects() []interpreter.SideEffect {
i := s.arr[len(s.arr)-1]
s.arr = s.arr[:len(s.arr)-1]
return i.([]interpreter.SideEffect)
}
func (s *stack) PushValue(val values.Value) {
s.arr = append(s.arr, val)
}
func (s *stack) PopValue() values.Value {
i := s.arr[len(s.arr)-1]
s.arr = s.arr[:len(s.arr)-1]
return i.(values.Value)
}
func (s *stack) PushQuery(q flux.Query) {
s.arr = append(s.arr, q)
}
func (s *stack) PopQuery() flux.Query {
i := s.arr[len(s.arr)-1]
s.arr = s.arr[:len(s.arr)-1]
return i.(flux.Query)
}
func (s *stack) PushInt(i int) {
s.arr = append(s.arr, i)
}
func (s *stack) PopInt() int {
i := s.arr[len(s.arr)-1]
s.arr = s.arr[:len(s.arr)-1]
return i.(int)
}
func (s *stack) PushScope(sc values.Scope) {
s.arr = append(s.arr, sc)
}
func (s *stack) PopScope() values.Scope {
i := s.arr[len(s.arr)-1]
s.arr = s.arr[:len(s.arr)-1]
return i.(values.Scope)
}
func (s *stack) Pop() {
s.arr = s.arr[:len(s.arr)-1]
}
// query implements the flux.Query interface.
type query struct {
results chan flux.Result
stats flux.Statistics
alloc *memory.Allocator
span opentracing.Span
cancel func()
err error
wg sync.WaitGroup
}
func (q *query) Results() <-chan flux.Result {
return q.results
}
func (q *query) Done() {
q.cancel()
q.wg.Wait()
q.stats.MaxAllocated = q.alloc.MaxAllocated()
q.stats.TotalAllocated = q.alloc.TotalAllocated()
if q.span != nil {
q.span.Finish()
q.span = nil
}
}
func (q *query) Cancel() {
q.cancel()
}
func (q *query) Err() error {
return q.err
}
func (q *query) Statistics() flux.Statistics {
return q.stats
}
func (q *query) ProfilerResults() (flux.ResultIterator, error) {
return nil, nil
}
func functionName(call *semantic.CallExpression) string {
switch callee := call.Callee.(type) {
case *semantic.IdentifierExpression:
return callee.Name
case *semantic.MemberExpression:
return callee.Property
default:
return "<anonymous function>"
}
}
func emptyObject() values.Object {
vsMap := make(map[string]values.Value)
return values.NewObjectWithValues(vsMap)
}
func objectFromRow(idx int, cr flux.ColReader) values.Object {
vsMap := make(map[string]values.Value, len(cr.Cols()))
for j, c := range cr.Cols() {
var v values.Value
switch c.Type {
case flux.TString:
if vs := cr.Strings(j); vs.IsValid(idx) {
v = values.New(vs.ValueString(idx))
} else {
v = values.NewNull(semantic.BasicString)
}
case flux.TInt:
if vs := cr.Ints(j); vs.IsValid(idx) {
v = values.New(vs.Value(idx))
} else {
v = values.NewNull(semantic.BasicInt)
}
case flux.TUInt:
if vs := cr.UInts(j); vs.IsValid(idx) {
v = values.New(vs.Value(idx))
} else {
v = values.NewNull(semantic.BasicUint)
}
case flux.TFloat:
if vs := cr.Floats(j); vs.IsValid(idx) {
v = values.New(vs.Value(idx))
} else {
v = values.NewNull(semantic.BasicFloat)
}
case flux.TBool:
if vs := cr.Bools(j); vs.IsValid(idx) {
v = values.New(vs.Value(idx))
} else {
v = values.NewNull(semantic.BasicBool)
}
case flux.TTime:
if vs := cr.Times(j); vs.IsValid(idx) {
v = values.New(values.Time(vs.Value(idx)))
} else {
v = values.NewNull(semantic.BasicTime)
}
default:
execute.PanicUnknownType(c.Type)
}
vsMap[c.Label] = v
}
return values.NewObjectWithValues(vsMap)
}
func Execute(ctx context.Context, alloc *memory.Allocator, now time.Time, code []bctypes.OpCode, logger *zap.Logger, scope values.Scope) (flux.Query, error) {
fmt.Printf("-> execution starting\n")
stack := &stack{}
loop:
for ip := 0; ip < len(code); {
b := code[ip]
switch b.In {
case bctypes.IN_NONE:
/* 0, not an instruction */
panic("IN_NONE")
case bctypes.IN_CALL:
callOp := b.Args.(interpreter.CallOp)
call := callOp.Call
pipe := callOp.Pipe
properties := callOp.Properties
var pipeValue values.Value
propertyValues := make([]values.Value, len(properties))
// Pipe evaluated last, popped first.
if pipe != nil {
pipeValue = stack.PopValue()
}
// Popping call args requires iterating in reverse.
for i := len(properties) - 1; i >= 0; i-- {
propertyValues[i] = stack.PopValue()
}
callee := stack.PopValue()
ft := callee.Type()
if ft.Nature() != semantic.Function {
return nil, errors.Newf(codes.Invalid, "cannot call function: %s: value is of type %v", call.Callee.Location(), callee.Type())
}
// Determine which argument matches the pipe argument.
var pipeArgument string
if pipe != nil {
n, err := ft.NumArguments()
if err != nil {
return nil, err
}
for i := 0; i < n; i++ {
arg, err := ft.Argument(i)
if err != nil {
return nil, err
}
if arg.Pipe() {
pipeArgument = string(arg.Name())
break
}
}
if pipeArgument == "" {
return nil, errors.New(codes.Invalid, "pipe parameter value provided to function with no pipe parameter defined")
}
}
argsObj, err := values.BuildObject(func(set values.ObjectSetter) error {
// Pipe evaluated last, popped first.
if pipe != nil {
set(pipeArgument, pipeValue)
}
// Popping call args requires iterating in reverse.
for i := len(properties) - 1; i >= 0; i-- {
p := properties[i]
if pipe != nil && p.Key.Key() == pipeArgument {
return errors.Newf(codes.Invalid, "pipe argument also specified as a keyword parameter: %q", p.Key.Key())
}
set(p.Key.Key(), propertyValues[i])
}
return nil
})
if err != nil {
return nil, err
}
f := callee.Function()
// Check if the function is an interpFunction and rebind it.
// This is needed so that any side effects produced when
// calling this function are bound to the correct interpreter.
// if af, ok := f.(function); ok {
// af.itrp = itrp
// f = af
// }
// Call the function. We attach source location information
// to this call so it can be available for the function if needed.
// We do not attach this source location information when evaluating
// arguments as this source location information is only
// for the currently called function.
fname := functionName(call)
// ctx = withStackEntry(ctx, fname, call.Location())
fmt.Printf("-- IN_CALL: %v\n", callee)
// Bit of a hack here. The call interface doesn't accept the scope because it uses the one
// set during the original interpretation pass. That pass is now a synthesis with a bogus scope, the real
// scope is in our state here. So bypass the abstraction and pass it in.
if sf, ok := f.(interpreter.SynthesizedFunction); ok {
// Abandoning the interface here. Need to pass a scope and return the new one.
value, retScope, err := sf.PrepCall(ctx, argsObj, scope)
if err != nil {
return nil, errors.Wrapf(err, codes.Inherit, "error calling function %q @%s", fname, call.Location())
}
// If the function is a synthesized function, then the return
// value of the call is the bytecode offset we need to call to.
// Preserve the scope.
stack.PushScope(scope)
// Replace with the nested scope computed during Call()
scope = retScope
// Push return location, the next instruction
stack.PushInt(ip + 1)
// Jump to targs. Skip the increment.
ip = int(value.Int())
fmt.Printf("-> this is a synthesized call, jumping to offset %v\n", ip)
continue loop
} else {
value, err := f.Call(ctx, argsObj)
if err != nil {
return nil, errors.Wrapf(err, codes.Inherit, "error calling function %q @%s", fname, call.Location())
}
// If not a synthesized call, then the above Call() invocation
// actually called the function. The return value comes back.
stack.PushValue(value)
}
case bctypes.IN_RET:
fmt.Printf("-- IN_RET\n")
// Return value is on the top of the stack.
retVal := stack.PopValue()
fmt.Printf("-> got retval %v\n", retVal)
// Return location is next.
ip = stack.PopInt()
fmt.Printf("-> return to %v\n", ip)
// Scope is last.
scope = stack.PopScope()
fmt.Printf("-> scope is %v\n", scope)
// Now put the return value back.
stack.PushValue(retVal)
// Continue because we have modified ip and do not need to advance
// it.
continue loop
case bctypes.IN_SCOPE_LOOKUP:
scopeLookup := b.Args.(interpreter.ScopeLookup)
name := scopeLookup.Name
fmt.Printf("-- IN_SCOPE_LOOKUP: %v\n", name)
value, ok := scope.Lookup(name)
if !ok {
return nil, errors.Newf(codes.Invalid, "undefined identifier %q", name)
}
stack.PushValue(value)
case bctypes.IN_SCOPE_SET:
scopeSet := b.Args.(interpreter.ScopeSet)
name := scopeSet.Name
fmt.Printf("-- IN_SCOPE_SET: %v\n", name)
value := stack.PopValue()
scope.Set(name, value)
case bctypes.IN_POP:
fmt.Printf("-- IN_POP\n")
stack.Pop()
case bctypes.IN_CONS_SIDE_EFFECTS:
fmt.Printf("-- IN_CONS_SIDE_EFFECTS\n")
stack.PushSideEffects(make([]interpreter.SideEffect, 0))
case bctypes.IN_LOAD_VALUE:
loadValue := b.Args.(interpreter.LoadValue)
value := loadValue.Value
fmt.Printf("-- IN_LOAD_VALUE: %v\n", value)
stack.PushValue(value)
case bctypes.IN_APPEND_SIDE_EFFECT:
fmt.Printf("-- IN_APPEND_SIDE_EFFECT\n")
// Semanic node comes from the instruction arguments
appendSideEffect := b.Args.(interpreter.AppendSideEffect)
node := appendSideEffect.Node
// Value comes from the stack. The side effects to add to is one deeper.
value := stack.PopValue()
sideEffects := stack.PopSideEffects()
sideEffects = append(sideEffects, interpreter.SideEffect{Node: node, Value: value})
// Result on stack.
stack.PushSideEffects(sideEffects)
case bctypes.IN_FIND_RECORD:
fmt.Printf("-- IN_FIND_RECORD\n")
// Take a query from the stack, drain it, push a record. Ripped from table_fns.go
query := stack.PopQuery()
var tv *objects.Table
var found bool
var val values.Object
var err error
const rowIdx int = 0
for res := range query.Results() {
if err := res.Tables().Do(func(tbl flux.Table) error {
defer tbl.Done()
if found {
// the result is filled, you can skip other tables
return nil
}
found = true
if found {
tv, err = objects.NewTable(tbl)
if err != nil {
return err
}
} else {
_ = tbl.Do(func(flux.ColReader) error { return nil })
}
return nil
}); err != nil {
return nil, err
}
}
if tv == nil {
val = emptyObject()
} else {
tbl := tv.Table()
err := tbl.Do(func(cr flux.ColReader) error {
if rowIdx < 0 || int(rowIdx) >= cr.Len() {
val = emptyObject()
return nil
}
val = objectFromRow(int(rowIdx), cr)
return nil
})
if err != nil {
return nil, err
}
}
stack.PushValue(val)
case bctypes.IN_EXECUTE_FLUX:
fmt.Printf("-- IN_EXECUTE_FLUX\n")
sideEffects := stack.PopSideEffects()
// Producing flux spec: side effects -> *flux.Spec
var sp *flux.Spec
var err error
sp, err = spec.FromEvaluation(ctx, sideEffects, now)
if err != nil {
return nil, errors.Wrap(err, codes.Inherit, "error in query specification while starting program")
}
// Planning: *flux.Spec -> plan.Spec
var ps *plan.Spec
// TODO: need to get plan options from the execution dependencies.
// These are set during evaluation and need to be retrieved along
// with now.
pb := plan.PlannerBuilder{}
// planOptions := nil //o.planOptions
// lopts := planOptions.logical
// popts := planOptions.physical
// pb.AddLogicalOptions(lopts...)
// pb.AddPhysicalOptions(popts...)
ps, err = pb.Build().Plan(ctx, sp)
if err != nil {
return nil, errors.Wrap(err, codes.Inherit, "error in building plan while starting program")
}
ctx, cancel := context.WithCancel(ctx)
// This span gets closed by the query when it is done.
s, cctx := opentracing.StartSpanFromContext(ctx, "execute")
results := make(chan flux.Result)
q := &query{
results: results,
alloc: alloc,
span: s,
cancel: cancel,
stats: flux.Statistics{
Metadata: make(metadata.Metadata),
},
}
if execute.HaveExecutionDependencies(ctx) {
deps := execute.GetExecutionDependencies(ctx)
q.stats.Metadata.AddAll(deps.Metadata)
}
q.stats.Metadata.Add("flux/query-plan",
fmt.Sprintf("%v", plan.Formatted(ps, plan.WithDetails())))
// Execute
e := execute.NewExecutor(logger)
resultMap, md, err := e.Execute(cctx, ps, q.alloc)
if err != nil {
s.Finish()
return nil, err
}
// There was no error so send the results downstream.
q.wg.Add(1)
go processResults(cctx, q, resultMap)
// Begin reading from the metadata channel.
q.wg.Add(1)
go readMetadata(q, md)
stack.PushQuery(q)
case bctypes.IN_STOP:
fmt.Printf("-- IN_STOP\n")
break loop
}
ip = ip + 1
}
query := stack.PopQuery()
stack.PanicIfNotEmpty()
return query, nil
}
func processResults(ctx context.Context, q *query, resultMap map[string]flux.Result) {
defer q.wg.Done()
defer close(q.results)
for _, res := range resultMap {
select {
case q.results <- res:
case <-ctx.Done():
q.err = ctx.Err()
return
}
}
}
func readMetadata(q *query, metaCh <-chan metadata.Metadata) {
defer q.wg.Done()
for md := range metaCh {
q.stats.Metadata.AddAll(md)
}
}