You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB

5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
  1. package broker
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/cluster"
  5. "github.com/chrislusf/seaweedfs/weed/pb/mq_pb"
  6. "github.com/chrislusf/seaweedfs/weed/wdclient"
  7. "time"
  8. "google.golang.org/grpc"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  13. )
  14. type MessageQueueBrokerOption struct {
  15. Masters map[string]pb.ServerAddress
  16. FilerGroup string
  17. Filers []pb.ServerAddress
  18. DefaultReplication string
  19. MaxMB int
  20. Ip string
  21. Port int
  22. Cipher bool
  23. }
  24. type MessageQueueBroker struct {
  25. mq_pb.UnimplementedSeaweedMessagingServer
  26. option *MessageQueueBrokerOption
  27. grpcDialOption grpc.DialOption
  28. MasterClient *wdclient.MasterClient
  29. topicManager *TopicManager
  30. }
  31. func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.DialOption) (mqBroker *MessageQueueBroker, err error) {
  32. mqBroker = &MessageQueueBroker{
  33. option: option,
  34. grpcDialOption: grpcDialOption,
  35. MasterClient: wdclient.NewMasterClient(grpcDialOption, option.FilerGroup, cluster.BrokerType, pb.NewServerAddress(option.Ip, option.Port, 0), "", option.Masters),
  36. }
  37. mqBroker.topicManager = NewTopicManager(mqBroker)
  38. mqBroker.checkFilers()
  39. go mqBroker.keepConnectedToOneFiler()
  40. go mqBroker.MasterClient.KeepConnectedToMaster()
  41. return mqBroker, nil
  42. }
  43. func (broker *MessageQueueBroker) keepConnectedToOneFiler() {
  44. for {
  45. for _, filer := range broker.option.Filers {
  46. broker.withFilerClient(false, filer, func(client filer_pb.SeaweedFilerClient) error {
  47. ctx, cancel := context.WithCancel(context.Background())
  48. defer cancel()
  49. stream, err := client.KeepConnected(ctx)
  50. if err != nil {
  51. glog.V(0).Infof("%s:%d failed to keep connected to %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  52. return err
  53. }
  54. initRequest := &filer_pb.KeepConnectedRequest{
  55. Name: broker.option.Ip,
  56. GrpcPort: uint32(broker.option.Port),
  57. }
  58. for _, tp := range broker.topicManager.ListTopicPartitions() {
  59. initRequest.Resources = append(initRequest.Resources, tp.String())
  60. }
  61. if err := stream.Send(&filer_pb.KeepConnectedRequest{
  62. Name: broker.option.Ip,
  63. GrpcPort: uint32(broker.option.Port),
  64. }); err != nil {
  65. glog.V(0).Infof("broker %s:%d failed to init at %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  66. return err
  67. }
  68. // TODO send events of adding/removing topics
  69. glog.V(0).Infof("conntected with filer: %v", filer)
  70. for {
  71. if err := stream.Send(&filer_pb.KeepConnectedRequest{
  72. Name: broker.option.Ip,
  73. GrpcPort: uint32(broker.option.Port),
  74. }); err != nil {
  75. glog.V(0).Infof("%s:%d failed to sendto %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  76. return err
  77. }
  78. // println("send heartbeat")
  79. if _, err := stream.Recv(); err != nil {
  80. glog.V(0).Infof("%s:%d failed to receive from %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  81. return err
  82. }
  83. // println("received reply")
  84. time.Sleep(11 * time.Second)
  85. // println("woke up")
  86. }
  87. return nil
  88. })
  89. time.Sleep(3 * time.Second)
  90. }
  91. }
  92. }
  93. func (broker *MessageQueueBroker) withFilerClient(streamingMode bool, filer pb.ServerAddress, fn func(filer_pb.SeaweedFilerClient) error) error {
  94. return pb.WithFilerClient(streamingMode, filer, broker.grpcDialOption, fn)
  95. }
  96. func (broker *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
  97. return pb.WithMasterClient(streamingMode, master, broker.grpcDialOption, func(client master_pb.SeaweedClient) error {
  98. return fn(client)
  99. })
  100. }