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.

119 lines
3.2 KiB

  1. package broker
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  11. )
  12. /*
  13. Topic discovery:
  14. When pub or sub connects, it ask for the whole broker list, and run consistent hashing to find the broker.
  15. The broker will check peers whether it is already hosted by some other broker, if that broker is alive and acknowledged alive, redirect to it.
  16. Otherwise, just host the topic.
  17. So, if the pub or sub connects around the same time, they would connect to the same broker. Everyone is happy.
  18. If one of the pub or sub connects very late, and the system topo changed quite a bit with new servers added or old servers died, checking peers will help.
  19. */
  20. func (broker *MessageBroker) FindBroker(c context.Context, request *messaging_pb.FindBrokerRequest) (*messaging_pb.FindBrokerResponse, error) {
  21. t := &messaging_pb.FindBrokerResponse{}
  22. var peers []string
  23. targetTopicPartition := fmt.Sprintf(TopicPartitionFmt, request.Namespace, request.Topic, request.Parition)
  24. for _, filer := range broker.option.Filers {
  25. err := broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
  26. resp, err := client.LocateBroker(context.Background(), &filer_pb.LocateBrokerRequest{
  27. Resource: targetTopicPartition,
  28. })
  29. if err != nil {
  30. return err
  31. }
  32. if resp.Found && len(resp.Resources) > 0 {
  33. t.Broker = resp.Resources[0].GrpcAddresses
  34. return nil
  35. }
  36. for _, b := range resp.Resources {
  37. peers = append(peers, b.GrpcAddresses)
  38. }
  39. return nil
  40. })
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. t.Broker = PickMember(peers, []byte(targetTopicPartition))
  46. return t, nil
  47. }
  48. func (broker *MessageBroker) checkFilers() {
  49. // contact a filer about masters
  50. var masters []pb.ServerAddress
  51. found := false
  52. for !found {
  53. for _, filer := range broker.option.Filers {
  54. err := broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
  55. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  56. if err != nil {
  57. return err
  58. }
  59. for _, m := range resp.Masters {
  60. masters = append(masters, pb.ServerAddress(m))
  61. }
  62. return nil
  63. })
  64. if err == nil {
  65. found = true
  66. break
  67. }
  68. glog.V(0).Infof("failed to read masters from %+v: %v", broker.option.Filers, err)
  69. time.Sleep(time.Second)
  70. }
  71. }
  72. glog.V(0).Infof("received master list: %s", masters)
  73. // contact each masters for filers
  74. var filers []pb.ServerAddress
  75. found = false
  76. for !found {
  77. for _, master := range masters {
  78. err := broker.withMasterClient(master, func(client master_pb.SeaweedClient) error {
  79. resp, err := client.ListMasterClients(context.Background(), &master_pb.ListMasterClientsRequest{
  80. ClientType: "filer",
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. filers = append(filers, pb.FromAddressStrings(resp.GrpcAddresses)...)
  86. return nil
  87. })
  88. if err == nil {
  89. found = true
  90. break
  91. }
  92. glog.V(0).Infof("failed to list filers: %v", err)
  93. time.Sleep(time.Second)
  94. }
  95. }
  96. glog.V(0).Infof("received filer list: %s", filers)
  97. broker.option.Filers = filers
  98. }