publisher.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package pub_client
  2. import (
  3. "github.com/rdleal/intervalst/interval"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util/buffered_queue"
  9. "google.golang.org/grpc"
  10. "google.golang.org/grpc/credentials/insecure"
  11. "log"
  12. "sync"
  13. )
  14. type PublisherConfiguration struct {
  15. Topic topic.Topic
  16. PartitionCount int32
  17. Brokers []string
  18. PublisherName string // for debugging
  19. RecordType *schema_pb.RecordType
  20. }
  21. type PublishClient struct {
  22. mq_pb.SeaweedMessaging_PublishMessageClient
  23. Broker string
  24. Err error
  25. }
  26. type TopicPublisher struct {
  27. partition2Buffer *interval.SearchTree[*buffered_queue.BufferedQueue[*mq_pb.DataMessage], int32]
  28. grpcDialOption grpc.DialOption
  29. sync.Mutex // protects grpc
  30. config *PublisherConfiguration
  31. jobs []*EachPartitionPublishJob
  32. }
  33. func NewTopicPublisher(config *PublisherConfiguration) *TopicPublisher {
  34. tp := &TopicPublisher{
  35. partition2Buffer: interval.NewSearchTree[*buffered_queue.BufferedQueue[*mq_pb.DataMessage]](func(a, b int32) int {
  36. return int(a - b)
  37. }),
  38. grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
  39. config: config,
  40. }
  41. wg := sync.WaitGroup{}
  42. wg.Add(1)
  43. go func() {
  44. if err := tp.startSchedulerThread(&wg); err != nil {
  45. log.Println(err)
  46. return
  47. }
  48. }()
  49. wg.Wait()
  50. return tp
  51. }
  52. func (p *TopicPublisher) Shutdown() error {
  53. if inputBuffers, found := p.partition2Buffer.AllIntersections(0, pub_balancer.MaxPartitionCount); found {
  54. for _, inputBuffer := range inputBuffers {
  55. inputBuffer.CloseInput()
  56. }
  57. }
  58. for _, job := range p.jobs {
  59. job.wg.Wait()
  60. }
  61. return nil
  62. }