forked from apache/beam
/
sdf_invokers.go
301 lines (276 loc) · 9.02 KB
/
sdf_invokers.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
// 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 exec
import (
"github.com/apache/beam/sdks/go/pkg/beam/core/funcx"
"github.com/apache/beam/sdks/go/pkg/beam/core/sdf"
"github.com/apache/beam/sdks/go/pkg/beam/core/util/reflectx"
"github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
"reflect"
)
// This file contains invokers for SDF methods. These invokers are based off
// exec.invoker which is used for regular DoFns. Since exec.invoker is
// specialized for DoFns it cannot be used for SDF methods. Instead, these
// invokers pare down the functionality to only what is essential for
// executing SDF methods, including per-element optimizations.
//
// Each SDF method invoker in this file is specific to a certain method, but
// they are all used the same way. Create an invoker with new[Method]Invoker
// in the Up method of an exec.Unit, and then invoke it with Invoke. Finally,
// call Reset on it when the bundle ends in FinishBundle.
//
// These invokers are not thread-safe.
// cirInvoker is an invoker for CreateInitialRestriction.
type cirInvoker struct {
fn *funcx.Fn
args []interface{} // Cache to avoid allocating new slices per-element.
call func(elms *FullValue) (rest interface{})
}
func newCreateInitialRestrictionInvoker(fn *funcx.Fn) (*cirInvoker, error) {
n := &cirInvoker{
fn: fn,
args: make([]interface{}, len(fn.Param)),
}
if err := n.initCallFn(); err != nil {
return nil, errors.WithContext(err, "sdf CreateInitialRestriction invoker")
}
return n, nil
}
func (n *cirInvoker) initCallFn() error {
// Expects a signature of the form:
// (key?, value) restriction
// TODO(BEAM-9643): Link to full documentation.
switch fnT := n.fn.Fn.(type) {
case reflectx.Func1x1:
n.call = func(elms *FullValue) interface{} {
return fnT.Call1x1(elms.Elm)
}
case reflectx.Func2x1:
n.call = func(elms *FullValue) interface{} {
return fnT.Call2x1(elms.Elm, elms.Elm2)
}
default:
switch len(n.fn.Param) {
case 1:
n.call = func(elms *FullValue) interface{} {
n.args[0] = elms.Elm
return n.fn.Fn.Call(n.args)[0]
}
case 2:
n.call = func(elms *FullValue) interface{} {
n.args[0] = elms.Elm
n.args[1] = elms.Elm2
return n.fn.Fn.Call(n.args)[0]
}
default:
return errors.Errorf("CreateInitialRestriction fn %v has unexpected number of parameters: %v",
n.fn.Fn.Name(), len(n.fn.Param))
}
}
return nil
}
// Invoke calls CreateInitialRestriction with the given FullValue as the element
// and returns the resulting restriction.
func (n *cirInvoker) Invoke(elms *FullValue) (rest interface{}) {
return n.call(elms)
}
// Reset zeroes argument entries in the cached slice to allow values to be
// garbage collected after the bundle ends.
func (n *cirInvoker) Reset() {
for i := range n.args {
n.args[i] = nil
}
}
// srInvoker is an invoker for SplitRestriction.
type srInvoker struct {
fn *funcx.Fn
args []interface{} // Cache to avoid allocating new slices per-element.
call func(elms *FullValue, rest interface{}) (splits interface{})
}
func newSplitRestrictionInvoker(fn *funcx.Fn) (*srInvoker, error) {
n := &srInvoker{
fn: fn,
args: make([]interface{}, len(fn.Param)),
}
if err := n.initCallFn(); err != nil {
return nil, errors.WithContext(err, "sdf SplitRestriction invoker")
}
return n, nil
}
func (n *srInvoker) initCallFn() error {
// Expects a signature of the form:
// (key?, value, restriction) []restriction
// TODO(BEAM-9643): Link to full documentation.
switch fnT := n.fn.Fn.(type) {
case reflectx.Func2x1:
n.call = func(elms *FullValue, rest interface{}) interface{} {
return fnT.Call2x1(elms.Elm, rest)
}
case reflectx.Func3x1:
n.call = func(elms *FullValue, rest interface{}) interface{} {
return fnT.Call3x1(elms.Elm, elms.Elm2, rest)
}
default:
switch len(n.fn.Param) {
case 2:
n.call = func(elms *FullValue, rest interface{}) interface{} {
n.args[0] = elms.Elm
n.args[1] = rest
return n.fn.Fn.Call(n.args)[0]
}
case 3:
n.call = func(elms *FullValue, rest interface{}) interface{} {
n.args[0] = elms.Elm
n.args[1] = elms.Elm2
n.args[2] = rest
return n.fn.Fn.Call(n.args)[0]
}
default:
return errors.Errorf("SplitRestriction fn %v has unexpected number of parameters: %v",
n.fn.Fn.Name(), len(n.fn.Param))
}
}
return nil
}
// Invoke calls SplitRestriction given a FullValue containing an element and
// the associated restriction, and returns a slice of split restrictions.
func (n *srInvoker) Invoke(elms *FullValue, rest interface{}) (splits []interface{}) {
ret := n.call(elms, rest)
// Return value is an interface{}, but we need to convert it to a []interface{}.
val := reflect.ValueOf(ret)
s := make([]interface{}, 0, val.Len())
for i := 0; i < val.Len(); i++ {
s = append(s, val.Index(i).Interface())
}
return s
}
// Reset zeroes argument entries in the cached slice to allow values to be
// garbage collected after the bundle ends.
func (n *srInvoker) Reset() {
for i := range n.args {
n.args[i] = nil
}
}
// rsInvoker is an invoker for RestrictionSize.
type rsInvoker struct {
fn *funcx.Fn
args []interface{} // Cache to avoid allocating new slices per-element.
call func(elms *FullValue, rest interface{}) (size float64)
}
func newRestrictionSizeInvoker(fn *funcx.Fn) (*rsInvoker, error) {
n := &rsInvoker{
fn: fn,
args: make([]interface{}, len(fn.Param)),
}
if err := n.initCallFn(); err != nil {
return nil, errors.WithContext(err, "sdf RestrictionSize invoker")
}
return n, nil
}
func (n *rsInvoker) initCallFn() error {
// Expects a signature of the form:
// (key?, value, restriction) float64
// TODO(BEAM-9643): Link to full documentation.
switch fnT := n.fn.Fn.(type) {
case reflectx.Func2x1:
n.call = func(elms *FullValue, rest interface{}) float64 {
return fnT.Call2x1(elms.Elm, rest).(float64)
}
case reflectx.Func3x1:
n.call = func(elms *FullValue, rest interface{}) float64 {
return fnT.Call3x1(elms.Elm, elms.Elm2, rest).(float64)
}
default:
switch len(n.fn.Param) {
case 2:
n.call = func(elms *FullValue, rest interface{}) float64 {
n.args[0] = elms.Elm
n.args[1] = rest
return n.fn.Fn.Call(n.args)[0].(float64)
}
case 3:
n.call = func(elms *FullValue, rest interface{}) float64 {
n.args[0] = elms.Elm
n.args[1] = elms.Elm2
n.args[2] = rest
return n.fn.Fn.Call(n.args)[0].(float64)
}
default:
return errors.Errorf("RestrictionSize fn %v has unexpected number of parameters: %v",
n.fn.Fn.Name(), len(n.fn.Param))
}
}
return nil
}
// Invoke calls RestrictionSize given a FullValue containing an element and
// the associated restriction, and returns a size.
func (n *rsInvoker) Invoke(elms *FullValue, rest interface{}) (size float64) {
return n.call(elms, rest)
}
// Reset zeroes argument entries in the cached slice to allow values to be
// garbage collected after the bundle ends.
func (n *rsInvoker) Reset() {
for i := range n.args {
n.args[i] = nil
}
}
// ctInvoker is an invoker for CreateTracker.
type ctInvoker struct {
fn *funcx.Fn
args []interface{} // Cache to avoid allocating new slices per-element.
call func(rest interface{}) sdf.RTracker
}
func newCreateTrackerInvoker(fn *funcx.Fn) (*ctInvoker, error) {
n := &ctInvoker{
fn: fn,
args: make([]interface{}, len(fn.Param)),
}
if err := n.initCallFn(); err != nil {
return nil, errors.WithContext(err, "sdf CreateTracker invoker")
}
return n, nil
}
func (n *ctInvoker) initCallFn() error {
// Expects a signature of the form:
// (restriction) sdf.RTracker
// TODO(BEAM-9643): Link to full documentation.
switch fnT := n.fn.Fn.(type) {
case reflectx.Func1x1:
n.call = func(rest interface{}) sdf.RTracker {
return fnT.Call1x1(rest).(sdf.RTracker)
}
default:
if len(n.fn.Param) != 1 {
return errors.Errorf("CreateTracker fn %v has unexpected number of parameters: %v",
n.fn.Fn.Name(), len(n.fn.Param))
}
n.call = func(rest interface{}) sdf.RTracker {
n.args[0] = rest
return n.fn.Fn.Call(n.args)[0].(sdf.RTracker)
}
}
return nil
}
// Invoke calls CreateTracker given a restriction and returns an sdf.RTracker.
func (n *ctInvoker) Invoke(rest interface{}) sdf.RTracker {
return n.call(rest)
}
// Reset zeroes argument entries in the cached slice to allow values to be
// garbage collected after the bundle ends.
func (n *ctInvoker) Reset() {
for i := range n.args {
n.args[i] = nil
}
}