broker_server.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }