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.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package broker
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "google.golang.org/grpc"
  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. topicLocks: NewTopicLocks(),
  28. }
  29. go messageBroker.loopForEver()
  30. return messageBroker, nil
  31. }
  32. func (broker *MessageBroker) loopForEver() {
  33. for {
  34. broker.checkPeers()
  35. time.Sleep(3 * time.Second)
  36. }
  37. }
  38. func (broker *MessageBroker) checkPeers() {
  39. // contact a filer about masters
  40. var masters []string
  41. for _, filer := range broker.option.Filers {
  42. err := broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
  43. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  44. if err != nil {
  45. return err
  46. }
  47. masters = append(masters, resp.Masters...)
  48. return nil
  49. })
  50. if err != nil {
  51. fmt.Printf("failed to read masters from %+v: %v\n", broker.option.Filers, err)
  52. return
  53. }
  54. }
  55. // contact each masters for filers
  56. var filers []string
  57. for _, master := range masters {
  58. err := broker.withMasterClient(master, func(client master_pb.SeaweedClient) error {
  59. resp, err := client.ListMasterClients(context.Background(), &master_pb.ListMasterClientsRequest{
  60. ClientType: "filer",
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. fmt.Printf("filers: %+v\n", resp.GrpcAddresses)
  66. filers = append(filers, resp.GrpcAddresses...)
  67. return nil
  68. })
  69. if err != nil {
  70. fmt.Printf("failed to list filers: %v\n", err)
  71. return
  72. }
  73. }
  74. // contact each filer about brokers
  75. for _, filer := range filers {
  76. err := broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
  77. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  78. if err != nil {
  79. return err
  80. }
  81. masters = append(masters, resp.Masters...)
  82. return nil
  83. })
  84. if err != nil {
  85. fmt.Printf("failed to read masters from %+v: %v\n", broker.option.Filers, err)
  86. return
  87. }
  88. }
  89. }
  90. func (broker *MessageBroker) withFilerClient(filer string, fn func(filer_pb.SeaweedFilerClient) error) error {
  91. return pb.WithFilerClient(filer, broker.grpcDialOption, fn)
  92. }
  93. func (broker *MessageBroker) withMasterClient(master string, fn func(client master_pb.SeaweedClient) error) error {
  94. return pb.WithMasterClient(master, broker.grpcDialOption, func(client master_pb.SeaweedClient) error {
  95. return fn(client)
  96. })
  97. }