2020-10-29 20:42:47 +08:00
|
|
|
package msgstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2020-11-26 17:58:08 +08:00
|
|
|
"reflect"
|
2020-10-29 20:42:47 +08:00
|
|
|
"sync"
|
2020-12-03 19:00:11 +08:00
|
|
|
"time"
|
2020-10-29 20:42:47 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
"github.com/apache/pulsar-client-go/pulsar"
|
2020-11-11 09:54:01 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-11-12 12:04:12 +08:00
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
2020-11-12 11:18:23 +08:00
|
|
|
commonPb "github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2020-11-04 17:58:43 +08:00
|
|
|
internalPb "github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2020-11-03 14:53:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
)
|
2020-10-29 20:42:47 +08:00
|
|
|
|
2020-11-04 17:58:43 +08:00
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
type Timestamp = typeutil.Timestamp
|
|
|
|
type IntPrimaryKey = typeutil.IntPrimaryKey
|
|
|
|
|
2020-10-29 20:42:47 +08:00
|
|
|
type MsgPack struct {
|
2020-11-04 17:58:43 +08:00
|
|
|
BeginTs Timestamp
|
|
|
|
EndTs Timestamp
|
2020-11-17 14:10:07 +08:00
|
|
|
Msgs []TsMsg
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
type RepackFunc func(msgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error)
|
2020-10-29 20:42:47 +08:00
|
|
|
|
|
|
|
type MsgStream interface {
|
2020-11-02 16:01:04 +08:00
|
|
|
Start()
|
|
|
|
Close()
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
Produce(*MsgPack) error
|
|
|
|
Broadcast(*MsgPack) error
|
|
|
|
Consume() *MsgPack
|
2020-11-10 15:56:57 +08:00
|
|
|
Chan() <-chan *MsgPack
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type PulsarMsgStream struct {
|
2020-11-12 12:04:12 +08:00
|
|
|
ctx context.Context
|
|
|
|
client *pulsar.Client
|
|
|
|
producers []*pulsar.Producer
|
|
|
|
consumers []*pulsar.Consumer
|
|
|
|
repackFunc RepackFunc
|
|
|
|
unmarshal *UnmarshalDispatcher
|
|
|
|
receiveBuf chan *MsgPack
|
|
|
|
wait *sync.WaitGroup
|
|
|
|
streamCancel func()
|
2020-11-07 13:19:31 +08:00
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
func NewPulsarMsgStream(ctx context.Context, receiveBufSize int64) *PulsarMsgStream {
|
|
|
|
streamCtx, streamCancel := context.WithCancel(ctx)
|
2020-11-12 11:18:23 +08:00
|
|
|
stream := &PulsarMsgStream{
|
2020-11-12 12:04:12 +08:00
|
|
|
ctx: streamCtx,
|
|
|
|
streamCancel: streamCancel,
|
2020-11-07 13:19:31 +08:00
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
stream.receiveBuf = make(chan *MsgPack, receiveBufSize)
|
|
|
|
return stream
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 19:46:18 +08:00
|
|
|
func (ms *PulsarMsgStream) SetPulsarClient(address string) {
|
2020-10-29 20:42:47 +08:00
|
|
|
client, err := pulsar.NewClient(pulsar.ClientOptions{URL: address})
|
|
|
|
if err != nil {
|
2020-11-07 13:19:31 +08:00
|
|
|
log.Printf("Set pulsar client failed, error = %v", err)
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
ms.client = &client
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
func (ms *PulsarMsgStream) CreatePulsarProducers(channels []string) {
|
2020-10-29 20:42:47 +08:00
|
|
|
for i := 0; i < len(channels); i++ {
|
2020-12-03 19:00:11 +08:00
|
|
|
fn := func() error {
|
|
|
|
pp, err := (*ms.client).CreateProducer(pulsar.ProducerOptions{Topic: channels[i]})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pp == nil {
|
|
|
|
return errors.New("pulsar is not ready, producer is nil")
|
|
|
|
}
|
|
|
|
ms.producers = append(ms.producers, &pp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := Retry(10, time.Millisecond*200, fn)
|
2020-10-29 20:42:47 +08:00
|
|
|
if err != nil {
|
2020-12-03 19:00:11 +08:00
|
|
|
errMsg := "Failed to create producer " + channels[i] + ", error = " + err.Error()
|
|
|
|
panic(errMsg)
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
func (ms *PulsarMsgStream) CreatePulsarConsumers(channels []string,
|
|
|
|
subName string,
|
|
|
|
unmarshal *UnmarshalDispatcher,
|
|
|
|
pulsarBufSize int64) {
|
|
|
|
ms.unmarshal = unmarshal
|
2020-10-29 20:42:47 +08:00
|
|
|
for i := 0; i < len(channels); i++ {
|
2020-12-03 19:00:11 +08:00
|
|
|
fn := func() error {
|
|
|
|
receiveChannel := make(chan pulsar.ConsumerMessage, pulsarBufSize)
|
|
|
|
pc, err := (*ms.client).Subscribe(pulsar.ConsumerOptions{
|
|
|
|
Topic: channels[i],
|
|
|
|
SubscriptionName: subName,
|
|
|
|
Type: pulsar.KeyShared,
|
|
|
|
SubscriptionInitialPosition: pulsar.SubscriptionPositionEarliest,
|
|
|
|
MessageChannel: receiveChannel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pc == nil {
|
|
|
|
return errors.New("pulsar is not ready, consumer is nil")
|
|
|
|
}
|
|
|
|
ms.consumers = append(ms.consumers, &pc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := Retry(10, time.Millisecond*200, fn)
|
2020-10-29 20:42:47 +08:00
|
|
|
if err != nil {
|
2020-12-03 19:00:11 +08:00
|
|
|
errMsg := "Failed to create consumer " + channels[i] + ", error = " + err.Error()
|
|
|
|
panic(errMsg)
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:01:04 +08:00
|
|
|
func (ms *PulsarMsgStream) SetRepackFunc(repackFunc RepackFunc) {
|
|
|
|
ms.repackFunc = repackFunc
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (ms *PulsarMsgStream) Start() {
|
2020-11-09 17:52:47 +08:00
|
|
|
ms.wait = &sync.WaitGroup{}
|
2020-11-09 16:27:11 +08:00
|
|
|
if ms.consumers != nil {
|
|
|
|
ms.wait.Add(1)
|
|
|
|
go ms.bufMsgPackToChannel()
|
|
|
|
}
|
2020-11-02 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *PulsarMsgStream) Close() {
|
2020-11-09 16:27:11 +08:00
|
|
|
ms.streamCancel()
|
|
|
|
|
2020-11-02 16:01:04 +08:00
|
|
|
for _, producer := range ms.producers {
|
|
|
|
if producer != nil {
|
|
|
|
(*producer).Close()
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 16:01:04 +08:00
|
|
|
for _, consumer := range ms.consumers {
|
|
|
|
if consumer != nil {
|
|
|
|
(*consumer).Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ms.client != nil {
|
|
|
|
(*ms.client).Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
func (ms *PulsarMsgStream) Produce(msgPack *MsgPack) error {
|
2020-11-02 16:01:04 +08:00
|
|
|
tsMsgs := msgPack.Msgs
|
2020-11-03 14:53:36 +08:00
|
|
|
if len(tsMsgs) <= 0 {
|
2020-11-07 13:19:31 +08:00
|
|
|
log.Printf("Warning: Receive empty msgPack")
|
|
|
|
return nil
|
2020-11-02 16:01:04 +08:00
|
|
|
}
|
|
|
|
reBucketValues := make([][]int32, len(tsMsgs))
|
2020-11-09 17:52:47 +08:00
|
|
|
for channelID, tsMsg := range tsMsgs {
|
2020-11-17 14:10:07 +08:00
|
|
|
hashValues := tsMsg.HashKeys()
|
2020-11-02 16:01:04 +08:00
|
|
|
bucketValues := make([]int32, len(hashValues))
|
|
|
|
for index, hashValue := range hashValues {
|
2020-11-26 16:01:31 +08:00
|
|
|
if tsMsg.Type() == internalPb.MsgType_kSearchResult {
|
|
|
|
searchResult := tsMsg.(*SearchResultMsg)
|
|
|
|
channelID := int32(searchResult.ResultChannelID)
|
|
|
|
if channelID >= int32(len(ms.producers)) {
|
|
|
|
return errors.New("Failed to produce pulsar msg to unKnow channel")
|
|
|
|
}
|
|
|
|
bucketValues[index] = channelID
|
|
|
|
continue
|
|
|
|
}
|
2020-11-30 19:38:23 +08:00
|
|
|
bucketValues[index] = int32(hashValue % uint32(len(ms.producers)))
|
2020-11-02 16:01:04 +08:00
|
|
|
}
|
2020-11-09 17:52:47 +08:00
|
|
|
reBucketValues[channelID] = bucketValues
|
2020-11-02 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:28:14 +08:00
|
|
|
var result map[int32]*MsgPack
|
2020-11-12 11:18:23 +08:00
|
|
|
var err error
|
2020-11-04 16:28:14 +08:00
|
|
|
if ms.repackFunc != nil {
|
2020-11-12 11:18:23 +08:00
|
|
|
result, err = ms.repackFunc(tsMsgs, reBucketValues)
|
2020-11-04 16:28:14 +08:00
|
|
|
} else {
|
2020-11-17 14:10:07 +08:00
|
|
|
msgType := (tsMsgs[0]).Type()
|
2020-11-12 11:18:23 +08:00
|
|
|
switch msgType {
|
|
|
|
case internalPb.MsgType_kInsert:
|
|
|
|
result, err = insertRepackFunc(tsMsgs, reBucketValues)
|
|
|
|
case internalPb.MsgType_kDelete:
|
|
|
|
result, err = deleteRepackFunc(tsMsgs, reBucketValues)
|
|
|
|
default:
|
|
|
|
result, err = defaultRepackFunc(tsMsgs, reBucketValues)
|
2020-11-04 16:28:14 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
for k, v := range result {
|
|
|
|
for i := 0; i < len(v.Msgs); i++ {
|
2020-11-17 14:10:07 +08:00
|
|
|
mb, err := v.Msgs[i].Marshal(v.Msgs[i])
|
2020-11-07 13:19:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
if _, err := (*ms.producers[k]).Send(
|
|
|
|
context.Background(),
|
|
|
|
&pulsar.ProducerMessage{Payload: mb},
|
|
|
|
); err != nil {
|
2020-11-07 13:19:31 +08:00
|
|
|
return err
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
2020-11-03 11:25:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-07 13:19:31 +08:00
|
|
|
return nil
|
2020-11-03 11:25:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
func (ms *PulsarMsgStream) Broadcast(msgPack *MsgPack) error {
|
2020-11-03 11:25:47 +08:00
|
|
|
producerLen := len(ms.producers)
|
|
|
|
for _, v := range msgPack.Msgs {
|
2020-11-17 14:10:07 +08:00
|
|
|
mb, err := v.Marshal(v)
|
2020-11-07 13:19:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-03 11:25:47 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < producerLen; i++ {
|
|
|
|
if _, err := (*ms.producers[i]).Send(
|
|
|
|
context.Background(),
|
|
|
|
&pulsar.ProducerMessage{Payload: mb},
|
|
|
|
); err != nil {
|
2020-11-07 13:19:31 +08:00
|
|
|
return err
|
2020-11-03 11:25:47 +08:00
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-07 13:19:31 +08:00
|
|
|
return nil
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
2020-11-02 16:01:04 +08:00
|
|
|
|
2020-10-29 20:42:47 +08:00
|
|
|
func (ms *PulsarMsgStream) Consume() *MsgPack {
|
2020-11-02 16:01:04 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case cm, ok := <-ms.receiveBuf:
|
|
|
|
if !ok {
|
|
|
|
log.Println("buf chan closed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cm
|
2020-11-07 13:19:31 +08:00
|
|
|
case <-ms.ctx.Done():
|
|
|
|
log.Printf("context closed")
|
2020-11-02 16:01:04 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *PulsarMsgStream) bufMsgPackToChannel() {
|
2020-11-07 13:19:31 +08:00
|
|
|
defer ms.wait.Done()
|
2020-11-26 17:58:08 +08:00
|
|
|
|
|
|
|
cases := make([]reflect.SelectCase, len(ms.consumers))
|
|
|
|
for i := 0; i < len(ms.consumers); i++ {
|
|
|
|
ch := (*ms.consumers[i]).Chan()
|
|
|
|
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)}
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:01:04 +08:00
|
|
|
for {
|
2020-11-07 13:19:31 +08:00
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2020-11-17 14:10:07 +08:00
|
|
|
tsMsgList := make([]TsMsg, 0)
|
2020-11-07 13:19:31 +08:00
|
|
|
|
2020-11-26 17:58:08 +08:00
|
|
|
for {
|
|
|
|
chosen, value, ok := reflect.Select(cases)
|
|
|
|
if !ok {
|
|
|
|
log.Printf("channel closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pulsarMsg, ok := value.Interface().(pulsar.ConsumerMessage)
|
|
|
|
if !ok {
|
|
|
|
log.Printf("type assertion failed, not consumer message type")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
(*ms.consumers[chosen]).AckID(pulsarMsg.ID())
|
|
|
|
|
|
|
|
headerMsg := internalPb.MsgHeader{}
|
|
|
|
err := proto.Unmarshal(pulsarMsg.Payload(), &headerMsg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to unmarshal message header, error = %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tsMsg, err := ms.unmarshal.Unmarshal(pulsarMsg.Payload(), headerMsg.MsgType)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to unmarshal tsMsg, error = %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tsMsgList = append(tsMsgList, tsMsg)
|
|
|
|
|
|
|
|
noMoreMessage := true
|
|
|
|
for i := 0; i < len(ms.consumers); i++ {
|
|
|
|
if len((*ms.consumers[i]).Chan()) > 0 {
|
|
|
|
noMoreMessage = false
|
2020-11-07 13:19:31 +08:00
|
|
|
}
|
2020-11-26 17:58:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if noMoreMessage {
|
|
|
|
break
|
2020-11-02 16:01:04 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 17:58:08 +08:00
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
if len(tsMsgList) > 0 {
|
|
|
|
msgPack := MsgPack{Msgs: tsMsgList}
|
|
|
|
ms.receiveBuf <- &msgPack
|
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:56:57 +08:00
|
|
|
func (ms *PulsarMsgStream) Chan() <-chan *MsgPack {
|
|
|
|
return ms.receiveBuf
|
|
|
|
}
|
|
|
|
|
2020-10-29 20:42:47 +08:00
|
|
|
type PulsarTtMsgStream struct {
|
|
|
|
PulsarMsgStream
|
2020-11-17 14:10:07 +08:00
|
|
|
inputBuf []TsMsg
|
|
|
|
unsolvedBuf []TsMsg
|
2020-11-04 17:58:43 +08:00
|
|
|
lastTimeStamp Timestamp
|
2020-11-02 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
func NewPulsarTtMsgStream(ctx context.Context, receiveBufSize int64) *PulsarTtMsgStream {
|
2020-11-09 16:27:11 +08:00
|
|
|
streamCtx, streamCancel := context.WithCancel(ctx)
|
2020-11-07 13:19:31 +08:00
|
|
|
pulsarMsgStream := PulsarMsgStream{
|
2020-11-12 12:04:12 +08:00
|
|
|
ctx: streamCtx,
|
|
|
|
streamCancel: streamCancel,
|
2020-11-07 13:19:31 +08:00
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
pulsarMsgStream.receiveBuf = make(chan *MsgPack, receiveBufSize)
|
2020-11-07 13:19:31 +08:00
|
|
|
return &PulsarTtMsgStream{
|
|
|
|
PulsarMsgStream: pulsarMsgStream,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (ms *PulsarTtMsgStream) Start() {
|
2020-11-09 17:52:47 +08:00
|
|
|
ms.wait = &sync.WaitGroup{}
|
|
|
|
if ms.consumers != nil {
|
|
|
|
ms.wait.Add(1)
|
|
|
|
go ms.bufMsgPackToChannel()
|
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:01:04 +08:00
|
|
|
func (ms *PulsarTtMsgStream) bufMsgPackToChannel() {
|
2020-11-07 13:19:31 +08:00
|
|
|
defer ms.wait.Done()
|
2020-11-17 14:10:07 +08:00
|
|
|
ms.unsolvedBuf = make([]TsMsg, 0)
|
|
|
|
ms.inputBuf = make([]TsMsg, 0)
|
2020-12-10 16:31:09 +08:00
|
|
|
isChannelReady := make([]bool, len(ms.consumers))
|
|
|
|
eofMsgTimeStamp := make(map[int]Timestamp)
|
2020-11-07 13:19:31 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ms.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
mu := sync.Mutex{}
|
|
|
|
for i := 0; i < len(ms.consumers); i++ {
|
2020-12-10 16:31:09 +08:00
|
|
|
if isChannelReady[i] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
2020-11-07 13:19:31 +08:00
|
|
|
go ms.findTimeTick(i, eofMsgTimeStamp, &wg, &mu)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2020-12-10 16:31:09 +08:00
|
|
|
timeStamp, ok := checkTimeTickMsg(eofMsgTimeStamp, isChannelReady)
|
|
|
|
if !ok || timeStamp <= ms.lastTimeStamp {
|
2020-11-09 17:52:47 +08:00
|
|
|
log.Printf("All timeTick's timestamps are inconsistent")
|
2020-12-10 16:31:09 +08:00
|
|
|
continue
|
2020-11-07 13:19:31 +08:00
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
timeTickBuf := make([]TsMsg, 0)
|
2020-11-07 13:19:31 +08:00
|
|
|
ms.inputBuf = append(ms.inputBuf, ms.unsolvedBuf...)
|
|
|
|
ms.unsolvedBuf = ms.unsolvedBuf[:0]
|
|
|
|
for _, v := range ms.inputBuf {
|
2020-11-17 14:10:07 +08:00
|
|
|
if v.EndTs() <= timeStamp {
|
2020-11-07 13:19:31 +08:00
|
|
|
timeTickBuf = append(timeTickBuf, v)
|
|
|
|
} else {
|
|
|
|
ms.unsolvedBuf = append(ms.unsolvedBuf, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ms.inputBuf = ms.inputBuf[:0]
|
|
|
|
|
|
|
|
msgPack := MsgPack{
|
|
|
|
BeginTs: ms.lastTimeStamp,
|
|
|
|
EndTs: timeStamp,
|
|
|
|
Msgs: timeTickBuf,
|
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
ms.receiveBuf <- &msgPack
|
|
|
|
ms.lastTimeStamp = timeStamp
|
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 13:19:31 +08:00
|
|
|
func (ms *PulsarTtMsgStream) findTimeTick(channelIndex int,
|
2020-11-04 17:58:43 +08:00
|
|
|
eofMsgMap map[int]Timestamp,
|
2020-10-29 20:42:47 +08:00
|
|
|
wg *sync.WaitGroup,
|
|
|
|
mu *sync.Mutex) {
|
2020-11-07 13:19:31 +08:00
|
|
|
defer wg.Done()
|
2020-10-29 20:42:47 +08:00
|
|
|
for {
|
|
|
|
select {
|
2020-11-07 13:19:31 +08:00
|
|
|
case <-ms.ctx.Done():
|
2020-10-29 20:42:47 +08:00
|
|
|
return
|
|
|
|
case pulsarMsg, ok := <-(*ms.consumers[channelIndex]).Chan():
|
2020-11-09 17:52:47 +08:00
|
|
|
if !ok {
|
2020-11-07 13:19:31 +08:00
|
|
|
log.Printf("consumer closed!")
|
|
|
|
return
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
(*ms.consumers[channelIndex]).Ack(pulsarMsg)
|
2020-11-07 13:19:31 +08:00
|
|
|
|
|
|
|
headerMsg := internalPb.MsgHeader{}
|
|
|
|
err := proto.Unmarshal(pulsarMsg.Payload(), &headerMsg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to unmarshal, error = %v", err)
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
2020-11-07 13:19:31 +08:00
|
|
|
unMarshalFunc := (*ms.unmarshal).tempMap[headerMsg.MsgType]
|
2020-12-31 17:25:45 +08:00
|
|
|
if unMarshalFunc == nil {
|
|
|
|
panic("null unMarshalFunc for " + headerMsg.MsgType.String() + " msg type")
|
|
|
|
}
|
2020-11-07 13:19:31 +08:00
|
|
|
tsMsg, err := unMarshalFunc(pulsarMsg.Payload())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to unmarshal, error = %v", err)
|
|
|
|
}
|
|
|
|
if headerMsg.MsgType == internalPb.MsgType_kTimeTick {
|
2020-11-17 14:10:07 +08:00
|
|
|
eofMsgMap[channelIndex] = tsMsg.(*TimeTickMsg).Timestamp
|
2020-11-07 13:19:31 +08:00
|
|
|
return
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
ms.inputBuf = append(ms.inputBuf, tsMsg)
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:06:46 +08:00
|
|
|
//TODO test InMemMsgStream
|
2020-11-21 11:39:23 +08:00
|
|
|
/*
|
|
|
|
type InMemMsgStream struct {
|
|
|
|
buffer chan *MsgPack
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *InMemMsgStream) Start() {}
|
|
|
|
func (ms *InMemMsgStream) Close() {}
|
|
|
|
|
|
|
|
func (ms *InMemMsgStream) ProduceOne(msg TsMsg) error {
|
|
|
|
msgPack := MsgPack{}
|
|
|
|
msgPack.BeginTs = msg.BeginTs()
|
|
|
|
msgPack.EndTs = msg.EndTs()
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
|
|
|
buffer <- &msgPack
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *InMemMsgStream) Produce(msgPack *MsgPack) error {
|
|
|
|
buffer <- msgPack
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *InMemMsgStream) Broadcast(msgPack *MsgPack) error {
|
|
|
|
return ms.Produce(msgPack)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *InMemMsgStream) Consume() *MsgPack {
|
|
|
|
select {
|
|
|
|
case msgPack := <-ms.buffer:
|
|
|
|
return msgPack
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *InMemMsgStream) Chan() <- chan *MsgPack {
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-12-10 16:31:09 +08:00
|
|
|
func checkTimeTickMsg(msg map[int]Timestamp, isChannelReady []bool) (Timestamp, bool) {
|
2020-11-04 17:58:43 +08:00
|
|
|
checkMap := make(map[Timestamp]int)
|
2020-12-10 16:31:09 +08:00
|
|
|
var maxTime Timestamp = 0
|
2020-10-29 20:42:47 +08:00
|
|
|
for _, v := range msg {
|
2020-11-09 17:52:47 +08:00
|
|
|
checkMap[v]++
|
2020-12-10 16:31:09 +08:00
|
|
|
if v > maxTime {
|
|
|
|
maxTime = v
|
|
|
|
}
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
if len(checkMap) <= 1 {
|
2020-12-10 16:31:09 +08:00
|
|
|
for i := range msg {
|
|
|
|
isChannelReady[i] = false
|
|
|
|
}
|
|
|
|
return maxTime, true
|
|
|
|
}
|
|
|
|
for i, v := range msg {
|
|
|
|
if v != maxTime {
|
|
|
|
isChannelReady[i] = false
|
|
|
|
} else {
|
|
|
|
isChannelReady[i] = true
|
2020-10-29 20:42:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-10 16:31:09 +08:00
|
|
|
|
2020-10-29 20:42:47 +08:00
|
|
|
return 0, false
|
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
func insertRepackFunc(tsMsgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error) {
|
2020-11-12 11:18:23 +08:00
|
|
|
result := make(map[int32]*MsgPack)
|
|
|
|
for i, request := range tsMsgs {
|
2020-11-17 14:10:07 +08:00
|
|
|
if request.Type() != internalPb.MsgType_kInsert {
|
2020-11-12 11:18:23 +08:00
|
|
|
return nil, errors.New(string("msg's must be Insert"))
|
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
insertRequest := request.(*InsertMsg)
|
2020-11-12 11:18:23 +08:00
|
|
|
keys := hashKeys[i]
|
|
|
|
|
|
|
|
timestampLen := len(insertRequest.Timestamps)
|
2020-11-13 15:17:18 +08:00
|
|
|
rowIDLen := len(insertRequest.RowIDs)
|
2020-11-12 11:18:23 +08:00
|
|
|
rowDataLen := len(insertRequest.RowData)
|
|
|
|
keysLen := len(keys)
|
|
|
|
|
|
|
|
if keysLen != timestampLen || keysLen != rowIDLen || keysLen != rowDataLen {
|
|
|
|
return nil, errors.New(string("the length of hashValue, timestamps, rowIDs, RowData are not equal"))
|
|
|
|
}
|
|
|
|
for index, key := range keys {
|
|
|
|
_, ok := result[key]
|
|
|
|
if !ok {
|
|
|
|
msgPack := MsgPack{}
|
|
|
|
result[key] = &msgPack
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceRequest := internalPb.InsertRequest{
|
2020-11-12 12:04:12 +08:00
|
|
|
MsgType: internalPb.MsgType_kInsert,
|
2020-11-13 15:17:18 +08:00
|
|
|
ReqID: insertRequest.ReqID,
|
2020-11-12 11:18:23 +08:00
|
|
|
CollectionName: insertRequest.CollectionName,
|
2020-11-12 12:04:12 +08:00
|
|
|
PartitionTag: insertRequest.PartitionTag,
|
2020-11-13 15:17:18 +08:00
|
|
|
SegmentID: insertRequest.SegmentID,
|
|
|
|
ChannelID: insertRequest.ChannelID,
|
|
|
|
ProxyID: insertRequest.ProxyID,
|
2020-11-12 12:04:12 +08:00
|
|
|
Timestamps: []uint64{insertRequest.Timestamps[index]},
|
2020-11-13 15:17:18 +08:00
|
|
|
RowIDs: []int64{insertRequest.RowIDs[index]},
|
2020-11-12 12:04:12 +08:00
|
|
|
RowData: []*commonPb.Blob{insertRequest.RowData[index]},
|
2020-11-12 11:18:23 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
insertMsg := &InsertMsg{
|
2020-11-12 11:18:23 +08:00
|
|
|
InsertRequest: sliceRequest,
|
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
result[key].Msgs = append(result[key].Msgs, insertMsg)
|
2020-11-12 11:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
func deleteRepackFunc(tsMsgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error) {
|
2020-11-12 11:18:23 +08:00
|
|
|
result := make(map[int32]*MsgPack)
|
|
|
|
for i, request := range tsMsgs {
|
2020-11-17 14:10:07 +08:00
|
|
|
if request.Type() != internalPb.MsgType_kDelete {
|
2020-11-12 11:18:23 +08:00
|
|
|
return nil, errors.New(string("msg's must be Delete"))
|
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
deleteRequest := request.(*DeleteMsg)
|
2020-11-12 11:18:23 +08:00
|
|
|
keys := hashKeys[i]
|
|
|
|
|
|
|
|
timestampLen := len(deleteRequest.Timestamps)
|
|
|
|
primaryKeysLen := len(deleteRequest.PrimaryKeys)
|
|
|
|
keysLen := len(keys)
|
|
|
|
|
|
|
|
if keysLen != timestampLen || keysLen != primaryKeysLen {
|
|
|
|
return nil, errors.New(string("the length of hashValue, timestamps, primaryKeys are not equal"))
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, key := range keys {
|
|
|
|
_, ok := result[key]
|
|
|
|
if !ok {
|
|
|
|
msgPack := MsgPack{}
|
|
|
|
result[key] = &msgPack
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceRequest := internalPb.DeleteRequest{
|
2020-11-12 12:04:12 +08:00
|
|
|
MsgType: internalPb.MsgType_kDelete,
|
2020-11-13 15:17:18 +08:00
|
|
|
ReqID: deleteRequest.ReqID,
|
2020-11-12 11:18:23 +08:00
|
|
|
CollectionName: deleteRequest.CollectionName,
|
2020-11-13 15:17:18 +08:00
|
|
|
ChannelID: deleteRequest.ChannelID,
|
|
|
|
ProxyID: deleteRequest.ProxyID,
|
2020-11-12 12:04:12 +08:00
|
|
|
Timestamps: []uint64{deleteRequest.Timestamps[index]},
|
|
|
|
PrimaryKeys: []int64{deleteRequest.PrimaryKeys[index]},
|
2020-11-12 11:18:23 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
deleteMsg := &DeleteMsg{
|
2020-11-12 11:18:23 +08:00
|
|
|
DeleteRequest: sliceRequest,
|
|
|
|
}
|
2020-11-17 14:10:07 +08:00
|
|
|
result[key].Msgs = append(result[key].Msgs, deleteMsg)
|
2020-11-12 11:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 14:10:07 +08:00
|
|
|
func defaultRepackFunc(tsMsgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error) {
|
2020-11-12 11:18:23 +08:00
|
|
|
result := make(map[int32]*MsgPack)
|
|
|
|
for i, request := range tsMsgs {
|
|
|
|
keys := hashKeys[i]
|
|
|
|
if len(keys) != 1 {
|
|
|
|
return nil, errors.New(string("len(msg.hashValue) must equal 1"))
|
|
|
|
}
|
|
|
|
key := keys[0]
|
|
|
|
_, ok := result[key]
|
|
|
|
if !ok {
|
|
|
|
msgPack := MsgPack{}
|
|
|
|
result[key] = &msgPack
|
|
|
|
}
|
|
|
|
result[key].Msgs = append(result[key].Msgs, request)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|