Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: get msg type from the msg header to reduce the Unmarshal usage #36409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -320,7 +320,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 @@ -389,18 +391,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
Loading