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.

116 lines
3.1 KiB

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