broker_server.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package broker
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/sub_coordinator"
  7. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  8. "sync"
  9. "time"
  10. "github.com/seaweedfs/seaweedfs/weed/cluster"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  13. "google.golang.org/grpc"
  14. "github.com/seaweedfs/seaweedfs/weed/pb"
  15. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  16. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  17. )
  18. type MessageQueueBrokerOption struct {
  19. Masters map[string]pb.ServerAddress
  20. FilerGroup string
  21. DataCenter string
  22. Rack string
  23. DefaultReplication string
  24. MaxMB int
  25. Ip string
  26. Port int
  27. Cipher bool
  28. VolumeServerAccess string // how to access volume servers
  29. }
  30. func (option *MessageQueueBrokerOption) BrokerAddress() pb.ServerAddress {
  31. return pb.NewServerAddress(option.Ip, option.Port, 0)
  32. }
  33. type MessageQueueBroker struct {
  34. mq_pb.UnimplementedSeaweedMessagingServer
  35. option *MessageQueueBrokerOption
  36. grpcDialOption grpc.DialOption
  37. MasterClient *wdclient.MasterClient
  38. filers map[pb.ServerAddress]struct{}
  39. currentFiler pb.ServerAddress
  40. localTopicManager *topic.LocalTopicManager
  41. Balancer *pub_balancer.Balancer
  42. lockAsBalancer *cluster.LiveLock
  43. Coordinator *sub_coordinator.Coordinator
  44. accessLock sync.Mutex
  45. }
  46. func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.DialOption) (mqBroker *MessageQueueBroker, err error) {
  47. pub_broker_balancer := pub_balancer.NewBalancer()
  48. coordinator := sub_coordinator.NewCoordinator(pub_broker_balancer)
  49. mqBroker = &MessageQueueBroker{
  50. option: option,
  51. grpcDialOption: grpcDialOption,
  52. MasterClient: wdclient.NewMasterClient(grpcDialOption, option.FilerGroup, cluster.BrokerType, option.BrokerAddress(), option.DataCenter, option.Rack, *pb.NewServiceDiscoveryFromMap(option.Masters)),
  53. filers: make(map[pb.ServerAddress]struct{}),
  54. localTopicManager: topic.NewLocalTopicManager(),
  55. Balancer: pub_broker_balancer,
  56. Coordinator: coordinator,
  57. }
  58. mqBroker.MasterClient.SetOnPeerUpdateFn(mqBroker.OnBrokerUpdate)
  59. pub_broker_balancer.OnPartitionChange = mqBroker.Coordinator.OnPartitionChange
  60. pub_broker_balancer.OnAddBroker = mqBroker.Coordinator.OnSubAddBroker
  61. pub_broker_balancer.OnRemoveBroker = mqBroker.Coordinator.OnSubRemoveBroker
  62. go mqBroker.MasterClient.KeepConnectedToMaster(context.Background())
  63. existingNodes := cluster.ListExistingPeerUpdates(mqBroker.MasterClient.GetMaster(context.Background()), grpcDialOption, option.FilerGroup, cluster.FilerType)
  64. for _, newNode := range existingNodes {
  65. mqBroker.OnBrokerUpdate(newNode, time.Now())
  66. }
  67. // keep connecting to balancer
  68. go func() {
  69. for mqBroker.currentFiler == "" {
  70. time.Sleep(time.Millisecond * 237)
  71. }
  72. self := option.BrokerAddress()
  73. glog.V(0).Infof("broker %s found filer %s", self, mqBroker.currentFiler)
  74. newBrokerBalancerCh := make(chan string, 1)
  75. lockClient := cluster.NewLockClient(grpcDialOption, mqBroker.currentFiler)
  76. mqBroker.lockAsBalancer = lockClient.StartLongLivedLock(pub_balancer.LockBrokerBalancer, string(self), func(newLockOwner string) {
  77. glog.V(0).Infof("broker %s found balanacer %s", self, newLockOwner)
  78. newBrokerBalancerCh <- newLockOwner
  79. })
  80. mqBroker.KeepConnectedToBrokerBalancer(newBrokerBalancerCh)
  81. }()
  82. return mqBroker, nil
  83. }
  84. func (b *MessageQueueBroker) OnBrokerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
  85. if update.NodeType != cluster.FilerType {
  86. return
  87. }
  88. address := pb.ServerAddress(update.Address)
  89. if update.IsAdd {
  90. b.filers[address] = struct{}{}
  91. if b.currentFiler == "" {
  92. b.currentFiler = address
  93. }
  94. } else {
  95. delete(b.filers, address)
  96. if b.currentFiler == address {
  97. for filer := range b.filers {
  98. b.currentFiler = filer
  99. break
  100. }
  101. }
  102. }
  103. }
  104. func (b *MessageQueueBroker) GetFiler() pb.ServerAddress {
  105. return b.currentFiler
  106. }
  107. func (b *MessageQueueBroker) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  108. return pb.WithFilerClient(streamingMode, 0, b.GetFiler(), b.grpcDialOption, fn)
  109. }
  110. func (b *MessageQueueBroker) AdjustedUrl(location *filer_pb.Location) string {
  111. return location.Url
  112. }
  113. func (b *MessageQueueBroker) GetDataCenter() string {
  114. return ""
  115. }
  116. func (b *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
  117. return pb.WithMasterClient(streamingMode, master, b.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
  118. return fn(client)
  119. })
  120. }
  121. func (b *MessageQueueBroker) withBrokerClient(streamingMode bool, server pb.ServerAddress, fn func(client mq_pb.SeaweedMessagingClient) error) error {
  122. return pb.WithBrokerGrpcClient(streamingMode, server.String(), b.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  123. return fn(client)
  124. })
  125. }