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.

64 lines
1.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
  1. package topic
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  8. "time"
  9. )
  10. type LocalPartition struct {
  11. Partition
  12. isLeader bool
  13. FollowerBrokers []pb.ServerAddress
  14. logBuffer *log_buffer.LogBuffer
  15. }
  16. func NewLocalPartition(topic Topic, partition Partition, isLeader bool, followerBrokers []pb.ServerAddress) *LocalPartition {
  17. return &LocalPartition{
  18. Partition: partition,
  19. isLeader: isLeader,
  20. FollowerBrokers: followerBrokers,
  21. logBuffer: log_buffer.NewLogBuffer(
  22. fmt.Sprintf("%s/%s/%4d-%4d", topic.Namespace, topic.Name, partition.RangeStart, partition.RangeStop),
  23. 2*time.Minute,
  24. func(startTime, stopTime time.Time, buf []byte) {
  25. },
  26. func() {
  27. },
  28. ),
  29. }
  30. }
  31. type OnEachMessageFn func(logEntry *filer_pb.LogEntry) error
  32. func (p LocalPartition) Publish(message *mq_pb.DataMessage) {
  33. p.logBuffer.AddToBuffer(message.Key, message.Value, time.Now().UnixNano())
  34. }
  35. func (p LocalPartition) Subscribe(clientName string, startReadTime time.Time, eachMessageFn OnEachMessageFn) {
  36. p.logBuffer.LoopProcessLogData(clientName, startReadTime, 0, func() bool {
  37. return true
  38. }, eachMessageFn)
  39. }
  40. func FromPbBrokerPartitionAssignment(self pb.ServerAddress, assignment *mq_pb.BrokerPartitionAssignment) *LocalPartition {
  41. isLeaer := assignment.LeaderBroker == string(self)
  42. localPartition := &LocalPartition{
  43. Partition: FromPbPartition(assignment.Partition),
  44. isLeader: isLeaer,
  45. }
  46. if !isLeaer {
  47. return localPartition
  48. }
  49. followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
  50. for i, follower := range assignment.FollowerBrokers {
  51. followers[i] = pb.ServerAddress(follower)
  52. }
  53. localPartition.FollowerBrokers = followers
  54. return localPartition
  55. }