lang: funcs: structs: Core function struct modifications

These changes are needed for the lambdas implementation.

Co-authored-by: Samuel Gélineau <gelisam@gmail.com>
This commit is contained in:
James Shubin
2023-09-25 17:14:21 -04:00
parent b4a8d0d783
commit a1a23b66c8
7 changed files with 446 additions and 424 deletions

View File

@@ -23,30 +23,38 @@ import (
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/lang/types/full"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// CallFuncName is the unique name identifier for this function.
CallFuncName = "call"
// CallFuncArgNameFunction is the name for the edge which connects the
// input function to CallFunc.
CallFuncArgNameFunction = "fn"
)
// CallFunc is a function that takes in a function and all the args, and passes
// through the results of running the function call.
// CallFunc receives a function from upstream, but not the arguments. Instead,
// the Funcs which emit those arguments must be specified at construction time.
// The arguments are connected to the received FuncValues in such a way that
// CallFunc emits the result of applying the function to the arguments.
type CallFunc struct {
Type *types.Type // this is the type of the var's value that we hold
FuncType *types.Type
Edge string // name of the edge used (typically starts with: `call:`)
//Func interfaces.Func // this isn't actually used in the Stream :/
//Fn *types.FuncValue // pass in the actual function instead of Edge
Type *types.Type // the type of the result of applying the function
FuncType *types.Type // the type of the function
EdgeName string // name of the edge used
// Indexed specifies that args are accessed by index instead of name.
// This is currently unused.
Indexed bool
ArgVertices []interfaces.Func
init *interfaces.Init
last types.Value // last value received to use for diff
result types.Value // last calculated output
init *interfaces.Init
lastFuncValue *full.FuncValue // remember the last function value
// outputChan is an initially-nil channel from which we receive output
// lists from the subgraph. This channel is reset when the subgraph is
// recreated.
outputChan chan types.Value
}
// String returns a simple name for this function. This is needed so this struct
@@ -64,7 +72,7 @@ func (obj *CallFunc) Validate() error {
return fmt.Errorf("must specify a func type")
}
// TODO: maybe we can remove this if we use this for core functions...
if obj.Edge == "" {
if obj.EdgeName == "" {
return fmt.Errorf("must specify an edge name")
}
typ := obj.FuncType
@@ -72,6 +80,9 @@ func (obj *CallFunc) Validate() error {
if err := obj.Type.Cmp(typ.Out); err != nil {
return errwrap.Wrapf(err, "call expr type must match func out type")
}
if len(obj.ArgVertices) != len(typ.Ord) {
return fmt.Errorf("number of arg Funcs must match number of func args in the type")
}
return nil
}
@@ -79,25 +90,8 @@ func (obj *CallFunc) Validate() error {
// Info returns some static info about itself.
func (obj *CallFunc) Info() *interfaces.Info {
var typ *types.Type
if obj.Type != nil { // don't panic if called speculatively
typ = &types.Type{
Kind: types.KindFunc, // function type
Map: make(map[string]*types.Type),
Ord: []string{},
Out: obj.Type, // this is the output type for the expression
}
sig := obj.FuncType
if obj.Edge != "" {
typ.Map[obj.Edge] = sig // we get a function in
typ.Ord = append(typ.Ord, obj.Edge)
}
// add any incoming args
for _, key := range sig.Ord { // sig.Out, not sig!
typ.Map[key] = sig.Map[key]
typ.Ord = append(typ.Ord, key)
}
if obj.Type != nil && obj.FuncType != nil { // don't panic if called speculatively
typ = types.NewType(fmt.Sprintf("func(%s %s) %s", obj.EdgeName, obj.FuncType, obj.Type))
}
return &interfaces.Info{
@@ -111,6 +105,7 @@ func (obj *CallFunc) Info() *interfaces.Info {
// Init runs some startup code for this composite function.
func (obj *CallFunc) Init(init *interfaces.Init) error {
obj.init = init
obj.lastFuncValue = nil
return nil
}
@@ -119,65 +114,119 @@ func (obj *CallFunc) Init(init *interfaces.Init) error {
// on the changing inputs to that value.
func (obj *CallFunc) Stream(ctx context.Context) error {
defer close(obj.init.Output) // the sender closes
obj.outputChan = nil
defer func() {
obj.init.Txn.Reverse()
}()
canReceiveMoreFuncValues := true
canReceiveMoreOutputValues := true
for {
select {
case input, ok := <-obj.init.Input:
if !ok {
return nil // can't output any more
}
//if err := input.Type().Cmp(obj.Info().Sig.Input); err != nil {
// return errwrap.Wrapf(err, "wrong function input")
//}
if obj.last != nil && input.Cmp(obj.last) == nil {
continue // value didn't change, skip it
}
obj.last = input // store for next
st := input.(*types.StructValue) // must be!
// get the function
fn, exists := st.Lookup(obj.Edge)
if !exists {
return fmt.Errorf("missing expected input argument `%s`", obj.Edge)
}
// get the arguments to call the function
args := []types.Value{}
typ := obj.FuncType
for ix, key := range typ.Ord { // sig!
if obj.Indexed {
key = fmt.Sprintf("%d", ix)
}
value, exists := st.Lookup(key)
// TODO: replace with:
//value, exists := st.Lookup(fmt.Sprintf("arg:%s", key))
if !exists {
return fmt.Errorf("missing expected input argument `%s`", key)
}
args = append(args, value)
}
// actually call it
result, err := fn.(*types.FuncValue).Call(args)
if err != nil {
return errwrap.Wrapf(err, "error calling function")
}
// skip sending an update...
if obj.result != nil && result.Cmp(obj.result) == nil {
continue // result didn't change
}
obj.result = result // store new result
case <-ctx.Done():
if !canReceiveMoreFuncValues && !canReceiveMoreOutputValues {
// break
return nil
}
select {
case obj.init.Output <- obj.result: // send
// pass
case input, ok := <-obj.init.Input:
if !ok {
obj.init.Input = nil // block looping back here
canReceiveMoreFuncValues = false
continue
}
value, exists := input.Struct()[obj.EdgeName]
if !exists {
return fmt.Errorf("programming error, can't find edge")
}
newFuncValue, ok := value.(*full.FuncValue)
if !ok {
return fmt.Errorf("programming error, can't convert to *FuncValue")
}
// It's important to have this compare step to avoid
// redundant graph replacements which slow things down,
// but also cause the engine to lock, which can preempt
// the process scheduler, which can cause duplicate or
// unnecessary re-sending of values here, which causes
// the whole process to repeat ad-nauseum.
if newFuncValue == obj.lastFuncValue {
continue
}
// If we have a new function, then we need to replace
// the subgraph with a new one that uses the new
// function.
obj.lastFuncValue = newFuncValue
if err := obj.replaceSubGraph(newFuncValue); err != nil {
return errwrap.Wrapf(err, "could not replace subgraph")
}
canReceiveMoreOutputValues = true
continue
case outputValue, ok := <-obj.outputChan:
// send the new output value downstream
if !ok {
obj.outputChan = nil
canReceiveMoreOutputValues = false
continue
}
// send to the output
select {
case obj.init.Output <- outputValue:
case <-ctx.Done():
return nil
}
case <-ctx.Done():
return nil
}
}
}
func (obj *CallFunc) replaceSubGraph(newFuncValue *full.FuncValue) error {
// Create a subgraph which looks as follows. Most of the nodes are
// elided because we don't know which nodes the FuncValues will create.
//
// digraph {
// ArgVertices[0] -> ...
// ArgVertices[1] -> ...
// ArgVertices[2] -> ...
//
// outputFunc -> "subgraphOutput"
// }
// delete the old subgraph
if err := obj.init.Txn.Reverse(); err != nil {
return errwrap.Wrapf(err, "could not Reverse")
}
// create the new subgraph
// This passed in Txn has AddVertex, AddEdge, and possibly AddGraph
// methods called on it. Nothing else. It will _not_ call Commit or
// Reverse. It adds to the graph, and our Commit and Reverse operations
// are the ones that actually make the change.
outputFunc, err := newFuncValue.Call(obj.init.Txn, obj.ArgVertices)
if err != nil {
return errwrap.Wrapf(err, "could not call newFuncValue.Call()")
}
obj.outputChan = make(chan types.Value)
edgeName := ChannelBasedSinkFuncArgName
subgraphOutput := &ChannelBasedSinkFunc{
Name: "subgraphOutput",
Target: obj,
EdgeName: edgeName,
Chan: obj.outputChan,
Type: obj.Type,
}
edge := &interfaces.FuncEdge{Args: []string{edgeName}}
obj.init.Txn.AddVertex(subgraphOutput)
obj.init.Txn.AddEdge(outputFunc, subgraphOutput, edge)
return obj.init.Txn.Commit()
}