2023-12-12 04:05:54 +08:00
|
|
|
package sub_coordinator
|
|
|
|
|
|
|
|
import (
|
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
|
|
|
"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
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
type ConsumerGroup struct {
|
|
|
|
// map a consumer group instance id to a consumer group instance
|
|
|
|
ConsumerGroupInstances cmap.ConcurrentMap[string, *ConsumerGroupInstance]
|
2023-12-29 03:56:37 +08:00
|
|
|
mapping *PartitionConsumerMapping
|
|
|
|
reBalanceTimer *time.Timer
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsumerGroup() *ConsumerGroup {
|
|
|
|
return &ConsumerGroup{
|
|
|
|
ConsumerGroupInstances: cmap.New[*ConsumerGroupInstance](),
|
2023-12-23 03:33:50 +08:00
|
|
|
mapping: NewPartitionConsumerMapping(pub_balancer.MaxPartitionCount),
|
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) {
|
2023-12-29 03:56:37 +08:00
|
|
|
cg.onConsumerGroupInstanceChange()
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnRemoveConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic) {
|
2023-12-29 03:56:37 +08:00
|
|
|
cg.onConsumerGroupInstanceChange()
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2023-12-29 03:56:37 +08:00
|
|
|
func (cg *ConsumerGroup) onConsumerGroupInstanceChange(){
|
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
|
|
|
cg.reBalanceTimer = time.AfterFunc(5*time.Second, func() {
|
|
|
|
cg.Rebalance()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
})
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnPartitionListChange() {
|
2023-12-29 03:56:37 +08:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
|
|
|
cg.Rebalance()
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2023-12-29 03:56:37 +08:00
|
|
|
func (cg *ConsumerGroup) Rebalance() {
|
|
|
|
println("rebalance...")
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|