2021-01-26 13:41:41 +08:00
|
|
|
package proxyservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-26 13:41:41 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
)
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
type TimeTick struct {
|
|
|
|
ttBarrier TimeTickBarrier
|
|
|
|
channels []msgstream.MsgStream
|
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
2021-01-26 13:41:41 +08:00
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (tt *TimeTick) Start() error {
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("start time tick ...")
|
2021-01-26 13:41:41 +08:00
|
|
|
tt.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer tt.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tt.ctx.Done():
|
|
|
|
log.Println("time tick loop was canceled by context!")
|
2021-01-26 16:41:10 +08:00
|
|
|
return
|
2021-01-26 13:41:41 +08:00
|
|
|
default:
|
|
|
|
current, err := tt.ttBarrier.GetTimeTick()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("GetTimeTick error: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
msgPack := msgstream.MsgPack{}
|
|
|
|
timeTickMsg := &msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
},
|
|
|
|
TimeTickMsg: internalpb2.TimeTickMsg{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_kTimeTick,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: current,
|
|
|
|
SourceID: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, timeTickMsg)
|
2021-02-25 17:35:36 +08:00
|
|
|
for _, msg := range msgPack.Msgs {
|
|
|
|
log.Println("msg type xxxxxxxxxxxxxxxxxxxxxxxx", msg.Type())
|
|
|
|
}
|
2021-02-03 20:04:29 +08:00
|
|
|
for _, channel := range tt.channels {
|
2021-02-24 09:48:17 +08:00
|
|
|
err = channel.Broadcast(tt.ctx, &msgPack)
|
2021-02-03 20:04:29 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("send time tick error: ", err)
|
|
|
|
}
|
2021-01-26 13:41:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-03 20:04:29 +08:00
|
|
|
for _, channel := range tt.channels {
|
|
|
|
channel.Start()
|
|
|
|
}
|
2021-01-26 13:41:41 +08:00
|
|
|
|
|
|
|
err := tt.ttBarrier.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (tt *TimeTick) Close() {
|
2021-02-03 20:04:29 +08:00
|
|
|
for _, channel := range tt.channels {
|
|
|
|
channel.Close()
|
|
|
|
}
|
2021-01-26 13:41:41 +08:00
|
|
|
tt.ttBarrier.Close()
|
|
|
|
tt.cancel()
|
|
|
|
tt.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func newTimeTick(ctx context.Context, ttBarrier TimeTickBarrier, channels ...msgstream.MsgStream) *TimeTick {
|
2021-01-26 13:41:41 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-03-05 16:52:45 +08:00
|
|
|
return &TimeTick{ctx: ctx1, cancel: cancel, ttBarrier: ttBarrier, channels: channels}
|
2021-01-26 13:41:41 +08:00
|
|
|
}
|