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.

144 lines
4.3 KiB

10 months ago
10 months ago
10 months ago
11 months ago
10 months ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
10 months ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
1 year ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
10 months ago
  1. package topic
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  8. "google.golang.org/grpc"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. )
  13. type LocalPartition struct {
  14. ListenersWaits int64
  15. // notifying clients
  16. ListenersLock sync.Mutex
  17. ListenersCond *sync.Cond
  18. Partition
  19. isLeader bool
  20. FollowerBrokers []pb.ServerAddress
  21. LogBuffer *log_buffer.LogBuffer
  22. ConsumerCount int32
  23. Publishers *LocalPartitionPublishers
  24. Subscribers *LocalPartitionSubscribers
  25. FollowerId int32
  26. FollowerStream mq_pb.SeaweedMessaging_PublishFollowMeClient
  27. GrpcConnection *grpc.ClientConn
  28. }
  29. var TIME_FORMAT = "2006-01-02-15-04-05"
  30. func NewLocalPartition(partition Partition, isLeader bool, followerBrokers []pb.ServerAddress, logFlushFn log_buffer.LogFlushFuncType, readFromDiskFn log_buffer.LogReadFromDiskFuncType) *LocalPartition {
  31. lp := &LocalPartition{
  32. Partition: partition,
  33. isLeader: isLeader,
  34. FollowerBrokers: followerBrokers,
  35. Publishers: NewLocalPartitionPublishers(),
  36. Subscribers: NewLocalPartitionSubscribers(),
  37. }
  38. lp.ListenersCond = sync.NewCond(&lp.ListenersLock)
  39. lp.LogBuffer = log_buffer.NewLogBuffer(fmt.Sprintf("%d/%04d-%04d", partition.UnixTimeNs, partition.RangeStart, partition.RangeStop),
  40. 2*time.Minute, logFlushFn, readFromDiskFn, func() {
  41. if atomic.LoadInt64(&lp.ListenersWaits) > 0 {
  42. lp.ListenersCond.Broadcast()
  43. }
  44. })
  45. return lp
  46. }
  47. func (p *LocalPartition) Publish(message *mq_pb.DataMessage) {
  48. p.LogBuffer.AddToBuffer(message.Key, message.Value, time.Now().UnixNano())
  49. }
  50. func (p *LocalPartition) Subscribe(clientName string, startPosition log_buffer.MessagePosition,
  51. onNoMessageFn func() bool, eachMessageFn log_buffer.EachLogEntryFuncType) error {
  52. var processedPosition log_buffer.MessagePosition
  53. var readPersistedLogErr error
  54. var readInMemoryLogErr error
  55. var isDone bool
  56. for {
  57. processedPosition, isDone, readPersistedLogErr = p.LogBuffer.ReadFromDiskFn(startPosition, 0, eachMessageFn)
  58. if readPersistedLogErr != nil {
  59. glog.V(0).Infof("%s read %v persisted log: %v", clientName, p.Partition, readPersistedLogErr)
  60. return readPersistedLogErr
  61. }
  62. if isDone {
  63. return nil
  64. }
  65. startPosition = processedPosition
  66. processedPosition, isDone, readInMemoryLogErr = p.LogBuffer.LoopProcessLogData(clientName, startPosition, 0, onNoMessageFn, eachMessageFn)
  67. if isDone {
  68. return nil
  69. }
  70. startPosition = processedPosition
  71. if readInMemoryLogErr == log_buffer.ResumeFromDiskError {
  72. continue
  73. }
  74. if readInMemoryLogErr != nil {
  75. glog.V(0).Infof("%s read %v in memory log: %v", clientName, p.Partition, readInMemoryLogErr)
  76. return readInMemoryLogErr
  77. }
  78. }
  79. }
  80. func (p *LocalPartition) GetEarliestMessageTimeInMemory() time.Time {
  81. return p.LogBuffer.GetEarliestTime()
  82. }
  83. func (p *LocalPartition) HasData() bool {
  84. return !p.LogBuffer.GetEarliestTime().IsZero()
  85. }
  86. func (p *LocalPartition) GetEarliestInMemoryMessagePosition() log_buffer.MessagePosition {
  87. return p.LogBuffer.GetEarliestPosition()
  88. }
  89. func FromPbBrokerPartitionAssignment(self pb.ServerAddress, partition Partition, assignment *mq_pb.BrokerPartitionAssignment, logFlushFn log_buffer.LogFlushFuncType, readFromDiskFn log_buffer.LogReadFromDiskFuncType) *LocalPartition {
  90. isLeader := assignment.LeaderBroker == string(self)
  91. followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
  92. for i, followerBroker := range assignment.FollowerBrokers {
  93. followers[i] = pb.ServerAddress(followerBroker)
  94. }
  95. return NewLocalPartition(partition, isLeader, followers, logFlushFn, readFromDiskFn)
  96. }
  97. func (p *LocalPartition) closePublishers() {
  98. p.Publishers.SignalShutdown()
  99. }
  100. func (p *LocalPartition) closeSubscribers() {
  101. p.Subscribers.SignalShutdown()
  102. }
  103. func (p *LocalPartition) WaitUntilNoPublishers() {
  104. for {
  105. if p.Publishers.IsEmpty() {
  106. return
  107. }
  108. time.Sleep(113 * time.Millisecond)
  109. }
  110. }
  111. func (p *LocalPartition) MaybeShutdownLocalPartition() (hasShutdown bool) {
  112. if p.Publishers.IsEmpty() && p.Subscribers.IsEmpty() {
  113. p.LogBuffer.ShutdownLogBuffer()
  114. hasShutdown = true
  115. }
  116. return
  117. }
  118. func (p *LocalPartition) Shutdown() {
  119. p.closePublishers()
  120. p.closeSubscribers()
  121. p.LogBuffer.ShutdownLogBuffer()
  122. atomic.StoreInt32(&p.FollowerId, 0)
  123. glog.V(0).Infof("local partition %v shutting down", p.Partition)
  124. }