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.

112 lines
3.1 KiB

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