-
Notifications
You must be signed in to change notification settings - Fork 368
Add parition reader to solve the Reader fragmented read data. #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
476b01d
b088300
7d393c6
82b3e45
8ad69ab
576f944
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package pulsar | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add license header for new file?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| ReaderSubNamePrefix = "reader" | ||
| ) | ||
|
|
||
| func (r *reader) internalTopicReadToPartitions() error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this function doing I'm a little confused by the name. Also, this looks like a lot of duplication code from the consumer partitions is there any way to refactor/combine in to helper functions to avoid duplication?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to issues, we need to subscribe to partition_consumer separately. The meaning of this function is to convert topic into partition_topic for subscription
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's more difficult is that reader requires a lot of specific parameters. After thinking about it, I decided to encapsulate directly. If not, this will swell the member variables of the reader.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, the Reader will process startMessageID, but the consumer will not. |
||
| 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 | ||
| } | ||
|
|
||
| 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: ReaderSubNamePrefix + "-" + generateRandomName(), | ||
| subscriptionType: Exclusive, | ||
| partitionIdx: idx, | ||
| receiverQueueSize: r.options.ReceiverQueueSize, | ||
| nackRedeliveryDelay: defaultNackRedeliveryDelay, | ||
| metadata: r.options.Properties, | ||
| replicateSubscriptionState: false, | ||
| startMessageID: startMessageID, | ||
| subscriptionMode: nonDurable, | ||
| readCompacted: r.options.ReadCompacted, | ||
| decryption: r.options.Decryption, | ||
| } | ||
|
|
||
| cons, err := newPartitionConsumer(nil, r.client, opts, r.messageCh, r.dlq, r.metrics) | ||
| ch <- ConsumerError{ | ||
| err: err, | ||
| partition: idx, | ||
| consumer: cons, | ||
| } | ||
| }(partitionIdx, partitionTopic) | ||
| } | ||
|
|
||
| go func() { | ||
| wg.Wait() | ||
| close(ch) | ||
| }() | ||
|
|
||
| for ce := range ch { | ||
| if ce.err != nil { | ||
| err = ce.err | ||
| } else { | ||
| r.consumers[ce.partition] = ce.consumer | ||
| } | ||
| } | ||
|
|
||
| if err != nil { | ||
| // Since there were some failures, | ||
| // cleanup all the partitions that succeeded in creating the consumer | ||
| for _, c := range r.consumers { | ||
| if c != nil { | ||
| c.Close() | ||
| } | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| if newNumPartitions < oldNumPartitions { | ||
| r.metrics.ConsumersPartitions.Set(float64(newNumPartitions)) | ||
| } else { | ||
| r.metrics.ConsumersPartitions.Add(float64(partitionsToAdd)) | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could there be an invalid partition (out of range) index here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?The number of Consumers is determined by the TopicPartitions() function. If there is a problem here, then the Patition_Consumer is also problematic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cckellogg