2023-08-21 05:53:05 +00:00
|
|
|
package topic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
isLeader bool
|
|
|
|
FollowerBrokers []pb.ServerAddress
|
|
|
|
logBuffer *log_buffer.LogBuffer
|
|
|
|
}
|
|
|
|
|
2023-08-27 20:13:14 +00:00
|
|
|
type OnEachMessageFn func(logEntry *filer_pb.LogEntry) error
|
|
|
|
|
2023-08-26 20:39:21 +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-08-27 20:13:14 +00:00
|
|
|
func (p LocalPartition) Subscribe(clientName string, startReadTime time.Time, eachMessageFn OnEachMessageFn) {
|
|
|
|
p.logBuffer.LoopProcessLogData(clientName, startReadTime, 0, func() bool {
|
|
|
|
return true
|
|
|
|
}, eachMessageFn)
|
|
|
|
}
|
|
|
|
|
2023-08-28 01:59:04 +00:00
|
|
|
func FromPbBrokerPartitionAssignment(self pb.ServerAddress, assignment *mq_pb.BrokerPartitionAssignment) *LocalPartition {
|
2023-08-21 05:53:05 +00:00
|
|
|
isLeaer := assignment.LeaderBroker == string(self)
|
|
|
|
localPartition := &LocalPartition{
|
2023-08-28 01:59:04 +00:00
|
|
|
Partition: FromPbPartition(assignment.Partition),
|
|
|
|
isLeader: isLeaer,
|
2023-08-21 05:53:05 +00:00
|
|
|
}
|
|
|
|
if !isLeaer {
|
|
|
|
return localPartition
|
|
|
|
}
|
|
|
|
followers := make([]pb.ServerAddress, len(assignment.FollowerBrokers))
|
|
|
|
for i, follower := range assignment.FollowerBrokers {
|
|
|
|
followers[i] = pb.ServerAddress(follower)
|
|
|
|
}
|
|
|
|
localPartition.FollowerBrokers = followers
|
|
|
|
return localPartition
|
|
|
|
}
|