mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-06 17:18:35 +08:00
issue: #33285 - optimize the message package - add interceptor package to achieve append operation intercepting. - add timetick interceptor to attach timetick properties for message. - add timetick background task to send timetick message. Signed-off-by: chyezh <chyezh@outlook.com>
122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
package message
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
type messageImpl struct {
|
|
payload []byte
|
|
properties propertiesImpl
|
|
}
|
|
|
|
// MessageType returns the type of message.
|
|
func (m *messageImpl) MessageType() MessageType {
|
|
val, ok := m.properties.Get(messageTypeKey)
|
|
if !ok {
|
|
return MessageTypeUnknown
|
|
}
|
|
return unmarshalMessageType(val)
|
|
}
|
|
|
|
// Version returns the message format version.
|
|
func (m *messageImpl) Version() Version {
|
|
value, ok := m.properties.Get(messageVersion)
|
|
if !ok {
|
|
return VersionOld
|
|
}
|
|
return newMessageVersionFromString(value)
|
|
}
|
|
|
|
// Payload returns payload of current message.
|
|
func (m *messageImpl) Payload() []byte {
|
|
return m.payload
|
|
}
|
|
|
|
// Properties returns the message properties.
|
|
func (m *messageImpl) Properties() Properties {
|
|
return m.properties
|
|
}
|
|
|
|
// EstimateSize returns the estimated size of current message.
|
|
func (m *messageImpl) EstimateSize() int {
|
|
// TODO: more accurate size estimation.
|
|
return len(m.payload) + m.properties.EstimateSize()
|
|
}
|
|
|
|
// WithTimeTick sets the time tick of current message.
|
|
func (m *messageImpl) WithTimeTick(tt uint64) MutableMessage {
|
|
t := proto.EncodeVarint(tt)
|
|
m.properties.Set(messageTimeTick, string(t))
|
|
return m
|
|
}
|
|
|
|
// WithLastConfirmed sets the last confirmed message id of current message.
|
|
func (m *messageImpl) WithLastConfirmed(id MessageID) MutableMessage {
|
|
m.properties.Set(messageLastConfirmed, string(id.Marshal()))
|
|
return m
|
|
}
|
|
|
|
// IntoImmutableMessage converts current message to immutable message.
|
|
func (m *messageImpl) IntoImmutableMessage(id MessageID) ImmutableMessage {
|
|
return &immutableMessageImpl{
|
|
messageImpl: *m,
|
|
id: id,
|
|
}
|
|
}
|
|
|
|
type immutableMessageImpl struct {
|
|
messageImpl
|
|
id MessageID
|
|
}
|
|
|
|
// WALName returns the name of message related wal.
|
|
func (m *immutableMessageImpl) WALName() string {
|
|
return m.id.WALName()
|
|
}
|
|
|
|
// TimeTick returns the time tick of current message.
|
|
func (m *immutableMessageImpl) TimeTick() uint64 {
|
|
value, ok := m.properties.Get(messageTimeTick)
|
|
if !ok {
|
|
panic(fmt.Sprintf("there's a bug in the message codes, timetick lost in properties of message, id: %+v", m.id))
|
|
}
|
|
v := []byte(value)
|
|
tt, n := proto.DecodeVarint(v)
|
|
if n != len(v) {
|
|
panic(fmt.Sprintf("there's a bug in the message codes, dirty timetick in properties of message, id: %+v", m.id))
|
|
}
|
|
return tt
|
|
}
|
|
|
|
func (m *immutableMessageImpl) LastConfirmedMessageID() MessageID {
|
|
value, ok := m.properties.Get(messageLastConfirmed)
|
|
if !ok {
|
|
panic(fmt.Sprintf("there's a bug in the message codes, last confirmed message lost in properties of message, id: %+v", m.id))
|
|
}
|
|
id, err := UnmarshalMessageID(m.id.WALName(), []byte(value))
|
|
if err != nil {
|
|
panic(fmt.Sprintf("there's a bug in the message codes, dirty last confirmed message in properties of message, id: %+v", m.id))
|
|
}
|
|
return id
|
|
}
|
|
|
|
// MessageID returns the message id.
|
|
func (m *immutableMessageImpl) MessageID() MessageID {
|
|
return m.id
|
|
}
|
|
|
|
func (m *immutableMessageImpl) VChannel() string {
|
|
value, ok := m.properties.Get(messageVChannel)
|
|
if !ok {
|
|
panic(fmt.Sprintf("there's a bug in the message codes, vchannel lost in properties of message, id: %+v", m.id))
|
|
}
|
|
return value
|
|
}
|
|
|
|
// Properties returns the message read only properties.
|
|
func (m *immutableMessageImpl) Properties() RProperties {
|
|
return m.properties
|
|
}
|