2021-04-19 13:42:47 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2020-11-02 16:44:54 +08:00
|
|
|
package flowgraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-07-15 14:51:55 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-02 16:44:54 +08:00
|
|
|
)
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
// Flow graph basic example: count `d = pow(a) + sqrt(a)`
|
|
|
|
// nodeA: receive input value a from input channel
|
|
|
|
// nodeB: count b = pow(a, 2)
|
|
|
|
// nodeC: count c = sqrt(a)
|
|
|
|
// nodeD: count d = b + c
|
2020-11-02 16:44:54 +08:00
|
|
|
|
|
|
|
type nodeA struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2021-07-15 14:51:55 +08:00
|
|
|
inputChan chan float64
|
|
|
|
a float64
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type nodeB struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2020-11-02 16:44:54 +08:00
|
|
|
b float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeC struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2020-11-02 16:44:54 +08:00
|
|
|
c float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeD struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2021-07-15 14:51:55 +08:00
|
|
|
d float64
|
|
|
|
outputChan chan float64
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
type numMsg struct {
|
2020-11-02 16:44:54 +08:00
|
|
|
num float64
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (m *numMsg) TimeTick() Timestamp {
|
|
|
|
return Timestamp(0)
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeA) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeA"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeA) Operate(in []Msg) []Msg {
|
|
|
|
// ignore `in` because nodeA doesn't have any upstream node.
|
|
|
|
a := <-n.inputChan
|
|
|
|
var res Msg = &numMsg{
|
|
|
|
num: a,
|
|
|
|
}
|
|
|
|
return []Msg{res, res}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeB) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeB"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeB) Operate(in []Msg) []Msg {
|
|
|
|
if len(in) != 1 {
|
|
|
|
panic("illegal in")
|
|
|
|
}
|
|
|
|
a, ok := in[0].(*numMsg)
|
|
|
|
if !ok {
|
|
|
|
return []Msg{}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-07-15 14:51:55 +08:00
|
|
|
b := math.Pow(a.num, 2)
|
|
|
|
var res Msg = &numMsg{
|
|
|
|
num: b,
|
|
|
|
}
|
|
|
|
return []Msg{res}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeC) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeC"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeC) Operate(in []Msg) []Msg {
|
|
|
|
if len(in) != 1 {
|
|
|
|
panic("illegal in")
|
|
|
|
}
|
|
|
|
a, ok := in[0].(*numMsg)
|
|
|
|
if !ok {
|
|
|
|
return []Msg{}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-07-15 14:51:55 +08:00
|
|
|
c := math.Sqrt(a.num)
|
|
|
|
var res Msg = &numMsg{
|
|
|
|
num: c,
|
|
|
|
}
|
|
|
|
return []Msg{res}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeD) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeD"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeD) Operate(in []Msg) []Msg {
|
|
|
|
if len(in) != 2 {
|
|
|
|
panic("illegal in")
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-07-15 14:51:55 +08:00
|
|
|
b, ok := in[0].(*numMsg)
|
2020-11-02 16:44:54 +08:00
|
|
|
if !ok {
|
2021-07-15 14:51:55 +08:00
|
|
|
return nil
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-07-15 14:51:55 +08:00
|
|
|
c, ok := in[1].(*numMsg)
|
2020-11-02 16:44:54 +08:00
|
|
|
if !ok {
|
2021-07-15 14:51:55 +08:00
|
|
|
return nil
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-07-15 14:51:55 +08:00
|
|
|
d := b.num + c.num
|
|
|
|
n.outputChan <- d
|
|
|
|
// return nil because nodeD doesn't have any downstream node.
|
|
|
|
return nil
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func createExampleFlowGraph() (*TimeTickedFlowGraph, chan float64, chan float64, context.CancelFunc) {
|
2020-11-18 17:32:52 +08:00
|
|
|
const MaxQueueLength = 1024
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
inputChan := make(chan float64, MaxQueueLength)
|
|
|
|
outputChan := make(chan float64, MaxQueueLength)
|
2020-11-12 12:04:12 +08:00
|
|
|
|
2020-11-02 16:44:54 +08:00
|
|
|
fg := NewTimeTickedFlowGraph(ctx)
|
|
|
|
|
|
|
|
var a Node = &nodeA{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
2021-07-15 14:51:55 +08:00
|
|
|
inputChan: inputChan,
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
var b Node = &nodeB{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var c Node = &nodeC{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var d Node = &nodeD{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
2021-07-15 14:51:55 +08:00
|
|
|
outputChan: outputChan,
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
fg.AddNode(a)
|
|
|
|
fg.AddNode(b)
|
|
|
|
fg.AddNode(c)
|
|
|
|
fg.AddNode(d)
|
2020-11-02 16:44:54 +08:00
|
|
|
|
|
|
|
var err = fg.SetEdges(a.Name(),
|
|
|
|
[]string{},
|
|
|
|
[]string{b.Name(), c.Name()},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("set edges failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fg.SetEdges(b.Name(),
|
|
|
|
[]string{a.Name()},
|
|
|
|
[]string{d.Name()},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("set edges failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fg.SetEdges(c.Name(),
|
|
|
|
[]string{a.Name()},
|
|
|
|
[]string{d.Name()},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("set edges failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fg.SetEdges(d.Name(),
|
|
|
|
[]string{b.Name(), c.Name()},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("set edges failed")
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
return fg, inputChan, outputChan, cancel
|
|
|
|
}
|
2020-11-02 16:44:54 +08:00
|
|
|
|
2021-09-07 09:39:47 +08:00
|
|
|
func TestTimeTickedFlowGraphAddNode(t *testing.T) {
|
|
|
|
const MaxQueueLength = 1024
|
|
|
|
inputChan := make(chan float64, MaxQueueLength)
|
|
|
|
|
|
|
|
fg := NewTimeTickedFlowGraph(context.TODO())
|
|
|
|
|
|
|
|
var a Node = &nodeA{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
inputChan: inputChan,
|
|
|
|
}
|
|
|
|
var b Node = &nodeB{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fg.AddNode(a)
|
|
|
|
assert.Equal(t, len(fg.nodeCtx), 1)
|
|
|
|
fg.AddNode(b)
|
|
|
|
assert.Equal(t, len(fg.nodeCtx), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeTickedFlowGraphSetEdges(t *testing.T) {
|
|
|
|
const MaxQueueLength = 1024
|
|
|
|
inputChan := make(chan float64, MaxQueueLength)
|
|
|
|
|
|
|
|
fg := NewTimeTickedFlowGraph(context.TODO())
|
|
|
|
|
|
|
|
var a Node = &nodeA{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
inputChan: inputChan,
|
|
|
|
}
|
|
|
|
var b Node = &nodeB{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var c Node = &nodeC{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fg.AddNode(a)
|
|
|
|
fg.AddNode(b)
|
|
|
|
fg.AddNode(c)
|
|
|
|
|
|
|
|
var err = fg.SetEdges(a.Name(),
|
|
|
|
[]string{b.Name()},
|
|
|
|
[]string{c.Name()},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = fg.SetEdges("Invalid",
|
|
|
|
[]string{b.Name()},
|
|
|
|
[]string{c.Name()},
|
|
|
|
)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = fg.SetEdges(a.Name(),
|
|
|
|
[]string{"Invalid"},
|
|
|
|
[]string{c.Name()},
|
|
|
|
)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = fg.SetEdges(a.Name(),
|
|
|
|
[]string{b.Name()},
|
|
|
|
[]string{"Invalid"},
|
|
|
|
)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeTickedFlowGraphStart(t *testing.T) {
|
2021-07-15 14:51:55 +08:00
|
|
|
fg, inputChan, outputChan, cancel := createExampleFlowGraph()
|
|
|
|
defer cancel()
|
2020-11-02 16:44:54 +08:00
|
|
|
go fg.Start()
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
// input
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
a := float64(rand.Int())
|
|
|
|
inputChan <- a
|
|
|
|
|
|
|
|
// output check
|
|
|
|
d := <-outputChan
|
|
|
|
res := math.Pow(a, 2) + math.Sqrt(a)
|
|
|
|
assert.Equal(t, d, res)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-09-07 09:39:47 +08:00
|
|
|
|
|
|
|
func TestTimeTickedFlowGraphClose(t *testing.T) {
|
|
|
|
fg, _, _, cancel := createExampleFlowGraph()
|
|
|
|
defer cancel()
|
|
|
|
fg.Close()
|
|
|
|
}
|