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"
|
2020-11-12 12:04:12 +08:00
|
|
|
"fmt"
|
2020-11-05 10:52:50 +08:00
|
|
|
"log"
|
2020-11-02 16:44:54 +08:00
|
|
|
"sync"
|
2021-02-25 15:08:50 +08:00
|
|
|
"time"
|
2020-11-02 16:44:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Node interface {
|
|
|
|
Name() string
|
|
|
|
MaxQueueLength() int32
|
|
|
|
MaxParallelism() int32
|
2021-03-25 14:41:46 +08:00
|
|
|
Operate(in []Msg) []Msg
|
2020-11-09 16:27:11 +08:00
|
|
|
IsInputNode() bool
|
2021-03-26 18:40:04 +08:00
|
|
|
Close()
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 19:30:12 +08:00
|
|
|
type BaseNode struct {
|
2020-11-02 16:44:54 +08:00
|
|
|
maxQueueLength int32
|
|
|
|
maxParallelism int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeCtx struct {
|
2021-02-25 17:35:36 +08:00
|
|
|
node Node
|
2021-03-25 14:41:46 +08:00
|
|
|
inputChannels []chan Msg
|
2021-02-25 17:35:36 +08:00
|
|
|
inputMessages []Msg
|
2020-11-02 16:44:54 +08:00
|
|
|
downstream []*nodeCtx
|
|
|
|
downstreamInputChanIdx map[string]int
|
2020-11-09 16:27:11 +08:00
|
|
|
|
|
|
|
NumActiveTasks int64
|
|
|
|
NumCompletedTasks int64
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) Start(ctx context.Context, wg *sync.WaitGroup) {
|
2021-02-25 17:35:36 +08:00
|
|
|
if nodeCtx.node.IsInputNode() {
|
2020-12-10 16:31:09 +08:00
|
|
|
// fmt.Println("start InputNode.inStream")
|
2021-02-25 17:35:36 +08:00
|
|
|
inStream, ok := nodeCtx.node.(*InputNode)
|
2020-11-09 16:27:11 +08:00
|
|
|
if !ok {
|
|
|
|
log.Fatal("Invalid inputNode")
|
|
|
|
}
|
2020-11-12 12:04:12 +08:00
|
|
|
(*inStream.inStream).Start()
|
2020-11-09 16:27:11 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:44:54 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
wg.Done()
|
2021-06-09 11:37:55 +08:00
|
|
|
fmt.Println(nodeCtx.node.Name(), "closed")
|
2020-11-02 16:44:54 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// inputs from inputsMessages for Operate
|
2021-02-25 17:35:36 +08:00
|
|
|
inputs := make([]Msg, 0)
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
var res []Msg
|
|
|
|
if !nodeCtx.node.IsInputNode() {
|
2021-03-25 14:41:46 +08:00
|
|
|
nodeCtx.collectInputMessages(ctx)
|
2020-11-09 16:27:11 +08:00
|
|
|
inputs = nodeCtx.inputMessages
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-02-25 17:35:36 +08:00
|
|
|
n := nodeCtx.node
|
2021-03-25 14:41:46 +08:00
|
|
|
res = n.Operate(inputs)
|
2020-11-12 12:04:12 +08:00
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
downstreamLength := len(nodeCtx.downstreamInputChanIdx)
|
|
|
|
if len(nodeCtx.downstream) < downstreamLength {
|
2020-11-12 12:04:12 +08:00
|
|
|
log.Println("nodeCtx.downstream length = ", len(nodeCtx.downstream))
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
if len(res) < downstreamLength {
|
2021-03-22 16:36:10 +08:00
|
|
|
// log.Println("node result length = ", len(res))
|
2020-11-12 12:04:12 +08:00
|
|
|
break
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
2020-11-12 12:04:12 +08:00
|
|
|
|
|
|
|
w := sync.WaitGroup{}
|
2020-11-05 10:52:50 +08:00
|
|
|
for i := 0; i < downstreamLength; i++ {
|
2020-11-12 12:04:12 +08:00
|
|
|
w.Add(1)
|
2021-03-25 14:41:46 +08:00
|
|
|
go nodeCtx.downstream[i].ReceiveMsg(&w, res[i], nodeCtx.downstreamInputChanIdx[nodeCtx.downstream[i].node.Name()])
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2020-11-12 12:04:12 +08:00
|
|
|
w.Wait()
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nodeCtx *nodeCtx) Close() {
|
|
|
|
for _, channel := range nodeCtx.inputChannels {
|
|
|
|
close(channel)
|
2020-11-12 12:04:12 +08:00
|
|
|
fmt.Println("close inputChannel")
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-03-26 18:40:04 +08:00
|
|
|
nodeCtx.node.Close()
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:41:46 +08:00
|
|
|
func (nodeCtx *nodeCtx) ReceiveMsg(wg *sync.WaitGroup, msg Msg, inputChanIdx int) {
|
2021-03-26 18:40:04 +08:00
|
|
|
defer wg.Done()
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-03-25 14:41:46 +08:00
|
|
|
nodeCtx.inputChannels[inputChanIdx] <- msg
|
2020-11-12 12:04:12 +08:00
|
|
|
//fmt.Println((*nodeCtx.node).Name(), "receive to input channel ", inputChanIdx)
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:41:46 +08:00
|
|
|
func (nodeCtx *nodeCtx) collectInputMessages(exitCtx context.Context) {
|
2020-11-02 16:44:54 +08:00
|
|
|
inputsNum := len(nodeCtx.inputChannels)
|
2021-02-25 17:35:36 +08:00
|
|
|
nodeCtx.inputMessages = make([]Msg, inputsNum)
|
2020-11-02 16:44:54 +08:00
|
|
|
|
|
|
|
// init inputMessages,
|
|
|
|
// receive messages from inputChannels,
|
|
|
|
// and move them to inputMessages.
|
|
|
|
for i := 0; i < inputsNum; i++ {
|
|
|
|
channel := nodeCtx.inputChannels[i]
|
2021-03-23 01:49:50 +08:00
|
|
|
select {
|
|
|
|
case <-exitCtx.Done():
|
2021-03-25 14:41:46 +08:00
|
|
|
return
|
|
|
|
case msg, ok := <-channel:
|
2021-03-23 01:49:50 +08:00
|
|
|
if !ok {
|
|
|
|
// TODO: add status
|
|
|
|
log.Println("input channel closed")
|
2021-03-25 14:41:46 +08:00
|
|
|
return
|
2021-03-23 01:49:50 +08:00
|
|
|
}
|
2021-03-25 14:41:46 +08:00
|
|
|
nodeCtx.inputMessages[i] = msg
|
2020-11-12 12:04:12 +08:00
|
|
|
}
|
2021-02-25 17:35:36 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 16:31:09 +08:00
|
|
|
// timeTick alignment check
|
|
|
|
if len(nodeCtx.inputMessages) > 1 {
|
2021-02-25 17:35:36 +08:00
|
|
|
t := nodeCtx.inputMessages[0].TimeTick()
|
2021-02-25 15:08:50 +08:00
|
|
|
latestTime := t
|
2020-12-10 16:31:09 +08:00
|
|
|
for i := 1; i < len(nodeCtx.inputMessages); i++ {
|
2021-02-25 17:35:36 +08:00
|
|
|
if t < nodeCtx.inputMessages[i].TimeTick() {
|
|
|
|
latestTime = nodeCtx.inputMessages[i].TimeTick()
|
2021-02-25 15:08:50 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-27 18:33:29 +08:00
|
|
|
|
2021-02-25 15:08:50 +08:00
|
|
|
// wait for time tick
|
2021-02-27 18:33:29 +08:00
|
|
|
sign := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < len(nodeCtx.inputMessages); i++ {
|
|
|
|
for nodeCtx.inputMessages[i].TimeTick() != latestTime {
|
|
|
|
fmt.Println("try to align timestamp, t1 =", latestTime, ", t2 =", nodeCtx.inputMessages[i].TimeTick())
|
|
|
|
channel := nodeCtx.inputChannels[i]
|
2021-03-23 01:49:50 +08:00
|
|
|
select {
|
|
|
|
case <-exitCtx.Done():
|
2021-02-27 18:33:29 +08:00
|
|
|
return
|
2021-03-23 01:49:50 +08:00
|
|
|
case msg, ok := <-channel:
|
|
|
|
if !ok {
|
|
|
|
log.Println("input channel closed")
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 14:41:46 +08:00
|
|
|
nodeCtx.inputMessages[i] = msg
|
2021-02-25 15:08:50 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
2021-02-27 18:33:29 +08:00
|
|
|
sign <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
panic("Fatal, misaligned time tick, please restart pulsar")
|
|
|
|
case <-sign:
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
2021-02-27 18:33:29 +08:00
|
|
|
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 19:30:12 +08:00
|
|
|
func (node *BaseNode) MaxQueueLength() int32 {
|
2020-11-02 16:44:54 +08:00
|
|
|
return node.maxQueueLength
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:30:12 +08:00
|
|
|
func (node *BaseNode) MaxParallelism() int32 {
|
2020-11-02 16:44:54 +08:00
|
|
|
return node.maxParallelism
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:30:12 +08:00
|
|
|
func (node *BaseNode) SetMaxQueueLength(n int32) {
|
2020-11-02 16:44:54 +08:00
|
|
|
node.maxQueueLength = n
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:30:12 +08:00
|
|
|
func (node *BaseNode) SetMaxParallelism(n int32) {
|
2020-11-02 16:44:54 +08:00
|
|
|
node.maxParallelism = n
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
func (node *BaseNode) IsInputNode() bool {
|
|
|
|
return false
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-03-26 18:40:04 +08:00
|
|
|
|
|
|
|
func (node *BaseNode) Close() {
|
|
|
|
//TODO
|
|
|
|
}
|