youngoli commented on a change in pull request #11257: [BEAM-9642] Create 
runtime invokers for SDF methods.
URL: https://github.com/apache/beam/pull/11257#discussion_r401966131
 
 

 ##########
 File path: sdks/go/pkg/beam/core/runtime/exec/sdf_invokers.go
 ##########
 @@ -0,0 +1,302 @@
+// 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{}) (split 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{}) (split 
[]interface{}) {
 
 Review comment:
   Agreed, "splits" makes more sense.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to