messaging: able to pub sub multiple partitions
This commit is contained in:
@@ -3,14 +3,38 @@ package client
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/OneOfOne/xxhash"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
|
||||
)
|
||||
|
||||
type Publisher struct {
|
||||
publishClient messaging_pb.SeaweedMessaging_PublishClient
|
||||
publishClients []messaging_pb.SeaweedMessaging_PublishClient
|
||||
topicConfiguration *messaging_pb.TopicConfiguration
|
||||
messageCount uint64
|
||||
publisherId string
|
||||
}
|
||||
|
||||
func (mc *MessagingClient) NewPublisher(namespace, topic string) (*Publisher, error) {
|
||||
func (mc *MessagingClient) NewPublisher(publisherId, namespace, topic string) (*Publisher, error) {
|
||||
// read topic configuration
|
||||
topicConfiguration := &messaging_pb.TopicConfiguration{
|
||||
PartitionCount: 4,
|
||||
}
|
||||
publishClients := make([]messaging_pb.SeaweedMessaging_PublishClient, topicConfiguration.PartitionCount)
|
||||
for i := 0; i < int(topicConfiguration.PartitionCount); i++ {
|
||||
client, err := mc.setupPublisherClient(namespace, topic, int32(i))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
publishClients[i] = client
|
||||
}
|
||||
return &Publisher{
|
||||
publishClients: publishClients,
|
||||
topicConfiguration: topicConfiguration,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (mc *MessagingClient) setupPublisherClient(namespace, topic string, partition int32) (messaging_pb.SeaweedMessaging_PublishClient, error) {
|
||||
|
||||
stream, err := messaging_pb.NewSeaweedMessagingClient(mc.grpcConnection).Publish(context.Background())
|
||||
if err != nil {
|
||||
@@ -22,7 +46,7 @@ func (mc *MessagingClient) NewPublisher(namespace, topic string) (*Publisher, er
|
||||
Init: &messaging_pb.PublishRequest_InitMessage{
|
||||
Namespace: namespace,
|
||||
Topic: topic,
|
||||
Partition: 0,
|
||||
Partition: partition,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -56,20 +80,34 @@ func (mc *MessagingClient) NewPublisher(namespace, topic string) (*Publisher, er
|
||||
}
|
||||
}()
|
||||
|
||||
return &Publisher{
|
||||
publishClient: stream,
|
||||
}, nil
|
||||
return stream, nil
|
||||
|
||||
}
|
||||
|
||||
func (p *Publisher) Publish(m *messaging_pb.Message) error {
|
||||
hashValue := p.messageCount
|
||||
p.messageCount++
|
||||
if p.topicConfiguration.Partitoning == messaging_pb.TopicConfiguration_NonNullKeyHash {
|
||||
if m.Key != nil {
|
||||
hashValue = xxhash.Checksum64(m.Key)
|
||||
}
|
||||
} else if p.topicConfiguration.Partitoning == messaging_pb.TopicConfiguration_KeyHash {
|
||||
hashValue = xxhash.Checksum64(m.Key)
|
||||
} else {
|
||||
// round robin
|
||||
}
|
||||
|
||||
return p.publishClient.Send(&messaging_pb.PublishRequest{
|
||||
idx := int(hashValue) % len(p.publishClients)
|
||||
if idx < 0 {
|
||||
idx += len(p.publishClients)
|
||||
}
|
||||
return p.publishClients[idx].Send(&messaging_pb.PublishRequest{
|
||||
Data: m,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (p *Publisher) Close() error {
|
||||
|
||||
return p.publishClient.CloseSend()
|
||||
func (p *Publisher) Shutdown() {
|
||||
for _, client := range p.publishClients {
|
||||
client.CloseSend()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user