2023-12-11 20:05:54 +00:00
|
|
|
package sub_coordinator
|
|
|
|
|
|
|
|
import (
|
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2023-12-29 04:35:15 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-12-11 20:05:54 +00: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-28 19:56:37 +00:00
|
|
|
"time"
|
2023-12-11 20:05:54 +00:00
|
|
|
)
|
2023-12-22 19:33:50 +00:00
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
type ConsumerGroupInstance struct {
|
|
|
|
InstanceId string
|
|
|
|
// the consumer group instance may not have an active partition
|
2023-12-22 19:33:50 +00:00
|
|
|
Partitions []*topic.Partition
|
|
|
|
ResponseChan chan *mq_pb.SubscriberToSubCoordinatorResponse
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
type ConsumerGroup struct {
|
2023-12-29 04:35:15 +00:00
|
|
|
topic topic.Topic
|
2023-12-11 20:05:54 +00:00
|
|
|
// map a consumer group instance id to a consumer group instance
|
|
|
|
ConsumerGroupInstances cmap.ConcurrentMap[string, *ConsumerGroupInstance]
|
2023-12-28 19:56:37 +00:00
|
|
|
mapping *PartitionConsumerMapping
|
|
|
|
reBalanceTimer *time.Timer
|
2023-12-29 04:35:15 +00:00
|
|
|
pubBalancer *pub_balancer.Balancer
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 04:35:15 +00:00
|
|
|
func NewConsumerGroup(t *mq_pb.Topic, pubBalancer *pub_balancer.Balancer) *ConsumerGroup {
|
2023-12-11 20:05:54 +00:00
|
|
|
return &ConsumerGroup{
|
2023-12-29 04:35:15 +00:00
|
|
|
topic: topic.FromPbTopic(t),
|
2023-12-11 20:05:54 +00:00
|
|
|
ConsumerGroupInstances: cmap.New[*ConsumerGroupInstance](),
|
2023-12-22 19:33:50 +00:00
|
|
|
mapping: NewPartitionConsumerMapping(pub_balancer.MaxPartitionCount),
|
2023-12-29 04:35:15 +00:00
|
|
|
pubBalancer: pubBalancer,
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsumerGroupInstance(instanceId string) *ConsumerGroupInstance {
|
|
|
|
return &ConsumerGroupInstance{
|
2023-12-22 19:33:50 +00:00
|
|
|
InstanceId: instanceId,
|
2023-12-11 20:05:54 +00:00
|
|
|
ResponseChan: make(chan *mq_pb.SubscriberToSubCoordinatorResponse, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnAddConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic) {
|
2023-12-29 05:13:49 +00:00
|
|
|
cg.onConsumerGroupInstanceChange("add consumer instance "+ consumerGroupInstance)
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnRemoveConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic) {
|
2023-12-29 05:13:49 +00:00
|
|
|
cg.onConsumerGroupInstanceChange("remove consumer instance "+ consumerGroupInstance)
|
2023-12-28 19:56:37 +00:00
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
|
2023-12-29 05:13:49 +00:00
|
|
|
func (cg *ConsumerGroup) onConsumerGroupInstanceChange(reason string){
|
2023-12-28 19:56:37 +00:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
|
|
|
cg.reBalanceTimer = time.AfterFunc(5*time.Second, func() {
|
2024-01-03 21:30:30 +00:00
|
|
|
cg.RebalanceConsumberGroupInstances(nil, reason)
|
2023-12-28 19:56:37 +00:00
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
})
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
2024-01-03 21:30:30 +00:00
|
|
|
func (cg *ConsumerGroup) OnPartitionListChange(assignments []*mq_pb.BrokerPartitionAssignment) {
|
2023-12-28 19:56:37 +00:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
2024-01-03 21:30:30 +00:00
|
|
|
partitionSlotToBrokerList := pub_balancer.NewPartitionSlotToBrokerList(pub_balancer.MaxPartitionCount)
|
|
|
|
for _, assignment := range assignments {
|
|
|
|
partitionSlotToBrokerList.AddBroker(assignment.Partition, assignment.LeaderBroker)
|
|
|
|
}
|
|
|
|
cg.RebalanceConsumberGroupInstances(partitionSlotToBrokerList, "partition list change")
|
2023-12-28 19:56:37 +00:00
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
|
2024-01-03 21:30:30 +00:00
|
|
|
func (cg *ConsumerGroup) RebalanceConsumberGroupInstances(knownPartitionSlotToBrokerList *pub_balancer.PartitionSlotToBrokerList, reason string) {
|
|
|
|
glog.V(0).Infof("rebalance consumer group %s due to %s", cg.topic.String(), reason)
|
2023-12-29 04:35:15 +00:00
|
|
|
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
|
|
|
|
// collect current topic partitions
|
2024-01-03 21:30:30 +00:00
|
|
|
partitionSlotToBrokerList := knownPartitionSlotToBrokerList
|
|
|
|
if partitionSlotToBrokerList == nil {
|
|
|
|
var found bool
|
|
|
|
partitionSlotToBrokerList, found = cg.pubBalancer.TopicToBrokers.Get(cg.topic.String())
|
|
|
|
if !found {
|
|
|
|
glog.V(0).Infof("topic %s not found in balancer", cg.topic.String())
|
|
|
|
return
|
|
|
|
}
|
2023-12-29 04:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// collect current consumer group instance ids
|
2024-01-03 21:30:30 +00:00
|
|
|
var consumerInstanceIds []string
|
2023-12-29 04:35:15 +00:00
|
|
|
for _, consumerGroupInstance := range cg.ConsumerGroupInstances.Items() {
|
|
|
|
consumerInstanceIds = append(consumerInstanceIds, consumerGroupInstance.InstanceId)
|
|
|
|
}
|
|
|
|
|
2023-12-29 05:13:49 +00:00
|
|
|
cg.mapping.BalanceToConsumerInstanceIds(partitionSlotToBrokerList, consumerInstanceIds)
|
2023-12-29 04:35:15 +00: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)
|
|
|
|
}
|
|
|
|
consumerGroupInstance.Partitions = ToPartitions(partitionSlotToBrokerList.RingSize, partitionSlots, now)
|
|
|
|
assignedPartitions := make([]*mq_pb.SubscriberToSubCoordinatorResponse_AssignedPartition, len(partitionSlots))
|
|
|
|
for i, partitionSlot := range partitionSlots {
|
|
|
|
assignedPartitions[i] = &mq_pb.SubscriberToSubCoordinatorResponse_AssignedPartition{
|
|
|
|
Partition: &mq_pb.Partition{
|
|
|
|
RangeStop: partitionSlot.RangeStop,
|
|
|
|
RangeStart: partitionSlot.RangeStart,
|
|
|
|
RingSize: partitionSlotToBrokerList.RingSize,
|
|
|
|
UnixTimeNs: now,
|
|
|
|
},
|
2023-12-29 05:13:49 +00:00
|
|
|
Broker: partitionSlot.Broker,
|
2023-12-29 04:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
response := &mq_pb.SubscriberToSubCoordinatorResponse{
|
|
|
|
Message: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment_{
|
|
|
|
Assignment: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment{
|
|
|
|
AssignedPartitions: assignedPartitions,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-01-03 21:30:30 +00:00
|
|
|
println("sending response to", consumerGroupInstance.InstanceId, "...")
|
2023-12-29 04:35:15 +00:00
|
|
|
consumerGroupInstance.ResponseChan <- response
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|