2023-12-11 20:05:54 +00: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-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 {
|
|
|
|
// 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-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsumerGroup() *ConsumerGroup {
|
|
|
|
return &ConsumerGroup{
|
|
|
|
ConsumerGroupInstances: cmap.New[*ConsumerGroupInstance](),
|
2023-12-22 19:33:50 +00:00
|
|
|
mapping: NewPartitionConsumerMapping(pub_balancer.MaxPartitionCount),
|
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-28 19:56:37 +00:00
|
|
|
cg.onConsumerGroupInstanceChange()
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnRemoveConsumerGroupInstance(consumerGroupInstance string, topic *mq_pb.Topic) {
|
2023-12-28 19:56:37 +00:00
|
|
|
cg.onConsumerGroupInstanceChange()
|
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
|
2023-12-28 19:56:37 +00: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-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
func (cg *ConsumerGroup) OnPartitionListChange() {
|
2023-12-28 19:56:37 +00:00
|
|
|
if cg.reBalanceTimer != nil {
|
|
|
|
cg.reBalanceTimer.Stop()
|
|
|
|
cg.reBalanceTimer = nil
|
|
|
|
}
|
|
|
|
cg.Rebalance()
|
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
|
2023-12-28 19:56:37 +00:00
|
|
|
func (cg *ConsumerGroup) Rebalance() {
|
|
|
|
println("rebalance...")
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|