2021-01-15 14:38:36 +08:00
|
|
|
## 8. Message Stream
|
2020-12-27 09:05:24 +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-01-12 18:03:24 +08:00
|
|
|
CreateChannels(req CreateChannelRequest) (CreateChannelResponse, error)
|
|
|
|
DestoryChannels(req DestoryChannelRequest) error
|
|
|
|
DescribeChannels(req DescribeChannelRequest) (DescribeChannelResponse, error)
|
2020-12-29 18:02:44 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
* *CreateChannels*
|
|
|
|
|
2020-12-27 09:05:24 +08:00
|
|
|
```go
|
|
|
|
type OwnerDescription struct {
|
|
|
|
Role string
|
|
|
|
Address string
|
|
|
|
//Token string
|
|
|
|
DescriptionText string
|
|
|
|
}
|
|
|
|
|
2020-12-29 18:02:44 +08:00
|
|
|
type CreateChannelRequest struct {
|
|
|
|
OwnerDescription OwnerDescription
|
2021-01-12 18:03:24 +08:00
|
|
|
NumChannels int
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateChannelResponse struct {
|
2021-01-14 20:30:27 +08:00
|
|
|
ChannelNames []string
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
* *DestoryChannels*
|
|
|
|
|
|
|
|
```go
|
|
|
|
type DestoryChannelRequest struct {
|
2021-01-14 20:30:27 +08:00
|
|
|
ChannelNames []string
|
2020-12-29 18:02:44 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* *DescribeChannels*
|
|
|
|
|
|
|
|
```go
|
2021-01-12 18:03:24 +08:00
|
|
|
type DescribeChannelRequest struct {
|
2021-01-14 20:30:27 +08:00
|
|
|
ChannelNames []string
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-27 09:05:24 +08:00
|
|
|
type ChannelDescription struct {
|
2021-01-14 20:30:27 +08:00
|
|
|
ChannelName string
|
2020-12-27 09:05:24 +08:00
|
|
|
Owner OwnerDescription
|
|
|
|
}
|
|
|
|
|
2021-01-12 18:03:24 +08:00
|
|
|
type DescribeChannelResponse struct {
|
2020-12-29 18:02:44 +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-01-15 14:38:36 +08:00
|
|
|
* Overview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<img src="./figs/msg_stream_input_output.jpeg" width=700>
|
|
|
|
|
|
|
|
* Interface
|
|
|
|
|
2021-01-04 14:16:43 +08:00
|
|
|
``` go
|
|
|
|
type MsgType uint32
|
|
|
|
const {
|
|
|
|
kInsert MsgType = 400
|
|
|
|
kDelete MsgType = 401
|
|
|
|
kSearch MsgType = 500
|
2021-01-12 18:03:24 +08:00
|
|
|
kSearchResult MsgType = 1000
|
2021-01-04 14:16:43 +08:00
|
|
|
|
|
|
|
kSegStatistics MsgType = 1100
|
|
|
|
|
|
|
|
kTimeTick MsgType = 1200
|
|
|
|
kTimeSync MsgType = 1201
|
|
|
|
}
|
|
|
|
|
|
|
|
type TsMsg interface {
|
|
|
|
SetTs(ts Timestamp)
|
|
|
|
BeginTs() Timestamp
|
|
|
|
EndTs() Timestamp
|
|
|
|
Type() MsgType
|
|
|
|
Marshal(*TsMsg) []byte
|
|
|
|
Unmarshal([]byte) *TsMsg
|
|
|
|
}
|
|
|
|
|
2021-01-18 10:09:17 +08:00
|
|
|
type MsgPosition {
|
|
|
|
ChannelName string
|
|
|
|
MsgID string
|
|
|
|
TimestampFilter Timestamp
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:16:43 +08:00
|
|
|
type MsgPack struct {
|
|
|
|
BeginTs Timestamp
|
|
|
|
EndTs Timestamp
|
|
|
|
Msgs []TsMsg
|
2021-01-18 10:09:17 +08:00
|
|
|
StartPositions []MsgPosition
|
|
|
|
EndPositions []MsgPosition
|
2021-01-04 14:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MsgStream interface {
|
|
|
|
Produce(*MsgPack) error
|
|
|
|
Broadcast(*MsgPack) error
|
|
|
|
Consume() *MsgPack // message can be consumed exactly once
|
2021-01-18 10:09:17 +08:00
|
|
|
ShowChannelNames() []string
|
|
|
|
Seek(offset MsgPosition) error
|
2021-01-04 14:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type RepackFunc(msgs []* TsMsg, hashKeys [][]int32) map[int32] *MsgPack
|
|
|
|
|
|
|
|
type PulsarMsgStream struct {
|
|
|
|
client *pulsar.Client
|
|
|
|
repackFunc RepackFunc
|
|
|
|
producers []*pulsar.Producer
|
|
|
|
consumers []*pulsar.Consumer
|
|
|
|
unmarshal *UnmarshalDispatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *PulsarMsgStream) CreatePulsarProducers(topics []string)
|
|
|
|
func (ms *PulsarMsgStream) CreatePulsarConsumers(subname string, topics []string, unmarshal *UnmarshalDispatcher)
|
|
|
|
func (ms *PulsarMsgStream) SetRepackFunc(repackFunc RepackFunc)
|
|
|
|
func (ms *PulsarMsgStream) Produce(msgs *MsgPack) error
|
|
|
|
func (ms *PulsarMsgStream) Broadcast(msgs *MsgPack) error
|
|
|
|
func (ms *PulsarMsgStream) Consume() (*MsgPack, error)
|
|
|
|
func (ms *PulsarMsgStream) Start() error
|
|
|
|
func (ms *PulsarMsgStream) Close() error
|
|
|
|
|
|
|
|
func NewPulsarMsgStream(ctx context.Context, pulsarAddr string) *PulsarMsgStream
|
|
|
|
|
|
|
|
|
|
|
|
type PulsarTtMsgStream struct {
|
|
|
|
client *pulsar.Client
|
|
|
|
repackFunc RepackFunc
|
|
|
|
producers []*pulsar.Producer
|
|
|
|
consumers []*pulsar.Consumer
|
|
|
|
unmarshal *UnmarshalDispatcher
|
|
|
|
inputBuf []*TsMsg
|
|
|
|
unsolvedBuf []*TsMsg
|
|
|
|
msgPacks []*MsgPack
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *PulsarTtMsgStream) CreatePulsarProducers(topics []string)
|
|
|
|
func (ms *PulsarTtMsgStream) CreatePulsarConsumers(subname string, topics []string, unmarshal *UnmarshalDispatcher)
|
|
|
|
func (ms *PulsarTtMsgStream) SetRepackFunc(repackFunc RepackFunc)
|
|
|
|
func (ms *PulsarTtMsgStream) Produce(msgs *MsgPack) error
|
|
|
|
func (ms *PulsarTtMsgStream) Broadcast(msgs *MsgPack) error
|
|
|
|
func (ms *PulsarTtMsgStream) Consume() *MsgPack //return messages in one time tick
|
|
|
|
func (ms *PulsarTtMsgStream) Start() error
|
|
|
|
func (ms *PulsarTtMsgStream) Close() error
|
|
|
|
|
|
|
|
func NewPulsarTtMsgStream(ctx context.Context, pulsarAddr string) *PulsarTtMsgStream
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
type MarshalFunc func(*TsMsg) []byte
|
|
|
|
type UnmarshalFunc func([]byte) *TsMsg
|
|
|
|
|
|
|
|
|
|
|
|
type UnmarshalDispatcher struct {
|
|
|
|
tempMap map[ReqType]UnmarshalFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dispatcher *MarshalDispatcher) Unmarshal([]byte) *TsMsg
|
|
|
|
func (dispatcher *MarshalDispatcher) AddMsgTemplate(msgType MsgType, marshal MarshalFunc)
|
|
|
|
func (dispatcher *MarshalDispatcher) addDefaultMsgTemplates()
|
|
|
|
|
|
|
|
func NewUnmarshalDispatcher() *UnmarshalDispatcher
|
|
|
|
```
|
|
|
|
|
|
|
|
|
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-01-12 10:52:57 +08:00
|
|
|
payload []byte
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
2021-01-12 10:52:57 +08:00
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
type ConsumerMessage struct {
|
2021-01-22 15:41:54 +08:00
|
|
|
msgID UniqueID
|
2021-01-12 10:52:57 +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-01-22 15:41:54 +08:00
|
|
|
store *gorocksdb.DB
|
|
|
|
kv kv.Base
|
|
|
|
idAllocator IDAllocator
|
|
|
|
produceMu sync.Mutex
|
|
|
|
consumeMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-11 18:35:54 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-12 10:52:57 +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
|
|
|
|
```
|