2021-04-19 13:47:10 +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-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-11-18 16:38:28 +08:00
|
|
|
|
|
|
|
import (
|
2021-06-15 12:41:40 +08:00
|
|
|
"context"
|
2021-09-28 12:28:03 +08:00
|
|
|
"errors"
|
2021-06-15 12:41:40 +08:00
|
|
|
"math"
|
2020-11-18 16:38:28 +08:00
|
|
|
"sync"
|
2021-06-15 12:41:40 +08:00
|
|
|
|
2021-09-24 13:57:54 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2021-11-26 16:15:16 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2020-11-18 16:38:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type tSafeWatcher struct {
|
|
|
|
notifyChan chan bool
|
2021-11-26 01:33:16 +08:00
|
|
|
closeCh chan struct{}
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTSafeWatcher() *tSafeWatcher {
|
|
|
|
return &tSafeWatcher{
|
2020-11-28 19:06:48 +08:00
|
|
|
notifyChan: make(chan bool, 1),
|
2021-11-26 01:33:16 +08:00
|
|
|
closeCh: make(chan struct{}, 1),
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher *tSafeWatcher) notify() {
|
|
|
|
if len(watcher.notifyChan) == 0 {
|
|
|
|
watcher.notifyChan <- true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 11:37:55 +08:00
|
|
|
func (watcher *tSafeWatcher) watcherChan() <-chan bool {
|
|
|
|
return watcher.notifyChan
|
|
|
|
}
|
|
|
|
|
2021-11-26 01:33:16 +08:00
|
|
|
func (watcher *tSafeWatcher) close() {
|
|
|
|
watcher.closeCh <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
type tSafer interface {
|
2020-11-18 16:38:28 +08:00
|
|
|
get() Timestamp
|
2021-06-15 12:41:40 +08:00
|
|
|
set(id UniqueID, t Timestamp)
|
2021-09-28 12:28:03 +08:00
|
|
|
registerTSafeWatcher(t *tSafeWatcher) error
|
2021-06-15 12:41:40 +08:00
|
|
|
start()
|
2021-03-22 16:36:10 +08:00
|
|
|
close()
|
2021-09-24 13:57:54 +08:00
|
|
|
removeRecord(partitionID UniqueID)
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
type tSafeMsg struct {
|
|
|
|
t Timestamp
|
|
|
|
id UniqueID // collectionID or partitionID
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
type tSafe struct {
|
2021-06-15 12:41:40 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-06-15 20:06:10 +08:00
|
|
|
channel Channel
|
2021-01-13 10:40:46 +08:00
|
|
|
tSafeMu sync.Mutex // guards all fields
|
2020-11-18 16:38:28 +08:00
|
|
|
tSafe Timestamp
|
|
|
|
watcherList []*tSafeWatcher
|
2021-06-15 12:41:40 +08:00
|
|
|
tSafeChan chan tSafeMsg
|
|
|
|
tSafeRecord map[UniqueID]Timestamp
|
2021-09-26 21:20:10 +08:00
|
|
|
isClose bool
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 20:06:10 +08:00
|
|
|
func newTSafe(ctx context.Context, channel Channel) tSafer {
|
2021-06-15 12:41:40 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
const channelSize = 4096
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
var t tSafer = &tSafe{
|
2021-06-15 12:41:40 +08:00
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
channel: channel,
|
2020-11-18 16:38:28 +08:00
|
|
|
watcherList: make([]*tSafeWatcher, 0),
|
2021-06-15 12:41:40 +08:00
|
|
|
tSafeChan: make(chan tSafeMsg, channelSize),
|
|
|
|
tSafeRecord: make(map[UniqueID]Timestamp),
|
2021-11-26 16:15:16 +08:00
|
|
|
tSafe: typeutil.ZeroTimestamp,
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
2020-12-08 14:41:04 +08:00
|
|
|
return t
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
func (ts *tSafe) start() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ts.ctx.Done():
|
2021-09-26 21:20:10 +08:00
|
|
|
ts.tSafeMu.Lock()
|
|
|
|
ts.isClose = true
|
2021-09-24 13:57:54 +08:00
|
|
|
log.Debug("tSafe context done",
|
|
|
|
zap.Any("channel", ts.channel),
|
|
|
|
)
|
2021-09-26 21:20:10 +08:00
|
|
|
for _, watcher := range ts.watcherList {
|
|
|
|
close(watcher.notifyChan)
|
|
|
|
}
|
2021-09-28 12:28:03 +08:00
|
|
|
ts.watcherList = nil
|
2021-09-26 21:20:10 +08:00
|
|
|
close(ts.tSafeChan)
|
|
|
|
ts.tSafeMu.Unlock()
|
2021-06-15 12:41:40 +08:00
|
|
|
return
|
2021-09-26 21:20:10 +08:00
|
|
|
case m, ok := <-ts.tSafeChan:
|
|
|
|
if !ok {
|
|
|
|
// should not happen!!
|
|
|
|
return
|
|
|
|
}
|
2021-06-15 12:41:40 +08:00
|
|
|
ts.tSafeMu.Lock()
|
|
|
|
ts.tSafeRecord[m.id] = m.t
|
|
|
|
var tmpT Timestamp = math.MaxUint64
|
|
|
|
for _, t := range ts.tSafeRecord {
|
|
|
|
if t <= tmpT {
|
|
|
|
tmpT = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts.tSafe = tmpT
|
|
|
|
for _, watcher := range ts.watcherList {
|
|
|
|
watcher.notify()
|
|
|
|
}
|
|
|
|
|
2021-06-19 11:45:09 +08:00
|
|
|
//log.Debug("set tSafe done",
|
|
|
|
// zap.Any("id", m.id),
|
|
|
|
// zap.Any("channel", ts.channel),
|
|
|
|
// zap.Any("t", m.t),
|
|
|
|
// zap.Any("tSafe", ts.tSafe))
|
2021-06-15 12:41:40 +08:00
|
|
|
ts.tSafeMu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:57:54 +08:00
|
|
|
// removeRecord for deleting the old partition which has been released,
|
|
|
|
// if we don't delete this, tSafe would always be the old partition's timestamp
|
|
|
|
// (because we set tSafe to the minimum timestamp) from old partition
|
|
|
|
// flow graph which has been closed and would not update tSafe any more.
|
|
|
|
// removeRecord should be called when flow graph is been removed.
|
|
|
|
func (ts *tSafe) removeRecord(partitionID UniqueID) {
|
|
|
|
ts.tSafeMu.Lock()
|
|
|
|
defer ts.tSafeMu.Unlock()
|
2021-09-28 12:28:03 +08:00
|
|
|
if ts.isClose {
|
|
|
|
// should not happen if tsafe_replica guard correctly
|
|
|
|
log.Warn("Try to remove record with tsafe close ",
|
|
|
|
zap.Any("channel", ts.channel),
|
|
|
|
zap.Any("id", partitionID))
|
|
|
|
return
|
|
|
|
}
|
2021-09-24 13:57:54 +08:00
|
|
|
log.Debug("remove tSafeRecord",
|
|
|
|
zap.Any("partitionID", partitionID),
|
|
|
|
)
|
|
|
|
delete(ts.tSafeRecord, partitionID)
|
2021-09-28 12:28:03 +08:00
|
|
|
var tmpT Timestamp = math.MaxUint64
|
|
|
|
for _, t := range ts.tSafeRecord {
|
|
|
|
if t <= tmpT {
|
|
|
|
tmpT = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts.tSafe = tmpT
|
|
|
|
for _, watcher := range ts.watcherList {
|
|
|
|
watcher.notify()
|
|
|
|
}
|
2021-09-24 13:57:54 +08:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:28:03 +08:00
|
|
|
func (ts *tSafe) registerTSafeWatcher(t *tSafeWatcher) error {
|
2021-01-12 18:03:24 +08:00
|
|
|
ts.tSafeMu.Lock()
|
2021-09-28 12:28:03 +08:00
|
|
|
if ts.isClose {
|
|
|
|
return errors.New("Failed to register tsafe watcher because tsafe is closed " + ts.channel)
|
|
|
|
}
|
2021-01-12 18:03:24 +08:00
|
|
|
defer ts.tSafeMu.Unlock()
|
2020-11-18 16:38:28 +08:00
|
|
|
ts.watcherList = append(ts.watcherList, t)
|
2021-09-28 12:28:03 +08:00
|
|
|
return nil
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (ts *tSafe) get() Timestamp {
|
2020-11-18 16:38:28 +08:00
|
|
|
ts.tSafeMu.Lock()
|
|
|
|
defer ts.tSafeMu.Unlock()
|
|
|
|
return ts.tSafe
|
|
|
|
}
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
func (ts *tSafe) set(id UniqueID, t Timestamp) {
|
2020-11-18 16:38:28 +08:00
|
|
|
ts.tSafeMu.Lock()
|
2021-01-12 18:03:24 +08:00
|
|
|
defer ts.tSafeMu.Unlock()
|
2021-09-26 21:20:10 +08:00
|
|
|
if ts.isClose {
|
|
|
|
// should not happen if tsafe_replica guard correctly
|
2021-09-28 12:28:03 +08:00
|
|
|
log.Warn("Try to set id with tsafe close ",
|
2021-09-26 21:20:10 +08:00
|
|
|
zap.Any("channel", ts.channel),
|
2021-09-28 12:28:03 +08:00
|
|
|
zap.Any("id", id))
|
2021-09-26 21:20:10 +08:00
|
|
|
return
|
|
|
|
}
|
2021-06-15 12:41:40 +08:00
|
|
|
msg := tSafeMsg{
|
|
|
|
t: t,
|
|
|
|
id: id,
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
2021-06-15 12:41:40 +08:00
|
|
|
ts.tSafeChan <- msg
|
2020-11-18 16:38:28 +08:00
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
|
|
|
|
func (ts *tSafe) close() {
|
2021-06-15 12:41:40 +08:00
|
|
|
ts.cancel()
|
2021-03-22 16:36:10 +08:00
|
|
|
}
|