EAHITechnology commented on a change in pull request #696:
URL: https://github.com/apache/pulsar-client-go/pull/696#discussion_r784657424



##########
File path: pulsar/reader_partition.go
##########
@@ -0,0 +1,142 @@
+package pulsar
+
+import (
+       "sync"
+       "time"
+)
+
+func (r *reader) internalTopicReadToPartitions() error {
+       partitions, err := r.client.TopicPartitions(r.topic)
+       if err != nil {
+               return err
+       }
+
+       oldNumPartitions, newNumPartitions := 0, len(partitions)
+
+       r.Lock()
+       defer r.Unlock()
+
+       oldReaders, oldNumPartitions := r.consumers, len(r.consumers)
+       if oldReaders != nil {
+               if oldNumPartitions == newNumPartitions {
+                       r.log.Debug("Number of partitions in topic has not 
changed")
+                       return nil
+               }
+
+               r.log.WithField("old_partitions", oldNumPartitions).
+                       WithField("new_partitions", newNumPartitions).
+                       Info("Changed number of partitions in topic")
+       }
+
+       r.consumers = make([]*partitionConsumer, newNumPartitions)
+
+       // When for some reason (eg: forced deletion of sub partition) causes 
oldNumPartitions> newNumPartitions,
+       // we need to rebuild the cache of new consumers, otherwise the array 
will be out of bounds.
+       if oldReaders != nil && oldNumPartitions < newNumPartitions {
+               // Copy over the existing consumer instances
+               for i := 0; i < oldNumPartitions; i++ {
+                       r.consumers[i] = oldReaders[i]
+               }
+       }
+
+       type ConsumerError struct {
+               err       error
+               partition int
+               consumer  *partitionConsumer
+       }
+
+       subscriptionName := r.options.SubscriptionRolePrefix
+       if subscriptionName == "" {
+               subscriptionName = "reader"
+       }
+       subscriptionName += "-" + generateRandomName()
+
+       startMessageID, ok := toTrackingMessageID(r.options.StartMessageID)
+       if !ok {
+               // a custom type satisfying MessageID may not be a messageID or 
trackingMessageID
+               // so re-create messageID using its data
+               deserMsgID, err := 
deserializeMessageID(r.options.StartMessageID.Serialize())
+               if err != nil {
+                       return err
+               }
+               // de-serialized MessageID is a messageID
+               startMessageID = trackingMessageID{
+                       messageID:    deserMsgID.(messageID),
+                       receivedTime: time.Now(),
+               }
+       }
+
+       startPartition := oldNumPartitions
+       partitionsToAdd := newNumPartitions - oldNumPartitions
+
+       if partitionsToAdd < 0 {
+               partitionsToAdd = newNumPartitions
+               startPartition = 0
+       }
+
+       var wg sync.WaitGroup
+       ch := make(chan ConsumerError, partitionsToAdd)
+       wg.Add(partitionsToAdd)
+
+       for partitionIdx := startPartition; partitionIdx < newNumPartitions; 
partitionIdx++ {
+               partitionTopic := partitions[partitionIdx]
+
+               go func(idx int, pt string) {
+                       defer wg.Done()
+
+                       opts := &partitionConsumerOpts{
+                               topic:                      pt,
+                               consumerName:               r.options.Name,
+                               subscription:               subscriptionName,
+                               subscriptionType:           Exclusive,
+                               partitionIdx:               idx,
+                               receiverQueueSize:          
r.options.ReceiverQueueSize,
+                               nackRedeliveryDelay:        
defaultNackRedeliveryDelay,
+                               metadata:                   
r.options.Properties,
+                               replicateSubscriptionState: false,
+                               startMessageID:             startMessageID,
+                               subscriptionMode:           durable,

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to