forked from apache/beam
/
dataflow.go
366 lines (312 loc) · 10.4 KB
/
dataflow.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
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
// 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 graphx
import (
"github.com/apache/beam/sdks/go/pkg/beam/core/graph/coder"
"github.com/apache/beam/sdks/go/pkg/beam/core/runtime/graphx/schema"
v1pb "github.com/apache/beam/sdks/go/pkg/beam/core/runtime/graphx/v1"
"github.com/apache/beam/sdks/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/go/pkg/beam/core/util/protox"
"github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
pipepb "github.com/apache/beam/sdks/go/pkg/beam/model/pipeline_v1"
)
// TODO(herohde) 7/17/2018: move CoderRef to dataflowlib once Dataflow
// sends back pure coder protos in the process bundle descriptors.
// CoderRef defines the (structured) Coder in serializable form. It is
// an artifact of the CloudObject encoding.
type CoderRef struct {
Type string `json:"@type,omitempty"`
Components []*CoderRef `json:"component_encodings,omitempty"`
IsWrapper bool `json:"is_wrapper,omitempty"`
IsPairLike bool `json:"is_pair_like,omitempty"`
IsStreamLike bool `json:"is_stream_like,omitempty"`
PipelineProtoCoderID string `json:"pipeline_proto_coder_id,omitempty"`
}
// Exported types are used for translation lookup.
const (
windowedValueType = "kind:windowed_value"
bytesType = "kind:bytes"
boolType = "kind:bool"
varIntType = "kind:varint"
doubleType = "kind:double"
streamType = "kind:stream"
pairType = "kind:pair"
lengthPrefixType = "kind:length_prefix"
rowType = "kind:row"
globalWindowType = "kind:global_window"
intervalWindowType = "kind:interval_window"
cogbklistType = "kind:cogbklist" // CoGBK representation. Not a coder.
stringType = "kind:string" // Not a classical Dataflow kind.
)
// WrapIterable adds an iterable (stream) coder for Dataflow side input.
func WrapIterable(c *CoderRef) *CoderRef {
return &CoderRef{Type: streamType, Components: []*CoderRef{c}, IsStreamLike: true}
}
// WrapWindowed adds a windowed coder for Dataflow collections.
func WrapWindowed(c *CoderRef, wc *coder.WindowCoder) (*CoderRef, error) {
w, err := encodeWindowCoder(wc)
if err != nil {
return nil, err
}
return &CoderRef{Type: windowedValueType, Components: []*CoderRef{c, w}, IsWrapper: true}, nil
}
// EncodeCoderRefs returns the encoded forms understood by the runner.
func EncodeCoderRefs(list []*coder.Coder) ([]*CoderRef, error) {
var refs []*CoderRef
for _, c := range list {
ref, err := EncodeCoderRef(c)
if err != nil {
return nil, err
}
refs = append(refs, ref)
}
return refs, nil
}
// EncodeCoderRef returns the encoded form understood by the runner.
func EncodeCoderRef(c *coder.Coder) (*CoderRef, error) {
switch c.Kind {
case coder.Custom:
ref, err := encodeCustomCoder(c.Custom)
if err != nil {
return nil, err
}
data, err := protox.EncodeBase64(ref)
if err != nil {
return nil, err
}
return &CoderRef{
Type: lengthPrefixType,
Components: []*CoderRef{{Type: data, PipelineProtoCoderID: c.Custom.ID}},
}, nil
case coder.KV:
if len(c.Components) != 2 {
return nil, errors.Errorf("bad KV: %v", c)
}
key, err := EncodeCoderRef(c.Components[0])
if err != nil {
return nil, err
}
value, err := EncodeCoderRef(c.Components[1])
if err != nil {
return nil, err
}
return &CoderRef{Type: pairType, Components: []*CoderRef{key, value}, IsPairLike: true}, nil
case coder.CoGBK:
if len(c.Components) < 2 {
return nil, errors.Errorf("bad CoGBK: %v", c)
}
refs, err := EncodeCoderRefs(c.Components)
if err != nil {
return nil, err
}
value := refs[1]
if len(c.Components) > 2 {
// TODO(BEAM-490): don't inject union coder for CoGBK.
union := &CoderRef{Type: cogbklistType, Components: refs[1:]}
value = &CoderRef{Type: lengthPrefixType, Components: []*CoderRef{union}}
}
stream := &CoderRef{Type: streamType, Components: []*CoderRef{value}, IsStreamLike: true}
return &CoderRef{Type: pairType, Components: []*CoderRef{refs[0], stream}, IsPairLike: true}, nil
case coder.WindowedValue:
if len(c.Components) != 1 || c.Window == nil {
return nil, errors.Errorf("bad windowed value: %v", c)
}
elm, err := EncodeCoderRef(c.Components[0])
if err != nil {
return nil, err
}
w, err := encodeWindowCoder(c.Window)
if err != nil {
return nil, err
}
return &CoderRef{Type: windowedValueType, Components: []*CoderRef{elm, w}, IsWrapper: true}, nil
case coder.Bytes:
return &CoderRef{Type: bytesType}, nil
case coder.Bool:
return &CoderRef{Type: boolType}, nil
case coder.VarInt:
return &CoderRef{Type: varIntType}, nil
case coder.Double:
return &CoderRef{Type: doubleType}, nil
case coder.String:
return &CoderRef{
Type: lengthPrefixType,
Components: []*CoderRef{{Type: stringType, PipelineProtoCoderID: c.ID}},
}, nil
case coder.Row:
schm, err := schema.FromType(c.T.Type())
if err != nil {
return nil, err
}
data, err := protox.EncodeBase64(schm)
if err != nil {
return nil, err
}
return &CoderRef{
Type: rowType,
Components: []*CoderRef{{Type: data}},
}, nil
default:
return nil, errors.Errorf("bad coder kind: %v", c.Kind)
}
}
// DecodeCoderRefs extracts usable coders from the encoded runner form.
func DecodeCoderRefs(list []*CoderRef) ([]*coder.Coder, error) {
var ret []*coder.Coder
for _, ref := range list {
c, err := DecodeCoderRef(ref)
if err != nil {
return nil, err
}
ret = append(ret, c)
}
return ret, nil
}
// DecodeCoderRef extracts a usable coder from the encoded runner form.
func DecodeCoderRef(c *CoderRef) (*coder.Coder, error) {
switch c.Type {
case bytesType:
return coder.NewBytes(), nil
case boolType:
return coder.NewBool(), nil
case varIntType:
return coder.NewVarInt(), nil
case doubleType:
return coder.NewDouble(), nil
case stringType:
return coder.NewString(), nil
case pairType:
if len(c.Components) != 2 {
return nil, errors.Errorf("bad pair: %+v", c)
}
key, err := DecodeCoderRef(c.Components[0])
if err != nil {
return nil, err
}
elm := c.Components[1]
kind := coder.KV
root := typex.KVType
isGBK := elm.Type == streamType
if isGBK {
elm = elm.Components[0]
kind = coder.CoGBK
root = typex.CoGBKType
// TODO(BEAM-490): If CoGBK with > 1 input, handle as special GBK. We expect
// it to be encoded as CoGBK<K,LP<Union<V,W,..>>. Remove this handling once
// CoGBK has a first-class representation.
if refs, ok := isCoGBKList(elm); ok {
values, err := DecodeCoderRefs(refs)
if err != nil {
return nil, err
}
t := typex.New(root, append([]typex.FullType{key.T}, coder.Types(values)...)...)
return &coder.Coder{Kind: kind, T: t, Components: append([]*coder.Coder{key}, values...)}, nil
}
}
value, err := DecodeCoderRef(elm)
if err != nil {
return nil, err
}
t := typex.New(root, key.T, value.T)
return &coder.Coder{Kind: kind, T: t, Components: []*coder.Coder{key, value}}, nil
case lengthPrefixType:
if len(c.Components) != 1 {
return nil, errors.Errorf("bad length prefix: %+v", c)
}
subC := c.Components[0]
switch subC.Type {
case stringType: // Needs special handling if wrapped by dataflow.
return coder.NewString(), nil
default:
return decodeDataflowCustomCoder(subC.Type)
}
case windowedValueType:
if len(c.Components) != 2 {
return nil, errors.Errorf("bad windowed value: %+v", c)
}
elm, err := DecodeCoderRef(c.Components[0])
if err != nil {
return nil, err
}
w, err := decodeWindowCoder(c.Components[1])
if err != nil {
return nil, err
}
t := typex.New(typex.WindowedValueType, elm.T)
return &coder.Coder{Kind: coder.WindowedValue, T: t, Components: []*coder.Coder{elm}, Window: w}, nil
case streamType:
return nil, errors.Errorf("stream must be pair value: %+v", c)
case rowType:
subC := c.Components[0]
schm := &pipepb.Schema{}
if err := protox.DecodeBase64(subC.Type, schm); err != nil {
return nil, err
}
t, err := schema.ToType(schm)
if err != nil {
return nil, err
}
return &coder.Coder{Kind: coder.Row, T: typex.New(t)}, nil
default:
return nil, errors.Errorf("custom coders must be length prefixed: %+v", c)
}
}
func decodeDataflowCustomCoder(payload string) (*coder.Coder, error) {
var ref v1pb.CustomCoder
if err := protox.DecodeBase64(payload, &ref); err != nil {
return nil, errors.Wrapf(err, "base64 decode for %v failed", payload)
}
custom, err := decodeCustomCoder(&ref)
if err != nil {
return nil, err
}
t := typex.New(custom.Type)
return &coder.Coder{Kind: coder.Custom, T: t, Custom: custom}, nil
}
func isCoGBKList(ref *CoderRef) ([]*CoderRef, bool) {
if ref.Type != lengthPrefixType {
return nil, false
}
ref2 := ref.Components[0]
if ref2.Type != cogbklistType {
return nil, false
}
return ref2.Components, true
}
// encodeWindowCoder translates the preprocessed representation of a Beam coder
// into the wire representation, capturing the underlying types used by
// the coder.
func encodeWindowCoder(w *coder.WindowCoder) (*CoderRef, error) {
switch w.Kind {
case coder.GlobalWindow:
return &CoderRef{Type: globalWindowType}, nil
case coder.IntervalWindow:
return &CoderRef{Type: intervalWindowType}, nil
default:
return nil, errors.Errorf("bad window kind: %v", w.Kind)
}
}
// decodeWindowCoder receives the wire representation of a Beam coder, extracting
// the preprocessed representation, expanding all types used by the coder.
func decodeWindowCoder(w *CoderRef) (*coder.WindowCoder, error) {
switch w.Type {
case globalWindowType:
return coder.NewGlobalWindow(), nil
case intervalWindowType:
return coder.NewIntervalWindow(), nil
default:
return nil, errors.Errorf("bad window: %v", w.Type)
}
}