2023-08-21 05:53:05 +00:00
|
|
|
package topic
|
|
|
|
|
|
|
|
import (
|
2023-08-28 16:02:12 +00:00
|
|
|
"fmt"
|
2023-08-21 05:53:05 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2023-08-27 20:13:14 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2023-08-21 05:53:05 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalPartition struct {
|
|
|
|
Partition
|
2023-12-11 20:05:54 +00: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 05:53:05 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func NewLocalPartition(partition Partition, isLeader bool, followerBrokers []pb.ServerAddress) *LocalPartition {
|
2023-08-28 16:02:12 +00:00
|
|
|
return &LocalPartition{
|
|
|
|
Partition: partition,
|
|
|
|
isLeader: isLeader,
|
|
|
|
FollowerBrokers: followerBrokers,
|
|
|
|
logBuffer: log_buffer.NewLogBuffer(
|
2023-12-11 20:05:54 +00:00
|
|
|
fmt.Sprintf("%d/%4d-%4d", partition.UnixTimeNs, partition.RangeStart, partition.RangeStop),
|
2023-08-28 16:02:12 +00:00
|
|
|
2*time.Minute,
|
|
|
|
func(startTime, stopTime time.Time, buf []byte) {
|
|
|
|
|
|
|
|
},
|
|
|
|
func() {
|
|
|
|
|
|
|
|
},
|
|
|
|
),
|
2023-12-11 20:05:54 +00:00
|
|
|
Publishers: NewLocalPartitionPublishers(),
|
|
|
|
Subscribers: NewLocalPartitionSubscribers(),
|
2023-08-28 16:02:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 20:13:14 +00:00
|
|
|
type OnEachMessageFn func(logEntry *filer_pb.LogEntry) error
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (p *LocalPartition) Publish(message *mq_pb.DataMessage) {
|
2023-08-21 05:53:05 +00:00
|
|
|
p.logBuffer.AddToBuffer(message.Key, message.Value, time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (p *LocalPartition) Subscribe(clientName string, startReadTime time.Time, onNoMessageFn func() bool, eachMessageFn OnEachMessageFn) {
|
|
|
|
p.logBuffer.LoopProcessLogData(clientName, startReadTime, 0, onNoMessageFn, eachMessageFn)
|
2023-08-27 20:13:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 01:59:04 +00:00
|
|
|
func FromPbBrokerPartitionAssignment(self pb.ServerAddress, assignment *mq_pb.BrokerPartitionAssignment) *LocalPartition {
|
2023-12-11 20:05:54 +00:00
|
|
|
isLeader := assignment.LeaderBroker == string(self)
|
2023-08-21 05:53:05 +00:00
|
|
|
followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
|
2023-12-11 20:05:54 +00:00
|
|
|
for i, followerBroker := range assignment.FollowerBrokers {
|
|
|
|
followers[i] = pb.ServerAddress(followerBroker)
|
|
|
|
}
|
|
|
|
return NewLocalPartition(FromPbPartition(assignment.Partition), isLeader, followers)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 05:53:05 +00:00
|
|
|
}
|
|
|
|
}
|