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 (
|
2021-01-22 11:07:07 +08:00
|
|
|
"context"
|
2021-01-19 12:10:49 +08:00
|
|
|
|
2021-03-11 14:15:18 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
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 TimeTickWatcher interface {
|
|
|
|
Watch(msg *ms.TimeTickMsg)
|
2021-01-22 11:07:07 +08:00
|
|
|
StartBackgroundLoop(ctx context.Context)
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
2021-01-19 12:10:49 +08:00
|
|
|
|
|
|
|
type MsgTimeTickWatcher struct {
|
2021-01-22 11:07:07 +08:00
|
|
|
streams []ms.MsgStream
|
|
|
|
msgQueue chan *ms.TimeTickMsg
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:41 +08:00
|
|
|
func NewMsgTimeTickWatcher(streams ...ms.MsgStream) *MsgTimeTickWatcher {
|
|
|
|
watcher := &MsgTimeTickWatcher{
|
|
|
|
streams: streams,
|
|
|
|
msgQueue: make(chan *ms.TimeTickMsg),
|
|
|
|
}
|
|
|
|
return watcher
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:10:49 +08:00
|
|
|
func (watcher *MsgTimeTickWatcher) Watch(msg *ms.TimeTickMsg) {
|
2021-01-22 11:07:07 +08:00
|
|
|
watcher.msgQueue <- msg
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
func (watcher *MsgTimeTickWatcher) StartBackgroundLoop(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("msg time tick watcher closed")
|
2021-01-22 11:07:07 +08:00
|
|
|
return
|
|
|
|
case msg := <-watcher.msgQueue:
|
|
|
|
msgPack := &ms.MsgPack{}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
|
|
|
for _, stream := range watcher.streams {
|
2021-03-25 14:41:46 +08:00
|
|
|
if err := stream.Broadcast(msgPack); err != nil {
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Warn("stream broadcast failed", zap.Error(err))
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher *MsgTimeTickWatcher) Close() {
|
|
|
|
}
|