mirror of
https://gitee.com/milvus-io/milvus.git
synced 2026-01-07 19:31:51 +08:00
220 lines
5.4 KiB
Go
220 lines
5.4 KiB
Go
package msgstream
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
)
|
|
|
|
type UniqueID = typeutil.UniqueID
|
|
type Timestamp = typeutil.Timestamp
|
|
type IntPrimaryKey = typeutil.IntPrimaryKey
|
|
|
|
type MsgPack struct {
|
|
BeginTs Timestamp
|
|
EndTs Timestamp
|
|
Msgs []TsMsg
|
|
}
|
|
|
|
type RepackFunc func(msgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error)
|
|
|
|
type MsgStream interface {
|
|
Start()
|
|
Close()
|
|
|
|
Produce(*MsgPack) error
|
|
Broadcast(*MsgPack) error
|
|
Consume() *MsgPack
|
|
Chan() <-chan *MsgPack
|
|
}
|
|
|
|
//TODO test InMemMsgStream
|
|
/*
|
|
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
|
|
}
|
|
*/
|
|
|
|
func CheckTimeTickMsg(msg map[int]Timestamp, isChannelReady []bool, mu *sync.RWMutex) (Timestamp, bool) {
|
|
checkMap := make(map[Timestamp]int)
|
|
var maxTime Timestamp = 0
|
|
for _, v := range msg {
|
|
checkMap[v]++
|
|
if v > maxTime {
|
|
maxTime = v
|
|
}
|
|
}
|
|
if len(checkMap) <= 1 {
|
|
for i := range msg {
|
|
isChannelReady[i] = false
|
|
}
|
|
return maxTime, true
|
|
}
|
|
for i := range msg {
|
|
mu.RLock()
|
|
v := msg[i]
|
|
mu.Unlock()
|
|
if v != maxTime {
|
|
isChannelReady[i] = false
|
|
} else {
|
|
isChannelReady[i] = true
|
|
}
|
|
}
|
|
|
|
return 0, false
|
|
}
|
|
|
|
func InsertRepackFunc(tsMsgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error) {
|
|
result := make(map[int32]*MsgPack)
|
|
for i, request := range tsMsgs {
|
|
if request.Type() != commonpb.MsgType_kInsert {
|
|
return nil, errors.New("msg's must be Insert")
|
|
}
|
|
insertRequest := request.(*InsertMsg)
|
|
keys := hashKeys[i]
|
|
|
|
timestampLen := len(insertRequest.Timestamps)
|
|
rowIDLen := len(insertRequest.RowIDs)
|
|
rowDataLen := len(insertRequest.RowData)
|
|
keysLen := len(keys)
|
|
|
|
if keysLen != timestampLen || keysLen != rowIDLen || keysLen != rowDataLen {
|
|
return nil, errors.New("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 := internalpb2.InsertRequest{
|
|
Base: &commonpb.MsgBase{
|
|
MsgType: commonpb.MsgType_kInsert,
|
|
MsgID: insertRequest.Base.MsgID,
|
|
Timestamp: insertRequest.Timestamps[index],
|
|
SourceID: insertRequest.Base.SourceID,
|
|
},
|
|
CollectionName: insertRequest.CollectionName,
|
|
PartitionName: insertRequest.PartitionName,
|
|
SegmentID: insertRequest.SegmentID,
|
|
ChannelID: insertRequest.ChannelID,
|
|
Timestamps: []uint64{insertRequest.Timestamps[index]},
|
|
RowIDs: []int64{insertRequest.RowIDs[index]},
|
|
RowData: []*commonpb.Blob{insertRequest.RowData[index]},
|
|
}
|
|
|
|
insertMsg := &InsertMsg{
|
|
BaseMsg: BaseMsg{
|
|
MsgCtx: request.GetMsgContext(),
|
|
},
|
|
InsertRequest: sliceRequest,
|
|
}
|
|
result[key].Msgs = append(result[key].Msgs, insertMsg)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func DeleteRepackFunc(tsMsgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error) {
|
|
result := make(map[int32]*MsgPack)
|
|
for i, request := range tsMsgs {
|
|
if request.Type() != commonpb.MsgType_kDelete {
|
|
return nil, errors.New("msg's must be Delete")
|
|
}
|
|
deleteRequest := request.(*DeleteMsg)
|
|
keys := hashKeys[i]
|
|
|
|
timestampLen := len(deleteRequest.Timestamps)
|
|
primaryKeysLen := len(deleteRequest.PrimaryKeys)
|
|
keysLen := len(keys)
|
|
|
|
if keysLen != timestampLen || keysLen != primaryKeysLen {
|
|
return nil, errors.New("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 := internalpb2.DeleteRequest{
|
|
Base: &commonpb.MsgBase{
|
|
MsgType: commonpb.MsgType_kDelete,
|
|
MsgID: deleteRequest.Base.MsgID,
|
|
Timestamp: deleteRequest.Timestamps[index],
|
|
SourceID: deleteRequest.Base.SourceID,
|
|
},
|
|
CollectionName: deleteRequest.CollectionName,
|
|
ChannelID: deleteRequest.ChannelID,
|
|
Timestamps: []uint64{deleteRequest.Timestamps[index]},
|
|
PrimaryKeys: []int64{deleteRequest.PrimaryKeys[index]},
|
|
}
|
|
|
|
deleteMsg := &DeleteMsg{
|
|
BaseMsg: BaseMsg{
|
|
MsgCtx: request.GetMsgContext(),
|
|
},
|
|
DeleteRequest: sliceRequest,
|
|
}
|
|
result[key].Msgs = append(result[key].Msgs, deleteMsg)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func DefaultRepackFunc(tsMsgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error) {
|
|
result := make(map[int32]*MsgPack)
|
|
for i, request := range tsMsgs {
|
|
keys := hashKeys[i]
|
|
if len(keys) != 1 {
|
|
return nil, errors.New("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
|
|
}
|