2021-01-15 06:38:36 +00:00
|
|
|
## 8. Message Stream
|
2020-12-27 01:05:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-04 02:35:28 +00:00
|
|
|
// TODO
|
2021-01-12 10:03:24 +00:00
|
|
|
#### 8.2 Message Stream Service API
|
2020-12-27 01:05:24 +00:00
|
|
|
|
2020-12-29 10:02:44 +00:00
|
|
|
```go
|
|
|
|
type Client interface {
|
2021-01-12 10:03:24 +00:00
|
|
|
CreateChannels(req CreateChannelRequest) (CreateChannelResponse, error)
|
|
|
|
DestoryChannels(req DestoryChannelRequest) error
|
|
|
|
DescribeChannels(req DescribeChannelRequest) (DescribeChannelResponse, error)
|
2020-12-29 10:02:44 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
* *CreateChannels*
|
|
|
|
|
2020-12-27 01:05:24 +00:00
|
|
|
```go
|
|
|
|
type OwnerDescription struct {
|
|
|
|
Role string
|
|
|
|
Address string
|
|
|
|
//Token string
|
|
|
|
DescriptionText string
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:02:44 +00:00
|
|
|
type CreateChannelRequest struct {
|
|
|
|
OwnerDescription OwnerDescription
|
2021-01-12 10:03:24 +00:00
|
|
|
NumChannels int
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateChannelResponse struct {
|
2021-01-14 12:30:27 +00:00
|
|
|
ChannelNames []string
|
2021-01-12 10:03:24 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
* *DestoryChannels*
|
|
|
|
|
|
|
|
```go
|
|
|
|
type DestoryChannelRequest struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
ChannelNames []string
|
2020-12-29 10:02:44 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* *DescribeChannels*
|
|
|
|
|
|
|
|
```go
|
2021-01-12 10:03:24 +00:00
|
|
|
type DescribeChannelRequest struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
ChannelNames []string
|
2021-01-12 10:03:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 01:05:24 +00:00
|
|
|
type ChannelDescription struct {
|
2021-01-14 12:30:27 +00:00
|
|
|
ChannelName string
|
2020-12-27 01:05:24 +00:00
|
|
|
Owner OwnerDescription
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:03:24 +00:00
|
|
|
type DescribeChannelResponse struct {
|
2020-12-29 10:02:44 +00:00
|
|
|
Descriptions []ChannelDescription
|
2020-12-27 01:05:24 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-04 06:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
#### A.3 Message Stream
|
|
|
|
|
2021-01-15 06:38:36 +00:00
|
|
|
* Overview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<img src="./figs/msg_stream_input_output.jpeg" width=700>
|
|
|
|
|
|
|
|
* Interface
|
|
|
|
|
2021-01-04 06:16:43 +00:00
|
|
|
``` go
|
2021-02-02 08:32:15 +00:00
|
|
|
// Msg
|
|
|
|
|
2021-01-04 06:16:43 +00:00
|
|
|
type MsgType uint32
|
|
|
|
const {
|
2021-03-04 02:35:28 +00:00
|
|
|
MsgType_kNone MsgType = 0
|
|
|
|
// Definition Requests: collection
|
|
|
|
MsgType_kCreateCollection MsgType = 100
|
|
|
|
MsgType_kDropCollection MsgType = 101
|
|
|
|
MsgType_kHasCollection MsgType = 102
|
|
|
|
MsgType_kDescribeCollection MsgType = 103
|
|
|
|
MsgType_kShowCollections MsgType = 104
|
|
|
|
MsgType_kGetSysConfigs MsgType = 105
|
|
|
|
MsgType_kLoadCollection MsgType = 106
|
|
|
|
MsgType_kReleaseCollection MsgType = 107
|
|
|
|
// Definition Requests: partition
|
|
|
|
MsgType_kCreatePartition MsgType = 200
|
|
|
|
MsgType_kDropPartition MsgType = 201
|
|
|
|
MsgType_kHasPartition MsgType = 202
|
|
|
|
MsgType_kDescribePartition MsgType = 203
|
|
|
|
MsgType_kShowPartitions MsgType = 204
|
|
|
|
MsgType_kLoadPartition MsgType = 205
|
|
|
|
MsgType_kReleasePartition MsgType = 206
|
|
|
|
// Define Requests: segment
|
|
|
|
MsgType_kShowSegment MsgType = 250
|
|
|
|
MsgType_kDescribeSegment MsgType = 251
|
|
|
|
// Definition Requests: Index
|
|
|
|
MsgType_kCreateIndex MsgType = 300
|
|
|
|
MsgType_kDescribeIndex MsgType = 301
|
|
|
|
MsgType_kDropIndex MsgType = 302
|
|
|
|
// Manipulation Requests
|
|
|
|
MsgType_kInsert MsgType = 400
|
|
|
|
MsgType_kDelete MsgType = 401
|
|
|
|
MsgType_kFlush MsgType = 402
|
|
|
|
// Query
|
|
|
|
MsgType_kSearch MsgType = 500
|
|
|
|
MsgType_kSearchResult MsgType = 501
|
|
|
|
MsgType_kGetIndexState MsgType = 502
|
|
|
|
MsgType_kGetCollectionStatistics MsgType = 503
|
|
|
|
MsgType_kGetPartitionStatistics MsgType = 504
|
|
|
|
// Data Service
|
|
|
|
MsgType_kSegmentInfo MsgType = 600
|
|
|
|
// System Control
|
|
|
|
MsgType_kTimeTick MsgType = 1200
|
|
|
|
MsgType_kQueryNodeStats MsgType = 1201
|
|
|
|
MsgType_kLoadIndex MsgType = 1202
|
|
|
|
MsgType_kRequestID MsgType = 1203
|
|
|
|
MsgType_kRequestTSO MsgType = 1204
|
|
|
|
MsgType_kAllocateSegment MsgType = 1205
|
|
|
|
MsgType_kSegmentStatistics MsgType = 1206
|
|
|
|
MsgType_kSegmentFlushDone MsgType = 1207
|
2021-01-04 06:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 02:35:28 +00:00
|
|
|
type MsgPosition struct{
|
2021-01-18 02:09:17 +00:00
|
|
|
ChannelName string
|
2021-03-04 02:35:28 +00:00
|
|
|
MsgID string
|
|
|
|
Timestamp uint64
|
2021-01-18 02:09:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 06:16:43 +00:00
|
|
|
type MsgPack struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
BeginTs Timestamp
|
|
|
|
EndTs Timestamp
|
|
|
|
Msgs []TsMsg
|
|
|
|
StartPositions []*MsgPosition
|
|
|
|
endPositions []*MsgPosition
|
2021-01-04 06:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 02:35:28 +00:00
|
|
|
type TsMsg interface {
|
|
|
|
ID() UniqueID
|
|
|
|
BeginTs() Timestamp
|
|
|
|
EndTs() Timestamp
|
|
|
|
Type() MsgType
|
|
|
|
HashKeys() []uint32
|
|
|
|
Marshal(TsMsg) (MarshalType, error)
|
|
|
|
Unmarshal(MarshalType) (TsMsg, error)
|
|
|
|
Position() *MsgPosition
|
|
|
|
SetPosition(*MsgPosition)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type RepackFunc(msgs []TsMsg, hashKeys [][]int32) (map[int32]*MsgPack, error)
|
2021-02-02 08:32:15 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Unmarshal
|
|
|
|
|
|
|
|
// Interface
|
2021-03-04 02:35:28 +00:00
|
|
|
type UnmarshalFunc func(interface{}) (TsMsg, error)
|
2021-02-02 08:32:15 +00:00
|
|
|
|
|
|
|
type UnmarshalDispatcher interface {
|
2021-03-04 02:35:28 +00:00
|
|
|
Unmarshal(input interface{}, msgType commonpb.MsgType) (TsMsg, error)
|
|
|
|
AddMsgTemplate(msgType commonpb.MsgType, unmarshalFunc UnmarshalFunc)
|
2021-02-02 08:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UnmarshalDispatcherFactory interface {
|
|
|
|
NewUnmarshalDispatcher() *UnmarshalDispatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proto & Mem Implementation
|
|
|
|
type ProtoUDFactory struct {}
|
2021-03-04 02:35:28 +00:00
|
|
|
func (pudf *ProtoUDFactory) NewUnmarshalDispatcher() *ProtoUnmarshalDispatcher
|
2021-02-02 08:32:15 +00:00
|
|
|
|
2021-03-04 02:35:28 +00:00
|
|
|
// TODO
|
2021-02-02 08:32:15 +00:00
|
|
|
type MemUDFactory struct {}
|
|
|
|
func (mudf *MemUDFactory) NewUnmarshalDispatcher() *UnmarshalDispatcher
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
// MsgStream
|
|
|
|
|
|
|
|
// Interface
|
2021-01-04 06:16:43 +00:00
|
|
|
type MsgStream interface {
|
2021-03-04 02:35:28 +00:00
|
|
|
Start()
|
|
|
|
Close()
|
|
|
|
Chan() <-chan *MsgPack
|
|
|
|
AsProducer(channels []string)
|
|
|
|
AsConsumer(channels []string, subName string)
|
|
|
|
SetRepackFunc(repackFunc RepackFunc)
|
|
|
|
|
|
|
|
Produce(context.Context, *MsgPack) error
|
|
|
|
Broadcast(context.Context, *MsgPack) error
|
|
|
|
Consume() (*MsgPack, context.Context)
|
|
|
|
Seek(offset *MsgPosition) error
|
2021-01-04 06:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
type MsgStreamFactory interface {
|
2021-03-04 02:35:28 +00:00
|
|
|
SetParams(params map[string]interface{}) error
|
|
|
|
NewMsgStream(ctx context.Context) (MsgStream, error)
|
|
|
|
NewTtMsgStream(ctx context.Context) (MsgStream, error)
|
2021-02-02 08:32:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 02:35:28 +00:00
|
|
|
//TODO
|
2021-02-02 08:32:15 +00:00
|
|
|
// Pulsar
|
|
|
|
type PulsarMsgStreamFactory interface {}
|
|
|
|
func (pmsf *PulsarMsgStreamFactory) NewMsgStream() *MsgStream
|
|
|
|
func (pmsf *PulsarMsgStreamFactory) NewTtMsgStream() *MsgStream
|
|
|
|
|
2021-03-04 02:35:28 +00:00
|
|
|
//TODO
|
2021-02-02 08:32:15 +00:00
|
|
|
// RockMQ
|
|
|
|
type RmqMsgStreamFactory interface {}
|
|
|
|
func (rmsf *RmqMsgStreamFactory) NewMsgStream() *MsgStream
|
|
|
|
func (rmsf *RmqMsgStreamFactory) NewTtMsgStream() *MsgStream
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
// PulsarMsgStream
|
2021-01-04 06:16:43 +00:00
|
|
|
|
|
|
|
type PulsarMsgStream struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
ctx context.Context
|
|
|
|
client pulsar.Client
|
|
|
|
producers []Producer
|
|
|
|
consumers []Consumer
|
|
|
|
consumerChannels []string
|
|
|
|
repackFunc RepackFunc
|
|
|
|
unmarshal UnmarshalDispatcher
|
|
|
|
receiveBuf chan *MsgPack
|
|
|
|
wait *sync.WaitGroup
|
|
|
|
streamCancel func()
|
|
|
|
pulsarBufSize int64
|
|
|
|
consumerLock *sync.Mutex
|
|
|
|
consumerReflects []reflect.SelectCase
|
|
|
|
|
|
|
|
scMap *sync.Map
|
2021-01-04 06:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *PulsarMsgStream) Start() error
|
|
|
|
func (ms *PulsarMsgStream) Close() error
|
|
|
|
func (ms *PulsarMsgStream) AsProducer(channels []string)
|
|
|
|
func (ms *PulsarMsgStream) AsConsumer(channels []string, subName string)
|
2021-03-04 02:35:28 +00:00
|
|
|
func (ms *PulsarMsgStream) Produce(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *PulsarMsgStream) Broadcast(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *PulsarMsgStream) Consume() (*MsgPack, context.Context)
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *PulsarMsgStream) Seek(mp *MsgPosition) error
|
|
|
|
func (ms *PulsarMsgStream) SetRepackFunc(repackFunc RepackFunc)
|
2021-01-04 06:16:43 +00:00
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
func NewPulsarMsgStream(ctx context.Context, pulsarAddr string, bufferSize int64) *PulsarMsgStream
|
2021-01-04 06:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
type PulsarTtMsgStream struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
client *pulsar.Client
|
|
|
|
repackFunc RepackFunc
|
|
|
|
producers []*pulsar.Producer
|
|
|
|
consumers []*pulsar.Consumer
|
|
|
|
unmarshal *UnmarshalDispatcher
|
|
|
|
inputBuf []*TsMsg
|
2021-01-04 06:16:43 +00:00
|
|
|
unsolvedBuf []*TsMsg
|
2021-03-04 02:35:28 +00:00
|
|
|
msgPacks []*MsgPack
|
2021-01-04 06:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *PulsarTtMsgStream) Start() error
|
|
|
|
func (ms *PulsarTtMsgStream) Close() error
|
|
|
|
func (ms *PulsarTtMsgStream) AsProducer(channels []string)
|
|
|
|
func (ms *PulsarTtMsgStream) AsConsumer(channels []string, subName string)
|
2021-03-04 02:35:28 +00:00
|
|
|
func (ms *PulsarTtMsgStream) Produce(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *PulsarTtMsgStream) Broadcast(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *PulsarTtMsgStream) Consume() (*MsgPack, context.Context) //return messages in one time tick
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *PulsarTtMsgStream) Seek(mp *MsgPosition) error
|
|
|
|
func (ms *PulsarTtMsgStream) SetRepackFunc(repackFunc RepackFunc)
|
2021-01-04 06:16:43 +00:00
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
func NewPulsarTtMsgStream(ctx context.Context, pulsarAddr string, bufferSize int64) *PulsarTtMsgStream
|
2021-01-04 06:16:43 +00:00
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
// RmqMsgStream
|
2021-01-04 06:16:43 +00:00
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
type RmqMsgStream struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
client *rockermq.RocksMQ
|
2021-02-02 08:32:15 +00:00
|
|
|
repackFunc RepackFunc
|
2021-03-04 02:35:28 +00:00
|
|
|
producers []string
|
|
|
|
consumers []string
|
|
|
|
subName string
|
|
|
|
unmarshal *UnmarshalDispatcher
|
2021-01-04 06:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *RmqMsgStream) Start() error
|
|
|
|
func (ms *RmqMsgStream) Close() error
|
|
|
|
func (ms *RmqMsgStream) AsProducer(channels []string)
|
|
|
|
func (ms *RmqMsgStream) AsConsumer(channels []string, subName string)
|
2021-03-04 02:35:28 +00:00
|
|
|
func (ms *RmqMsgStream) Produce(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *RmqMsgStream) Broadcast(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *RmqMsgStream) Consume() (*MsgPack, context.Context)
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *RmqMsgStream) Seek(mp *MsgPosition) error
|
|
|
|
func (ms *RmqMsgStream) SetRepackFunc(repackFunc RepackFunc)
|
|
|
|
|
|
|
|
func NewRmqMsgStream(ctx context.Context) *RmqMsgStream
|
|
|
|
|
|
|
|
type RmqTtMsgStream struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
client *rockermq.RocksMQ
|
2021-02-02 08:32:15 +00:00
|
|
|
repackFunc RepackFunc
|
2021-03-04 02:35:28 +00:00
|
|
|
producers []string
|
|
|
|
consumers []string
|
|
|
|
subName string
|
|
|
|
unmarshal *UnmarshalDispatcher
|
2021-02-02 08:32:15 +00:00
|
|
|
}
|
2021-01-04 06:16:43 +00:00
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *RmqTtMsgStream) Start() error
|
|
|
|
func (ms *RmqTtMsgStream) Close() error
|
|
|
|
func (ms *RmqTtMsgStream) AsProducer(channels []string)
|
|
|
|
func (ms *RmqTtMsgStream) AsConsumer(channels []string, subName string)
|
2021-03-04 02:35:28 +00:00
|
|
|
func (ms *RmqTtMsgStream) Produce(ctx context.Context, msgs *MsgPack) error
|
|
|
|
func (ms *RmqTtMsgStream) Broadcast(ctx conext.Context) msgs *MsgPack) error
|
|
|
|
func (ms *RmqTtMsgStream) Consume() (*MsgPack, context.Context)
|
2021-02-02 08:32:15 +00:00
|
|
|
func (ms *RmqTtMsgStream) Seek(mp *MsgPosition) error
|
|
|
|
func (ms *RmqTtMsgStream) SetRepackFunc(repackFunc RepackFunc)
|
|
|
|
|
|
|
|
func NewRmqTtMsgStream(ctx context.Context) *RmqTtMsgStream
|
2021-01-04 06:16:43 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
2021-01-11 10:35:54 +00:00
|
|
|
|
2021-02-02 08:32:15 +00:00
|
|
|
|
|
|
|
|
2021-01-11 10:35:54 +00:00
|
|
|
#### A.4 RocksMQ
|
|
|
|
|
|
|
|
RocksMQ is a RocksDB-based messaging/streaming library.
|
|
|
|
|
2021-01-22 07:41:54 +00:00
|
|
|
```GO
|
|
|
|
// All the following UniqueIDs are 64-bit integer, which is combined with timestamp and increasing number
|
|
|
|
|
2021-01-11 10:35:54 +00:00
|
|
|
type ProducerMessage struct {
|
2021-01-12 02:52:57 +00:00
|
|
|
payload []byte
|
2021-01-11 10:35:54 +00:00
|
|
|
}
|
2021-01-12 02:52:57 +00:00
|
|
|
|
2021-01-11 10:35:54 +00:00
|
|
|
type ConsumerMessage struct {
|
2021-03-04 02:35:28 +00:00
|
|
|
msgID UniqueID
|
2021-01-12 02:52:57 +00:00
|
|
|
payload []byte
|
2021-01-11 10:35:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 07:41:54 +00:00
|
|
|
type IDAllocator interface {
|
|
|
|
Alloc(count uint32) (UniqueID, UniqueID, error)
|
|
|
|
AllocOne() (UniqueID, error)
|
|
|
|
UpdateID() error
|
2021-01-11 10:35:54 +00:00
|
|
|
}
|
2021-01-12 02:52:57 +00:00
|
|
|
|
|
|
|
// Every collection has its RocksMQ
|
|
|
|
type RocksMQ struct {
|
2021-01-22 07:41:54 +00: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 10:35:54 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-12 02:52:57 +00:00
|
|
|
##### A.4.1 Meta (stored in Etcd)
|
2021-01-11 10:35:54 +00:00
|
|
|
|
|
|
|
```go
|
2021-01-22 07:41:54 +00:00
|
|
|
// channel meta
|
|
|
|
"$(channel_name)/begin_id", UniqueID
|
|
|
|
"$(channel_name)/end_id", UniqueID
|
2021-01-11 10:35:54 +00:00
|
|
|
|
2021-01-22 07:41:54 +00:00
|
|
|
// consumer group meta
|
|
|
|
"$(group_name)/$(channel_name)/current_id", UniqueID
|
2021-01-11 10:35:54 +00:00
|
|
|
```
|
|
|
|
|
2021-01-12 02:52:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
##### A.4.2 Data (stored in RocksDB)
|
|
|
|
|
|
|
|
- data
|
|
|
|
|
|
|
|
```go
|
|
|
|
"$(channel_name)/$(unique_id)", []byte
|
|
|
|
```
|