2021-04-19 11:30:19 +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-04-02 13:48:25 +08:00
|
|
|
package msgstream
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
import (
|
2021-05-31 17:46:30 +08:00
|
|
|
"bytes"
|
2021-03-26 20:10:11 +08:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-05-29 23:21:34 +08:00
|
|
|
"fmt"
|
2021-03-26 20:10:11 +08:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-09-18 19:06:09 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/mqclient"
|
2021-08-03 10:39:24 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-03-26 20:10:11 +08:00
|
|
|
)
|
|
|
|
|
2021-09-27 14:10:09 +08:00
|
|
|
var _ MsgStream = (*mqMsgStream)(nil)
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
type mqMsgStream struct {
|
2021-03-26 20:10:11 +08:00
|
|
|
ctx context.Context
|
2021-04-02 13:48:25 +08:00
|
|
|
client mqclient.Client
|
|
|
|
producers map[string]mqclient.Producer
|
2021-03-26 20:10:11 +08:00
|
|
|
producerChannels []string
|
2021-04-02 13:48:25 +08:00
|
|
|
consumers map[string]mqclient.Consumer
|
2021-03-26 20:10:11 +08:00
|
|
|
consumerChannels []string
|
|
|
|
repackFunc RepackFunc
|
|
|
|
unmarshal UnmarshalDispatcher
|
|
|
|
receiveBuf chan *MsgPack
|
|
|
|
wait *sync.WaitGroup
|
|
|
|
streamCancel func()
|
|
|
|
bufSize int64
|
|
|
|
producerLock *sync.Mutex
|
|
|
|
consumerLock *sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:57:55 +08:00
|
|
|
// NewMqMsgStream is used to generate a new mqMsgStream object
|
2021-04-02 13:48:25 +08:00
|
|
|
func NewMqMsgStream(ctx context.Context,
|
2021-03-26 20:10:11 +08:00
|
|
|
receiveBufSize int64,
|
|
|
|
bufSize int64,
|
2021-04-02 13:48:25 +08:00
|
|
|
client mqclient.Client,
|
|
|
|
unmarshal UnmarshalDispatcher) (*mqMsgStream, error) {
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
streamCtx, streamCancel := context.WithCancel(ctx)
|
2021-04-02 13:48:25 +08:00
|
|
|
producers := make(map[string]mqclient.Producer)
|
|
|
|
consumers := make(map[string]mqclient.Consumer)
|
2021-03-26 20:10:11 +08:00
|
|
|
producerChannels := make([]string, 0)
|
|
|
|
consumerChannels := make([]string, 0)
|
|
|
|
receiveBuf := make(chan *MsgPack, receiveBufSize)
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
stream := &mqMsgStream{
|
2021-03-26 20:10:11 +08:00
|
|
|
ctx: streamCtx,
|
|
|
|
client: client,
|
|
|
|
producers: producers,
|
|
|
|
producerChannels: producerChannels,
|
|
|
|
consumers: consumers,
|
|
|
|
consumerChannels: consumerChannels,
|
|
|
|
unmarshal: unmarshal,
|
|
|
|
bufSize: bufSize,
|
|
|
|
receiveBuf: receiveBuf,
|
|
|
|
streamCancel: streamCancel,
|
|
|
|
producerLock: &sync.Mutex{},
|
|
|
|
consumerLock: &sync.Mutex{},
|
|
|
|
wait: &sync.WaitGroup{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream, nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:16:14 +08:00
|
|
|
// AsProducer create producer to send message to channels
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) AsProducer(channels []string) {
|
2021-03-26 20:10:11 +08:00
|
|
|
for _, channel := range channels {
|
2021-06-15 12:41:40 +08:00
|
|
|
if len(channel) == 0 {
|
|
|
|
log.Error("MsgStream asProducer's channel is a empty string")
|
|
|
|
break
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
fn := func() error {
|
2021-04-02 10:01:11 +08:00
|
|
|
pp, err := ms.client.CreateProducer(mqclient.ProducerOptions{Topic: channel})
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pp == nil {
|
|
|
|
return errors.New("Producer is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.producerLock.Lock()
|
2021-09-02 14:56:10 +08:00
|
|
|
defer ms.producerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.producers[channel] = pp
|
|
|
|
ms.producerChannels = append(ms.producerChannels, channel)
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-03 10:39:24 +08:00
|
|
|
err := retry.Do(context.TODO(), fn, retry.Attempts(20), retry.Sleep(time.Millisecond*200))
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
errMsg := "Failed to create producer " + channel + ", error = " + err.Error()
|
|
|
|
panic(errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:08:05 +08:00
|
|
|
// Create consumer to receive message from channels
|
2021-05-27 10:38:37 +08:00
|
|
|
func (ms *mqMsgStream) AsConsumer(channels []string, subName string) {
|
2021-10-15 11:46:33 +08:00
|
|
|
ms.AsConsumerWithPosition(channels, subName, mqclient.SubscriptionPositionEarliest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create consumer to receive message from channels, with initial position
|
|
|
|
func (ms *mqMsgStream) AsConsumerWithPosition(channels []string, subName string, position mqclient.SubscriptionInitialPosition) {
|
2021-03-26 20:10:11 +08:00
|
|
|
for _, channel := range channels {
|
|
|
|
if _, ok := ms.consumers[channel]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fn := func() error {
|
2021-04-02 10:01:11 +08:00
|
|
|
receiveChannel := make(chan mqclient.ConsumerMessage, ms.bufSize)
|
|
|
|
pc, err := ms.client.Subscribe(mqclient.ConsumerOptions{
|
2021-03-26 20:10:11 +08:00
|
|
|
Topic: channel,
|
|
|
|
SubscriptionName: subName,
|
2021-04-02 10:01:11 +08:00
|
|
|
Type: mqclient.KeyShared,
|
2021-10-15 11:46:33 +08:00
|
|
|
SubscriptionInitialPosition: position,
|
2021-03-26 20:10:11 +08:00
|
|
|
MessageChannel: receiveChannel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pc == nil {
|
|
|
|
return errors.New("Consumer is nil")
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.consumerLock.Lock()
|
2021-09-02 14:56:10 +08:00
|
|
|
defer ms.consumerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.consumers[channel] = pc
|
|
|
|
ms.consumerChannels = append(ms.consumerChannels, channel)
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-03 10:39:24 +08:00
|
|
|
err := retry.Do(context.TODO(), fn, retry.Attempts(20), retry.Sleep(time.Millisecond*200))
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
errMsg := "Failed to create consumer " + channel + ", error = " + err.Error()
|
|
|
|
panic(errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) SetRepackFunc(repackFunc RepackFunc) {
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.repackFunc = repackFunc
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) Start() {
|
2021-05-31 17:46:30 +08:00
|
|
|
for _, c := range ms.consumers {
|
|
|
|
ms.wait.Add(1)
|
|
|
|
go ms.receiveMsg(c)
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) Close() {
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.streamCancel()
|
|
|
|
ms.wait.Wait()
|
|
|
|
|
|
|
|
for _, producer := range ms.producers {
|
|
|
|
if producer != nil {
|
|
|
|
producer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, consumer := range ms.consumers {
|
|
|
|
if consumer != nil {
|
|
|
|
consumer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:30:09 +08:00
|
|
|
func (ms *mqMsgStream) ComputeProduceChannelIndexes(tsMsgs []TsMsg) [][]int32 {
|
2021-03-26 20:10:11 +08:00
|
|
|
if len(tsMsgs) <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
reBucketValues := make([][]int32, len(tsMsgs))
|
2021-05-24 16:30:09 +08:00
|
|
|
channelNum := uint32(len(ms.producerChannels))
|
2021-05-25 19:53:15 +08:00
|
|
|
|
2021-05-24 16:30:09 +08:00
|
|
|
if channelNum == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
for idx, tsMsg := range tsMsgs {
|
|
|
|
hashValues := tsMsg.HashKeys()
|
|
|
|
bucketValues := make([]int32, len(hashValues))
|
|
|
|
for index, hashValue := range hashValues {
|
2021-05-24 16:30:09 +08:00
|
|
|
bucketValues[index] = int32(hashValue % channelNum)
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
reBucketValues[idx] = bucketValues
|
|
|
|
}
|
2021-05-24 16:30:09 +08:00
|
|
|
return reBucketValues
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-05-25 19:53:15 +08:00
|
|
|
func (ms *mqMsgStream) GetProduceChannels() []string {
|
|
|
|
return ms.producerChannels
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:30:09 +08:00
|
|
|
func (ms *mqMsgStream) Produce(msgPack *MsgPack) error {
|
2021-05-27 10:38:37 +08:00
|
|
|
if msgPack == nil || len(msgPack.Msgs) <= 0 {
|
2021-05-24 16:30:09 +08:00
|
|
|
log.Debug("Warning: Receive empty msgPack")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(ms.producers) <= 0 {
|
|
|
|
return errors.New("nil producer in msg stream")
|
|
|
|
}
|
2021-05-27 10:38:37 +08:00
|
|
|
tsMsgs := msgPack.Msgs
|
2021-05-24 16:30:09 +08:00
|
|
|
reBucketValues := ms.ComputeProduceChannelIndexes(msgPack.Msgs)
|
2021-03-26 20:10:11 +08:00
|
|
|
var result map[int32]*MsgPack
|
|
|
|
var err error
|
|
|
|
if ms.repackFunc != nil {
|
|
|
|
result, err = ms.repackFunc(tsMsgs, reBucketValues)
|
|
|
|
} else {
|
|
|
|
msgType := (tsMsgs[0]).Type()
|
|
|
|
switch msgType {
|
|
|
|
case commonpb.MsgType_Insert:
|
2021-04-02 13:48:25 +08:00
|
|
|
result, err = InsertRepackFunc(tsMsgs, reBucketValues)
|
2021-09-27 10:35:58 +08:00
|
|
|
case commonpb.MsgType_Delete:
|
|
|
|
result, err = DeleteRepackFunc(tsMsgs, reBucketValues)
|
2021-03-26 20:10:11 +08:00
|
|
|
default:
|
2021-04-02 13:48:25 +08:00
|
|
|
result, err = DefaultRepackFunc(tsMsgs, reBucketValues)
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range result {
|
|
|
|
channel := ms.producerChannels[k]
|
|
|
|
for i := 0; i < len(v.Msgs); i++ {
|
2021-04-02 13:48:25 +08:00
|
|
|
sp, spanCtx := MsgSpanFromCtx(v.Msgs[i].TraceCtx(), v.Msgs[i])
|
2021-03-30 10:52:42 +08:00
|
|
|
|
2021-03-26 20:10:11 +08:00
|
|
|
mb, err := v.Msgs[i].Marshal(v.Msgs[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-26 17:36:07 +08:00
|
|
|
m, err := convertToByteArray(mb)
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
msg := &mqclient.ProducerMessage{Payload: m, Properties: map[string]string{}}
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
trace.InjectContextToPulsarMsgProperties(sp.Context(), msg.Properties)
|
|
|
|
|
2021-09-02 14:56:10 +08:00
|
|
|
ms.producerLock.Lock()
|
2021-09-26 17:38:07 +08:00
|
|
|
if _, err := ms.producers[channel].Send(
|
2021-03-26 20:10:11 +08:00
|
|
|
spanCtx,
|
|
|
|
msg,
|
|
|
|
); err != nil {
|
2021-09-02 14:56:10 +08:00
|
|
|
ms.producerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
trace.LogError(sp, err)
|
|
|
|
sp.Finish()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sp.Finish()
|
2021-09-02 14:56:10 +08:00
|
|
|
ms.producerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 11:38:57 +08:00
|
|
|
// ProduceMark send msg pack to all producers and returns corresponding msg id
|
|
|
|
// the returned message id serves as marking
|
|
|
|
func (ms *mqMsgStream) ProduceMark(msgPack *MsgPack) (map[string][]MessageID, error) {
|
|
|
|
ids := make(map[string][]MessageID)
|
|
|
|
if msgPack == nil || len(msgPack.Msgs) <= 0 {
|
|
|
|
return ids, errors.New("empty msgs")
|
|
|
|
}
|
|
|
|
if len(ms.producers) <= 0 {
|
|
|
|
return ids, errors.New("nil producer in msg stream")
|
|
|
|
}
|
|
|
|
tsMsgs := msgPack.Msgs
|
|
|
|
reBucketValues := ms.ComputeProduceChannelIndexes(msgPack.Msgs)
|
|
|
|
var result map[int32]*MsgPack
|
|
|
|
var err error
|
|
|
|
if ms.repackFunc != nil {
|
|
|
|
result, err = ms.repackFunc(tsMsgs, reBucketValues)
|
|
|
|
} else {
|
|
|
|
msgType := (tsMsgs[0]).Type()
|
|
|
|
switch msgType {
|
|
|
|
case commonpb.MsgType_Insert:
|
|
|
|
result, err = InsertRepackFunc(tsMsgs, reBucketValues)
|
|
|
|
case commonpb.MsgType_Delete:
|
|
|
|
result, err = DeleteRepackFunc(tsMsgs, reBucketValues)
|
|
|
|
default:
|
|
|
|
result, err = DefaultRepackFunc(tsMsgs, reBucketValues)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
for k, v := range result {
|
|
|
|
channel := ms.producerChannels[k]
|
|
|
|
for i, tsMsg := range v.Msgs {
|
|
|
|
sp, spanCtx := MsgSpanFromCtx(v.Msgs[i].TraceCtx(), tsMsg)
|
|
|
|
|
|
|
|
mb, err := tsMsg.Marshal(tsMsg)
|
|
|
|
if err != nil {
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := convertToByteArray(mb)
|
|
|
|
if err != nil {
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &mqclient.ProducerMessage{Payload: m, Properties: map[string]string{}}
|
|
|
|
|
|
|
|
trace.InjectContextToPulsarMsgProperties(sp.Context(), msg.Properties)
|
|
|
|
|
|
|
|
ms.producerLock.Lock()
|
|
|
|
id, err := ms.producers[channel].Send(
|
|
|
|
spanCtx,
|
|
|
|
msg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
ms.producerLock.Unlock()
|
|
|
|
trace.LogError(sp, err)
|
|
|
|
sp.Finish()
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
ids[channel] = append(ids[channel], id)
|
|
|
|
sp.Finish()
|
|
|
|
ms.producerLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:10:09 +08:00
|
|
|
// Broadcast put msgPack to all producer in current msgstream
|
|
|
|
// which ignores repackFunc logic
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) Broadcast(msgPack *MsgPack) error {
|
2021-05-27 10:38:37 +08:00
|
|
|
if msgPack == nil || len(msgPack.Msgs) <= 0 {
|
|
|
|
log.Debug("Warning: Receive empty msgPack")
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
for _, v := range msgPack.Msgs {
|
2021-04-02 13:48:25 +08:00
|
|
|
sp, spanCtx := MsgSpanFromCtx(v.TraceCtx(), v)
|
2021-03-30 10:52:42 +08:00
|
|
|
|
2021-03-26 20:10:11 +08:00
|
|
|
mb, err := v.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-26 17:36:07 +08:00
|
|
|
m, err := convertToByteArray(mb)
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
msg := &mqclient.ProducerMessage{Payload: m, Properties: map[string]string{}}
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
trace.InjectContextToPulsarMsgProperties(sp.Context(), msg.Properties)
|
|
|
|
|
|
|
|
ms.producerLock.Lock()
|
|
|
|
for _, producer := range ms.producers {
|
2021-09-26 17:38:07 +08:00
|
|
|
if _, err := producer.Send(
|
2021-03-26 20:10:11 +08:00
|
|
|
spanCtx,
|
|
|
|
msg,
|
|
|
|
); err != nil {
|
2021-09-02 14:56:10 +08:00
|
|
|
ms.producerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
trace.LogError(sp, err)
|
|
|
|
sp.Finish()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ms.producerLock.Unlock()
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:10:09 +08:00
|
|
|
// BroadcastMark broadcast msg pack to all producers and returns corresponding msg id
|
|
|
|
// the returned message id serves as marking
|
|
|
|
func (ms *mqMsgStream) BroadcastMark(msgPack *MsgPack) (map[string][]MessageID, error) {
|
|
|
|
ids := make(map[string][]MessageID)
|
|
|
|
if msgPack == nil || len(msgPack.Msgs) <= 0 {
|
|
|
|
return ids, errors.New("empty msgs")
|
|
|
|
}
|
|
|
|
for _, v := range msgPack.Msgs {
|
|
|
|
sp, spanCtx := MsgSpanFromCtx(v.TraceCtx(), v)
|
|
|
|
|
|
|
|
mb, err := v.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := convertToByteArray(mb)
|
|
|
|
if err != nil {
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &mqclient.ProducerMessage{Payload: m, Properties: map[string]string{}}
|
|
|
|
|
|
|
|
trace.InjectContextToPulsarMsgProperties(sp.Context(), msg.Properties)
|
|
|
|
|
|
|
|
ms.producerLock.Lock()
|
|
|
|
for channel, producer := range ms.producers {
|
|
|
|
id, err := producer.Send(spanCtx, msg)
|
|
|
|
if err != nil {
|
|
|
|
ms.producerLock.Unlock()
|
|
|
|
trace.LogError(sp, err)
|
|
|
|
sp.Finish()
|
|
|
|
return ids, err
|
|
|
|
}
|
|
|
|
ids[channel] = append(ids[channel], id)
|
|
|
|
}
|
|
|
|
ms.producerLock.Unlock()
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) Consume() *MsgPack {
|
2021-03-26 20:10:11 +08:00
|
|
|
for {
|
|
|
|
select {
|
2021-05-31 11:31:30 +08:00
|
|
|
case <-ms.ctx.Done():
|
|
|
|
//log.Debug("context closed")
|
|
|
|
return nil
|
2021-03-26 20:10:11 +08:00
|
|
|
case cm, ok := <-ms.receiveBuf:
|
|
|
|
if !ok {
|
|
|
|
log.Debug("buf chan closed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
func (ms *mqMsgStream) getTsMsgFromConsumerMsg(msg mqclient.ConsumerMessage) (TsMsg, error) {
|
|
|
|
header := commonpb.MsgHeader{}
|
|
|
|
err := proto.Unmarshal(msg.Payload(), &header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to unmarshal message header, err %s", err.Error())
|
|
|
|
}
|
|
|
|
tsMsg, err := ms.unmarshal.Unmarshal(msg.Payload(), header.Base.MsgType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to unmarshal tsMsg, err %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// set msg info to tsMsg
|
|
|
|
tsMsg.SetPosition(&MsgPosition{
|
|
|
|
ChannelName: filepath.Base(msg.Topic()),
|
|
|
|
MsgID: msg.ID().Serialize(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return tsMsg, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) receiveMsg(consumer mqclient.Consumer) {
|
2021-03-26 20:10:11 +08:00
|
|
|
defer ms.wait.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return
|
|
|
|
case msg, ok := <-consumer.Chan():
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
consumer.Ack(msg)
|
2021-05-31 11:31:30 +08:00
|
|
|
|
|
|
|
tsMsg, err := ms.getTsMsgFromConsumerMsg(msg)
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
2021-05-31 11:31:30 +08:00
|
|
|
log.Error("Failed to getTsMsgFromConsumerMsg", zap.Error(err))
|
2021-03-26 20:10:11 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-06-09 16:01:48 +08:00
|
|
|
pos := tsMsg.Position()
|
|
|
|
tsMsg.SetPosition(&MsgPosition{
|
|
|
|
ChannelName: pos.ChannelName,
|
|
|
|
MsgID: pos.MsgID,
|
|
|
|
MsgGroup: consumer.Subscription(),
|
|
|
|
Timestamp: tsMsg.BeginTs(),
|
|
|
|
})
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
sp, ok := ExtractFromPulsarMsgProperties(tsMsg, msg.Properties())
|
2021-03-30 10:52:42 +08:00
|
|
|
if ok {
|
|
|
|
tsMsg.SetTraceCtx(opentracing.ContextWithSpan(context.Background(), sp))
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:46:30 +08:00
|
|
|
msgPack := MsgPack{
|
|
|
|
Msgs: []TsMsg{tsMsg},
|
|
|
|
StartPositions: []*internalpb.MsgPosition{tsMsg.Position()},
|
|
|
|
EndPositions: []*internalpb.MsgPosition{tsMsg.Position()},
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.receiveBuf <- &msgPack
|
|
|
|
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *mqMsgStream) Chan() <-chan *MsgPack {
|
2021-03-26 20:10:11 +08:00
|
|
|
return ms.receiveBuf
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:17:53 +08:00
|
|
|
// Seek reset the subscription associated with this consumer to a specific position
|
2021-05-29 23:21:34 +08:00
|
|
|
func (ms *mqMsgStream) Seek(msgPositions []*internalpb.MsgPosition) error {
|
|
|
|
for _, mp := range msgPositions {
|
|
|
|
consumer, ok := ms.consumers[mp.ChannelName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("channel %s not subscribed", mp.ChannelName)
|
|
|
|
}
|
2021-03-27 09:46:54 +08:00
|
|
|
messageID, err := ms.client.BytesToMsgID(mp.MsgID)
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-01 13:46:16 +08:00
|
|
|
log.Debug("MsgStream begin to seek", zap.Any("MessageID", messageID))
|
2021-03-26 20:10:11 +08:00
|
|
|
err = consumer.Seek(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-01 13:46:16 +08:00
|
|
|
log.Debug("MsgStream seek finished", zap.Any("MessageID", messageID))
|
2021-06-24 16:44:07 +08:00
|
|
|
if _, ok := consumer.(*mqclient.RmqConsumer); !ok {
|
2021-07-01 13:46:16 +08:00
|
|
|
log.Debug("MsgStream begin to read one message after seek")
|
2021-06-24 16:44:07 +08:00
|
|
|
msg, ok := <-consumer.Chan()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("consumer closed")
|
|
|
|
}
|
2021-07-01 13:46:16 +08:00
|
|
|
log.Debug("MsgStream finish reading one message after seek")
|
2021-06-24 16:44:07 +08:00
|
|
|
consumer.Ack(msg)
|
|
|
|
if !bytes.Equal(msg.ID().Serialize(), messageID.Serialize()) {
|
|
|
|
err = fmt.Errorf("seek msg not correct")
|
|
|
|
log.Error("msMsgStream seek", zap.Error(err))
|
|
|
|
}
|
2021-05-31 17:46:30 +08:00
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
2021-05-29 23:21:34 +08:00
|
|
|
return nil
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 14:10:09 +08:00
|
|
|
var _ MsgStream = (*MqTtMsgStream)(nil)
|
|
|
|
|
2021-09-23 21:57:55 +08:00
|
|
|
// MqTtMsgStream is a msgstream that contains timeticks
|
2021-04-02 13:48:25 +08:00
|
|
|
type MqTtMsgStream struct {
|
|
|
|
mqMsgStream
|
2021-05-31 11:31:30 +08:00
|
|
|
chanMsgBuf map[mqclient.Consumer][]TsMsg
|
|
|
|
chanMsgPos map[mqclient.Consumer]*internalpb.MsgPosition
|
|
|
|
chanStopChan map[mqclient.Consumer]chan bool
|
|
|
|
chanTtMsgTime map[mqclient.Consumer]Timestamp
|
|
|
|
chanMsgBufMutex *sync.Mutex
|
|
|
|
chanTtMsgTimeMutex *sync.RWMutex
|
|
|
|
chanWaitGroup *sync.WaitGroup
|
|
|
|
lastTimeStamp Timestamp
|
|
|
|
syncConsumer chan int
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
2021-09-23 21:57:55 +08:00
|
|
|
// NewMqTtMsgStream is used to generate a new MqTtMsgStream object
|
2021-04-02 13:48:25 +08:00
|
|
|
func NewMqTtMsgStream(ctx context.Context,
|
2021-03-26 20:10:11 +08:00
|
|
|
receiveBufSize int64,
|
|
|
|
bufSize int64,
|
2021-04-02 13:48:25 +08:00
|
|
|
client mqclient.Client,
|
|
|
|
unmarshal UnmarshalDispatcher) (*MqTtMsgStream, error) {
|
|
|
|
msgStream, err := NewMqMsgStream(ctx, receiveBufSize, bufSize, client, unmarshal)
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
chanMsgBuf := make(map[mqclient.Consumer][]TsMsg)
|
|
|
|
chanMsgPos := make(map[mqclient.Consumer]*internalpb.MsgPosition)
|
|
|
|
chanStopChan := make(map[mqclient.Consumer]chan bool)
|
|
|
|
chanTtMsgTime := make(map[mqclient.Consumer]Timestamp)
|
2021-03-26 20:10:11 +08:00
|
|
|
syncConsumer := make(chan int, 1)
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
return &MqTtMsgStream{
|
2021-05-31 11:31:30 +08:00
|
|
|
mqMsgStream: *msgStream,
|
|
|
|
chanMsgBuf: chanMsgBuf,
|
|
|
|
chanMsgPos: chanMsgPos,
|
|
|
|
chanStopChan: chanStopChan,
|
|
|
|
chanTtMsgTime: chanTtMsgTime,
|
|
|
|
chanMsgBufMutex: &sync.Mutex{},
|
|
|
|
chanTtMsgTimeMutex: &sync.RWMutex{},
|
|
|
|
chanWaitGroup: &sync.WaitGroup{},
|
|
|
|
syncConsumer: syncConsumer,
|
2021-03-26 20:10:11 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *MqTtMsgStream) addConsumer(consumer mqclient.Consumer, channel string) {
|
2021-03-26 20:10:11 +08:00
|
|
|
if len(ms.consumers) == 0 {
|
|
|
|
ms.syncConsumer <- 1
|
|
|
|
}
|
|
|
|
ms.consumers[channel] = consumer
|
|
|
|
ms.consumerChannels = append(ms.consumerChannels, channel)
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgBuf[consumer] = make([]TsMsg, 0)
|
|
|
|
ms.chanMsgPos[consumer] = &internalpb.MsgPosition{
|
2021-03-26 20:10:11 +08:00
|
|
|
ChannelName: channel,
|
2021-03-27 09:46:54 +08:00
|
|
|
MsgID: make([]byte, 0),
|
2021-03-26 20:10:11 +08:00
|
|
|
Timestamp: ms.lastTimeStamp,
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanStopChan[consumer] = make(chan bool)
|
|
|
|
ms.chanTtMsgTime[consumer] = 0
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
// AsConsumer subscribes channels as consumer for a MsgStream
|
|
|
|
func (ms *MqTtMsgStream) AsConsumer(channels []string, subName string) {
|
2021-10-15 11:46:33 +08:00
|
|
|
ms.AsConsumerWithPosition(channels, subName, mqclient.SubscriptionPositionEarliest)
|
|
|
|
}
|
|
|
|
|
2021-10-28 23:44:46 +08:00
|
|
|
// AsConsumerWithPosition subscribes channels as consumer for a MsgStream and seeks to a certain position.
|
2021-10-15 11:46:33 +08:00
|
|
|
func (ms *MqTtMsgStream) AsConsumerWithPosition(channels []string, subName string, position mqclient.SubscriptionInitialPosition) {
|
2021-03-26 20:10:11 +08:00
|
|
|
for _, channel := range channels {
|
|
|
|
if _, ok := ms.consumers[channel]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fn := func() error {
|
2021-04-02 10:01:11 +08:00
|
|
|
receiveChannel := make(chan mqclient.ConsumerMessage, ms.bufSize)
|
|
|
|
pc, err := ms.client.Subscribe(mqclient.ConsumerOptions{
|
2021-03-26 20:10:11 +08:00
|
|
|
Topic: channel,
|
|
|
|
SubscriptionName: subName,
|
2021-04-02 10:01:11 +08:00
|
|
|
Type: mqclient.KeyShared,
|
2021-10-15 11:46:33 +08:00
|
|
|
SubscriptionInitialPosition: position,
|
2021-03-26 20:10:11 +08:00
|
|
|
MessageChannel: receiveChannel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pc == nil {
|
|
|
|
return errors.New("Consumer is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.consumerLock.Lock()
|
2021-09-02 14:56:10 +08:00
|
|
|
defer ms.consumerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.addConsumer(pc, channel)
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-03 10:39:24 +08:00
|
|
|
err := retry.Do(context.TODO(), fn, retry.Attempts(20), retry.Sleep(time.Millisecond*200))
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
|
|
|
errMsg := "Failed to create consumer " + channel + ", error = " + err.Error()
|
|
|
|
panic(errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:57:55 +08:00
|
|
|
// Start will start a goroutine which keep carrying msg from pulsar/rocksmq to golang chan
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *MqTtMsgStream) Start() {
|
2021-03-26 20:10:11 +08:00
|
|
|
if ms.consumers != nil {
|
|
|
|
ms.wait.Add(1)
|
|
|
|
go ms.bufMsgPackToChannel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:57:55 +08:00
|
|
|
// Close will stop goroutine and free internal producers and consumers
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *MqTtMsgStream) Close() {
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.streamCancel()
|
|
|
|
close(ms.syncConsumer)
|
|
|
|
ms.wait.Wait()
|
|
|
|
|
|
|
|
for _, producer := range ms.producers {
|
|
|
|
if producer != nil {
|
|
|
|
producer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, consumer := range ms.consumers {
|
|
|
|
if consumer != nil {
|
|
|
|
consumer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ms *MqTtMsgStream) bufMsgPackToChannel() {
|
2021-03-26 20:10:11 +08:00
|
|
|
defer ms.wait.Done()
|
2021-05-31 11:31:30 +08:00
|
|
|
chanTtMsgSync := make(map[mqclient.Consumer]bool)
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
// block here until addConsumer
|
2021-03-26 20:10:11 +08:00
|
|
|
if _, ok := <-ms.syncConsumer; !ok {
|
|
|
|
log.Debug("consumer closed!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
ms.consumerLock.Lock()
|
2021-05-31 11:31:30 +08:00
|
|
|
|
|
|
|
// wait all channels get ttMsg
|
2021-03-26 20:10:11 +08:00
|
|
|
for _, consumer := range ms.consumers {
|
2021-05-31 11:31:30 +08:00
|
|
|
if !chanTtMsgSync[consumer] {
|
|
|
|
ms.chanWaitGroup.Add(1)
|
|
|
|
go ms.consumeToTtMsg(consumer)
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanWaitGroup.Wait()
|
|
|
|
|
|
|
|
// block here until all channels reach same timetick
|
|
|
|
currTs, ok := ms.allChanReachSameTtMsg(chanTtMsgSync)
|
|
|
|
if !ok || currTs <= ms.lastTimeStamp {
|
2021-03-26 20:10:11 +08:00
|
|
|
//log.Printf("All timeTick's timestamps are inconsistent")
|
2021-05-27 10:38:37 +08:00
|
|
|
ms.consumerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
|
2021-03-26 20:10:11 +08:00
|
|
|
timeTickBuf := make([]TsMsg, 0)
|
|
|
|
startMsgPosition := make([]*internalpb.MsgPosition, 0)
|
|
|
|
endMsgPositions := make([]*internalpb.MsgPosition, 0)
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgBufMutex.Lock()
|
|
|
|
for consumer, msgs := range ms.chanMsgBuf {
|
2021-03-26 20:10:11 +08:00
|
|
|
if len(msgs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tempBuffer := make([]TsMsg, 0)
|
|
|
|
var timeTickMsg TsMsg
|
|
|
|
for _, v := range msgs {
|
|
|
|
if v.Type() == commonpb.MsgType_TimeTick {
|
|
|
|
timeTickMsg = v
|
|
|
|
continue
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
if v.EndTs() <= currTs {
|
2021-03-26 20:10:11 +08:00
|
|
|
timeTickBuf = append(timeTickBuf, v)
|
2021-05-31 11:31:30 +08:00
|
|
|
//log.Debug("pack msg", zap.Uint64("curr", v.EndTs()), zap.Uint64("currTs", currTs))
|
2021-03-26 20:10:11 +08:00
|
|
|
} else {
|
|
|
|
tempBuffer = append(tempBuffer, v)
|
|
|
|
}
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgBuf[consumer] = tempBuffer
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-06-21 14:16:03 +08:00
|
|
|
startMsgPosition = append(startMsgPosition, proto.Clone(ms.chanMsgPos[consumer]).(*internalpb.MsgPosition))
|
2021-03-26 20:10:11 +08:00
|
|
|
var newPos *internalpb.MsgPosition
|
|
|
|
if len(tempBuffer) > 0 {
|
2021-05-31 11:31:30 +08:00
|
|
|
// if tempBuffer is not empty, use tempBuffer[0] to seek
|
2021-03-26 20:10:11 +08:00
|
|
|
newPos = &internalpb.MsgPosition{
|
|
|
|
ChannelName: tempBuffer[0].Position().ChannelName,
|
|
|
|
MsgID: tempBuffer[0].Position().MsgID,
|
2021-05-31 11:31:30 +08:00
|
|
|
Timestamp: currTs,
|
2021-05-28 10:26:30 +08:00
|
|
|
MsgGroup: consumer.Subscription(),
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
endMsgPositions = append(endMsgPositions, newPos)
|
2021-05-31 11:31:30 +08:00
|
|
|
} else if timeTickMsg != nil {
|
|
|
|
// if tempBuffer is empty, use timeTickMsg to seek
|
2021-03-26 20:10:11 +08:00
|
|
|
newPos = &internalpb.MsgPosition{
|
|
|
|
ChannelName: timeTickMsg.Position().ChannelName,
|
|
|
|
MsgID: timeTickMsg.Position().MsgID,
|
2021-05-31 11:31:30 +08:00
|
|
|
Timestamp: currTs,
|
2021-05-28 10:26:30 +08:00
|
|
|
MsgGroup: consumer.Subscription(),
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
endMsgPositions = append(endMsgPositions, newPos)
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgPos[consumer] = newPos
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgBufMutex.Unlock()
|
2021-05-27 10:38:37 +08:00
|
|
|
ms.consumerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
msgPack := MsgPack{
|
|
|
|
BeginTs: ms.lastTimeStamp,
|
2021-05-31 11:31:30 +08:00
|
|
|
EndTs: currTs,
|
2021-03-26 20:10:11 +08:00
|
|
|
Msgs: timeTickBuf,
|
|
|
|
StartPositions: startMsgPosition,
|
|
|
|
EndPositions: endMsgPositions,
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
//log.Debug("send msg pack", zap.Int("len", len(msgPack.Msgs)), zap.Uint64("currTs", currTs))
|
2021-03-26 20:10:11 +08:00
|
|
|
ms.receiveBuf <- &msgPack
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.lastTimeStamp = currTs
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
// Save all msgs into chanMsgBuf[] till receive one ttMsg
|
|
|
|
func (ms *MqTtMsgStream) consumeToTtMsg(consumer mqclient.Consumer) {
|
|
|
|
defer ms.chanWaitGroup.Done()
|
2021-03-26 20:10:11 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return
|
2021-05-31 11:31:30 +08:00
|
|
|
case <-ms.chanStopChan[consumer]:
|
|
|
|
return
|
2021-03-26 20:10:11 +08:00
|
|
|
case msg, ok := <-consumer.Chan():
|
|
|
|
if !ok {
|
|
|
|
log.Debug("consumer closed!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
consumer.Ack(msg)
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
tsMsg, err := ms.getTsMsgFromConsumerMsg(msg)
|
2021-03-26 20:10:11 +08:00
|
|
|
if err != nil {
|
2021-05-31 11:31:30 +08:00
|
|
|
log.Error("Failed to getTsMsgFromConsumerMsg", zap.Error(err))
|
2021-03-26 20:10:11 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:48:25 +08:00
|
|
|
sp, ok := ExtractFromPulsarMsgProperties(tsMsg, msg.Properties())
|
2021-03-26 20:10:11 +08:00
|
|
|
if ok {
|
2021-03-30 10:52:42 +08:00
|
|
|
tsMsg.SetTraceCtx(opentracing.ContextWithSpan(context.Background(), sp))
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgBufMutex.Lock()
|
|
|
|
ms.chanMsgBuf[consumer] = append(ms.chanMsgBuf[consumer], tsMsg)
|
|
|
|
ms.chanMsgBufMutex.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
if tsMsg.Type() == commonpb.MsgType_TimeTick {
|
|
|
|
ms.chanTtMsgTimeMutex.Lock()
|
|
|
|
ms.chanTtMsgTime[consumer] = tsMsg.(*TimeTickMsg).Base.Timestamp
|
|
|
|
ms.chanTtMsgTimeMutex.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
sp.Finish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:31:30 +08:00
|
|
|
// return true only when all channels reach same timetick
|
|
|
|
func (ms *MqTtMsgStream) allChanReachSameTtMsg(chanTtMsgSync map[mqclient.Consumer]bool) (Timestamp, bool) {
|
|
|
|
tsMap := make(map[Timestamp]int)
|
2021-03-26 20:10:11 +08:00
|
|
|
var maxTime Timestamp = 0
|
2021-05-31 11:31:30 +08:00
|
|
|
for _, t := range ms.chanTtMsgTime {
|
|
|
|
tsMap[t]++
|
|
|
|
if t > maxTime {
|
|
|
|
maxTime = t
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
// when all channels reach same timetick, timeMap should contain only 1 timestamp
|
|
|
|
if len(tsMap) <= 1 {
|
|
|
|
for consumer := range ms.chanTtMsgTime {
|
|
|
|
chanTtMsgSync[consumer] = false
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
return maxTime, true
|
|
|
|
}
|
2021-05-31 11:31:30 +08:00
|
|
|
for consumer := range ms.chanTtMsgTime {
|
|
|
|
ms.chanTtMsgTimeMutex.RLock()
|
|
|
|
chanTtMsgSync[consumer] = (ms.chanTtMsgTime[consumer] == maxTime)
|
|
|
|
ms.chanTtMsgTimeMutex.RUnlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2021-05-29 23:21:34 +08:00
|
|
|
// Seek to the specified position
|
|
|
|
func (ms *MqTtMsgStream) Seek(msgPositions []*internalpb.MsgPosition) error {
|
2021-04-02 13:48:25 +08:00
|
|
|
var consumer mqclient.Consumer
|
2021-05-29 23:21:34 +08:00
|
|
|
var mp *MsgPosition
|
2021-03-26 20:10:11 +08:00
|
|
|
var err error
|
2021-03-30 13:53:49 +08:00
|
|
|
fn := func() error {
|
2021-06-09 16:01:48 +08:00
|
|
|
var ok bool
|
|
|
|
consumer, ok = ms.consumers[mp.ChannelName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("please subcribe the channel, channel name =%s", mp.ChannelName)
|
2021-03-30 13:53:49 +08:00
|
|
|
}
|
2021-06-09 16:01:48 +08:00
|
|
|
|
2021-03-30 13:53:49 +08:00
|
|
|
if consumer == nil {
|
2021-05-29 23:21:34 +08:00
|
|
|
return fmt.Errorf("consumer is nil")
|
2021-03-30 13:53:49 +08:00
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-03-30 13:53:49 +08:00
|
|
|
seekMsgID, err := ms.client.BytesToMsgID(mp.MsgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = consumer.Seek(seekMsgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-05-29 23:21:34 +08:00
|
|
|
ms.consumerLock.Lock()
|
|
|
|
defer ms.consumerLock.Unlock()
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2021-05-29 23:21:34 +08:00
|
|
|
for idx := range msgPositions {
|
|
|
|
mp = msgPositions[idx]
|
|
|
|
if len(mp.MsgID) == 0 {
|
|
|
|
return fmt.Errorf("when msgID's length equal to 0, please use AsConsumer interface")
|
|
|
|
}
|
2021-08-03 10:39:24 +08:00
|
|
|
if err = retry.Do(context.TODO(), fn, retry.Attempts(20), retry.Sleep(time.Millisecond*200)); err != nil {
|
2021-05-29 23:21:34 +08:00
|
|
|
return fmt.Errorf("Failed to seek, error %s", err.Error())
|
|
|
|
}
|
|
|
|
ms.addConsumer(consumer, mp.ChannelName)
|
|
|
|
|
|
|
|
runLoop := true
|
|
|
|
for runLoop {
|
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return nil
|
|
|
|
case msg, ok := <-consumer.Chan():
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("consumer closed")
|
|
|
|
}
|
|
|
|
consumer.Ack(msg)
|
|
|
|
|
|
|
|
headerMsg := commonpb.MsgHeader{}
|
|
|
|
err := proto.Unmarshal(msg.Payload(), &headerMsg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to unmarshal message header, err %s", err.Error())
|
|
|
|
}
|
|
|
|
tsMsg, err := ms.unmarshal.Unmarshal(msg.Payload(), headerMsg.Base.MsgType)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to unmarshal tsMsg, err %s", err.Error())
|
|
|
|
}
|
|
|
|
if tsMsg.Type() == commonpb.MsgType_TimeTick && tsMsg.BeginTs() >= mp.Timestamp {
|
|
|
|
runLoop = false
|
|
|
|
break
|
|
|
|
} else if tsMsg.BeginTs() > mp.Timestamp {
|
|
|
|
tsMsg.SetPosition(&MsgPosition{
|
|
|
|
ChannelName: filepath.Base(msg.Topic()),
|
|
|
|
MsgID: msg.ID().Serialize(),
|
|
|
|
})
|
2021-05-31 11:31:30 +08:00
|
|
|
ms.chanMsgBuf[consumer] = append(ms.chanMsgBuf[consumer], tsMsg)
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 23:21:34 +08:00
|
|
|
return nil
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|