2021-04-19 19:28:11 +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 17:09:41 +08:00
|
|
|
package timesync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
2021-04-13 09:47:02 +08:00
|
|
|
"fmt"
|
2021-01-15 17:09:41 +08:00
|
|
|
"math"
|
2021-02-06 10:28:53 +08:00
|
|
|
"sync"
|
2021-01-15 17:09:41 +08:00
|
|
|
"sync/atomic"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/logutil"
|
2021-03-04 16:01:30 +08:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-01-15 17:09:41 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
ms "github.com/milvus-io/milvus/internal/msgstream"
|
2021-01-15 17:09:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Timestamp = typeutil.Timestamp
|
|
|
|
UniqueID = typeutil.UniqueID
|
|
|
|
|
|
|
|
TimeTickBarrier interface {
|
|
|
|
GetTimeTick() (Timestamp, error)
|
2021-02-06 10:28:53 +08:00
|
|
|
Start()
|
|
|
|
Close()
|
2021-04-13 09:47:02 +08:00
|
|
|
AddPeer(peerID UniqueID) error
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
softTimeTickBarrier struct {
|
|
|
|
peer2LastTt map[UniqueID]Timestamp
|
|
|
|
minTtInterval Timestamp
|
|
|
|
lastTt int64
|
|
|
|
outTt chan Timestamp
|
|
|
|
ttStream ms.MsgStream
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
hardTimeTickBarrier struct {
|
2021-04-13 09:47:02 +08:00
|
|
|
peer2Tt map[UniqueID]Timestamp
|
|
|
|
peer2TtMu sync.Mutex
|
|
|
|
outTt chan Timestamp
|
|
|
|
ttStream ms.MsgStream
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-04 18:11:19 +08:00
|
|
|
func NewSoftTimeTickBarrier(ctx context.Context, ttStream ms.MsgStream, peerIds []UniqueID, minTtInterval Timestamp) *softTimeTickBarrier {
|
2021-01-22 11:07:07 +08:00
|
|
|
if len(peerIds) <= 0 {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("[newSoftTimeTickBarrier] Error: peerIds is empty!")
|
2021-01-22 11:07:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sttbarrier := softTimeTickBarrier{}
|
|
|
|
sttbarrier.minTtInterval = minTtInterval
|
2021-01-22 19:43:27 +08:00
|
|
|
sttbarrier.ttStream = ttStream
|
2021-01-22 11:07:07 +08:00
|
|
|
sttbarrier.outTt = make(chan Timestamp, 1024)
|
|
|
|
sttbarrier.peer2LastTt = make(map[UniqueID]Timestamp)
|
2021-02-04 18:11:19 +08:00
|
|
|
sttbarrier.ctx = ctx
|
2021-01-22 11:07:07 +08:00
|
|
|
for _, id := range peerIds {
|
|
|
|
sttbarrier.peer2LastTt[id] = Timestamp(0)
|
|
|
|
}
|
|
|
|
if len(peerIds) != len(sttbarrier.peer2LastTt) {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("[newSoftTimeTickBarrier] Warning: there are duplicate peerIds!")
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &sttbarrier
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:09:41 +08:00
|
|
|
func (ttBarrier *softTimeTickBarrier) GetTimeTick() (Timestamp, error) {
|
|
|
|
select {
|
|
|
|
case <-ttBarrier.ctx.Done():
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, errors.New("getTimeTick closed")
|
2021-01-15 17:09:41 +08:00
|
|
|
case ts, ok := <-ttBarrier.outTt:
|
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, errors.New("getTimeTick closed")
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
num := len(ttBarrier.outTt)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
ts, ok = <-ttBarrier.outTt
|
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, errors.New("getTimeTick closed")
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.StoreInt64(&(ttBarrier.lastTt), int64(ts))
|
|
|
|
return ts, ttBarrier.ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 10:28:53 +08:00
|
|
|
func (ttBarrier *softTimeTickBarrier) Start() {
|
2021-01-22 11:07:07 +08:00
|
|
|
for {
|
|
|
|
select {
|
2021-02-04 18:11:19 +08:00
|
|
|
case <-ttBarrier.ctx.Done():
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("[TtBarrierStart] shut down", zap.Error(ttBarrier.ctx.Err()))
|
2021-01-22 11:07:07 +08:00
|
|
|
return
|
2021-01-22 19:43:27 +08:00
|
|
|
default:
|
|
|
|
}
|
2021-03-25 14:41:46 +08:00
|
|
|
ttmsgs := ttBarrier.ttStream.Consume()
|
2021-01-22 19:43:27 +08:00
|
|
|
if len(ttmsgs.Msgs) > 0 {
|
|
|
|
for _, timetickmsg := range ttmsgs.Msgs {
|
|
|
|
ttmsg := timetickmsg.(*ms.TimeTickMsg)
|
|
|
|
oldT, ok := ttBarrier.peer2LastTt[ttmsg.Base.SourceID]
|
|
|
|
// log.Printf("[softTimeTickBarrier] peer(%d)=%d\n", ttmsg.PeerID, ttmsg.Timestamp)
|
|
|
|
|
|
|
|
if !ok {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Warn("[softTimeTickBarrier] peerID not exist", zap.Int64("peerID", ttmsg.Base.SourceID))
|
2021-01-22 19:43:27 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ttmsg.Base.Timestamp > oldT {
|
|
|
|
ttBarrier.peer2LastTt[ttmsg.Base.SourceID] = ttmsg.Base.Timestamp
|
|
|
|
|
|
|
|
// get a legal Timestamp
|
|
|
|
ts := ttBarrier.minTimestamp()
|
|
|
|
lastTt := atomic.LoadInt64(&(ttBarrier.lastTt))
|
|
|
|
if lastTt != 0 && ttBarrier.minTtInterval > ts-Timestamp(lastTt) {
|
2021-01-22 11:07:07 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-22 19:43:27 +08:00
|
|
|
ttBarrier.outTt <- ts
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ttBarrier *softTimeTickBarrier) minTimestamp() Timestamp {
|
|
|
|
tempMin := Timestamp(math.MaxUint64)
|
|
|
|
for _, tt := range ttBarrier.peer2LastTt {
|
|
|
|
if tt < tempMin {
|
|
|
|
tempMin = tt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tempMin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ttBarrier *hardTimeTickBarrier) GetTimeTick() (Timestamp, error) {
|
|
|
|
select {
|
|
|
|
case <-ttBarrier.ctx.Done():
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, errors.New("getTimeTick closed")
|
2021-01-15 17:09:41 +08:00
|
|
|
case ts, ok := <-ttBarrier.outTt:
|
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, errors.New("getTimeTick closed")
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
return ts, ttBarrier.ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 10:28:53 +08:00
|
|
|
func (ttBarrier *hardTimeTickBarrier) Start() {
|
2021-01-22 11:07:07 +08:00
|
|
|
// Last timestamp synchronized
|
2021-02-06 10:28:53 +08:00
|
|
|
ttBarrier.wg.Add(1)
|
2021-01-22 11:07:07 +08:00
|
|
|
state := Timestamp(0)
|
2021-02-06 10:28:53 +08:00
|
|
|
go func(ctx context.Context) {
|
2021-03-04 16:01:30 +08:00
|
|
|
defer logutil.LogPanic()
|
2021-02-06 10:28:53 +08:00
|
|
|
defer ttBarrier.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("[TtBarrierStart] shut down", zap.Error(ttBarrier.ctx.Err()))
|
2021-02-06 10:28:53 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2021-03-25 14:41:46 +08:00
|
|
|
ttmsgs := ttBarrier.ttStream.Consume()
|
2021-04-13 09:47:02 +08:00
|
|
|
|
|
|
|
if ttmsgs != nil && len(ttmsgs.Msgs) > 0 {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("receive tt msg")
|
2021-04-13 09:47:02 +08:00
|
|
|
ttBarrier.peer2TtMu.Lock()
|
2021-02-06 10:28:53 +08:00
|
|
|
for _, timetickmsg := range ttmsgs.Msgs {
|
|
|
|
// Suppose ttmsg.Timestamp from stream is always larger than the previous one,
|
|
|
|
// that `ttmsg.Timestamp > oldT`
|
|
|
|
ttmsg := timetickmsg.(*ms.TimeTickMsg)
|
|
|
|
|
|
|
|
oldT, ok := ttBarrier.peer2Tt[ttmsg.Base.SourceID]
|
|
|
|
if !ok {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Warn("[hardTimeTickBarrier] peerID not exist", zap.Int64("peerID", ttmsg.Base.SourceID))
|
2021-02-06 10:28:53 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-15 17:09:41 +08:00
|
|
|
|
2021-02-06 10:28:53 +08:00
|
|
|
if oldT > state {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Warn("[hardTimeTickBarrier] peer's timestamp ahead",
|
|
|
|
zap.Int64("peerID", ttmsg.Base.SourceID), zap.Uint64("timestamp", ttmsg.Base.Timestamp))
|
2021-02-06 10:28:53 +08:00
|
|
|
}
|
2021-01-15 17:09:41 +08:00
|
|
|
|
2021-02-06 10:28:53 +08:00
|
|
|
ttBarrier.peer2Tt[ttmsg.Base.SourceID] = ttmsg.Base.Timestamp
|
|
|
|
newState := ttBarrier.minTimestamp()
|
|
|
|
if newState > state {
|
|
|
|
ttBarrier.outTt <- newState
|
|
|
|
state = newState
|
|
|
|
}
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
ttBarrier.peer2TtMu.Unlock()
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
}(ttBarrier.ctx)
|
2021-02-06 10:28:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ttBarrier *hardTimeTickBarrier) Close() {
|
2021-04-13 09:47:02 +08:00
|
|
|
ttBarrier.cancel()
|
2021-02-06 10:28:53 +08:00
|
|
|
ttBarrier.wg.Wait()
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ttBarrier *hardTimeTickBarrier) minTimestamp() Timestamp {
|
|
|
|
tempMin := Timestamp(math.MaxUint64)
|
|
|
|
for _, tt := range ttBarrier.peer2Tt {
|
|
|
|
if tt < tempMin {
|
|
|
|
tempMin = tt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tempMin
|
|
|
|
}
|
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
func (ttBarrier *hardTimeTickBarrier) AddPeer(peerID UniqueID) error {
|
|
|
|
ttBarrier.peer2TtMu.Lock()
|
|
|
|
defer ttBarrier.peer2TtMu.Unlock()
|
|
|
|
if _, ok := ttBarrier.peer2Tt[peerID]; ok {
|
|
|
|
return fmt.Errorf("peer %d already exist", peerID)
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
ttBarrier.peer2Tt[peerID] = Timestamp(0)
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-15 17:09:41 +08:00
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
func NewHardTimeTickBarrier(ctx context.Context, ttStream ms.MsgStream, peerIds []UniqueID) *hardTimeTickBarrier {
|
|
|
|
ttbarrier := hardTimeTickBarrier{}
|
|
|
|
ttbarrier.ttStream = ttStream
|
|
|
|
ttbarrier.outTt = make(chan Timestamp, 1024)
|
2021-01-15 17:09:41 +08:00
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
ttbarrier.peer2Tt = make(map[UniqueID]Timestamp)
|
|
|
|
ttbarrier.ctx, ttbarrier.cancel = context.WithCancel(ctx)
|
2021-01-15 17:09:41 +08:00
|
|
|
for _, id := range peerIds {
|
2021-04-13 09:47:02 +08:00
|
|
|
ttbarrier.peer2Tt[id] = Timestamp(0)
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
2021-04-13 09:47:02 +08:00
|
|
|
if len(peerIds) != len(ttbarrier.peer2Tt) {
|
|
|
|
log.Warn("[newHardTimeTickBarrier] there are duplicate peerIds!", zap.Int64s("peerIDs", peerIds))
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
|
|
|
|
2021-04-13 09:47:02 +08:00
|
|
|
return &ttbarrier
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|