broker_server.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 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 string, fn func(filer_pb.SeaweedFilerClient) error) error {
  85. return pb.WithFilerClient(filer, broker.grpcDialOption, fn)
  86. }
  87. func (broker *MessageBroker) withMasterClient(master string, 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. }