2020-11-02 08:44:54 +00:00
|
|
|
package flowgraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-05 02:52:50 +00:00
|
|
|
"log"
|
2020-11-02 08:44:54 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxQueueLength = 1024
|
|
|
|
|
|
|
|
type Node interface {
|
|
|
|
Name() string
|
|
|
|
MaxQueueLength() int32
|
|
|
|
MaxParallelism() int32
|
|
|
|
SetPipelineStates(states *flowGraphStates)
|
|
|
|
Operate(in []*Msg) []*Msg
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:30:12 +00:00
|
|
|
type BaseNode struct {
|
2020-11-02 08:44:54 +00:00
|
|
|
maxQueueLength int32
|
|
|
|
maxParallelism int32
|
|
|
|
graphStates *flowGraphStates
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeCtx struct {
|
|
|
|
node *Node
|
|
|
|
inputChannels []chan *Msg
|
|
|
|
inputMessages [][]*Msg
|
|
|
|
downstream []*nodeCtx
|
|
|
|
downstreamInputChanIdx map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) Start(ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if !nodeCtx.allUpstreamDone() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodeCtx.getMessagesFromChannel()
|
|
|
|
// inputs from inputsMessages for Operate
|
|
|
|
inputs := make([]*Msg, 0)
|
|
|
|
for i := 0; i < len(nodeCtx.inputMessages); i++ {
|
|
|
|
inputs = append(inputs, nodeCtx.inputMessages[i]...)
|
|
|
|
}
|
|
|
|
n := *nodeCtx.node
|
|
|
|
res := n.Operate(inputs)
|
|
|
|
wg := sync.WaitGroup{}
|
2020-11-05 02:52:50 +00:00
|
|
|
downstreamLength := len(nodeCtx.downstreamInputChanIdx)
|
|
|
|
if len(nodeCtx.downstream) < downstreamLength {
|
|
|
|
log.Fatal("nodeCtx.downstream length = ", len(nodeCtx.downstream))
|
|
|
|
}
|
|
|
|
if len(res) < downstreamLength {
|
|
|
|
log.Fatal("node result length = ", len(res))
|
|
|
|
}
|
|
|
|
for i := 0; i < downstreamLength; i++ {
|
2020-11-02 08:44:54 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go nodeCtx.downstream[i].ReceiveMsg(&wg, res[i], nodeCtx.downstreamInputChanIdx[(*nodeCtx.downstream[i].node).Name()])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) Close() {
|
|
|
|
for _, channel := range nodeCtx.inputChannels {
|
|
|
|
close(channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) ReceiveMsg(wg *sync.WaitGroup, msg *Msg, inputChanIdx int) {
|
|
|
|
nodeCtx.inputChannels[inputChanIdx] <- msg
|
2020-11-05 02:52:50 +00:00
|
|
|
// fmt.Println((*nodeCtx.node).Name(), "receive to input channel ", inputChanIdx)
|
|
|
|
|
2020-11-02 08:44:54 +00:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) allUpstreamDone() bool {
|
|
|
|
inputsNum := len(nodeCtx.inputChannels)
|
|
|
|
hasInputs := 0
|
|
|
|
for i := 0; i < inputsNum; i++ {
|
|
|
|
channel := nodeCtx.inputChannels[i]
|
|
|
|
if len(channel) > 0 {
|
|
|
|
hasInputs++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasInputs == inputsNum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) getMessagesFromChannel() {
|
|
|
|
inputsNum := len(nodeCtx.inputChannels)
|
|
|
|
nodeCtx.inputMessages = make([][]*Msg, inputsNum)
|
|
|
|
|
|
|
|
// init inputMessages,
|
|
|
|
// receive messages from inputChannels,
|
|
|
|
// and move them to inputMessages.
|
|
|
|
for i := 0; i < inputsNum; i++ {
|
|
|
|
nodeCtx.inputMessages[i] = make([]*Msg, 0)
|
|
|
|
channel := nodeCtx.inputChannels[i]
|
|
|
|
msg := <-channel
|
|
|
|
nodeCtx.inputMessages[i] = append(nodeCtx.inputMessages[i], msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:30:12 +00:00
|
|
|
func (node *BaseNode) MaxQueueLength() int32 {
|
2020-11-02 08:44:54 +00:00
|
|
|
return node.maxQueueLength
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:30:12 +00:00
|
|
|
func (node *BaseNode) MaxParallelism() int32 {
|
2020-11-02 08:44:54 +00:00
|
|
|
return node.maxParallelism
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:30:12 +00:00
|
|
|
func (node *BaseNode) SetMaxQueueLength(n int32) {
|
2020-11-02 08:44:54 +00:00
|
|
|
node.maxQueueLength = n
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:30:12 +00:00
|
|
|
func (node *BaseNode) SetMaxParallelism(n int32) {
|
2020-11-02 08:44:54 +00:00
|
|
|
node.maxParallelism = n
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:30:12 +00:00
|
|
|
func (node *BaseNode) SetPipelineStates(states *flowGraphStates) {
|
2020-11-02 08:44:54 +00:00
|
|
|
node.graphStates = states
|
|
|
|
}
|