2022-01-13 14:21:34 +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
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2022-03-09 15:39:58 +08:00
|
|
|
"sync"
|
2022-01-13 14:21:34 +08:00
|
|
|
"time"
|
|
|
|
|
2023-02-15 16:20:34 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"go.uber.org/atomic"
|
2022-01-13 14:21:34 +08:00
|
|
|
"go.uber.org/zap"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2022-01-13 14:21:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const retryWatchInterval = 20 * time.Second
|
|
|
|
|
|
|
|
type event struct {
|
|
|
|
eventType int
|
|
|
|
vChanName string
|
2022-03-09 15:39:58 +08:00
|
|
|
version int64
|
2022-01-13 14:21:34 +08:00
|
|
|
info *datapb.ChannelWatchInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type channelEventManager struct {
|
2022-03-09 15:39:58 +08:00
|
|
|
sync.Once
|
2023-05-18 11:43:23 +08:00
|
|
|
wg sync.WaitGroup
|
2022-01-13 14:21:34 +08:00
|
|
|
eventChan chan event
|
|
|
|
closeChan chan struct{}
|
2022-03-09 15:39:58 +08:00
|
|
|
handlePutEvent func(watchInfo *datapb.ChannelWatchInfo, version int64) error // node.handlePutEvent
|
2022-07-06 13:54:21 +08:00
|
|
|
handleDeleteEvent func(vChanName string) // node.handleDeleteEvent
|
2022-03-09 15:39:58 +08:00
|
|
|
retryInterval time.Duration
|
2022-01-13 14:21:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
putEventType = 1
|
|
|
|
deleteEventType = 2
|
|
|
|
)
|
|
|
|
|
2022-03-09 15:39:58 +08:00
|
|
|
func newChannelEventManager(handlePut func(*datapb.ChannelWatchInfo, int64) error,
|
2022-07-06 13:54:21 +08:00
|
|
|
handleDel func(string), retryInterval time.Duration) *channelEventManager {
|
2022-03-09 15:39:58 +08:00
|
|
|
return &channelEventManager{
|
|
|
|
eventChan: make(chan event, 10),
|
|
|
|
closeChan: make(chan struct{}),
|
|
|
|
handlePutEvent: handlePut,
|
|
|
|
handleDeleteEvent: handleDel,
|
|
|
|
retryInterval: retryInterval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:21:34 +08:00
|
|
|
func (e *channelEventManager) Run() {
|
2023-05-18 11:43:23 +08:00
|
|
|
e.wg.Add(1)
|
2022-01-13 14:21:34 +08:00
|
|
|
go func() {
|
2023-05-18 11:43:23 +08:00
|
|
|
defer e.wg.Done()
|
2022-01-13 14:21:34 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-e.eventChan:
|
|
|
|
switch event.eventType {
|
|
|
|
case putEventType:
|
2023-07-12 17:26:30 +08:00
|
|
|
err := e.handlePutEvent(event.info, event.version)
|
|
|
|
if err != nil {
|
|
|
|
// logging the error is convenient for follow-up investigation of problems
|
|
|
|
log.Warn("handle put event failed", zap.String("vChanName", event.vChanName), zap.Error(err))
|
|
|
|
}
|
2022-01-13 14:21:34 +08:00
|
|
|
case deleteEventType:
|
|
|
|
e.handleDeleteEvent(event.vChanName)
|
|
|
|
}
|
|
|
|
case <-e.closeChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:20:34 +08:00
|
|
|
func (e *channelEventManager) handleEvent(event event) {
|
|
|
|
e.eventChan <- event
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *channelEventManager) Close() {
|
|
|
|
e.Do(func() {
|
|
|
|
close(e.closeChan)
|
2023-05-18 11:43:23 +08:00
|
|
|
e.wg.Wait()
|
2023-02-15 16:20:34 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEndWatchState(state datapb.ChannelWatchState) bool {
|
|
|
|
return state != datapb.ChannelWatchState_ToWatch && // start watch
|
|
|
|
state != datapb.ChannelWatchState_ToRelease && // start release
|
|
|
|
state != datapb.ChannelWatchState_Uncomplete // legacy state, equal to ToWatch
|
|
|
|
}
|
|
|
|
|
|
|
|
type tickler struct {
|
|
|
|
progress *atomic.Int32
|
|
|
|
version int64
|
|
|
|
|
|
|
|
kv kv.MetaKv
|
|
|
|
path string
|
|
|
|
watchInfo *datapb.ChannelWatchInfo
|
|
|
|
|
2023-08-20 21:20:24 +08:00
|
|
|
interval time.Duration
|
|
|
|
closeCh chan struct{}
|
|
|
|
closeWg sync.WaitGroup
|
|
|
|
isWatchFailed bool
|
2023-02-15 16:20:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tickler) inc() {
|
|
|
|
t.progress.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tickler) watch() {
|
|
|
|
if t.interval == 0 {
|
|
|
|
log.Info("zero interval, close ticler watch",
|
2023-07-14 15:56:31 +08:00
|
|
|
zap.String("channelName", t.watchInfo.GetVchan().GetChannelName()),
|
2023-02-15 16:20:34 +08:00
|
|
|
)
|
2022-03-09 15:39:58 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:20:34 +08:00
|
|
|
t.closeWg.Add(1)
|
|
|
|
go func() {
|
2023-08-20 21:20:24 +08:00
|
|
|
defer t.closeWg.Done()
|
2023-02-23 18:59:45 +08:00
|
|
|
ticker := time.NewTicker(t.interval)
|
|
|
|
defer ticker.Stop()
|
2023-02-15 16:20:34 +08:00
|
|
|
for {
|
2022-03-09 15:39:58 +08:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2023-02-15 16:20:34 +08:00
|
|
|
nowProgress := t.progress.Load()
|
|
|
|
if t.watchInfo.Progress == nowProgress {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t.watchInfo.Progress = nowProgress
|
|
|
|
v, err := proto.Marshal(t.watchInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("fail to marshal watchInfo with progress at tickler",
|
|
|
|
zap.String("vChanName", t.watchInfo.Vchan.ChannelName),
|
|
|
|
zap.Int32("progree", nowProgress),
|
|
|
|
zap.Error(err))
|
2023-08-20 21:20:24 +08:00
|
|
|
t.isWatchFailed = true
|
2022-03-09 15:39:58 +08:00
|
|
|
return
|
|
|
|
}
|
2023-02-15 16:20:34 +08:00
|
|
|
success, err := t.kv.CompareVersionAndSwap(t.path, t.version, string(v))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("tickler update failed", zap.Error(err))
|
|
|
|
continue
|
2022-03-09 15:39:58 +08:00
|
|
|
}
|
2023-02-15 16:20:34 +08:00
|
|
|
|
|
|
|
if !success {
|
|
|
|
log.Error("tickler update failed: failed to compare version and swap",
|
|
|
|
zap.String("key", t.path), zap.Int32("progress", nowProgress), zap.Int64("version", t.version),
|
|
|
|
zap.String("vChanName", t.watchInfo.GetVchan().ChannelName))
|
2023-08-20 21:20:24 +08:00
|
|
|
t.isWatchFailed = true
|
2022-03-09 15:39:58 +08:00
|
|
|
return
|
|
|
|
}
|
2023-02-15 16:20:34 +08:00
|
|
|
log.Debug("tickler update success", zap.Int32("progress", nowProgress), zap.Int64("version", t.version),
|
|
|
|
zap.String("vChanName", t.watchInfo.GetVchan().ChannelName))
|
|
|
|
t.version++
|
|
|
|
case <-t.closeCh:
|
2022-03-09 15:39:58 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 16:20:34 +08:00
|
|
|
}()
|
2022-01-13 14:21:34 +08:00
|
|
|
}
|
|
|
|
|
2023-02-15 16:20:34 +08:00
|
|
|
func (t *tickler) stop() {
|
|
|
|
close(t.closeCh)
|
|
|
|
t.closeWg.Wait()
|
2022-03-09 15:39:58 +08:00
|
|
|
}
|
|
|
|
|
2023-02-15 16:20:34 +08:00
|
|
|
func newTickler(version int64, path string, watchInfo *datapb.ChannelWatchInfo, kv kv.MetaKv, interval time.Duration) *tickler {
|
|
|
|
return &tickler{
|
|
|
|
progress: atomic.NewInt32(0),
|
|
|
|
path: path,
|
|
|
|
kv: kv,
|
|
|
|
watchInfo: watchInfo,
|
|
|
|
version: version,
|
|
|
|
interval: interval,
|
|
|
|
closeCh: make(chan struct{}),
|
|
|
|
}
|
2022-01-13 14:21:34 +08:00
|
|
|
}
|