2023-08-21 13:53:05 +08:00
|
|
|
package topic
|
|
|
|
|
|
|
|
import (
|
2023-08-29 00:02:12 +08:00
|
|
|
"fmt"
|
2023-08-21 13:53:05 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2023-08-28 04:13:14 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2023-08-21 13:53:05 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalPartition struct {
|
|
|
|
Partition
|
2023-12-12 04:05:54 +08:00
|
|
|
isLeader bool
|
|
|
|
FollowerBrokers []pb.ServerAddress
|
|
|
|
logBuffer *log_buffer.LogBuffer
|
|
|
|
ConsumerCount int32
|
|
|
|
StopPublishersCh chan struct{}
|
|
|
|
Publishers *LocalPartitionPublishers
|
|
|
|
StopSubscribersCh chan struct{}
|
|
|
|
Subscribers *LocalPartitionSubscribers
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:27:02 +08:00
|
|
|
var TIME_FORMAT = "2006-01-02-15-04-05"
|
|
|
|
func NewLocalPartition(partition Partition, isLeader bool, followerBrokers []pb.ServerAddress, logFlushFn log_buffer.LogFlushFuncType) *LocalPartition {
|
2023-08-29 00:02:12 +08:00
|
|
|
return &LocalPartition{
|
|
|
|
Partition: partition,
|
|
|
|
isLeader: isLeader,
|
|
|
|
FollowerBrokers: followerBrokers,
|
|
|
|
logBuffer: log_buffer.NewLogBuffer(
|
2023-12-12 04:05:54 +08:00
|
|
|
fmt.Sprintf("%d/%4d-%4d", partition.UnixTimeNs, partition.RangeStart, partition.RangeStop),
|
2023-08-29 00:02:12 +08:00
|
|
|
2*time.Minute,
|
2024-01-09 15:27:02 +08:00
|
|
|
logFlushFn,
|
2023-08-29 00:02:12 +08:00
|
|
|
func() {
|
|
|
|
|
|
|
|
},
|
|
|
|
),
|
2023-12-12 04:05:54 +08:00
|
|
|
Publishers: NewLocalPartitionPublishers(),
|
|
|
|
Subscribers: NewLocalPartitionSubscribers(),
|
2023-08-29 00:02:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:13:14 +08:00
|
|
|
type OnEachMessageFn func(logEntry *filer_pb.LogEntry) error
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (p *LocalPartition) Publish(message *mq_pb.DataMessage) {
|
2023-08-21 13:53:05 +08:00
|
|
|
p.logBuffer.AddToBuffer(message.Key, message.Value, time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2024-01-08 16:03:08 +08:00
|
|
|
func (p *LocalPartition) Subscribe(clientName string, startPosition log_buffer.MessagePosition, inMemoryOnly bool, onNoMessageFn func() bool, eachMessageFn OnEachMessageFn) {
|
|
|
|
p.logBuffer.LoopProcessLogData(clientName, startPosition, inMemoryOnly, 0, onNoMessageFn, eachMessageFn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LocalPartition) GetEarliestMessageTimeInMemory() time.Time {
|
|
|
|
return p.logBuffer.GetEarliestTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LocalPartition) HasData() bool {
|
|
|
|
return !p.logBuffer.GetEarliestTime().IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LocalPartition) GetEarliestInMemoryMessagePosition() log_buffer.MessagePosition {
|
|
|
|
return p.logBuffer.GetEarliestPosition()
|
2023-08-28 04:13:14 +08:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:27:02 +08:00
|
|
|
func FromPbBrokerPartitionAssignment(self pb.ServerAddress, assignment *mq_pb.BrokerPartitionAssignment, logFlushFn log_buffer.LogFlushFuncType) *LocalPartition {
|
2023-12-12 04:05:54 +08:00
|
|
|
isLeader := assignment.LeaderBroker == string(self)
|
2023-08-21 13:53:05 +08:00
|
|
|
followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
|
2023-12-12 04:05:54 +08:00
|
|
|
for i, followerBroker := range assignment.FollowerBrokers {
|
|
|
|
followers[i] = pb.ServerAddress(followerBroker)
|
|
|
|
}
|
2024-01-09 15:27:02 +08:00
|
|
|
return NewLocalPartition(FromPbPartition(assignment.Partition), isLeader, followers, logFlushFn)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LocalPartition) closePublishers() {
|
|
|
|
p.Publishers.SignalShutdown()
|
|
|
|
close(p.StopPublishersCh)
|
|
|
|
}
|
|
|
|
func (p *LocalPartition) closeSubscribers() {
|
|
|
|
p.Subscribers.SignalShutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LocalPartition) WaitUntilNoPublishers() {
|
|
|
|
for {
|
|
|
|
if p.Publishers.IsEmpty() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(113 * time.Millisecond)
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
|
|
|
}
|