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"
|
2021-02-25 17:35:36 +08:00
|
|
|
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/trace"
|
2020-11-02 16:44:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Node interface {
|
|
|
|
Name() string
|
|
|
|
MaxQueueLength() int32
|
|
|
|
MaxParallelism() int32
|
2021-02-25 17:35:36 +08:00
|
|
|
Operate(ctx context.Context, in []Msg) ([]Msg, context.Context)
|
2020-11-09 16:27:11 +08:00
|
|
|
IsInputNode() bool
|
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
|
|
|
|
inputChannels []chan *MsgWithCtx
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
type MsgWithCtx struct {
|
|
|
|
ctx context.Context
|
|
|
|
msg Msg
|
|
|
|
}
|
|
|
|
|
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-03-23 01:49:50 +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 msgCtx context.Context
|
|
|
|
var res []Msg
|
|
|
|
var sp opentracing.Span
|
|
|
|
if !nodeCtx.node.IsInputNode() {
|
2021-03-23 01:49:50 +08:00
|
|
|
msgCtx = 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
|
|
|
|
res, msgCtx = n.Operate(msgCtx, inputs)
|
|
|
|
sp, msgCtx = trace.StartSpanFromContext(msgCtx)
|
|
|
|
sp.SetTag("node name", n.Name())
|
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-02-25 17:35:36 +08:00
|
|
|
go nodeCtx.downstream[i].ReceiveMsg(msgCtx, &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()
|
2021-02-25 17:35:36 +08:00
|
|
|
sp.Finish()
|
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-02-25 17:35:36 +08:00
|
|
|
func (nodeCtx *nodeCtx) ReceiveMsg(ctx context.Context, wg *sync.WaitGroup, msg Msg, inputChanIdx int) {
|
|
|
|
sp, ctx := trace.StartSpanFromContext(ctx)
|
|
|
|
defer sp.Finish()
|
|
|
|
nodeCtx.inputChannels[inputChanIdx] <- &MsgWithCtx{ctx: ctx, msg: msg}
|
2020-11-12 12:04:12 +08:00
|
|
|
//fmt.Println((*nodeCtx.node).Name(), "receive to input channel ", inputChanIdx)
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-11-02 16:44:54 +08:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
2021-03-23 01:49:50 +08:00
|
|
|
func (nodeCtx *nodeCtx) collectInputMessages(exitCtx context.Context) context.Context {
|
2021-02-25 17:35:36 +08:00
|
|
|
var opts []opentracing.StartSpanOption
|
|
|
|
|
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-02-25 17:35:36 +08:00
|
|
|
return nil
|
2021-03-23 01:49:50 +08:00
|
|
|
case msgWithCtx, ok := <-channel:
|
|
|
|
if !ok {
|
|
|
|
// TODO: add status
|
|
|
|
log.Println("input channel closed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nodeCtx.inputMessages[i] = msgWithCtx.msg
|
|
|
|
if msgWithCtx.ctx != nil {
|
|
|
|
sp, _ := trace.StartSpanFromContext(msgWithCtx.ctx)
|
|
|
|
opts = append(opts, opentracing.ChildOf(sp.Context()))
|
|
|
|
sp.Finish()
|
|
|
|
}
|
2020-11-12 12:04:12 +08:00
|
|
|
}
|
2021-02-25 17:35:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var sp opentracing.Span
|
|
|
|
if len(opts) != 0 {
|
|
|
|
sp, ctx = trace.StartSpanFromContext(context.Background(), opts...)
|
|
|
|
defer sp.Finish()
|
2020-11-02 16:44:54 +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
|
|
|
|
}
|
|
|
|
nodeCtx.inputMessages[i] = msg.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
|
|
|
}
|
2021-02-25 17:35:36 +08:00
|
|
|
return ctx
|
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
|
|
|
}
|