broker_grpc_sub_coordinator.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package broker
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/sub_coordinator"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. )
  10. // SubscriberToSubCoordinator coordinates the subscribers
  11. func (b *MessageQueueBroker) SubscriberToSubCoordinator(stream mq_pb.SeaweedMessaging_SubscriberToSubCoordinatorServer) error {
  12. if !b.isLockOwner() {
  13. return status.Errorf(codes.Unavailable, "not current broker balancer")
  14. }
  15. req, err := stream.Recv()
  16. if err != nil {
  17. return err
  18. }
  19. var cgi *sub_coordinator.ConsumerGroupInstance
  20. var cg *sub_coordinator.ConsumerGroup
  21. // process init message
  22. initMessage := req.GetInit()
  23. if initMessage != nil {
  24. cg, cgi, err = b.SubCoordinator.AddSubscriber(initMessage)
  25. if err != nil {
  26. return status.Errorf(codes.InvalidArgument, err.Error())
  27. }
  28. glog.V(0).Infof("subscriber %s/%s/%s connected", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic)
  29. } else {
  30. return status.Errorf(codes.InvalidArgument, "subscriber init message is empty")
  31. }
  32. defer func() {
  33. b.SubCoordinator.RemoveSubscriber(initMessage)
  34. glog.V(0).Infof("subscriber %s/%s/%s disconnected: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
  35. }()
  36. ctx := stream.Context()
  37. go func() {
  38. // process ack messages
  39. for {
  40. req, err := stream.Recv()
  41. if err != nil {
  42. glog.V(0).Infof("subscriber %s/%s/%s receive: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
  43. }
  44. if ackUnAssignment := req.GetAckUnAssignment(); ackUnAssignment != nil {
  45. glog.V(0).Infof("subscriber %s/%s/%s ack close of %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, ackUnAssignment)
  46. cg.AckUnAssignment(cgi, ackUnAssignment)
  47. }
  48. if ackAssignment := req.GetAckAssignment(); ackAssignment != nil {
  49. glog.V(0).Infof("subscriber %s/%s/%s ack assignment %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, ackAssignment)
  50. cg.AckAssignment(cgi, ackAssignment)
  51. }
  52. select {
  53. case <-ctx.Done():
  54. err := ctx.Err()
  55. if err == context.Canceled {
  56. // Client disconnected
  57. return
  58. }
  59. return
  60. default:
  61. // Continue processing the request
  62. }
  63. }
  64. }()
  65. // send commands to subscriber
  66. for {
  67. select {
  68. case <-ctx.Done():
  69. err := ctx.Err()
  70. if err == context.Canceled {
  71. // Client disconnected
  72. return err
  73. }
  74. glog.V(0).Infof("subscriber %s/%s/%s disconnected: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
  75. return err
  76. case message := <-cgi.ResponseChan:
  77. glog.V(0).Infof("subscriber %s/%s/%s send: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, message)
  78. if err := stream.Send(message); err != nil {
  79. glog.V(0).Infof("subscriber %s/%s/%s send: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
  80. }
  81. }
  82. }
  83. }