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.

114 lines
3.2 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
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 []pb.ServerAddress
  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. ctx, cancel := context.WithCancel(context.Background())
  39. defer cancel()
  40. stream, err := client.KeepConnected(ctx)
  41. if err != nil {
  42. glog.V(0).Infof("%s:%d failed to keep connected to %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  43. return err
  44. }
  45. initRequest := &filer_pb.KeepConnectedRequest{
  46. Name: broker.option.Ip,
  47. GrpcPort: uint32(broker.option.Port),
  48. }
  49. for _, tp := range broker.topicManager.ListTopicPartitions() {
  50. initRequest.Resources = append(initRequest.Resources, tp.String())
  51. }
  52. if err := stream.Send(&filer_pb.KeepConnectedRequest{
  53. Name: broker.option.Ip,
  54. GrpcPort: uint32(broker.option.Port),
  55. }); err != nil {
  56. glog.V(0).Infof("broker %s:%d failed to init at %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  57. return err
  58. }
  59. // TODO send events of adding/removing topics
  60. glog.V(0).Infof("conntected with filer: %v", filer)
  61. for {
  62. if err := stream.Send(&filer_pb.KeepConnectedRequest{
  63. Name: broker.option.Ip,
  64. GrpcPort: uint32(broker.option.Port),
  65. }); err != nil {
  66. glog.V(0).Infof("%s:%d failed to sendto %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  67. return err
  68. }
  69. // println("send heartbeat")
  70. if _, err := stream.Recv(); err != nil {
  71. glog.V(0).Infof("%s:%d failed to receive from %s: %v", broker.option.Ip, broker.option.Port, filer, err)
  72. return err
  73. }
  74. // println("received reply")
  75. time.Sleep(11 * time.Second)
  76. // println("woke up")
  77. }
  78. return nil
  79. })
  80. time.Sleep(3 * time.Second)
  81. }
  82. }
  83. }
  84. func (broker *MessageBroker) withFilerClient(filer pb.ServerAddress, fn func(filer_pb.SeaweedFilerClient) error) error {
  85. return pb.WithFilerClient(filer, broker.grpcDialOption, fn)
  86. }
  87. func (broker *MessageBroker) withMasterClient(master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
  88. return pb.WithMasterClient(master, broker.grpcDialOption, func(client master_pb.SeaweedClient) error {
  89. return fn(client)
  90. })
  91. }