Skip to content

Commit

Permalink
enhance: get msg type from the msg header to reduce the Unmarshal usa…
Browse files Browse the repository at this point in the history
…ge (milvus-io#36409)

/kind improvement

Signed-off-by: SimFG <bang.fu@zilliz.com>
  • Loading branch information
SimFG authored Sep 24, 2024
1 parent 51cdee1 commit ddadefc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
35 changes: 35 additions & 0 deletions pkg/mq/common/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

package common

import (
"fmt"

"google.golang.org/protobuf/proto"

"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
)

// ProducerOptions contains the options of a producer
type ProducerOptions struct {
// The topic that this Producer will publish
Expand Down Expand Up @@ -65,3 +73,30 @@ const (
// SubscriptionPositionUnkown indicates we don't care about the consumer location, since we are doing another seek or only some meta api over that
SubscriptionPositionUnknown
)

const MsgTypeKey = "msg_type"

func GetMsgType(msg Message) (commonpb.MsgType, error) {
msgType := commonpb.MsgType_Undefined
properties := msg.Properties()
if properties != nil {
if val, ok := properties[MsgTypeKey]; ok {
msgType = commonpb.MsgType(commonpb.MsgType_value[val])
}
}
if msgType == commonpb.MsgType_Undefined {
header := commonpb.MsgHeader{}
if msg.Payload() == nil {
return msgType, fmt.Errorf("failed to unmarshal message header, payload is empty")
}
err := proto.Unmarshal(msg.Payload(), &header)
if err != nil {
return msgType, fmt.Errorf("failed to unmarshal message header, err %s", err.Error())
}
if header.Base == nil {
return msgType, fmt.Errorf("failed to unmarshal message, header is uncomplete")
}
msgType = header.Base.MsgType
}
return msgType, nil
}
17 changes: 6 additions & 11 deletions pkg/mq/msgstream/mq_msgstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ func (ms *mqMsgStream) Produce(msgPack *MsgPack) error {
return err
}

msg := &common.ProducerMessage{Payload: m, Properties: map[string]string{}}
msg := &common.ProducerMessage{Payload: m, Properties: map[string]string{
common.MsgTypeKey: v.Msgs[i].Type().String(),
}}
InjectCtx(spanCtx, msg.Properties)

ms.producerLock.RLock()
Expand Down Expand Up @@ -396,18 +398,11 @@ func (ms *mqMsgStream) getTsMsgFromConsumerMsg(msg common.Message) (TsMsg, error

// GetTsMsgFromConsumerMsg get TsMsg from consumer message
func GetTsMsgFromConsumerMsg(unmarshalDispatcher UnmarshalDispatcher, msg common.Message) (TsMsg, error) {
header := commonpb.MsgHeader{}
if msg.Payload() == nil {
return nil, fmt.Errorf("failed to unmarshal message header, payload is empty")
}
err := proto.Unmarshal(msg.Payload(), &header)
msgType, err := common.GetMsgType(msg)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal message header, err %s", err.Error())
}
if header.Base == nil {
return nil, fmt.Errorf("failed to unmarshal message, header is uncomplete")
return nil, err
}
tsMsg, err := unmarshalDispatcher.Unmarshal(msg.Payload(), header.Base.MsgType)
tsMsg, err := unmarshalDispatcher.Unmarshal(msg.Payload(), msgType)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal tsMsg, err %s", err.Error())
}
Expand Down

0 comments on commit ddadefc

Please sign in to comment.