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.

87 lines
2.3 KiB

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