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.

122 lines
2.8 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
  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. Port int
  16. Cipher bool
  17. }
  18. type MessageBroker struct {
  19. option *MessageBrokerOption
  20. grpcDialOption grpc.DialOption
  21. topicLocks *TopicLocks
  22. }
  23. func NewMessageBroker(option *MessageBrokerOption, grpcDialOption grpc.DialOption) (messageBroker *MessageBroker, err error) {
  24. messageBroker = &MessageBroker{
  25. option: option,
  26. grpcDialOption: grpcDialOption,
  27. }
  28. messageBroker.topicLocks = NewTopicLocks(messageBroker)
  29. messageBroker.checkPeers()
  30. // go messageBroker.loopForEver()
  31. return messageBroker, nil
  32. }
  33. func (broker *MessageBroker) loopForEver() {
  34. for {
  35. broker.checkPeers()
  36. time.Sleep(3 * time.Second)
  37. }
  38. }
  39. func (broker *MessageBroker) checkPeers() {
  40. // contact a filer about masters
  41. var masters []string
  42. found := false
  43. for !found {
  44. for _, filer := range broker.option.Filers {
  45. err := broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
  46. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  47. if err != nil {
  48. return err
  49. }
  50. masters = append(masters, resp.Masters...)
  51. return nil
  52. })
  53. if err == nil {
  54. found = true
  55. break
  56. }
  57. glog.V(0).Infof("failed to read masters from %+v: %v", broker.option.Filers, err)
  58. time.Sleep(time.Second)
  59. }
  60. }
  61. glog.V(0).Infof("received master list: %s", masters)
  62. // contact each masters for filers
  63. var filers []string
  64. found = false
  65. for !found {
  66. for _, master := range masters {
  67. err := broker.withMasterClient(master, func(client master_pb.SeaweedClient) error {
  68. resp, err := client.ListMasterClients(context.Background(), &master_pb.ListMasterClientsRequest{
  69. ClientType: "filer",
  70. })
  71. if err != nil {
  72. return err
  73. }
  74. filers = append(filers, resp.GrpcAddresses...)
  75. return nil
  76. })
  77. if err == nil {
  78. found = true
  79. break
  80. }
  81. glog.V(0).Infof("failed to list filers: %v", err)
  82. time.Sleep(time.Second)
  83. }
  84. }
  85. glog.V(0).Infof("received filer list: %s", filers)
  86. broker.option.Filers = filers
  87. }
  88. func (broker *MessageBroker) withFilerClient(filer string, fn func(filer_pb.SeaweedFilerClient) error) error {
  89. return pb.WithFilerClient(filer, broker.grpcDialOption, fn)
  90. }
  91. func (broker *MessageBroker) withMasterClient(master string, fn func(client master_pb.SeaweedClient) error) error {
  92. return pb.WithMasterClient(master, broker.grpcDialOption, func(client master_pb.SeaweedClient) error {
  93. return fn(client)
  94. })
  95. }