Skip to content

Commit

Permalink
Fix the partition number not equals expected error (apache#9446)
Browse files Browse the repository at this point in the history
Fixes apache#8000

### Motivation

Fix the partition number not equals expected error

### Verifying this change

New tests added, without this fix, you can see errors like 
`topics consumer java.lang.IllegalStateException: allTopicPartitionsNumber 2 not equals expected: 5`
  • Loading branch information
codelipenghui authored Feb 5, 2021
1 parent 416ef28 commit bbce00a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.pulsar.common.policies.data.SubscriptionStats;
import org.apache.pulsar.common.policies.data.TenantInfo;
import org.apache.pulsar.common.policies.data.TopicStats;
import org.awaitility.Awaitility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
Expand Down Expand Up @@ -1219,4 +1220,48 @@ public void testSubscriptionMustCompleteWhenOperationTimeoutOnMultipleTopics() t
}
}

@Test(timeOut = testTimeout)
public void testPartitionsUpdatesForMultipleTopics() throws Exception {
final String topicName0 = "persistent://public/default/testPartitionsUpdatesForMultipleTopics-0";
final String subName = "my-sub";
admin.topics().createPartitionedTopic(topicName0, 2);
assertEquals(admin.topics().getPartitionedTopicMetadata(topicName0).partitions, 2);

PatternMultiTopicsConsumerImpl<String> consumer = (PatternMultiTopicsConsumerImpl<String>) pulsarClient.newConsumer(Schema.STRING)
.topicsPattern("persistent://public/default/test.*")
.subscriptionType(SubscriptionType.Failover)
.subscriptionName(subName)
.subscribe();

Assert.assertEquals(consumer.getPartitionsOfTheTopicMap(), 2);
Assert.assertEquals(consumer.allTopicPartitionsNumber.intValue(), 2);

admin.topics().updatePartitionedTopic(topicName0, 5);
consumer.getPartitionsAutoUpdateTimeout().task().run(consumer.getPartitionsAutoUpdateTimeout());

Awaitility.await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> {
Assert.assertEquals(consumer.getPartitionsOfTheTopicMap(), 5);
Assert.assertEquals(consumer.allTopicPartitionsNumber.intValue(), 5);
});

final String topicName1 = "persistent://public/default/testPartitionsUpdatesForMultipleTopics-1";
admin.topics().createPartitionedTopic(topicName1, 3);
assertEquals(admin.topics().getPartitionedTopicMetadata(topicName1).partitions, 3);

consumer.getRecheckPatternTimeout().task().run(consumer.getRecheckPatternTimeout());

Awaitility.await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> {
Assert.assertEquals(consumer.getPartitionsOfTheTopicMap(), 8);
Assert.assertEquals(consumer.allTopicPartitionsNumber.intValue(), 8);
});

admin.topics().updatePartitionedTopic(topicName1, 5);
consumer.getPartitionsAutoUpdateTimeout().task().run(consumer.getPartitionsAutoUpdateTimeout());

Awaitility.await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> {
Assert.assertEquals(consumer.getPartitionsOfTheTopicMap(), 10);
Assert.assertEquals(consumer.allTopicPartitionsNumber.intValue(), 10);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,11 @@ public List<ConsumerImpl<T>> getConsumers() {
return consumers.values().stream().collect(Collectors.toList());
}

// get all partitions that in the topics map
int getPartitionsOfTheTopicMap() {
return topics.values().stream().mapToInt(Integer::intValue).sum();
}

@Override
public void pause() {
synchronized (pauseMutex) {
Expand Down Expand Up @@ -1246,7 +1251,8 @@ private CompletableFuture<Void> subscribeIncreasedTopicPartitions(String topicNa
future.complete(null);
return future;
} else if (oldPartitionNumber < currentPartitionNumber) {
allTopicPartitionsNumber.compareAndSet(oldPartitionNumber, currentPartitionNumber);
allTopicPartitionsNumber.addAndGet(currentPartitionNumber - oldPartitionNumber);
topics.put(topicName, currentPartitionNumber);
List<String> newPartitions = list.subList(oldPartitionNumber, currentPartitionNumber);
// subscribe new added partitions
List<CompletableFuture<Consumer<T>>> futureList = newPartitions
Expand Down

0 comments on commit bbce00a

Please sign in to comment.