2021-11-10 23:57:08 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-06-22 14:40:07 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-10 23:57:08 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-06-22 14:40:07 +08:00
|
|
|
//
|
2021-11-10 23:57:08 +08:00
|
|
|
// 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-06-22 14:40:07 +08:00
|
|
|
|
|
|
|
package proxy
|
2021-05-24 09:49:28 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-11-05 09:14:02 +08:00
|
|
|
"go.uber.org/zap"
|
2021-09-26 19:23:56 +08:00
|
|
|
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2021-05-24 09:49:28 +08:00
|
|
|
)
|
|
|
|
|
2021-12-24 17:36:19 +08:00
|
|
|
// ticker can update ts only when the minTs are greater than the ts of ticker, we can use maxTs to update current later
|
2021-06-15 10:19:38 +08:00
|
|
|
type getPChanStatisticsFuncType func() (map[pChan]*pChanStatistics, error)
|
2021-05-24 09:49:28 +08:00
|
|
|
|
2021-10-01 15:40:17 +08:00
|
|
|
// channelsTimeTicker manages the timestamp statistics
|
2021-05-24 09:49:28 +08:00
|
|
|
type channelsTimeTicker interface {
|
2022-01-05 19:07:20 +08:00
|
|
|
// start starts the channels time ticker.
|
2021-05-24 09:49:28 +08:00
|
|
|
start() error
|
2022-01-05 19:07:20 +08:00
|
|
|
// close closes the channels time ticker.
|
2021-05-24 09:49:28 +08:00
|
|
|
close() error
|
2021-12-29 17:25:37 +08:00
|
|
|
// getLastTick returns the last write timestamp of specific pchan.
|
2021-05-24 09:49:28 +08:00
|
|
|
getLastTick(pchan pChan) (Timestamp, error)
|
2021-12-30 20:41:33 +08:00
|
|
|
// getMinTsStatistics returns the last write timestamp of all pchans.
|
2021-11-05 09:14:02 +08:00
|
|
|
getMinTsStatistics() (map[pChan]Timestamp, Timestamp, error)
|
2022-01-04 14:11:51 +08:00
|
|
|
// getMinTick returns the minimum last write timestamp between all pchans.
|
2021-09-26 19:23:56 +08:00
|
|
|
getMinTick() Timestamp
|
2021-05-24 09:49:28 +08:00
|
|
|
}
|
|
|
|
|
2022-01-06 23:24:16 +08:00
|
|
|
// make sure channelsTimeTickerImpl implements channelsTimeTicker.
|
|
|
|
var _ channelsTimeTicker = (*channelsTimeTickerImpl)(nil)
|
|
|
|
|
2021-12-21 21:01:36 +08:00
|
|
|
// channelsTimeTickerImpl implements channelsTimeTicker.
|
2021-05-24 09:49:28 +08:00
|
|
|
type channelsTimeTickerImpl struct {
|
2021-05-31 17:28:31 +08:00
|
|
|
interval time.Duration // interval to synchronize
|
|
|
|
minTsStatistics map[pChan]Timestamp // pchan -> min Timestamp
|
|
|
|
statisticsMtx sync.RWMutex
|
|
|
|
getStatisticsFunc getPChanStatisticsFuncType
|
|
|
|
tso tsoAllocator
|
|
|
|
currents map[pChan]Timestamp
|
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-11-05 09:14:02 +08:00
|
|
|
defaultTimestamp Timestamp
|
|
|
|
minTimestamp Timestamp
|
2021-05-31 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 09:14:02 +08:00
|
|
|
func (ticker *channelsTimeTickerImpl) getMinTsStatistics() (map[pChan]Timestamp, Timestamp, error) {
|
2021-06-01 16:50:32 +08:00
|
|
|
ticker.statisticsMtx.RLock()
|
|
|
|
defer ticker.statisticsMtx.RUnlock()
|
|
|
|
|
2021-06-10 14:57:55 +08:00
|
|
|
ret := make(map[pChan]Timestamp)
|
|
|
|
for k, v := range ticker.minTsStatistics {
|
2021-06-15 10:19:38 +08:00
|
|
|
if v > 0 {
|
|
|
|
ret[k] = v
|
|
|
|
}
|
2021-06-10 14:57:55 +08:00
|
|
|
}
|
2021-11-05 09:14:02 +08:00
|
|
|
return ret, ticker.defaultTimestamp, nil
|
2021-05-24 09:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ticker *channelsTimeTickerImpl) initStatistics() {
|
|
|
|
ticker.statisticsMtx.Lock()
|
|
|
|
defer ticker.statisticsMtx.Unlock()
|
|
|
|
|
|
|
|
for pchan := range ticker.minTsStatistics {
|
|
|
|
ticker.minTsStatistics[pchan] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ticker *channelsTimeTickerImpl) initCurrents(current Timestamp) {
|
|
|
|
for pchan := range ticker.currents {
|
|
|
|
ticker.currents[pchan] = current
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 10:19:38 +08:00
|
|
|
func (ticker *channelsTimeTickerImpl) tick() error {
|
2023-03-09 18:51:52 +08:00
|
|
|
now, err := ticker.tso.AllocOne(ticker.ctx)
|
2021-06-15 10:19:38 +08:00
|
|
|
if err != nil {
|
2021-06-22 14:40:07 +08:00
|
|
|
log.Warn("Proxy channelsTimeTickerImpl failed to get ts from tso", zap.Error(err))
|
2021-06-15 10:19:38 +08:00
|
|
|
return err
|
2021-05-24 09:49:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:51:21 +08:00
|
|
|
stats, err2 := ticker.getStatisticsFunc()
|
|
|
|
if err2 != nil {
|
2022-11-08 21:59:02 +08:00
|
|
|
log.Warn("failed to get tt statistics", zap.Error(err))
|
2021-09-03 11:53:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 09:49:28 +08:00
|
|
|
ticker.statisticsMtx.Lock()
|
|
|
|
defer ticker.statisticsMtx.Unlock()
|
|
|
|
|
2021-11-05 09:14:02 +08:00
|
|
|
ticker.defaultTimestamp = now
|
|
|
|
minTs := now
|
2021-05-24 09:49:28 +08:00
|
|
|
|
|
|
|
for pchan := range ticker.currents {
|
|
|
|
current := ticker.currents[pchan]
|
2021-06-15 10:19:38 +08:00
|
|
|
stat, ok := stats[pchan]
|
2021-07-27 14:34:48 +08:00
|
|
|
|
2021-06-15 10:19:38 +08:00
|
|
|
if !ok {
|
2021-11-05 09:14:02 +08:00
|
|
|
delete(ticker.minTsStatistics, pchan)
|
|
|
|
delete(ticker.currents, pchan)
|
2021-06-15 10:19:38 +08:00
|
|
|
} else {
|
|
|
|
if stat.minTs > current {
|
2021-07-27 14:34:48 +08:00
|
|
|
ticker.minTsStatistics[pchan] = stat.minTs - 1
|
2022-12-07 18:01:19 +08:00
|
|
|
next := now + Timestamp(Params.ProxyCfg.TimeTickInterval.GetAsDuration(time.Millisecond))
|
2021-06-15 10:19:38 +08:00
|
|
|
if next > stat.maxTs {
|
|
|
|
next = stat.maxTs
|
|
|
|
}
|
|
|
|
ticker.currents[pchan] = next
|
2021-06-08 19:25:37 +08:00
|
|
|
}
|
2021-11-05 09:14:02 +08:00
|
|
|
lastMin := ticker.minTsStatistics[pchan]
|
|
|
|
if minTs > lastMin {
|
|
|
|
minTs = lastMin
|
|
|
|
}
|
2021-05-24 09:49:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 09:14:02 +08:00
|
|
|
for pchan, value := range stats {
|
2023-03-15 11:17:55 +08:00
|
|
|
if value.minTs == typeutil.ZeroTimestamp {
|
|
|
|
log.Warn("channelsTimeTickerImpl.tick, stats contains physical channel which min ts is zero ",
|
|
|
|
zap.String("pchan", pchan))
|
|
|
|
continue
|
|
|
|
}
|
2021-11-05 09:14:02 +08:00
|
|
|
_, ok := ticker.currents[pchan]
|
|
|
|
if !ok {
|
|
|
|
ticker.minTsStatistics[pchan] = value.minTs - 1
|
|
|
|
ticker.currents[pchan] = now
|
|
|
|
}
|
|
|
|
if minTs > value.minTs-1 {
|
|
|
|
minTs = value.minTs - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ticker.minTimestamp = minTs
|
|
|
|
|
2021-05-24 09:49:28 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ticker *channelsTimeTickerImpl) tickLoop() {
|
|
|
|
defer ticker.wg.Done()
|
|
|
|
|
|
|
|
timer := time.NewTicker(ticker.interval)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
err := ticker.tick()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("channelsTimeTickerImpl.tickLoop", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ticker *channelsTimeTickerImpl) start() error {
|
|
|
|
ticker.initStatistics()
|
|
|
|
|
2023-03-09 18:51:52 +08:00
|
|
|
current, err := ticker.tso.AllocOne(ticker.ctx)
|
2021-05-24 09:49:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ticker.initCurrents(current)
|
|
|
|
|
|
|
|
ticker.wg.Add(1)
|
|
|
|
go ticker.tickLoop()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ticker *channelsTimeTickerImpl) close() error {
|
|
|
|
ticker.cancel()
|
|
|
|
ticker.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ticker *channelsTimeTickerImpl) getLastTick(pchan pChan) (Timestamp, error) {
|
|
|
|
ticker.statisticsMtx.RLock()
|
|
|
|
defer ticker.statisticsMtx.RUnlock()
|
|
|
|
|
|
|
|
ts, ok := ticker.minTsStatistics[pchan]
|
|
|
|
if !ok {
|
2021-11-05 09:14:02 +08:00
|
|
|
return ticker.defaultTimestamp, nil
|
2021-05-24 09:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:23:56 +08:00
|
|
|
func (ticker *channelsTimeTickerImpl) getMinTick() Timestamp {
|
|
|
|
ticker.statisticsMtx.RLock()
|
|
|
|
defer ticker.statisticsMtx.RUnlock()
|
2021-11-05 09:14:02 +08:00
|
|
|
// may be zero
|
|
|
|
return ticker.minTimestamp
|
2021-09-26 19:23:56 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:01:30 +08:00
|
|
|
// newChannelsTimeTicker returns a channels time ticker.
|
2021-05-24 09:49:28 +08:00
|
|
|
func newChannelsTimeTicker(
|
|
|
|
ctx context.Context,
|
|
|
|
interval time.Duration,
|
|
|
|
pchans []pChan,
|
2021-05-31 17:28:31 +08:00
|
|
|
getStatisticsFunc getPChanStatisticsFuncType,
|
2021-05-24 09:49:28 +08:00
|
|
|
tso tsoAllocator,
|
|
|
|
) *channelsTimeTickerImpl {
|
|
|
|
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
ticker := &channelsTimeTickerImpl{
|
2021-05-31 17:28:31 +08:00
|
|
|
interval: interval,
|
|
|
|
minTsStatistics: make(map[pChan]Timestamp),
|
|
|
|
getStatisticsFunc: getStatisticsFunc,
|
|
|
|
tso: tso,
|
|
|
|
currents: make(map[pChan]Timestamp),
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
2021-05-24 09:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pchan := range pchans {
|
|
|
|
ticker.minTsStatistics[pchan] = 0
|
|
|
|
ticker.currents[pchan] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticker
|
|
|
|
}
|