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
|
2023-12-23 03:33:50 +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
|
|
|
|
pubBalancer *pub_balancer.Balancer
|
2024-05-14 23:50:17 +08:00
|
|
|
filerClientAccessor *FilerClientAccessor
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
2024-05-14 23:50:17 +08:00
|
|
|
func NewConsumerGroup(t *mq_pb.Topic, pubBalancer *pub_balancer.Balancer, 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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnAddConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic) {
|
2024-03-01 01:38:52 +08:00
|
|
|
cg.onConsumerGroupInstanceChange("add consumer instance " + consumerGroupInstance)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnRemoveConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic) {
|
2024-03-01 01:38:52 +08:00
|
|
|
cg.onConsumerGroupInstanceChange("remove consumer instance " + consumerGroupInstance)
|
2023-12-29 03:56:37 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2024-03-01 01:38:52 +08:00
|
|
|
func (cg *ConsumerGroup) onConsumerGroupInstanceChange(reason string) {
|
2023-12-29 03:56:37 +08:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
|
|
|
cg.reBalanceTimer = time.AfterFunc(5*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 {
|
|
|
|
partitionSlotToBrokerList.AddBroker(assignment.Partition, assignment.LeaderBroker)
|
|
|
|
}
|
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 {
|
|
|
|
partitionSlotToBrokerList.AddBroker(assignment.Partition, assignment.LeaderBroker)
|
|
|
|
}
|
|
|
|
} 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-03-25 03:57:09 +08:00
|
|
|
assignedPartitions := make([]*mq_pb.BrokerPartitionAssignment, len(partitionSlots))
|
2023-12-29 12:35:15 +08:00
|
|
|
for i, partitionSlot := range partitionSlots {
|
2024-03-25 03:57:09 +08:00
|
|
|
assignedPartitions[i] = &mq_pb.BrokerPartitionAssignment{
|
2023-12-29 12:35:15 +08:00
|
|
|
Partition: &mq_pb.Partition{
|
2024-03-01 01:38:52 +08:00
|
|
|
RangeStop: partitionSlot.RangeStop,
|
2023-12-29 12:35:15 +08:00
|
|
|
RangeStart: partitionSlot.RangeStart,
|
2024-03-01 01:38:52 +08:00
|
|
|
RingSize: partitionSlotToBrokerList.RingSize,
|
2024-01-12 15:03:55 +08:00
|
|
|
UnixTimeNs: partitionSlot.UnixTimeNs,
|
2023-12-29 12:35:15 +08:00
|
|
|
},
|
2024-03-25 03:57:09 +08:00
|
|
|
LeaderBroker: partitionSlot.Broker,
|
2023-12-29 12:35:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
response := &mq_pb.SubscriberToSubCoordinatorResponse{
|
|
|
|
Message: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment_{
|
|
|
|
Assignment: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment{
|
2024-03-25 03:57:09 +08:00
|
|
|
PartitionAssignments: assignedPartitions,
|
2023-12-29 12:35:15 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-01-04 05:30:30 +08:00
|
|
|
println("sending response to", consumerGroupInstance.InstanceId, "...")
|
2023-12-29 12:35:15 +08:00
|
|
|
consumerGroupInstance.ResponseChan <- response
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|