2020-10-15 21:31:50 +08:00
|
|
|
package proxy
|
2020-10-15 16:32:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-03 14:53:36 +08:00
|
|
|
"log"
|
2020-11-11 09:54:01 +08:00
|
|
|
"sync"
|
2020-11-03 14:53:36 +08:00
|
|
|
"time"
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
"github.com/apache/pulsar-client-go/pulsar"
|
2020-11-11 09:54:01 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2020-11-05 19:11:12 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2020-10-15 16:32:22 +08:00
|
|
|
)
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
type tickCheckFunc = func(Timestamp) bool
|
|
|
|
|
2020-10-15 16:32:22 +08:00
|
|
|
type timeTick struct {
|
2020-11-11 09:54:01 +08:00
|
|
|
lastTick Timestamp
|
|
|
|
currentTick Timestamp
|
2020-11-21 19:12:59 +08:00
|
|
|
interval time.Duration
|
2020-11-11 09:54:01 +08:00
|
|
|
|
|
|
|
pulsarProducer pulsar.Producer
|
|
|
|
|
|
|
|
tsoAllocator *allocator.TimestampAllocator
|
|
|
|
tickMsgStream *msgstream.PulsarMsgStream
|
|
|
|
|
|
|
|
peerID UniqueID
|
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
timer *time.Ticker
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
checkFunc tickCheckFunc
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func newTimeTick(ctx context.Context,
|
|
|
|
tsoAllocator *allocator.TimestampAllocator,
|
2020-11-21 19:12:59 +08:00
|
|
|
interval time.Duration,
|
2020-11-14 11:24:49 +08:00
|
|
|
checkFunc tickCheckFunc) *timeTick {
|
2020-11-11 09:54:01 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
t := &timeTick{
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
tsoAllocator: tsoAllocator,
|
2020-11-21 19:12:59 +08:00
|
|
|
interval: interval,
|
2020-11-28 19:06:48 +08:00
|
|
|
peerID: Params.ProxyID(),
|
2020-11-14 11:24:49 +08:00
|
|
|
checkFunc: checkFunc,
|
2020-11-11 09:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:12:59 +08:00
|
|
|
t.tickMsgStream = msgstream.NewPulsarMsgStream(t.ctx, Params.MsgStreamTimeTickBufSize())
|
2020-11-21 15:06:46 +08:00
|
|
|
pulsarAddress := Params.PulsarAddress()
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2020-11-18 19:46:18 +08:00
|
|
|
t.tickMsgStream.SetPulsarClient(pulsarAddress)
|
2020-11-21 19:12:59 +08:00
|
|
|
t.tickMsgStream.CreatePulsarProducers(Params.ProxyTimeTickChannelNames())
|
2020-11-11 09:54:01 +08:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:27:58 +08:00
|
|
|
func (tt *timeTick) tick() error {
|
2020-10-15 16:32:22 +08:00
|
|
|
if tt.lastTick == tt.currentTick {
|
2020-11-11 09:54:01 +08:00
|
|
|
ts, err := tt.tsoAllocator.AllocOne()
|
2020-10-30 16:27:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
tt.currentTick = ts
|
|
|
|
}
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
if !tt.checkFunc(tt.currentTick) {
|
2020-11-11 09:54:01 +08:00
|
|
|
return nil
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
2020-11-11 09:54:01 +08:00
|
|
|
msgPack := msgstream.MsgPack{}
|
2020-11-17 14:10:07 +08:00
|
|
|
timeTickMsg := &msgstream.TimeTickMsg{
|
2020-11-26 17:47:46 +08:00
|
|
|
BaseMsg: msgstream.BaseMsg{
|
2020-11-30 10:07:04 +08:00
|
|
|
HashValues: []int32{int32(Params.ProxyID())},
|
2020-11-26 17:47:46 +08:00
|
|
|
},
|
2020-11-11 09:54:01 +08:00
|
|
|
TimeTickMsg: internalpb.TimeTickMsg{
|
|
|
|
MsgType: internalpb.MsgType_kTimeTick,
|
2020-11-13 15:17:18 +08:00
|
|
|
PeerID: tt.peerID,
|
2020-11-11 09:54:01 +08:00
|
|
|
Timestamp: tt.currentTick,
|
|
|
|
},
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
msgPack.Msgs = append(msgPack.Msgs, timeTickMsg)
|
2020-11-26 17:47:46 +08:00
|
|
|
err := tt.tickMsgStream.Produce(&msgPack)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("proxy send time tick error: %v", err)
|
|
|
|
} else {
|
2020-11-28 19:06:48 +08:00
|
|
|
//log.Printf("proxy send time tick message")
|
2020-11-26 17:47:46 +08:00
|
|
|
}
|
2020-10-15 16:32:22 +08:00
|
|
|
tt.lastTick = tt.currentTick
|
2020-10-30 16:27:58 +08:00
|
|
|
return nil
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
func (tt *timeTick) tickLoop() {
|
|
|
|
defer tt.wg.Done()
|
2020-11-21 19:12:59 +08:00
|
|
|
tt.timer = time.NewTicker(tt.interval)
|
2020-11-11 09:54:01 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tt.timer.C:
|
|
|
|
if err := tt.tick(); err != nil {
|
|
|
|
log.Printf("timeTick error")
|
|
|
|
}
|
|
|
|
case <-tt.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *timeTick) Start() error {
|
2020-10-15 16:32:22 +08:00
|
|
|
tt.lastTick = 0
|
2020-11-11 09:54:01 +08:00
|
|
|
ts, err := tt.tsoAllocator.AllocOne()
|
2020-11-03 14:53:36 +08:00
|
|
|
if err != nil {
|
2020-10-30 16:27:58 +08:00
|
|
|
return err
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-10-15 16:32:22 +08:00
|
|
|
tt.currentTick = ts
|
2020-11-11 09:54:01 +08:00
|
|
|
tt.tickMsgStream.Start()
|
|
|
|
tt.wg.Add(1)
|
|
|
|
go tt.tickLoop()
|
2020-10-30 16:27:58 +08:00
|
|
|
return nil
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
2020-11-11 09:54:01 +08:00
|
|
|
|
|
|
|
func (tt *timeTick) Close() {
|
|
|
|
if tt.timer != nil {
|
|
|
|
tt.timer.Stop()
|
|
|
|
}
|
|
|
|
tt.cancel()
|
|
|
|
tt.tickMsgStream.Close()
|
|
|
|
tt.wg.Wait()
|
|
|
|
}
|