2021-04-19 10:09:43 +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.
|
|
|
|
|
2021-01-15 14:38:36 +08:00
|
|
|
package proxynode
|
2020-10-15 16:32:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-11 09:54:01 +08:00
|
|
|
"sync"
|
2020-11-03 14:53:36 +08:00
|
|
|
"time"
|
|
|
|
|
2021-03-08 19:39:36 +08:00
|
|
|
"go.uber.org/zap"
|
2021-01-16 15:06:19 +08:00
|
|
|
|
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"
|
2021-03-08 19:39:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2020-11-11 09:54:01 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-03-08 19:39:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-03-12 14:22:09 +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
|
2021-02-04 14:37:12 +08:00
|
|
|
tickMsgStream msgstream.MsgStream
|
2021-02-08 14:30:54 +08:00
|
|
|
msFactory msgstream.Factory
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2021-01-12 18:03:24 +08:00
|
|
|
peerID UniqueID
|
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
timer *time.Ticker
|
|
|
|
tickLock sync.RWMutex
|
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,
|
2021-02-08 14:30:54 +08:00
|
|
|
checkFunc tickCheckFunc,
|
|
|
|
factory msgstream.Factory) *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,
|
2021-01-28 20:51:44 +08:00
|
|
|
peerID: Params.ProxyID,
|
2020-11-14 11:24:49 +08:00
|
|
|
checkFunc: checkFunc,
|
2021-02-08 14:30:54 +08:00
|
|
|
msFactory: factory,
|
2020-11-11 09:54:01 +08:00
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
t.tickMsgStream, _ = t.msFactory.NewMsgStream(t.ctx)
|
2021-02-04 14:37:12 +08:00
|
|
|
t.tickMsgStream.AsProducer(Params.ProxyTimeTickChannelNames)
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode", zap.Strings("proxynode AsProducer", 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{
|
2021-01-28 20:51:44 +08:00
|
|
|
HashValues: []uint32{uint32(Params.ProxyID)},
|
2020-11-26 17:47:46 +08:00
|
|
|
},
|
2021-03-12 14:22:09 +08:00
|
|
|
TimeTickMsg: internalpb.TimeTickMsg{
|
2021-01-18 19:32:08 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_TimeTick,
|
2021-01-18 19:32:08 +08:00
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: tt.currentTick,
|
|
|
|
SourceID: tt.peerID,
|
|
|
|
},
|
2020-11-11 09:54:01 +08:00
|
|
|
},
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
msgPack.Msgs = append(msgPack.Msgs, timeTickMsg)
|
2021-03-25 14:41:46 +08:00
|
|
|
err := tt.tickMsgStream.Produce(&msgPack)
|
2020-11-26 17:47:46 +08:00
|
|
|
if err != nil {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Warn("proxynode", zap.String("error", err.Error()))
|
2020-11-26 17:47:46 +08:00
|
|
|
}
|
2021-01-12 18:03:24 +08:00
|
|
|
tt.tickLock.Lock()
|
|
|
|
defer tt.tickLock.Unlock()
|
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 {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Warn("timeTick error")
|
2020-11-11 09:54:01 +08:00
|
|
|
}
|
|
|
|
case <-tt.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (tt *timeTick) LastTick() Timestamp {
|
2021-01-12 18:03:24 +08:00
|
|
|
tt.tickLock.RLock()
|
|
|
|
defer tt.tickLock.RUnlock()
|
2020-12-24 20:55:40 +08:00
|
|
|
return tt.lastTick
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
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()
|
|
|
|
}
|