seaweedfs/weed/mq/client/pub_client/publish.go

62 lines
1.6 KiB
Go
Raw Normal View History

2023-08-29 00:02:12 +08:00
package pub_client
import (
"fmt"
2024-04-13 14:36:15 +08:00
"github.com/golang/protobuf/proto"
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
2023-12-12 04:05:54 +08:00
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
2023-08-29 00:02:12 +08:00
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
2024-04-13 14:36:15 +08:00
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
2023-08-29 00:02:12 +08:00
"github.com/seaweedfs/seaweedfs/weed/util"
"time"
2023-08-29 00:02:12 +08:00
)
func (p *TopicPublisher) Publish(key, value []byte) error {
2024-05-02 23:48:51 +08:00
if p.config.RecordType != nil {
return fmt.Errorf("record type is set, use PublishRecord instead")
}
return p.doPublish(key, value)
}
func (p *TopicPublisher) doPublish(key, value []byte) error {
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
2023-12-12 04:05:54 +08:00
hashKey := util.HashToInt32(key) % pub_balancer.MaxPartitionCount
2023-08-29 00:02:12 +08:00
if hashKey < 0 {
hashKey = -hashKey
}
inputBuffer, found := p.partition2Buffer.Floor(hashKey+1, hashKey+1)
2023-08-29 00:02:12 +08:00
if !found {
return fmt.Errorf("no input buffer found for key %d", hashKey)
2023-08-29 00:02:12 +08:00
}
return inputBuffer.Enqueue(&mq_pb.DataMessage{
Key: key,
Value: value,
TsNs: time.Now().UnixNano(),
})
2023-08-29 00:02:12 +08:00
}
2024-03-31 16:28:40 +08:00
2024-04-13 14:36:15 +08:00
func (p *TopicPublisher) PublishRecord(key []byte, recordValue *schema_pb.RecordValue) error {
// serialize record value
value, err := proto.Marshal(recordValue)
if err != nil {
return fmt.Errorf("failed to marshal record value: %v", err)
}
2024-05-02 23:48:51 +08:00
return p.doPublish(key, value)
2024-04-13 14:36:15 +08:00
}
2024-03-31 16:28:40 +08:00
func (p *TopicPublisher) FinishPublish() error {
if inputBuffers, found := p.partition2Buffer.AllIntersections(0, pub_balancer.MaxPartitionCount); found {
for _, inputBuffer := range inputBuffers {
inputBuffer.Enqueue(&mq_pb.DataMessage{
2024-05-21 02:03:56 +08:00
TsNs: time.Now().UnixNano(),
2024-04-01 07:35:46 +08:00
Ctrl: &mq_pb.ControlMessage{
IsClose: true,
2024-05-30 14:31:48 +08:00
PublisherName: p.config.PublisherName,
2024-04-01 07:35:46 +08:00
},
2024-03-31 16:28:40 +08:00
})
}
}
return nil
}