2023-12-12 04:05:54 +08:00
|
|
|
package sub_coordinator
|
|
|
|
|
|
|
|
import (
|
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2023-12-29 12:35:15 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
2023-12-29 03:56:37 +08:00
|
|
|
"time"
|
2023-12-12 04:05:54 +08:00
|
|
|
)
|
2023-12-23 03:33:50 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
type ConsumerGroupInstance struct {
|
|
|
|
InstanceId string
|
|
|
|
// the consumer group instance may not have an active partition
|
2024-05-21 02:05:18 +08:00
|
|
|
Partitions []*topic.Partition
|
|
|
|
ResponseChan chan *mq_pb.SubscriberToSubCoordinatorResponse
|
2024-05-14 09:30:10 +08:00
|
|
|
MaxPartitionCount int32
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
type ConsumerGroup struct {
|
2024-03-01 01:38:52 +08:00
|
|
|
topic topic.Topic
|
2023-12-12 04:05:54 +08:00
|
|
|
// map a consumer group instance id to a consumer group instance
|
|
|
|
ConsumerGroupInstances cmap.ConcurrentMap[string, *ConsumerGroupInstance]
|
2024-03-01 01:38:52 +08:00
|
|
|
mapping *PartitionConsumerMapping
|
|
|
|
reBalanceTimer *time.Timer
|
2024-05-22 00:56:30 +08:00
|
|
|
pubBalancer *pub_balancer.PubBalancer
|
2024-05-14 23:50:17 +08:00
|
|
|
filerClientAccessor *FilerClientAccessor
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
2024-05-22 00:56:30 +08:00
|
|
|
func NewConsumerGroup(t *mq_pb.Topic, pubBalancer *pub_balancer.PubBalancer, filerClientAccessor *FilerClientAccessor) *ConsumerGroup {
|
2023-12-12 04:05:54 +08:00
|
|
|
return &ConsumerGroup{
|
2023-12-29 12:35:15 +08:00
|
|
|
topic: topic.FromPbTopic(t),
|
2023-12-12 04:05:54 +08:00
|
|
|
ConsumerGroupInstances: cmap.New[*ConsumerGroupInstance](),
|
2023-12-23 03:33:50 +08:00
|
|
|
mapping: NewPartitionConsumerMapping(pub_balancer.MaxPartitionCount),
|
2023-12-29 12:35:15 +08:00
|
|
|
pubBalancer: pubBalancer,
|
2024-05-14 23:50:17 +08:00
|
|
|
filerClientAccessor: filerClientAccessor,
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsumerGroupInstance(instanceId string) *ConsumerGroupInstance {
|
|
|
|
return &ConsumerGroupInstance{
|
2023-12-23 03:33:50 +08:00
|
|
|
InstanceId: instanceId,
|
2023-12-12 04:05:54 +08:00
|
|
|
ResponseChan: make(chan *mq_pb.SubscriberToSubCoordinatorResponse, 1),
|
|
|
|
}
|
|
|
|
}
|
2024-05-20 05:52:38 +08:00
|
|
|
func (cg *ConsumerGroup) OnAddConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic, maxPartitionCount, rebalanceSeconds int32) {
|
2024-05-21 02:05:18 +08:00
|
|
|
cg.onConsumerGroupInstanceChange(true, "add consumer instance "+consumerGroupInstance, maxPartitionCount, rebalanceSeconds)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
2024-05-20 05:52:38 +08:00
|
|
|
func (cg *ConsumerGroup) OnRemoveConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic, maxPartitionCount, rebalanceSeconds int32) {
|
2024-05-21 02:05:18 +08:00
|
|
|
cg.onConsumerGroupInstanceChange(false, "remove consumer instance "+consumerGroupInstance, maxPartitionCount, rebalanceSeconds)
|
2023-12-29 03:56:37 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2024-05-20 05:52:38 +08:00
|
|
|
func (cg *ConsumerGroup) onConsumerGroupInstanceChange(isAdd bool, reason string, maxPartitionCount, rebalanceSeconds int32) {
|
2023-12-29 03:56:37 +08:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
2024-05-20 05:52:38 +08:00
|
|
|
if maxPartitionCount == 0 {
|
|
|
|
maxPartitionCount = 1
|
|
|
|
}
|
|
|
|
if rebalanceSeconds == 0 {
|
|
|
|
rebalanceSeconds = 10
|
|
|
|
}
|
|
|
|
if isAdd {
|
|
|
|
if conf, err := cg.filerClientAccessor.ReadTopicConfFromFiler(cg.topic); err == nil {
|
|
|
|
var sumMaxPartitionCount int32
|
|
|
|
for _, cgi := range cg.ConsumerGroupInstances.Items() {
|
|
|
|
sumMaxPartitionCount += cgi.MaxPartitionCount
|
|
|
|
}
|
|
|
|
if sumMaxPartitionCount < int32(len(conf.BrokerPartitionAssignments)) && sumMaxPartitionCount+maxPartitionCount >= int32(len(conf.BrokerPartitionAssignments)) {
|
|
|
|
partitionSlotToBrokerList := pub_balancer.NewPartitionSlotToBrokerList(pub_balancer.MaxPartitionCount)
|
|
|
|
for _, assignment := range conf.BrokerPartitionAssignments {
|
|
|
|
partitionSlotToBrokerList.AddBroker(assignment.Partition, assignment.LeaderBroker, assignment.FollowerBroker)
|
|
|
|
}
|
|
|
|
cg.BalanceConsumerGroupInstances(partitionSlotToBrokerList, reason)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cg.reBalanceTimer = time.AfterFunc(time.Duration(rebalanceSeconds)*time.Second, func() {
|
2024-05-14 10:24:47 +08:00
|
|
|
cg.BalanceConsumerGroupInstances(nil, reason)
|
2023-12-29 03:56:37 +08:00
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
})
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
2024-01-04 05:30:30 +08:00
|
|
|
func (cg *ConsumerGroup) OnPartitionListChange(assignments []*mq_pb.BrokerPartitionAssignment) {
|
2023-12-29 03:56:37 +08:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
2024-01-04 05:30:30 +08:00
|
|
|
partitionSlotToBrokerList := pub_balancer.NewPartitionSlotToBrokerList(pub_balancer.MaxPartitionCount)
|
|
|
|
for _, assignment := range assignments {
|
2024-05-19 15:46:12 +08:00
|
|
|
partitionSlotToBrokerList.AddBroker(assignment.Partition, assignment.LeaderBroker, assignment.FollowerBroker)
|
2024-01-04 05:30:30 +08:00
|
|
|
}
|
2024-05-14 10:24:47 +08:00
|
|
|
cg.BalanceConsumerGroupInstances(partitionSlotToBrokerList, "partition list change")
|
2023-12-29 03:56:37 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2024-05-14 10:24:47 +08:00
|
|
|
func (cg *ConsumerGroup) BalanceConsumerGroupInstances(knownPartitionSlotToBrokerList *pub_balancer.PartitionSlotToBrokerList, reason string) {
|
2024-01-04 05:30:30 +08:00
|
|
|
glog.V(0).Infof("rebalance consumer group %s due to %s", cg.topic.String(), reason)
|
2023-12-29 12:35:15 +08:00
|
|
|
|
|
|
|
// collect current topic partitions
|
2024-01-04 05:30:30 +08:00
|
|
|
partitionSlotToBrokerList := knownPartitionSlotToBrokerList
|
|
|
|
if partitionSlotToBrokerList == nil {
|
2024-05-15 00:09:36 +08:00
|
|
|
if conf, err := cg.filerClientAccessor.ReadTopicConfFromFiler(cg.topic); err == nil {
|
|
|
|
partitionSlotToBrokerList = pub_balancer.NewPartitionSlotToBrokerList(pub_balancer.MaxPartitionCount)
|
|
|
|
for _, assignment := range conf.BrokerPartitionAssignments {
|
2024-05-19 15:46:12 +08:00
|
|
|
partitionSlotToBrokerList.AddBroker(assignment.Partition, assignment.LeaderBroker, assignment.FollowerBroker)
|
2024-05-15 00:09:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.V(0).Infof("fail to read topic conf from filer: %v", err)
|
2024-01-04 05:30:30 +08:00
|
|
|
return
|
|
|
|
}
|
2023-12-29 12:35:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// collect current consumer group instance ids
|
2024-05-14 13:03:57 +08:00
|
|
|
var consumerInstances []*ConsumerGroupInstance
|
2023-12-29 12:35:15 +08:00
|
|
|
for _, consumerGroupInstance := range cg.ConsumerGroupInstances.Items() {
|
2024-05-14 13:03:57 +08:00
|
|
|
consumerInstances = append(consumerInstances, consumerGroupInstance)
|
2023-12-29 12:35:15 +08:00
|
|
|
}
|
|
|
|
|
2024-05-14 13:03:57 +08:00
|
|
|
cg.mapping.BalanceToConsumerInstances(partitionSlotToBrokerList, consumerInstances)
|
2023-12-29 12:35:15 +08:00
|
|
|
|
|
|
|
// convert cg.mapping currentMapping to map of consumer group instance id to partition slots
|
|
|
|
consumerInstanceToPartitionSlots := make(map[string][]*PartitionSlotToConsumerInstance)
|
|
|
|
for _, partitionSlot := range cg.mapping.currentMapping.PartitionSlots {
|
|
|
|
consumerInstanceToPartitionSlots[partitionSlot.AssignedInstanceId] = append(consumerInstanceToPartitionSlots[partitionSlot.AssignedInstanceId], partitionSlot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify consumer group instances
|
|
|
|
for _, consumerGroupInstance := range cg.ConsumerGroupInstances.Items() {
|
|
|
|
partitionSlots, found := consumerInstanceToPartitionSlots[consumerGroupInstance.InstanceId]
|
|
|
|
if !found {
|
|
|
|
partitionSlots = make([]*PartitionSlotToConsumerInstance, 0)
|
|
|
|
}
|
2024-01-12 15:03:55 +08:00
|
|
|
consumerGroupInstance.Partitions = ToPartitions(partitionSlotToBrokerList.RingSize, partitionSlots)
|
2024-05-21 23:05:56 +08:00
|
|
|
for _, partitionSlot := range partitionSlots {
|
|
|
|
consumerGroupInstance.ResponseChan <- &mq_pb.SubscriberToSubCoordinatorResponse{
|
|
|
|
Message: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment_{
|
|
|
|
Assignment: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment{
|
|
|
|
PartitionAssignment: &mq_pb.BrokerPartitionAssignment{
|
|
|
|
Partition: &mq_pb.Partition{
|
|
|
|
RangeStop: partitionSlot.RangeStop,
|
|
|
|
RangeStart: partitionSlot.RangeStart,
|
|
|
|
RingSize: partitionSlotToBrokerList.RingSize,
|
|
|
|
UnixTimeNs: partitionSlot.UnixTimeNs,
|
|
|
|
},
|
|
|
|
LeaderBroker: partitionSlot.Broker,
|
|
|
|
FollowerBroker: partitionSlot.FollowerBroker,
|
|
|
|
},
|
|
|
|
},
|
2023-12-29 12:35:15 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|