mem_msgstream.go 4.7 KB
Newer Older
X
xige-16 已提交
1 2 3 4 5 6 7 8 9 10 11
// 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.

X
Xiangyu Wang 已提交
12
package msgstream
G
groot 已提交
13 14 15 16 17 18 19

import (
	"context"
	"errors"
	"log"
	"sync"

X
Xiangyu Wang 已提交
20
	"github.com/milvus-io/milvus/internal/proto/commonpb"
G
groot 已提交
21 22 23 24 25 26
)

type MemMsgStream struct {
	ctx          context.Context
	streamCancel func()

X
Xiangyu Wang 已提交
27
	repackFunc RepackFunc
G
groot 已提交
28

X
Xiangyu Wang 已提交
29
	consumers []*MemConsumer
G
groot 已提交
30 31
	producers []string

X
Xiangyu Wang 已提交
32
	receiveBuf chan *MsgPack
G
groot 已提交
33 34 35 36 37 38

	wait sync.WaitGroup
}

func NewMemMsgStream(ctx context.Context, receiveBufSize int64) (*MemMsgStream, error) {
	streamCtx, streamCancel := context.WithCancel(ctx)
X
Xiangyu Wang 已提交
39
	receiveBuf := make(chan *MsgPack, receiveBufSize)
G
groot 已提交
40
	channels := make([]string, 0)
X
Xiangyu Wang 已提交
41
	consumers := make([]*MemConsumer, 0)
G
groot 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	stream := &MemMsgStream{
		ctx:          streamCtx,
		streamCancel: streamCancel,
		receiveBuf:   receiveBuf,
		consumers:    consumers,
		producers:    channels,
	}

	return stream, nil
}

func (mms *MemMsgStream) Start() {
}

func (mms *MemMsgStream) Close() {
	for _, consumer := range mms.consumers {
		Mmq.DestroyConsumerGroup(consumer.GroupName, consumer.ChannelName)
	}

	mms.streamCancel()
	mms.wait.Wait()
}

func (mms *MemMsgStream) SetRepackFunc(repackFunc RepackFunc) {
	mms.repackFunc = repackFunc
}

func (mms *MemMsgStream) AsProducer(channels []string) {
	for _, channel := range channels {
		err := Mmq.CreateChannel(channel)
		if err == nil {
			mms.producers = append(mms.producers, channel)
		} else {
			errMsg := "Failed to create producer " + channel + ", error = " + err.Error()
			panic(errMsg)
		}
	}
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
func (mms *MemMsgStream) ComputeProduceChannelIndexes(tsMsgs []TsMsg) [][]int32 {
	if len(tsMsgs) <= 0 {
		return nil
	}
	reBucketValues := make([][]int32, len(tsMsgs))
	channelNum := uint32(len(mms.producers))
	if channelNum == 0 {
		return nil
	}
	for idx, tsMsg := range tsMsgs {
		hashValues := tsMsg.HashKeys()
		bucketValues := make([]int32, len(hashValues))
		for index, hashValue := range hashValues {
			bucketValues[index] = int32(hashValue % channelNum)
		}
		reBucketValues[idx] = bucketValues
	}
	return reBucketValues
}

G
groot 已提交
102 103 104 105 106 107 108 109 110 111 112 113
func (mms *MemMsgStream) AsConsumer(channels []string, groupName string) {
	for _, channelName := range channels {
		consumer, err := Mmq.CreateConsumerGroup(groupName, channelName)
		if err == nil {
			mms.consumers = append(mms.consumers, consumer)

			mms.wait.Add(1)
			go mms.receiveMsg(*consumer)
		}
	}
}

X
Xiangyu Wang 已提交
114
func (mms *MemMsgStream) Produce(pack *MsgPack) error {
G
groot 已提交
115 116 117 118 119 120 121 122
	tsMsgs := pack.Msgs
	if len(tsMsgs) <= 0 {
		log.Printf("Warning: Receive empty msgPack")
		return nil
	}
	if len(mms.producers) <= 0 {
		return errors.New("nil producer in msg stream")
	}
123
	reBucketValues := mms.ComputeProduceChannelIndexes(pack.Msgs)
X
Xiangyu Wang 已提交
124
	var result map[int32]*MsgPack
G
groot 已提交
125 126 127 128 129 130 131
	var err error
	if mms.repackFunc != nil {
		result, err = mms.repackFunc(tsMsgs, reBucketValues)
	} else {
		msgType := (tsMsgs[0]).Type()
		switch msgType {
		case commonpb.MsgType_Insert:
X
Xiangyu Wang 已提交
132
			result, err = InsertRepackFunc(tsMsgs, reBucketValues)
G
groot 已提交
133
		case commonpb.MsgType_Delete:
X
Xiangyu Wang 已提交
134
			result, err = DeleteRepackFunc(tsMsgs, reBucketValues)
G
groot 已提交
135
		default:
X
Xiangyu Wang 已提交
136
			result, err = DefaultRepackFunc(tsMsgs, reBucketValues)
G
groot 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150
		}
	}
	if err != nil {
		return err
	}
	for k, v := range result {
		err := Mmq.Produce(mms.producers[k], v)
		if err != nil {
			return err
		}
	}
	return nil
}

X
Xiangyu Wang 已提交
151
func (mms *MemMsgStream) Broadcast(msgPack *MsgPack) error {
G
groot 已提交
152 153 154 155 156 157 158 159 160 161
	for _, channelName := range mms.producers {
		err := Mmq.Produce(channelName, msgPack)
		if err != nil {
			return err
		}
	}

	return nil
}

X
Xiangyu Wang 已提交
162
func (mms *MemMsgStream) Consume() *MsgPack {
G
groot 已提交
163 164 165 166 167
	for {
		select {
		case cm, ok := <-mms.receiveBuf:
			if !ok {
				log.Println("buf chan closed")
168
				return nil
G
groot 已提交
169
			}
170
			return cm
G
groot 已提交
171 172
		case <-mms.ctx.Done():
			log.Printf("context closed")
173
			return nil
G
groot 已提交
174 175 176 177 178 179 180 181
		}
	}
}

/**
receiveMsg func is used to solve search timeout problem
which is caused by selectcase
*/
X
Xiangyu Wang 已提交
182
func (mms *MemMsgStream) receiveMsg(consumer MemConsumer) {
G
groot 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	defer mms.wait.Done()
	for {
		select {
		case <-mms.ctx.Done():
			return
		case msg := <-consumer.MsgChan:
			if msg == nil {
				return
			}

			mms.receiveBuf <- msg
		}
	}
}

X
Xiangyu Wang 已提交
198
func (mms *MemMsgStream) Chan() <-chan *MsgPack {
G
groot 已提交
199 200 201
	return mms.receiveBuf
}

X
Xiangyu Wang 已提交
202
func (mms *MemMsgStream) Seek(offset *MsgPosition) error {
G
groot 已提交
203 204
	return errors.New("MemMsgStream seek not implemented")
}