forked from apache/beam
/
direct.go
286 lines (229 loc) · 7.04 KB
/
direct.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
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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package direct contains the direct runner for running single-bundle
// pipelines in the current process. Useful for testing.
package direct
import (
"context"
"fmt"
"path"
"github.com/apache/beam/sdks/go/pkg/beam"
"github.com/apache/beam/sdks/go/pkg/beam/core/graph"
"github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
"github.com/apache/beam/sdks/go/pkg/beam/core/runtime/exec"
"github.com/apache/beam/sdks/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/go/pkg/beam/log"
)
func init() {
beam.RegisterRunner("direct", Execute)
}
// Execute runs the pipeline in-process.
func Execute(ctx context.Context, p *beam.Pipeline) error {
log.Info(ctx, "Pipeline:")
log.Info(ctx, p)
edges, _, err := p.Build()
if err != nil {
return fmt.Errorf("invalid pipeline: %v", err)
}
plan, err := Compile(edges)
if err != nil {
return fmt.Errorf("translation failed: %v", err)
}
log.Info(ctx, plan)
if err = plan.Execute(ctx, "", exec.DataContext{}); err != nil {
plan.Down(ctx) // ignore any teardown errors
return err
}
if err = plan.Down(ctx); err != nil {
return err
}
metrics.DumpToLog(ctx)
return nil
}
// Compile translates a pipeline to a multi-bundle execution plan.
func Compile(edges []*graph.MultiEdge) (*exec.Plan, error) {
// (1) Preprocess graph structure to allow insertion of Multiplex,
// Flatten and Discard.
prev := make(map[int]int) // nodeID -> #incoming
succ := make(map[int][]linkID) // nodeID -> []linkID
edgeMap := make(map[int]*graph.MultiEdge)
for _, edge := range edges {
edgeMap[edge.ID()] = edge
for i, in := range edge.Input {
from := in.From.ID()
succ[from] = append(succ[from], linkID{edge.ID(), i})
}
for _, out := range edge.Output {
to := out.To.ID()
prev[to]++
}
}
// (2) Constructs the plan units recursively.
b := &builder{
prev: prev,
succ: succ,
edges: edgeMap,
nodes: make(map[int]exec.Node),
links: make(map[linkID]exec.Node),
idgen: &exec.GenID{},
}
var roots []exec.Unit
for _, edge := range edges {
switch edge.Op {
case graph.Impulse:
out, err := b.makeNode(edge.Output[0].To.ID())
if err != nil {
return nil, err
}
u := &Impulse{UID: b.idgen.New(), Value: edge.Value, Out: out}
roots = append(roots, u)
default:
// skip non-roots
}
}
return exec.NewPlan("plan", append(roots, b.units...))
}
// linkID represents an incoming data link to an Edge.
type linkID struct {
to int // graph.MultiEdge
input int // input index. If > 0, it's a side or CoGBK input.
}
// builder is the recursive builder for non-root execution nodes.
type builder struct {
prev map[int]int // nodeID -> #incoming
succ map[int][]linkID // nodeID -> []linkID
edges map[int]*graph.MultiEdge // edgeID -> Edge
nodes map[int]exec.Node // nodeID -> Node (cache)
links map[linkID]exec.Node // linkID -> Node (cache)
units []exec.Unit // result
idgen *exec.GenID
}
func (b *builder) makeNodes(out []*graph.Outbound) ([]exec.Node, error) {
var ret []exec.Node
for _, o := range out {
n, err := b.makeNode(o.To.ID())
if err != nil {
return nil, err
}
ret = append(ret, n)
}
return ret, nil
}
func (b *builder) makeNode(id int) (exec.Node, error) {
if n, ok := b.nodes[id]; ok {
return n, nil
}
list := b.succ[id]
var u exec.Node
switch len(list) {
case 0:
// Discard.
u = &exec.Discard{UID: b.idgen.New()}
case 1:
return b.makeLink(list[0])
default:
// Multiplex.
out, err := b.makeLinks(list)
if err != nil {
return nil, err
}
u = &exec.Multiplex{UID: b.idgen.New(), Out: out}
}
if count := b.prev[id]; count > 1 {
// Guard node with Flatten, if needed.
b.units = append(b.units, u)
u = &exec.Flatten{UID: b.idgen.New(), N: count, Out: u}
}
b.nodes[id] = u
b.units = append(b.units, u)
return u, nil
}
func (b *builder) makeLinks(ids []linkID) ([]exec.Node, error) {
var ret []exec.Node
for _, id := range ids {
n, err := b.makeLink(id)
if err != nil {
return nil, err
}
ret = append(ret, n)
}
return ret, nil
}
func (b *builder) makeLink(id linkID) (exec.Node, error) {
if n, ok := b.links[id]; ok {
return n, nil
}
// Process all incoming links for the edge and cache them. It thus doesn't matter
// which exact link triggers the Node generation. The link caching is only needed
// to process ParDo side inputs and CoGBK.
edge := b.edges[id.to]
out, err := b.makeNodes(edge.Output)
if err != nil {
return nil, err
}
var u exec.Node
switch edge.Op {
case graph.ParDo:
pardo := &exec.ParDo{UID: b.idgen.New(), Fn: edge.DoFn, Inbound: edge.Input, Out: out}
pardo.PID = path.Base(pardo.Fn.Name())
if len(edge.Input) == 1 {
u = pardo
break
}
// ParDo w/ side input. We need to insert buffering and wait. We also need to
// ensure that we return the correct link node.
b.units = append(b.units, pardo)
w := &wait{UID: b.idgen.New(), need: len(edge.Input) - 1, next: pardo}
b.units = append(b.units, w)
b.links[linkID{edge.ID(), 0}] = w
for i := 1; i < len(edge.Input); i++ {
n := &buffer{uid: b.idgen.New(), next: w.ID(), read: pardo.ID(), notify: w.notify}
pardo.Side = append(pardo.Side, n)
b.units = append(b.units, n)
b.links[linkID{edge.ID(), i}] = n
}
return b.links[id], nil
case graph.Combine:
usesKey := typex.IsKV(edge.Input[0].Type)
u = &exec.Combine{UID: b.idgen.New(), Fn: edge.CombineFn, UsesKey: usesKey, Out: out[0]}
case graph.CoGBK:
u = &CoGBK{UID: b.idgen.New(), Edge: edge, Out: out[0]}
b.units = append(b.units, u)
// CoGBK needs injection of each incoming index. If > 1 incoming,
// insert Flatten as well.
if len(edge.Input) > 1 {
u = &exec.Flatten{UID: b.idgen.New(), N: len(edge.Input), Out: u}
b.units = append(b.units, u)
}
for i := 0; i < len(edge.Input); i++ {
n := &Inject{UID: b.idgen.New(), N: i, Out: u}
b.units = append(b.units, n)
b.links[linkID{edge.ID(), i}] = n
}
return b.links[id], nil
case graph.Flatten:
u = &exec.Flatten{UID: b.idgen.New(), N: len(edge.Input), Out: out[0]}
for i := 0; i < len(edge.Input); i++ {
b.links[linkID{edge.ID(), i}] = u
}
case graph.WindowInto:
u = &exec.WindowInto{UID: b.idgen.New(), Fn: edge.WindowFn, Out: out[0]}
default:
return nil, fmt.Errorf("unexpected edge: %v", edge)
}
b.links[id] = u
b.units = append(b.units, u)
return u, nil
}