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

5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
5 years ago
  1. package broker
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  4. "time"
  5. "github.com/seaweedfs/seaweedfs/weed/cluster"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  8. "google.golang.org/grpc"
  9. "github.com/seaweedfs/seaweedfs/weed/pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  12. )
  13. type MessageQueueBrokerOption struct {
  14. Masters map[string]pb.ServerAddress
  15. FilerGroup string
  16. DataCenter string
  17. Rack string
  18. DefaultReplication string
  19. MaxMB int
  20. Ip string
  21. Port int
  22. Cipher bool
  23. }
  24. type MessageQueueBroker struct {
  25. mq_pb.UnimplementedSeaweedMessagingServer
  26. option *MessageQueueBrokerOption
  27. grpcDialOption grpc.DialOption
  28. MasterClient *wdclient.MasterClient
  29. filers map[pb.ServerAddress]struct{}
  30. currentFiler pb.ServerAddress
  31. localTopicManager *topic.LocalTopicManager
  32. }
  33. func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.DialOption) (mqBroker *MessageQueueBroker, err error) {
  34. mqBroker = &MessageQueueBroker{
  35. option: option,
  36. grpcDialOption: grpcDialOption,
  37. MasterClient: wdclient.NewMasterClient(grpcDialOption, option.FilerGroup, cluster.BrokerType, pb.NewServerAddress(option.Ip, option.Port, 0), option.DataCenter, option.Rack, *pb.NewServiceDiscoveryFromMap(option.Masters)),
  38. filers: make(map[pb.ServerAddress]struct{}),
  39. localTopicManager: topic.NewLocalTopicManager(),
  40. }
  41. mqBroker.MasterClient.SetOnPeerUpdateFn(mqBroker.OnBrokerUpdate)
  42. go mqBroker.MasterClient.KeepConnectedToMaster()
  43. existingNodes := cluster.ListExistingPeerUpdates(mqBroker.MasterClient.GetMaster(), grpcDialOption, option.FilerGroup, cluster.FilerType)
  44. for _, newNode := range existingNodes {
  45. mqBroker.OnBrokerUpdate(newNode, time.Now())
  46. }
  47. return mqBroker, nil
  48. }
  49. func (broker *MessageQueueBroker) OnBrokerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
  50. if update.NodeType != cluster.FilerType {
  51. return
  52. }
  53. address := pb.ServerAddress(update.Address)
  54. if update.IsAdd {
  55. broker.filers[address] = struct{}{}
  56. if broker.currentFiler == "" {
  57. broker.currentFiler = address
  58. }
  59. } else {
  60. delete(broker.filers, address)
  61. if broker.currentFiler == address {
  62. for filer := range broker.filers {
  63. broker.currentFiler = filer
  64. break
  65. }
  66. }
  67. }
  68. }
  69. func (broker *MessageQueueBroker) GetFiler() pb.ServerAddress {
  70. return broker.currentFiler
  71. }
  72. func (broker *MessageQueueBroker) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  73. return pb.WithFilerClient(streamingMode, 0, broker.GetFiler(), broker.grpcDialOption, fn)
  74. }
  75. func (broker *MessageQueueBroker) AdjustedUrl(location *filer_pb.Location) string {
  76. return location.Url
  77. }
  78. func (broker *MessageQueueBroker) GetDataCenter() string {
  79. return ""
  80. }
  81. func (broker *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
  82. return pb.WithMasterClient(streamingMode, master, broker.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
  83. return fn(client)
  84. })
  85. }
  86. func (broker *MessageQueueBroker) withBrokerClient(streamingMode bool, server pb.ServerAddress, fn func(client mq_pb.SeaweedMessagingClient) error) error {
  87. return pb.WithBrokerClient(streamingMode, server, broker.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  88. return fn(client)
  89. })
  90. }