refactor
ToDo 1. handle unassignment on client side 2. get notified of completed unassignment 3. in consumer_group.go, balance and send unassignment
This commit is contained in:
@@ -30,37 +30,42 @@ func (sub *TopicSubscriber) startProcessors() {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
semaphore := make(chan struct{}, sub.SubscriberConfig.MaxPartitionCount)
|
semaphore := make(chan struct{}, sub.SubscriberConfig.MaxPartitionCount)
|
||||||
|
|
||||||
for assigned := range sub.brokerPartitionAssignmentChan {
|
for message := range sub.brokerPartitionAssignmentChan {
|
||||||
wg.Add(1)
|
if assigned := message.GetAssignment(); assigned != nil {
|
||||||
semaphore <- struct{}{}
|
wg.Add(1)
|
||||||
|
semaphore <- struct{}{}
|
||||||
|
|
||||||
topicPartition := topic.FromPbPartition(assigned.Partition)
|
topicPartition := topic.FromPbPartition(assigned.PartitionAssignment.Partition)
|
||||||
|
|
||||||
// wait until no covering partition is still in progress
|
// wait until no covering partition is still in progress
|
||||||
sub.waitUntilNoOverlappingPartitionInFlight(topicPartition)
|
sub.waitUntilNoOverlappingPartitionInFlight(topicPartition)
|
||||||
|
|
||||||
// start a processors
|
// start a processors
|
||||||
sub.activeProcessorsLock.Lock()
|
sub.activeProcessorsLock.Lock()
|
||||||
sub.activeProcessors[topicPartition] = &ProcessorState{}
|
sub.activeProcessors[topicPartition] = &ProcessorState{}
|
||||||
sub.activeProcessorsLock.Unlock()
|
sub.activeProcessorsLock.Unlock()
|
||||||
|
|
||||||
go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) {
|
go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) {
|
||||||
defer func() {
|
defer func() {
|
||||||
sub.activeProcessorsLock.Lock()
|
sub.activeProcessorsLock.Lock()
|
||||||
delete(sub.activeProcessors, topicPartition)
|
delete(sub.activeProcessors, topicPartition)
|
||||||
sub.activeProcessorsLock.Unlock()
|
sub.activeProcessorsLock.Unlock()
|
||||||
|
|
||||||
<-semaphore
|
<-semaphore
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
|
glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
|
||||||
err := sub.onEachPartition(assigned)
|
err := sub.onEachPartition(assigned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.V(0).Infof("subscriber %s/%s partition %+v at %v: %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker, err)
|
glog.V(0).Infof("subscriber %s/%s partition %+v at %v: %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker, err)
|
||||||
} else {
|
} else {
|
||||||
glog.V(0).Infof("subscriber %s/%s partition %+v at %v completed", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
|
glog.V(0).Infof("subscriber %s/%s partition %+v at %v completed", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
|
||||||
}
|
}
|
||||||
}(assigned, topicPartition)
|
}(assigned.PartitionAssignment, topicPartition)
|
||||||
|
}
|
||||||
|
if unAssignment := message.GetUnAssignment(); unAssignment != nil {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ type OnCompletionFunc func()
|
|||||||
type TopicSubscriber struct {
|
type TopicSubscriber struct {
|
||||||
SubscriberConfig *SubscriberConfiguration
|
SubscriberConfig *SubscriberConfiguration
|
||||||
ContentConfig *ContentConfiguration
|
ContentConfig *ContentConfiguration
|
||||||
brokerPartitionAssignmentChan chan *mq_pb.BrokerPartitionAssignment
|
brokerPartitionAssignmentChan chan *mq_pb.SubscriberToSubCoordinatorResponse
|
||||||
OnEachMessageFunc OnEachMessageFunc
|
OnEachMessageFunc OnEachMessageFunc
|
||||||
OnCompletionFunc OnCompletionFunc
|
OnCompletionFunc OnCompletionFunc
|
||||||
bootstrapBrokers []string
|
bootstrapBrokers []string
|
||||||
@@ -42,7 +42,7 @@ func NewTopicSubscriber(bootstrapBrokers []string, subscriber *SubscriberConfigu
|
|||||||
return &TopicSubscriber{
|
return &TopicSubscriber{
|
||||||
SubscriberConfig: subscriber,
|
SubscriberConfig: subscriber,
|
||||||
ContentConfig: content,
|
ContentConfig: content,
|
||||||
brokerPartitionAssignmentChan: make(chan *mq_pb.BrokerPartitionAssignment, 1024),
|
brokerPartitionAssignmentChan: make(chan *mq_pb.SubscriberToSubCoordinatorResponse, 1024),
|
||||||
bootstrapBrokers: bootstrapBrokers,
|
bootstrapBrokers: bootstrapBrokers,
|
||||||
waitForMoreMessage: true,
|
waitForMoreMessage: true,
|
||||||
activeProcessors: make(map[topic.Partition]*ProcessorState),
|
activeProcessors: make(map[topic.Partition]*ProcessorState),
|
||||||
|
|||||||
@@ -131,27 +131,24 @@ func (cg *ConsumerGroup) BalanceConsumerGroupInstances(knownPartitionSlotToBroke
|
|||||||
partitionSlots = make([]*PartitionSlotToConsumerInstance, 0)
|
partitionSlots = make([]*PartitionSlotToConsumerInstance, 0)
|
||||||
}
|
}
|
||||||
consumerGroupInstance.Partitions = ToPartitions(partitionSlotToBrokerList.RingSize, partitionSlots)
|
consumerGroupInstance.Partitions = ToPartitions(partitionSlotToBrokerList.RingSize, partitionSlots)
|
||||||
assignedPartitions := make([]*mq_pb.BrokerPartitionAssignment, len(partitionSlots))
|
for _, partitionSlot := range partitionSlots {
|
||||||
for i, partitionSlot := range partitionSlots {
|
consumerGroupInstance.ResponseChan <- &mq_pb.SubscriberToSubCoordinatorResponse{
|
||||||
assignedPartitions[i] = &mq_pb.BrokerPartitionAssignment{
|
Message: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment_{
|
||||||
Partition: &mq_pb.Partition{
|
Assignment: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment{
|
||||||
RangeStop: partitionSlot.RangeStop,
|
PartitionAssignment: &mq_pb.BrokerPartitionAssignment{
|
||||||
RangeStart: partitionSlot.RangeStart,
|
Partition: &mq_pb.Partition{
|
||||||
RingSize: partitionSlotToBrokerList.RingSize,
|
RangeStop: partitionSlot.RangeStop,
|
||||||
UnixTimeNs: partitionSlot.UnixTimeNs,
|
RangeStart: partitionSlot.RangeStart,
|
||||||
|
RingSize: partitionSlotToBrokerList.RingSize,
|
||||||
|
UnixTimeNs: partitionSlot.UnixTimeNs,
|
||||||
|
},
|
||||||
|
LeaderBroker: partitionSlot.Broker,
|
||||||
|
FollowerBroker: partitionSlot.FollowerBroker,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
LeaderBroker: partitionSlot.Broker,
|
|
||||||
FollowerBroker: partitionSlot.FollowerBroker,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response := &mq_pb.SubscriberToSubCoordinatorResponse{
|
|
||||||
Message: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment_{
|
|
||||||
Assignment: &mq_pb.SubscriberToSubCoordinatorResponse_Assignment{
|
|
||||||
PartitionAssignments: assignedPartitions,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
consumerGroupInstance.ResponseChan <- response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -183,11 +183,14 @@ message SubscriberToSubCoordinatorRequest {
|
|||||||
}
|
}
|
||||||
message SubscriberToSubCoordinatorResponse {
|
message SubscriberToSubCoordinatorResponse {
|
||||||
message Assignment {
|
message Assignment {
|
||||||
int64 generation = 1;
|
BrokerPartitionAssignment partition_assignment = 1;
|
||||||
repeated BrokerPartitionAssignment partition_assignments = 2;
|
}
|
||||||
|
message UnAssignment {
|
||||||
|
Partition partition = 1;
|
||||||
}
|
}
|
||||||
oneof message {
|
oneof message {
|
||||||
Assignment assignment = 1;
|
Assignment assignment = 1;
|
||||||
|
UnAssignment un_assignment = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user