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.

45 lines
1.3 KiB

1 year ago
1 year ago
1 year ago
1 year ago
  1. package topic
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  7. "time"
  8. )
  9. type LocalPartition struct {
  10. Partition
  11. isLeader bool
  12. FollowerBrokers []pb.ServerAddress
  13. logBuffer *log_buffer.LogBuffer
  14. }
  15. type OnEachMessageFn func(logEntry *filer_pb.LogEntry) error
  16. func (p LocalPartition) Publish(message *mq_pb.DataMessage) {
  17. p.logBuffer.AddToBuffer(message.Key, message.Value, time.Now().UnixNano())
  18. }
  19. func (p LocalPartition) Subscribe(clientName string, startReadTime time.Time, eachMessageFn OnEachMessageFn) {
  20. p.logBuffer.LoopProcessLogData(clientName, startReadTime, 0, func() bool {
  21. return true
  22. }, eachMessageFn)
  23. }
  24. func FromPbBrokerPartitionAssignment(self pb.ServerAddress, assignment *mq_pb.BrokerPartitionAssignment) *LocalPartition {
  25. isLeaer := assignment.LeaderBroker == string(self)
  26. localPartition := &LocalPartition{
  27. Partition: FromPbPartition(assignment.Partition),
  28. isLeader: isLeaer,
  29. }
  30. if !isLeaer {
  31. return localPartition
  32. }
  33. followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
  34. for i, follower := range assignment.FollowerBrokers {
  35. followers[i] = pb.ServerAddress(follower)
  36. }
  37. localPartition.FollowerBrokers = followers
  38. return localPartition
  39. }