2021-01-15 14:38:36 +08:00
|
|
|
## 8. Message Stream
|
2020-12-27 09:05:24 +08:00
|
|
|
|
2021-04-12 12:45:38 +08:00
|
|
|
// TODO remove?
|
2021-10-19 14:20:46 +08:00
|
|
|
|
2021-01-12 18:03:24 +08:00
|
|
|
#### 8.2 Message Stream Service API
|
2020-12-27 09:05:24 +08:00
|
|
|
|
2020-12-29 18:02:44 +08:00
|
|
|
```go
|
|
|
|
type Client interface {
|
2021-04-12 12:45:38 +08:00
|
|
|
CreateChannels(req CreateChannelRequest) (CreateChannelResponse, error)
|
2023-10-24 09:30:10 +08:00
|
|
|
DestroyChannels(req DestroyChannelRequest) error
|
2021-04-12 12:45:38 +08:00
|
|
|
DescribeChannels(req DescribeChannelRequest) (DescribeChannelResponse, error)
|
2020-12-29 18:02:44 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-19 14:20:46 +08:00
|
|
|
- _CreateChannels_
|
2020-12-29 18:02:44 +08:00
|
|
|
|
2020-12-27 09:05:24 +08:00
|
|
|
```go
|
|
|
|
type OwnerDescription struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
Role string
|
|
|
|
Address string
|
|
|
|
//Token string
|
|
|
|
DescriptionText string
|
2020-12-27 09:05:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-29 18:02:44 +08:00
|
|
|
type CreateChannelRequest struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
OwnerDescription OwnerDescription
|
|
|
|
NumChannels int
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateChannelResponse struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
ChannelNames []string
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-10-24 09:30:10 +08:00
|
|
|
- _DestroyChannels_
|
2021-01-12 18:03:24 +08:00
|
|
|
|
|
|
|
```go
|
2023-10-24 09:30:10 +08:00
|
|
|
type DestroyChannelRequest struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
ChannelNames []string
|
2020-12-29 18:02:44 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-19 14:20:46 +08:00
|
|
|
- _DescribeChannels_
|
2020-12-29 18:02:44 +08:00
|
|
|
|
|
|
|
```go
|
2021-01-12 18:03:24 +08:00
|
|
|
type DescribeChannelRequest struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
ChannelNames []string
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-27 09:05:24 +08:00
|
|
|
type ChannelDescription struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
ChannelName string
|
|
|
|
Owner OwnerDescription
|
2020-12-27 09:05:24 +08:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:03:24 +08:00
|
|
|
type DescribeChannelResponse struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
Descriptions []ChannelDescription
|
2020-12-27 09:05:24 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-04 14:16:43 +08:00
|
|
|
#### A.3 Message Stream
|
|
|
|
|
2021-10-19 14:20:46 +08:00
|
|
|
- Overview
|
2021-01-15 14:38:36 +08:00
|
|
|
|
|
|
|
<img src="./figs/msg_stream_input_output.jpeg" width=700>
|
|
|
|
|
2021-10-19 14:20:46 +08:00
|
|
|
- Interface
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-10-19 14:20:46 +08:00
|
|
|
```go
|
2021-02-02 16:32:15 +08:00
|
|
|
// Msg
|
|
|
|
|
2021-01-04 14:16:43 +08:00
|
|
|
type MsgType uint32
|
2021-04-12 12:45:38 +08:00
|
|
|
const (
|
|
|
|
MsgType_Undefined MsgType = 0
|
|
|
|
// DEFINITION REQUESTS: COLLECTION
|
|
|
|
MsgType_CreateCollection MsgType = 100
|
|
|
|
MsgType_DropCollection MsgType = 101
|
|
|
|
MsgType_HasCollection MsgType = 102
|
|
|
|
MsgType_DescribeCollection MsgType = 103
|
|
|
|
MsgType_ShowCollections MsgType = 104
|
|
|
|
MsgType_GetSystemConfigs MsgType = 105
|
|
|
|
MsgType_LoadCollection MsgType = 106
|
|
|
|
MsgType_ReleaseCollection MsgType = 107
|
2021-09-19 09:25:54 +08:00
|
|
|
MsgType_CreateAlias MsgType = 108
|
|
|
|
MsgType_DropAlias MsgType = 109
|
|
|
|
MsgType_AlterAlias MsgType = 110
|
2021-04-12 12:45:38 +08:00
|
|
|
// DEFINITION REQUESTS: PARTITION
|
|
|
|
MsgType_CreatePartition MsgType = 200
|
|
|
|
MsgType_DropPartition MsgType = 201
|
|
|
|
MsgType_HasPartition MsgType = 202
|
|
|
|
MsgType_DescribePartition MsgType = 203
|
|
|
|
MsgType_ShowPartitions MsgType = 204
|
|
|
|
MsgType_LoadPartitions MsgType = 205
|
|
|
|
MsgType_ReleasePartitions MsgType = 206
|
|
|
|
// DEFINE REQUESTS: SEGMENT
|
2021-09-19 09:25:54 +08:00
|
|
|
MsgType_ShowSegments MsgType = 250
|
|
|
|
MsgType_DescribeSegment MsgType = 251
|
|
|
|
MsgType_LoadSegments MsgType = 252
|
|
|
|
MsgType_ReleaseSegments MsgType = 253
|
|
|
|
MsgType_HandoffSegments MsgType = 254
|
|
|
|
MsgType_LoadBalanceSegments MsgType = 255
|
2021-04-12 12:45:38 +08:00
|
|
|
// DEFINITION REQUESTS: INDEX
|
|
|
|
MsgType_CreateIndex MsgType = 300
|
|
|
|
MsgType_DescribeIndex MsgType = 301
|
|
|
|
MsgType_DropIndex MsgType = 302
|
|
|
|
// MANIPULATION REQUESTS
|
|
|
|
MsgType_Insert MsgType = 400
|
|
|
|
MsgType_Delete MsgType = 401
|
|
|
|
MsgType_Flush MsgType = 402
|
|
|
|
// QUERY
|
|
|
|
MsgType_Search MsgType = 500
|
|
|
|
MsgType_SearchResult MsgType = 501
|
|
|
|
MsgType_GetIndexState MsgType = 502
|
2021-09-19 09:25:54 +08:00
|
|
|
MsgType_GetIndexBuildProgress MsgType = 503
|
|
|
|
MsgType_GetCollectionStatistics MsgType = 504
|
|
|
|
MsgType_GetPartitionStatistics MsgType = 505
|
|
|
|
MsgType_Retrieve MsgType = 506
|
|
|
|
MsgType_RetrieveResult MsgType = 507
|
|
|
|
MsgType_WatchDmChannels MsgType = 508
|
|
|
|
MsgType_RemoveDmChannels MsgType = 509
|
|
|
|
MsgType_WatchQueryChannels MsgType = 510
|
|
|
|
MsgType_RemoveQueryChannels MsgType = 511
|
2021-04-12 12:45:38 +08:00
|
|
|
// DATA SERVICE
|
|
|
|
MsgType_SegmentInfo MsgType = 600
|
2021-11-12 19:15:29 +08:00
|
|
|
MsgType_SystemInfo MsgType = 601
|
2021-04-12 12:45:38 +08:00
|
|
|
// SYSTEM CONTROL
|
|
|
|
MsgType_TimeTick MsgType = 1200
|
|
|
|
MsgType_QueryNodeStats MsgType = 1201
|
|
|
|
MsgType_LoadIndex MsgType = 1202
|
|
|
|
MsgType_RequestID MsgType = 1203
|
|
|
|
MsgType_RequestTSO MsgType = 1204
|
|
|
|
MsgType_AllocateSegment MsgType = 1205
|
|
|
|
MsgType_SegmentStatistics MsgType = 1206
|
|
|
|
MsgType_SegmentFlushDone MsgType = 1207
|
2021-09-19 09:25:54 +08:00
|
|
|
MsgType_DataNodeTt MsgType = 1208
|
2021-04-12 12:45:38 +08:00
|
|
|
)
|
2021-01-04 14:16:43 +08:00
|
|
|
|
2021-03-04 10:35:28 +08:00
|
|
|
type MsgPosition struct{
|
2021-04-12 12:45:38 +08:00
|
|
|
ChannelName string
|
|
|
|
MsgID []byte
|
|
|
|
MsgGroup string
|
|
|
|
Timestamp uint64
|
2021-01-18 10:09:17 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:16:43 +08:00
|
|
|
type MsgPack struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
BeginTs Timestamp
|
|
|
|
EndTs Timestamp
|
|
|
|
Msgs []TsMsg
|
|
|
|
StartPositions []*MsgPosition
|
|
|
|
EndPositions []*MsgPosition
|
2021-01-04 14:16:43 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:35:28 +08:00
|
|
|
type TsMsg interface {
|
2021-04-12 12:45:38 +08:00
|
|
|
TraceCtx() context.Context
|
|
|
|
SetTraceCtx(ctx context.Context)
|
|
|
|
ID() UniqueID
|
|
|
|
BeginTs() Timestamp
|
|
|
|
EndTs() Timestamp
|
|
|
|
Type() MsgType
|
2021-09-22 16:13:54 +08:00
|
|
|
SourceID() int64
|
2021-04-12 12:45:38 +08:00
|
|
|
HashKeys() []uint32
|
|
|
|
Marshal(TsMsg) (MarshalType, error)
|
|
|
|
Unmarshal(MarshalType) (TsMsg, error)
|
|
|
|
Position() *MsgPosition
|
|
|
|
SetPosition(*MsgPosition)
|
2021-03-04 10:35:28 +08:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:45:38 +08:00
|
|
|
type RepackFunc func(msgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error)
|
2021-02-02 16:32:15 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Unmarshal
|
|
|
|
|
|
|
|
// Interface
|
2021-03-04 10:35:28 +08:00
|
|
|
type UnmarshalFunc func(interface{}) (TsMsg, error)
|
2021-02-02 16:32:15 +08:00
|
|
|
|
2021-12-31 14:16:12 +08:00
|
|
|
// UnmarshalDispatcher is an interface that contains method Unmarshal
|
2021-02-02 16:32:15 +08:00
|
|
|
type UnmarshalDispatcher interface {
|
2021-04-12 12:45:38 +08:00
|
|
|
Unmarshal(input interface{}, msgType commonpb.MsgType) (TsMsg, error)
|
|
|
|
AddMsgTemplate(msgType commonpb.MsgType, unmarshalFunc UnmarshalFunc)
|
2021-02-02 16:32:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type UnmarshalDispatcherFactory interface {
|
2021-04-12 12:45:38 +08:00
|
|
|
NewUnmarshalDispatcher() *UnmarshalDispatcher
|
2021-02-02 16:32:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proto & Mem Implementation
|
|
|
|
type ProtoUDFactory struct {}
|
2021-03-04 10:35:28 +08:00
|
|
|
func (pudf *ProtoUDFactory) NewUnmarshalDispatcher() *ProtoUnmarshalDispatcher
|
2021-02-02 16:32:15 +08:00
|
|
|
|
2021-03-04 10:35:28 +08:00
|
|
|
// TODO
|
2021-02-02 16:32:15 +08:00
|
|
|
type MemUDFactory struct {}
|
|
|
|
func (mudf *MemUDFactory) NewUnmarshalDispatcher() *UnmarshalDispatcher
|
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
2021-11-12 13:21:22 +08:00
|
|
|
// MsgStream is an interface that can be used to produce and consume message on message queue
|
2021-02-02 16:32:15 +08:00
|
|
|
|
|
|
|
// Interface
|
2021-01-04 14:16:43 +08:00
|
|
|
type MsgStream interface {
|
2021-04-12 12:45:38 +08:00
|
|
|
Start()
|
|
|
|
Close()
|
|
|
|
Chan() <-chan *MsgPack
|
|
|
|
AsProducer(channels []string)
|
|
|
|
AsConsumer(channels []string, subName string)
|
|
|
|
SetRepackFunc(repackFunc RepackFunc)
|
2021-10-03 21:40:14 +08:00
|
|
|
ComputeProduceChannelIndexes(tsMsgs []TsMsg) [][]int32
|
|
|
|
GetProduceChannels() []string
|
|
|
|
Produce(*MsgPack) error
|
|
|
|
Broadcast(*MsgPack) error
|
|
|
|
BroadcastMark(*MsgPack) (map[string][]MessageID, error)
|
|
|
|
Consume() *MsgPack
|
|
|
|
Seek(offset []*MsgPosition) error
|
2021-01-04 14:16:43 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 21:42:02 +08:00
|
|
|
type Factory interface {
|
2022-03-24 10:15:25 +08:00
|
|
|
Init(params *paramtable.ComponentParam) error
|
2021-04-12 12:45:38 +08:00
|
|
|
NewMsgStream(ctx context.Context) (MsgStream, error)
|
|
|
|
NewTtMsgStream(ctx context.Context) (MsgStream, error)
|
2021-02-02 16:32:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pulsar
|
2021-10-04 13:36:06 +08:00
|
|
|
type PmsFactory struct {
|
|
|
|
dispatcherFactory ProtoUDFactory
|
|
|
|
// the following members must be public, so that mapstructure.Decode() can access them
|
|
|
|
PulsarAddress string
|
|
|
|
ReceiveBufSize int64
|
|
|
|
PulsarBufSize int64
|
|
|
|
}
|
2021-02-02 16:32:15 +08:00
|
|
|
|
2021-10-04 13:38:06 +08:00
|
|
|
// RmsFactory
|
|
|
|
type RmsFactory struct {
|
|
|
|
dispatcherFactory ProtoUDFactory
|
|
|
|
ReceiveBufSize int64
|
|
|
|
RmqBufSize int64
|
|
|
|
}
|
2021-02-02 16:32:15 +08:00
|
|
|
```
|
|
|
|
|
2021-10-19 14:20:46 +08:00
|
|
|
````go
|
2021-01-04 14:16:43 +08:00
|
|
|
|
2021-10-07 22:22:55 +08:00
|
|
|
// mqMsgStream
|
|
|
|
type mqMsgStream struct {
|
|
|
|
ctx context.Context
|
|
|
|
client mqclient.Client
|
|
|
|
producers map[string]mqclient.Producer
|
|
|
|
producerChannels []string
|
|
|
|
consumers map[string]mqclient.Consumer
|
|
|
|
consumerChannels []string
|
|
|
|
repackFunc RepackFunc
|
|
|
|
unmarshal UnmarshalDispatcher
|
|
|
|
receiveBuf chan *MsgPack
|
|
|
|
wait *sync.WaitGroup
|
|
|
|
streamCancel func()
|
|
|
|
bufSize int64
|
|
|
|
producerLock *sync.Mutex
|
|
|
|
consumerLock *sync.Mutex
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2021-02-02 16:32:15 +08:00
|
|
|
|
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
#### A.4 RocksMQ
|
|
|
|
|
|
|
|
RocksMQ is a RocksDB-based messaging/streaming library.
|
|
|
|
|
2021-01-22 15:41:54 +08:00
|
|
|
```GO
|
|
|
|
// All the following UniqueIDs are 64-bit integer, which is combined with timestamp and increasing number
|
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
type ProducerMessage struct {
|
2021-04-12 12:45:38 +08:00
|
|
|
payload []byte
|
|
|
|
}
|
2021-01-12 10:52:57 +08:00
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
type ConsumerMessage struct {
|
2021-10-01 20:50:56 +08:00
|
|
|
msgID UniqueID
|
2021-04-12 12:45:38 +08:00
|
|
|
payload []byte
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2021-01-22 15:41:54 +08:00
|
|
|
type IDAllocator interface {
|
|
|
|
Alloc(count uint32) (UniqueID, UniqueID, error)
|
|
|
|
AllocOne() (UniqueID, error)
|
|
|
|
UpdateID() error
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
2021-01-12 10:52:57 +08:00
|
|
|
|
|
|
|
// Every collection has its RocksMQ
|
|
|
|
type RocksMQ struct {
|
2021-09-18 19:12:03 +08:00
|
|
|
store *gorocksdb.DB
|
|
|
|
kv kv.Base
|
2021-01-22 15:41:54 +08:00
|
|
|
idAllocator IDAllocator
|
2021-09-18 19:12:03 +08:00
|
|
|
produceMu sync.Mutex
|
|
|
|
consumeMu sync.Mutex
|
2021-01-22 15:41:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rmq *RocksMQ) CreateChannel(channelName string) error
|
|
|
|
func (rmq *RocksMQ) DestroyChannel(channelName string) error
|
|
|
|
func (rmq *RocksMQ) CreateConsumerGroup(groupName string) error
|
|
|
|
func (rmq *RocksMQ) DestroyConsumerGroup(groupName string) error
|
|
|
|
func (rmq *RocksMQ) Produce(channelName string, messages []ProducerMessage) error
|
|
|
|
func (rmq *RocksMQ) Consume(groupName string, channelName string, n int) ([]ConsumerMessage, error)
|
|
|
|
func (rmq *RocksMQ) Seek(groupName string, channelName string, msgID MessageID) error
|
|
|
|
|
|
|
|
func NewRocksMQ(name string, idAllocator IDAllocator) (*RocksMQ, error)
|
2021-10-19 14:20:46 +08:00
|
|
|
````
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2021-10-14 17:04:56 +08:00
|
|
|
##### A.4.1 Meta (stored in etcd)
|
2021-01-11 18:35:54 +08:00
|
|
|
|
|
|
|
```go
|
2021-01-22 15:41:54 +08:00
|
|
|
// channel meta
|
|
|
|
"$(channel_name)/begin_id", UniqueID
|
|
|
|
"$(channel_name)/end_id", UniqueID
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2021-01-22 15:41:54 +08:00
|
|
|
// consumer group meta
|
|
|
|
"$(group_name)/$(channel_name)/current_id", UniqueID
|
2021-01-11 18:35:54 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 10:52:57 +08:00
|
|
|
##### A.4.2 Data (stored in RocksDB)
|
|
|
|
|
|
|
|
- data
|
|
|
|
|
|
|
|
```go
|
|
|
|
"$(channel_name)/$(unique_id)", []byte
|
2021-04-12 12:45:38 +08:00
|
|
|
```
|