2021-01-15 09:09:41 +00:00
|
|
|
package timesync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
ms "github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-16 07:06:19 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-01-18 11:32:08 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-01-15 09:09:41 +00:00
|
|
|
)
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
type MsgProducer struct {
|
|
|
|
ttBarrier TimeTickBarrier
|
2021-01-15 09:09:41 +00:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
watchers []TimeTickWatcher
|
2021-01-15 09:09:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
func NewTimeSyncMsgProducer(ctx context.Context, ttBarrier TimeTickBarrier, watchers ...TimeTickWatcher) (*MsgProducer, error) {
|
|
|
|
childCtx, cancelFunc := context.WithCancel(ctx)
|
|
|
|
return &MsgProducer{
|
|
|
|
ctx: childCtx,
|
|
|
|
cancel: cancelFunc,
|
|
|
|
ttBarrier: ttBarrier,
|
|
|
|
watchers: watchers,
|
|
|
|
}, nil
|
2021-01-15 09:09:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
func (producer *MsgProducer) broadcastMsg() {
|
2021-01-15 09:09:41 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-01-19 04:10:49 +00:00
|
|
|
case <-producer.ctx.Done():
|
|
|
|
log.Printf("broadcast context done, exit")
|
2021-01-15 09:09:41 +00:00
|
|
|
default:
|
2021-01-19 04:10:49 +00:00
|
|
|
tt, err := producer.ttBarrier.GetTimeTick()
|
2021-01-15 09:09:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("broadcast get time tick error")
|
|
|
|
}
|
|
|
|
baseMsg := ms.BaseMsg{
|
2021-01-19 04:10:49 +00:00
|
|
|
BeginTimestamp: tt,
|
|
|
|
EndTimestamp: tt,
|
2021-01-15 09:09:41 +00:00
|
|
|
HashValues: []uint32{0},
|
|
|
|
}
|
2021-01-18 11:32:08 +00:00
|
|
|
timeTickResult := internalpb2.TimeTickMsg{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_kTimeTick,
|
|
|
|
MsgID: 0,
|
2021-01-19 04:10:49 +00:00
|
|
|
Timestamp: tt,
|
2021-01-18 11:32:08 +00:00
|
|
|
SourceID: 0,
|
|
|
|
},
|
2021-01-15 09:09:41 +00:00
|
|
|
}
|
|
|
|
timeTickMsg := &ms.TimeTickMsg{
|
|
|
|
BaseMsg: baseMsg,
|
|
|
|
TimeTickMsg: timeTickResult,
|
|
|
|
}
|
2021-01-19 04:10:49 +00:00
|
|
|
for _, watcher := range producer.watchers {
|
2021-01-15 09:09:41 +00:00
|
|
|
watcher.Watch(timeTickMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
func (producer *MsgProducer) Start() error {
|
|
|
|
err := producer.ttBarrier.Start()
|
2021-01-15 09:09:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
for _, watcher := range producer.watchers {
|
2021-01-15 09:09:41 +00:00
|
|
|
watcher.Start()
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
go producer.broadcastMsg()
|
2021-01-15 09:09:41 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:10:49 +00:00
|
|
|
func (producer *MsgProducer) Close() {
|
|
|
|
producer.cancel()
|
|
|
|
producer.ttBarrier.Close()
|
|
|
|
for _, watcher := range producer.watchers {
|
2021-01-15 09:09:41 +00:00
|
|
|
watcher.Close()
|
|
|
|
}
|
|
|
|
}
|